home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dosdisas.zip / dispsrch.zip / SRCHSIG.C < prev    next >
C/C++ Source or Header  |  1993-10-22  |  6KB  |  288 lines

  1. /* Quick program to see if a pattern is in a sig file. Pattern is supplied
  2.     in a small .bin or .com style file */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <memory.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;                /* Sig file being read */
  16. FILE *fpat;                /* Pattern file being read */
  17.  
  18. static    word    *T1base, *T2base;    /* Pointers to start of T1, T2 */
  19. static    word    *g;                    /* g[] */
  20.  
  21. #define SYMLEN 16
  22. #define PATLEN 23
  23.  
  24. typedef struct HT_tag
  25. {
  26.     /* Hash table structure */
  27.     char    htSym[SYMLEN];
  28.     byte    htPat[PATLEN];
  29. } HT;
  30.  
  31. HT *ht;                    /* Declare a pointer to a hash table */
  32.  
  33. /* prototypes */
  34. void grab(int n);
  35. word readFileShort(void);
  36. void cleanup(void);
  37. void fixWildCards(char *buf);        /* In fixwild.c */
  38. void pattSearch(void);
  39.  
  40.  
  41. void
  42. main(int argc, char *argv[])
  43. {
  44.     word w, len;
  45.     int h, i;
  46.     int patlen;
  47.  
  48.     if (argc <= 2)
  49.     {
  50.         printf("Usage: srchsig <SigFilename> <PattFilename>\n");
  51.         printf("Searches the signature file for the given pattern\n");
  52.         printf("e.g. %s dccm8s.sig mypatt.bin\n", argv[0]);
  53.         exit(1);
  54.     }
  55.  
  56.     if ((f = fopen(argv[1], "rb")) == NULL)
  57.     {
  58.         printf("Cannot open signature file %s\n", argv[1]);
  59.         exit(2);
  60.     }
  61.  
  62.     if ((fpat = fopen(argv[2], "rb")) == NULL)
  63.     {
  64.         printf("Cannot open pattern file %s\n", argv[2]);
  65.         exit(2);
  66.     }
  67.  
  68.     /* Read the parameters */
  69.     grab(4);
  70.     if (memcmp("dccs", buf, 4) != 0)
  71.     {
  72.         printf("Not a dccs file!\n");
  73.         exit(3);
  74.     }
  75.     numKeys = readFileShort();
  76.     numVert = readFileShort();
  77.     PatLen = readFileShort();
  78.     SymLen = readFileShort();
  79.  
  80.     /* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
  81.     hashParams(                        /* Set the parameters for the hash table */
  82.         numKeys,                    /* The number of symbols */
  83.         PatLen,                        /* The length of the pattern to be hashed */
  84.         256,                        /* The character set of the pattern (0-FF) */
  85.         0,                            /* Minimum pattern character value */
  86.         numVert);                    /* Specifies C, the sparseness of the graph.
  87.                                         See Czech, Havas and Majewski for details
  88.                                     */
  89.  
  90.     T1base    = readT1();
  91.     T2base    = readT2();
  92.     g        = readG();
  93.  
  94.     /* Read T1 and T2 tables */
  95.     grab(2);
  96.     if (memcmp("T1", buf, 2) != 0)
  97.     {
  98.         printf("Expected 'T1'\n");
  99.         exit(3);
  100.     }
  101.     len = PatLen * 256 * sizeof(word);
  102.     w = readFileShort();
  103.     if (w != len)
  104.     {
  105.         printf("Problem with size of T1: file %d, calc %d\n", w, len);
  106.         exit(4);
  107.     }
  108.     if (fread(T1base, 1, len, f) != len)
  109.     {
  110.         printf("Could not read T1\n");
  111.         exit(5);
  112.     }
  113.  
  114.     grab(2);
  115.     if (memcmp("T2", buf, 2) != 0)
  116.     {
  117.         printf("Expected 'T2'\n");
  118.         exit(3);
  119.     }
  120.     w = readFileShort();
  121.     if (w != len)
  122.     {
  123.         printf("Problem with size of T2: file %d, calc %d\n", w, len);
  124.         exit(4);
  125.     }
  126.     if (fread(T2base, 1, len, f) != len)
  127.     {
  128.         printf("Could not read T2\n");
  129.         exit(5);
  130.     }
  131.  
  132.     /* Now read the function g[] */
  133.     grab(2);
  134.     if (memcmp("gg", buf, 2) != 0)
  135.     {
  136.         printf("Expected 'gg'\n");
  137.         exit(3);
  138.     }
  139.     len = numVert * sizeof(word);
  140.     w = readFileShort();
  141.     if (w != len)
  142.     {
  143.         printf("Problem with size of g[]: file %d, calc %d\n", w, len);
  144.         exit(4);
  145.     }
  146.     if (fread(g, 1, len, f) != len)
  147.     {
  148.         printf("Could not read T2\n");
  149.         exit(5);
  150.     }
  151.  
  152.  
  153.     /* This is now the hash table */
  154.     /* First allocate space for the table */
  155.     if ((ht = (HT *)malloc(numKeys * sizeof(HT))) == 0)
  156.     {
  157.         printf("Could not allocate hash table\n");
  158.         exit(1);
  159.     }
  160.     grab(2);
  161.     if (memcmp("ht", buf, 2) != 0)
  162.     {
  163.         printf("Expected 'ht'\n");
  164.         exit(3);
  165.     }
  166.     w = readFileShort();
  167.     if (w != numKeys * (SymLen + PatLen + sizeof(word)))
  168.     {
  169.         printf("Problem with size of hash table: file %d, calc %d\n", w, len);
  170.         exit(6);
  171.     }
  172.  
  173.     for (i=0; i < numKeys; i++)
  174.     {
  175.         if ((int)fread(&ht[i], 1, SymLen + PatLen, f) != SymLen + PatLen)
  176.         {
  177.             printf("Could not read\n");
  178.             exit(11);
  179.         }
  180.     }
  181.  
  182.     /* Read the pattern to buf */
  183.     if ((patlen = fread(buf, 1, 100, fpat)) == 0)
  184.     {
  185.         printf("Could not read pattern\n");
  186.         exit(11);
  187.     }
  188.     if (patlen != PATLEN)
  189.     {
  190.         printf("Error: pattern length is %d, should be %d\n", patlen, PATLEN);
  191.         exit(12);
  192.     }
  193.  
  194.     /* Fix the wildcards */
  195.     fixWildCards(buf);
  196.  
  197.     printf("Pattern:\n");
  198.     for (i=0; i < PATLEN; i++)
  199.         printf("%02X ", buf[i]);
  200.     printf("\n");
  201.  
  202.  
  203.     h = hash(buf);
  204.     printf("Pattern hashed to %d (0x%X), symbol %s\n", h, h, ht[h].htSym);
  205.     if (memcmp(ht[h].htPat, buf, PATLEN) == 0)
  206.     {
  207.         printf("Pattern matched");
  208.     }
  209.     else
  210.     {
  211.         printf("Pattern mismatch: found following pattern\n");
  212.         for (i=0; i < PATLEN; i++)
  213.             printf("%02X ", ht[h].htPat[i]);
  214.         printf("\n");
  215.         pattSearch();                        /* Look for it the hard way */
  216.     }
  217.     cleanup();
  218.     free(ht);
  219.     fclose(f);
  220.     fclose(fpat);
  221.  
  222. }
  223.  
  224. void pattSearch(void)
  225. {
  226.     int i;
  227.  
  228.     for (i=0; i < numKeys; i++)
  229.     {
  230.         if ((i % 100) == 0) printf("\r%d ", i);
  231.         if (memcmp(ht[i].htPat, buf, PATLEN) == 0)
  232.         {
  233.             printf("\nPattern matched offset %d (0x%X)\n", i, i);
  234.         }
  235.     }
  236.     printf("\n");
  237. }
  238.  
  239.  
  240. void
  241. cleanup(void)
  242. {
  243.     /* Free the storage for variable sized tables etc */
  244.     if (T1base)    free(T1base);
  245.     if (T2base)    free(T2base);
  246.     if (g) free(g);
  247. }
  248.  
  249. void grab(int n)
  250. {
  251.     if (fread(buf, 1, n, f) != (size_t)n)
  252.     {
  253.         printf("Could not read\n");
  254.         exit(11);
  255.     }
  256. }
  257.  
  258. word
  259. readFileShort(void)
  260. {
  261.     byte b1, b2;
  262.  
  263.     if (fread(&b1, 1, 1, f) != 1)
  264.     {
  265.         printf("Could not read\n");
  266.         exit(11);
  267.     }
  268.     if (fread(&b2, 1, 1, f) != 1)
  269.     {
  270.         printf("Could not read\n");
  271.         exit(11);
  272.     }
  273.     return (b2 << 8) + b1;
  274. }
  275.  
  276. /* Following two functions not needed unless creating tables */
  277.  
  278. void getKey(int i, byte **keys)
  279. {
  280. }
  281.  
  282. /* Display key i */
  283. void
  284. dispKey(int i)
  285. {
  286. }
  287.  
  288.