Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / extensions / spellcheck / hunspell / src / hunzip.cpp
blobddf410783a6bf8f974933e83af949e8b67ef5222
1 /******* BEGIN LICENSE BLOCK *******
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Initial Developers of the Original Code are Kevin Hendricks (MySpell)
15 * and László Németh (Hunspell). Portions created by the Initial Developers
16 * are Copyright (C) 2002-2005 the Initial Developers. All Rights Reserved.
18 * Contributor(s): László Németh (nemethl@gyorsposta.hu)
20 * Alternatively, the contents of this file may be used under the terms of
21 * either the GNU General Public License Version 2 or later (the "GPL"), or
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the GPL or the LGPL are applicable instead
24 * of those above. If you wish to allow use of your version of this file only
25 * under the terms of either the GPL or the LGPL, and not to allow others to
26 * use your version of this file under the terms of the MPL, indicate your
27 * decision by deleting the provisions above and replace them with the notice
28 * and other provisions required by the GPL or the LGPL. If you do not delete
29 * the provisions above, a recipient may use your version of this file under
30 * the terms of any one of the MPL, the GPL or the LGPL.
32 ******* END LICENSE BLOCK *******/
34 #ifndef MOZILLA_CLIENT
35 #include <cstdlib>
36 #include <cstring>
37 #include <cstdio>
38 #else
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #endif
44 #include "hunzip.hxx"
46 #define CODELEN 65536
47 #define BASEBITREC 5000
49 #define UNCOMPRESSED '\002'
50 #define MAGIC "hz0"
51 #define MAGIC_ENCRYPT "hz1"
52 #define MAGICLEN (sizeof(MAGIC) - 1)
54 int Hunzip::fail(const char * err, const char * par) {
55 fprintf(stderr, err, par);
56 return -1;
59 Hunzip::Hunzip(const char * file, const char * key) {
60 bufsiz = 0;
61 lastbit = 0;
62 inc = 0;
63 outc = 0;
64 dec = NULL;
65 filename = (char *) malloc(strlen(file) + 1);
66 if (filename) strcpy(filename, file);
67 if (getcode(key) == -1) bufsiz = -1;
68 else bufsiz = getbuf();
71 int Hunzip::getcode(const char * key) {
72 unsigned char c[2];
73 int i, j, n, p;
74 int allocatedbit = BASEBITREC;
75 const char * enc = key;
77 fin = fopen(filename, "rb");
78 if (!fin) return -1;
80 // read magic number
81 if ((fread(in, 1, 3, fin) < MAGICLEN)
82 || !(strncmp(MAGIC, in, MAGICLEN) == 0 ||
83 strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0)) {
84 return fail(MSG_FORMAT, filename);
87 // check encryption
88 if (strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0) {
89 unsigned char cs;
90 if (!key) return fail(MSG_KEY, filename);
91 if (fread(&c, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
92 for (cs = 0; *enc; enc++) cs ^= *enc;
93 if (cs != c[0]) return fail(MSG_KEY, filename);
94 enc = key;
95 } else key = NULL;
97 // read record count
98 if (fread(&c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
100 if (key) {
101 c[0] ^= *enc;
102 if (*(++enc) == '\0') enc = key;
103 c[1] ^= *enc;
106 n = ((int) c[0] << 8) + c[1];
107 dec = (struct bit *) malloc(BASEBITREC * sizeof(struct bit));
108 if (!dec) return fail(MSG_MEMORY, filename);
109 dec[0].v[0] = 0;
110 dec[0].v[1] = 0;
112 // read codes
113 for (i = 0; i < n; i++) {
114 unsigned char l;
115 if (fread(c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
116 if (key) {
117 if (*(++enc) == '\0') enc = key;
118 c[0] ^= *enc;
119 if (*(++enc) == '\0') enc = key;
120 c[1] ^= *enc;
122 if (fread(&l, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
123 if (key) {
124 if (*(++enc) == '\0') enc = key;
125 l ^= *enc;
127 if (fread(in, 1, l/8+1, fin) < (size_t) l/8+1) return fail(MSG_FORMAT, filename);
128 if (key) for (j = 0; j <= l/8; j++) {
129 if (*(++enc) == '\0') enc = key;
130 in[j] ^= *enc;
132 p = 0;
133 for (j = 0; j < l; j++) {
134 int b = (in[j/8] & (1 << (7 - (j % 8)))) ? 1 : 0;
135 int oldp = p;
136 p = dec[p].v[b];
137 if (p == 0) {
138 lastbit++;
139 if (lastbit == allocatedbit) {
140 allocatedbit += BASEBITREC;
141 dec = (struct bit *) realloc(dec, allocatedbit * sizeof(struct bit));
143 dec[lastbit].v[0] = 0;
144 dec[lastbit].v[1] = 0;
145 dec[oldp].v[b] = lastbit;
146 p = lastbit;
149 dec[p].c[0] = c[0];
150 dec[p].c[1] = c[1];
152 return 0;
155 Hunzip::~Hunzip()
157 if (dec) free(dec);
158 if (fin) fclose(fin);
159 if (filename) free(filename);
162 int Hunzip::getbuf() {
163 int p = 0;
164 int o = 0;
165 do {
166 if (inc == 0) inbits = fread(in, 1, BUFSIZE, fin) * 8;
167 for (; inc < inbits; inc++) {
168 int b = (in[inc / 8] & (1 << (7 - (inc % 8)))) ? 1 : 0;
169 int oldp = p;
170 p = dec[p].v[b];
171 if (p == 0) {
172 if (oldp == lastbit) {
173 fclose(fin);
174 fin = NULL;
175 // add last odd byte
176 if (dec[lastbit].c[0]) out[o++] = dec[lastbit].c[1];
177 return o;
179 out[o++] = dec[oldp].c[0];
180 out[o++] = dec[oldp].c[1];
181 if (o == BUFSIZE) return o;
182 p = dec[p].v[b];
185 inc = 0;
186 } while (inbits == BUFSIZE * 8);
187 return fail(MSG_FORMAT, filename);
190 const char * Hunzip::getline() {
191 char linebuf[BUFSIZE];
192 int l = 0, eol = 0, left = 0, right = 0;
193 if (bufsiz == -1) return NULL;
194 while (l < bufsiz && !eol) {
195 linebuf[l++] = out[outc];
196 switch (out[outc]) {
197 case '\t': break;
198 case 31: { // escape
199 if (++outc == bufsiz) {
200 bufsiz = getbuf();
201 outc = 0;
203 linebuf[l - 1] = out[outc];
204 break;
206 case ' ': break;
207 default: if (((unsigned char) out[outc]) < 47) {
208 if (out[outc] > 32) {
209 right = out[outc] - 31;
210 if (++outc == bufsiz) {
211 bufsiz = getbuf();
212 outc = 0;
215 if (out[outc] == 30) left = 9; else left = out[outc];
216 linebuf[l-1] = '\n';
217 eol = 1;
220 if (++outc == bufsiz) {
221 outc = 0;
222 bufsiz = fin ? getbuf(): -1;
225 if (right) strcpy(linebuf + l - 1, line + strlen(line) - right - 1);
226 else linebuf[l] = '\0';
227 strcpy(line + left, linebuf);
228 return line;