srchsig.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Quick program to see if a pattern is in a sig file. Pattern is supplied
  2. in a small .bin or .com style file */
  3. #include "perfhlib.h"
  4. #include <memory.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. /* statics */
  8. uint8_t buf[100];
  9. int numKeys; /* Number of hash table entries (keys) */
  10. int numVert; /* Number of vertices in the graph (also size of g[]) */
  11. int PatLen; /* Size of the keys (pattern length) */
  12. int SymLen; /* Max size of the symbols, including null */
  13. FILE *f; /* Sig file being read */
  14. FILE *fpat; /* Pattern file being read */
  15. static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
  16. static uint16_t *g; /* g[] */
  17. #define SYMLEN 16
  18. #define PATLEN 23
  19. typedef struct HT_tag {
  20. /* Hash table structure */
  21. char htSym[SYMLEN];
  22. uint8_t htPat[PATLEN];
  23. } HT;
  24. HT *ht; /* Declare a pointer to a hash table */
  25. /* prototypes */
  26. void grab(int n);
  27. uint16_t readFileShort(void);
  28. void cleanup(void);
  29. extern void fixWildCards(uint8_t pat[]); /* In fixwild.c */
  30. void pattSearch(void);
  31. PerfectHash g_pattern_hasher;
  32. int main(int argc, char *argv[]) {
  33. uint16_t w, len;
  34. int h, i;
  35. int patlen;
  36. if (argc <= 2) {
  37. printf("Usage: srchsig <SigFilename> <PattFilename>\n");
  38. printf("Searches the signature file for the given pattern\n");
  39. printf("e.g. %s dccm8s.sig mypatt.bin\n", argv[0]);
  40. exit(1);
  41. }
  42. if ((f = fopen(argv[1], "rb")) == NULL) {
  43. printf("Cannot open signature file %s\n", argv[1]);
  44. exit(2);
  45. }
  46. if ((fpat = fopen(argv[2], "rb")) == NULL) {
  47. printf("Cannot open pattern file %s\n", argv[2]);
  48. exit(2);
  49. }
  50. /* Read the parameters */
  51. grab(4);
  52. if (memcmp("dccs", buf, 4) != 0) {
  53. printf("Not a dccs file!\n");
  54. exit(3);
  55. }
  56. numKeys = readFileShort();
  57. numVert = readFileShort();
  58. PatLen = readFileShort();
  59. SymLen = readFileShort();
  60. /* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
  61. g_pattern_hasher.setHashParams(
  62. numKeys, /* The number of symbols */
  63. PatLen, /* The length of the pattern to be hashed */
  64. 256, /* The character set of the pattern (0-FF) */
  65. 0, /* Minimum pattern character value */
  66. numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas
  67. and Majewski for details */
  68. T1base = g_pattern_hasher.readT1();
  69. T2base = g_pattern_hasher.readT2();
  70. g = g_pattern_hasher.readG();
  71. /* Read T1 and T2 tables */
  72. grab(2);
  73. if (memcmp("T1", buf, 2) != 0) {
  74. printf("Expected 'T1'\n");
  75. exit(3);
  76. }
  77. len = PatLen * 256 * sizeof(uint16_t);
  78. w = readFileShort();
  79. if (w != len) {
  80. printf("Problem with size of T1: file %d, calc %d\n", w, len);
  81. exit(4);
  82. }
  83. if (fread(T1base, 1, len, f) != len) {
  84. printf("Could not read T1\n");
  85. exit(5);
  86. }
  87. grab(2);
  88. if (memcmp("T2", buf, 2) != 0) {
  89. printf("Expected 'T2'\n");
  90. exit(3);
  91. }
  92. w = readFileShort();
  93. if (w != len) {
  94. printf("Problem with size of T2: file %d, calc %d\n", w, len);
  95. exit(4);
  96. }
  97. if (fread(T2base, 1, len, f) != len) {
  98. printf("Could not read T2\n");
  99. exit(5);
  100. }
  101. /* Now read the function g[] */
  102. grab(2);
  103. if (memcmp("gg", buf, 2) != 0) {
  104. printf("Expected 'gg'\n");
  105. exit(3);
  106. }
  107. len = numVert * sizeof(uint16_t);
  108. w = readFileShort();
  109. if (w != len) {
  110. printf("Problem with size of g[]: file %d, calc %d\n", w, len);
  111. exit(4);
  112. }
  113. if (fread(g, 1, len, f) != len) {
  114. printf("Could not read T2\n");
  115. exit(5);
  116. }
  117. /* This is now the hash table */
  118. /* First allocate space for the table */
  119. if ((ht = (HT *)malloc(numKeys * sizeof(HT))) == 0) {
  120. printf("Could not allocate hash table\n");
  121. exit(1);
  122. }
  123. grab(2);
  124. if (memcmp("ht", buf, 2) != 0) {
  125. printf("Expected 'ht'\n");
  126. exit(3);
  127. }
  128. w = readFileShort();
  129. if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t))) {
  130. printf("Problem with size of hash table: file %d, calc %d\n", w, len);
  131. exit(6);
  132. }
  133. for (i = 0; i < numKeys; i++) {
  134. if ((int)fread(&ht[i], 1, SymLen + PatLen, f) != SymLen + PatLen) {
  135. printf("Could not read\n");
  136. exit(11);
  137. }
  138. }
  139. /* Read the pattern to buf */
  140. if ((patlen = fread(buf, 1, 100, fpat)) == 0) {
  141. printf("Could not read pattern\n");
  142. exit(11);
  143. }
  144. if (patlen != PATLEN) {
  145. printf("Error: pattern length is %d, should be %d\n", patlen, PATLEN);
  146. exit(12);
  147. }
  148. /* Fix the wildcards */
  149. fixWildCards(buf);
  150. printf("Pattern:\n");
  151. for (i = 0; i < PATLEN; i++)
  152. printf("%02X ", buf[i]);
  153. printf("\n");
  154. h = g_pattern_hasher.hash(buf);
  155. printf("Pattern hashed to %d (0x%X), symbol %s\n", h, h, ht[h].htSym);
  156. if (memcmp(ht[h].htPat, buf, PATLEN) == 0) {
  157. printf("Pattern matched");
  158. } else {
  159. printf("Pattern mismatch: found following pattern\n");
  160. for (i = 0; i < PATLEN; i++)
  161. printf("%02X ", ht[h].htPat[i]);
  162. printf("\n");
  163. pattSearch(); /* Look for it the hard way */
  164. }
  165. cleanup();
  166. free(ht);
  167. fclose(f);
  168. fclose(fpat);
  169. return 0;
  170. }
  171. void pattSearch(void) {
  172. int i;
  173. for (i = 0; i < numKeys; i++) {
  174. if ((i % 100) == 0)
  175. printf("\r%d ", i);
  176. if (memcmp(ht[i].htPat, buf, PATLEN) == 0) {
  177. printf("\nPattern matched offset %d (0x%X)\n", i, i);
  178. }
  179. }
  180. printf("\n");
  181. }
  182. void cleanup(void) {
  183. /* Free the storage for variable sized tables etc */
  184. if (T1base)
  185. free(T1base);
  186. if (T2base)
  187. free(T2base);
  188. if (g)
  189. free(g);
  190. }
  191. void grab(int n) {
  192. if (fread(buf, 1, n, f) != (size_t)n) {
  193. printf("Could not read\n");
  194. exit(11);
  195. }
  196. }
  197. uint16_t readFileShort(void) {
  198. uint8_t b1, b2;
  199. if (fread(&b1, 1, 1, f) != 1) {
  200. printf("Could not read\n");
  201. exit(11);
  202. }
  203. if (fread(&b2, 1, 1, f) != 1) {
  204. printf("Could not read\n");
  205. exit(11);
  206. }
  207. return (b2 << 8) + b1;
  208. }
  209. /* Following two functions not needed unless creating tables */
  210. void getKey(int i, uint8_t **keys) {}
  211. /* Display key i */
  212. void dispKey(int i) {}