From 4b3279c3fffc836a9e55ebbf14af0aa831d6db3b Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Mon, 23 Oct 2017 19:04:50 +0700 Subject: [PATCH 1/5] Fix a small mistake in hex2long() --- source/compiler/sc6.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/compiler/sc6.c b/source/compiler/sc6.c index 9007477..207417b 100644 --- a/source/compiler/sc6.c +++ b/source/compiler/sc6.c @@ -75,7 +75,7 @@ static ucell hex2long(const char *s,char **n) s++; } /* if */ - assert((*s>='0' && *s<='9') || (*s>='a' && *s<='f') || (*s>='a' && *s<='f')); + assert((*s>='0' && *s<='9') || (*s>='a' && *s<='f') || (*s>='A' && *s<='F')); for ( ;; ) { if (*s>='0' && *s<='9') digit=*s-'0'; From b185b32ba1966163e8abc760fbd9a0eb1f1b68d6 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Mon, 23 Oct 2017 19:07:10 +0700 Subject: [PATCH 2/5] Use hex2long() instead of getparam() in do_dump() and do_dumpn() --- source/compiler/sc6.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/compiler/sc6.c b/source/compiler/sc6.c index 207417b..b2b792c 100644 --- a/source/compiler/sc6.c +++ b/source/compiler/sc6.c @@ -385,7 +385,7 @@ static cell OPHANDLER_CALL do_dump(FILE *fbin,char *params,cell opcode) int num=0; while (*params!='\0') { - p=getparam(params,¶ms); + p=hex2long(params,¶ms); if (fbin!=NULL) write_encoded(fbin,&p,1); num++; @@ -399,8 +399,8 @@ static cell OPHANDLER_CALL do_dumpn(FILE *fbin,char *params,cell opcode) { ucell value,num,i; - value=getparam(params,¶ms); - num=getparam(params,NULL); + value=hex2long(params,¶ms); + num=hex2long(params,NULL); if (fbin!=NULL) { for (i=0; i Date: Tue, 24 Oct 2017 05:17:25 +0700 Subject: [PATCH 3/5] Decode dumped value in do_dumpn() only once --- source/compiler/sc6.c | 94 +++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 30 deletions(-) diff --git a/source/compiler/sc6.c b/source/compiler/sc6.c index b2b792c..fcccdfd 100644 --- a/source/compiler/sc6.c +++ b/source/compiler/sc6.c @@ -228,7 +228,7 @@ static char *stripwhitespace(char *str) return str; } -static void write_encoded(FILE *fbin,ucell *c,int num) +static unsigned char *encode_cell(ucell c,int *len) { #if PAWN_CELL_SIZE == 16 #define ENC_MAX 3 /* a 16-bit cell is encoded in max. 3 bytes */ @@ -241,32 +241,43 @@ static void write_encoded(FILE *fbin,ucell *c,int num) #define ENC_MASK 0x01 /* after 9x7 bits, 1 bit remains to make 64 bits */ #endif + static unsigned char buffer[ENC_MAX]; + char *ptr; + int index; + + assert(len!=NULL); + index=ENC_MAX-1; + do { + buffer[index--]=(unsigned char)(c & 0x7f); /* store 7 bits */ + c>>=7; + } while (index>=0); + /* skip leading zeros */ + while (index0) { if (sc_compress) { - ucell p=(ucell)*c; - unsigned char t[ENC_MAX]; - unsigned char code; - int index; - for (index=0; index>=7; - } /* for */ - /* skip leading zeros */ - while (index>1 && t[index-1]==0 && (t[index-2] & 0x40)==0) - index--; - /* skip leading -1s */ - if (index==ENC_MAX && t[index-1]==ENC_MASK && (t[index-2] & 0x40)!=0) - index--; - while (index>1 && t[index-1]==0x7f && (t[index-2] & 0x40)!=0) - index--; - /* write high byte first, write continuation bits */ - assert(index>0); - while (index-->0) { - code=(unsigned char)((index==0) ? t[index] : (t[index]|0x80)); - writeerror |= !pc_writebin(fbin,&code,1); - bytes_out++; - } /* while */ + bytes=encode_cell(*c,&len); + assert(len>0); + writeerror |= !pc_writebin(fbin,bytes,len); + bytes_out+=len; bytes_in+=sizeof *c; assert(AMX_COMPACTMARGIN>2); if (bytes_out-bytes_in>=AMX_COMPACTMARGIN-2) @@ -279,6 +290,30 @@ static void write_encoded(FILE *fbin,ucell *c,int num) } /* while */ } +static void write_encoded_n(FILE *fbin,ucell c,int num) +{ + int len; + unsigned char *bytes; + + assert(fbin != NULL); + if (sc_compress) { + bytes=encode_cell(c,&len); + assert(len>0); + while (num-->0) + writeerror |= !pc_writebin(fbin,bytes,len); + bytes_in += num*sizeof c; + bytes_out += num*len; + if (bytes_out-bytes_in>=AMX_COMPACTMARGIN-2) + longjmp(compact_err,1); + } else { + c=aligncell(c); + while (num-->0) { + assert((pc_lengthbin(fbin) % sizeof(cell)) == 0); + writeerror |= !pc_writebin(fbin,&c,sizeof c); + } /* while */ + } /* if */ +} + #if defined __BORLANDC__ || defined __WATCOMC__ #pragma argsused #endif @@ -397,14 +432,13 @@ static cell OPHANDLER_CALL do_dump(FILE *fbin,char *params,cell opcode) static cell OPHANDLER_CALL do_dumpn(FILE *fbin,char *params,cell opcode) { - ucell value,num,i; + ucell value; + int num; value=hex2long(params,¶ms); - num=hex2long(params,NULL); - if (fbin!=NULL) { - for (i=0; i Date: Tue, 24 Oct 2017 16:13:34 +0700 Subject: [PATCH 4/5] Minor fix in encode_cell() --- source/compiler/sc6.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/compiler/sc6.c b/source/compiler/sc6.c index fcccdfd..55f3cd2 100644 --- a/source/compiler/sc6.c +++ b/source/compiler/sc6.c @@ -242,7 +242,7 @@ static unsigned char *encode_cell(ucell c,int *len) #endif static unsigned char buffer[ENC_MAX]; - char *ptr; + unsigned char *ptr; int index; assert(len!=NULL); From 6947f2e3c517f3401676f92892b0cb083f94c692 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Sun, 5 Nov 2017 21:29:50 +0700 Subject: [PATCH 5/5] Fix input/output size miscalculation in write_encoded_n() --- source/compiler/sc6.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/compiler/sc6.c b/source/compiler/sc6.c index 55f3cd2..a8261a8 100644 --- a/source/compiler/sc6.c +++ b/source/compiler/sc6.c @@ -299,12 +299,12 @@ static void write_encoded_n(FILE *fbin,ucell c,int num) if (sc_compress) { bytes=encode_cell(c,&len); assert(len>0); - while (num-->0) - writeerror |= !pc_writebin(fbin,bytes,len); bytes_in += num*sizeof c; bytes_out += num*len; if (bytes_out-bytes_in>=AMX_COMPACTMARGIN-2) longjmp(compact_err,1); + while (num-->0) + writeerror |= !pc_writebin(fbin,bytes,len); } else { c=aligncell(c); while (num-->0) {