d72476522b688d0d7a7fb24766f2d696ec25d1f1
[framework/uifw/embryo.git] / src / bin / embryo_cc_scexpand.c
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
14 /*
15  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
16  */
17
18 #include "embryo_cc_sc.h"
19
20 #define STACKSIZE 16
21
22 int
23 strexpand(char *dest, unsigned char *source, int maxlen, unsigned char pairtable[128][2])
24 {
25    unsigned char       stack[STACKSIZE];
26    short               c, top = 0;
27    int                 len;
28
29    len = 1;                     /* already 1 byte for '\0' */
30    for (;;)
31      {
32         /* Pop byte from stack or read byte from the input string */
33         if (top)
34           c = stack[--top];
35         else if ((c = *(unsigned char *)source++) == '\0')
36           break;
37
38         /* Push pair on stack or output byte to the output string */
39         if (c > 127)
40           {
41              stack[top++] = pairtable[c - 128][1];
42              stack[top++] = pairtable[c - 128][0];
43           }
44         else
45           {
46              len++;
47              if (maxlen > 1)
48                {
49                   *dest++ = (char)c;
50                   maxlen--;
51                }
52           }
53      }
54    *dest = '\0';
55    return len;
56 }