-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·657 lines (599 loc) · 15.7 KB
/
main.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
/**
*******************************************************************************
* @file main.c
* @brief Main function (sources)
* @author Edouard DUPIN
* @date 05/02/2011
* @par Project
* hexViewer
*
* @par Copyright
* Copyright 2010 Edouard DUPIN, all right reserved
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY.
*
* Licence : GNU Lesser General Public License (LGPL) v3.0
*
*******************************************************************************
*/
#include "generalDefine.h"
#include "parameter.h"
#include "display.h"
fileProperties_ts fileProp[2];
int32_t findFirstDiff(void)
{
uint8_t data1, data2;
int32_t offset = 0;
if( NULL == fileProp[0].pointer
|| NULL == fileProp[1].pointer ) {
return 0;
}
int32_t paddingFile = getPaddingOffsetFile();
int32_t pad1 = 0;
int32_t pad2 = 0;
if (paddingFile>0) {
pad1 = paddingFile;
pad2 = 0;
} else {
pad1 = 0;
pad2 = -1*paddingFile;
}
if(NULL != fileProp[0].pointer) {
fseek ( fileProp[0].pointer , pad1+fileProp[0].fileBasicOffset , SEEK_SET );
}
if(NULL != fileProp[1].pointer) {
fseek ( fileProp[1].pointer , pad2+fileProp[1].fileBasicOffset , SEEK_SET );
}
while ( fread(&data1, sizeof(uint8_t), 1, fileProp[0].pointer) == 1
&& fread(&data2, sizeof(uint8_t), 1, fileProp[1].pointer) == 1)
{
offset ++;
if (data1 != data2) {
return offset;
}
}
return offset;
}
void AutoSetPadding(void)
{
displayPaddingOffset(0);
if( NULL == fileProp[0].pointer
|| NULL == fileProp[1].pointer ) {
return;
}
if(NULL != fileProp[0].pointer) {
fseek ( fileProp[0].pointer , fileProp[0].fileBasicOffset , SEEK_SET );
}
int32_t offset1 = 0;
char data;
while(fread(&data, sizeof(uint8_t), 1, fileProp[0].pointer) == 1) {
if (data != 0) {
break;
}
offset1 ++;
}
if(NULL != fileProp[1].pointer) {
fseek ( fileProp[1].pointer , fileProp[1].fileBasicOffset , SEEK_SET );
}
int32_t offset2 = 0;
while(fread(&data, sizeof(uint8_t), 1, fileProp[1].pointer) == 1)
{
if (data != 0) {
break;
}
offset2 ++;
}
if (offset1 == offset2) {
return;
}
if (0 == offset1) {
return;
}
if (0 == offset2) {
return;
}
displayPaddingOffset(offset1 - offset2);
}
void UpdateFilesSize(void)
{
// get size for 2 files
int32_t iii=0;
for (iii=0; iii<2; iii++) {
if ( NULL != fileProp[iii].pointer) {
fseek ( fileProp[iii].pointer , 0 , SEEK_END );
fileProp[iii].size = ftell (fileProp[iii].pointer) - fileProp[iii].fileBasicOffset;
fseek ( fileProp[iii].pointer , 0 , SEEK_SET );
}
}
}
void ResetProperties(void)
{
int32_t iii=0;
for (iii=0; iii<2; iii++) {
fileProp[iii].availlable = false;
fileProp[iii].pointer = NULL;
fileProp[iii].size = 0;
strcpy(fileProp[iii].name, "No-File");
fileProp[iii].fileBasicOffset = 0;
fileProp[iii].type = SHOW_TYPE_UNKNOW;
fileProp[iii].typeSize = SHOW_TYPE_SIZE_UNKNOW;
fileProp[iii].slotSize = 0;
fileProp[iii].delta = 0;
}
}
void OpenFiles(void)
{
int32_t iii=0;
for (iii=0; iii<2; iii++) {
if (true == fileProp[iii].availlable) {
// Open file 1
fileProp[iii].pointer = fopen(fileProp[iii].name, "rb");
if ( NULL == fileProp[iii].pointer) {
//printf("Can not Open [File_1] = %s\n", fileName[0]);
}
}
if (fileProp[iii].pointer==NULL) {
continue;
}
// check if file has specifi header :
char dataheader[128];
if(16 == fread(&dataheader, sizeof(uint8_t), 16, fileProp[iii].pointer)) {
// parse header
if( dataheader[0]=='#'
&& dataheader[1]=='M'
&& dataheader[2]=='E'
&& dataheader[3]=='T') {
// ==> "#MET %c %s %04d "
// type unused ...
if( dataheader[5] == 'I'
|| dataheader[5] == 'F'
|| dataheader[5] == 'D') {
fileProp[iii].type = SHOW_TYPE_DECIMAL_SIGNED;
} else if (dataheader[5] == 'U') {
fileProp[iii].type = SHOW_TYPE_DECIMAL_UNSIGNED;
} else if (dataheader[5] == 'U') {
fileProp[iii].type = SHOW_TYPE_HEX;
} else {
printf("Error while parsing the header ... \n");
fileProp[iii].type = SHOW_TYPE_UNKNOW;
}
if (strncmp(&dataheader[6], "08", 2)==0) {
fileProp[iii].typeSize = SHOW_TYPE_SIZE_8;
} else if (strncmp(&dataheader[6], "16", 2)==0) {
fileProp[iii].typeSize = SHOW_TYPE_SIZE_16;
} else if (strncmp(&dataheader[6], "32", 2)==0) {
fileProp[iii].typeSize = SHOW_TYPE_SIZE_32;
} else if (strncmp(&dataheader[6], "64", 2)==0) {
fileProp[iii].typeSize = SHOW_TYPE_SIZE_64;
} else if (strncmp(&dataheader[6], "28", 2)==0) {
fileProp[iii].typeSize = SHOW_TYPE_SIZE_128;
} else if (strncmp(&dataheader[6], "LO", 2)==0) {
fileProp[iii].typeSize = SHOW_TYPE_SIZE_FLOAT;
} else if (strncmp(&dataheader[6], "OU", 2)==0) {
fileProp[iii].typeSize = SHOW_TYPE_SIZE_DOUBLE;
} else {
printf("Error while parsing the header ... \n");
fileProp[iii].typeSize = SHOW_TYPE_SIZE_UNKNOW;
}
char tmpVal[5];
tmpVal[0] = dataheader[8];
tmpVal[1] = dataheader[9];
tmpVal[2] = dataheader[10];
tmpVal[3] = dataheader[11];
tmpVal[4] = '\0';
sscanf(tmpVal, "%04d", &fileProp[iii].slotSize);
sscanf(&dataheader[12], "%04d", &fileProp[iii].delta);
//printf("slot size [%d]=%d\n", iii, fileProp[iii].slotSize);
// ofset of the header :
fileProp[iii].fileBasicOffset = 16;
}
} // else : no header present ==> raw file
fseek ( fileProp[iii].pointer , fileProp[iii].fileBasicOffset , SEEK_SET );
}
// check internal properties :
if (fileProp[0].fileBasicOffset!=0 && fileProp[1].fileBasicOffset!=0) {
if (fileProp[0].typeSize == fileProp[1].typeSize) {
setTypeSize(fileProp[0].typeSize);
} else {
printf("Error The 2 files has not the same header typeSize properties header ... \n");
}
if (fileProp[0].type == fileProp[1].type) {
setType(fileProp[0].type);
} else {
printf("Error The 2 files has not the same header type properties header ... \n");
}
if (fileProp[0].slotSize == fileProp[1].slotSize) {
setSlotSize(fileProp[0].slotSize);
if (fileProp[0].slotSize>0) {
setSlotDisplayMode(true);
}
} else {
printf("Error The 2 files has not the same header slotSize properties header ... \n");
}
} else if (fileProp[0].fileBasicOffset!=0) {
setTypeSize(fileProp[0].typeSize);
setType(fileProp[0].type);
setSlotSize(fileProp[0].slotSize);
if (fileProp[0].slotSize>0) {
setSlotDisplayMode(true);
}
} else if (fileProp[1].fileBasicOffset!=0) {
setTypeSize(fileProp[1].typeSize);
setType(fileProp[1].type);
setSlotSize(fileProp[1].slotSize);
if (fileProp[1].slotSize>0) {
setSlotDisplayMode(true);
}
}
int32_t sizeElement=1;
showTypeSize_te tmpType = getTypeSize();
switch(tmpType)
{
default:
case SHOW_TYPE_SIZE_8:
sizeElement = 1;
break;
case SHOW_TYPE_SIZE_16:
sizeElement = 2;
break;
case SHOW_TYPE_SIZE_FLOAT:
case SHOW_TYPE_SIZE_32:
sizeElement = 4;
break;
case SHOW_TYPE_SIZE_DOUBLE:
case SHOW_TYPE_SIZE_64:
sizeElement = 8;
break;
}
int32_t tmpDela = (fileProp[1].delta - fileProp[0].delta) * sizeElement;
displayPaddingOffset(tmpDela);
UpdateFilesSize();
}
void CloseFiles(void)
{
int32_t iii=0;
for (iii=0; iii<2; iii++) {
if (NULL != fileProp[iii].pointer) {
fclose(fileProp[iii].pointer);
}
fileProp[iii].pointer = NULL;
fileProp[iii].fileBasicOffset = 0;
fileProp[iii].type = SHOW_TYPE_UNKNOW;
fileProp[iii].typeSize = SHOW_TYPE_SIZE_UNKNOW;
fileProp[iii].slotSize = 0;
fileProp[iii].delta = 0;
}
}
int main (int argc, char**argv)
{
int32_t first_Error = 0;
ResetProperties();
UpdateNumberOfRawAndColomn();
// check error
if (argc < 2) {
printf("You set more than 3 argument at the commande line\n");
usage();
return -1;
}
bool directCheckFiles = false;
int basicIDParsing = 1;
if (0==strcmp("-d",argv[1])) {
basicIDParsing++;
directCheckFiles = true;
}
// one file
if (basicIDParsing+1 <= argc) {
strcpy(fileProp[0].name, argv[basicIDParsing]);
fileProp[0].availlable = true;
}
// a second file
if (basicIDParsing+2 <= argc) {
strcpy(fileProp[1].name, argv[basicIDParsing+1]);
fileProp[1].availlable = true;
}
// open the files
OpenFiles();
// user requested to have the direct error ID of the file...
if (directCheckFiles==true) {
float dividor = 1;
int32_t maxSlot = (fileProp[0].slotSize>fileProp[1].slotSize)?fileProp[0].slotSize:fileProp[1].slotSize;
if (0!=maxSlot) {
dividor = maxSlot;
}
int32_t idError = findFirstDiff();
int minSizeFile = ((fileProp[0].size<fileProp[1].size)?fileProp[0].size:fileProp[1].size) - abs(getPaddingOffsetFile());
if (minSizeFile<=idError) {
printf(" --- slot=%3d nb frame=%d", (int32_t)dividor, (int32_t)(idError/dividor));
CloseFiles();
// 0 : no error
return 0;
} else {
int32_t sizeElement = 1;
showTypeSize_te tmpType = getTypeSize();
switch(tmpType)
{
default:
case SHOW_TYPE_SIZE_8:
sizeElement = 1;
break;
case SHOW_TYPE_SIZE_16:
sizeElement = 2;
break;
case SHOW_TYPE_SIZE_FLOAT:
case SHOW_TYPE_SIZE_32:
sizeElement = 4;
break;
case SHOW_TYPE_SIZE_DOUBLE:
case SHOW_TYPE_SIZE_64:
sizeElement = 8;
break;
}
int32_t elementIDError = idError/sizeElement;
float frameRatio = (float)elementIDError/dividor;
int32_t idFrame = frameRatio;
int32_t idFrameElement = (frameRatio-idFrame)*dividor;
printf("%9d / frame=%9.2f ==> frame=%5d element=%5d slot=%3d", elementIDError, frameRatio, idFrame, idFrameElement, (int32_t)dividor);
CloseFiles();
return idError;
}
}
// rendre la lecture des données non canonique
system("stty -icanon");
// supression de l'écho des caractères
system("stty -echo");
// enable mouse event ...
printf(MOUSE_REPORTING_ENABLE);
// hide cursor ...
printf(CURSOR_DISPLAY_DISABLE);
system("clear");
CleanDisplay();
int ret = 0;
pthread_t HangleThreadDisplay;
ret = pthread_create ( & HangleThreadDisplay, NULL, threadDisplay, NULL );
if (! ret)
{
while (1)
{
int32_t inputValue;
inputValue = getc(stdin);
switch(inputValue)
{
case 'q':
case 'Q':
goto exit_programme;
break;
//case 0x1B:
case '\e':
inputValue = getc (stdin);
//printf("\n get data : 0x%08x ..... : \n", (unsigned int)inputValue);
// [ == 0x5B
if (inputValue == '[')
{
inputValue = getc (stdin);
char charValue = inputValue;
// printf("\n get data : 0x%08x ..... : \\e[%c\n", (unsigned int)inputValue, charValue);
if ( charValue == 'A' //0x41 ==> UP
|| charValue == 'B' //0x42 ==> DOWN
|| charValue == 'C' //0x43 ==> RIGHT
|| charValue == 'D' //0x44 ==> LEFT
)
{
//printf("\n get data :\\e[%c ==> MoveKey \n", charValue);
if (charValue == 'A') {
upDownOfsetFile(-5);
} else if ( charValue == 'B') {
upDownOfsetFile(5);
} else if ( charValue == 'C') {
upDownOfsetFile((GetNumberOfRaw()-NB_HEARDER_RAW));
} else if ( charValue == 'D') {
upDownOfsetFile(-(GetNumberOfRaw()-NB_HEARDER_RAW));
}
} else if (charValue == 'M' ) { //0x4d ==> mouse
//printf("\n get data :\\e[%c ==> Mouse \n", charValue);
int32_t button = getc(stdin);
int32_t xPos = getc(stdin);
xPos -= 0x20;
int32_t yPos = getc(stdin);
yPos -= 0x20;
int32_t bt=0;
switch (button) {
case 97:
bt = 4;
break;
case 96:
bt = 5;
break;
case 32:
bt = 1;
break;
case 33:
bt = 3;
break;
}
//printf(" bt=%#x=%d \n", button, bt);
//printf(" x=%d y=%d\n", xPos, yPos);
if (bt == 4) {
upDownOfsetFile(5);
} else if (bt == 5) {
upDownOfsetFile(-5);
}
}
}
break;
// change the type of interpretation the file
case 't':
case 'T':
nextType();
break;
// change the size of showing the file
case 's':
case 'S':
nextTypeSize();
break;
// find the first ERROR
case 'f':
case 'F':
// try to find the first error...
first_Error = findFirstDiff();
setOfsetFile((first_Error/16)*16 - 128);
needRedraw();
break;
// find the first ERROR
case 'a':
case 'A':
setOfsetFile(0);
break;
// go to the end of the file (File 1 and next File 2)
case 'z':
case 'Z':
{
UpdateFilesSize();
static bool whichElement = false;
if (whichElement == false) {
whichElement = true;
setOfsetFile((fileProp[0].size/16)*16 - 256);
} else {
whichElement = false;
setOfsetFile((fileProp[1].size/16)*16 - 256);
}
}
break;
// Reload the 2 files
case 'r':
case 'R':
CloseFiles();
UpdateNumberOfRawAndColomn();
CleanDisplay();
OpenFiles();
needRedraw();
break;
// Display Slot mode :
case 'j':
case 'J':
setSlotDisplayMode((getSlotDisplayMode()==true)?false:true);
needRedraw();
break;
case 'U':
setSlotSize(getSlotSize()-9);
case 'u':
setSlotSize(getSlotSize()-1);
needRedraw();
break;
case 'I':
setSlotSize(getSlotSize()+9);
case 'i':
setSlotSize(getSlotSize()+1);
needRedraw();
break;
case 'k':
case 'K':
setSlotSize(0);
setSlotDisplayMode(false);
needRedraw();
break;
// Add padding offset between left an right file
case 'o':
displayPaddingOffset(-1);
break;
case 'O':
displayPaddingOffset(-16);
break;
case 'p':
displayPaddingOffset(1);
break;
case 'P':
displayPaddingOffset(16);
break;
// Clear the padding
case 'm':
case 'M':
displayPaddingOffsetClear();
break;
case 'c':
case 'C':
AutoSetPadding();
break;
case 'h':
case 'H':
setHelpDisplay((getHelpDisplay()==true)?false:true);
needRedraw();
break;
}
}
} else {
fprintf (stderr, "%s", strerror (ret));
}
exit_programme :
CloseFiles();
// disable mouse event ...
printf(MOUSE_REPORTING_DISABLE);
// Back cursor ON ...
printf(CURSOR_DISPLAY_ENABLE);
// remettre la lecture des données canonique
system("stty icanon");
// repositionnement de l'écho des caractères
system("stty echo");
return 0;
}
void displayCouleurBash(void)
{
/* Initialise the main variables
* colour: for the 256 colours (0-255)
* space: to insert space or newline
*/
int colour = 0;
int espace = 0;
/* Print the 16 first colours, known as colours system */
printf("System colours:\n");
for( ; colour < 16; colour++) {
printf("\e[48;5;%dm ", colour);
}
printf("\e[0m\n\n");
/* The 216 colours */
printf("Color cube: 6x6x6\n");
for ( ; colour < 232; colour++, espace++) {
if ((espace%6) == 0) {
printf("\e[0m ");
}
if ((espace%36 == 0)) {
printf("\e[0m\n");
}
printf("\e[48;5;%dm ", colour);
}
printf("\e[0m\n\n");
/* And the grey colours */
printf("Greyscale ramp\n");
for ( ; colour < 256; colour++) {
printf("\e[48;5;%dm ", colour);
}
printf("\e[0m\n\n");
/* Initialise the main variables
* colour: for the 256 colours (0-255)
* space: to insert space or newline
*/
/* Print the 16 first colours, known as colours system */
printf("System colours:\n");
for( ; colour < 16; colour++) {
printf("\e[48;5;%dm ", colour);
}
printf("\e[0m\n\n");
/* The 216 colours */
printf("Color cube: 6x6x6\n");
for ( ; colour < 232; colour++, espace++) {
if ((espace%6) == 0) {
printf("\e[0m ");
}
if ((espace%36 == 0)) {
printf("\e[0m\n");
}
printf("\e[48;5;%dm ", colour);
}
printf("\e[0m\n\n");
/* And the grey colours */
printf("Greyscale ramp\n");
for ( ; colour < 256; colour++) {
printf("\e[48;5;%dm ", colour);
}
printf("\e[0m\n\n");
}