dispsig.cpp 4.6 KB

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