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