< prev index next >

src/java.base/share/native/libzip/zlib/infback.c

Print this page

 22  * questions.
 23  */
 24 
 25 /* infback.c -- inflate using a call-back interface
 26  * Copyright (C) 1995-2022 Mark Adler
 27  * For conditions of distribution and use, see copyright notice in zlib.h
 28  */
 29 
 30 /*
 31    This code is largely copied from inflate.c.  Normally either infback.o or
 32    inflate.o would be linked into an application--not both.  The interface
 33    with inffast.c is retained so that optimized assembler-coded versions of
 34    inflate_fast() can be used with either inflate.c or infback.c.
 35  */
 36 
 37 #include "zutil.h"
 38 #include "inftrees.h"
 39 #include "inflate.h"
 40 #include "inffast.h"
 41 
 42 /* function prototypes */
 43 local void fixedtables OF((struct inflate_state FAR *state));
 44 
 45 /*
 46    strm provides memory allocation functions in zalloc and zfree, or
 47    Z_NULL to use the library memory allocation functions.
 48 
 49    windowBits is in the range 8..15, and window is a user-supplied
 50    window and output buffer that is 2**windowBits bytes.
 51  */
 52 int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
 53 z_streamp strm;
 54 int windowBits;
 55 unsigned char FAR *window;
 56 const char *version;
 57 int stream_size;
 58 {
 59     struct inflate_state FAR *state;
 60 
 61     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
 62         stream_size != (int)(sizeof(z_stream)))
 63         return Z_VERSION_ERROR;
 64     if (strm == Z_NULL || window == Z_NULL ||
 65         windowBits < 8 || windowBits > 15)
 66         return Z_STREAM_ERROR;
 67     strm->msg = Z_NULL;                 /* in case we return an error */
 68     if (strm->zalloc == (alloc_func)0) {
 69 #ifdef Z_SOLO
 70         return Z_STREAM_ERROR;
 71 #else
 72         strm->zalloc = zcalloc;
 73         strm->opaque = (voidpf)0;
 74 #endif
 75     }
 76     if (strm->zfree == (free_func)0)
 77 #ifdef Z_SOLO
 78         return Z_STREAM_ERROR;

 87     state->dmax = 32768U;
 88     state->wbits = (uInt)windowBits;
 89     state->wsize = 1U << windowBits;
 90     state->window = window;
 91     state->wnext = 0;
 92     state->whave = 0;
 93     state->sane = 1;
 94     return Z_OK;
 95 }
 96 
 97 /*
 98    Return state with length and distance decoding tables and index sizes set to
 99    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
100    If BUILDFIXED is defined, then instead this routine builds the tables the
101    first time it's called, and returns those tables the first time and
102    thereafter.  This reduces the size of the code by about 2K bytes, in
103    exchange for a little execution time.  However, BUILDFIXED should not be
104    used for threaded applications, since the rewriting of the tables and virgin
105    may not be thread-safe.
106  */
107 local void fixedtables(state)
108 struct inflate_state FAR *state;
109 {
110 #ifdef BUILDFIXED
111     static int virgin = 1;
112     static code *lenfix, *distfix;
113     static code fixed[544];
114 
115     /* build fixed huffman tables if first call (may not be thread safe) */
116     if (virgin) {
117         unsigned sym, bits;
118         static code *next;
119 
120         /* literal/length table */
121         sym = 0;
122         while (sym < 144) state->lens[sym++] = 8;
123         while (sym < 256) state->lens[sym++] = 9;
124         while (sym < 280) state->lens[sym++] = 7;
125         while (sym < 288) state->lens[sym++] = 8;
126         next = fixed;
127         lenfix = next;
128         bits = 9;
129         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);

255    filled the window with output, or when it completes with data in the
256    window, it calls out() to write out the data.  The application must not
257    change the provided input until in() is called again or inflateBack()
258    returns.  The application must not change the window/output buffer until
259    inflateBack() returns.
260 
261    in() and out() are called with a descriptor parameter provided in the
262    inflateBack() call.  This parameter can be a structure that provides the
263    information required to do the read or write, as well as accumulated
264    information on the input and output such as totals and check values.
265 
266    in() should return zero on failure.  out() should return non-zero on
267    failure.  If either in() or out() fails, than inflateBack() returns a
268    Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
269    was in() or out() that caused in the error.  Otherwise,  inflateBack()
270    returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
271    error, or Z_MEM_ERROR if it could not allocate memory for the state.
272    inflateBack() can also return Z_STREAM_ERROR if the input parameters
273    are not correct, i.e. strm is Z_NULL or the state was not initialized.
274  */
275 int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
276 z_streamp strm;
277 in_func in;
278 void FAR *in_desc;
279 out_func out;
280 void FAR *out_desc;
281 {
282     struct inflate_state FAR *state;
283     z_const unsigned char FAR *next;    /* next input */
284     unsigned char FAR *put;     /* next output */
285     unsigned have, left;        /* available input and output */
286     unsigned long hold;         /* bit buffer */
287     unsigned bits;              /* bits in bit buffer */
288     unsigned copy;              /* number of stored or match bytes to copy */
289     unsigned char FAR *from;    /* where to copy match bytes from */
290     code here;                  /* current decoding table entry */
291     code last;                  /* parent table entry */
292     unsigned len;               /* length to copy for repeats, bits to drop */
293     int ret;                    /* return code */
294     static const unsigned short order[19] = /* permutation of code lengths */
295         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
296 
297     /* Check that the strm exists and that the state was initialized */
298     if (strm == Z_NULL || strm->state == Z_NULL)
299         return Z_STREAM_ERROR;
300     state = (struct inflate_state FAR *)strm->state;
301 

639             goto inf_leave;
640 
641         default:
642             /* can't happen, but makes compilers happy */
643             ret = Z_STREAM_ERROR;
644             goto inf_leave;
645         }
646 
647     /* Write leftover output and return unused input */
648   inf_leave:
649     if (left < state->wsize) {
650         if (out(out_desc, state->window, state->wsize - left) &&
651             ret == Z_STREAM_END)
652             ret = Z_BUF_ERROR;
653     }
654     strm->next_in = next;
655     strm->avail_in = have;
656     return ret;
657 }
658 
659 int ZEXPORT inflateBackEnd(strm)
660 z_streamp strm;
661 {
662     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
663         return Z_STREAM_ERROR;
664     ZFREE(strm, strm->state);
665     strm->state = Z_NULL;
666     Tracev((stderr, "inflate: end\n"));
667     return Z_OK;
668 }

 22  * questions.
 23  */
 24 
 25 /* infback.c -- inflate using a call-back interface
 26  * Copyright (C) 1995-2022 Mark Adler
 27  * For conditions of distribution and use, see copyright notice in zlib.h
 28  */
 29 
 30 /*
 31    This code is largely copied from inflate.c.  Normally either infback.o or
 32    inflate.o would be linked into an application--not both.  The interface
 33    with inffast.c is retained so that optimized assembler-coded versions of
 34    inflate_fast() can be used with either inflate.c or infback.c.
 35  */
 36 
 37 #include "zutil.h"
 38 #include "inftrees.h"
 39 #include "inflate.h"
 40 #include "inffast.h"
 41 



 42 /*
 43    strm provides memory allocation functions in zalloc and zfree, or
 44    Z_NULL to use the library memory allocation functions.
 45 
 46    windowBits is in the range 8..15, and window is a user-supplied
 47    window and output buffer that is 2**windowBits bytes.
 48  */
 49 int ZEXPORT inflateBackInit_(z_streamp strm, int windowBits,
 50                              unsigned char FAR *window, const char *version,
 51                              int stream_size) {




 52     struct inflate_state FAR *state;
 53 
 54     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
 55         stream_size != (int)(sizeof(z_stream)))
 56         return Z_VERSION_ERROR;
 57     if (strm == Z_NULL || window == Z_NULL ||
 58         windowBits < 8 || windowBits > 15)
 59         return Z_STREAM_ERROR;
 60     strm->msg = Z_NULL;                 /* in case we return an error */
 61     if (strm->zalloc == (alloc_func)0) {
 62 #ifdef Z_SOLO
 63         return Z_STREAM_ERROR;
 64 #else
 65         strm->zalloc = zcalloc;
 66         strm->opaque = (voidpf)0;
 67 #endif
 68     }
 69     if (strm->zfree == (free_func)0)
 70 #ifdef Z_SOLO
 71         return Z_STREAM_ERROR;

 80     state->dmax = 32768U;
 81     state->wbits = (uInt)windowBits;
 82     state->wsize = 1U << windowBits;
 83     state->window = window;
 84     state->wnext = 0;
 85     state->whave = 0;
 86     state->sane = 1;
 87     return Z_OK;
 88 }
 89 
 90 /*
 91    Return state with length and distance decoding tables and index sizes set to
 92    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
 93    If BUILDFIXED is defined, then instead this routine builds the tables the
 94    first time it's called, and returns those tables the first time and
 95    thereafter.  This reduces the size of the code by about 2K bytes, in
 96    exchange for a little execution time.  However, BUILDFIXED should not be
 97    used for threaded applications, since the rewriting of the tables and virgin
 98    may not be thread-safe.
 99  */
