aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/embryo/src/bin/embryo_cc_scexpand.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/embryo/src/bin/embryo_cc_scexpand.c')
-rw-r--r--libraries/embryo/src/bin/embryo_cc_scexpand.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/libraries/embryo/src/bin/embryo_cc_scexpand.c b/libraries/embryo/src/bin/embryo_cc_scexpand.c
new file mode 100644
index 0000000..6ab34a1
--- /dev/null
+++ b/libraries/embryo/src/bin/embryo_cc_scexpand.c
@@ -0,0 +1,53 @@
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#include "embryo_cc_sc.h"
16
17#define STACKSIZE 16
18
19int
20strexpand(char *dest, unsigned char *source, int maxlen, unsigned char pairtable[128][2])
21{
22 unsigned char stack[STACKSIZE];
23 short c, top = 0;
24 int len;
25
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 {
38 stack[top++] = pairtable[c - 128][1];
39 stack[top++] = pairtable[c - 128][0];
40 }
41 else
42 {
43 len++;
44 if (maxlen > 1)
45 {
46 *dest++ = (char)c;
47 maxlen--;
48 }
49 }
50 }
51 *dest = '\0';
52 return len;
53}