~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/SpellChecker/hunspell/src/hunspell/hunzip.cxx

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::Blocks 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
13
 *
 
14
 * The Original Code is Hunspell, based on MySpell.
 
15
 *
 
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.
 
20
 *
 
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
 
26
 *
 
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.
 
38
 *
 
39
 * ***** END LICENSE BLOCK ***** */
 
40
 
 
41
#include <stdlib.h> 
 
42
#include <string.h>
 
43
#include <stdio.h> 
 
44
 
 
45
#include "hunzip.hxx"
 
46
#include "csutil.hxx"
 
47
 
 
48
#define CODELEN  65536
 
49
#define BASEBITREC 5000
 
50
 
 
51
#define UNCOMPRESSED '\002'
 
52
#define MAGIC "hz0"
 
53
#define MAGIC_ENCRYPT "hz1"
 
54
#define MAGICLEN (sizeof(MAGIC) - 1)
 
55
 
 
56
int Hunzip::fail(const char * err, const char * par) {
 
57
    fprintf(stderr, err, par);
 
58
    return -1;
 
59
}
 
60
 
 
61
Hunzip::Hunzip(const char * file, const char * key)
 
62
    : fin(NULL)
 
63
    , bufsiz(0)
 
64
    , lastbit(0)
 
65
    , inc(0)
 
66
    , inbits(0)
 
67
    , outc(0)
 
68
    , dec(NULL)
 
69
{
 
70
    in[0] = out[0] = line[0] = '\0';
 
71
    filename = mystrdup(file);
 
72
    if (getcode(key) == -1) bufsiz = -1;
 
73
    else bufsiz = getbuf();
 
74
}
 
75
 
 
76
int Hunzip::getcode(const char * key) {
 
77
    unsigned char c[2];
 
78
    int i, j, n, p;
 
79
    int allocatedbit = BASEBITREC;
 
80
    const char * enc = key;
 
81
 
 
82
    if (!filename) return -1;
 
83
 
 
84
    fin = myfopen(filename, "rb");
 
85
    if (!fin) return -1;
 
86
 
 
87
    // read magic number
 
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);
 
92
    }
 
93
 
 
94
    // check encryption
 
95
    if (strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0) {
 
96
        unsigned char cs;
 
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);
 
101
        enc = key;
 
102
    } else key = NULL;
 
103
 
 
104
    // read record count
 
105
    if (fread(&c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
 
106
 
 
107
    if (key) {
 
108
        c[0] ^= *enc;
 
109
        if (*(++enc) == '\0') enc = key;
 
110
        c[1] ^= *enc;
 
111
    }        
 
112
    
 
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);
 
116
    dec[0].v[0] = 0;
 
117
    dec[0].v[1] = 0;
 
118
 
 
119
    // read codes
 
120
    for (i = 0; i < n; i++) {
 
121
        unsigned char l;
 
122
        if (fread(c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
 
123
        if (key) {
 
124
            if (*(++enc) == '\0') enc = key;
 
125
            c[0] ^= *enc;
 
126
            if (*(++enc) == '\0') enc = key;            
 
127
            c[1] ^= *enc;
 
128
        }        
 
129
        if (fread(&l, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
 
130
        if (key) {
 
131
            if (*(++enc) == '\0') enc = key;
 
132
            l ^= *enc;
 
133
        }
 
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;
 
137
            in[j] ^= *enc;
 
138
        }
 
139
        p = 0;
 
140
        for (j = 0; j < l; j++) {
 
141
            int b = (in[j/8] & (1 << (7 - (j % 8)))) ? 1 : 0;
 
142
            int oldp = p;
 
143
            p = dec[p].v[b];
 
144
            if (p == 0) {
 
145
                lastbit++;
 
146
                if (lastbit == allocatedbit) {
 
147
                    allocatedbit += BASEBITREC;
 
148
                    dec = (struct bit *) realloc(dec, allocatedbit * sizeof(struct bit));
 
149
                }
 
150
                dec[lastbit].v[0] = 0;
 
151
                dec[lastbit].v[1] = 0;
 
152
                dec[oldp].v[b] = lastbit;
 
153
                p = lastbit;
 
154
            }
 
155
        }
 
156
        dec[p].c[0] = c[0];
 
157
        dec[p].c[1] = c[1];
 
158
    }
 
159
    return 0;
 
160
}
 
161
 
 
162
Hunzip::~Hunzip()
 
163
{
 
164
    if (dec) free(dec);
 
165
    if (fin) fclose(fin);
 
166
    if (filename) free(filename);
 
167
}
 
168
 
 
169
int Hunzip::getbuf() {
 
170
    int p = 0;
 
171
    int o = 0;
 
172
    do {
 
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;
 
176
            int oldp = p;
 
177
            p = dec[p].v[b];
 
178
            if (p == 0) {
 
179
                if (oldp == lastbit) {
 
180
                    fclose(fin);
 
181
                    fin = NULL;
 
182
                    // add last odd byte
 
183
                    if (dec[lastbit].c[0]) out[o++]  = dec[lastbit].c[1];
 
184
                    return o;
 
185
                }
 
186
                out[o++] = dec[oldp].c[0];
 
187
                out[o++] = dec[oldp].c[1];
 
188
                if (o == BUFSIZE) return o;
 
189
                p = dec[p].v[b];
 
190
            }
 
191
        }
 
192
        inc = 0;
 
193
    } while (inbits == BUFSIZE * 8);
 
194
    return fail(MSG_FORMAT, filename);
 
195
}
 
196
 
 
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];
 
203
        switch (out[outc]) {
 
204
            case '\t': break;
 
205
            case 31: { // escape
 
206
                if (++outc == bufsiz) {
 
207
                    bufsiz = getbuf();
 
208
                    outc = 0;
 
209
                }
 
210
                linebuf[l - 1] = out[outc];
 
211
                break;
 
212
            }
 
213
            case ' ': break;
 
214
            default: if (((unsigned char) out[outc]) < 47) {
 
215
                if (out[outc] > 32) {
 
216
                    right = out[outc] - 31;
 
217
                    if (++outc == bufsiz) {
 
218
                        bufsiz = getbuf();
 
219
                        outc = 0;
 
220
                    }
 
221
                }
 
222
                if (out[outc] == 30) left = 9; else left = out[outc];
 
223
                linebuf[l-1] = '\n';
 
224
                eol = 1;
 
225
            }
 
226
        }
 
227
        if (++outc == bufsiz) {
 
228
            outc = 0;
 
229
            bufsiz = fin ? getbuf(): -1;
 
230
        }
 
231
    }
 
232
    if (right) strcpy(linebuf + l - 1, line + strlen(line) - right - 1);
 
233
    else linebuf[l] = '\0';
 
234
    strcpy(line + left, linebuf);
 
235
    return line;
 
236
}