Merge pull request #221 from xPaw/burn-scpack

Remove scpack
This commit is contained in:
Vincent Herbet 2015-03-10 19:52:29 +01:00
commit a25090de4b
7 changed files with 21 additions and 1416 deletions

View File

@ -1,42 +1,6 @@
# vim: set sts=2 ts=8 sw=2 tw=99 et ft=python:
import os.path
# Build the packing binary garbage.
scpack = AMXX.Program(builder, 'scpack')
if builder.target_platform == 'windows':
scpack.compiler.linkflags.remove('/SUBSYSTEM:WINDOWS')
scpack.compiler.linkflags.append('/SUBSYSTEM:CONSOLE')
scpack.sources = ['scpack.c']
scpack = builder.Add(scpack)
# Generate pack files.
packed_files = ['sc5', 'sc7']
packed_includes = []
for packed_file in packed_files:
# The absolute path to sc5-in.scp etc.
in_path = os.path.join(builder.currentSourcePath, '{0}-in.scp'.format(packed_file))
# The output file relative to the output folder, i.e. sourcepawn/compiler/sc5.scp.
out_path = os.path.join(builder.buildFolder, '{0}.scp'.format(packed_file))
# The absolute path to the build folder, i.e. /Users/.../sourcepawn/compiler.
build_folder = os.path.join(builder.buildPath, builder.buildFolder)
# scpack runs in ./sourcepawn/compiler/scpack/ so we build relative paths
# from there.
scpack_argv = [
os.path.join(builder.buildPath, scpack.binary.path),
os.path.relpath(in_path, build_folder),
os.path.relpath(os.path.join(builder.buildPath, out_path), build_folder),
]
_, (entry,) = builder.AddCommand(
inputs = [scpack.binary, in_path],
argv = scpack_argv,
outputs = ['{0}.scp'.format(packed_file)],
)
packed_includes += [entry]
binary = AMXX.Library(builder, 'amxxpc32')
binary.compiler.includes += [
@ -44,8 +8,6 @@ binary.compiler.includes += [
os.path.join(builder.buildPath, builder.buildFolder),
]
binary.compiler.sourcedeps += packed_includes
if builder.target_platform in ['mac', 'linux']:
binary.compiler.defines += ['ENABLE_BINRELOC']
binary.compiler.postlink += ['-lm', '-lpthread']
@ -70,7 +32,6 @@ binary.sources = [
'scstate.c',
'sclist.c',
'sci18n.c',
'scexpand.c',
'pawncc.c',
'libpawnc.c',
'prefix.c',

View File

@ -17,28 +17,9 @@
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
* Version: $Id: sc5.scp 2167 2005-11-19 09:16:27Z dvander $
*/
SC_FUNC int strexpand(char *dest, unsigned char *source, int maxlen, unsigned char pairtable[128][2]);
#define SCPACK_TABLE errstr_table
/*-*SCPACK start of pair table, do not change or remove this line */
unsigned char errstr_table[][2] = {
{101,32}, {111,110}, {116,32}, {105,110}, {97,114}, {116,105}, {100,32}, {115,32}, {101,110}, {101,114}, {97,108}, {133,129}, {37,115}, {34,140}, {141,34}, {117,110},
{115,105}, {110,111}, {114,101}, {121,32}, {97,110}, {111,114}, {97,116}, {109,136}, {100,101}, {139,32}, {115,116}, {101,134}, {109,98}, {41,10}, {118,138}, {114,97},
{143,99}, {145,130}, {32,142}, {102,160}, {117,115}, {98,108}, {111,108}, {97,32}, {115,144}, {163,153}, {105,134}, {132,159}, {149,32}, {158,170}, {115,121}, {117,151},
{174,156}, {103,32}, {176,166}, {103,175}, {131,173}, {132,179}, {116,104}, {101,120}, {137,32}, {101,100}, {111,102}, {99,104}, {105,135}, {171,147}, {152,102}, {116,111},
{186,32}, {97,165}, {99,129}, {181,130}, {101,10}, {115,10}, {112,146}, {164,130}, {98,128}, {162,10}, {44,32}, {109,199}, {109,97}, {193,128}, {34,32}, {129,32},
{116,97}, {100,105}, {144,122}, {190,131}, {118,132}, {101,135}, {154,150}, {102,105}, {212,105}, {101,108}, {117,108}, {99,111}, {109,150}, {58,201}, {216,205}, {132,97},
{119,105}, {111,112}, {97,115}, {225,137}, {194,154}, {178,162}, {110,32}, {138,32}, {148,32}, {198,168}, {131,177}, {203,200}, {104,97}, {183,233}, {108,111}, {109,112},
{148,130}, {108,128}, {229,157}, {142,157}, {40,242}, {110,117}, {118,128}, {116,137}, {116,10}, {102,149}, {227,150}, {101,151}, {245,156}, {111,32}, {145,32}
};
/*-*SCPACK end of pair table, do not change or remove this line */
static char *errmsg[] = {
#ifdef SCPACK
/*001*/ "expected token: \"%s\", but found \"%s\"\n",
/*002*/ "only a single statement (or expression) can follow each \"case\"\n",
/*003*/ "declaration of a local variable must appear in a compound block\n",
@ -127,100 +108,9 @@ static char *errmsg[] = {
/*086*/ "unknown automaton \"%s\"\n",
/*087*/ "unknown state \"%s\" for automaton \"%s\"\n",
/*088*/ "number of arguments does not match definition\n"
#else
"\267pect\233\277k\210:\242\312bu\202fo\217\206\216\012",
"\201l\223\247s\203g\361\326\373\202(\254\355\201) c\350f\246\356w ea\273 \042c\342e\042\012",
"\230cl\337\231\300\247\356c\347\336\313appe\204 \203 \247\333\357o\217\206\245ock\012",
"\251\216 \274\241i\357l\373t\271\012",
"\251\314\223\241\354\366\265t\305",
"\353a\250gn\233\277 \350\253y\012",
"\372\254c\224\241\310\222\323\271\012",
"\353\247\344\360\355\201; \342sum\233z\211o\012",
"\264\275\322\200(nega\205\366\254z\211o\235",
"\264\251\254\230cl\337\213\012",
"\264out\220d\200\243\213\305",
"\264\251c\212l\312\241\247\255add\222s\305",
"\376\210tr\223po\203\202(\376pu\245ic \243\213s\235",
"\264\326\373t; \241\203 s\340t\273\012",
"\042\276a\332t\316c\342\200\353\266\200l\342\202c\342\200\203 s\340t\273 \326\373\370",
"m\332\205p\361\276a\332t\207\203 \042s\340t\273\042\012",
"\217\323\233\262\311",
"\203i\205\212iza\231d\226\247\267ce\271\207\230cl\204\233\322\304",
"\241\247lab\331\335",
"\264\262 nam\200\216\012",
"\262 \212\222ad\223\323\271\335",
"\353l\236u\200(n\201-\344\224t\235",
"\275a\250gn\227\202\353\220\357\361a\250gn\227\370",
"\042b\222ak\316\254\042\302t\203ue\316\274ou\202\300\302t\267\370",
"\251head\352\321ff\211\207from pro\277typ\304",
"\376\334\273\352\042#if...\042\012",
"\264\273\337ct\270\344\224\370",
"\264subscrip\202(\241\350\275\254\277\375m\224\223subscripts)\335",
"\264\355\201\312\342sum\233z\211o\012",
"\333\357o\217\206\326\373\202\241c\356s\233a\202\266\200\210\206\300\327l\304",
"\217k\221w\346\321\222c\205v\304",
"\275\203\230x ou\202\300bo\217d\207(\336\363",
"\275\353\203\230x\233(\336\363",
"\303do\325\241\354\366\247\276a\332\202\236u\200(\303%d\235",
"\303typ\200mis\334\273 (\303%d\235",
"e\357t\223\326\373\370",
"\264\232r\352(po\250\245\223n\201-\367m\203\226\233\232r\203g\235",
"\267t\237 \273\337c\367\207\317l\203\304",
"\344\360\262 \354\207\376\322\304",
"duplic\226\200\042c\342e\316lab\331 (\236u\200%d\235",
"\264\331lip\220s\312\275\322\200\274\241k\221wn\012",
"\264\333\234\203a\231\300cl\342\207speci\327\211\305",
"\273\337ct\270\344\360\267ce\271\207r\224g\200f\254pack\233\232r\203g\012",
"po\220\213\347p\337me\367\207\313\306c\271\200\212l nam\233p\337me\367\305",
"\277\375m\224\223\251\265t\305",
"\217k\221w\346\275\322\200(\336\363",
"\275\322\325d\375\241\334\273\312\254\230\232\203a\231\275\274\277\375sm\212l\012",
"\275\321\227\220\201\207d\375\241\334\273\012",
"\264l\203\200\302t\203ua\213\012",
"\264r\224g\304",
"\264subscript\312\244\200\042[ ]\316\372\225\207\317\314j\254\321\227\220\201\305",
"m\332\205-\321\227\220\201\347\253y\207\353f\332l\223\203i\205\212iz\271\012",
"\267ce\271\352\314ximum \374\270\300\321\227\220\201\305",
"\217\334\273\233c\356s\352b\237c\304",
"\232\204\202\300\251bod\223\340\266ou\202\251head\211\012",
"\253ys\312\356c\347\330\301\325\224\206\251\265t\207c\224\241\310pu\245ic (\336\363",
"\217f\203ish\233\355\317be\371\200\333\357il\270\321\222c\205v\304",
"duplic\226\200\265t; sam\200\303\274p\342s\233t\340c\304",
"\251\303\314\223\241\354\366\247\276a\332\202\236u\200(\336\363",
"m\332\205p\361\042#\331se\316\321\222c\205v\325betwe\210 \042#if ... #\210\321f\042\012",
"\042#\331seif\316\321\222c\205\366f\246\356w\207\350\042#\331se\316\321\222c\205v\304",
"\374\270\300\343\224d\207do\325\241\327\202\266\200\372\225\012",
"\251\222s\332\202\320\261\300\372\225\242 \353\216\012",
"c\224\241\273\224g\200\306\323\233\372\225\305",
"\251\303\314\223\201l\223\354\366\247s\203g\361\320\261(\303%d\235",
"\251\303\314\223\241\310\247\222f\211\210c\200\303\254\350\275(\303\363",
"\336c\224\241\310bo\266 \247\222f\211\210c\200\224\206\350\275(\336\363",
"\264\237\213\347\374\270\306ci\220\317\203 #p\237g\314\012",
"\237\213\347\374\270\371\314\202\212\222ad\223\323\271\012",
"\237\213\347\374\270supp\225\202wa\207\241\210\301\271\012",
"\244\211-\323\233\372\254\353\230cl\204\233be\371\200\244\200(\251\363",
"\042\322e\272\316\372\254\274\264\317\042\243\213\316\262\305",
"\251\303\353\350\275(\303\363",
"#\323\200p\226\367\346\313\232\204\202\340\266 \350\212p\354be\205c \273\337c\367\012",
"\203pu\202l\203\200\277\375l\201\261(aft\270subs\205tu\213s\235",
"\256n\320x \211r\254\203 \266\200\355\201\312\254\264\251c\212l\012",
"m\212\371m\233UTF-8 \210\333d\203g\312\254c\225rupt\233\327le: \214\012",
"\251\244\325bo\266 \042\222turn\316\224\206\042\222tur\346<\236ue>\042\012",
"\203\302\220\232\210\202\222tur\346typ\325(\275& n\201-\253y\235",
"\217k\221w\346\262\312\254\241\247\344\360\262 \364",
"c\224\241\320k\200\247\320\261a\207\247\276a\332\202\236u\200f\254\350\203\230x\233\275p\337met\270\364",
"\244\211-\323\233\372\225\207\224\206na\205\366\243\213\207\314\223\241\354\366\326e\305",
"\247\251\314\223\201l\223b\331\201\261\277 \247s\203g\361au\277\334\317\364",
"\326\200\302flict: \201\200\300\266\200\326\325\274\212\222ad\223a\250gn\233\277 a\221\266\270i\357l\373\320\231\364",
"\376\326\325\204\200\323\233f\254\251\216\012",
"\217k\221w\346au\277\334\201\311",
"\217k\221w\346\326\200\216 f\254au\277\334\201\311",
"\374\270\300\265t\207do\325\241\334\273 \323i\213\012"
#endif
};
static char *fatalmsg[] = {
#ifdef SCPACK
/*100*/ "cannot read from file: \"%s\"\n",
/*101*/ "cannot write to file: \"%s\"\n",
/*102*/ "table overflow: \"%s\"\n",
@ -239,24 +129,9 @@ static char *fatalmsg[] = {
/*109*/ "invalid path: \"%s\"\n",
/*110*/ "assertion failed: %s\n",
/*111*/ "user error: %s\n",
#else
"c\224\241\222a\206from \327le\335",
"c\224\241writ\200\277 \327le\335",
"t\315ov\211f\356w\335",
"\203suf\327ci\210\202mem\225y\012",
"\264\342se\234l\270\203\232ruc\231\216\012",
"\365m\211ic ov\211f\356w\312\267ce\271\352capacity\012",
"\333\357il\233scrip\202\267ce\271\207\266\200\314ximum mem\225\223\322\200(%l\206bytes\235",
"\277\375m\224\223\211r\254messag\325\317\201\200l\203\304",
"\333\230pag\200\314pp\352\327\361\241fo\217d\012",
"\264p\226h\335",
"\342s\211\231fail\271: \214\012",
"\244\270\211r\225: \214\012"
#endif
};
static char *warnmsg[] = {
#ifdef SCPACK
/*200*/ "symbol \"%s\" is truncated to %d characters\n",
/*201*/ "redefinition of constant/macro (symbol \"%s\")\n",
/*202*/ "number of arguments does not match definition\n",
@ -292,39 +167,4 @@ static char *warnmsg[] = {
/*232*/ "output file is written, but with compact encoding disabled\n"
/*233*/ "symbol \"%s\" is marked as deprecated: %s\n",
/*234*/ "recursive function \"%s\"\n",
#else
"\345 \274tr\240\226\233\277 %\206\273\337c\367\305",
"\222\323i\231\300\344\224t/\314cr\375\364",
"\374\270\300\265t\207do\325\241\334\273 \323i\213\012",
"\262 \274nev\270\244\271\335",
"\262 \274a\250gn\233\247\236u\200\266a\202\274nev\270\244\271\335",
"\222d\217d\360\333\230: \344\360\355\317\274z\211o\012",
"\222d\217d\360te\232: \344\360\355\317\274n\201-z\211o\012",
"\217k\221w\346#p\237g\314\012",
"\251\340\266 \320\261\222s\332\202\244\233be\371\200\323i\213\312\371c\352\222p\204s\304",
"\251\216 sho\332\206\222tur\346\247\236u\304",
"po\250\245\200\244\200\300\262 be\371\200\203i\205\212iza\213\335",
"po\250\245\223\217\203t\210\230\206a\250gn\227\370",
"po\250\245\223\217\203t\210\230\206bit\340s\200\343a\213\012",
"\320\261mis\334\273\012",
"po\250\245\223\247\042\344\316\275\303wa\207\203t\210\230d\335",
"\355\317\354\207\376effec\370",
"ne\232\233\333m\227\370",
"\356os\200\203d\210\320\213\012",
"\246\206\232y\361pro\277typ\325\244\233\340\266 \341\213\347semic\246umn\305",
"\356c\347\336\216 s\354dow\207\247\336a\202\247\306c\271\352lev\331\012",
"\355\317\340\266 \320\261ov\211rid\200\313appe\204 betwe\210 p\204\210\266ese\305",
"lab\331 nam\200\216 s\354dow\207\320\261nam\304",
"\374\270\300\321git\207\267ce\271\207\237\213\347\374\270\306ci\220\201\012",
"\222d\217d\360\042\322e\272\042: \303\322\200\274\212way\2071 \364",
"\203\230\367m\203\226\200\275\322\200\203 \042\322e\272\316\355\317\364",
"\217\222a\273\315\333\230\012",
"\247\336\274a\250gn\233\277 its\331f \364",
"m\225\200\203i\205\212l\211\207\266\350\210um \327\331d\305",
"l\210g\266 \300\203i\205\212l\270\267ce\271\207\322\200\300\266\200\210um \327\331d\012",
"\203\230x \320\261mis\334\273 \364",
"\376i\357l\373\320\231f\254\326\200\216 / \251\216\312\376f\212l-back\012",
"\326\200speci\327ca\231\317\371w\204\206\230cl\337\231\274ig\221\222d\012",
"outpu\202\327\361\274writt\210\312bu\202\340\266 \333\357ac\202\210\333d\352\321s\301\271\012"
#endif
};

View File

@ -41,7 +41,7 @@
#pragma warning(disable:4125) /* decimal digit terminates octal escape sequence */
#endif
#include <sc5.scp>
#include "sc5-in.scp"
#if defined _MSC_VER
#pragma warning(pop)
@ -115,8 +115,6 @@ static short lastfile;
}
} /* if */
strexpand(string,(unsigned char *)msg,sizeof string,SCPACK_TABLE);
if (errline>0)
errstart=errline; /* forced error position, set single line destination */
else

File diff suppressed because it is too large Load Diff

View File

@ -51,7 +51,7 @@
#pragma warning(disable:4125) /* decimal digit terminates octal escape sequence */
#endif
#include <sc7.scp>
#include "sc7-in.scp"
#if defined _MSC_VER
#pragma warning(pop)
@ -334,65 +334,15 @@ SC_FUNC void stgset(int onoff)
* are embedded in the .EXE file in compressed format, here we expand
* them (and allocate memory for the sequences).
*/
static SEQUENCE *sequences;
static SEQUENCE *sequences = sequences_cmp;
SC_FUNC int phopt_init(void)
{
int number, i, len;
char str[160];
/* count number of sequences */
for (number=0; sequences_cmp[number].find!=NULL; number++)
/* nothing */;
number++; /* include an item for the NULL terminator */
if ((sequences=(SEQUENCE*)malloc(number * sizeof(SEQUENCE)))==NULL)
return FALSE;
/* pre-initialize all to NULL (in case of failure) */
for (i=0; i<number; i++) {
sequences[i].find=NULL;
sequences[i].replace=NULL;
sequences[i].savesize=0;
} /* for */
/* expand all strings */
for (i=0; i<number-1; i++) {
len = strexpand(str,(unsigned char*)sequences_cmp[i].find,sizeof str,SCPACK_TABLE);
assert(len<=sizeof str);
assert(len==(int)strlen(str)+1);
sequences[i].find=(char*)malloc(len);
if (sequences[i].find!=NULL)
strcpy(sequences[i].find,str);
len = strexpand(str,(unsigned char*)sequences_cmp[i].replace,sizeof str,SCPACK_TABLE);
assert(len<=sizeof str);
assert(len==(int)strlen(str)+1);
sequences[i].replace=(char*)malloc(len);
if (sequences[i].replace!=NULL)
strcpy(sequences[i].replace,str);
sequences[i].savesize=sequences_cmp[i].savesize;
if (sequences[i].find==NULL || sequences[i].replace==NULL)
return phopt_cleanup();
} /* for */
return TRUE;
}
SC_FUNC int phopt_cleanup(void)
{
int i;
if (sequences!=NULL) {
i=0;
while (sequences[i].find!=NULL || sequences[i].replace!=NULL) {
if (sequences[i].find!=NULL)
free(sequences[i].find);
if (sequences[i].replace!=NULL)
free(sequences[i].replace);
i++;
} /* while */
free(sequences);
sequences=NULL;
} /* if */
return FALSE;
}
@ -404,13 +354,13 @@ SC_FUNC int phopt_cleanup(void)
#define MAX_ALIAS (PAWN_CELL_SIZE/4) * MAX_OPT_CAT
#endif
static int matchsequence(char *start,char *end,char *pattern,
static int matchsequence(const char *start,const char *end,const char *pattern,
char symbols[MAX_OPT_VARS][MAX_ALIAS+1],
int *match_length)
{
int var,i;
char str[MAX_ALIAS+1];
char *start_org=start;
const char *start_org=start;
cell value;
char *ptr;
@ -443,7 +393,7 @@ static int matchsequence(char *start,char *end,char *pattern,
} /* if */
break;
case '-':
value=-strtol(pattern+1,&pattern,16);
value=-strtol(pattern+1,(char **)&pattern,16);
ptr=itoh((ucell)value);
while (*ptr!='\0') {
if (tolower(*start) != tolower(*ptr))
@ -482,9 +432,9 @@ static int matchsequence(char *start,char *end,char *pattern,
return TRUE;
}
static char *replacesequence(char *pattern,char symbols[MAX_OPT_VARS][MAX_ALIAS+1],int *repl_length)
static char *replacesequence(const char *pattern,char symbols[MAX_OPT_VARS][MAX_ALIAS+1],int *repl_length)
{
char *lptr;
const char *lptr;
int var;
char *buffer;
@ -520,10 +470,10 @@ static char *replacesequence(char *pattern,char symbols[MAX_OPT_VARS][MAX_ALIAS+
return (char*)error(103);
/* replace the pattern into this temporary buffer */
lptr=buffer;
*lptr++='\t'; /* the "replace" patterns do not have tabs */
char *ptr=buffer;
*ptr++='\t'; /* the "replace" patterns do not have tabs */
while (*pattern) {
assert((int)(lptr-buffer)<*repl_length);
assert((int)(ptr-buffer)<*repl_length);
switch (*pattern) {
case '%':
/* write out the symbol */
@ -532,23 +482,23 @@ static char *replacesequence(char *pattern,char symbols[MAX_OPT_VARS][MAX_ALIAS+
var=atoi(pattern) - 1;
assert(var>=0 && var<MAX_OPT_VARS);
assert(symbols[var][0]!='\0'); /* variable should be defined */
strcpy(lptr,symbols[var]);
lptr+=strlen(symbols[var]);
strcpy(ptr,symbols[var]);
ptr+=strlen(symbols[var]);
break;
case '!':
/* finish the line, optionally start the next line with an indent */
*lptr++='\n';
*lptr++='\0';
*ptr++='\n';
*ptr++='\0';
if (*(pattern+1)!='\0')
*lptr++='\t';
*ptr++='\t';
break;
default:
*lptr++=*pattern;
*ptr++=*pattern;
} /* switch */
pattern++;
} /* while */
assert((int)(lptr-buffer)==*repl_length);
assert((int)(ptr-buffer)==*repl_length);
return buffer;
}

View File

@ -1,68 +0,0 @@
/* expand.c -- Byte Pair Encoding decompression */
/* Copyright 1996 Philip Gage */
/* Byte Pair Compression appeared in the September 1997
* issue of C/C++ Users Journal. The original source code
* may still be found at the web site of the magazine
* (www.cuj.com).
*
* The decompressor has been modified by me (Thiadmer
* Riemersma) to accept a string as input, instead of a
* complete file.
*/
#include <assert.h>
#include <stdio.h>
#include "sc.h"
#define STACKSIZE 16
SC_FUNC int strexpand(char *dest, unsigned char *source, int maxlen, unsigned char pairtable[128][2])
{
unsigned char stack[STACKSIZE];
short c, top = 0;
int len;
assert(maxlen > 0);
len = 1; /* already 1 byte for '\0' */
for (;;) {
/* Pop byte from stack or read byte from the input string */
if (top)
c = stack[--top];
else if ((c = *(unsigned char *)source++) == '\0')
break;
/* Push pair on stack or output byte to the output string */
if (c > 127) {
assert(top+2 <= STACKSIZE);
stack[top++] = pairtable[c-128][1];
stack[top++] = pairtable[c-128][0];
}
else {
len++;
if (maxlen > 1) { /* reserve one byte for the '\0' */
*dest++ = (char)c;
maxlen--;
}
}
}
*dest = '\0';
return len; /* return number of bytes decoded */
}
#if 0 /*for testing*/
#include "sc5.scp"
int main (int argc, char **argv)
{
int i;
char str[128];
for (i=0; i<58; i++) {
strexpand(str, errmsg[i], sizeof str, SCPACK_TABLE);
printf("%s", str);
} /* for */
return 0;
}
#endif

View File

@ -1,450 +0,0 @@
/* compress.c -- Byte Pair Encoding compression */
/* Copyright 1996 Philip Gage */
/* This program appeared in the September 1997 issue of
* C/C++ Users Journal. The original source code may still
* be found at the web site of the magazine (www.cuj.com).
*
* It has been modified by me (Thiadmer Riemersma) to
* compress only a section of the input file and to store
* the compressed output along with the input as "C" strings.
*
* Compiling instructions:
* Borland C++ 16-bit (large memory model is required):
* bcc -ml scpack.c
*
* Watcom C/C++ 32-bit:
* wcl386 scpack.c
*
* GNU C (Linux), 32-bit:
* gcc scpack.c -o scpack
*/
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if UINT_MAX > 0xFFFFU
#define MAXSIZE 1024*1024L
#else
#define MAXSIZE UINT_MAX /* Input file buffer size */
#endif
#define HASHSIZE 8192 /* Hash table size, power of 2 */
#define THRESHOLD 3 /* Increase for speed, min 3 */
#define START_TOKEN "#ifdef SCPACK" /* start reading the buffer here */
#define NAME_TOKEN "#define SCPACK_TABLE"
#define SEP_TOKEN "#define SCPACK_SEPARATOR"
#define TERM_TOKEN "#define SCPACK_TERMINATOR"
#define TEMPFILE "~SCPACK.TMP"
static char tablename[32+1] = "scpack_table";
static char separator[16]=",";
static char terminator[16]="";
int compress(unsigned char *buffer, unsigned buffersize, unsigned char pairtable[128][2])
{
unsigned char *left, *right, *count;
unsigned char a, b, bestcount;
unsigned i, j, index, bestindex, code=128;
/* Dynamically allocate buffers and check for errors */
left = (unsigned char *)malloc(HASHSIZE);
right = (unsigned char *)malloc(HASHSIZE);
count = (unsigned char *)malloc(HASHSIZE);
if (left==NULL || right==NULL || count==NULL) {
printf("Error allocating memory\n");
exit(1);
}
/* Check for errors */
for (i=0; i<buffersize; i++)
if (buffer[i] > 127) {
printf("This program works only on text files (7-bit ASCII)\n");
exit(1);
}
memset(pairtable, 0, 128*2*sizeof(char));
do { /* Replace frequent pairs with bytes 128..255 */
/* Enter counts of all byte pairs into hash table */
memset(count,0,HASHSIZE);
for (i=0; i<buffersize-1; i++) {
a = buffer[i];
b = buffer[i+1];
/* ignore any pair with a '\0' */
if (a == 0 || b == 0)
continue;
index = (a ^ (b << 6)) & (HASHSIZE-1);
while ((left[index] != a || right[index] != b) &&
count[index] != 0)
index = (index + 1) & (HASHSIZE-1);
left[index] = a;
right[index] = b;
if (count[index] < 255)
count[index] += (unsigned char)1;
}
/* Search hash table for most frequent pair */
bestcount = THRESHOLD - 1;
for (i=0; i<HASHSIZE; i++) {
if (count[i] > bestcount) {
bestcount = count[i];
bestindex = i;
}
}
/* Compress if enough occurrences of pair */
if (bestcount >= THRESHOLD) {
/* Add pair to table using code as index */
a = pairtable[code-128][0] = left[bestindex];
b = pairtable[code-128][1] = right[bestindex];
/* Replace all pair occurrences with unused byte */
for (i=0, j=0; i<buffersize; i++, j++)
if (a == buffer[i] && b == buffer[i+1]) {
buffer[j] = (unsigned char)code;
++i;
}
else
buffer[j] = buffer[i];
buffersize = j;
}
else
break;
} while (++code < 255);
/* done */
free(left); free(right); free(count);
return buffersize; /* return adjusted buffersize */
}
static int strmatch(char *str, char *token, int *indent)
{
int i = 0;
/* skip whitespace */
while (*str==' ' || *str=='\t') {
str++;
i++;
} /* while */
if (strncmp(str,token,strlen(token))!=0)
return 0;
if (indent != NULL)
*indent = i;
return 1;
}
static void check_if(char *str,int linenr)
{
if (strmatch(str,"#if",NULL)) {
printf("Error: \"#if...\" preprocessor statement should not be in SCPACK section "
"(line %d)\n", linenr);
exit(1);
} /* if */
}
static int check_tablename(char *str)
{
int i;
if (strmatch(str,NAME_TOKEN,NULL)) {
str += strlen(NAME_TOKEN);
while (*str==' ' || *str=='\t')
str++;
for (i=0; i<(sizeof tablename - 1) && *str!='\0' && strchr(" \t\n",*str)==NULL; i++, str++)
tablename[i] = *str;
tablename[i] = '\0';
return 1;
} /* if */
return 0;
}
static int check_separator(char *str)
{
int i;
if (strmatch(str,SEP_TOKEN,NULL)) {
str += strlen(SEP_TOKEN);
while (*str==' ' || *str=='\t')
str++;
for (i=0; i<(sizeof separator - 1) && *str!='\0' && strchr(" \t\n",*str)==NULL; i++, str++)
separator[i] = *str;
separator[i] = '\0';
return 1;
} /* if */
if (strmatch(str,TERM_TOKEN,NULL)) {
str += strlen(TERM_TOKEN);
while (*str==' ' || *str=='\t')
str++;
for (i=0; i<(sizeof terminator - 1) && *str!='\0' && strchr(" \t\n",*str)==NULL; i++, str++)
terminator[i] = *str;
terminator[i] = '\0';
return 1;
} /* if */
return 0;
}
/* readbuffer
* Reads in the input file and stores all strings in the
* section between "#ifdef SCPACK" and "#else" in a buffer.
* Only text that is between double quotes is added to the
* buffer; the \" escape code is handled. Multiple strings
* on one line are handled.
*/
unsigned readbuffer(FILE *input, unsigned char *buffer)
{
char str[256];
unsigned buffersize;
int i,linenr;
linenr=0;
buffersize=0;
rewind(input);
while (!feof(input)) {
while (fgets(str,sizeof str,input)!=NULL) {
linenr++;
check_tablename(str);
check_separator(str);
if (strmatch(str,START_TOKEN,NULL))
break;
} /* while */
if (!strmatch(str,START_TOKEN,NULL))
return buffersize; /* no (more) section found, quit */
while (fgets(str,sizeof str,input)!=NULL) {
linenr++;
check_if(str,linenr);
if (check_tablename(str))
printf("Error: table name definition should not be in SCPACK section (line %d)\n", linenr);
check_separator(str);
if (strmatch(str,"#else",NULL))
break; /* done */
/* add to the buffer only what is between double quotes */
i=0;
do {
while (str[i]!='\0' && str[i]!='"')
i++;
if (str[i]=='"') {
/* we are in a string */
i++;
while (str[i]!='\0' && str[i]!='"') {
/* handle escape sequences */
if (str[i]=='\\') {
i++;
switch (str[i]) {
case 'a': /* alarm */
buffer[buffersize++]='\a';
i++;
break;
case 'b': /* backspace */
buffer[buffersize++]='\b';
i++;
break;
case 'f': /* form feed */
buffer[buffersize++]='\f';
i++;
break;
case 'n': /* newline */
buffer[buffersize++]='\n';
i++;
break;
case 'r': /* carriage return */
buffer[buffersize++]='\n';
i++;
break;
case 't': /* tab */
buffer[buffersize++]='\t';
i++;
break;
case '\'':
buffer[buffersize++]='\'';
i++;
break;
case '"':
buffer[buffersize++]='"';
i++;
break;
default:
// ??? octal character code escapes and hexadecimal escapes
// not supported
printf("Unknown escape sequence '\\%c' on line %d\n",
str[i], linenr);
} /* switch */
} else {
buffer[buffersize++]=str[i++];
} /* if */
} /* while */
if (str[i]=='"') {
buffer[buffersize++]='\0'; /* terminate each string */
i++;
} else {
printf("Error: unterminated string on line %d\n",linenr);
} /* if */
} /* if */
} while (str[i]!='\0');
} /* while - in SCPACK section */
/* put in another '\0' to terminate the section */
buffer[buffersize++]='\0';
} /* while - !feof(input) */
return buffersize;
}
static void write_pairtable(FILE *output, unsigned char pairtable[128][2], char *tablename)
{
int i;
/* dump the pair table */
fprintf(output, "/*-*SCPACK start of pair table, do not change or remove this line */\n");
fprintf(output, "unsigned char %s[][2] = {", tablename);
for (i=0; i<128 && pairtable[i][0]!=0 && pairtable[i][1]!=0; i++) {
if ((i % 16)==0)
fprintf(output, "\n ");
else
fprintf(output, " ");
fprintf(output, "{%d,%d}", pairtable[i][0], pairtable[i][1]);
/* check if something follows this pair */
if (i+1<128 && pairtable[i+1][0]!=0 && pairtable[i+1][1]!=0)
fprintf(output, ",");
} /* for */
fprintf(output, "\n};\n");
fprintf(output, "/*-*SCPACK end of pair table, do not change or remove this line */\n");
}
void writefile(FILE *input, FILE *output, unsigned char *buffer, unsigned buffersize, unsigned char pairtable[128][2])
{
char str[256];
int insection, indent, needseparator;
unsigned char *bufptr;
bufptr = buffer;
insection = 0;
rewind(input);
while (!feof(input)) {
while (fgets(str,sizeof str,input)!=NULL) {
fprintf(output,"%s",str);
if (check_tablename(str)) {
write_pairtable(output, pairtable, tablename);
/* strip an existing pair table from the file */
if (fgets(str,sizeof str,input)!=NULL) {
if (strmatch(str,"/*-*SCPACK",NULL)) {
while (fgets(str,sizeof str,input)!=NULL)
if (strmatch(str,"/*-*SCPACK",NULL))
break;
} else {
fprintf(output,"%s",str);
} /* if */
} /* if */
} /* if */
if (strmatch(str,START_TOKEN,NULL))
insection = 1;
if (insection && strmatch(str,"#else",NULL))
break;
} /* while */
if (!strmatch(str,"#else",&indent))
return; /* no (more) section found, quit */
insection=0;
/* dump the buffer as strings, separated with commas */
needseparator = 0;
while (*bufptr != '\0') {
assert((unsigned)(bufptr-buffer) < buffersize);
if (needseparator)
fprintf(output, "%s\n",separator);
fprintf(output, "%*c\"",indent+2,' ');
/* loop over string */
while (*bufptr != '\0') {
if (*bufptr<' ' || *bufptr >= 128 || *bufptr == '"' || *bufptr == '\\')
fprintf(output, "\\%03o", *bufptr);
else
fprintf(output, "%c", *bufptr);
bufptr++;
} /* while */
fprintf(output, "\"");
needseparator = 1;
bufptr++; /* skip '\0' */
} /* while */
fprintf(output, "%s\n",terminator);
bufptr++;
/* skip the input file until the #endif section */
while (fgets(str,sizeof str,input)!=NULL) {
if (strmatch(str,"#endif",NULL)) {
fprintf(output,"%s",str);
break; /* done */
} /* if */
} /* while */
} /* while - !feof(input) */
}
static void usage(void)
{
printf("Usage: scpack <filename> [output file]\n");
exit(1);
}
int main(int argc, char **argv)
{
FILE *in, *out;
unsigned char *buffer;
unsigned buffersize, orgbuffersize;
unsigned char pairtable[128][2];
if (argc < 2 || argc > 3)
usage();
if ((in=fopen(argv[1],"rt"))==NULL) {
printf("SCPACK: error opening input %s\n",argv[1]);
usage();
} /* if */
if (argc == 2) {
if ((out=fopen(TEMPFILE,"wt"))==NULL) {
printf("SCPACK: error opening temporary file %s\n",TEMPFILE);
usage();
} /* if */
} else {
if ((out=fopen(argv[2],"wt"))==NULL) {
printf("SCPACK: error opening output file %s\n",argv[2]);
usage();
} /* if */
} /* if */
buffer = (unsigned char *)malloc(MAXSIZE);
if (buffer == NULL) {
printf("SCPACK: error allocating memory\n");
return 1;
} /* if */
/* 1. read the buffer
* 2. compress the buffer
* 3. copy the file, insert the compressed buffer
*/
buffersize = readbuffer(in, buffer);
orgbuffersize = buffersize;
if (buffersize > 0) {
buffersize = compress(buffer, buffersize, pairtable);
writefile(in, out, buffer, buffersize, pairtable);
printf("SCPACK: compression ratio: %ld%% (%d -> %d)\n",
100L-(100L*buffersize)/orgbuffersize, orgbuffersize, buffersize);
} else {
printf("SCPACK: no SCPACK section found, nothing to do\n");
} /* if */
fclose(out);
fclose(in);
/* let the new file replace the old file */
if (buffersize == 0) {
if (argc == 2)
remove(TEMPFILE);
else
remove(argv[2]);
} else if (argc == 2) {
remove(argv[1]);
rename(TEMPFILE,argv[1]);
} /* if */
return 0;
}