summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/luapplib/src/util/utilflate.c
blob: 27e44d409a1540cf61640d5bb3d48cc64a6598d8 (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

#include <zlib.h>

#include "utilmem.h"
#include "utillog.h"
#include "utilflate.h"

/* flate codec */

/*
Flate codec example provided at http://www.zlib.net/zpipe.c (http://www.zlib.net/zlib_how.html) uses the following scheme:
- provide input data buffer
- keep providing output until codec function uses it

For encoder:

  z->zalloc = z->zfree = z->zopaque = NULL;
  deflateInit(z, compression_level);
  do {
    z->next_in = <input buffer>
    z->avail_in = <input buffer bytes>
    do {
      z->next_out = <output buffer>
      z->avail_out = <output buffer bytes>
      deflate(z, flush);
      // write obtained output from deflate
    } while (z->avail_out == 0);
    assert(z->avail_in == 0);
  } while (flush != Z_FINISH);
  deflateEnd(z);

'z' is an internal codec state of type z_stream, 'flush' is either Z_NO_FLUSH or Z_FINISH at the end of data.
deflate() ensures to consume the entire input if there are no obstackles to write an output. The inner loop
provides an output space as long as it is used by deflate(). When deflate() wrote everything it could,
it leaves z->avail_out > 0, which breaks the inner loop. At this point z->avail_in should also be zero.
The example documentation claims that the return codes from deflate() doesn't really need to be checked,
as checking z->avail_out for zero is enough.

The scheme for decoder is pretty similar, but with substantial differences:
- the end of stream is automatically found by decoder, so using Z_FINISH flag to indicate an end of stream
  is not necessary, but if provided, it MUST be given only if the EOF marker actually occurs in the input chunk,
  and subsequent calls to inflate() must consequently use Z_FINISH
- calling inflate() as long as it uses the output buffer provided still works for decoder, but inflate()
  does not ensure to consume the entire input, as it will read until end of stream marker
- the return code from inflate() must be checked to ensure the proper reaction on invalid data stream and
  end of stream signals
- initialization must set an input buffer to NULL or to some existing chunk (the later helps zlib to perform
  better on inflate(), but inflate() does the research on the first call anyway)

  z->zalloc = z->zfree = z->zopaque = NULL;
  z->next_in = NULL, z->avail_in = 0;
  inflateInit(z);
  do {
    z->next_in = <input buffer>
    z->avail_in = <input buffer bytes>
    do {
      z->next_out = <output buffer>
      z->avail_out = <output buffer bytes>
      status = inflate(z, flush);
      // check return status
      // write obtained output from inflate
    } while (z->avail_out == 0);
  } while (status != Z_STREAM_END);
  inflateEnd(z);

Our wrapper generally follows "prepare input, keep pomping output" scheme, but we need to support handler function
breaks on IOFEMPTY and IOFFULL. For a consistent come back from those on subsequent calls to the handler function,
we use 3 states:
- FLATE_IN - get input, when got something then goto FALTE_OUT
- FLATE_OUT - set z_stream buffers and keep writing output until enything to write, then goto FLATE_IN or FLATE_DONE
- FLATE_DONE - we are done, no return from that state
Distinction of FLATE_IN and FLATE_OUT states guarantees that we will not get more input until zlib consumes the stuff
from the previous feed, possibly interrupted by IOFFULL return on filling the output buffer. This distinction is not
critical, but makes the filter running according to the scheme described above. Note that we set zlib input buffer
(z->next_in, z->avail_in) at the beginning of FLATE_OUT state. Also note that we always update our buffers according
to updated avail_in / avail_out values, just after a call to inflate() / deflate(). So no matter what have happens
between handler calls, zlib input buffer is in sync with ours.
*/

struct flate_state {
  z_stream z;
  int flush;
  int status;
  int level; /* encoder compression level -1..9 */
};

typedef union { flate_state *flatestate; void *voidstate; } flate_state_pointer; // to avoid 'dereferencing type-puned ...' warnings

enum {
  FLATE_IN,
  FLATE_OUT,
  FLATE_DONE
};

flate_state * flate_decoder_init (flate_state *state)
{ /* initialize zlib */
  z_stream *z = &state->z;
  z->zalloc = Z_NULL;
  z->zfree = Z_NULL;
  z->opaque = Z_NULL;
  z->avail_in = 0;     /* must be initialized before inflateInit() */
  z->next_in = Z_NULL; /* ditto */
  if (inflateInit(z) != Z_OK)
    return NULL;
  state->status = FLATE_IN;
  return state;
}

flate_state * flate_encoder_init (flate_state *state)
{
  z_stream *z = &state->z;
  z->zalloc = Z_NULL;
  z->zfree = Z_NULL;
  z->opaque = Z_NULL;
  z->avail_in = 0;
  z->next_in = Z_NULL;
  state->level = Z_DEFAULT_COMPRESSION; // will probably be moved upward
  if (deflateInit(z, state->level) != Z_OK)
    return NULL;
  state->status = FLATE_IN;
  return state;
}

static const char * zmess (int zstatus)
{
  switch (zstatus)
  {
    case Z_OK:            return "ok";
    case Z_STREAM_END:    return "end of stream";
    case Z_BUF_ERROR:     return "buffer error";
    case Z_STREAM_ERROR:  return "stream error";
    case Z_NEED_DICT:     return "need dict";
    case Z_DATA_ERROR:    return "data error";
    case Z_MEM_ERROR:     return "memory error";
    case Z_VERSION_ERROR: return "version error";
    case Z_ERRNO:         return "io error";
    default:
      break;
  }
  return "unknown error";
}

iof_status flate_decode_state (iof *I, iof *O, flate_state *state)
{
  z_stream *z;
  int zstatus = Z_OK;
  z = &state->z;
  while (state->status != FLATE_DONE)
  {
    if (state->status == FLATE_IN)
    {
      if (!iof_readable(I))
        return state->flush ? IOFERR : IOFEMPTY;
      state->status = FLATE_OUT;
    }
    z->next_in = (Bytef *)I->pos;
    z->avail_in = (uInt)iof_left(I);
    do {
      if (!iof_writable(O))
        return IOFFULL;
      z->next_out = (Bytef *)O->pos;
      z->avail_out = (uInt)iof_left(O);
      zstatus = inflate(z, Z_NO_FLUSH);
      I->pos += iof_left(I) - z->avail_in;
      O->pos += iof_left(O) - z->avail_out;
      switch (zstatus)
      {
        case Z_OK:
        case Z_STREAM_END:
          break;
        default:
          loggerf("flate decoder %s (%d)", zmess(zstatus), zstatus);
          return IOFERR;
      }
    } while (z->avail_out == 0);
    state->status = zstatus == Z_STREAM_END ? FLATE_DONE : FLATE_IN;
  }
  return IOFEOF;
}

iof_status flate_encode_state (iof *I, iof *O, flate_state *state)
{
  z_stream *z;
  int zstatus;
  z = &state->z;
  while (state->status != FLATE_DONE)
  {
    if (state->status == FLATE_IN)
    {
      if (!iof_readable(I))
        if (!state->flush)
          return IOFEMPTY;
      state->status = FLATE_OUT;
    }
    z->next_in = (Bytef *)I->pos;
    z->avail_in = (uInt)iof_left(I);
    do {
      if (!iof_writable(O))
        return IOFFULL;
      z->next_out = (Bytef *)O->pos;
      z->avail_out = (uInt)iof_left(O);
      zstatus = deflate(z, state->flush ? Z_FINISH : Z_NO_FLUSH);
      I->pos += iof_left(I) - z->avail_in;
      O->pos += iof_left(O) - z->avail_out;
      switch (zstatus)
      {
        case Z_OK:
        case Z_STREAM_END:
          break;
        default:
          loggerf("flate encoder %s (%d)", zmess(zstatus), zstatus);
          return IOFERR;
      }
    } while (z->avail_out == 0);
    state->status = state->flush ? FLATE_DONE : FLATE_IN;
  }
  return IOFEOF;
}


void flate_decoder_close (flate_state *state)
{
  inflateEnd(&state->z);
}

void flate_encoder_close (flate_state *state)
{
  deflateEnd(&state->z);
}

/* filter */

// flate decoder function

static size_t flate_decoder (iof *F, iof_mode mode)
{
  flate_state *state;
  iof_status status;
  size_t tail;

  state = iof_filter_state(flate_state *, F);
  switch(mode)
  {
    case IOFLOAD:
    case IOFREAD:
      if (F->flags & IOF_STOPPED)
        return 0;
      tail = iof_tail(F);
      F->pos = F->buf + tail;
      F->end = F->buf + F->space;
      do {
        status = flate_decode_state(F->next, F, state);
      } while (mode == IOFLOAD && status == IOFFULL && iof_resize_buffer(F));
      return iof_decoder_retval(F, "flate", status);
    case IOFCLOSE:
      flate_decoder_close(state);
      iof_free(F);
      return 0;
    default:
      break;
  }
  return 0;
}

// flate encoder function

static size_t flate_encoder (iof *F, iof_mode mode)
{
  flate_state *state;
  iof_status status;

  state = iof_filter_state(flate_state *, F);
  switch (mode)
  {
    case IOFFLUSH:
      state->flush = 1;
      FALLTHRU // fall through
    case IOFWRITE:
      F->end = F->pos;
      F->pos = F->buf;
      status = flate_encode_state(F, F->next, state);
      return iof_encoder_retval(F, "flate", status);
    case IOFCLOSE:
      if (!state->flush)
        flate_encoder(F, IOFFLUSH);
      flate_encoder_close(state);
      iof_free(F);
      return 0;
    default:
      break;
  }
  return 0;
}

iof * iof_filter_flate_decoder (iof *N)
{
  iof *I;
  flate_state_pointer P;
  I = iof_filter_reader(flate_decoder, sizeof(flate_state), &P.voidstate);
  iof_setup_next(I, N);
  if (flate_decoder_init(P.flatestate) == NULL)
  {
    iof_discard(I);
    return NULL;
  }
  P.flatestate->flush = 1;
  return I;
}

iof * iof_filter_flate_encoder (iof *N)
{
  iof *O;
  flate_state_pointer P;
  O = iof_filter_writer(flate_encoder, sizeof(flate_state), &P.voidstate);
  iof_setup_next(O, N);
  if (flate_encoder_init(P.flatestate) == NULL)
  {
    iof_discard(O);
    return NULL;
  }
  return O;
}