srchsig.cpp 5.5 KB

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