Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* inflate.c -- zlib interface to inflate modules |
| 2 | * Copyright (C) 1995-1998 Mark Adler |
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ |
| 5 | |
| 6 | #include <linux/zutil.h> |
| 7 | #include "infblock.h" |
| 8 | #include "infutil.h" |
| 9 | |
| 10 | int zlib_inflateSync( |
| 11 | z_streamp z |
| 12 | ) |
| 13 | { |
| 14 | uInt n; /* number of bytes to look at */ |
| 15 | Byte *p; /* pointer to bytes */ |
| 16 | uInt m; /* number of marker bytes found in a row */ |
| 17 | uLong r, w; /* temporaries to save total_in and total_out */ |
| 18 | |
| 19 | /* set up */ |
| 20 | if (z == NULL || z->state == NULL) |
| 21 | return Z_STREAM_ERROR; |
| 22 | if (z->state->mode != I_BAD) |
| 23 | { |
| 24 | z->state->mode = I_BAD; |
| 25 | z->state->sub.marker = 0; |
| 26 | } |
| 27 | if ((n = z->avail_in) == 0) |
| 28 | return Z_BUF_ERROR; |
| 29 | p = z->next_in; |
| 30 | m = z->state->sub.marker; |
| 31 | |
| 32 | /* search */ |
| 33 | while (n && m < 4) |
| 34 | { |
| 35 | static const Byte mark[4] = {0, 0, 0xff, 0xff}; |
| 36 | if (*p == mark[m]) |
| 37 | m++; |
| 38 | else if (*p) |
| 39 | m = 0; |
| 40 | else |
| 41 | m = 4 - m; |
| 42 | p++, n--; |
| 43 | } |
| 44 | |
| 45 | /* restore */ |
| 46 | z->total_in += p - z->next_in; |
| 47 | z->next_in = p; |
| 48 | z->avail_in = n; |
| 49 | z->state->sub.marker = m; |
| 50 | |
| 51 | /* return no joy or set up to restart on a new block */ |
| 52 | if (m != 4) |
| 53 | return Z_DATA_ERROR; |
| 54 | r = z->total_in; w = z->total_out; |
| 55 | zlib_inflateReset(z); |
| 56 | z->total_in = r; z->total_out = w; |
| 57 | z->state->mode = BLOCKS; |
| 58 | return Z_OK; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /* Returns true if inflate is currently at the end of a block generated |
| 63 | * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP |
| 64 | * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH |
| 65 | * but removes the length bytes of the resulting empty stored block. When |
| 66 | * decompressing, PPP checks that at the end of input packet, inflate is |
| 67 | * waiting for these length bytes. |
| 68 | */ |
| 69 | int zlib_inflateSyncPoint( |
| 70 | z_streamp z |
| 71 | ) |
| 72 | { |
| 73 | if (z == NULL || z->state == NULL || z->state->blocks == NULL) |
| 74 | return Z_STREAM_ERROR; |
| 75 | return zlib_inflate_blocks_sync_point(z->state->blocks); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * This subroutine adds the data at next_in/avail_in to the output history |
| 80 | * without performing any output. The output buffer must be "caught up"; |
| 81 | * i.e. no pending output (hence s->read equals s->write), and the state must |
| 82 | * be BLOCKS (i.e. we should be willing to see the start of a series of |
| 83 | * BLOCKS). On exit, the output will also be caught up, and the checksum |
| 84 | * will have been updated if need be. |
| 85 | */ |
| 86 | static int zlib_inflate_addhistory(inflate_blocks_statef *s, |
| 87 | z_stream *z) |
| 88 | { |
| 89 | uLong b; /* bit buffer */ /* NOT USED HERE */ |
| 90 | uInt k; /* bits in bit buffer */ /* NOT USED HERE */ |
| 91 | uInt t; /* temporary storage */ |
| 92 | Byte *p; /* input data pointer */ |
| 93 | uInt n; /* bytes available there */ |
| 94 | Byte *q; /* output window write pointer */ |
| 95 | uInt m; /* bytes to end of window or read pointer */ |
| 96 | |
| 97 | if (s->read != s->write) |
| 98 | return Z_STREAM_ERROR; |
| 99 | if (s->mode != TYPE) |
| 100 | return Z_DATA_ERROR; |
| 101 | |
| 102 | /* we're ready to rock */ |
| 103 | LOAD |
| 104 | /* while there is input ready, copy to output buffer, moving |
| 105 | * pointers as needed. |
| 106 | */ |
| 107 | while (n) { |
| 108 | t = n; /* how many to do */ |
| 109 | /* is there room until end of buffer? */ |
| 110 | if (t > m) t = m; |
| 111 | /* update check information */ |
| 112 | if (s->checkfn != NULL) |
| 113 | s->check = (*s->checkfn)(s->check, q, t); |
| 114 | memcpy(q, p, t); |
| 115 | q += t; |
| 116 | p += t; |
| 117 | n -= t; |
| 118 | z->total_out += t; |
| 119 | s->read = q; /* drag read pointer forward */ |
| 120 | /* WWRAP */ /* expand WWRAP macro by hand to handle s->read */ |
| 121 | if (q == s->end) { |
| 122 | s->read = q = s->window; |
| 123 | m = WAVAIL; |
| 124 | } |
| 125 | } |
| 126 | UPDATE |
| 127 | return Z_OK; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* |
| 132 | * This subroutine adds the data at next_in/avail_in to the output history |
| 133 | * without performing any output. The output buffer must be "caught up"; |
| 134 | * i.e. no pending output (hence s->read equals s->write), and the state must |
| 135 | * be BLOCKS (i.e. we should be willing to see the start of a series of |
| 136 | * BLOCKS). On exit, the output will also be caught up, and the checksum |
| 137 | * will have been updated if need be. |
| 138 | */ |
| 139 | |
| 140 | int zlib_inflateIncomp( |
| 141 | z_stream *z |
| 142 | |
| 143 | ) |
| 144 | { |
| 145 | if (z->state->mode != BLOCKS) |
| 146 | return Z_DATA_ERROR; |
| 147 | return zlib_inflate_addhistory(z->state->blocks, z); |
| 148 | } |