~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/lib/zlib/infcodes.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Xorg: infcodes.c,v 1.3 2000/08/17 19:46:47 cpqbld Exp $ */
 
2
 
 
3
/* infcodes.c -- process literals and length/distance pairs
 
4
 * Copyright (C) 1995-1998 Mark Adler
 
5
 * For conditions of distribution and use, see copyright notice in zlib.h 
 
6
 */
 
7
 
 
8
#include "zutil.h"
 
9
#include "inftrees.h"
 
10
#include "infblock.h"
 
11
#include "infcodes.h"
 
12
#include "infutil.h"
 
13
#include "inffast.h"
 
14
 
 
15
/* simplify the use of the inflate_huft type with some defines */
 
16
#define base more.Base
 
17
#define next more.Next
 
18
#define exop word.what.Exop
 
19
#define bits word.what.Bits
 
20
 
 
21
typedef enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
 
22
      START,    /* x: set up for LEN */
 
23
      LEN,      /* i: get length/literal/eob next */
 
24
      LENEXT,   /* i: getting length extra (have base) */
 
25
      DIST,     /* i: get distance next */
 
26
      DISTEXT,  /* i: getting distance extra */
 
27
      COPY,     /* o: copying bytes in window, waiting for space */
 
28
      LIT,      /* o: got literal, waiting for output space */
 
29
      WASH,     /* o: got eob, possibly still output waiting */
 
30
      END,      /* x: got eob and all data flushed */
 
31
      BADCODE}  /* x: got error */
 
32
inflate_codes_mode;
 
33
 
 
34
/* inflate codes private state */
 
35
struct inflate_codes_state {
 
36
 
 
37
  /* mode */
 
38
  inflate_codes_mode mode;      /* current inflate_codes mode */
 
39
 
 
40
  /* mode dependent information */
 
41
  uInt len;
 
42
  union {
 
43
    struct {
 
44
      inflate_huft *tree;       /* pointer into tree */
 
45
      uInt need;                /* bits needed */
 
46
    } code;             /* if LEN or DIST, where in tree */
 
47
    uInt lit;           /* if LIT, literal */
 
48
    struct {
 
49
      uInt get;                 /* bits to get for extra */
 
50
      uInt dist;                /* distance back to copy from */
 
51
    } copy;             /* if EXT or COPY, where and how much */
 
52
  } sub;                /* submode */
 
53
 
 
54
  /* mode independent information */
 
55
  Byte lbits;           /* ltree bits decoded per branch */
 
56
  Byte dbits;           /* dtree bits decoder per branch */
 
57
  inflate_huft *ltree;          /* literal/length/eob tree */
 
58
  inflate_huft *dtree;          /* distance tree */
 
59
 
 
60
};
 
61
 
 
62
 
 
63
inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
 
64
uInt bl, bd;
 
65
inflate_huft *tl;
 
66
inflate_huft *td; /* need separate declaration for Borland C++ */
 
67
z_streamp z;
 
