243
Dump
- #include "dump.h"
- #include <cctype>
- using namespace std;
- void dump( unsigned char *buf, unsigned size, ostream &os ) {
- int i, n, col = 0, line = 0;
- const unsigned ncols = 16;
- unsigned char c[ 16 ];
- for ( n = 0; n < size; n++ ) {
- if( col == 0 ) {
- os.fill( '0' );
- os.width( 4 );
- os.setf( ios::hex, ios::basefield );
- os << line * ncols << ' ';
- line++;
- }
- os.fill( '0' );
- os.width( 2 );
- os.setf( ios::hex, ios::basefield );
- os << (unsigned)buf[ n ] << ' ';
- c[ col ] = buf[ n ];
- col++;
- if ( col == ncols / 2 )
- os << ' ';
- else if ( col == ncols ) {
- for ( i = 0; i < col; i++ )
- os << (isprint( c[ i ] ) ? (char)c[ i ] : '.');
- os << '\n';
- col = 0;
- }
- }
- if ( col != 0 ) {
- for ( i = ncols - col; i > 0; i-- )
- os << " ";
- if ( col < ncols / 2 )
- os << ' ';
- for ( i = 0; i < col; i++ )
- os << (isprint( c[ i ] ) ? (char)c[ i ] : '.');
- os << '\n';
- }
- }
- #if defined( STANDALONE )
- #include <fstream>
- main( int argc, char **argv ) {
- char buf[ 4096 ];
- ifstream fin;
- using namespace std;
- if( argc != 2 ) {
- cerr << argv[ 0 ] << " file\n";
- exit( 1 );
- }
- fin.open( argv[ 1 ], ios::in | ios::binary );
- if ( !fin ) {
- cout << argv[ 1 ] << "?\n";
- exit( 2 );
- }
- fin.read( buf, sizeof (buf) );
- while ( fin.gcount() ) {
- dump( (unsigned char *)buf, fin.gcount(), cout );
- fin.read( buf, sizeof (buf) );
- }
- exit( 0 );
- }
- #endif
Commentaires