diff options
| author | David M. Syzdek <david@syzdek.net> | 2010-01-08 06:35:29 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2010-01-08 06:35:29 -0900 |
| commit | 71a580f4ab5d22ab2f8c6977fb52e7bc24d6cc2d (patch) | |
| tree | 5c3b67959236573ffe7abce2f0dcd64e2f6c5c0b /src/bindump.c | |
| parent | 5f4133ec2cf221f9d758507a117b1d476112ae41 (diff) | |
| download | dmstools-71a580f4ab5d22ab2f8c6977fb52e7bc24d6cc2d.tar.gz dmstools-71a580f4ab5d22ab2f8c6977fb52e7bc24d6cc2d.tar.bz2 dmstools-71a580f4ab5d22ab2f8c6977fb52e7bc24d6cc2d.tar.xz dmstools-71a580f4ab5d22ab2f8c6977fb52e7bc24d6cc2d.zip | |
Creating functions for read/displaying data in bindump
Diffstat (limited to 'src/bindump.c')
| -rw-r--r-- | src/bindump.c | 139 |
1 files changed, 97 insertions, 42 deletions
diff --git a/src/bindump.c b/src/bindump.c index a35f29e..40d672e 100644 --- a/src/bindump.c +++ b/src/bindump.c | |||
| @@ -126,7 +126,9 @@ typedef struct bindump_file BinDumpFile; | |||
| 126 | struct bindump_file | 126 | struct bindump_file |
| 127 | { | 127 | { |
| 128 | int fd; ///< file handle of open file | 128 | int fd; ///< file handle of open file |
| 129 | int eof; ///< toggledupon reaching the end-of-file | ||
| 129 | size_t pos; ///< current position within the file | 130 | size_t pos; ///< current position within the file |
| 131 | size_t read; ///< number of bytes read | ||
| 130 | ssize_t code; ///< return code of last file operation | 132 | ssize_t code; ///< return code of last file operation |
| 131 | const char * filename; ///< name of the file | 133 | const char * filename; ///< name of the file |
| 132 | char data[8]; ///< buffer for storing data for processing | 134 | char data[8]; ///< buffer for storing data for processing |
| @@ -148,6 +150,14 @@ int my_close PARAMS((BinDumpFile * file, uint32_t verbose)); | |||
| 148 | // preforms lseek on file | 150 | // preforms lseek on file |
| 149 | int my_lseek PARAMS((BinDumpFile * file, size_t offset, uint32_t verbose)); | 151 | int my_lseek PARAMS((BinDumpFile * file, size_t offset, uint32_t verbose)); |
| 150 | 152 | ||
| 153 | // displays one line 8 byte chunk of data | ||
| 154 | size_t my_print_dump PARAMS((BinDumpFile * file, size_t offset, size_t len, | ||
| 155 | uint32_t opts)); | ||
| 156 | |||
| 157 | // performs read upon file | ||
| 158 | int my_read PARAMS((BinDumpFile * file, size_t offset, size_t len, | ||
| 159 | uint32_t verbose)); | ||
| 160 | |||
| 151 | //displays usage information | 161 | //displays usage information |
| 152 | void my_usage PARAMS((void)); | 162 | void my_usage PARAMS((void)); |
| 153 | 163 | ||
| @@ -171,12 +181,11 @@ int main(int argc, char * argv[]) | |||
| 171 | { | 181 | { |
| 172 | int c; | 182 | int c; |
| 173 | int opt_index; | 183 | int opt_index; |
| 174 | char buff1[9]; | ||
| 175 | size_t offset; | 184 | size_t offset; |
| 176 | size_t offset_mod; | 185 | size_t offset_mod; |
| 177 | size_t offset_div; | 186 | size_t offset_div; |
| 178 | size_t s; | ||
| 179 | size_t len; | 187 | size_t len; |
| 188 | size_t line; | ||
| 180 | uint32_t verbose; | 189 | uint32_t verbose; |
| 181 | BinDumpFile file1; | 190 | BinDumpFile file1; |
| 182 | 191 | ||
| @@ -234,7 +243,6 @@ int main(int argc, char * argv[]) | |||
| 234 | }; | 243 | }; |
| 235 | 244 | ||
| 236 | // determines file to process (or STDIN) | 245 | // determines file to process (or STDIN) |
| 237 | file1.filename = NULL; | ||
| 238 | switch(argc - optind) | 246 | switch(argc - optind) |
| 239 | { | 247 | { |
| 240 | case 1: | 248 | case 1: |
| @@ -276,9 +284,6 @@ int main(int argc, char * argv[]) | |||
| 276 | }; | 284 | }; |
| 277 | }; | 285 | }; |
| 278 | 286 | ||
| 279 | // mark the starting position | ||
| 280 | file1.pos = 0; | ||
| 281 | |||
| 282 | // move to the specified offset | 287 | // move to the specified offset |
| 283 | if (offset) | 288 | if (offset) |
| 284 | { | 289 | { |
| @@ -288,58 +293,33 @@ int main(int argc, char * argv[]) | |||
| 288 | return(1); | 293 | return(1); |
| 289 | }; | 294 | }; |
| 290 | 295 | ||
| 296 | line = 0; | ||
| 297 | |||
| 291 | // fill in white space so the offset markings align with the position in the file | 298 | // fill in white space so the offset markings align with the position in the file |
| 292 | if (verbose > 2) | 299 | if (verbose > 2) |
| 293 | printf("reading data...\n"); | 300 | printf("reading data...\n"); |
| 294 | printf("offset 00 01 02 03 04 05 06 07 \n"); | 301 | printf("offset 00 01 02 03 04 05 06 07 \n"); |
| 295 | if (file1.pos % 8) | 302 | if (offset_mod) |
| 296 | { | 303 | { |
| 297 | printf("0%04zo0:", (file1.pos/8)); | 304 | if ((my_read(&file1, offset_mod, len, verbose) == -1)) |
| 298 | for(s = 0; s < (file1.pos % 8); s++) | ||
| 299 | printf(" "); | ||
| 300 | if ((file1.code = read(file1.fd, file1.data, 8)) == -1) | ||
| 301 | { | ||
| 302 | perror(PROGRAM_NAME ": read()"); | ||
| 303 | my_close(&file1, verbose); | ||
| 304 | if (verbose > 0) | ||
| 305 | printf("read %zu bytes\n", (file1.pos-offset)); | ||
| 306 | return(1); | 305 | return(1); |
| 307 | }; | 306 | line += my_print_dump(&file1, offset_mod, len, 0); |
| 308 | for(s = offset_mod; ((s < 8) && ((s-offset_mod) < ((size_t)file1.code))); s++) | ||
| 309 | printf(" %s", my_byte2str(file1.data[s], buff1)); | ||
| 310 | file1.pos += file1.code; | ||
| 311 | }; | 307 | }; |
| 312 | 308 | ||
| 313 | // read data from file handle | 309 | // read data from file handle |
| 314 | while((file1.code = read(file1.fd, file1.data, 8)) > 0) | 310 | while(!(file1.eof)) |
| 315 | { | 311 | { |
| 316 | if ((file1.pos % 8) == 0) | 312 | if ((my_read(&file1, 0, len, verbose) == -1)) |
| 317 | printf("0%04zo0:", (file1.pos/8)); | 313 | return(1); |
| 318 | for(s = 0; ((s < 8) && (s < ((size_t)file1.code))); s++) | 314 | line += my_print_dump(&file1, 0, (len - (file1.pos-offset)), 0); |
| 319 | printf(" %s", my_byte2str(file1.data[s], buff1)); | 315 | if (!(line % 23)) |
| 320 | file1.pos += file1.code; | ||
| 321 | if (!(file1.pos % 8)) | ||
| 322 | printf("\n"); | ||
| 323 | if ( ((file1.pos-offset) >= len) && (len)) | ||
| 324 | break; | ||
| 325 | if ( (!(file1.pos % 8)) && (!(file1.pos % (8 * 23))) ) | ||
| 326 | printf("offset 00 01 02 03 04 05 06 07 \n"); | 316 | printf("offset 00 01 02 03 04 05 06 07 \n"); |
| 327 | }; | 317 | }; |
| 328 | if (file1.pos % 8) | ||
| 329 | printf("\n"); | ||
| 330 | if (file1.code == -1) | ||
| 331 | { | ||
| 332 | perror(PROGRAM_NAME ": read()"); | ||
| 333 | my_close(&file1, verbose); | ||
| 334 | if (verbose > 0) | ||
| 335 | printf("read %zu bytes\n", (file1.pos-offset)); | ||
| 336 | return(1); | ||
| 337 | }; | ||
| 338 | 318 | ||
| 339 | // close file and finish up | 319 | // close file and finish up |
| 340 | my_close(&file1, verbose); | 320 | my_close(&file1, verbose); |
| 341 | if (verbose > 0) | 321 | if (verbose > 0) |
| 342 | printf("read %zu bytes\n", (file1.pos-offset)); | 322 | printf("read %zu bytes\n", (file1.read)); |
| 343 | printf("\n"); | 323 | printf("\n"); |
| 344 | 324 | ||
| 345 | return(0); | 325 | return(0); |
| @@ -376,6 +356,81 @@ int my_lseek(BinDumpFile * file, size_t offset, uint32_t verbose) | |||
| 376 | } | 356 | } |
| 377 | 357 | ||
| 378 | 358 | ||
| 359 | /// displays one line 8 byte chunk of data | ||
| 360 | /// @param[in] file file to use for operations | ||
| 361 | /// @param[in] offset offset | ||
| 362 | /// @param[in] len len | ||
| 363 | /// @param[in] verbose verbose level of messages to display | ||
| 364 | /// @param[in] opts output options | ||
| 365 | size_t my_print_dump(BinDumpFile * file, size_t offset, size_t len, | ||
| 366 | uint32_t opts) | ||
| 367 | { | ||
| 368 | char buff[9]; | ||
| 369 | size_t s; | ||
| 370 | size_t max; | ||
| 371 | |||
| 372 | if (opts) | ||
| 373 | return(0); | ||
| 374 | |||
| 375 | if (file->code < 1) | ||
| 376 | return(0); | ||
| 377 | |||
| 378 | // prints line offset | ||
| 379 | printf("0%04zo0:", (file->pos/8)); | ||
| 380 | |||
| 381 | // prints spaces | ||
| 382 | for(s = 0; s < offset; s++) | ||
| 383 | printf(" "); | ||
| 384 | |||
| 385 | // prints each byte | ||
| 386 | len = len ? (len - (file->pos-offset)) : 8; | ||
| 387 | max = (len > ((size_t)file->code)) ? (size_t)file->code : len; | ||
| 388 | for(s = 0; s < max; s++) | ||
| 389 | printf(" %s", my_byte2str(file->data[s], buff)); | ||
| 390 | printf("\n"); | ||
| 391 | |||
| 392 | file->pos += max; | ||
| 393 | |||
| 394 | return(1); | ||
| 395 | } | ||
| 396 | |||
| 397 | |||
| 398 | /// performs read upon file | ||
| 399 | /// @param[in] file file to use for operations | ||
| 400 | /// @param[in] offset offset | ||
| 401 | /// @param[in] len len | ||
| 402 | int my_read(BinDumpFile * file, size_t offset, size_t len, uint32_t verbose) | ||
| 403 | { | ||
| 404 | size_t max; | ||
| 405 | |||
| 406 | file->code = 0; | ||
| 407 | |||
| 408 | max = 8 - offset; | ||
| 409 | if (len) | ||
| 410 | max = ((len - file->read) < max) ? len - file->read : max; | ||
| 411 | |||
| 412 | if ((file->code = read(file->fd, file->data, max)) == -1) | ||
| 413 | { | ||
| 414 | perror(PROGRAM_NAME ": read()"); | ||
| 415 | my_close(file, verbose); | ||
| 416 | if (verbose > 0) | ||
| 417 | printf("read %zu bytes\n", (file->read)); | ||
| 418 | return(1); | ||
| 419 | }; | ||
| 420 | |||
| 421 | if (file->code < 1) | ||
| 422 | file->eof = 1; | ||
| 423 | else | ||
| 424 | file->read += file->code; | ||
| 425 | |||
| 426 | if (len) | ||
| 427 | if (file->read > len) | ||
| 428 | file->eof = 1; | ||
| 429 | |||
| 430 | return(file->code); | ||
| 431 | } | ||
| 432 | |||
| 433 | |||
| 379 | /// displays usage information | 434 | /// displays usage information |
| 380 | void my_usage() | 435 | void my_usage() |
| 381 | { | 436 | { |
