comparison tools/fanalyze.c @ 2433:2fcd8d712b1e

More improvements to the grep mode.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 19 Feb 2020 18:25:50 +0200
parents 758223ba6551
children 0d86e7ff49e1
comparison
equal deleted inserted replaced
2432:758223ba6551 2433:2fcd8d712b1e
105 105
106 106
107 typedef struct 107 typedef struct
108 { 108 {
109 char *name; 109 char *name;
110 char *desc;
110 Uint32 nmax; 111 Uint32 nmax;
111 unsigned int bsize; 112 unsigned int bsize;
112 } DMGrepType; 113 } DMGrepType;
113 114
114 115
115 static const DMGrepType dmGrepTypes[DMGV_last] = 116 static const DMGrepType dmGrepTypes[DMGV_last] =
116 { 117 {
117 { "8bit (byte)" , (1ULL << 8) - 1, 1 }, 118 { "8" , "8bit (byte)" , (1ULL << 8) - 1, 1 },
118 { "16bit (word) LE" , (1ULL << 16) - 1, 2 }, 119 { "le16" , "16bit (word) LE" , (1ULL << 16) - 1, 2 },
119 { "16bit (word) BE" , (1ULL << 16) - 1, 2 }, 120 { "be16" , "16bit (word) BE" , (1ULL << 16) - 1, 2 },
120 { "32bit (word) LE" , (1ULL << 32) - 1, 4 }, 121 { "le32" , "32bit (word) LE" , (1ULL << 32) - 1, 4 },
121 { "32bit (word) BE" , (1ULL << 32) - 1, 4 }, 122 { "be32" , "32bit (word) BE" , (1ULL << 32) - 1, 4 },
122 }; 123 };
123 124
124 125
125 typedef struct 126 typedef struct
126 { 127 {
153 DMSourceFile srcFiles[SET_MAX_FILES]; // Source file names 154 DMSourceFile srcFiles[SET_MAX_FILES]; // Source file names
154 DMStats totalStats; 155 DMStats totalStats;
155 int nsetGrepValues = 0; 156 int nsetGrepValues = 0;
156 DMGrepValue setGrepValues[SET_MAX_GREP_VALUES]; 157 DMGrepValue setGrepValues[SET_MAX_GREP_VALUES];
157 size_t optMinMatchLen = 8; 158 size_t optMinMatchLen = 8;
159 BOOL optOffsetMode = FALSE;
158 160
159 DMMatchSeq dmSequences[SET_MAX_SEQUENCES]; 161 DMMatchSeq dmSequences[SET_MAX_SEQUENCES];
160 int ndmSequences = 0; 162 int ndmSequences = 0;
161 163
162 164
170 172
171 { 10, 'g', "grep" , "Binary grep <val>[,<val2>...][:<le|be>[8|16|32]]", OPT_ARGREQ }, 173 { 10, 'g', "grep" , "Binary grep <val>[,<val2>...][:<le|be>[8|16|32]]", OPT_ARGREQ },
172 { 12, 'o', "offset" , "Show data in offset <offs>[,<offs2>...][:<le|be>[8|16|32][d|x]]", OPT_ARGREQ }, 174 { 12, 'o', "offset" , "Show data in offset <offs>[,<offs2>...][:<le|be>[8|16|32][d|x]]", OPT_ARGREQ },
173 { 14, 'm', "match" , "Find matching sequences minimum of <n> bytes long", OPT_NONE }, 175 { 14, 'm', "match" , "Find matching sequences minimum of <n> bytes long", OPT_NONE },
174 { 16, 'n', "minmatch" , "Minimum match sequence length", OPT_ARGREQ }, 176 { 16, 'n', "minmatch" , "Minimum match sequence length", OPT_ARGREQ },
177
178 { 18, 'O', "offset-mode" , "Output -o offset list when in grep mode (-g)", OPT_NONE },
175 }; 179 };
176 180
177 static const int optListN = sizeof(optList) / sizeof(optList[0]); 181 static const int optListN = sizeof(optList) / sizeof(optList[0]);
178 182
179 183
443 if (!val.vwildcards[n] && val.values[n] > dmGrepTypes[val.type].nmax) 447 if (!val.vwildcards[n] && val.values[n] > dmGrepTypes[val.type].nmax)
444 { 448 {
445 ret = dmError(DMERR_INVALID_ARGS, 449 ret = dmError(DMERR_INVALID_ARGS,
446 "Integer value %d <= %d <= %d out of range for type %s.\n", 450 "Integer value %d <= %d <= %d out of range for type %s.\n",
447 val.values[n], 0, dmGrepTypes[val.type].nmax, 451 val.values[n], 0, dmGrepTypes[val.type].nmax,
448 dmGrepTypes[val.type].name); 452 dmGrepTypes[val.type].desc);
449 453
450 goto out; 454 goto out;
451 } 455 }
452 } 456 }
453 else 457 else
463 memcpy(node, &val, sizeof(val)); 467 memcpy(node, &val, sizeof(val));
464 468
465 if (mode == FA_GREP) 469 if (mode == FA_GREP)
466 { 470 {
467 printf("Grep %s: ", 471 printf("Grep %s: ",
468 dmGrepTypes[val.type].name); 472 dmGrepTypes[val.type].desc);
469 473
470 dmPrintGrepValueList(stdout, node, FALSE, NULL, 0); 474 dmPrintGrepValueList(stdout, node, FALSE, NULL, 0);
471 printf("\n"); 475 printf("\n");
472 } 476 }
473 } 477 }
521 optArg); 525 optArg);
522 return FALSE; 526 return FALSE;
523 } 527 }
524 return TRUE; 528 return TRUE;
525 529
530 case 18:
531 optOffsetMode = TRUE;
532 break;
533
526 default: 534 default:
527 dmErrorMsg("Unknown argument '%s'.\n", currArg); 535 dmErrorMsg("Unknown argument '%s'.\n", currArg);
528 return FALSE; 536 return FALSE;
529 } 537 }
530 538
717 725
718 for (int n = 0; n < nsetGrepValues; n++) 726 for (int n = 0; n < nsetGrepValues; n++)
719 { 727 {
720 DMGrepValue *node = &setGrepValues[n]; 728 DMGrepValue *node = &setGrepValues[n];
721 const DMGrepType *def = &dmGrepTypes[node->type]; 729 const DMGrepType *def = &dmGrepTypes[node->type];
730 BOOL sep = FALSE;
731
732 if (optOffsetMode)
733 printf("-o ");
722 734
723 for (size_t offs = 0; offs + (def->bsize * node->nvalues) < file->size; offs++) 735 for (size_t offs = 0; offs + (def->bsize * node->nvalues) < file->size; offs++)
724 { 736 {
725 BOOL match = TRUE; 737 BOOL match = TRUE;
726 for (int n = 0; n < node->nvalues; n++) 738 for (int n = 0; n < node->nvalues; n++)
736 } 748 }
737 } 749 }
738 750
739 if (match) 751 if (match)
740 { 752 {
741 printf("%08" DM_PRIx_SIZE_T, offs); 753 if (optOffsetMode)
742 if (dmVerbosity >= 1)
743 { 754 {
744 printf(" : "); 755 printf("%s0x%" DM_PRIx_SIZE_T, sep ? "," : "", offs);
745 dmPrintGrepValueList(stdout, node, TRUE, file, offs); 756 sep = TRUE;
746 } 757 }
747 printf("\n"); 758 else
759 printf("%08" DM_PRIx_SIZE_T, offs);
760
761 if (!optOffsetMode)
762 {
763 if (dmVerbosity >= 1)
764 {
765 printf(" : ");
766 dmPrintGrepValueList(stdout, node, TRUE, file, offs);
767 }
768 printf("\n");
769 }
748 } 770 }
749 } 771 }
772
773 if (optOffsetMode)
774 printf(":%s\n", def->name);
750 } 775 }
751 } 776 }
752 } 777 }
753 else 778 else
754 if (setMode == FA_OFFSET) 779 if (setMode == FA_OFFSET)