8 #define BASEBITREC 5000
10 #define UNCOMPRESSED '\002'
12 #define MAGIC_ENCRYPT "hz1"
13 #define MAGICLEN (sizeof(MAGIC) - 1)
15 int Hunzip::fail(const char * err
, const char * par
) {
16 fprintf(stderr
, err
, par
);
20 Hunzip::Hunzip(const char * file
, const char * key
) {
27 filename
= (char *) malloc(strlen(file
) + 1);
28 if (filename
) strcpy(filename
, file
);
29 if (getcode(key
) == -1) bufsiz
= -1;
30 else bufsiz
= getbuf();
33 int Hunzip::getcode(const char * key
) {
36 int allocatedbit
= BASEBITREC
;
37 const char * enc
= key
;
39 if (!filename
) return -1;
41 fin
= fopen(filename
, "rb");
45 if ((fread(in
, 1, 3, fin
) < MAGICLEN
)
46 || !(strncmp(MAGIC
, in
, MAGICLEN
) == 0 ||
47 strncmp(MAGIC_ENCRYPT
, in
, MAGICLEN
) == 0)) {
48 return fail(MSG_FORMAT
, filename
);
52 if (strncmp(MAGIC_ENCRYPT
, in
, MAGICLEN
) == 0) {
54 if (!key
) return fail(MSG_KEY
, filename
);
55 if (fread(&c
, 1, 1, fin
) < 1) return fail(MSG_FORMAT
, filename
);
56 for (cs
= 0; *enc
; enc
++) cs
^= *enc
;
57 if (cs
!= c
[0]) return fail(MSG_KEY
, filename
);
62 if (fread(&c
, 1, 2, fin
) < 2) return fail(MSG_FORMAT
, filename
);
66 if (*(++enc
) == '\0') enc
= key
;
70 n
= ((int) c
[0] << 8) + c
[1];
71 dec
= (struct bit
*) malloc(BASEBITREC
* sizeof(struct bit
));
72 if (!dec
) return fail(MSG_MEMORY
, filename
);
77 for (i
= 0; i
< n
; i
++) {
79 if (fread(c
, 1, 2, fin
) < 2) return fail(MSG_FORMAT
, filename
);
81 if (*(++enc
) == '\0') enc
= key
;
83 if (*(++enc
) == '\0') enc
= key
;
86 if (fread(&l
, 1, 1, fin
) < 1) return fail(MSG_FORMAT
, filename
);
88 if (*(++enc
) == '\0') enc
= key
;
91 if (fread(in
, 1, l
/8+1, fin
) < (size_t) l
/8+1) return fail(MSG_FORMAT
, filename
);
92 if (key
) for (j
= 0; j
<= l
/8; j
++) {
93 if (*(++enc
) == '\0') enc
= key
;
97 for (j
= 0; j
< l
; j
++) {
98 int b
= (in
[j
/8] & (1 << (7 - (j
% 8)))) ? 1 : 0;
103 if (lastbit
== allocatedbit
) {
104 allocatedbit
+= BASEBITREC
;
105 dec
= (struct bit
*) realloc(dec
, allocatedbit
* sizeof(struct bit
));
107 dec
[lastbit
].v
[0] = 0;
108 dec
[lastbit
].v
[1] = 0;
109 dec
[oldp
].v
[b
] = lastbit
;
122 if (fin
) fclose(fin
);
123 if (filename
) free(filename
);
126 int Hunzip::getbuf() {
130 if (inc
== 0) inbits
= fread(in
, 1, BUFSIZE
, fin
) * 8;
131 for (; inc
< inbits
; inc
++) {
132 int b
= (in
[inc
/ 8] & (1 << (7 - (inc
% 8)))) ? 1 : 0;
136 if (oldp
== lastbit
) {
140 if (dec
[lastbit
].c
[0]) out
[o
++] = dec
[lastbit
].c
[1];
143 out
[o
++] = dec
[oldp
].c
[0];
144 out
[o
++] = dec
[oldp
].c
[1];
145 if (o
== BUFSIZE
) return o
;
150 } while (inbits
== BUFSIZE
* 8);
151 return fail(MSG_FORMAT
, filename
);
154 const char * Hunzip::getline() {
155 char linebuf
[BUFSIZE
];
156 int l
= 0, eol
= 0, left
= 0, right
= 0;
157 if (bufsiz
== -1) return NULL
;
158 while (l
< bufsiz
&& !eol
) {
159 linebuf
[l
++] = out
[outc
];
163 if (++outc
== bufsiz
) {
167 linebuf
[l
- 1] = out
[outc
];
171 default: if (((unsigned char) out
[outc
]) < 47) {
172 if (out
[outc
] > 32) {
173 right
= out
[outc
] - 31;
174 if (++outc
== bufsiz
) {
179 if (out
[outc
] == 30) left
= 9; else left
= out
[outc
];
184 if (++outc
== bufsiz
) {
186 bufsiz
= fin
? getbuf(): -1;
189 if (right
) strcpy(linebuf
+ l
- 1, line
+ strlen(line
) - right
- 1);
190 else linebuf
[l
] = '\0';
191 strcpy(line
+ left
, linebuf
);