Merge remote-tracking branch 'origin' into experimental_command_streams

This commit is contained in:
nemerle 2016-05-20 10:20:51 +02:00
commit 8875371cee
7 changed files with 538 additions and 631 deletions

View File

@ -1,2 +1,4 @@
add_subdirectory(dispsrch)
add_subdirectory(makedsig) add_subdirectory(makedsig)
add_subdirectory(readsig)
add_subdirectory(parsehdr) add_subdirectory(parsehdr)

View File

@ -0,0 +1,9 @@
add_executable(dispsig dispsig)
target_link_libraries(dispsig dcc_hash)
qt5_use_modules(dispsig Core)
add_executable(srchsig srchsig)
target_link_libraries(srchsig dcc_hash dcc_lib)
qt5_use_modules(srchsig Core)

View File

@ -1,191 +1,164 @@
/* Quick program to copy a named signature to a small file */ /* Quick program to copy a named signature to a small file */
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include "perfhlib.h" #include "perfhlib.h"
/* statics */ #include <QtCore/QString>
byte buf[100]; #include <memory.h>
int numKeys; /* Number of hash table entries (keys) */ #include <stdio.h>
int numVert; /* Number of vertices in the graph (also size of g[]) */ #include <stdlib.h>
int PatLen; /* Size of the keys (pattern length) */ #include <string.h>
int SymLen; /* Max size of the symbols, including null */
FILE *f; /* File being read */
FILE *f2; /* File being written */
static word *T1base, *T2base; /* Pointers to start of T1, T2 */ /* statics */
static word *g; /* g[] */ uint8_t buf[100];
int numKeys; /* Number of hash table entries (keys) */
int numVert; /* Number of vertices in the graph (also size of g[]) */
int PatLen; /* Size of the keys (pattern length) */
int SymLen; /* Max size of the symbols, including null */
FILE *f; /* File being read */
FILE *f2; /* File being written */
static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
static uint16_t *g; /* g[] */
/* prototypes */ /* prototypes */
void grab(int n); void grab(int n);
word readFileShort(void); uint16_t readFileShort(void);
void cleanup(void); void cleanup(void);
#define SYMLEN 16
#define SYMLEN 16 #define PATLEN 23
#define PATLEN 23
/* Hash table structure */ /* Hash table structure */
typedef struct HT_tag typedef struct HT_tag {
{ char htSym[SYMLEN];
char htSym[SYMLEN]; uint8_t htPat[PATLEN];
byte htPat[PATLEN];
} HT; } HT;
HT ht; /* One hash table entry */ HT ht; /* One hash table entry */
PerfectHash g_pattern_hasher;
int main(int argc, char *argv[]) {
uint16_t w, len;
int i;
void if (argc <= 3) {
main(int argc, char *argv[]) printf("Usage: dispsig <SigFilename> <FunctionName> <BinFileName>\n");
{ printf("Example: dispsig dccm8s.sig printf printf.bin\n");
word w, len; exit(1);
int i; }
if (argc <= 3) if ((f = fopen(argv[1], "rb")) == NULL) {
{ printf("Cannot open %s\n", argv[1]);
printf("Usage: dispsig <SigFilename> <FunctionName> <BinFileName>\n"); exit(2);
printf("Example: dispsig dccm8s.sig printf printf.bin\n"); }
exit(1);
}
if ((f = fopen(argv[1], "rb")) == NULL) if ((f2 = fopen(argv[3], "wb")) == NULL) {
{ printf("Cannot write to %s\n", argv[3]);
printf("Cannot open %s\n", argv[1]); exit(2);
exit(2); }
}
if ((f2 = fopen(argv[3], "wb")) == NULL) /* Read the parameters */
{ grab(4);
printf("Cannot write to %s\n", argv[3]); if (memcmp("dccs", buf, 4) != 0) {
exit(2); printf("Not a dccs file!\n");
} exit(3);
}
numKeys = readFileShort();
numVert = readFileShort();
PatLen = readFileShort();
SymLen = readFileShort();
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
g_pattern_hasher.setHashParams(
numKeys, /* The number of symbols */
PatLen, /* The length of the pattern to be hashed */
256, /* The character set of the pattern (0-FF) */
0, /* Minimum pattern character value */
numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas and Majewski for details */
T1base = g_pattern_hasher.readT1();
T2base = g_pattern_hasher.readT2();
g = g_pattern_hasher.readG();
/* Read the parameters */ /* Read T1 and T2 tables */
grab(4); grab(2);
if (memcmp("dccs", buf, 4) != 0) if (memcmp("T1", buf, 2) != 0) {
{ printf("Expected 'T1'\n");
printf("Not a dccs file!\n"); exit(3);
exit(3); }
} len = PatLen * 256 * sizeof(uint16_t);
numKeys = readFileShort(); w = readFileShort();
numVert = readFileShort(); if (w != len) {
PatLen = readFileShort(); printf("Problem with size of T1: file %d, calc %d\n", w, len);
SymLen = readFileShort(); exit(4);
}
if (fread(T1base, 1, len, f) != len) {
printf("Could not read T1\n");
exit(5);
}
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */ grab(2);
hashParams( /* Set the parameters for the hash table */ if (memcmp("T2", buf, 2) != 0) {
numKeys, /* The number of symbols */ printf("Expected 'T2'\n");
PatLen, /* The length of the pattern to be hashed */ exit(3);
256, /* The character set of the pattern (0-FF) */ }
0, /* Minimum pattern character value */ w = readFileShort();
numVert); /* Specifies C, the sparseness of the graph. if (w != len) {
See Czech, Havas and Majewski for details printf("Problem with size of T2: file %d, calc %d\n", w, len);
*/ exit(4);
}
if (fread(T2base, 1, len, f) != len) {
printf("Could not read T2\n");
exit(5);
}
T1base = readT1(); /* Now read the function g[] */
T2base = readT2(); grab(2);
g = readG(); if (memcmp("gg", buf, 2) != 0) {
printf("Expected 'gg'\n");
exit(3);
}
len = numVert * sizeof(uint16_t);
w = readFileShort();
if (w != len) {
printf("Problem with size of g[]: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(g, 1, len, f) != len) {
printf("Could not read T2\n");
exit(5);
}
/* Read T1 and T2 tables */ /* This is now the hash table */
grab(2); grab(2);
if (memcmp("T1", buf, 2) != 0) if (memcmp("ht", buf, 2) != 0) {
{ printf("Expected 'ht'\n");
printf("Expected 'T1'\n"); exit(3);
exit(3); }
} w = readFileShort();
len = PatLen * 256 * sizeof(word); if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t))) {
w = readFileShort(); printf("Problem with size of hash table: file %d, calc %d\n", w, len);
if (w != len) exit(6);
{ }
printf("Problem with size of T1: file %d, calc %d\n", w, len); QString argv2(argv[2]);
exit(4); for (i = 0; i < numKeys; i++) {
} if (fread(&ht, 1, SymLen + PatLen, f) != (size_t)(SymLen + PatLen)) {
if (fread(T1base, 1, len, f) != len)
{
printf("Could not read T1\n");
exit(5);
}
grab(2);
if (memcmp("T2", buf, 2) != 0)
{
printf("Expected 'T2'\n");
exit(3);
}
w = readFileShort();
if (w != len)
{
printf("Problem with size of T2: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(T2base, 1, len, f) != len)
{
printf("Could not read T2\n");
exit(5);
}
/* Now read the function g[] */
grab(2);
if (memcmp("gg", buf, 2) != 0)
{
printf("Expected 'gg'\n");
exit(3);
}
len = numVert * sizeof(word);
w = readFileShort();
if (w != len)
{
printf("Problem with size of g[]: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(g, 1, len, f) != len)
{
printf("Could not read T2\n");
exit(5);
}
/* This is now the hash table */
grab(2);
if (memcmp("ht", buf, 2) != 0)
{
printf("Expected 'ht'\n");
exit(3);
}
w = readFileShort();
if (w != numKeys * (SymLen + PatLen + sizeof(word)))
{
printf("Problem with size of hash table: file %d, calc %d\n", w, len);
exit(6);
}
for (i=0; i < numKeys; i++)
{
if (fread(&ht, 1, SymLen + PatLen, f) != (size_t)(SymLen + PatLen))
{
printf("Could not read pattern %d from %s\n", i, argv[1]); printf("Could not read pattern %d from %s\n", i, argv[1]);
exit(7); exit(7);
} }
if (stricmp(ht.htSym, argv[2]) == 0) if (argv2.compare(ht.htSym, Qt::CaseInsensitive) == 0) {
{
/* Found it! */ /* Found it! */
break; break;
} }
}
}
fclose(f); fclose(f);
if (i == numKeys) if (i == numKeys) {
{
printf("Function %s not found!\n", argv[2]); printf("Function %s not found!\n", argv[2]);
exit(2); exit(2);
} }
printf("Function %s index %d\n", ht.htSym, i); printf("Function %s index %d\n", ht.htSym, i);
for (i=0; i < PatLen; i++) for (i = 0; i < PatLen; i++) {
{
printf("%02X ", ht.htPat[i]); printf("%02X ", ht.htPat[i]);
} }
@ -193,56 +166,36 @@ main(int argc, char *argv[])
fclose(f2); fclose(f2);
printf("\n"); printf("\n");
} }
void cleanup(void) {
void /* Free the storage for variable sized tables etc */
cleanup(void) if (T1base)
{ free(T1base);
/* Free the storage for variable sized tables etc */ if (T2base)
if (T1base) free(T1base); free(T2base);
if (T2base) free(T2base); if (g)
if (g) free(g); free(g);
} }
void grab(int n) void grab(int n) {
{ if (fread(buf, 1, n, f) != (size_t)n) {
if (fread(buf, 1, n, f) != (size_t)n) printf("Could not read\n");
{ exit(11);
printf("Could not read\n"); }
exit(11);
}
} }
word uint16_t readFileShort(void) {
readFileShort(void) uint8_t b1, b2;
{
byte b1, b2;
if (fread(&b1, 1, 1, f) != 1) if (fread(&b1, 1, 1, f) != 1) {
{ printf("Could not read\n");
printf("Could not read\n"); exit(11);
exit(11); }
} if (fread(&b2, 1, 1, f) != 1) {
if (fread(&b2, 1, 1, f) != 1) printf("Could not read\n");
{ exit(11);
printf("Could not read\n"); }
exit(11); return (b2 << 8) + b1;
}
return (b2 << 8) + b1;
}
/* Following two functions not needed unless creating tables */
void getKey(int i, byte **keys)
{
}
/* Display key i */
void
dispKey(int i)
{
} }

View File

@ -1,287 +1,245 @@
/* Quick program to see if a pattern is in a sig file. Pattern is supplied /* Quick program to see if a pattern is in a sig file. Pattern is supplied
in a small .bin or .com style file */ in a small .bin or .com style file */
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "perfhlib.h" #include "perfhlib.h"
/* statics */ #include <memory.h>
byte buf[100]; #include <stdio.h>
int numKeys; /* Number of hash table entries (keys) */ #include <stdlib.h>
int numVert; /* Number of vertices in the graph (also size of g[]) */
int PatLen; /* Size of the keys (pattern length) */
int SymLen; /* Max size of the symbols, including null */
FILE *f; /* Sig file being read */
FILE *fpat; /* Pattern file being read */
static word *T1base, *T2base; /* Pointers to start of T1, T2 */ /* statics */
static word *g; /* g[] */ uint8_t buf[100];
int numKeys; /* Number of hash table entries (keys) */
int numVert; /* Number of vertices in the graph (also size of g[]) */
int PatLen; /* Size of the keys (pattern length) */
int SymLen; /* Max size of the symbols, including null */
FILE *f; /* Sig file being read */
FILE *fpat; /* Pattern file being read */
static uint16_t *T1base, *T2base; /* Pointers to start of T1, T2 */
static uint16_t *g; /* g[] */
#define SYMLEN 16 #define SYMLEN 16
#define PATLEN 23 #define PATLEN 23
typedef struct HT_tag typedef struct HT_tag {
{ /* Hash table structure */
/* Hash table structure */ char htSym[SYMLEN];
char htSym[SYMLEN]; uint8_t htPat[PATLEN];
byte htPat[PATLEN];
} HT; } HT;
HT *ht; /* Declare a pointer to a hash table */ HT *ht; /* Declare a pointer to a hash table */
/* prototypes */ /* prototypes */
void grab(int n); void grab(int n);
word readFileShort(void); uint16_t readFileShort(void);
void cleanup(void); void cleanup(void);
void fixWildCards(char *buf); /* In fixwild.c */ extern void fixWildCards(uint8_t pat[]); /* In fixwild.c */
void pattSearch(void); void pattSearch(void);
PerfectHash g_pattern_hasher;
int main(int argc, char *argv[]) {
uint16_t w, len;
int h, i;
int patlen;
void if (argc <= 2) {
main(int argc, char *argv[]) printf("Usage: srchsig <SigFilename> <PattFilename>\n");
{ printf("Searches the signature file for the given pattern\n");
word w, len; printf("e.g. %s dccm8s.sig mypatt.bin\n", argv[0]);
int h, i; exit(1);
int patlen; }
if (argc <= 2) if ((f = fopen(argv[1], "rb")) == NULL) {
{ printf("Cannot open signature file %s\n", argv[1]);
printf("Usage: srchsig <SigFilename> <PattFilename>\n"); exit(2);
printf("Searches the signature file for the given pattern\n"); }
printf("e.g. %s dccm8s.sig mypatt.bin\n", argv[0]);
exit(1);
}
if ((f = fopen(argv[1], "rb")) == NULL) if ((fpat = fopen(argv[2], "rb")) == NULL) {
{ printf("Cannot open pattern file %s\n", argv[2]);
printf("Cannot open signature file %s\n", argv[1]); exit(2);
exit(2); }
}
if ((fpat = fopen(argv[2], "rb")) == NULL) /* Read the parameters */
{ grab(4);
printf("Cannot open pattern file %s\n", argv[2]); if (memcmp("dccs", buf, 4) != 0) {
exit(2); printf("Not a dccs file!\n");
} exit(3);
}
numKeys = readFileShort();
numVert = readFileShort();
PatLen = readFileShort();
SymLen = readFileShort();
/* Read the parameters */ /* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
grab(4); g_pattern_hasher.setHashParams(
if (memcmp("dccs", buf, 4) != 0) numKeys, /* The number of symbols */
{ PatLen, /* The length of the pattern to be hashed */
printf("Not a dccs file!\n"); 256, /* The character set of the pattern (0-FF) */
exit(3); 0, /* Minimum pattern character value */
} numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas
numKeys = readFileShort(); and Majewski for details */
numVert = readFileShort();
PatLen = readFileShort();
SymLen = readFileShort();
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */ T1base = g_pattern_hasher.readT1();
hashParams( /* Set the parameters for the hash table */ T2base = g_pattern_hasher.readT2();
numKeys, /* The number of symbols */ g = g_pattern_hasher.readG();
PatLen, /* The length of the pattern to be hashed */
256, /* The character set of the pattern (0-FF) */
0, /* Minimum pattern character value */
numVert); /* Specifies C, the sparseness of the graph.
See Czech, Havas and Majewski for details
*/
T1base = readT1(); /* Read T1 and T2 tables */
T2base = readT2(); grab(2);
g = readG(); if (memcmp("T1", buf, 2) != 0) {
printf("Expected 'T1'\n");
exit(3);
}
len = PatLen * 256 * sizeof(uint16_t);
w = readFileShort();
if (w != len) {
printf("Problem with size of T1: file %d, calc %d\n", w, len);
exit(4);
}
if (fread(T1base, 1, len, f) != len) {
printf("Could not read T1\n");
exit(5);
}
/* Read T1 and T2 tables */ grab(2);
grab(2); if (memcmp("T2", buf, 2) != 0) {
if (memcmp("T1", buf, 2) != 0) printf("Expected 'T2'\n");
{ exit(3);
printf("Expected 'T1'\n"); }
exit(3); w = readFileShort();
} if (w != len) {
len = PatLen * 256 * sizeof(word); printf("Problem with size of T2: file %d, calc %d\n", w, len);
w = readFileShort(); exit(4);
if (w != len) }
{ if (fread(T2base, 1, len, f) != len) {
printf("Problem with size of T1: file %d, calc %d\n", w, len); printf("Could not read T2\n");
exit(4); exit(5);
} }
if (fread(T1base, 1, len, f) != len)
{
printf("Could not read T1\n");
exit(5);
}
grab(2); /* Now read the function g[] */
if (memcmp("T2", buf, 2) != 0) grab(2);
{ if (memcmp("gg", buf, 2) != 0) {
printf("Expected 'T2'\n"); printf("Expected 'gg'\n");
exit(3); exit(3);
} }
w = readFileShort(); len = numVert * sizeof(uint16_t);
if (w != len) w = readFileShort();
{ if (w != len) {
printf("Problem with size of T2: file %d, calc %d\n", w, len); printf("Problem with size of g[]: file %d, calc %d\n", w, len);
exit(4); exit(4);
} }
if (fread(T2base, 1, len, f) != len) if (fread(g, 1, len, f) != len) {
{ printf("Could not read T2\n");
printf("Could not read T2\n"); exit(5);
exit(5); }
}
/* Now read the function g[] */ /* This is now the hash table */
grab(2); /* First allocate space for the table */
if (memcmp("gg", buf, 2) != 0) if ((ht = (HT *)malloc(numKeys * sizeof(HT))) == 0) {
{ printf("Could not allocate hash table\n");
printf("Expected 'gg'\n"); exit(1);
exit(3); }
} grab(2);
len = numVert * sizeof(word); if (memcmp("ht", buf, 2) != 0) {
w = readFileShort(); printf("Expected 'ht'\n");
if (w != len) exit(3);
{ }
printf("Problem with size of g[]: file %d, calc %d\n", w, len); w = readFileShort();
exit(4); if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t))) {
} printf("Problem with size of hash table: file %d, calc %d\n", w, len);
if (fread(g, 1, len, f) != len) exit(6);
{ }
printf("Could not read T2\n");
exit(5);
}
for (i = 0; i < numKeys; i++) {
if ((int)fread(&ht[i], 1, SymLen + PatLen, f) != SymLen + PatLen) {
printf("Could not read\n");
exit(11);
}
}
/* This is now the hash table */ /* Read the pattern to buf */
/* First allocate space for the table */ if ((patlen = fread(buf, 1, 100, fpat)) == 0) {
if ((ht = (HT *)malloc(numKeys * sizeof(HT))) == 0) printf("Could not read pattern\n");
{ exit(11);
printf("Could not allocate hash table\n"); }
exit(1); if (patlen != PATLEN) {
}
grab(2);
if (memcmp("ht", buf, 2) != 0)
{
printf("Expected 'ht'\n");
exit(3);
}
w = readFileShort();
if (w != numKeys * (SymLen + PatLen + sizeof(word)))
{
printf("Problem with size of hash table: file %d, calc %d\n", w, len);
exit(6);
}
for (i=0; i < numKeys; i++)
{
if ((int)fread(&ht[i], 1, SymLen + PatLen, f) != SymLen + PatLen)
{
printf("Could not read\n");
exit(11);
}
}
/* Read the pattern to buf */
if ((patlen = fread(buf, 1, 100, fpat)) == 0)
{
printf("Could not read pattern\n");
exit(11);
}
if (patlen != PATLEN)
{
printf("Error: pattern length is %d, should be %d\n", patlen, PATLEN); printf("Error: pattern length is %d, should be %d\n", patlen, PATLEN);
exit(12); exit(12);
} }
/* Fix the wildcards */ /* Fix the wildcards */
fixWildCards(buf); fixWildCards(buf);
printf("Pattern:\n"); printf("Pattern:\n");
for (i=0; i < PATLEN; i++) for (i = 0; i < PATLEN; i++)
printf("%02X ", buf[i]); printf("%02X ", buf[i]);
printf("\n"); printf("\n");
h = hash(buf);
printf("Pattern hashed to %d (0x%X), symbol %s\n", h, h, ht[h].htSym);
if (memcmp(ht[h].htPat, buf, PATLEN) == 0)
{
printf("Pattern matched");
}
else
{
printf("Pattern mismatch: found following pattern\n");
for (i=0; i < PATLEN; i++)
printf("%02X ", ht[h].htPat[i]);
printf("\n");
pattSearch(); /* Look for it the hard way */
}
cleanup();
free(ht);
fclose(f);
fclose(fpat);
h = g_pattern_hasher.hash(buf);
printf("Pattern hashed to %d (0x%X), symbol %s\n", h, h, ht[h].htSym);
if (memcmp(ht[h].htPat, buf, PATLEN) == 0) {
printf("Pattern matched");
} else {
printf("Pattern mismatch: found following pattern\n");
for (i = 0; i < PATLEN; i++)
printf("%02X ", ht[h].htPat[i]);
printf("\n");
pattSearch(); /* Look for it the hard way */
}
cleanup();
free(ht);
fclose(f);
fclose(fpat);
return 0;
} }
void pattSearch(void) void pattSearch(void) {
{ int i;
int i;
for (i=0; i < numKeys; i++) for (i = 0; i < numKeys; i++) {
{ if ((i % 100) == 0)
if ((i % 100) == 0) printf("\r%d ", i); printf("\r%d ", i);
if (memcmp(ht[i].htPat, buf, PATLEN) == 0) if (memcmp(ht[i].htPat, buf, PATLEN) == 0) {
{ printf("\nPattern matched offset %d (0x%X)\n", i, i);
printf("\nPattern matched offset %d (0x%X)\n", i, i); }
} }
} printf("\n");
printf("\n");
} }
void cleanup(void) {
void /* Free the storage for variable sized tables etc */
cleanup(void) if (T1base)
{ free(T1base);
/* Free the storage for variable sized tables etc */ if (T2base)
if (T1base) free(T1base); free(T2base);
if (T2base) free(T2base); if (g)
if (g) free(g); free(g);
} }
void grab(int n) void grab(int n) {
{ if (fread(buf, 1, n, f) != (size_t)n) {
if (fread(buf, 1, n, f) != (size_t)n) printf("Could not read\n");
{ exit(11);
printf("Could not read\n"); }
exit(11);
}
} }
word uint16_t readFileShort(void) {
readFileShort(void) uint8_t b1, b2;
{
byte b1, b2;
if (fread(&b1, 1, 1, f) != 1) if (fread(&b1, 1, 1, f) != 1) {
{ printf("Could not read\n");
printf("Could not read\n"); exit(11);
exit(11); }
} if (fread(&b2, 1, 1, f) != 1) {
if (fread(&b2, 1, 1, f) != 1) printf("Could not read\n");
{ exit(11);
printf("Could not read\n"); }
exit(11); return (b2 << 8) + b1;
}
return (b2 << 8) + b1;
} }
/* Following two functions not needed unless creating tables */ /* Following two functions not needed unless creating tables */
void getKey(int i, byte **keys) void getKey(int i, uint8_t **keys) {}
{
}
/* Display key i */ /* Display key i */
void void dispKey(int i) {}
dispKey(int i)
{
}

