34 #include <sys/types.h>
66 BESDEBUG(
"bes",
"BESUncompressZ::uncompress - src=" << src.c_str() << endl ) ;
68 srcFile = open( src.c_str(), O_RDONLY ) ;
72 string err =
"Unable to open the compressed file " + src
74 char *serr = strerror( my_errno ) ;
81 err.append(
"unknown error occurred" ) ;
89 BESDEBUG(
"bes",
"BESUncompressZ::uncompress - target=" << target.c_str() << endl ) ;
91 destFile = open( target.c_str(), O_WRONLY | O_CREAT | O_TRUNC
92 , S_IRUSR | S_IWUSR ) ;
95 string err =
"Unable to create the uncompressed file "
97 char *serr = strerror( my_errno ) ;
104 err.append(
"unknown error occurred" ) ;
115 BESDEBUG(
"bes",
"BESUncompressZ::uncompress - start decompress" << endl) ;
117 #define FIRSTBYTE (unsigned char)'\037'
118 #define SECONDBYTE (unsigned char)'\235'
120 #define BIT_MASK 0x1f
121 #define BLOCK_MODE 0x80
122 #define MAXCODE(n) (1L << (n))
127 #define HSIZE (1<<HBITS)
128 #define HMASK (HSIZE-1)
130 #define de_stack ((unsigned char *)&(htab[HSIZE-1]))
131 #define BYTEORDER 0000
134 unsigned char htab[
HSIZE*4];
135 unsigned short codetab[
HSIZE];
139 unsigned char inbuf[BUFSIZ+64];
140 unsigned char outbuf[BUFSIZ+2048];
141 unsigned char *stackp;
159 BESDEBUG(
"bes",
"BESUncompressZ::uncompress - read file" << endl); ;
163 while( insize < 3 && (rsize = read(srcFile, inbuf+insize, BUFSIZ)) > 0) {
166 BESDEBUG(
"bes",
"BESUncompressZ::uncompress - insize: " << insize << endl); ;
172 BESDEBUG(
"bes",
"BESUncompressZ::uncompress - not a compress file" << endl); ;
174 string err =
"Could not read file ";
178 remove( target.c_str() ) ;
183 string err = src.c_str();
184 err +=
": not in compressed format";
187 remove( target.c_str() ) ;
191 string err =
"unknown error";
194 remove( target.c_str() ) ;
206 if( maxbits >
BITS ) {
207 string err = src.c_str();
208 err +=
": compressed with " ;
210 err +=
" bits, can only handle";
214 remove( target.c_str() ) ;
219 bitmask = (1<<n_bits)-1;
225 free_ent = ((block_mode) ?
FIRST : 256);
227 BESDEBUG(
"bes",
"BESUncompressZ::uncompress - entering loop" << endl) ;
229 memset(codetab, 0, 256);
231 for (code = 255 ; code >= 0 ; --code){
232 ((
unsigned char *)(htab))[code] = (
unsigned char) code;
243 e = insize - ( o = ( posbits >> 3 ) );
245 for (i = 0 ; i < e ; ++i)
246 inbuf[i] = inbuf[i+o];
252 if( insize <
sizeof( inbuf ) - BUFSIZ ) {
253 if( ( rsize = read( srcFile, inbuf + insize, BUFSIZ )) < 0) {
254 string err =
"Could not read file ";
258 remove( target.c_str() ) ;
265 inbits = ( ( rsize > 0 ) ? ( insize - insize % n_bits ) << 3 :
266 ( insize << 3 ) - ( n_bits - 1 ));
268 while( inbits > posbits ){
269 if( free_ent > maxcode ) {
270 posbits = ( ( posbits-1 ) +
272 ( posbits-1 + ( n_bits << 3)) %
277 if( n_bits == maxbits)
278 maxcode = maxmaxcode;
282 bitmask = (1<<n_bits)-1;
286 unsigned char*p = &inbuf[posbits>>3];
288 code = ( ( ( (long) ( p[0] ) ) | ( (
long )( p[1] ) << 8 ) |
289 ( (long) ( p[2] ) << 16 ) ) >> ( posbits & 0x7 ) ) &
297 string err =
"oldcode:-1 code: ";
299 err +=
" !!!! uncompress: corrupt input!!!";
302 remove( target.c_str() ) ;
305 outbuf[outpos++] = (
unsigned char)(finchar =
306 (
int)(oldcode = code));
311 if( code ==
CLEAR && block_mode) {
312 memset(codetab, 0, 256);
313 free_ent =
FIRST - 1;
314 posbits = ( ( posbits - 1 ) +
316 ( posbits - 1 + ( n_bits << 3 ) ) %
319 bitmask = ( 1 << n_bits )-1;
327 if( code >= free_ent ) {
328 if( code > free_ent ) {
331 p = &inbuf[posbits>>3];
333 string err =
"uncompress: corrupt input";
336 remove( target.c_str() ) ;
340 *--stackp = (
unsigned char )finchar;
345 while( (
unsigned long)code >= (
unsigned long)256) {
346 *--stackp = htab[code];
347 code = codetab[code];
350 *--stackp = (
unsigned char)(finchar = htab[code]);
355 if( outpos+(i = (
de_stack-stackp)) >= BUFSIZ) {
358 if( i > BUFSIZ-outpos) {
363 memcpy(outbuf+outpos, stackp, i);
367 if( outpos >= BUFSIZ) {
368 if( write(destFile, outbuf,outpos) != outpos) {
369 string err =
"uncompress: write eror";
372 remove( target.c_str() ) ;
381 while( (i = (
de_stack-stackp)) > 0) ;
384 memcpy(outbuf+outpos, stackp, i);
389 if( (code = free_ent) < maxmaxcode) {
390 codetab[code] = (
unsigned short)oldcode;
391 htab[code] = (
unsigned char)finchar;
401 if( outpos > 0 && write(destFile, outbuf, outpos) != outpos) {
402 string err =
"uncompress: write eror";
405 remove( target.c_str() ) ;
412 BESDEBUG(
"bes",
"BESUncompressZ::uncompress - end decompres" << endl) ;
static void uncompress(const string &src, const string &target)
uncompress a file with the .gz file extension
exception thrown if inernal error encountered
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream