4
Memory dump
- extern void dump( unsigned char *buf, size_t size, FILE *stream );
- void dump( unsigned char *buf, size_t size, FILE *stream ) {
- register int n, i, col = 0, line = 0;
- const int ncols = 16;
- unsigned char c[ 16 ];
- for ( n = 0; n < size; n++ ) {
- if ( col == 0 ) {
- fprintf( stream, "%04d ", line * ncols );
- line++;
- }
- fprintf( stream, "%02x ", buf[ n ] );
- c[ col ] = buf[ n ];
- col++;
- if ( col == ncols / 2 )
- fprintf( stream, " " );
- else if ( col == ncols ) {
- for ( i = 0; i < col; i++ )
- fprintf( stream, "%c", isprint(c[ i ] ) ? c[ i ] : '.' );
- fprintf( stream, "\n" );
- col = 0;
- }
- }
- if ( col != 0 ) {
- for ( i = ncols - col; i > 0; i-- )
- fprintf( stream, " " );
- if ( col < ncols / 2 )
- fprintf( stream, " " );
- for ( i = 0; i < col; i++ )
- fprintf( stream, "%c", isprint( c[ i ] ) ? c[ i ] : '.' );
- fprintf( stream, "\n" );
- }
- }
- #if defined( STANDALONE )
- #include <stdlib.h>
- #include <unistd.h>
- int main( void ) {
- unsigned char buf[ 4096 ];
- int n;
- while ( (n = read( 0, buf, sizeof ( buf ))) )
- dump( buf, n, stdout );
- exit( 0 );
- }
$ gcc -Wall -DDEBUG -DSTANDALONE -o test-dump dump.c
$ test-dump < dump.c
0000 23 69 6e 63 6c 75 64 65 20 22 64 75 6d 70 2e 68 #include "dump.h
0016 22 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 63 74 79 "..#include <cty
0032 70 65 2e 68 3e 0a 0a 76 6f 69 64 20 64 75 6d 70 pe.h>..void dump
0048 28 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 ( unsigned char
0064 2a 62 75 66 2c 20 73 69 7a 65 5f 74 20 73 69 7a *buf, size_t siz
...
Comments