readsig.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Quick program to read the output from makedsig */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <memory.h>
  5. #include <string.h>
  6. #include "perfhlib.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; /* File being read */
  14. /* prototypes */
  15. void grab(int n);
  16. uint16_t readFileShort(void);
  17. void cleanup(void);
  18. static bool bDispAll = false;
  19. static PerfectHash g_pattern_hasher;
  20. static uint16_t *T1base;
  21. static uint16_t *T2base;
  22. static uint16_t *g;
  23. int main(int argc, char *argv[])
  24. {
  25. uint16_t w, len;
  26. int h, i, j;
  27. long filePos;
  28. if (argc <= 1)
  29. {
  30. printf("Usage: readsig [-a] <SigFilename>\n");
  31. printf("-a for all symbols (else just duplicates)\n");
  32. exit(1);
  33. }
  34. i = 1;
  35. if (strcmp(argv[i], "-a") == 0)
  36. {
  37. i++;
  38. bDispAll = true;
  39. }
  40. if ((f = fopen(argv[i], "rb")) == NULL)
  41. {
  42. printf("Cannot open %s\n", argv[i]);
  43. exit(2);
  44. }
  45. /* Read the parameters */
  46. grab(4);
  47. if (memcmp("dccs", buf, 4) != 0)
  48. {
  49. printf("Not a dccs file!\n");
  50. exit(3);
  51. }
  52. numKeys = readFileShort();
  53. numVert = readFileShort();
  54. PatLen = readFileShort();
  55. SymLen = readFileShort();
  56. /* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
  57. g_pattern_hasher.setHashParams(
  58. numKeys, /* The number of symbols */
  59. PatLen, /* The length of the pattern to be hashed */
  60. 256, /* The character set of the pattern (0-FF) */
  61. 0, /* Minimum pattern character value */
  62. numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas and Majewski for details */
  63. T1base = g_pattern_hasher.readT1();
  64. T2base = g_pattern_hasher.readT2();
  65. g = g_pattern_hasher.readG();
  66. /* Read T1 and T2 tables */
  67. grab(2);
  68. if (memcmp("T1", buf, 2) != 0)
  69. {
  70. printf("Expected 'T1'\n");
  71. exit(3);
  72. }
  73. len = PatLen * 256 * sizeof(uint16_t);
  74. w = readFileShort();
  75. if (w != len)
  76. {
  77. printf("Problem with size of T1: file %d, calc %d\n", w, len);
  78. exit(4);
  79. }
  80. if (fread(T1base, 1, len, f) != len)
  81. {
  82. printf("Could not read T1\n");
  83. exit(5);
  84. }
  85. grab(2);
  86. if (memcmp("T2", buf, 2) != 0)
  87. {
  88. printf("Expected 'T2'\n");
  89. exit(3);
  90. }
  91. w = readFileShort();
  92. if (w != len)
  93. {
  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. {
  99. printf("Could not read T2\n");
  100. exit(5);
  101. }
  102. /* Now read the function g[] */
  103. grab(2);
  104. if (memcmp("gg", buf, 2) != 0)
  105. {
  106. printf("Expected 'gg'\n");
  107. exit(3);
  108. }
  109. len = numVert * sizeof(uint16_t);
  110. w = readFileShort();
  111. if (w != len)
  112. {
  113. printf("Problem with size of g[]: file %d, calc %d\n", w, len);
  114. exit(4);
  115. }
  116. if (fread(g, 1, len, f) != len)
  117. {
  118. printf("Could not read T2\n");
  119. exit(5);
  120. }
  121. /* This is now the hash table */
  122. grab(2);
  123. if (memcmp("ht", buf, 2) != 0)
  124. {
  125. printf("Expected 'ht'\n");
  126. exit(3);
  127. }
  128. w = readFileShort();
  129. if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t)))
  130. {
  131. printf("Problem with size of hash table: file %d, calc %d\n", w, len);
  132. exit(6);
  133. }
  134. if (bDispAll)
  135. {
  136. filePos = ftell(f);
  137. for (i=0; i < numKeys; i++)
  138. {
  139. grab(SymLen + PatLen);
  140. printf("%16s ", buf);
  141. for (j=0; j < PatLen; j++)
  142. {
  143. printf("%02X", buf[SymLen+j]);
  144. if ((j%4) == 3) printf(" ");
  145. }
  146. printf("\n");
  147. }
  148. printf("\n\n\n");
  149. fseek(f, filePos, SEEK_SET);
  150. }
  151. for (i=0; i < numKeys; i++)
  152. {
  153. grab(SymLen + PatLen);
  154. h = g_pattern_hasher.hash(&buf[SymLen]);
  155. if (h != i)
  156. {
  157. printf("Symbol %16s (index %3d) hashed to %d\n", buf, i, h);
  158. }
  159. }
  160. printf("Done!\n");
  161. fclose(f);
  162. return 0;
  163. }
  164. void
  165. cleanup(void)
  166. {
  167. /* Free the storage for variable sized tables etc */
  168. if (T1base) free(T1base);
  169. if (T2base) free(T2base);
  170. if (g) free(g);
  171. }
  172. void grab(int n)
  173. {
  174. if (fread(buf, 1, n, f) != (size_t)n)
  175. {
  176. printf("Could not read\n");
  177. exit(11);
  178. }
  179. }
  180. uint16_t readFileShort(void)
  181. {
  182. uint8_t b1, b2;
  183. if (fread(&b1, 1, 1, f) != 1)
  184. {
  185. printf("Could not read\n");
  186. exit(11);
  187. }
  188. if (fread(&b2, 1, 1, f) != 1)
  189. {
  190. printf("Could not read\n");
  191. exit(11);
  192. }
  193. return (b2 << 8) + b1;
  194. }