Repository navigation
Expand file tree
/
Copy pathcodec.c
More file actions
180 lines (165 loc) · 6.91 KB
/
Copy pathcodec.c
File metadata and controls
180 lines (165 loc) · 6.91 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* codec.c -- the deflate and inflate the pool runs over one slot at a time
* For conditions of distribution and use, see LICENSE.md
*/
#include <stdlib.h>
#include <string.h>
#include "codec.h"
#include "gzblock.h"
#include "util.h"
/* The output is full. A block may take any size, so double the buffer, up to GZBLOCK_MAX_BLOCK.
Returns 1 with room to inflate into. The fill is measured by pointer rather than total_out,
which member resets zero mid-slot. */
static int32_t grow_output(zng_stream *strm, slot_t *slot) {
size_t have = (size_t)(strm->next_out - slot->out);
size_t size = MIN(have * 2, (size_t)GZBLOCK_MAX_BLOCK);
uint8_t *grown;
/* Out of memory reads as a block too large to hold, which it is. */
if (size <= have || !(grown = (uint8_t *)realloc(slot->out, size)))
return 0;
slot->out = grown;
slot->out_size = size;
strm->next_out = slot->out + have;
strm->avail_out = (uint32_t)(size - have);
return 1;
}
/* Inflate one segment into the slot's output and say how it ended. Every segment ends at a marker
pair or the end of the stream and is a block at whatever size it produces, since no chance
pattern makes a pair, so its output grows on demand and any clean end of the input completes
it. */
static int32_t inflate_segment(zng_stream *strm, slot_t *slot) {
size_t left = slot->in_len;
int32_t err;
zng_inflateReset2(strm, -MAX_WBITS); /* back to raw, a member slot may have rewrapped it */
strm->next_in = (z_const uint8_t *)slot->in;
strm->avail_in = 0;
strm->next_out = slot->out;
strm->avail_out = (uint32_t)slot->out_size;
for (;;) {
if (strm->avail_in == 0 && left != 0) {
uint32_t chunk = clamp_u32(left);
strm->avail_in = chunk;
left -= chunk;
}
/* A full buffer grows before inflate needs the room. One at the cap is left to inflate,
which needs none for the empty stored blocks of a marker. */
if (strm->avail_out == 0)
grow_output(strm, slot);
/* Z_BLOCK returns at every block boundary, where data_type reports the position. */
err = zng_inflate(strm, Z_BLOCK);
if (err == Z_OK && (strm->data_type & (64 | 128)) == (64 | 128))
err = zng_inflate(strm, Z_BLOCK); /* past the final block, conclude the stream */
if (err == Z_STREAM_END)
return SEGMENT_END;
if (err == Z_BUF_ERROR && strm->avail_out == 0)
return SEGMENT_OVERFLOW;
if (err != Z_OK)
return SEGMENT_ERROR;
/* Complete once the input ends just past a deflate block on a byte boundary. Ending
anywhere else, the cut fell inside a block. */
if (strm->avail_in == 0 && left == 0)
return (strm->data_type & 128) && (strm->data_type & 7) == 0 ? SEGMENT_FULL : SEGMENT_SHORT;
}
}
const char *segment_status_name(int32_t status) {
switch (status) {
case SEGMENT_FULL:
return "complete";
case SEGMENT_END:
return "end of stream";
case SEGMENT_SHORT:
return "truncated";
case SEGMENT_OVERFLOW:
return "larger than the block size";
default:
return "corrupt";
}
}
/* Inflate whole gzip members back to back, zlib checking each header, crc, and length. Sized
members cut exactly, so the only clean end is all input consumed at a member's end. */
static int32_t inflate_members(zng_stream *strm, slot_t *slot) {
size_t left = slot->in_len;
int32_t err;
zng_inflateReset2(strm, MAX_WBITS + 16);
strm->next_in = (z_const uint8_t *)slot->in;
strm->avail_in = 0;
strm->next_out = slot->out;
strm->avail_out = (uint32_t)slot->out_size;
for (;;) {
if (strm->avail_in == 0 && left != 0) {
uint32_t chunk = clamp_u32(left);
strm->avail_in = chunk;
left -= chunk;
}
err = zng_inflate(strm, Z_NO_FLUSH);
if (err == Z_STREAM_END) {
if (strm->avail_in == 0 && left == 0)
return SEGMENT_FULL;
zng_inflateReset(strm); /* the next member follows directly */
continue;
}
if (err != Z_OK)
return SEGMENT_ERROR;
if (strm->avail_out == 0) {
if (!grow_output(strm, slot))
return SEGMENT_OVERFLOW;
continue;
}
if (strm->avail_in == 0 && left == 0)
return SEGMENT_SHORT; /* input ended inside a member */
}
}
/* Run a member slot. The totals reset with each member, so the fill is measured by pointer and
the crc is left to the per-member trailers zlib already checked. */
static void run_members(zng_stream *strm, slot_t *slot) {
slot->status = inflate_members(strm, slot);
slot->in_used = slot->in_len - strm->avail_in;
slot->out_len = (size_t)(strm->next_out - slot->out);
slot->crc = 0;
}
/* Inflate one slot and record what every exit of inflate_segment shares, the bytes consumed
and produced and the CRC of the output. */
static void run_segment(zng_stream *strm, slot_t *slot) {
slot->status = inflate_segment(strm, slot);
slot->in_used = (size_t)strm->total_in;
slot->out_len = (size_t)strm->total_out;
slot->crc = (uint32_t)zng_crc32_z(0, slot->out, slot->out_len);
}
/* Deflate one block on a fresh raw stream. A full flush ends it on a byte boundary with the empty
stored block marker, the last block ends the deflate stream instead. */
static void run_block(zng_stream *strm, slot_t *slot, size_t out_size) {
int32_t err;
zng_deflateReset(strm);
zng_deflateParams(strm, slot->level, slot->strategy);
strm->next_in = (z_const uint8_t *)slot->in;
strm->avail_in = (uint32_t)slot->in_len;
strm->next_out = slot->out;
strm->avail_out = (uint32_t)out_size;
err = zng_deflate(strm, slot->last ? Z_FINISH : Z_SYNC_FLUSH);
if (!slot->last && err == Z_OK)
err = zng_deflate(strm, Z_FULL_FLUSH); /* the second marker makes it a boundary */
slot->out_len = out_size - strm->avail_out;
slot->in_used = slot->in_len - strm->avail_in;
int32_t done = slot->last ? err == Z_STREAM_END : err == Z_OK && strm->avail_in == 0 && strm->avail_out != 0;
slot->status = done ? BLOCK_OK : BLOCK_ERROR;
slot->crc = (uint32_t)zng_crc32_z(0, slot->in, slot->in_len);
}
int32_t codec_init(pool_t *pool, zng_stream *strm) {
memset(strm, 0, sizeof(*strm));
if (pool->mode == POOL_DEFLATE)
return zng_deflateInit2(strm, pool->level, Z_DEFLATED, -MAX_WBITS, 8, pool->strategy);
return zng_inflateInit2(strm, -MAX_WBITS);
}
void codec_end(pool_t *pool, zng_stream *strm) {
if (pool->mode == POOL_DEFLATE)
zng_deflateEnd(strm);
else
zng_inflateEnd(strm);
}
void codec_run(pool_t *pool, zng_stream *strm, slot_t *slot) {
if (pool->mode == POOL_DEFLATE)
run_block(strm, slot, pool->out_size);
else if (slot->members != 0)
run_members(strm, slot);
else
run_segment(strm, slot);
}