Mercurial > hg > dmlib
annotate tools/fanalyze.c @ 1996:4a4c3e15b8c2
Add more analyzing.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 02 Jul 2018 02:26:52 +0300 |
parents | ea6337c873c6 |
children | 8e38fa3c4f98 |
rev | line source |
---|---|
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Fanalyze - Analyze similarities between multiple files |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * (C) Copyright 2018 Tecnic Software productions (TNSP) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #include "dmtool.h" |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include "dmlib.h" |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #include "dmargs.h" |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 #include "dmfile.h" |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 |
1996 | 13 #define SET_MAX_FILES 16 |
14 #define SET_MAX_ELEMS 256 | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 /* Typedefs |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 */ |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 typedef struct |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 { |
1996 | 21 Uint8 stats[SET_MAX_ELEMS]; |
22 Uint8 variants, data; | |
23 int interest[16]; | |
24 int interestF[16]; | |
25 } DMCompElem; | |
26 | |
27 | |
28 typedef struct | |
29 { | |
30 int count; | |
31 Uint8 value; | |
32 } DMStatValue; | |
33 | |
34 | |
35 typedef struct | |
36 { | |
37 DMStatValue cv[SET_MAX_ELEMS]; | |
38 } DMStats; | |
39 | |
40 | |
41 typedef struct | |
42 { | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 char *filename; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 Uint8 *data; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 size_t size; // offset, crop_start, crop_end, doCrop? |
1996 | 46 DMStats stats; |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 } DMSourceFile; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 /* Global variables |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 */ |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 int nsrcFiles = 0; // Number of source files |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 DMSourceFile srcFiles[SET_MAX_FILES]; // Source file names |
1996 | 54 DMStats totalStats; |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 /* Arguments |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 */ |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 static const DMOptArg optList[] = |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 { 0, '?', "help", "Show this help", OPT_NONE }, |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 }; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 static const int optListN = sizeof(optList) / sizeof(optList[0]); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 void argShowHelp() |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 dmPrintBanner(stdout, dmProgName, "[options] <input file #1> <input file #2> [...]"); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 dmArgsPrintHelp(stdout, optList, optListN, 0); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 { |
1995
ea6337c873c6
Silence an unused function argument warning.
Matti Hamalainen <ccr@tnsp.org>
parents:
1685
diff
changeset
|
77 (void) optArg; |
ea6337c873c6
Silence an unused function argument warning.
Matti Hamalainen <ccr@tnsp.org>
parents:
1685
diff
changeset
|
78 |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 switch (optN) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 case 0: |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 argShowHelp(); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 exit(0); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 break; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 case 1: |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 dmVerbosity++; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 break; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 default: |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 dmErrorMsg("Unknown argument '%s'.\n", currArg); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 return FALSE; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 return TRUE; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 BOOL argHandleNonOpt(char *currArg) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 if (nsrcFiles < SET_MAX_FILES) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 DMSourceFile *file = &srcFiles[nsrcFiles++]; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 file->filename = currArg; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 return TRUE; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 else |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
108 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 dmErrorMsg("Maximum number of input files exceeded (%d).\n", |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 SET_MAX_FILES); |
1996 | 111 return TRUE; |
112 } | |
113 } | |
114 | |
115 | |
116 void dmInitStats(DMStats *stats) | |
117 { | |
118 for (size_t n = 0; n < SET_MAX_ELEMS; n++) | |
119 { | |
120 stats->cv[n].count = 0; | |
121 stats->cv[n].value = n; | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 |
1996 | 126 int dmCompareStatFunc(const void *va, const void *vb) |
127 { | |
128 const DMStatValue *pa = va, *pb = vb; | |
129 return pb->count - pa->count; | |
130 } | |
131 | |
132 | |
133 void dmSortStats(DMStats *stats) | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 { |
1996 | 135 qsort(&stats->cv, sizeof(DMStatValue), SET_MAX_ELEMS, dmCompareStatFunc); |
136 } | |
137 | |
138 | |
139 void dmPrintStats(DMStats *stats, const int nmax, const size_t size) | |
140 { | |
141 for (int n = 0; n < nmax; n++) | |
142 { | |
143 printf("$%02x (%1.2f%%), ", | |
144 stats->cv[n].value, | |
145 ((float) stats->cv[n].count * 100.0f) / (float) size); | |
146 } | |
147 printf("\n\n"); | |
148 } | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 int main(int argc, char *argv[]) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 DMCompElem *compBuf = NULL; |
1996 | 154 size_t compBufSize = 0, totalSize = 0; |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 int res; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 dmInitProg("fanalyze", "File format analyzer", "0.1", NULL, NULL); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 dmVerbosity = 1; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 |
1996 | 160 dmInitStats(&totalStats); |
161 | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 // Parse arguments |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 if (!dmArgsProcess(argc, argv, optList, optListN, |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 argHandleOpt, argHandleNonOpt, OPTH_BAILOUT)) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 exit(1); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 if (nsrcFiles < 1) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 dmErrorMsg("Nothing to do. (try --help)\n"); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 goto out; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 // Read input files |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 for (int nfile = 0; nfile < nsrcFiles; nfile++) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 DMSourceFile *file = &srcFiles[nfile]; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 if ((res = dmReadDataFile(NULL, file->filename, &file->data, &file->size)) != DMERR_OK) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 dmErrorMsg("Could not read '%s': %s\n", |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 file->filename, dmErrorStr(res)); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 goto out; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 |
1685
904904f145b4
Various fixes in fanalyze.
Matti Hamalainen <ccr@tnsp.org>
parents:
1683
diff
changeset
|
184 dmPrint(2, "Input #%d: '%s', %" DM_PRIu_SIZE_T " bytes.\n", |
904904f145b4
Various fixes in fanalyze.
Matti Hamalainen <ccr@tnsp.org>
parents:
1683
diff
changeset
|
185 nfile + 1, file->filename, file->size); |
904904f145b4
Various fixes in fanalyze.
Matti Hamalainen <ccr@tnsp.org>
parents:
1683
diff
changeset
|
186 |
904904f145b4
Various fixes in fanalyze.
Matti Hamalainen <ccr@tnsp.org>
parents:
1683
diff
changeset
|
187 if (!compBufSize || file->size < compBufSize) |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 compBufSize = file->size; |
1996 | 189 |
190 totalSize += file->size; | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 // Allocate comparision buffer |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 // XXX: integer overflow? |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 dmPrint(2, "Allocating %d element (%d bytes) comparision buffer.\n", |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 compBufSize, compBufSize * sizeof(DMCompElem)); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 if ((compBuf = dmCalloc(compBufSize, sizeof(DMCompElem))) == NULL) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 dmErrorMsg("Out of memory. Could not allocate comparision buffer!\n"); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 goto out; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 // Begin analyzing .. |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 dmPrint(2, "Analyzing ..\n"); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 for (int nfile = 0; nfile < nsrcFiles; nfile++) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 DMSourceFile *file = &srcFiles[nfile]; |
1996 | 209 dmInitStats(&file->stats); |
210 | |
211 for (size_t offs = 0; offs < file->size; offs++) | |
212 { | |
213 Uint8 bv = file->data[offs]; | |
214 totalStats.cv[bv].count++; | |
215 file->stats.cv[bv].count++; | |
216 } | |
217 | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
218 for (size_t offs = 0; offs < compBufSize; offs++) |
1685
904904f145b4
Various fixes in fanalyze.
Matti Hamalainen <ccr@tnsp.org>
parents:
1683
diff
changeset
|
219 { |
904904f145b4
Various fixes in fanalyze.
Matti Hamalainen <ccr@tnsp.org>
parents:
1683
diff
changeset
|
220 Uint8 data = offs < file->size ? file->data[offs] : 0; |
1996 | 221 compBuf[offs].stats[data]++; |
1685
904904f145b4
Various fixes in fanalyze.
Matti Hamalainen <ccr@tnsp.org>
parents:
1683
diff
changeset
|
222 } |
1996 | 223 |
224 dmSortStats(&file->stats); | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 } |
1996 | 226 dmSortStats(&totalStats); |
227 | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 for (size_t offs = 0; offs < compBufSize; offs++) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 DMCompElem *el = &compBuf[offs]; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 for (int n = 0; n < SET_MAX_ELEMS; n++) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 { |
1996 | 234 if (el->stats[n] > 0) |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 el->variants++; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 el->data = n; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
238 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 // Display results |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 for (size_t offs = 0, n = 0; offs < compBufSize; offs++) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 DMCompElem *el = &compBuf[offs]; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
246 BOOL var = el->variants > 1; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 if (n == 0) |
1685
904904f145b4
Various fixes in fanalyze.
Matti Hamalainen <ccr@tnsp.org>
parents:
1683
diff
changeset
|
249 printf("%08" DM_PRIx_SIZE_T " | ", offs); |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 if (var) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 printf("[%2d] ", el->variants); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 else |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 printf(" %02x ", el->data); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 if (++n >= 16) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 printf("\n"); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 n = 0; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 printf("\n"); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 |
1996 | 265 // Attempt further analysis |
266 for (int nfile = 0; nfile < nsrcFiles; nfile++) | |
267 { | |
268 DMSourceFile *file = &srcFiles[nfile]; | |
269 size_t len = file->size > compBufSize ? compBufSize : file->size; | |
270 for (size_t offs = 0; offs + 4 < len; offs++) | |
271 { | |
272 DMCompElem *elem = &compBuf[offs]; | |
273 | |
274 for (int variant = 3; variant >= 0; variant--) | |
275 { | |
276 size_t nmax = (variant < 2) ? sizeof(Uint16) : sizeof(Uint32); | |
277 Uint32 tmp = 0; | |
278 | |
279 for (size_t n = 0; n < nmax; n++) | |
280 { | |
281 size_t boffs = (variant & 1) ? n : nmax - n; | |
282 | |
283 tmp <<= 8; | |
284 tmp |= file->data[offs + boffs]; | |
285 } | |
286 | |
287 if (file->size - tmp < 32) | |
288 { | |
289 elem->interest[variant] += 32 - (file->size - tmp); | |
290 elem->interestF[variant]++; | |
291 } | |
292 } | |
293 } | |
294 } | |
295 | |
296 printf("\nMore findings:\n"); | |
297 for (size_t offs = 0; offs + 4 < compBufSize; offs++) | |
298 { | |
299 DMCompElem *elem = &compBuf[offs]; | |
300 | |
301 for (int variant = 0; variant < 4; variant++) | |
302 if (elem->interestF[variant] > 0) | |
303 { | |
304 printf("%08" DM_PRIx_SIZE_T " | V%d : %d / %d\n", | |
305 offs, variant, | |
306 elem->interestF[variant], elem->interest[variant]); | |
307 } | |
308 } | |
309 | |
310 printf("\nGlobal most used bytes:\n"); | |
311 dmPrintStats(&totalStats, 16, totalSize); | |
312 | |
313 for (int nfile = 0; nfile < nsrcFiles; nfile++) | |
314 { | |
315 DMSourceFile *file = &srcFiles[nfile]; | |
316 printf("Most used bytes for '%s':\n", file->filename); | |
317 dmPrintStats(&file->stats, 16, file->size); | |
318 } | |
319 | |
1682
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
320 out: |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
321 for (int nfile = 0; nfile < nsrcFiles; nfile++) |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
322 { |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
323 DMSourceFile *file = &srcFiles[nfile]; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
324 dmFree(file->data); |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
325 } |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
326 |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
327 return 0; |
2cfb4806cf71
Add simple and naively implemented multi-file bindiff type file analyzer utility 'fanalyze'.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
328 } |