1 #include "../../burp.h"
2 #include "../../asfd.h"
3 #include "../../async.h"
4 #include "../../fzp.h"
5 #include "../../log.h"
6 #include "zlibio.h"
7 
zlib_inflate(struct asfd * asfd,const char * source_path,const char * dest_path,struct cntr * cntr)8 int zlib_inflate(struct asfd *asfd, const char *source_path,
9 	const char *dest_path, struct cntr *cntr)
10 {
11 	int ret=-1;
12 	size_t b=0;
13 	uint8_t in[ZCHUNK];
14 	struct fzp *src=NULL;
15 	struct fzp *dst=NULL;
16 
17 	if(!(src=fzp_gzopen(source_path, "rb")))
18 	{
19 		logw(asfd, cntr, "could not gzopen %s in %s: %s\n",
20 			source_path, __func__, strerror(errno));
21 		goto end;
22 	}
23 	if(!(dst=fzp_open(dest_path, "wb")))
24 	{
25 		logw(asfd, cntr, "could not open %s in %s: %s\n",
26 			dest_path, __func__, strerror(errno));
27 		goto end;
28 	}
29 
30 	while((b=fzp_read(src, in, ZCHUNK))>0)
31 	{
32 		if(fzp_write(dst, in, b)!=b)
33 		{
34 			logw(asfd, cntr,
35 				"error when writing to %s\n", dest_path);
36 			goto end;
37 		}
38 	}
39 	if(!fzp_eof(src))
40 	{
41 		logw(asfd, cntr,
42 			"error while reading %s in %s\n",
43 				source_path, __func__);
44 		goto end;
45 	}
46 	if(fzp_close(&dst))
47 	{
48 		logw(asfd, cntr,
49 			"error when closing %s in %s: %s\n",
50 				dest_path, __func__, strerror(errno));
51 		goto end;
52 	}
53 	ret=0;
54 end:
55 	fzp_close(&src);
56 	fzp_close(&dst);
57 	return ret;
58 }
59