Updated hunspell to 1.3.2
[TortoiseGit.git] / ext / hunspell / hunzip.cxx
blobac77028e6e0799767b28c4b0c4aa08b1e95a72a8
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
5 #include "hunzip.hxx"
7 #define CODELEN 65536
8 #define BASEBITREC 5000
10 #define UNCOMPRESSED '\002'
11 #define MAGIC "hz0"
12 #define MAGIC_ENCRYPT "hz1"
13 #define MAGICLEN (sizeof(MAGIC) - 1)
15 int Hunzip::fail(const char * err, const char * par) {
16 fprintf(stderr, err, par);
17 return -1;
20 Hunzip::Hunzip(const char * file, const char * key) {
21 bufsiz = 0;
22 lastbit = 0;
23 inc = 0;
24 outc = 0;
25 dec = NULL;
26 fin = NULL;
27 filename = (char *) malloc(strlen(file) + 1);
28 if (filename) strcpy(filename, file);
29 if (getcode(key) == -1) bufsiz = -1;
30 else bufsiz = getbuf();
33 int Hunzip::getcode(const char * key) {
34 unsigned char c[2];
35 int i, j, n, p;
36 int allocatedbit = BASEBITREC;
37 const char * enc = key;
39 if (!filename) return -1;
41 fin = fopen(filename, "rb");
42 if (!fin) return -1;
44 // read magic number
45 if ((fread(in, 1, 3, fin) < MAGICLEN)
46 || !(strncmp(MAGIC, in, MAGICLEN) == 0 ||
47 strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0)) {
48 return fail(MSG_FORMAT, filename);
51 // check encryption
52 if (strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0) {
53 unsigned char cs;
54 if (!key) return fail(MSG_KEY, filename);
55 if (fread(&c, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
56 for (cs = 0; *enc; enc++) cs ^= *enc;
57 if (cs != c[0]) return fail(MSG_KEY, filename);
58 enc = key;
59 } else key = NULL;
61 // read record count
62 if (fread(&c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
64 if (key) {
65 c[0] ^= *enc;
66 if (*(++enc) == '\0') enc = key;
67 c[1] ^= *enc;
70 n = ((int) c[0] << 8) + c[1];
71 dec = (struct bit *) malloc(BASEBITREC * sizeof(struct bit));
72 if (!dec) return fail(MSG_MEMORY, filename);
73 dec[0].v[0] = 0;
74 dec[0].v[1] = 0;
76 // read codes
77 for (i = 0; i < n; i++) {
78 unsigned char l;
79 if (fread(c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
80 if (key) {
81 if (*(++enc) == '\0') enc = key;
82 c[0] ^= *enc;
83 if (*(++enc) == '\0') enc = key;
84 c[1] ^= *enc;
86 if (fread(&l, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
87 if (key) {
88 if (*(++enc) == '\0') enc = key;
89 l ^= *enc;
91 if (fread(in, 1, l/8+1, fin) < (size_t) l/8+1) return fail(MSG_FORMAT, filename);
92 if (key) for (j = 0; j <= l/8; j++) {
93 if (*(++enc) == '\0') enc = key;
94 in[j] ^= *enc;
96 p = 0;
97 for (j = 0; j < l; j++) {
98 int b = (in[j/8] & (1 << (7 - (j % 8)))) ? 1 : 0;
99 int oldp = p;
100 p = dec[p].v[b];
101 if (p == 0) {
102 lastbit++;
103 if (lastbit == allocatedbit) {
104 allocatedbit += BASEBITREC;
105 dec = (struct bit *) realloc(dec, allocatedbit * sizeof(struct bit));
107 dec[lastbit].v[0] = 0;
108 dec[lastbit].v[1] = 0;
109 dec[oldp].v[b] = lastbit;
110 p = lastbit;
113 dec[p].c[0] = c[0];
114 dec[p].c[1] = c[1];
116 return 0;
119 Hunzip::~Hunzip()
121 if (dec) free(dec);
122 if (fin) fclose(fin);
123 if (filename) free(filename);
126 int Hunzip::getbuf() {
127 int p = 0;
128 int o = 0;
129 do {
130 if (inc == 0) inbits = fread(in, 1, BUFSIZE, fin) * 8;
131 for (; inc < inbits; inc++) {
132 int b = (in[inc / 8] & (1 << (7 - (inc % 8)))) ? 1 : 0;
133 int oldp = p;
134 p = dec[p].v[b];
135 if (p == 0) {
136 if (oldp == lastbit) {
137 fclose(fin);
138 fin = NULL;
139 // add last odd byte
140 if (dec[lastbit].c[0]) out[o++] = dec[lastbit].c[1];
141 return o;
143 out[o++] = dec[oldp].c[0];
144 out[o++] = dec[oldp].c[1];
145 if (o == BUFSIZE) return o;
146 p = dec[p].v[b];
149 inc = 0;
150 } while (inbits == BUFSIZE * 8);
151 return fail(MSG_FORMAT, filename);
154 const char * Hunzip::getline() {
155 char linebuf[BUFSIZE];
156 int l = 0, eol = 0, left = 0, right = 0;
157 if (bufsiz == -1) return NULL;
158 while (l < bufsiz && !eol) {
159 linebuf[l++] = out[outc];
160 switch (out[outc]) {
161 case '\t': break;
162 case 31: { // escape
163 if (++outc == bufsiz) {
164 bufsiz = getbuf();
165 outc = 0;
167 linebuf[l - 1] = out[outc];
168 break;
170 case ' ': break;
171 default: if (((unsigned char) out[outc]) < 47) {
172 if (out[outc] > 32) {
173 right = out[outc] - 31;
174 if (++outc == bufsiz) {
175 bufsiz = getbuf();
176 outc = 0;
179 if (out[outc] == 30) left = 9; else left = out[outc];
180 linebuf[l-1] = '\n';
181 eol = 1;
184 if (++outc == bufsiz) {
185 outc = 0;
186 bufsiz = fin ? getbuf(): -1;
189 if (right) strcpy(linebuf + l - 1, line + strlen(line) - right - 1);
190 else linebuf[l] = '\0';
191 strcpy(line + left, linebuf);
192 return line;