100 local void fixedtables(struct inflate_state FAR *state) {


101 #ifdef BUILDFIXED
102     static int virgin = 1;
103     static code *lenfix, *distfix;
104     static code fixed[544];
105 
106     /* build fixed huffman tables if first call (may not be thread safe) */
107     if (virgin) {
108         unsigned sym, bits;
109         static code *next;
110 
111         /* literal/length table */
112         sym = 0;
113         while (sym < 144) state->lens[sym++] = 8;
114         while (sym < 256) state->lens[sym++] = 9;
115         while (sym < 280) state->lens[sym++] = 7;
116         while (sym < 288) state->lens[sym++] = 8;
117         next = fixed;
118         lenfix = next;
119         bits = 9;
120         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);

246    filled the window with output, or when it completes with data in the
247    window, it calls out() to write out the data.  The application must not
248    change the provided input until in() is called again or inflateBack()
249    returns.  The application must not change the window/output buffer until
250    inflateBack() returns.
251 
252    in() and out() are called with a descriptor parameter provided in the
253    inflateBack() call.  This parameter can be a structure that provides the
254    information required to do the read or write, as well as accumulated
255    information on the input and output such as totals and check values.
256 
257    in() should return zero on failure.  out() should return non-zero on
258    failure.  If either in() or out() fails, than inflateBack() returns a
259    Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
260    was in() or out() that caused in the error.  Otherwise,  inflateBack()
261    returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
262    error, or Z_MEM_ERROR if it could not allocate memory for the state.
263    inflateBack() can also return Z_STREAM_ERROR if the input parameters
264    are not correct, i.e. strm is Z_NULL or the state was not initialized.
265  */
266 int ZEXPORT inflateBack(z_streamp strm, in_func in, void FAR *in_desc,
267                         out_func out, void FAR *out_desc) {





268     struct inflate_state FAR *state;
269     z_const unsigned char FAR *next;    /* next input */
270     unsigned char FAR *put;     /* next output */
271     unsigned have, left;        /* available input and output */
272     unsigned long hold;         /* bit buffer */
273     unsigned bits;              /* bits in bit buffer */
274     unsigned copy;              /* number of stored or match bytes to copy */
275     unsigned char FAR *from;    /* where to copy match bytes from */
276     code here;                  /* current decoding table entry */
277     code last;                  /* parent table entry */
278     unsigned len;               /* length to copy for repeats, bits to drop */
279     int ret;                    /* return code */
280     static const unsigned short order[19] = /* permutation of code lengths */
281         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
282 
283     /* Check that the strm exists and that the state was initialized */
284     if (strm == Z_NULL || strm->state == Z_NULL)
285         return Z_STREAM_ERROR;
286     state = (struct inflate_state FAR *)strm->state;
287 

625             goto inf_leave;
626 
627         default:
628             /* can't happen, but makes compilers happy */
629             ret = Z_STREAM_ERROR;
630             goto inf_leave;
631         }
632 
633     /* Write leftover output and return unused input */
634   inf_leave:
635     if (left < state->wsize) {
636         if (out(out_desc, state->window, state->wsize - left) &&
637             ret == Z_STREAM_END)
638             ret = Z_BUF_ERROR;
639     }
640     strm->next_in = next;
641     strm->avail_in = have;
642     return ret;
643 }
644 
645 int ZEXPORT inflateBackEnd(z_streamp strm) {


646     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
647         return Z_STREAM_ERROR;
648     ZFREE(strm, strm->state);
649     strm->state = Z_NULL;
650     Tracev((stderr, "inflate: end\n"));
651     return Z_OK;
652 }
< prev index next >