makedsig.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Program for making the DCC signature file */
  2. #include "LIB_PatternCollector.h"
  3. #include "TPL_PatternCollector.h"
  4. #include "perfhlib.h" /* Symbol table prototypes */
  5. #include <QtCore/QCoreApplication>
  6. #include <QtCore/QStringList>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <malloc.h>
  10. #include <memory.h>
  11. #include <string.h>
  12. #include <algorithm>
  13. /* Symbol table constnts */
  14. #define C 2.2 /* Sparseness of graph. See Czech, Havas and Majewski for details */
  15. /* prototypes */
  16. void saveFile(FILE *fl, const PerfectHash &p_hash, PatternCollector *coll); /* Save the info */
  17. int numKeys; /* Number of useful codeview symbols */
  18. static void printUsage(bool longusage) {
  19. if(longusage)
  20. printf(
  21. "This program is to make 'signatures' of known c and tpl library calls for the dcc program.\n"
  22. "It needs as the first arg the name of a library file, and as the second arg, the name "
  23. "of the signature file to be generated.\n"
  24. "Example: makedsig CL.LIB dccb3l.sig\n"
  25. " or makedsig turbo.tpl dcct4p.sig\n"
  26. );
  27. else
  28. printf("Usage: makedsig <libname> <signame>\n"
  29. "or makedsig -h for help\n");
  30. }
  31. int main(int argc, char *argv[])
  32. {
  33. QCoreApplication app(argc,argv);
  34. FILE *f2; // output file
  35. FILE *srcfile; // .lib file
  36. int s;
  37. if(app.arguments().size()<2) {
  38. printUsage(false);
  39. return 0;
  40. }
  41. QString arg2 = app.arguments()[1];
  42. if (arg2.startsWith("-h") || arg2.startsWith("-?"))
  43. {
  44. printUsage(true);
  45. return 0;
  46. }
  47. PatternCollector *collector;
  48. if(arg2.endsWith("tpl")) {
  49. collector = new TPL_PatternCollector;
  50. } else if(arg2.endsWith(".lib")) {
  51. collector = new LIB_PatternCollector;
  52. }
  53. if ((srcfile = fopen(argv[1], "rb")) == NULL)
  54. {
  55. printf("Cannot read %s\n", argv[1]);
  56. exit(2);
  57. }
  58. if ((f2 = fopen(argv[2], "wb")) == NULL)
  59. {
  60. printf("Cannot write %s\n", argv[2]);
  61. exit(2);
  62. }
  63. fprintf(stderr, "Seed: ");
  64. scanf("%d", &s);
  65. srand(s);
  66. PerfectHash p_hash;
  67. numKeys = collector->readSyms(srcfile); /* Read the keys (symbols) */
  68. printf("Num keys: %d; vertices: %d\n", numKeys, (int)(numKeys*C));
  69. /* Set the parameters for the hash table */
  70. p_hash.setHashParams( numKeys, /* The number of symbols */
  71. PATLEN, /* The length of the pattern to be hashed */
  72. 256, /* The character set of the pattern (0-FF) */
  73. 0, /* Minimum pattern character value */
  74. numKeys*C); /* C is the sparseness of the graph. See Czech,
  75. Havas and Majewski for details */
  76. /* The following two functions are in perfhlib.c */
  77. p_hash.map(collector); /* Perform the mapping. This will call getKey() repeatedly */
  78. p_hash.assign(); /* Generate the function g */
  79. saveFile(f2,p_hash,collector); /* Save the resultant information */
  80. fclose(srcfile);
  81. fclose(f2);
  82. }
  83. /* * * * * * * * * * * * *\
  84. * *
  85. * S a v e t h e s i g f i l e *
  86. * *
  87. \* * * * * * * * * * * * */
  88. void writeFile(FILE *fl,const char *buffer, int len)
  89. {
  90. if ((int)fwrite(buffer, 1, len, fl) != len)
  91. {
  92. printf("Could not write to file\n");
  93. exit(1);
  94. }
  95. }
  96. void writeFileShort(FILE *fl,uint16_t w)
  97. {
  98. uint8_t b;
  99. b = (uint8_t)(w & 0xFF);
  100. writeFile(fl,(char *)&b, 1); /* Write a short little endian */
  101. b = (uint8_t)(w>>8);
  102. writeFile(fl,(char *)&b, 1);
  103. }
  104. void saveFile(FILE *fl, const PerfectHash &p_hash, PatternCollector *coll)
  105. {
  106. int i, len;
  107. const uint16_t *pTable;
  108. writeFile(fl,"dccs", 4); /* Signature */
  109. writeFileShort(fl,numKeys); /* Number of keys */
  110. writeFileShort(fl,(short)(numKeys * C)); /* Number of vertices */
  111. writeFileShort(fl,PATLEN); /* Length of key part of entries */
  112. writeFileShort(fl,SYMLEN); /* Length of symbol part of entries */
  113. /* Write out the tables T1 and T2, with their sig and byte lengths in front */
  114. writeFile(fl,"T1", 2); /* "Signature" */
  115. pTable = p_hash.readT1();
  116. len = PATLEN * 256;
  117. writeFileShort(fl,len * sizeof(uint16_t));
  118. for (i=0; i < len; i++)
  119. {
  120. writeFileShort(fl,pTable[i]);
  121. }
  122. writeFile(fl,"T2", 2);
  123. pTable = p_hash.readT2();
  124. writeFileShort(fl,len * sizeof(uint16_t));
  125. for (i=0; i < len; i++)
  126. {
  127. writeFileShort(fl,pTable[i]);
  128. }
  129. /* Write out g[] */
  130. writeFile(fl,"gg", 2); /* "Signature" */
  131. pTable = p_hash.readG();
  132. len = (short)(numKeys * C);
  133. writeFileShort(fl,len * sizeof(uint16_t));
  134. for (i=0; i < len; i++)
  135. {
  136. writeFileShort(fl,pTable[i]);
  137. }
  138. /* Now the hash table itself */
  139. writeFile(fl,"ht ", 2); /* "Signature" */
  140. writeFileShort(fl,numKeys * (SYMLEN + PATLEN + sizeof(uint16_t))); /* byte len */
  141. for (i=0; i < numKeys; i++)
  142. {
  143. writeFile(fl,(char *)&coll->keys[i], SYMLEN + PATLEN);
  144. }
  145. }