1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * Copyright (C) 2002-2017 Németh László
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * Hunspell is based on MySpell which is Copyright (C) 2002 Kevin Hendricks.
18 * Contributor(s): David Einstein, Davide Prina, Giuseppe Modugno,
19 * Gianluca Turconi, Simon Brouwer, Noll János, Bíró Árpád,
20 * Goldman Eleonóra, Sarlós Tamás, Bencsáth Boldizsár, Halácsy Péter,
21 * Dvornik László, Gefferth András, Nagy Viktor, Varga Dániel, Chris Halls,
22 * Rene Engelhard, Bram Moolenaar, Dafydd Jones, Harri Pitkänen
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
46 #define BASEBITREC 5000
48 #define UNCOMPRESSED '\002'
50 #define MAGIC_ENCRYPT "hz1"
51 #define MAGICLEN (sizeof(MAGIC) - 1)
53 int Hunzip::fail(const char* err, const char* par) {
54 fprintf(stderr, err, par);
58 Hunzip::Hunzip(const char* file, const char* key)
59 : bufsiz(0), lastbit(0), inc(0), inbits(0), outc(0) {
60 in[0] = out[0] = line[0] = '\0';
61 filename = mystrdup(file);
62 if (getcode(key) == -1)
68 int Hunzip::getcode(const char* key) {
71 int allocatedbit = BASEBITREC;
72 const char* enc = key;
77 myopen(fin, filename, std::ios_base::in | std::ios_base::binary);
82 if (!fin.read(in, 3) ||
83 !(strncmp(MAGIC, in, MAGICLEN) == 0 ||
84 strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0)) {
85 return fail(MSG_FORMAT, filename);
89 if (strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0) {
92 return fail(MSG_KEY, filename);
93 if (!fin.read(reinterpret_cast<char*>(c), 1))
94 return fail(MSG_FORMAT, filename);
95 for (cs = 0; *enc; enc++)
98 return fail(MSG_KEY, filename);
104 if (!fin.read(reinterpret_cast<char*>(c), 2))
105 return fail(MSG_FORMAT, filename);
109 if (*(++enc) == '\0')
114 n = ((int)c[0] << 8) + c[1];
115 dec.resize(BASEBITREC);
120 for (i = 0; i < n; i++) {
122 if (!fin.read(reinterpret_cast<char*>(c), 2))
123 return fail(MSG_FORMAT, filename);
125 if (*(++enc) == '\0')
128 if (*(++enc) == '\0')
132 if (!fin.read(reinterpret_cast<char*>(&l), 1))
133 return fail(MSG_FORMAT, filename);
135 if (*(++enc) == '\0')
139 if (!fin.read(in, l / 8 + 1))
140 return fail(MSG_FORMAT, filename);
142 for (j = 0; j <= l / 8; j++) {
143 if (*(++enc) == '\0')
148 for (j = 0; j < l; j++) {
149 int b = (in[j / 8] & (1 << (7 - (j % 8)))) ? 1 : 0;
154 if (lastbit == allocatedbit) {
155 allocatedbit += BASEBITREC;
156 dec.resize(allocatedbit);
158 dec[lastbit].v[0] = 0;
159 dec[lastbit].v[1] = 0;
160 dec[oldp].v[b] = lastbit;
175 int Hunzip::getbuf() {
180 fin.read(in, BUFSIZE);
181 inbits = fin.gcount() * 8;
183 for (; inc < inbits; inc++) {
184 int b = (in[inc / 8] & (1 << (7 - (inc % 8)))) ? 1 : 0;
188 if (oldp == lastbit) {
191 if (dec[lastbit].c[0])
192 out[o++] = dec[lastbit].c[1];
195 out[o++] = dec[oldp].c[0];
196 out[o++] = dec[oldp].c[1];
203 } while (inbits == BUFSIZE * 8);
204 return fail(MSG_FORMAT, filename);
207 bool Hunzip::getline(std::string& dest) {
208 char linebuf[BUFSIZE];
209 int l = 0, eol = 0, left = 0, right = 0;
212 while (l < bufsiz && !eol) {
213 linebuf[l++] = out[outc];
218 if (++outc == bufsiz) {
222 linebuf[l - 1] = out[outc];
228 if (((unsigned char)out[outc]) < 47) {
229 if (out[outc] > 32) {
230 right = out[outc] - 31;
231 if (++outc == bufsiz) {
240 linebuf[l - 1] = '\n';
244 if (++outc == bufsiz) {
246 bufsiz = fin.is_open() ? getbuf() : -1;
250 strcpy(linebuf + l - 1, line + strlen(line) - right - 1);
253 strcpy(line + left, linebuf);