1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14
* The Original Code is Hunspell, based on MySpell.
16
* The Initial Developers of the Original Code are
17
* Kevin Hendricks (MySpell) and Németh László (Hunspell).
18
* Portions created by the Initial Developers are Copyright (C) 2002-2005
19
* the Initial Developers. All Rights Reserved.
21
* Contributor(s): David Einstein, Davide Prina, Giuseppe Modugno,
22
* Gianluca Turconi, Simon Brouwer, Noll János, Bíró Árpád,
23
* Goldman Eleonóra, Sarlós Tamás, Bencsáth Boldizsár, Halácsy Péter,
24
* Dvornik László, Gefferth András, Nagy Viktor, Varga Dániel, Chris Halls,
25
* Rene Engelhard, Bram Moolenaar, Dafydd Jones, Harri Pitkänen
27
* Alternatively, the contents of this file may be used under the terms of
28
* either the GNU General Public License Version 2 or later (the "GPL"), or
29
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30
* in which case the provisions of the GPL or the LGPL are applicable instead
31
* of those above. If you wish to allow use of your version of this file only
32
* under the terms of either the GPL or the LGPL, and not to allow others to
33
* use your version of this file under the terms of the MPL, indicate your
34
* decision by deleting the provisions above and replace them with the notice
35
* and other provisions required by the GPL or the LGPL. If you do not delete
36
* the provisions above, a recipient may use your version of this file under
37
* the terms of any one of the MPL, the GPL or the LGPL.
39
* ***** END LICENSE BLOCK ***** */
49
#define BASEBITREC 5000
51
#define UNCOMPRESSED '\002'
53
#define MAGIC_ENCRYPT "hz1"
54
#define MAGICLEN (sizeof(MAGIC) - 1)
56
int Hunzip::fail(const char * err, const char * par) {
57
fprintf(stderr, err, par);
61
Hunzip::Hunzip(const char * file, const char * key)
70
in[0] = out[0] = line[0] = '\0';
71
filename = mystrdup(file);
72
if (getcode(key) == -1) bufsiz = -1;
73
else bufsiz = getbuf();
76
int Hunzip::getcode(const char * key) {
79
int allocatedbit = BASEBITREC;
80
const char * enc = key;
82
if (!filename) return -1;
84
fin = myfopen(filename, "rb");
88
if ((fread(in, 1, 3, fin) < MAGICLEN)
89
|| !(strncmp(MAGIC, in, MAGICLEN) == 0 ||
90
strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0)) {
91
return fail(MSG_FORMAT, filename);
95
if (strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0) {
97
if (!key) return fail(MSG_KEY, filename);
98
if (fread(&c, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
99
for (cs = 0; *enc; enc++) cs ^= *enc;
100
if (cs != c[0]) return fail(MSG_KEY, filename);
105
if (fread(&c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
109
if (*(++enc) == '\0') enc = key;
113
n = ((int) c[0] << 8) + c[1];
114
dec = (struct bit *) malloc(BASEBITREC * sizeof(struct bit));
115
if (!dec) return fail(MSG_MEMORY, filename);
120
for (i = 0; i < n; i++) {
122
if (fread(c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
124
if (*(++enc) == '\0') enc = key;
126
if (*(++enc) == '\0') enc = key;
129
if (fread(&l, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
131
if (*(++enc) == '\0') enc = key;
134
if (fread(in, 1, l/8+1, fin) < (size_t) l/8+1) return fail(MSG_FORMAT, filename);
135
if (key) for (j = 0; j <= l/8; j++) {
136
if (*(++enc) == '\0') enc = key;
140
for (j = 0; j < l; j++) {
141
int b = (in[j/8] & (1 << (7 - (j % 8)))) ? 1 : 0;
146
if (lastbit == allocatedbit) {
147
allocatedbit += BASEBITREC;
148
dec = (struct bit *) realloc(dec, allocatedbit * sizeof(struct bit));
150
dec[lastbit].v[0] = 0;
151
dec[lastbit].v[1] = 0;
152
dec[oldp].v[b] = lastbit;
165
if (fin) fclose(fin);
166
if (filename) free(filename);
169
int Hunzip::getbuf() {
173
if (inc == 0) inbits = fread(in, 1, BUFSIZE, fin) * 8;
174
for (; inc < inbits; inc++) {
175
int b = (in[inc / 8] & (1 << (7 - (inc % 8)))) ? 1 : 0;
179
if (oldp == lastbit) {
183
if (dec[lastbit].c[0]) out[o++] = dec[lastbit].c[1];
186
out[o++] = dec[oldp].c[0];
187
out[o++] = dec[oldp].c[1];
188
if (o == BUFSIZE) return o;
193
} while (inbits == BUFSIZE * 8);
194
return fail(MSG_FORMAT, filename);
197
const char * Hunzip::getline() {
198
char linebuf[BUFSIZE];
199
int l = 0, eol = 0, left = 0, right = 0;
200
if (bufsiz == -1) return NULL;
201
while (l < bufsiz && !eol) {
202
linebuf[l++] = out[outc];
206
if (++outc == bufsiz) {
210
linebuf[l - 1] = out[outc];
214
default: if (((unsigned char) out[outc]) < 47) {
215
if (out[outc] > 32) {
216
right = out[outc] - 31;
217
if (++outc == bufsiz) {
222
if (out[outc] == 30) left = 9; else left = out[outc];
227
if (++outc == bufsiz) {
229
bufsiz = fin ? getbuf(): -1;
232
if (right) strcpy(linebuf + l - 1, line + strlen(line) - right - 1);
233
else linebuf[l] = '\0';
234
strcpy(line + left, linebuf);