File: gzops.c

package info (click to toggle)
hcxtools 7.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,152 kB
  • sloc: ansic: 21,223; python: 144; makefile: 101; sh: 99
file content (100 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <zlib.h>

#include "gzops.h"

/*===========================================================================*/
bool testgzipfile(char *pcapinname)
{
int pcapr_fd;
uint32_t magicnumber;

pcapr_fd = open(pcapinname, O_RDONLY);
if(pcapr_fd == -1) return false;
magicnumber = getmagicnumber(pcapr_fd);
close(pcapr_fd);
#ifdef BIG_ENDIAN_HOST
magicnumber = byte_swap_32(magicnumber);
#endif
if((magicnumber & 0xffff) != GZIPMAGICNUMBER) return false;
if(((magicnumber >> 16) & 0xff) != DEFLATE) return false;
return true;
}
/*===========================================================================*/
bool decompressgz(char *gzname, char *tmpoutname)
{
FILE *fhin = NULL;
FILE *fhout = NULL;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];

memset(&strm, 0, sizeof (strm));
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = in;
strm.avail_in = 0;
inflateInit2(& strm, windowBits | ENABLE_ZLIB_GZIP);
printf("decompressing %s to %s\n", basename(gzname), tmpoutname);
fhin = fopen (gzname, "r");
if(fhin == NULL)
	{
	printf("failed to decompress%s\n", gzname);
	return false;
	}

fhout = fopen (tmpoutname, "w");
if(fhout == NULL)
	{
	printf("failed to decompress%s\n", tmpoutname);
	fclose(fhin);
	return false;
	}

while(1)
	{
	int bytes_read;
	int zlib_status;
	bytes_read = fread(in, sizeof(char), sizeof(in), fhin);
	if(ferror(fhin))
		{
		inflateEnd(&strm);
		printf("failed to decompress %s\n", gzname);
		fclose(fhout);
		fclose(fhin);
		return false;
		}
	strm.avail_in = bytes_read;
	strm.next_in = in;
	do
		{
		unsigned have;
		strm.avail_out = CHUNK;
		strm.next_out = out;
		zlib_status = inflate(&strm, Z_NO_FLUSH);
		switch (zlib_status)
			{
			case Z_OK:
			case Z_STREAM_END:
			case Z_BUF_ERROR:
			break;
			default:
			inflateEnd (&strm);
			printf("failed to decompress %s\n", gzname);
			return false;
			}
		have = CHUNK -strm.avail_out;
		fwrite(out, sizeof (unsigned char), have, fhout);
		}
	while(strm.avail_out == 0);
	if(feof(fhin))
		{
		inflateEnd(&strm);
		break;
		}
	}
fclose(fhout);
fclose(fhin);
return true;
}
/*===========================================================================*/