View File

@ -32,7 +32,7 @@ byte func; /* Function header detected */
byte hash_ext; byte hash_ext;
int curly; /* Level inside curly brackets */ int curly; /* Level inside curly brackets */
int xtern; /* Level inside a extern "C" {} situation */ int xtern; /* Level inside a extern "C" {} situation */
int round; /* Level inside () */ int round1; /* Level inside () */
int line, col; int line, col;
dword chars; dword chars;
char lastch; char lastch;
@ -151,7 +151,7 @@ void phInit(char *filename) // filename is for reference only!!!
slosh = last_slosh = start = func = comment = double_slash = hash = ignore1 = slosh = last_slosh = start = func = comment = double_slash = hash = ignore1 =
quote1 = quote2 = hash_ext = false; quote1 = quote2 = hash_ext = false;
buff_idx = curly = xtern = col = round = 0; buff_idx = curly = xtern = col = round1 = 0;
line = 1; line = 1;
@ -268,12 +268,12 @@ void phChar(char ch) {
if (!IsIgnore()) { if (!IsIgnore()) {
char st[80]; char st[80];
if ((curly == xtern) && (round == 0) && (start)) { if ((curly == xtern) && (round1 == 0) && (start)) {
func = true; func = true;
DBG("[FUNCTION]") DBG("[FUNCTION]")
} }
round++; round1++;
sprintf(st, "[ROUND++ %d]", round); sprintf(st, "[ROUND++ %d]", round1);
DBG(st) DBG(st)
} }
break; break;
@ -282,9 +282,9 @@ void phChar(char ch) {
if (!IsIgnore()) { if (!IsIgnore()) {
char st[80]; char st[80];
if (round > 0) { if (round1 > 0) {
round--; round1--;
sprintf(st, "[ROUND-- %d]", round); sprintf(st, "[ROUND-- %d]", round1);
DBG(st) DBG(st)
} else { } else {
ERR("too many \")\"\n"); ERR("too many \")\"\n");
@ -464,8 +464,8 @@ boolT phPost(void) {
err = false; err = false;
} }
if (round > 0) { if (round1 > 0) {
sprintf(msg, "EOF: ( level = %d", round); sprintf(msg, "EOF: ( level = %d", round1);
WARN(msg); WARN(msg);
err = false; err = false;
} }
@ -835,7 +835,6 @@ boolT isAttrib(void) {
return false; return false;
return true; return true;
} }
boolT isCdecl(void) { boolT isCdecl(void) {
return ((strcmp(token, "__cdecl") == 0) || (strcmp(token, "_Cdecl") == 0) || return ((strcmp(token, "__cdecl") == 0) || (strcmp(token, "_Cdecl") == 0) ||
(strcmp(token, "cdecl") == 0)); (strcmp(token, "cdecl") == 0));

View File

@ -0,0 +1,4 @@
add_executable(readsig readsig.cpp)
target_link_libraries(readsig dcc_hash)
qt5_use_modules(readsig Core)

View File

@ -1,239 +1,221 @@
/* Quick program to read the output from makedsig */ /* Quick program to read the output from makedsig */
#include <stdio.h> #include <stdio.h>
#include <io.h>
#include <stdlib.h> #include <stdlib.h>
#include <memory.h> #include <memory.h>
#include <string.h> #include <string.h>
#include "perfhlib.h" #include "perfhlib.h"
/* statics */ /* statics */
byte buf[100]; uint8_t buf[100];
int numKeys; /* Number of hash table entries (keys) */ int numKeys; /* Number of hash table entries (keys) */
int numVert; /* Number of vertices in the graph (also size of g[]) */ int numVert; /* Number of vertices in the graph (also size of g[]) */
int PatLen; /* Size of the keys (pattern length) */ int PatLen; /* Size of the keys (pattern length) */
int SymLen; /* Max size of the symbols, including null */ int SymLen; /* Max size of the symbols, including null */
FILE *f; /* File being read */ FILE *f; /* File being read */
static word *T1base, *T2base; /* Pointers to start of T1, T2 */
static word *g; /* g[] */
/* prototypes */ /* prototypes */
void grab(int n); void grab(int n);
word readFileShort(void); uint16_t readFileShort(void);
void cleanup(void); void cleanup(void);
static bool bDispAll = FALSE; static bool bDispAll = false;
static PerfectHash g_pattern_hasher;
static uint16_t *T1base;
static uint16_t *T2base;
static uint16_t *g;
void int main(int argc, char *argv[])
main(int argc, char *argv[])
{ {
word w, len; uint16_t w, len;
int h, i, j; int h, i, j;
long filePos; long filePos;
if (argc <= 1) if (argc <= 1)
{ {
printf("Usage: readsig [-a] <SigFilename>\n"); printf("Usage: readsig [-a] <SigFilename>\n");
printf("-a for all symbols (else just duplicates)\n"); printf("-a for all symbols (else just duplicates)\n");
exit(1); exit(1);
} }
i = 1; i = 1;
if (strcmp(argv[i], "-a") == 0) if (strcmp(argv[i], "-a") == 0)
{ {
i++; i++;
bDispAll = TRUE; bDispAll = true;
} }
if ((f = fopen(argv[i], "rb")) == NULL) if ((f = fopen(argv[i], "rb")) == NULL)
{ {
printf("Cannot open %s\n", argv[i]); printf("Cannot open %s\n", argv[i]);
exit(2); exit(2);
} }
/* Read the parameters */ /* Read the parameters */
grab(4); grab(4);
if (memcmp("dccs", buf, 4) != 0) if (memcmp("dccs", buf, 4) != 0)
{ {
printf("Not a dccs file!\n"); printf("Not a dccs file!\n");
exit(3); exit(3);
} }
numKeys = readFileShort(); numKeys = readFileShort();
numVert = readFileShort(); numVert = readFileShort();
PatLen = readFileShort(); PatLen = readFileShort();
SymLen = readFileShort(); SymLen = readFileShort();
/* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */ /* Initialise the perfhlib stuff. Also allocates T1, T2, g, etc */
hashParams( /* Set the parameters for the hash table */ g_pattern_hasher.setHashParams(
numKeys, /* The number of symbols */ numKeys, /* The number of symbols */
PatLen, /* The length of the pattern to be hashed */ PatLen, /* The length of the pattern to be hashed */
256, /* The character set of the pattern (0-FF) */ 256, /* The character set of the pattern (0-FF) */
0, /* Minimum pattern character value */ 0, /* Minimum pattern character value */
numVert); /* Specifies C, the sparseness of the graph. numVert); /* Specifies C, the sparseness of the graph. See Czech, Havas and Majewski for details */
See Czech, Havas and Majewski for details
*/
T1base = readT1(); T1base = g_pattern_hasher.readT1();
T2base = readT2(); T2base = g_pattern_hasher.readT2();
g = readG(); g = g_pattern_hasher.readG();
/* Read T1 and T2 tables */ /* Read T1 and T2 tables */
grab(2); grab(2);
if (memcmp("T1", buf, 2) != 0) if (memcmp("T1", buf, 2) != 0)
{ {
printf("Expected 'T1'\n"); printf("Expected 'T1'\n");
exit(3); exit(3);
} }
len = PatLen * 256 * sizeof(word); len = PatLen * 256 * sizeof(uint16_t);
w = readFileShort(); w = readFileShort();
if (w != len) if (w != len)
{ {
printf("Problem with size of T1: file %d, calc %d\n", w, len); printf("Problem with size of T1: file %d, calc %d\n", w, len);
exit(4); exit(4);
} }
if (fread(T1base, 1, len, f) != len) if (fread(T1base, 1, len, f) != len)
{ {
printf("Could not read T1\n"); printf("Could not read T1\n");
exit(5); exit(5);
} }
grab(2); grab(2);
if (memcmp("T2", buf, 2) != 0) if (memcmp("T2", buf, 2) != 0)
{ {
printf("Expected 'T2'\n"); printf("Expected 'T2'\n");
exit(3); exit(3);
} }
w = readFileShort(); w = readFileShort();
if (w != len) if (w != len)
{ {
printf("Problem with size of T2: file %d, calc %d\n", w, len); printf("Problem with size of T2: file %d, calc %d\n", w, len);
exit(4); exit(4);
} }
if (fread(T2base, 1, len, f) != len) if (fread(T2base, 1, len, f) != len)
{ {
printf("Could not read T2\n"); printf("Could not read T2\n");
exit(5); exit(5);
} }
/* Now read the function g[] */ /* Now read the function g[] */
grab(2); grab(2);
if (memcmp("gg", buf, 2) != 0) if (memcmp("gg", buf, 2) != 0)
{ {
printf("Expected 'gg'\n"); printf("Expected 'gg'\n");
exit(3); exit(3);
} }
len = numVert * sizeof(word); len = numVert * sizeof(uint16_t);
w = readFileShort(); w = readFileShort();
if (w != len) if (w != len)
{ {
printf("Problem with size of g[]: file %d, calc %d\n", w, len); printf("Problem with size of g[]: file %d, calc %d\n", w, len);
exit(4); exit(4);
} }
if (fread(g, 1, len, f) != len) if (fread(g, 1, len, f) != len)
{ {
printf("Could not read T2\n"); printf("Could not read T2\n");
exit(5); exit(5);
} }
/* This is now the hash table */ /* This is now the hash table */
grab(2); grab(2);
if (memcmp("ht", buf, 2) != 0) if (memcmp("ht", buf, 2) != 0)
{ {
printf("Expected 'ht'\n"); printf("Expected 'ht'\n");
exit(3); exit(3);
} }
w = readFileShort(); w = readFileShort();
if (w != numKeys * (SymLen + PatLen + sizeof(word))) if (w != numKeys * (SymLen + PatLen + sizeof(uint16_t)))
{ {
printf("Problem with size of hash table: file %d, calc %d\n", w, len); printf("Problem with size of hash table: file %d, calc %d\n", w, len);
exit(6); exit(6);
} }
if (bDispAll) if (bDispAll)
{ {
fseek(f, 0, SEEK_CUR); /* Needed due to bug in MS fread()! */ filePos = ftell(f);
filePos = _lseek(fileno(f), 0, SEEK_CUR); for (i=0; i < numKeys; i++)
for (i=0; i < numKeys; i++) {
{ grab(SymLen + PatLen);
grab(SymLen + PatLen);
printf("%16s ", buf); printf("%16s ", buf);
for (j=0; j < PatLen; j++) for (j=0; j < PatLen; j++)
{ {
printf("%02X", buf[SymLen+j]); printf("%02X", buf[SymLen+j]);
if ((j%4) == 3) printf(" "); if ((j%4) == 3) printf(" ");
} }
printf("\n"); printf("\n");
} }
printf("\n\n\n"); printf("\n\n\n");
fseek(f, filePos, SEEK_SET); fseek(f, filePos, SEEK_SET);
} }
for (i=0; i < numKeys; i++) for (i=0; i < numKeys; i++)
{ {
grab(SymLen + PatLen); grab(SymLen + PatLen);
h = hash(&buf[SymLen]); h = g_pattern_hasher.hash(&buf[SymLen]);
if (h != i) if (h != i)
{ {
printf("Symbol %16s (index %3d) hashed to %d\n", printf("Symbol %16s (index %3d) hashed to %d\n", buf, i, h);
buf, i, h); }
} }
}
printf("Done!\n");
fclose(f);
printf("Done!\n");
fclose(f);
return 0;
} }
void void
cleanup(void) cleanup(void)
{ {
/* Free the storage for variable sized tables etc */ // TODO: g_pattern_hasher.hashCleanup();
if (T1base) free(T1base); /* Free the storage for variable sized tables etc */
if (T2base) free(T2base); if (T1base) free(T1base);
if (g) free(g); if (T2base) free(T2base);
if (g) free(g);
} }
void grab(int n) void grab(int n)
{ {
if (fread(buf, 1, n, f) != (size_t)n) if (fread(buf, 1, n, f) != (size_t)n)
{ {
printf("Could not read\n"); printf("Could not read\n");
exit(11); exit(11);
} }
} }
word uint16_t readFileShort(void)
readFileShort(void)
{ {
byte b1, b2; uint8_t b1, b2;
if (fread(&b1, 1, 1, f) != 1) if (fread(&b1, 1, 1, f) != 1)
{ {
printf("Could not read\n"); printf("Could not read\n");
exit(11); exit(11);
} }
if (fread(&b2, 1, 1, f) != 1) if (fread(&b2, 1, 1, f) != 1)
{ {
printf("Could not read\n"); printf("Could not read\n");
exit(11); exit(11);
} }
return (b2 << 8) + b1; return (b2 << 8) + b1;
} }
/* Following two functions not needed unless creating tables */
void getKey(int i, byte **keys)
{
}
/* Display key i */
void
dispKey(int i)
{
}