aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan2/decode.cpp
blob: 1995637eb0b2cec9fdb306ad53df2a9cb753884c (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include <stdlib.h>
#include "glk/alan2/main.h"
#include "glk/alan2/decode.h"

namespace Glk {
namespace Alan2 {

/* Bit output */
static int decodeBuffer;    /* Bits to be input */
static int bitsToGo;        /* Bits still in buffer */
static int garbageBits;     /* Bits past EOD */

static int inputBit() {
	int bit;

	if (!bitsToGo) {      /* More bits available ? */
		decodeBuffer = txtfil->readByte(); /* No, so get more */
		if ((uint)decodeBuffer == EOD) {
			garbageBits++;
			if (garbageBits > VALUEBITS - 2)
				syserr("Error in encoded data file.");
		} else
			bitsToGo = 8;     /* Another Char, 8 new bits */
	}
	bit = decodeBuffer & 1;   /* Get next bit */
	decodeBuffer = decodeBuffer >> 1; /* and remove it */
	bitsToGo--;
	return bit;
}


/* Current state of decoding */

static CodeValue value;         /* Currently seen code value */
static CodeValue low, high;     /* Current code region */


void startDecoding() {
	int i;

	bitsToGo = 0;
	garbageBits = 0;

	value = 0;
	for (i = 0; i < VALUEBITS; i++)
		value = 2 * value + inputBit();
	low = 0;
	high = TOPVALUE;
}

int decodeChar() {
	long range;
	int f;
	int symbol;

	range = (long)(high - low) + 1;
	f = (((long)(value - low) + 1) * freq[0] - 1) / range;

	/* Find the symbol */
	for (symbol = 1; (int)freq[symbol] > f; symbol++);

	high = low + range * freq[symbol - 1] / freq[0] - 1;
	low = low + range * freq[symbol] / freq[0];

	for (;;) {
		if (high < HALF)
			;
		else if (low >= HALF) {
			value = value - HALF;
			low = low - HALF;
			high = high - HALF;
		} else if (low >= ONEQUARTER && high < THREEQUARTER) {
			value = value - ONEQUARTER;
			low = low - ONEQUARTER;
			high = high - ONEQUARTER;
		} else
			break;

		/* Scale up the range */
		low = 2 * low;
		high = 2 * high + 1;
		value = 2 * value + inputBit();
	}
	return symbol - 1;
}



/* Structure for saved decode info */
typedef struct DecodeInfo {
	long fpos;
	int buffer;
	int bits;
	CodeValue value;
	CodeValue high;
	CodeValue low;
} DecodeInfo;


/*======================================================================

  pushDecode()

  Save so much info about the decoding process so it is possible to
  restore and continue later.

 */
void *pushDecode() {
	DecodeInfo *info;

	info = (DecodeInfo *) allocate(sizeof(DecodeInfo));
	info->fpos = txtfil->pos();
	info->buffer = decodeBuffer;
	info->bits = bitsToGo;
	info->value = value;
	info->high = high;
	info->low = low;
	return (info);
}


/*======================================================================

  popDecode()

  Restore enough info about the decoding process so it is possible to
  continue after having decoded something else.

 */
void popDecode(void *i) {
	DecodeInfo *info = (DecodeInfo *) i;
	fseek(txtfil, info->fpos, 0);
	decodeBuffer = info->buffer;
	bitsToGo = info->bits;
	value = info->value;
	high = info->high;
	low = info->low;

	free(info);
}

} // End of namespace Alan2
} // End of namespace Glk