-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdumps.c
942 lines (749 loc) · 34.8 KB
/
dumps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/**************************************************
* Hexdump for Windows
*-------------------------------------------------
*
* Command line tool inspired by Linux tool
* hexdump - stripped to bare hexadecimal dump
* of given file with option to limit dump length
*
* File: dumps.c
*
*-------------------------------------------------
* Made by Marek Poláèek (Polda18)
**************************************************/
// Global definitions
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <direct.h>
#include <errno.h>
// Local definitions
#include "dumps.h"
#include "info.h"
// Print offset - static function
static void print_offset(FILE* file, size_t ndigits, size_t position) {
// Construct string format for offset display
char format[100]; // Init format buffer
sprintf(format, "%%0%lux", ndigits); // Get format for offset
// Return offset hexadecimal value
fprintf(stdout, format, position);
}
// Calculate number of digits - static function
static size_t get_offset_digits(size_t number) {
// Get number of digits for offset display
size_t ndigits = MIN_OFFSET_DIGITS; // Init number of digits - bare minimum is 7
register size_t num_copy = number; // Init operations on given offset value
// For number greater or equal to 0x10000000 add appropriate number of digits
while(num_copy >= OVERFLOW_VALUE) {
num_copy /= 16; // Hexadecimal division
++ndigits; // Count the digits
}
// Return number of digits
return ndigits;
}
// Dump byte hex
int dump_plain_hex(
FILE* file,
bool verbose,
size_t offset,
size_t* position,
size_t endoffset
) {
// Format:
// "%07x" or greater for offset at the beginning (aligned to 0x10 for value less than end of file)
// 1 space
// "%04x" for each valid 2 bytes of input data (aligned as 8 16bit values, separated by 1 space)
// If end of file reached, only offset is printed
// Calculate number of digits based on ending offset
size_t ndigits = get_offset_digits(endoffset);
// Initialize buffers (16 bytes, arranged as 8 2-byte values)
uword_t buffer_current[WORD_ARRAY_NUM];
uword_t buffer_previous[WORD_ARRAY_NUM];
ubyte_t remainder = 0; // Save up for last remaining byte
bool data_repeat = false; // Repeating data
bool line_repeat = false; // Repeating line
size_t successfully_read = 0;
// Print out data in plain 2-byte hexadecimal format
do {
// Run at least once
// Check for previous iteration round - full line counts
if (successfully_read == WORD_ARRAY_NUM) {
// Previously read full 16 bytes -> copy all current buffer into previous one
for (int i = 0; i < WORD_ARRAY_NUM; ++i) {
buffer_previous[i] = buffer_current[i];
}
}
// Read into current buffer -> count number of successful
successfully_read = fread(buffer_current, WORD_SIZE, WORD_ARRAY_NUM, file);
if ((successfully_read < WORD_ARRAY_NUM) && (ftell(file) < endoffset)) {
// An error occured while reading file
putchar('\n'); // Divide space between data and error message
print_file_error("An error occured while file contents output");
return errno; // End with specific return code for given error
}
// Check for repeating data
if (*position > offset) {
// If whole 16 bytes have been read, proceed
// Mark repeating data initial to true
data_repeat = true;
// Iterate through both buffers to find repeating data
for (int i = 0; i < successfully_read; ++i) {
// If single element is different, next iteration won't matter
data_repeat = data_repeat && (buffer_current[i] == buffer_previous[i]);
}
if (!data_repeat) // If data isn't repeating, line isn't repeating eighter
line_repeat = false;
}
// If verbose isn't set and buffers are repeating, then print out single asterisk and skip iteration
if (!verbose && data_repeat) {
// Check for repeating line
if (!line_repeat) {
fprintf(stdout, "*\n"); // Print out asterisk
line_repeat = true; // Set repeating line
}
*position = ftell(file); // Get new position
continue; // Skip iteration
}
// Data are different or verbose is enabled - proceed
// Print current position
print_offset(file, ndigits, *position);
// Iterate through current buffer
for (int i = 0; i < successfully_read; ++i) {
// Iterate through read elements
fprintf(stdout, HEX_PLAIN_FORMAT, buffer_current[i]);
}
// Get new position from number of successfuly read bytes
*position += successfully_read * WORD_SIZE;
// Check for last remanining byte in incomplete 16byte data buffer
if ((*position == endoffset - BYTE_SIZE) && (successfully_read < 8)) {
fseek(file, *position, SEEK_SET); // Reset to last known position
successfully_read += fread(&remainder, BYTE_SIZE, 1, file); // Read single byte
fprintf(stdout, HEX_PLAIN_FORMAT, remainder); // Print remainder out
*position = ftell(file); // Update position to finish iteration
}
// Fill remaining space with white space
for (int i = successfully_read; i < WORD_ARRAY_NUM; ++i) {
fprintf(stdout, " ");
}
// Issue end of line
putchar('\n');
} while (*position < endoffset);
// Reached end of file
print_offset(file, ndigits, *position); // Print ending position
putchar('\n'); // Last line feed
return 0;
}
// Dump byte octal
int dump_byte_octal(
FILE* file,
bool verbose,
size_t offset,
size_t* position,
size_t endoffset
) {
// Format:
// "%07x" or greater for offset at the beginning (aligned to 0x10 for value less than end of file)
// 1 space
// "%03o" for each valid byte of input data (aligned as 16 8bit values) (separated by 1 space)
// or empty spaces for invalid bytes at the beginning
// If end of file reached, only offset is printed
// Calculate number of digits based on ending offset
size_t ndigits = get_offset_digits(endoffset);
// Initialize buffers (16 bytes, arranged as 8 2-byte values)
ubyte_t buffer_current[BYTE_ARRAY_NUM];
ubyte_t buffer_previous[BYTE_ARRAY_NUM];
bool data_repeat = false; // Repeating data
bool line_repeat = false; // Repeating line
size_t successfully_read = 0;
// Print out data in 1-byte octal format
do {
// Run at least once
// Check for previous iteration round - full line counts
if (successfully_read == BYTE_ARRAY_NUM) {
// Previously read full 16 bytes -> copy all current buffer into previous one
for (int i = 0; i < BYTE_ARRAY_NUM; ++i) {
buffer_previous[i] = buffer_current[i];
}
}
// Read into current buffer -> count number of successful
successfully_read = fread(buffer_current, BYTE_SIZE, BYTE_ARRAY_NUM, file);
if ((successfully_read < BYTE_ARRAY_NUM) && (ftell(file) < endoffset)) {
// An error occured while reading file
putchar('\n'); // Divide space between data and error message
print_file_error("An error occured while file contents output");
return errno; // End with specific return code for given error
}
// Check for repeating data
if (*position > offset) {
// If whole 16 bytes have been read, proceed
// Mark repeating data initial to true
data_repeat = true;
// Iterate through both buffers to find repeating data
for (int i = 0; i < successfully_read; ++i) {
// If single element is different, next iteration won't matter
data_repeat = data_repeat && (buffer_current[i] == buffer_previous[i]);
}
if (!data_repeat) // If data isn't repeating, line isn't repeating eighter
line_repeat = false;
}
// If verbose isn't set and buffers are repeating, then print out single asterisk and skip iteration
if (!verbose && data_repeat) {
// Check for repeating line
if (!line_repeat) {
fprintf(stdout, "*\n"); // Print out asterisk
line_repeat = true; // Set repeating line
}
*position = ftell(file); // Get new position
continue; // Skip iteration
}
// Data are different or verbose is enabled - proceed
// Print current position
print_offset(file, ndigits, *position);
// Iterate through current buffer
for (int i = 0; i < successfully_read; ++i) {
// Iterate through read elements
fprintf(stdout, OCTAL_BYTE_FORMAT, buffer_current[i]);
}
// Get new position from number of successfuly read bytes
*position += successfully_read * BYTE_SIZE;
// Fill remaining space with white spaces
for (int i = successfully_read; i < BYTE_ARRAY_NUM; ++i) {
fprintf(stdout, " ");
}
// Issue end of line
putchar('\n');
} while (*position < endoffset);
// Reached end of file
print_offset(file, ndigits, *position); // Print ending position
putchar('\n'); // Last line feed
return 0;
}
// Dump byte characters
int dump_byte_char(
FILE* file,
bool verbose,
size_t offset,
size_t* position,
size_t endoffset
) {
// Format:
// "%07x" or greater for offset at the beginning (aligned to 0x10 for value less than end of file)
// " %c" for each valid 16 bytes of input data (aligned as 8bit values) for printable characters
// or "\\?" where ? is escape sequence for control characters
// or "%3o" for other valid values (separated by spaces)
// or empty spaces for invalid bytes at the beginning
// If end of file reached, only offset is printed
// Calculate number of digits based on ending offset
size_t ndigits = get_offset_digits(endoffset);
// Initialize buffers (16 bytes, arranged as 8 2-byte values)
ubyte_t buffer_current[BYTE_ARRAY_NUM];
ubyte_t buffer_previous[BYTE_ARRAY_NUM];
bool data_repeat = false; // Repeating data
bool line_repeat = false; // Repeating line
size_t successfully_read = 0;
// Print out data in 1-byte char format
// (if valid character, or escape sequence for standard codes or octal number for other values)
do {
// Run at least once
// Check for previous iteration round - full line counts
if (successfully_read == BYTE_ARRAY_NUM) {
// Previously read full 16 bytes -> copy all current buffer into previous one
for (int i = 0; i < BYTE_ARRAY_NUM; ++i) {
buffer_previous[i] = buffer_current[i];
}
}
// Read into current buffer -> count number of successful
successfully_read = fread(buffer_current, BYTE_SIZE, BYTE_ARRAY_NUM, file);
if ((successfully_read < BYTE_ARRAY_NUM) && (ftell(file) < endoffset)) {
// An error occured while reading file
putchar('\n'); // Divide space between data and error message
print_file_error("An error occured while file contents output");
return errno; // End with specific return code for given error
}
// Check for repeating data
if (*position > offset) {
// If whole 16 bytes have been read, proceed
// Mark repeating data initial to true
data_repeat = true;
// Iterate through both buffers to find repeating data
for (int i = 0; i < successfully_read; ++i) {
// If single element is different, next iteration won't matter
data_repeat = data_repeat && (buffer_current[i] == buffer_previous[i]);
}
if (!data_repeat) // If data isn't repeating, line isn't repeating eighter
line_repeat = false;
}
// If verbose isn't set and buffers are repeating, then print out single asterisk and skip iteration
if (!verbose && data_repeat) {
// Check for repeating line
if (!line_repeat) {
fprintf(stdout, "*\n"); // Print out asterisk
line_repeat = true; // Set repeating line
}
*position = ftell(file); // Get new position
continue; // Skip iteration
}
// Data are different or verbose is enabled - proceed
// Print current position
print_offset(file, ndigits, *position);
// Iterate through current buffer
for (int i = 0; i < successfully_read; ++i) {
// Iterate through read elements
ubyte_t value = buffer_current[i]; // Copy value for determining output
if (value >= ASCII_LOWEST && value <= ASCII_HIGHEST || value >= ASCII_DOS_CP_BEGIN) {
// Byte is printable ASCII character or equals a value in DOS codepage
fprintf(stdout, CHAR_BYTE_FORMAT_PRINTABLE, value);
continue; // Continue in iteration
}
switch (value) {
// Is not printable nor in DOS codepage
case CHAR_BYTE_VALUE_NULL:
// Null byte
fprintf(stdout, CHAR_BYTE_FORMAT_ESCAPE_NULL);
break;
case CHAR_BYTE_VALUE_BELL:
// Beep
fprintf(stdout, CHAR_BYTE_FORMAT_ESCAPE_BELL);
break;
case CHAR_BYTE_VALUE_BS:
// Backspace
fprintf(stdout, CHAR_BYTE_FORMAT_ESCAPE_BS);
break;
case CHAR_BYTE_VALUE_TAB:
// Tabulator
fprintf(stdout, CHAR_BYTE_FORMAT_ESCAPE_TAB);
break;
case CHAR_BYTE_VALUE_LF:
// Line feed
fprintf(stdout, CHAR_BYTE_FORMAT_ESCAPE_LF);
break;
case CHAR_BYTE_VALUE_VT:
// Vertical tab
fprintf(stdout, CHAR_BYTE_FORMAT_ESCAPE_VT);
break;
case CHAR_BYTE_VALUE_FF:
// Form feed
fprintf(stdout, CHAR_BYTE_FORMAT_ESCAPE_FF);
break;
case CHAR_BYTE_VALUE_CR:
// Carriage return
fprintf(stdout, CHAR_BYTE_FORMAT_ESCAPE_CR);
break;
default:
// Another value that doesn't fall in standard escape sequence
fprintf(stdout, OCTAL_BYTE_FORMAT, value);
}
}
// Get new position from number of successfuly read bytes
*position += successfully_read * BYTE_SIZE;
// Fill remaining space with white space
for (int i = successfully_read; i < BYTE_ARRAY_NUM; ++i) {
fprintf(stdout, " ");
}
// Issue end of line
putchar('\n');
} while (*position < endoffset);
// Reached end of file
print_offset(file, ndigits, *position); // Print ending position
putchar('\n'); // Last line feed
return 0;
}
// Dump hex-ascii canonical
int dump_canonical(
FILE* file,
bool verbose,
size_t offset,
size_t* position,
size_t endoffset
) {
// Format:
// "%07x" or greater for offset at the beginning (aligned to 0x10 for value less than end of file)
// 2 spaces
// "%02x" for each first 8 bytes of input data (aligned as 8bit values) (separated by 1 space)
// 2 spaces
// "%02x" for each valid 16 bytes of input data (aligned as 8bit values) (separated by 1 space)
// or empty spaces for invalid bytes at the beginning
// 2 spaces
// 16 bytes of valid input data displayed as ASCII text or '.' for non-ASCII printable values,
// surrounded with '|' at the beginning and the end
// If end of file reached, only offset is printed
// Calculate number of digits based on ending offset
size_t ndigits = get_offset_digits(endoffset);
// Initialize buffers (16 bytes, arranged as 8 2-byte values)
ubyte_t buffer_current[BYTE_ARRAY_NUM];
ubyte_t buffer_previous[BYTE_ARRAY_NUM];
bool data_repeat = false; // Repeating data
bool line_repeat = false; // Repeating line
size_t successfully_read = 0;
// Print out data in canonical 1-byte hex + ASCII notation
do {
// Run at least once
// Check for previous iteration round - full line counts
if (successfully_read == BYTE_ARRAY_NUM) {
// Previously read full 16 bytes -> copy all current buffer into previous one
for (int i = 0; i < BYTE_ARRAY_NUM; ++i) {
buffer_previous[i] = buffer_current[i];
}
}
// Read into current buffer -> count number of successful
successfully_read = fread(buffer_current, BYTE_SIZE, BYTE_ARRAY_NUM, file);
if ((successfully_read < BYTE_ARRAY_NUM) && (ftell(file) < endoffset)) {
// An error occured while reading file
putchar('\n'); // Divide space between data and error message
print_file_error("An error occured while file contents output");
return errno; // End with specific return code for given error
}
// Check for repeating data
if (*position > offset) {
// If whole 16 bytes have been read, proceed
// Mark repeating data initial to true
data_repeat = true;
// Iterate through both buffers to find repeating data
for (int i = 0; i < successfully_read; ++i) {
// If single element is different, next iteration won't matter
data_repeat = data_repeat && (buffer_current[i] == buffer_previous[i]);
}
if (!data_repeat) // If data isn't repeating, line isn't repeating eighter
line_repeat = false;
}
// If verbose isn't set and buffers are repeating, then print out single asterisk and skip iteration
if (!verbose && data_repeat) {
// Check for repeating line
if (!line_repeat) {
fprintf(stdout, "*\n"); // Print out asterisk
line_repeat = true; // Set repeating line
}
*position = ftell(file); // Get new position
continue; // Skip iteration
}
// Data are different or verbose is enabled - proceed
// Print current position
print_offset(file, ndigits, *position);
// Separate position and hex values
putchar(CAN_HEX_SEPARATOR);
// Iterate through current buffer
for (int i = 0; i < successfully_read; ++i) {
// Check where the iteration is
if (i == WORD_ARRAY_NUM)
// Separate second half
putchar(CAN_HEX_SEPARATOR);
// Print hexadecimal format
fprintf(stdout, CAN_HEX_FORMAT, buffer_current[i]);
}
// Fill remaining space with white space
for (int i = successfully_read; i < BYTE_ARRAY_NUM; ++i) {
// Check where the iteration is
if (i == WORD_ARRAY_NUM)
// Separate second half
putchar(CAN_HEX_SEPARATOR);
// Print spaces
fprintf(stdout, " ");
}
// Separate hex values from ASCII data
fprintf(stdout, CAN_ASCII_SEPARATOR);
// Print ASCII border
putchar(CAN_ASCII_FORMAT_BORDER);
// Iterate through data and print their printable ASCII or '.'
for (int i = 0; i < successfully_read; ++i) {
// Copy value for comparison
ubyte_t value = buffer_current[i];
// Check byte data for printable ASCII values
if (value < ASCII_LOWEST || value > ASCII_HIGHEST)
// Not printable -> print '.' instead
putchar(CAN_ASCII_FORMAT_NONASCII);
else
// Printable ASCII value, print out direct value
putchar(value);
}
// Print ASCII border
putchar(CAN_ASCII_FORMAT_BORDER);
// Get new position from number of successfuly read bytes
*position += successfully_read * BYTE_SIZE;
// Line feed at the end
putchar('\n');
} while (*position < endoffset);
// Reached end of file
print_offset(file, ndigits, *position); // Print ending position
putchar('\n'); // Last line feed
return 0;
}
// Dump word decimal
int dump_word_decimal(
FILE* file,
bool verbose,
size_t offset,
size_t* position,
size_t endoffset
) {
// Format:
// "%07x" or greater for offset at the beginning (aligned to 0x10 for value less than end of file)
// 3 spaces
// "%05d" for each valid 16 bytes of input data (aligned as 16bit values) (separated with 3 spaces)
// or empty spaces for invalid bytes at the beginning
// If end of file reached, only offset is printed
// Calculate number of digits based on ending offset
size_t ndigits = get_offset_digits(endoffset);
// Initialize buffers (16 bytes, arranged as 8 2-byte values)
uword_t buffer_current[WORD_ARRAY_NUM];
uword_t buffer_previous[WORD_ARRAY_NUM];
ubyte_t remainder = 0; // Save up for last remaining byte
bool data_repeat = false; // Repeating data
bool line_repeat = false; // Repeating line
size_t successfully_read = 0;
// Print out data in 2-byte aligned decimal format
do {
// Run at least once
// Check for previous iteration round - full line counts
if (successfully_read == WORD_ARRAY_NUM) {
// Previously read full 16 bytes -> copy all current buffer into previous one
for (int i = 0; i < WORD_ARRAY_NUM; ++i) {
buffer_previous[i] = buffer_current[i];
}
}
// Read into current buffer -> count number of successful
successfully_read = fread(buffer_current, WORD_SIZE, WORD_ARRAY_NUM, file);
if ((successfully_read < WORD_ARRAY_NUM) && (ftell(file) < endoffset)) {
// An error occured while reading file
putchar('\n'); // Divide space between data and error message
print_file_error("An error occured while file contents output");
return errno; // End with specific return code for given error
}
// Check for repeating data
if (*position > offset) {
// If whole 16 bytes have been read, proceed
// Mark repeating data initial to true
data_repeat = true;
// Iterate through both buffers to find repeating data
for (int i = 0; i < successfully_read; ++i) {
// If single element is different, next iteration won't matter
data_repeat = data_repeat && (buffer_current[i] == buffer_previous[i]);
}
if (!data_repeat) // If data isn't repeating, line isn't repeating eighter
line_repeat = false;
}
// If verbose isn't set and buffers are repeating, then print out single asterisk and skip iteration
if (!verbose && data_repeat) {
// Check for repeating line
if (!line_repeat) {
fprintf(stdout, "*\n"); // Print out asterisk
line_repeat = true; // Set repeating line
}
*position = ftell(file); // Get new position
continue; // Skip iteration
}
// Data are different or verbose is enabled - proceed
// Print current position
print_offset(file, ndigits, *position);
// Iterate through current buffer
for (int i = 0; i < successfully_read; ++i) {
// Iterate through read elements
fprintf(stdout, DECIMAL_WORD_FORMAT, buffer_current[i]);
}
// Get new position from number of successfuly read bytes
*position += successfully_read * WORD_SIZE;
// Check for last remanining byte in incomplete 16byte data buffer
if ((*position == endoffset - BYTE_SIZE) && (successfully_read < 8)) {
fseek(file, *position, SEEK_SET); // Reset to last known position
successfully_read += fread(&remainder, BYTE_SIZE, 1, file); // Read single byte
fprintf(stdout, DECIMAL_WORD_FORMAT, remainder); // Print remainder out
*position = ftell(file); // Update position to finish iteration
}
// Fill remaining space with white spaces
for (int i = successfully_read; i < WORD_ARRAY_NUM; ++i) {
fprintf(stdout, " ");
}
// Issue end of line
putchar('\n');
} while (*position < endoffset);
// Reached end of file
print_offset(file, ndigits, *position); // Print ending position
putchar('\n'); // Last line feed
return 0;
}
// Dump word octal
int dump_word_octal(
FILE* file,
bool verbose,
size_t offset,
size_t* position,
size_t endoffset
) {
// Format:
// "%07x" or greater for offset at the beginning (aligned to 0x10 for value less than end of file)
// 2 spaces
// "%06o" for each valid 16 bytes of input data (aligned as 16bit values) (separated with 2 spaces)
// or empty spaces for invalid bytes at the beginning
// If end of file reached, only offset is printed
// Calculate number of digits based on ending offset
size_t ndigits = get_offset_digits(endoffset);
// Initialize buffers (16 bytes, arranged as 8 2-byte values)
uword_t buffer_current[WORD_ARRAY_NUM];
uword_t buffer_previous[WORD_ARRAY_NUM];
ubyte_t remainder = 0; // Save up for last remaining byte
bool data_repeat = false; // Repeating data
bool line_repeat = false; // Repeating line
size_t successfully_read = 0;
// Print out data in 2-byte aligned octal format
do {
// Run at least once
// Check for previous iteration round - full line counts
if (successfully_read == WORD_ARRAY_NUM) {
// Previously read full 16 bytes -> copy all current buffer into previous one
for (int i = 0; i < WORD_ARRAY_NUM; ++i) {
buffer_previous[i] = buffer_current[i];
}
}
// Read into current buffer -> count number of successful
successfully_read = fread(buffer_current, WORD_SIZE, WORD_ARRAY_NUM, file);
if ((successfully_read < WORD_ARRAY_NUM) && (ftell(file) < endoffset)) {
// An error occured while reading file
putchar('\n'); // Divide space between data and error message
print_file_error("An error occured while file contents output");
return errno; // End with specific return code for given error
}
// Check for repeating data
if (*position > offset) {
// If whole 16 bytes have been read, proceed
// Mark repeating data initial to true
data_repeat = true;
// Iterate through both buffers to find repeating data
for (int i = 0; i < successfully_read; ++i) {
// If single element is different, next iteration won't matter
data_repeat = data_repeat && (buffer_current[i] == buffer_previous[i]);
}
if (!data_repeat) // If data isn't repeating, line isn't repeating eighter
line_repeat = false;
}
// If verbose isn't set and buffers are repeating, then print out single asterisk and skip iteration
if (!verbose && data_repeat) {
// Check for repeating line
if (!line_repeat) {
fprintf(stdout, "*\n"); // Print out asterisk
line_repeat = true; // Set repeating line
}
*position = ftell(file); // Get new position
continue; // Skip iteration
}
// Data are different or verbose is enabled - proceed
// Print current position
print_offset(file, ndigits, *position);
// Iterate through current buffer
for (int i = 0; i < successfully_read; ++i) {
// Iterate through read elements
fprintf(stdout, OCTAL_WORD_FORMAT, buffer_current[i]);
}
// Get new position from number of successfuly read bytes
*position += successfully_read * WORD_SIZE;
// Check for last remanining byte in incomplete 16byte data buffer
if ((*position == endoffset - BYTE_SIZE) && (successfully_read < 8)) {
fseek(file, *position, SEEK_SET); // Reset to last known position
successfully_read += fread(&remainder, BYTE_SIZE, 1, file); // Read single byte
fprintf(stdout, OCTAL_WORD_FORMAT, remainder); // Print remainder out
*position = ftell(file); // Update position to finish iteration
}
// Fill remaining space with white spaces
for (int i = successfully_read; i < WORD_ARRAY_NUM; ++i) {
fprintf(stdout, " ");
}
// Issue end of line
putchar('\n');
} while (*position < endoffset);
// Reached end of file
print_offset(file, ndigits, *position); // Print ending position
putchar('\n'); // Last line feed
return 0;
}
// Dump word hex
int dump_word_hex(
FILE* file,
bool verbose,
size_t offset,
size_t* position,
size_t endoffset
) {
// Format:
// "%07x" or greater for offset at the beginning (aligned to 0x10 for value less than end of file)
// 4 spaces
// " %04x" for each valid 16 bytes of input data (aligned as 16bit values) (separated by 4 spaces)
// or empty spaces for invalid bytes at the beginning
// If end of file reached, only offset is printed
// Calculate number of digits based on ending offset
size_t ndigits = get_offset_digits(endoffset);
// Initialize buffers (16 bytes, arranged as 8 2-byte values)
uword_t buffer_current[WORD_ARRAY_NUM];
uword_t buffer_previous[WORD_ARRAY_NUM];
ubyte_t remainder = 0; // Save up for last remaining byte
bool data_repeat = false; // Repeating data
bool line_repeat = false; // Repeating line
size_t successfully_read = 0;
// Print out data in two-byte aligned hexadecimal format
do {
// Run at least once
// Check for previous iteration round - full line counts
if (successfully_read == WORD_ARRAY_NUM) {
// Previously read full 16 bytes -> copy all current buffer into previous one
for (int i = 0; i < WORD_ARRAY_NUM; ++i) {
buffer_previous[i] = buffer_current[i];
}
}
// Read into current buffer -> count number of successful
successfully_read = fread(buffer_current, WORD_SIZE, WORD_ARRAY_NUM, file);
if ((successfully_read < WORD_ARRAY_NUM) && (ftell(file) < endoffset)) {
// An error occured while reading file
putchar('\n'); // Divide space between data and error message
print_file_error("An error occured while file contents output");
return errno; // End with specific return code for given error
}
// Check for repeating data
if (*position > offset) {
// If whole 16 bytes have been read, proceed
// Mark repeating data initial to true
data_repeat = true;
// Iterate through both buffers to find repeating data
for (int i = 0; i < successfully_read; ++i) {
// If single element is different, next iteration won't matter
data_repeat = data_repeat && (buffer_current[i] == buffer_previous[i]);
}
if (!data_repeat) // If data isn't repeating, line isn't repeating eighter
line_repeat = false;
}
// If verbose isn't set and buffers are repeating, then print out single asterisk and skip iteration
if (!verbose && data_repeat) {
// Check for repeating line
if (!line_repeat) {
fprintf(stdout, "*\n"); // Print out asterisk
line_repeat = true; // Set repeating line
}
*position = ftell(file); // Get new position
continue; // Skip iteration
}
// Data are different or verbose is enabled - proceed
// Print current position
print_offset(file, ndigits, *position);
// Iterate through current buffer
for (int i = 0; i < successfully_read; ++i) {
// Iterate through read elements
fprintf(stdout, HEX_WORD_FORMAT, buffer_current[i]);
}
// Get new position from number of successfuly read bytes
*position += successfully_read * WORD_SIZE;
// Check for last remanining byte in incomplete 16byte data buffer
if ((*position == endoffset - BYTE_SIZE) && (successfully_read < 8)) {
fseek(file, *position, SEEK_SET); // Reset to last known position
successfully_read += fread(&remainder, BYTE_SIZE, 1, file); // Read single byte
fprintf(stdout, HEX_WORD_FORMAT, remainder); // Print remainder out
*position = ftell(file); // Update position to finish iteration
}
// Fill remaining space with white spaces
for (int i = successfully_read; i < WORD_ARRAY_NUM; ++i) {
fprintf(stdout, " ");
}
// Issue end of line
putchar('\n');
} while (*position < endoffset);
// Reached end of file
print_offset(file, ndigits, *position); // Print ending position
putchar('\n'); // Last line feed
return 0;
}