aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/embryo/src/bin/embryo_cc_scexpand.c
blob: 6ab34a17a576da735c42260f2c39f4910db82fd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
}