4

Dump

  1. #include "dump.h"
  2.  
  3. #include <cctype>
  4.  
  5. using namespace std;
  6.  
  7. void dump( unsigned char *buf, unsigned size, ostream &os ) {
  8.     int i, n, col = 0, line = 0;
  9.  
  10.     const unsigned ncols = 16;
  11.     unsigned char c[ 16 ];
  12.  
  13.     for ( n = 0; n < size; n++ ) {
  14.          if( col == 0 ) {
  15.             os.fill( '0' );
  16.             os.width( 4 );
  17.             os.setf( ios::hex, ios::basefield );
  18.             os << line * ncols << ' ';
  19.             line++;
  20.         }
  21.         os.fill( '0' );
  22.         os.width( 2 );
  23.         os.setf( ios::hex, ios::basefield );
  24.         os << (unsigned)buf[ n ] << ' ';
  25.         c[ col ] = buf[ n ];
  26.         col++;
  27.         if ( col == ncols / 2 )
  28.             os << ' ';
  29.         else if ( col == ncols ) {
  30.             for ( i = 0; i < col; i++ )
  31.                 os << (isprint( c[ i ] ) ? (char)c[ i ] : '.');
  32.             os << '\n';
  33.             col = 0;
  34.         }
  35.     }
  36.     if ( col != 0 ) {
  37.         for ( i = ncols - col; i > 0; i-- )
  38.             os << "   ";
  39.         if ( col < ncols / 2 )
  40.             os << ' ';
  41.         for ( i = 0; i < col; i++ )
  42.             os << (isprint( c[ i ] ) ? (char)c[ i ] : '.');
  43.         os << '\n';
  44.     }
  45. }
  46.  
  47. #if defined( STANDALONE )
  48.  
  49. #include <fstream>
  50.  
  51. main( int argc, char **argv ) {
  52.     char buf[ 4096 ];
  53.     ifstream fin;
  54.  
  55.     using namespace std;
  56.  
  57.     if( argc != 2 ) {
  58.         cerr << argv[ 0 ] << " file\n";
  59.         exit( 1 );
  60.     }
  61.  
  62.     fin.open( argv[ 1 ], ios::in | ios::binary );
  63.     if ( !fin ) {
  64.         cout << argv[ 1 ] << "?\n";
  65.         exit( 2 );
  66.     }
  67.     fin.read( buf, sizeof (buf) );
  68.     while ( fin.gcount() ) {
  69.         dump( (unsigned char *)buf, fin.gcount(), cout );
  70.         fin.read( buf, sizeof (buf) );
  71.     }
  72.     exit( 0 );
  73. }
  74.  
  75. #endif

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].