makedsig.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/QDebug>
  8. #include <QtCore/QStringList>
  9. #include <stdio.h>
  10. #include <stdlib.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. static 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. } else {
  54. qCritical() << "Unsupported file type.";
  55. return -1;
  56. }
  57. if ((srcfile = fopen(argv[1], "rb")) == NULL)
  58. {
  59. printf("Cannot read %s\n", argv[1]);
  60. exit(2);
  61. }
  62. if ((f2 = fopen(argv[2], "wb")) == NULL)
  63. {
  64. printf("Cannot write %s\n", argv[2]);
  65. exit(2);
  66. }
  67. fprintf(stderr, "Seed: ");
  68. scanf("%d", &s);
  69. srand(s);
  70. PerfectHash p_hash;
  71. numKeys = collector->readSyms(srcfile); /* Read the keys (symbols) */
  72. printf("Num keys: %d; vertices: %d\n", numKeys, (int)(numKeys*C));
  73. /* Set the parameters for the hash table */
  74. p_hash.setHashParams( numKeys, /* The number of symbols */
  75. PATLEN, /* The length of the pattern to be hashed */
  76. 256, /* The character set of the pattern (0-FF) */
  77. 0, /* Minimum pattern character value */
  78. numKeys*C); /* C is the sparseness of the graph. See Czech,
  79. Havas and Majewski for details */
  80. /* The following two functions are in perfhlib.c */
  81. p_hash.map(collector); /* Perform the mapping. This will call getKey() repeatedly */
  82. p_hash.assign(); /* Generate the function g */
  83. saveFile(f2,p_hash,collector); /* Save the resultant information */
  84. fclose(srcfile);
  85. fclose(f2);
  86. }
  87. /* * * * * * * * * * * * *\
  88. * *
  89. * S a v e t h e s i g f i l e *
  90. * *
  91. \* * * * * * * * * * * * */
  92. void writeFile(FILE *fl,const char *buffer, int len)
  93. {
  94. if ((int)fwrite(buffer, 1, len, fl) != len)
  95. {
  96. printf("Could not write to file\n");
  97. exit(1);
  98. }
  99. }
  100. void writeFileShort(FILE *fl,uint16_t w)
  101. {
  102. uint8_t b;
  103. b = (uint8_t)(w & 0xFF);
  104. writeFile(fl,(char *)&b, 1); /* Write a short little endian */
  105. b = (uint8_t)(w>>8);
  106. writeFile(fl,(char *)&b, 1);
  107. }
  108. void saveFile(FILE *fl, const PerfectHash &p_hash, PatternCollector *coll)
  109. {
  110. int i, len;
  111. const uint16_t *pTable;
  112. writeFile(fl,"dccs", 4); /* Signature */
  113. writeFileShort(fl,numKeys); /* Number of keys */
  114. writeFileShort(fl,(short)(numKeys * C)); /* Number of vertices */
  115. writeFileShort(fl,PATLEN); /* Length of key part of entries */
  116. writeFileShort(fl,SYMLEN); /* Length of symbol part of entries */
  117. /* Write out the tables T1 and T2, with their sig and byte lengths in front */
  118. writeFile(fl,"T1", 2); /* "Signature" */
  119. pTable = p_hash.readT1();
  120. len = PATLEN * 256;
  121. writeFileShort(fl,len * sizeof(uint16_t));
  122. for (i=0; i < len; i++)
  123. {
  124. writeFileShort(fl,pTable[i]);
  125. }
  126. writeFile(fl,"T2", 2);
  127. pTable = p_hash.readT2();
  128. writeFileShort(fl,len * sizeof(uint16_t));
  129. for (i=0; i < len; i++)
  130. {
  131. writeFileShort(fl,pTable[i]);
  132. }
  133. /* Write out g[] */
  134. writeFile(fl,"gg", 2); /* "Signature" */
  135. pTable = p_hash.readG();
  136. len = (short)(numKeys * C);
  137. writeFileShort(fl,len * sizeof(uint16_t));
  138. for (i=0; i < len; i++)
  139. {
  140. writeFileShort(fl,pTable[i]);
  141. }
  142. /* Now the hash table itself */
  143. writeFile(fl,"ht ", 2); /* "Signature" */
  144. writeFileShort(fl,numKeys * (SYMLEN + PATLEN + sizeof(uint16_t))); /* byte len */
  145. for (i=0; i < numKeys; i++)
  146. {
  147. writeFile(fl,(char *)&coll->keys[i], SYMLEN + PATLEN);
  148. }
  149. }