dispsig.cpp 5.1 KB

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