68
{
 
69
  inflate_codes_statef *c;
 
70
 
 
71
  if ((c = (inflate_codes_statef *)
 
72
       ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
 
73
  {
 
74
    c->mode = START;
 
75
    c->lbits = (Byte)bl;
 
76
    c->dbits = (Byte)bd;
 
77
    c->ltree = tl;
 
78
    c->dtree = td;
 
79
    Tracev((stderr, "inflate:       codes new\n"));
 
80
  }
 
81
  return c;
 
82
}
 
83
 
 
84
 
 
85
int inflate_codes(s, z, r)
 
86
inflate_blocks_statef *s;
 
87
z_streamp z;
 
88
int r;
 
89
{
 
90
  uInt j;               /* temporary storage */
 
91
  inflate_huft *t;      /* temporary pointer */
 
92
  uInt e;               /* extra bits or operation */
 
93
  uLong b;              /* bit buffer */
 
94
  uInt k;               /* bits in bit buffer */
 
95
  Bytef *p;             /* input data pointer */
 
96
  uInt n;               /* bytes available there */
 
97
  Bytef *q;             /* output window write pointer */
 
98
  uInt m;               /* bytes to end of window or read pointer */
 
99
  Bytef *f;             /* pointer to copy strings from */
 
100
  inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
 
101
 
 
102
  /* copy input/output information to locals (UPDATE macro restores) */
 
103
  LOAD
 
104
 
 
105
  /* process input and output based on current state */
 
106
  while (1) switch (c->mode)
 
107
  {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
 
108
    case START:         /* x: set up for LEN */
 
109
#ifndef SLOW
 
110
      if (m >= 258 && n >= 10)
 
111
      {
 
112
        UPDATE
 
113
        r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
 
114
        LOAD
 
115
        if (r != Z_OK)
 
116
        {
 
117
          c->mode = r == Z_STREAM_END ? WASH : BADCODE;
 
118
          break;
 
119
        }
 
120
      }
 
121
#endif /* !SLOW */
 
122
      c->sub.code.need = c->lbits;
 
123
      c->sub.code.tree = c->ltree;
 
124
      c->mode = LEN;
 
125
    case LEN:           /* i: get length/literal/eob next */
 
126
      j = c->sub.code.need;
 
127
      NEEDBITS(j)
 
128
      t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
 
129
      DUMPBITS(t->bits)
 
130
      e = (uInt)(t->exop);
 
131
      if (e == 0)               /* literal */
 
132
      {
 
133
        c->sub.lit = t->base;
 
134
        Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
 
135
                 "inflate:         literal '%c'\n" :
 
136
                 "inflate:         literal 0x%02x\n", t->base));
 
137
        c->mode = LIT;
 
138
        break;
 
139
      }
 
140
      if (e & 16)               /* length */
 
141
      {
 
142
        c->sub.copy.get = e & 15;
 
143
        c->len = t->base;
 
144
        c->mode = LENEXT;
 
145
        break;
 
146
      }
 
147
      if ((e & 64) == 0)        /* next table */
 
148
      {
 
149
        c->sub.code.need = e;
 
150
        c->sub.code.tree = t->next;
 
151
        break;
 
152
      }
 
153
      if (e & 32)               /* end of block */
 
154
      {
 
155
        Tracevv((stderr, "inflate:         end of block\n"));
 
156
        c->mode = WASH;
 
157
        break;
 
158
      }
 
159
      c->mode = BADCODE;        /* invalid code */
 
160
      z->msg = (char*)"invalid literal/length code";
 
161
      r = Z_DATA_ERROR;
 
162
      LEAVE
 
163
    case LENEXT:        /* i: getting length extra (have base) */
 
164
      j = c->sub.copy.get;
 
165
      NEEDBITS(j)
 
166
      c->len += (uInt)b & inflate_mask[j];
 
167
      DUMPBITS(j)
 
168
      c->sub.code.need = c->dbits;
 
169
      c->sub.code.tree = c->dtree;
 
170
      Tracevv((stderr, "inflate:         length %u\n", c->len));
 
171
      c->mode = DIST;
 
172
    case DIST:          /* i: get distance next */
 
173
      j = c->sub.code.need;
 
174
      NEEDBITS(j)
 
175
      t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
 
176
      DUMPBITS(t->bits)
 
177
      e = (uInt)(t->exop);
 
178
      if (e & 16)               /* distance */
 
179
      {
 
180
        c->sub.copy.get = e & 15;
 
181
        c->sub.copy.dist = t->base;
 
182
        c->mode = DISTEXT;
 
183
        break;
 
184
      }
 
185
      if ((e & 64) == 0)        /* next table */
 
186
      {
 
187
        c->sub.code.need = e;
 
188
        c->sub.code.tree = t->next;
 
189
        break;
 
190
      }
 
191
      c->mode = BADCODE;        /* invalid code */
 
192
      z->msg = (char*)"invalid distance code";
 
193
      r = Z_DATA_ERROR;
 
194
      LEAVE
 
195
    case DISTEXT:       /* i: getting distance extra */
 
196
      j = c->sub.copy.get;
 
197
      NEEDBITS(j)
 
198
      c->sub.copy.dist += (uInt)b & inflate_mask[j];
 
199
      DUMPBITS(j)
 
200
      Tracevv((stderr, "inflate:         distance %u\n", c->sub.copy.dist));
 
201
      c->mode = COPY;
 
202
    case COPY:          /* o: copying bytes in window, waiting for space */
 
203
#ifndef __TURBOC__ /* Turbo C bug for following expression */
 
204
      f = (uInt)(q - s->window) < c->sub.copy.dist ?
 
205
          s->end - (c->sub.copy.dist - (q - s->window)) :
 
206
          q - c->sub.copy.dist;
 
207
#else
 
208
      f = q - c->sub.copy.dist;
 
209
      if ((uInt)(q - s->window) < c->sub.copy.dist)
 
210
        f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
 
211
#endif
 
212
      while (c->len)
 
213
      {
 
214
        NEEDOUT
 
215
        OUTBYTE(*f++)
 
216
        if (f == s->end)
 
217
          f = s->window;
 
218
        c->len--;
 
219
      }
 
220
      c->mode = START;
 
221
      break;
 
222
    case LIT:           /* o: got literal, waiting for output space */
 
223
      NEEDOUT
 
224
      OUTBYTE(c->sub.lit)
 
225
      c->mode = START;
 
226
      break;
 
227
    case WASH:          /* o: got eob, possibly more output */
 
228
      FLUSH
 
229
      if (s->read != s->write)
 
230
        LEAVE
 
231
      c->mode = END;
 
232
    case END:
 
233
      r = Z_STREAM_END;
 
234
      LEAVE
 
235
    case BADCODE:       /* x: got error */
 
236
      r = Z_DATA_ERROR;
 
237
      LEAVE
 
238
    default:
 
239
      r = Z_STREAM_ERROR;
 
240
      LEAVE
 
241
  }
 
242
#ifdef NEED_DUMMY_RETURN
 
243
  return Z_STREAM_ERROR;  /* Some dumb compilers complain without this */
 
244
#endif
 
245
}
 
246
 
 
247
 
 
248
void inflate_codes_free(c, z)
 
249
inflate_codes_statef *c;
 
250
z_streamp z;
 
251
{
 
252
  ZFREE(z, c);
 
253
  Tracev((stderr, "inflate:       codes free\n"));
 
254
}