home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dosdisas.zip / dispsrch.zip / DISPSIG.C next >
C/C++ Source or Header  |  1994-03-25  |  5KB  |  249 lines

  1. /* Quick program to copy a named signature to a small file */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <memory.h>
  6. #include <string.h>
  7. #include "perfhlib.h"
  8.  
  9. /* statics */
  10. byte buf[100];
  11. int    numKeys;            /* Number of hash table entries (keys) */
  12. int numVert;            /* Number of vertices in the graph (also size of g[]) */
  13. int PatLen;                /* Size of the keys (pattern length) */
  14. int SymLen;                /* Max size of the symbols, including null */
  15. FILE *f;                /* File being read */
  16. FILE *f2;                /* File being written */
  17.  
  18. static    word    *T1base, *T2base;    /* Pointers to start of T1, T2 */
  19. static    word    *g;                    /* g[] */
  20.  
  21. /* prototypes */
  22. void grab(int n);
  23. word readFileShort(void);
  24. void cleanup(void);
  25.  
  26.  
  27. #define SYMLEN      16
  28. #define PATLEN      23
  29.  
  30. /* Hash table structure */
  31. typedef struct HT_tag
  32. {
  33.     char    htSym[SYMLEN];
  34.     byte    htPat[PATLEN];
  35. } HT;
  36.  
  37. HT  ht;                 /* One hash table entry */
  38.  
  39. void
  40. main(int argc, char *argv[])
  41. {
  42.     word w, len;
  43.     int i;
  44.  
  45.     if (argc <= 3)
  46.     {
  47.         printf("Usage: dispsig <SigFilename> <FunctionName> <BinFileName>\n");
  48.         printf("Example: dispsig dccm8s.sig printf printf.bin\n");
  49.         exit(1);
  50.     }
  51.  
  52.     if ((f = fopen(argv[1], "rb")) == NULL)
  53.     {
  54.         printf("Cannot open %s\n", argv[1]);
  55.         exit(2);
  56.     }
  57.  
  58.     if ((f2 = fopen(argv[3], "wb")) == NULL)
  59.     {
  60.         printf("Cannot write to %s\n", argv[3]);
  61.         exit(2);
  62.     }
  63.  
  64.  
  65.     /* Read the parameters */
  66.     grab(4);
  67.     if (memcmp("dccs", buf, 4) != 0)
  68.     {
  69.         printf("Not a dccs file!\n");
  70.         exit(3);
  71.     }
  72.     numKeys = readFileShort();
  73.     numVert = readFileShort();
  74.     PatLen = readFileShort();
  75.     SymLen = readFileShort();
  76.  
  77.     /* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
  78.     hashParams(                        /* Set the parameters for the hash table */
  79.         numKeys,                    /* The number of symbols */
  80.         PatLen,                        /* The length of the pattern to be hashed */
  81.         256,                        /* The character set of the pattern (0-FF) */
  82.         0,                            /* Minimum pattern character value */
  83.         numVert);                    /* Specifies C, the sparseness of the graph.
  84.                                         See Czech, Havas and Majewski for details
  85.                                     */
  86.  
  87.     T1base    = readT1();
  88.     T2base    = readT2();
  89.     g        = readG();
  90.  
  91.     /* Read T1 and T2 tables */
  92.     grab(2);
  93.     if (memcmp("T1", buf, 2) != 0)
  94.     {
  95.         printf("Expected 'T1'\n");
  96.         exit(3);
  97.     }
  98.     len = PatLen * 256 * sizeof(word);
  99.     w = readFileShort();
  100.     if (w != len)
  101.     {
  102.         printf("Problem with size of T1: file %d, calc %d\n", w, len);
  103.         exit(4);
  104.     }
  105.     if (fread(T1base, 1, len, f) != len)
  106.     {
  107.         printf("Could not read T1\n");
  108.         exit(5);
  109.     }
  110.  
  111.     grab(2);
  112.     if (memcmp("T2", buf, 2) != 0)
  113.     {
  114.         printf("Expected 'T2'\n");
  115.         exit(3);
  116.     }
  117.     w = readFileShort();
  118.     if (w != len)
  119.     {
  120.         printf("Problem with size of T2: file %d, calc %d\n", w, len);
  121.         exit(4);
  122.     }
  123.     if (fread(T2base, 1, len, f) != len)
  124.     {
  125.         printf("Could not read T2\n");
  126.         exit(5);
  127.     }
  128.  
  129.     /* Now read the function g[] */
  130.     grab(2);
  131.     if (memcmp("gg", buf, 2) != 0)
  132.     {
  133.         printf("Expected 'gg'\n");
  134.         exit(3);
  135.     }
  136.     len = numVert * sizeof(word);
  137.     w = readFileShort();
  138.     if (w != len)
  139.     {
  140.         printf("Problem with size of g[]: file %d, calc %d\n", w, len);
  141.         exit(4);
  142.     }
  143.     if (fread(g, 1, len, f) != len)
  144.     {
  145.         printf("Could not read T2\n");
  146.         exit(5);
  147.     }
  148.  
  149.  
  150.     /* This is now the hash table */
  151.     grab(2);
  152.     if (memcmp("ht", buf, 2) != 0)
  153.     {
  154.         printf("Expected 'ht'\n");
  155.         exit(3);
  156.     }
  157.     w = readFileShort();
  158.     if (w != numKeys * (SymLen + PatLen + sizeof(word)))
  159.     {
  160.         printf("Problem with size of hash table: file %d, calc %d\n", w, len);
  161.         exit(6);
  162.     }
  163.  
  164.  
  165.     for (i=0; i < numKeys; i++)
  166.     {
  167.         if (fread(&ht, 1, SymLen + PatLen, f) != (size_t)(SymLen + PatLen))
  168.         {
  169.             printf("Could not read pattern %d from %s\n", i, argv[1]);
  170.             exit(7);
  171.         }
  172.         if (stricmp(ht.htSym, argv[2]) == 0)
  173.         {
  174.             /* Found it! */
  175.             break;
  176.         }
  177.  
  178.     }
  179.     fclose(f);
  180.     if (i == numKeys)
  181.     {
  182.         printf("Function %s not found!\n", argv[2]);
  183.         exit(2);
  184.     }
  185.  
  186.     printf("Function %s index %d\n", ht.htSym, i);
  187.     for (i=0; i < PatLen; i++)
  188.     {
  189.         printf("%02X ", ht.htPat[i]);
  190.     }
  191.  
  192.     fwrite(ht.htPat, 1, PatLen, f2);
  193.     fclose(f2);
  194.  
  195.     printf("\n");
  196.  
  197.  
  198. }
  199.  
  200.  
  201. void
  202. cleanup(void)
  203. {
  204.     /* Free the storage for variable sized tables etc */
  205.     if (T1base)    free(T1base);
  206.     if (T2base)    free(T2base);
  207.     if (g) free(g);
  208. }
  209.  
  210. void grab(int n)
  211. {
  212.     if (fread(buf, 1, n, f) != (size_t)n)
  213.     {
  214.         printf("Could not read\n");
  215.         exit(11);
  216.     }
  217. }
  218.  
  219. word
  220. readFileShort(void)
  221. {
  222.     byte b1, b2;
  223.  
  224.     if (fread(&b1, 1, 1, f) != 1)
  225.     {
  226.         printf("Could not read\n");
  227.         exit(11);
  228.     }
  229.     if (fread(&b2, 1, 1, f) != 1)
  230.     {
  231.         printf("Could not read\n");
  232.         exit(11);
  233.     }
  234.     return (b2 << 8) + b1;
  235. }
  236.  
  237. /* Following two functions not needed unless creating tables */
  238.  
  239. void getKey(int i, byte **keys)
  240. {
  241. }
  242.  
  243. /* Display key i */
  244. void
  245. dispKey(int i)
  246. {
  247. }
  248.  
  249.