9 #define BASEBITREC 5000
11 #define UNCOMPRESSED '\002'
13 #define MAGIC_ENCRYPT "hz1"
14 #define MAGICLEN (sizeof(MAGIC) - 1)
16 int Hunzip::fail(const char * err, const char * par) {
17 fprintf(stderr, err, par);
21 Hunzip::Hunzip(const char * file, const char * key)
30 in[0] = out[0] = line[0] = '\0';
31 filename = mystrdup(file);
32 if (getcode(key) == -1) bufsiz = -1;
33 else bufsiz = getbuf();
36 int Hunzip::getcode(const char * key) {
39 int allocatedbit = BASEBITREC;
40 const char * enc = key;
42 if (!filename) return -1;
44 fin = myfopen(filename, "rb");
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);
55 if (strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0) {
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);
65 if (fread(&c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
69 if (*(++enc) == '\0') enc = key;
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);
80 for (i = 0; i < n; i++) {
82 if (fread(c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
84 if (*(++enc) == '\0') enc = key;
86 if (*(++enc) == '\0') enc = key;
89 if (fread(&l, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
91 if (*(++enc) == '\0') enc = key;
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;
100 for (j = 0; j < l; j++) {
101 int b = (in[j/8] & (1 << (7 - (j % 8)))) ? 1 : 0;
106 if (lastbit == allocatedbit) {
107 allocatedbit += BASEBITREC;
108 dec = (struct bit *) realloc(dec, allocatedbit * sizeof(struct bit));
110 dec[lastbit].v[0] = 0;
111 dec[lastbit].v[1] = 0;
112 dec[oldp].v[b] = lastbit;
125 if (fin) fclose(fin);
126 if (filename) free(filename);
129 int Hunzip::getbuf() {
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;
139 if (oldp == lastbit) {
143 if (dec[lastbit].c[0]) out[o++] = dec[lastbit].c[1];
146 out[o++] = dec[oldp].c[0];
147 out[o++] = dec[oldp].c[1];
148 if (o == BUFSIZE) return o;
153 } while (inbits == BUFSIZE * 8);
154 return fail(MSG_FORMAT, filename);
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];
166 if (++outc == bufsiz) {
170 linebuf[l - 1] = out[outc];
174 default: if (((unsigned char) out[outc]) < 47) {
175 if (out[outc] > 32) {
176 right = out[outc] - 31;
177 if (++outc == bufsiz) {
182 if (out[outc] == 30) left = 9; else left = out[outc];
187 if (++outc == bufsiz) {
189 bufsiz = fin ? getbuf(): -1;
192 if (right) strcpy(linebuf + l - 1, line + strlen(line) - right - 1);
193 else linebuf[l] = '\0';
194 strcpy(line + left, linebuf);