makedsig.cpp 5.1 KB

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