readsig.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* Quick program to read the output from makedsig */
  2. #include <stdio.h>
  3. #include <io.h>
  4. #include <stdlib.h>
  5. #include <memory.h>
  6. #include <string.h>
  7. #include "perfhlib.h"
  8. /* statics */
  9. byte buf[100];
  10. int numKeys; /* Number of hash table entries (keys) */
  11. int numVert; /* Number of vertices in the graph (also size of g[]) */
  12. int PatLen; /* Size of the keys (pattern length) */
  13. int SymLen; /* Max size of the symbols, including null */
  14. FILE *f; /* File being read */
  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. static bool bDispAll = FALSE;
  22. void
  23. main(int argc, char *argv[])
  24. {
  25. word 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. hashParams( /* Set the parameters for the hash table */
  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.
  63. See Czech, Havas and Majewski for details
  64. */
  65. T1base = readT1();
  66. T2base = readT2();
  67. g = readG();
  68. /* Read T1 and T2 tables */
  69. grab(2);
  70. if (memcmp("T1", buf, 2) != 0)
  71. {
  72. printf("Expected 'T1'\n");
  73. exit(3);
  74. }
  75. len = PatLen * 256 * sizeof(word);
  76. w = readFileShort();
  77. if (w != len)
  78. {
  79. printf("Problem with size of T1: file %d, calc %d\n", w, len);
  80. exit(4);
  81. }
  82. if (fread(T1base, 1, len, f) != len)
  83. {
  84. printf("Could not read T1\n");
  85. exit(5);
  86. }
  87. grab(2);
  88. if (memcmp("T2", buf, 2) != 0)
  89. {
  90. printf("Expected 'T2'\n");
  91. exit(3);
  92. }
  93. w = readFileShort();
  94. if (w != len)
  95. {
  96. printf("Problem with size of T2: file %d, calc %d\n", w, len);
  97. exit(4);
  98. }
  99. if (fread(T2base, 1, len, f) != len)
  100. {
  101. printf("Could not read T2\n");
  102. exit(5);
  103. }
  104. /* Now read the function g[] */
  105. grab(2);
  106. if (memcmp("gg", buf, 2) != 0)
  107. {
  108. printf("Expected 'gg'\n");
  109. exit(3);
  110. }
  111. len = numVert * sizeof(word);
  112. w = readFileShort();
  113. if (w != len)
  114. {
  115. printf("Problem with size of g[]: file %d, calc %d\n", w, len);
  116. exit(4);
  117. }
  118. if (fread(g, 1, len, f) != len)
  119. {
  120. printf("Could not read T2\n");
  121. exit(5);
  122. }
  123. /* This is now the hash table */
  124. grab(2);
  125. if (memcmp("ht", buf, 2) != 0)
  126. {
  127. printf("Expected 'ht'\n");
  128. exit(3);
  129. }
  130. w = readFileShort();
  131. if (w != numKeys * (SymLen + PatLen + sizeof(word)))
  132. {
  133. printf("Problem with size of hash table: file %d, calc %d\n", w, len);
  134. exit(6);
  135. }
  136. if (bDispAll)
  137. {
  138. fseek(f, 0, SEEK_CUR); /* Needed due to bug in MS fread()! */
  139. filePos = _lseek(fileno(f), 0, SEEK_CUR);
  140. for (i=0; i < numKeys; i++)
  141. {
  142. grab(SymLen + PatLen);
  143. printf("%16s ", buf);
  144. for (j=0; j < PatLen; j++)
  145. {
  146. printf("%02X", buf[SymLen+j]);
  147. if ((j%4) == 3) printf(" ");
  148. }
  149. printf("\n");
  150. }
  151. printf("\n\n\n");
  152. fseek(f, filePos, SEEK_SET);
  153. }
  154. for (i=0; i < numKeys; i++)
  155. {
  156. grab(SymLen + PatLen);
  157. h = hash(&buf[SymLen]);
  158. if (h != i)
  159. {
  160. printf("Symbol %16s (index %3d) hashed to %d\n",
  161. buf, i, h);
  162. }
  163. }
  164. printf("Done!\n");
  165. fclose(f);
  166. }
  167. void
  168. cleanup(void)
  169. {
  170. /* Free the storage for variable sized tables etc */
  171. if (T1base) free(T1base);
  172. if (T2base) free(T2base);
  173. if (g) free(g);
  174. }
  175. void grab(int n)
  176. {
  177. if (fread(buf, 1, n, f) != (size_t)n)
  178. {
  179. printf("Could not read\n");
  180. exit(11);
  181. }
  182. }
  183. word
  184. readFileShort(void)
  185. {
  186. byte b1, b2;
  187. if (fread(&b1, 1, 1, f) != 1)
  188. {
  189. printf("Could not read\n");
  190. exit(11);
  191. }
  192. if (fread(&b2, 1, 1, f) != 1)
  193. {
  194. printf("Could not read\n");
  195. exit(11);
  196. }
  197. return (b2 << 8) + b1;
  198. }
  199. /* Following two functions not needed unless creating tables */
  200. void getKey(int i, byte **keys)
  201. {
  202. }
  203. /* Display key i */
  204. void
  205. dispKey(int i)
  206. {
  207. }