17 int show_block(
size_t start,
size_t size,
unsigned char* buf);
19 int show_char(
size_t start,
size_t size,
unsigned char* buf);
20 int swap_block(
size_t size,
size_t nbytes,
unsigned char* buf);
21 int show_table(
size_t start,
size_t size,
unsigned char* buf);
22 int show_structure(
size_t start,
size_t size,
int swap,
char* strdef,
unsigned char* buf);
23 int poke_value(fstream* fin,
size_t loc,
char typechar,
double value);
24 int find_string(
const char*
string,
size_t start,
size_t size,
unsigned char* buf);
25 int replace(fstream* fin,
char* rstring1,
char* rstring2);
31 "Usage: bfile [options] infile [outfile]",
32 "---------------------------------------",
33 "Examines and modifies binary files.",
36 "-blocks Character value blocks (16 per line).",
37 "-hexadecimal Character hexadecimal value blocks (16 per line).",
38 "-characters Character blocks (64 per line).",
39 "-format bbsif Output formatted according to a sequence of data types.",
40 "-find astring Find the given string.",
41 "-poke 3124,20.4,f Poking at a location a value of a specific data type.",
42 "-replace \"\\n,\\r\" Replace all occurrences of the first string by the second.",
45 "-verbose 1 Verbosity of output.",
46 "-bytes 128 Number of bytes to read (default 64).",
47 "-start 646 Starting position to read from (default 0).",
48 "-reverse 2 Reverse byte order for (2) bytes.",
49 "-VAX Convert VAX floating point numbers.",
52 " b: byte or unsigned char (1 byte)",
53 " c: signed char (1 byte)",
54 " u: word or unsigned short (2 bytes)",
55 " s: short (2 bytes)",
56 " j: unsigned integer (4 bytes)",
57 " i: integer (4 bytes)",
58 " f: float (4 bytes)",
59 " d: double (8 bytes)",
64 int main(
int argc,
char **argv)
74 char* strdef =
new char[128];
80 char rstring1[200] =
"";
81 char rstring2[200] =
"";
87 for ( curropt = option; curropt; curropt = curropt->
next ) {
91 if ( curropt->
tag ==
"VAX" ) setvax = 1;
92 if ( curropt->
tag ==
"blocks" ) setblock = 1;
93 if ( curropt->
tag ==
"hexadecimal" ) sethex = 1;
94 if ( curropt->
tag ==
"characters" ) setchar = 1;
95 if ( curropt->
tag ==
"format" ) sscanf(curropt->
value.
c_str(),
"%s", strdef);
96 if ( curropt->
tag ==
"find" ) find = curropt->
value;
97 if ( curropt->
tag ==
"poke" ) sscanf(curropt->
value.
c_str(),
"%ld,%lf,%c", &loc, &pokevalue, &typechar);
98 if ( curropt->
tag ==
"replace" ) {
100 if ( strlen(rstring1) < 1 )
101 cerr <<
"-replace: Two strings must be specified!" << endl;
103 i = strcspn(rstring1,
",");
104 strcpy(rstring2, rstring1+i+1);
111 fstream* fin =
new fstream(argv[optind], fstream::in | fstream::out);
113 cerr <<
"Error: File " << argv[optind] <<
" not read!" << endl << endl;
117 if ( strlen(rstring1) > 0 ) {
118 replace(fin, rstring1, rstring2);
128 fin->seekg (0, ios::end);
129 size_t fsize = (size_t)fin->tellg() - start;
130 if ( buflen > fsize ) buflen = fsize;
132 size_t size = buflen*
sizeof(
unsigned char);
133 unsigned char* buf =
new unsigned char[size];
135 fin->seekg (start, ios::beg);
136 fin->read((
char *)buf, size);
155 if ( strlen(strdef) ) {
168 cout <<
"Getting VAX floating point" << endl;
169 for ( i=0; i<size; i+=4 )
vax2ieee((
unsigned char *)(buf+i), 0);
176 if ( optind < argc ) {
177 ofstream fout(argv[optind]);
179 cerr <<
"Error: Unable to open " << argv[optind] << endl;
182 fout.write((
char *)buf, size);
191 int show_block(
size_t start,
size_t size,
unsigned char* buf)
195 for ( i=0; i<size; i+=16 ) {
196 cout << setw(10) << i+start <<
": ";
197 for ( j=i; j<i+16; j++ ) cout << setw(4) << (int) buf[j];
199 for ( j=i; j<i+16; j++ ) {
200 if ( isprint(buf[j]) ) cout << buf[j];
213 for ( i=0; i<size; i+=16 ) {
214 cout << setw(10) << i+start <<
": ";
215 for ( j=i; j<i+16; j++ ) cout << hex << setw(3) << (int) buf[j];
217 for ( j=i; j<i+16; j++ ) {
218 if ( isprint(buf[j]) ) cout << buf[j];
227 int show_char(
size_t start,
size_t size,
unsigned char* buf)
231 for ( i=0; i<size; i+=64 ) {
232 cout << setw(10) << i+start <<
": ";
233 for ( j=i; j<i+64; j++ ) {
234 if ( isprint(buf[j]) ) cout << buf[j];
243 int swap_block(
size_t size,
size_t nbytes,
unsigned char* buf)
248 cout <<
"Swapping byte order for " << nbytes <<
"-byte types" << endl;
249 for ( i=0; i<size; i+=nbytes ) {
250 memcpy(t, buf+i, nbytes);
251 for( j=0; j<nbytes; j++ ) buf[i+j] = t[nbytes-1-j];
257 int show_table(
size_t start,
size_t size,
unsigned char* buf)
259 unsigned char bits[9], thechar;
261 short* inshort = (
short *) buf;
262 int* inint = (
int *) buf;
263 long* inlong = (
long *) buf;
264 float* infloat = (
float *) buf;
265 double* indouble = (
double *) buf;
267 cout <<
" loc char bits char short int long float double" << endl;
268 for ( i=0; i<size; i++ ) {
269 for ( j=0; j<8; j++) {
271 if ( ( buf[i] << j ) & 0x80 )
275 thechar = (
unsigned char)buf[i];
276 if ( !isprint(thechar) ) thechar =
' ';
277 cout << setw(10) << i+start << setw(4) << thechar << setw(10) << bits << setw(5) << (int)buf[i];
278 if ( i%2 == 0 ) cout << setw(8) << inshort[i/2];
279 if ( i%4 == 0 ) cout << setw(10) << inint[i/4];
280 if ( i%4 == 0 ) cout << setw(24) << infloat[i/4];
281 if ( i%8 == 1 ) cout << setw(36) << inlong[i/8];
282 if ( i%8 == 1 ) cout << setw(24) << indouble[i/8];
289 int show_structure(
size_t start,
size_t size,
int swap,
char* strdef,
unsigned char* buf)
292 unsigned char* uchar;
294 unsigned short* ushort;
295 signed short* sshort;
300 cout <<
"loc\ttype\tvalue" << endl;
303 switch ( strdef[j] ) {
305 uchar = (
unsigned char *) (buf + i);
306 cout << i+start <<
tab << strdef[j] <<
tab << *uchar << endl;
310 schar = (
signed char *) (buf + i);
311 cout << i+start <<
tab << strdef[j] <<
tab << *schar << endl;
315 ushort = (
unsigned short *) (buf + i);
316 if ( swap )
swapbytes((
unsigned char *) ushort,
sizeof(
short));
317 cout << i+start <<
tab << strdef[j] <<
tab << *ushort << endl;
321 sshort = (
short *) (buf + i);
322 if ( swap )
swapbytes((
unsigned char *) sshort,
sizeof(
short));
323 cout << i+start <<
tab << strdef[j] <<
tab << *sshort << endl;
327 integer = (
int *) (buf + i);
328 if ( swap )
swapbytes((
unsigned char *) integer,
sizeof(
int));
329 cout << i+start <<
tab << strdef[j] <<
tab << *integer << endl;
333 floating = (
float *) (buf + i);
334 if ( swap )
swapbytes((
unsigned char *) floating,
sizeof(
float));
335 cout << i+start <<
tab << strdef[j] <<
tab << *floating << endl;
339 doublefloat = (
double *) (buf + i);
340 if ( swap )
swapbytes((
unsigned char *) doublefloat,
sizeof(
double));
341 cout << i+start <<
tab << strdef[j] <<
tab << *doublefloat << endl;
347 if ( j > strlen(strdef) ) j = 0;
353 int find_string(
const char*
string,
size_t start,
size_t size,
unsigned char* buf)
355 size_t i, j, n(0), ssize = strlen(
string);
357 cout <<
"Searching for: " <<
string << endl;
358 for ( i=0; i<size - ssize + 1; i++, buf++ ) {
359 if ( strncasecmp(
string, (
char *)buf, ssize) == 0 ) {
360 cout << setw(10) << i+start <<
":";
361 for ( j=0; j<64; j++ ) cout << buf[j];
366 cout <<
"Found: " << n << endl;
371 int poke_value(fstream* fin,
size_t loc,
char typechar,
double value)
373 unsigned char bval = (
unsigned char)value;
374 signed char cval = (
signed char)value;
375 unsigned short uval = (
unsigned short)value;
376 short sval = (short)value;
377 int ival = (int)value;
378 double dval = (double)value;
380 fin->seekg(loc, ios::beg);
381 switch ( typechar ) {
383 fin->write((
char *)&bval,
sizeof(char));
386 fin->write((
char *)&cval,
sizeof(char));
389 fin->write((
char *)&uval,
sizeof(short));
392 fin->write((
char *)&sval,
sizeof(short));
395 fin->write((
char *)&ival,
sizeof(int));
398 fin->write((
char *)&value,
sizeof(float));
401 fin->write((
char *)&dval,
sizeof(double));
409 int replace(fstream* fin,
char* rstring1,
char* rstring2)
411 size_t i, j, m, n, nr(0);
417 cout <<
"Replacing \"" << rstring1 <<
"\" with \"" << rstring2 <<
"\"" << endl;
421 if ( strlen(fromstr) < 1 ) {
422 cerr <<
"Error: The search string has zero length!" << endl;
426 fin->seekg(0, ios::end);
427 size_t filesize = fin->tellg();
428 fin->seekg(0, ios::beg);
431 cout <<
"File size: " << filesize << endl;
433 char* buf =
new char[filesize];
434 char* newbuf =
new char[filesize];
436 fin->read(buf, filesize);
441 while ( i < filesize ) {
442 if ( memcmp(buf+i, fromstr, m) == 0 ) {
443 memcpy(newbuf+j, tostr, n);
454 fin->seekg(0, ios::beg);
456 fin->write(newbuf, filesize);
461 cout <<
"Number of replacements: " << nr << endl << endl;
468 size_t i, j, setcntl(0);
469 char* bytes =
new char[strlen(
string)+1];
472 for ( i=0; i<strlen(
string); i++ ) {
475 if (
string[i] ==
'a' ) bytes[j] = 7;
476 if (
string[i] ==
'b' ) bytes[j] = 8;
477 if (
string[i] ==
't' ) bytes[j] = 9;
478 if (
string[i] ==
'n' ) bytes[j] = 10;
479 if (
string[i] ==
'v' ) bytes[j] = 11;
480 if (
string[i] ==
'f' ) bytes[j] = 12;
481 if (
string[i] ==
'r' ) bytes[j] = 13;
482 if (
string[i] ==
'\\' ) bytes[j] = 28;
485 }
else if (
string[i] ==
'\\' ) {
488 bytes[j] =
string[i];
502 for ( i=0; i<strlen(
string); i++ )
if (
string[i] !=
'\\' ) n++;