| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "decode.h" |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
#include "syserr.h" |
| 13 |
#include "exe.h" |
| 14 |
#include "memory.h" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
Aword *freq; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
static int decodeBuffer; |
| 24 |
static int bitsToGo; |
| 25 |
static int garbageBits; |
| 26 |
|
| 27 |
|
| 28 |
static int inputBit(void) |
| 29 |
{ |
| 30 |
int bit; |
| 31 |
|
| 32 |
if (!bitsToGo) { |
| 33 |
decodeBuffer = getc(textFile); |
| 34 |
if (decodeBuffer == EOF) { |
| 35 |
garbageBits++; |
| 36 |
if (garbageBits > VALUEBITS-2) |
| 37 |
syserr("Error in encoded data file."); |
| 38 |
} else |
| 39 |
bitsToGo = 8; |
| 40 |
} |
| 41 |
bit = decodeBuffer&1; |
| 42 |
decodeBuffer = decodeBuffer>>1; |
| 43 |
bitsToGo--; |
| 44 |
return bit; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static CodeValue value; |
| 51 |
static CodeValue low, high; |
| 52 |
|
| 53 |
|
| 54 |
void startDecoding(void) |
| 55 |
{ |
| 56 |
int i; |
| 57 |
|
| 58 |
bitsToGo = 0; |
| 59 |
garbageBits = 0; |
| 60 |
|
| 61 |
value = 0; |
| 62 |
for (i = 0; i < VALUEBITS; i++) |
| 63 |
value = 2*value + inputBit(); |
| 64 |
low = 0; |
| 65 |
high = TOPVALUE; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
int decodeChar(void) |
| 70 |
{ |
| 71 |
long range; |
| 72 |
int f; |
| 73 |
int symbol; |
| 74 |
|
| 75 |
range = (long)(high-low) + 1; |
| 76 |
f = (((long)(value-low)+1)*freq[0]-1)/range; |
| 77 |
|
| 78 |
|
| 79 |
for (symbol = 1; freq[symbol] > f; symbol++); |
| 80 |
|
| 81 |
high = low + range*freq[symbol-1]/freq[0]-1; |
| 82 |
low = low + range*freq[symbol]/freq[0]; |
| 83 |
|
| 84 |
for (;;) { |
| 85 |
if (high < HALF) |
| 86 |
; |
| 87 |
else if (low >= HALF) { |
| 88 |
value = value - HALF; |
| 89 |
low = low - HALF; |
| 90 |
high = high - HALF; |
| 91 |
} else if (low >= ONEQUARTER && high < THREEQUARTER) { |
| 92 |
value = value - ONEQUARTER; |
| 93 |
low = low - ONEQUARTER; |
| 94 |
high = high - ONEQUARTER; |
| 95 |
} else |
| 96 |
break; |
| 97 |
|
| 98 |
|
| 99 |
low = 2*low; |
| 100 |
high = 2*high+1; |
| 101 |
value = 2*value + inputBit(); |
| 102 |
} |
| 103 |
return symbol-1; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
typedef struct DecodeInfo { |
| 110 |
long fpos; |
| 111 |
int buffer; |
| 112 |
int bits; |
| 113 |
CodeValue value; |
| 114 |
CodeValue high; |
| 115 |
CodeValue low; |
| 116 |
} DecodeInfo; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
void *pushDecode(void) |
| 128 |
{ |
| 129 |
DecodeInfo *info; |
| 130 |
|
| 131 |
info = (DecodeInfo *) allocate(sizeof(DecodeInfo)); |
| 132 |
info->fpos = ftell(textFile); |
| 133 |
info->buffer = decodeBuffer; |
| 134 |
info->bits = bitsToGo; |
| 135 |
info->value = value; |
| 136 |
info->high = high; |
| 137 |
info->low = low; |
| 138 |
return(info); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
void popDecode(void *i) |
| 151 |
{ |
| 152 |
DecodeInfo *info = (DecodeInfo *) i; |
| 153 |
|
| 154 |
fseek(textFile, info->fpos, 0); |
| 155 |
decodeBuffer = info->buffer; |
| 156 |
bitsToGo = info->bits; |
| 157 |
value = info->value; |
| 158 |
high = info->high; |
| 159 |
low = info->low; |
| 160 |
|
| 161 |
free(info); |
| 162 |
} |