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