| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | static unsigned char *ptr[100]; |
| 5 | static int ptrc; |
| 6 | |
| 7 | #define M 256 |
| 8 | |
| 9 | static struct |
| 10 | { |
| 11 | int pos; |
| 12 | int cnt; |
| 13 | } max[M]; |
| 14 | |
| 15 | |
| 16 | static int active[100]; |
| 17 | static int activec; |
| 18 | |
| 19 | static int active_pos; |
| 20 | |
| 21 | static void |
| 22 | fill_max(void) |
| 23 | { |
| 24 | int i, n, b, c, u; |
| 25 | |
| 26 | |
| 27 | memset(max, 0, sizeof(max)); |
| 28 | |
| 29 | for (b = 0; b < 0x800; b++) |
| 30 | { |
| 31 | c = 0; |
| 32 | for (i = 0; i < activec; i++) |
| 33 | { |
| 34 | for (u = i + 1; u < activec; u++) |
| 35 | { |
| 36 | if (ptr[active[i]][b] == ptr[active[u]][b]) |
| 37 | { |
| 38 | goto bad; |
| 39 | } |
| 40 | } |
| 41 | c ++; |
| 42 | bad:; |
| 43 | } |
| 44 | |
| 45 | for (i = 0; i < M; i ++) |
| 46 | { |
| 47 | if (max[i].cnt < c) |
| 48 | { |
| 49 | max[i].pos = b; |
| 50 | max[i].cnt = c; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | select_max(void) |
| 59 | { |
| 60 | int i, m, c; |
| 61 | |
| 62 | m = max[0].cnt; |
| 63 | |
| 64 | if (max[0].cnt == 1) |
| 65 | { |
| 66 | printf("no solution!\n"); |
| 67 | exit(1); |
| 68 | } |
| 69 | c = 0; |
| 70 | for (i = 0; i < M; i++) |
| 71 | { |
| 72 | if (m == max[i].cnt) |
| 73 | { |
| 74 | c++; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | i = random() % c; |
| 79 | active_pos = max[i].pos; |
| 80 | printf("0x%03X (%d) ", active_pos, max[i].cnt); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static void |
| 85 | search_active(void) |
| 86 | { |
| 87 | int i, j, a; |
| 88 | int tmpa[100]; |
| 89 | |
| 90 | |
| 91 | a = 0; |
| 92 | for (i = 0; i < activec; i++) |
| 93 | { |
| 94 | for (j = 0; j < activec; j++) |
| 95 | { |
| 96 | if (i == j) |
| 97 | { |
| 98 | continue; |
| 99 | } |
| 100 | if (ptr[active[i]][active_pos] == |
| 101 | ptr[active[j]][active_pos]) |
| 102 | { |
| 103 | tmpa[a] = active[i]; |
| 104 | a++; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | printf("(%d) ", a); |
| 111 | for (i = 0; i < activec; i++) |
| 112 | { |
| 113 | printf("%02X ", ptr[active[i]][active_pos]); |
| 114 | } |
| 115 | |
| 116 | printf("\n"); |
| 117 | |
| 118 | memcpy(active, tmpa, sizeof(active)); |
| 119 | activec = a; |
| 120 | } |
| 121 | |
| 122 | int |
| 123 | main(int argc, char *argv[]) |
| 124 | { |
| 125 | int i, u, b, c; |
| 126 | FILE *f; |
| 127 | |
| 128 | |
| 129 | srandom(time(NULL)); |
| 130 | |
| 131 | ptrc = argc - 1; |
| 132 | |
| 133 | /* |
| 134 | * read data |
| 135 | */ |
| 136 | |
| 137 | for (i = 0; i < ptrc; i++) |
| 138 | { |
| 139 | ptr[i] = malloc(0x800); |
| 140 | f = fopen(argv[i + 1], "rb"); |
| 141 | fread(ptr[i], 1, 0x800, f); |
| 142 | fclose(f); |
| 143 | active[i] = i; |
| 144 | } |
| 145 | activec = ptrc; |
| 146 | |
| 147 | while (activec > 0) |
| 148 | { |
| 149 | fill_max(); |
| 150 | select_max(); |
| 151 | search_active(); |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |