Commit 3229d26e authored by Sven Robertz's avatar Sven Robertz
Browse files

minor polishing

parent 2432621c
...@@ -12,13 +12,35 @@ ...@@ -12,13 +12,35 @@
#define FALSE 0 #define FALSE 0
#define TRUE 1 #define TRUE 1
#define USER_ID_BASE 0x00000080
typedef enum{
TYPE_DECL = 0x00000001,
SAMPLE_DECL = 0x00000002,
ARRAY_DECL = 0x00000010,
STRUCT_DECL = 0x00000011,
TYPE_BOOLEAN = 0x00000020,
TYPE_BYTE = 0x00000021,
TYPE_SHORT = 0x00000022,
TYPE_INTEGER = 0x00000023,
TYPE_LONG = 0x00000024,
TYPE_FLOAT = 0x00000025,
TYPE_DOUBLE = 0x00000026,
TYPE_STRING = 0x00000027
} labcomm_type ;
void error(char *s) { void error(char *s) {
fprintf(stderr, "%s\n", s); fprintf(stderr, "%s", s);
fprintf(stderr, "\nexiting\n");
exit(1); exit(1);
} }
#define BUF_SIZE 1024 #define BUF_SIZE 1024
#define STACK_SIZE 16 #define STACK_SIZE 16
/* internal type: stack for the parser */
typedef struct { typedef struct {
unsigned char* c; unsigned char* c;
unsigned int size; unsigned int size;
...@@ -28,6 +50,15 @@ typedef struct { ...@@ -28,6 +50,15 @@ typedef struct {
unsigned int top; unsigned int top;
} buffer; } buffer;
/* aux method for reading a big endian uint32 from a char* (i.e. ntohl but for explicit char*) */
static int unpack32(unsigned char *c, unsigned int idx) {
unsigned int b0=(c[idx]) << 3 ;
unsigned int b1=(c[idx+1]) << 2 ;
unsigned int b2=(c[idx+2]) << 1 ;
unsigned int b3=c[idx+3];
return b0 | b1 | b2 | b3;
}
void dumpStack(buffer *b) { void dumpStack(buffer *b) {
#ifdef DEBUG #ifdef DEBUG
...@@ -82,28 +113,10 @@ void advancen(buffer *b, size_t n) { ...@@ -82,28 +113,10 @@ void advancen(buffer *b, size_t n) {
b->idx+=n; b->idx+=n;
} }
static int unpack32(unsigned char *c, unsigned int idx) {
unsigned int b0=(c[idx]) << 3 ;
unsigned int b1=(c[idx+1]) << 2 ;
unsigned int b2=(c[idx+2]) << 1 ;
unsigned int b3=c[idx+3];
return b0 | b1 | b2 | b3;
}
unsigned int peek32(buffer *b) { unsigned int peek32(buffer *b) {
#if 0
unsigned int b0=(b->c[b->idx]) << 3 ;
unsigned int b1=(b->c[b->idx+1]) << 2 ;
unsigned int b2=(b->c[b->idx+2]) << 1 ;
unsigned int b3=b->c[b->idx+3];
return b0 | b1 | b2 | b3;
#else
return unpack32(b->c, b->idx); return unpack32(b->c, b->idx);
#endif
} }
void advance32(buffer *b) { void advance32(buffer *b) {
b->idx+=4; b->idx+=4;
} }
...@@ -122,26 +135,13 @@ void getStr(buffer *b, char *dest, size_t size) { ...@@ -122,26 +135,13 @@ void getStr(buffer *b, char *dest, size_t size) {
b->idx += size; b->idx += size;
} }
#define TYPE_DECL 0x00000001
#define SAMPLE_DECL 0x00000002
#define USER_ID_BASE 0x00000080
#define ARRAY_DECL 0x00000010
#define STRUCT_DECL 0x00000011
#define TYPE_BOOLEAN 0x00000020
#define TYPE_BYTE 0x00000021
#define TYPE_SHORT 0x00000022
#define TYPE_INTEGER 0x00000023
#define TYPE_LONG 0x00000024
#define TYPE_FLOAT 0x00000025
#define TYPE_DOUBLE 0x00000026
#define TYPE_STRING 0x00000027
//XXX experimental //XXX experimental
unsigned int signatures_length[10]; #define MAX_SIGNATURES 10
unsigned char signatures_name[10][64]; //HERE BE DRAGONS: add range checks #define MAX_NAME_LEN 32
unsigned char signatures[10][128]; #define MAX_SIG_LEN 128
unsigned int signatures_length[MAX_SIGNATURES];
unsigned char signatures_name[MAX_SIGNATURES][MAX_NAME_LEN]; //HERE BE DRAGONS: add range checks
unsigned char signatures[MAX_SIGNATURES][MAX_SIG_LEN];
unsigned int get_signature_len(unsigned int uid){ unsigned int get_signature_len(unsigned int uid){
return signatures_length[uid-USER_ID_BASE]; return signatures_length[uid-USER_ID_BASE];
...@@ -156,7 +156,7 @@ unsigned char* get_signature(unsigned int uid){ ...@@ -156,7 +156,7 @@ unsigned char* get_signature(unsigned int uid){
void dump_signature(unsigned int uid){ void dump_signature(unsigned int uid){
int i; int i;
unsigned int len = get_signature_len(uid); unsigned int len = get_signature_len(uid);
printf("sig %x : %s (len=%d):\n", uid, get_signature_name(uid), len); printf("signature for uid %x : %s (len=%d):\n", uid, get_signature_name(uid), len);
unsigned char* sig = get_signature(uid); unsigned char* sig = get_signature(uid);
for(i=0; i<len; i++) { for(i=0; i<len; i++) {
printf("%2.2x ",sig[i]); printf("%2.2x ",sig[i]);
...@@ -269,10 +269,18 @@ int accept_sample_decl(buffer *d){ ...@@ -269,10 +269,18 @@ int accept_sample_decl(buffer *d){
unsigned int end = d->idx; unsigned int end = d->idx;
unsigned int len = end-start; unsigned int len = end-start;
printf("signature for uid %x (start=%x,end=%x, nlen=%d,len=%d)\n", uid, start,end, nlen, len); printf("signature for uid %x (start=%x,end=%x, nlen=%d,len=%d)\n", uid, start,end, nlen, len);
signatures_length[uid-USER_ID_BASE] = len; if(len <= MAX_SIG_LEN) {
memcpy(signatures_name[uid-USER_ID_BASE], &d->c[nstart+3], nlen+1); signatures_length[uid-USER_ID_BASE] = len;
signatures_name[uid-USER_ID_BASE][nlen+1]=0; memcpy(signatures_name[uid-USER_ID_BASE], &d->c[nstart+3], nlen+1);
memcpy(signatures[uid-USER_ID_BASE], &d->c[start], len); } else {
error("sig longer than max length (this ought to be dynamic...)");
}
if(nlen < MAX_NAME_LEN) { // reserve space for terminating NULL
signatures_name[uid-USER_ID_BASE][nlen+1]=0;
memcpy(signatures[uid-USER_ID_BASE], &d->c[start], len);
} else {
error("sig name longer than max length (this ought to be dynamic...");
}
return TRUE; return TRUE;
} else { } else {
return FALSE; return FALSE;
...@@ -438,7 +446,9 @@ int accept_sample_data(buffer *d){ ...@@ -438,7 +446,9 @@ int accept_sample_data(buffer *d){
accept_user_id(d); accept_user_id(d);
unsigned int uid = pop(d); unsigned int uid = pop(d);
printf("sample data... %x\n", uid); printf("sample data... %x\n", uid);
#ifdef DEBUG
dump_signature(uid); dump_signature(uid);
#endif
unsigned int siglen = get_signature_len(uid); unsigned int siglen = get_signature_len(uid);
unsigned char *sig = get_signature(uid); unsigned char *sig = get_signature(uid);
skip_packed_sample_data(d, sig, siglen); skip_packed_sample_data(d, sig, siglen);
...@@ -458,10 +468,14 @@ int skip_struct(buffer *d, unsigned char *sig, unsigned int len, unsigned int *p ...@@ -458,10 +468,14 @@ int skip_struct(buffer *d, unsigned char *sig, unsigned int len, unsigned int *p
//skip name //skip name
unsigned int namelen = unpack32(sig, *pos); unsigned int namelen = unpack32(sig, *pos);
*pos += (4+namelen); // 32bit len + actual string *pos += (4+namelen); // 32bit len + actual string
#ifdef DEBUG
printf("namelen==%d \n",namelen); printf("namelen==%d \n",namelen);
#endif
unsigned int type = unpack32(sig, *pos); unsigned int type = unpack32(sig, *pos);
*pos += 4; *pos += 4;
#ifdef DEBUG
printf("type == %x\n", type); printf("type == %x\n", type);
#endif
skipped += skip_type(type, d, sig, len, pos); skipped += skip_type(type, d, sig, len, pos);
} }
return skipped; return skipped;
...@@ -472,14 +486,14 @@ int skip_type(unsigned int type, buffer *d, ...@@ -472,14 +486,14 @@ int skip_type(unsigned int type, buffer *d,
unsigned char *sig, unsigned int len, unsigned int *pos) unsigned char *sig, unsigned int len, unsigned int *pos)
{ {
int skipped=0; int skipped=0;
printf("skip_type %x\n", type); printf("skip_type %x:", type);
switch(type) { switch(type) {
case TYPE_BOOLEAN : case TYPE_BOOLEAN :
printf("boolean %d\n", get(d)); printf("boolean [%d]\n", get(d));
skipped++; skipped++;
break; break;
case TYPE_BYTE : case TYPE_BYTE :
printf("byte %d\n", get(d)); printf("byte [%d]\n", get(d));
skipped++; skipped++;
break; break;
case TYPE_SHORT : case TYPE_SHORT :
...@@ -488,7 +502,7 @@ int skip_type(unsigned int type, buffer *d, ...@@ -488,7 +502,7 @@ int skip_type(unsigned int type, buffer *d,
skipped+=2; skipped+=2;
break; break;
case TYPE_INTEGER : case TYPE_INTEGER :
printf("integer %d\n", get32(d)); printf("integer [%d]\n", get32(d));
skipped +=4; skipped +=4;
break; break;
case TYPE_FLOAT : case TYPE_FLOAT :
...@@ -506,10 +520,10 @@ int skip_type(unsigned int type, buffer *d, ...@@ -506,10 +520,10 @@ int skip_type(unsigned int type, buffer *d,
{unsigned int len = get32(d); {unsigned int len = get32(d);
//advancen(d,len); //advancen(d,len);
int i; int i;
printf("string "); printf("string [");
for(i=0; i<len; i++) for(i=0; i<len; i++)
printf("%c", get(d)); printf("%c", get(d));
printf("\n "); printf("]\n");
skipped+=len+4; skipped+=len+4;
break;} break;}
case ARRAY_DECL : case ARRAY_DECL :
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment