1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/compiler-init/scexpand.c Thu Jul 13 20:03:46 2006 +0000
1.3 @@ -0,0 +1,68 @@
1.4 +/* expand.c -- Byte Pair Encoding decompression */
1.5 +/* Copyright 1996 Philip Gage */
1.6 +
1.7 +/* Byte Pair Compression appeared in the September 1997
1.8 + * issue of C/C++ Users Journal. The original source code
1.9 + * may still be found at the web site of the magazine
1.10 + * (www.cuj.com).
1.11 + *
1.12 + * The decompressor has been modified by me (Thiadmer
1.13 + * Riemersma) to accept a string as input, instead of a
1.14 + * complete file.
1.15 + */
1.16 +#include <assert.h>
1.17 +#include <stdio.h>
1.18 +#include "sc.h"
1.19 +
1.20 +#define STACKSIZE 16
1.21 +
1.22 +SC_FUNC int strexpand(char *dest, unsigned char *source, int maxlen, unsigned char pairtable[128][2])
1.23 +{
1.24 + unsigned char stack[STACKSIZE];
1.25 + short c, top = 0;
1.26 + int len;
1.27 +
1.28 + assert(maxlen > 0);
1.29 + len = 1; /* already 1 byte for '\0' */
1.30 + for (;;) {
1.31 +
1.32 + /* Pop byte from stack or read byte from the input string */
1.33 + if (top)
1.34 + c = stack[--top];
1.35 + else if ((c = *(unsigned char *)source++) == '\0')
1.36 + break;
1.37 +
1.38 + /* Push pair on stack or output byte to the output string */
1.39 + if (c > 127) {
1.40 + assert(top+2 <= STACKSIZE);
1.41 + stack[top++] = pairtable[c-128][1];
1.42 + stack[top++] = pairtable[c-128][0];
1.43 + }
1.44 + else {
1.45 + len++;
1.46 + if (maxlen > 1) { /* reserve one byte for the '\0' */
1.47 + *dest++ = (char)c;
1.48 + maxlen--;
1.49 + }
1.50 + }
1.51 + }
1.52 + *dest = '\0';
1.53 + return len; /* return number of bytes decoded */
1.54 +}
1.55 +
1.56 +#if 0 /*for testing*/
1.57 +#include "sc5.scp"
1.58 +
1.59 +int main (int argc, char **argv)
1.60 +{
1.61 + int i;
1.62 + char str[128];
1.63 +
1.64 + for (i=0; i<58; i++) {
1.65 + strexpand(str, errmsg[i], sizeof str, SCPACK_TABLE);
1.66 + printf("%s", str);
1.67 + } /* for */
1.68 + return 0;
1.69 +}
1.70 +#endif
1.71 +