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 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
47 #define BASEBITREC 5000
49 #define UNCOMPRESSED '\002'
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
);
59 Hunzip::Hunzip(const char * file
, const char * key
) {
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
) {
74 int allocatedbit
= BASEBITREC
;
75 const char * enc
= key
;
77 fin
= fopen(filename
, "rb");
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
);
88 if (strncmp(MAGIC_ENCRYPT
, in
, MAGICLEN
) == 0) {
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
);
98 if (fread(&c
, 1, 2, fin
) < 2) return fail(MSG_FORMAT
, filename
);
102 if (*(++enc
) == '\0') enc
= key
;
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
);
113 for (i
= 0; i
< n
; i
++) {
115 if (fread(c
, 1, 2, fin
) < 2) return fail(MSG_FORMAT
, filename
);
117 if (*(++enc
) == '\0') enc
= key
;
119 if (*(++enc
) == '\0') enc
= key
;
122 if (fread(&l
, 1, 1, fin
) < 1) return fail(MSG_FORMAT
, filename
);
124 if (*(++enc
) == '\0') enc
= key
;
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
;
133 for (j
= 0; j
< l
; j
++) {
134 int b
= (in
[j
/8] & (1 << (7 - (j
% 8)))) ? 1 : 0;
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
;
158 if (fin
) fclose(fin
);
159 if (filename
) free(filename
);
162 int Hunzip::getbuf() {
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;
172 if (oldp
== lastbit
) {
176 if (dec
[lastbit
].c
[0]) out
[o
++] = dec
[lastbit
].c
[1];
179 out
[o
++] = dec
[oldp
].c
[0];
180 out
[o
++] = dec
[oldp
].c
[1];
181 if (o
== BUFSIZE
) return o
;
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
];
199 if (++outc
== bufsiz
) {
203 linebuf
[l
- 1] = out
[outc
];
207 default: if (((unsigned char) out
[outc
]) < 47) {
208 if (out
[outc
] > 32) {
209 right
= out
[outc
] - 31;
210 if (++outc
== bufsiz
) {
215 if (out
[outc
] == 30) left
= 9; else left
= out
[outc
];
220 if (++outc
== bufsiz
) {
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
);