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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/*
* Copyright (C) 2023 Patrick McDermott
*
* This file is part of opkg-opkg.
*
* opkg-opkg is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opkg-opkg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opkg-opkg. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <zlib.h>
#include "defs.h"
#include "gzip.h"
#include "ustar.h"
#define OPKG_OPK_GZIP_WINDOW_BITS_ (15 + 16)
struct opkg_opk_gzip {
int (*read)(void *, char **, size_t *);
void *user_data;
z_stream stream;
};
struct opkg_opk_gzip *
opkg_opk_gzip_init(opkg_opk_gzip_read_func *read, void *user_data)
{
struct opkg_opk_gzip *gzip;
gzip = malloc(sizeof(*gzip));
if (gzip == NULL) {
return NULL;
}
gzip->read = read;
gzip->user_data = user_data;
gzip->stream.next_in = Z_NULL;
gzip->stream.avail_in = 0;
gzip->stream.zalloc = Z_NULL;
gzip->stream.zfree = Z_NULL;
gzip->stream.opaque = Z_NULL;
if (inflateInit2(&gzip->stream, OPKG_OPK_GZIP_WINDOW_BITS_) != Z_OK) {
free(gzip);
return NULL;
}
return gzip;
}
int
opkg_opk_gzip_read(struct opkg_opk_gzip *gzip, void *record)
{
int end;
gzip->stream.next_out = record;
gzip->stream.avail_out = OPKG_OPK_USTAR_RECORD_SIZE;
for (;;) {
end = 0;
if (gzip->stream.avail_in == 0) {
/* Input buffer is empty and needs refilled. */
switch (gzip->read(gzip->user_data,
&gzip->stream.next_in,
&gzip->stream.avail_in)) {
case OPKG_OPK_OK:
break;
case OPKG_OPK_END:
end = 1;
break;
case OPKG_OPK_ERROR:
default:
return OPKG_OPK_ERROR;
}
}
switch (inflate(&gzip->stream, Z_SYNC_FLUSH)) {
case Z_OK:
break;
case Z_BUF_ERROR:
if (end == 1) {
return OPKG_OPK_ERROR;
}
break;
case Z_STREAM_END:
if (gzip->stream.avail_out != 0) {
/* Premature end */
return OPKG_OPK_ERROR;
}
return OPKG_OPK_END;
default:
return OPKG_OPK_ERROR;
}
if (gzip->stream.avail_out == 0) {
/* Output buffer is filled and ready for use. */
return OPKG_OPK_OK;
}
}
}
void
opkg_opk_gzip_free(struct opkg_opk_gzip *gzip)
{
inflateEnd(&gzip->stream);
free(gzip);
}
|