sourcepawn/compiler/scexpand.c
author David Anderson <dvander@alliedmods.net>
Mon Mar 17 01:09:28 2008 +0000 (2008-03-17)
changeset 1941 54539fa4e51f
parent 27 764c6dd3dc25
permissions -rw-r--r--
bumped versions
     1 /* expand.c -- Byte Pair Encoding decompression */
     2 /* Copyright 1996 Philip Gage */
     3 
     4 /* Byte Pair Compression appeared in the September 1997
     5  * issue of C/C++ Users Journal. The original source code
     6  * may still be found at the web site of the magazine
     7  * (www.cuj.com).
     8  *
     9  * The decompressor has been modified by me (Thiadmer
    10  * Riemersma) to accept a string as input, instead of a
    11  * complete file.
    12  */
    13 #include <assert.h>
    14 #include <stdio.h>
    15 #include "sc.h"
    16 
    17 #define STACKSIZE 16
    18 
    19 SC_FUNC int strexpand(char *dest, unsigned char *source, int maxlen, unsigned char pairtable[128][2])
    20 {
    21   unsigned char stack[STACKSIZE];
    22   short c, top = 0;
    23   int len;
    24 
    25   assert(maxlen > 0);
    26   len = 1;              /* already 1 byte for '\0' */
    27   for (;;) {
    28 
    29     /* Pop byte from stack or read byte from the input string */
    30     if (top)
    31       c = stack[--top];
    32     else if ((c = *(unsigned char *)source++) == '\0')
    33       break;
    34 
    35     /* Push pair on stack or output byte to the output string */
    36     if (c > 127) {
    37       assert(top+2 <= STACKSIZE);
    38       stack[top++] = pairtable[c-128][1];
    39       stack[top++] = pairtable[c-128][0];
    40     }
    41     else {
    42       len++;
    43       if (maxlen > 1) { /* reserve one byte for the '\0' */
    44         *dest++ = (char)c;
    45         maxlen--;
    46       }
    47     }
    48   }
    49   *dest = '\0';
    50   return len;           /* return number of bytes decoded */
    51 }
    52 
    53 #if 0 /*for testing*/
    54 #include "sc5.scp"
    55 
    56 int main (int argc, char **argv)
    57 {
    58   int i;
    59   char str[128];
    60 
    61   for (i=0; i<58; i++) {
    62     strexpand(str, errmsg[i], sizeof str, SCPACK_TABLE);
    63     printf("%s", str);
    64   } /* for */
    65   return 0;
    66 }
    67 #endif
    68