aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/embryo/embryo_cc_scexpand.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/embryo/embryo_cc_scexpand.c')
-rw-r--r--src/bin/embryo/embryo_cc_scexpand.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/bin/embryo/embryo_cc_scexpand.c b/src/bin/embryo/embryo_cc_scexpand.c
new file mode 100644
index 000000000..6ab34a17a
--- /dev/null
+++ b/src/bin/embryo/embryo_cc_scexpand.c
@@ -0,0 +1,53 @@
+/* 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 "embryo_cc_sc.h"
+
+#define STACKSIZE 16
+
+int
+strexpand(char *dest, unsigned char *source, int maxlen, unsigned char pairtable[128][2])
+{
+ unsigned char stack[STACKSIZE];
+ short c, top = 0;
+ int len;
+
+ 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)
+ {
+ stack[top++] = pairtable[c - 128][1];
+ stack[top++] = pairtable[c - 128][0];
+ }
+ else
+ {
+ len++;
+ if (maxlen > 1)
+ {
+ *dest++ = (char)c;
+ maxlen--;
+ }
+ }
+ }
+ *dest = '\0';
+ return len;
+}