Introduction
This program can dump a file (either a binary file or a text file) to standard output in a DOS window in Windows. It is a Windows edition for the famous od program in Unix/Linux. It is also a free program that implements a main function like a similar program in Windows, UltraEdit. It can dump a file in 10 different data types.
Implement
This is a simple C program which includes two sub-programs: wod.c and dump2stdout.c. The main program, wod.c, mainly gets user input parameters (dump data type) and a character stream from a file. Then, it transfers these parameters to a sub-program dump2stdout.c. It gets the dump data type predefined in the header file wod.h, which is an enum
data structure as follows:
Enum { OD_CHAR, OD_SHORT_INT, OD_LONG, OD_D_CHAR, OD_D_SHORT,
OD_D_LONG, OD_ASCII, OD_BIN, OD_FLOAT, OD_DOUBLE } ;
For some potential improvement reasons, the main program divides the whole file into fixed length segments and a tail segment:
Fixed segment length : blk_size = 1024 ( bytes )
Tail segment length : tail_len = file_len % blk_size ;
Segments count : blk_no = file_len / blk_size ;
There is a wired "tell(fd)
" in wod.c. I know "read
" should move the file pointer forward by one byte, but it seems it does not always work, so I put this redundant function here. For transforming between different data types, I have defined a UNION
data structure in wod.h as follows:
Union buf_union {
unsigned char ubuf [ MAX_BLOCK_SIZE ] ;
unsigned short int sint [ MAX_BLOCK_SIZE / 2 ] ;
unsigned long lint [ MAX_BLOCK_SIZE / 4 ] ;
float ieeef [ MAX_BLOCK_SIZE / 4 ] ;
double ieeed [ MAX_BLOCK_SIZE / 8 ] ;
} ;
In the sub-program dump2stdout.c, tmpc[16]
is used as bit mask in dumping binary, and asc_buf[16]
is used for storing temporary ASCII strings in one row. It is the most popular and useful function to dump ASCII. There are three columns in the ASCII dump. The first is the HEX address column, the second is the HEX characters column, and the third is ASCII characters. The variable start_poi
is the start offset position of each segment.
case OD_ASCII:
for ( i = 0 ; i < block_size ; i++ ) {
if ( i % 16 == 0 ) {
j = 0 ; if ( i > 0 ) printf ( " %.16s\n", asc_buf ) ;
printf ( "%.8X: ", i + start_poi ) ;
memset ( asc_buf, '\0', sizeof ( asc_buf ) ) ;
}
printf ( "%.2x ", buf [ i ] ) ;
if ( buf [ i ] >= 0x7F || buf [ i ] < 0x20 )
buf [ i ] = '.' ;
asc_buf [ j++ ] = buf [ i ] ;
}
l = strlen ( ( const char * ) asc_buf ) ;
if ( l < 16 ) {
for ( m = 0 ; m < 3 * ( 16 - l ) ; m++ ) printf (" ") ;
}
printf ( " %.16s", asc_buf ) ;
break ;
Compile
This program was compiled in MinGW environment in Windows XP. You have to set the PATH environment variable, or run setp.bat before compiling.
set PATH = %PATH%; C:\MinGW\bin ;
Then, run ccm.bat, or manually compile the program as follows:
g++ -c dump2stdout.c
g++ -c wod.c
g++ wod.o dump2stdout.o -o wod.exe
After all, you can run the program in a DOS window. You can alos redirect the output to a text file for convenience, as follows:
wod test.dat > test.txt
Improvement issues
- This pure C program is easy to be transplanted to different platforms.
- You can modify the main file wod.c to make this program more flexible. For example, you can add some run-time parameters as position control parameters, such as start position and end position.
- You can make the program to output to a text file by replacing "
printf
" with "fprintf
". - You can try to use "
lseek64
" and "__off64_t
" or similar functions to make the program support big files. - You can develop a GUI to wrap the tiny program if you are a stubborn win-style programmer.