~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/inflate64.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
 * This file contains code from zlib library v.1.2.3 with modifications
3
 
 * by aCaB <acab@clamav.net> to allow decompression of deflate64 stereams
4
 
 * (aka zip method 9). The implementation is heavily inspired by InfoZip
5
 
 *  and zlib's inf9back.c
6
 
 * 
7
 
 * Full copy of the original zlib license follows:
8
 
 */
9
 
 
10
 
/* zlib.h -- interface of the 'zlib' general purpose compression library
11
 
  version 1.2.3, July 18th, 2005
12
 
 
13
 
  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
14
 
 
15
 
  This software is provided 'as-is', without any express or implied
16
 
  warranty.  In no event will the authors be held liable for any damages
17
 
  arising from the use of this software.
18
 
 
19
 
  Permission is granted to anyone to use this software for any purpose,
20
 
  including commercial applications, and to alter it and redistribute it
21
 
  freely, subject to the following restrictions:
22
 
 
23
 
  1. The origin of this software must not be misrepresented; you must not
24
 
     claim that you wrote the original software. If you use this software
25
 
     in a product, an acknowledgment in the product documentation would be
26
 
     appreciated but is not required.
27
 
  2. Altered source versions must be plainly marked as such, and must not be
28
 
     misrepresented as being the original software.
29
 
  3. This notice may not be removed or altered from any source distribution.
30
 
 
31
 
  Jean-loup Gailly        Mark Adler
32
 
  jloup@gzip.org          madler@alumni.caltech.edu
33
 
 
34
 
 
35
 
  The data format used by the zlib library is described by RFCs (Request for
36
 
  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
37
 
  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
38
 
*/
39
 
 
40
 
const char inflate64_copyright[] =
41
 
   " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
42
 
/*
43
 
  If you use the zlib library in a product, an acknowledgment is welcome
44
 
  in the documentation of your product. If for some reason you cannot
45
 
  include such an acknowledgment, I would appreciate that you keep this
46
 
  copyright string in the executable of your product.
47
 
 */
48
 
 
49
 
 
50
 
 
51
 
#include "inflate64_priv.h"
52
 
 
53
 
#include <stdlib.h> /* calloc/free */
54
 
#include <string.h> /* memcpy */
55
 
 
56
 
#include "others.h"
57
 
 
58
 
/* function prototypes */
59
 
local void fixedtables OF((struct inflate_state FAR *state));
60
 
local int updatewindow OF((z_stream64p strm, unsigned out));
61
 
local int inflate_table OF((codetype type, unsigned short FAR *lens,
62
 
                             unsigned codes, code FAR * FAR *table,
63
 
                             unsigned FAR *bits, unsigned short FAR *work));
64
 
 
65
 
 
66
 
int ZEXPORT inflate64Init2(strm, windowBits)
67
 
z_stream64p strm;
68
 
int windowBits;
69
 
{
70
 
    struct inflate_state FAR *state;
71
 
 
72
 
    if (strm == Z_NULL) return Z_STREAM_ERROR;
73
 
    state = (struct inflate_state FAR *)cli_calloc(1, sizeof(struct inflate_state));
74
 
    if (state == Z_NULL) return Z_MEM_ERROR;
75
 
    Tracev((stderr, "inflate: allocated\n"));
76
 
    strm->state = (struct internal_state FAR *)state;
77
 
    if (windowBits < 0) {
78
 
        state->wrap = 0;
79
 
        windowBits = -windowBits;
80
 
    }
81
 
    else {
82
 
        state->wrap = (windowBits >> 4) + 1;
83
 
    }
84
 
    if (windowBits < 8 || windowBits > MAX_WBITS64) {
85
 
        free(state);
86
 
        strm->state = Z_NULL;
87
 
        return Z_STREAM_ERROR;
88
 
    }
89
 
    state->wbits = (unsigned)windowBits;
90
 
    state->window = Z_NULL;
91
 
    strm->total_in = strm->total_out = state->total = 0;
92
 
    strm->adler = 1;        /* to support ill-conceived Java test suite */
93
 
    state->mode = HEAD;
94
 
    state->last = 0;
95
 
    state->havedict = 0;
96
 
    state->dmax = 32768U;
97
 
    state->wsize = 0;
98
 
    state->whave = 0;
99
 
    state->write = 0;
100
 
    state->hold = 0;
101
 
    state->bits = 0;
102
 
    state->lencode = state->distcode = state->next = state->codes;
103
 
    Tracev((stderr, "inflate: reset\n"));
104
 
    return Z_OK;
105
 
}
106
 
 
107
 
/*
108
 
   Return state with length and distance decoding tables and index sizes set to
109
 
   fixed code decoding.  Normally this returns fixed tables from inffixed.h.
110
 
   If BUILDFIXED is defined, then instead this routine builds the tables the
111
 
   first time it's called, and returns those tables the first time and
112
 
   thereafter.  This reduces the size of the code by about 2K bytes, in
113
 
   exchange for a little execution time.  However, BUILDFIXED should not be
114
 
   used for threaded applications, since the rewriting of the tables and virgin
115
 
   may not be thread-safe.
116
 
 */
117
 
local void fixedtables(state)
118
 
struct inflate_state FAR *state;
119
 
{
120
 
#ifdef BUILDFIXED
121
 
    static int virgin = 1;
122
 
    static code *lenfix, *distfix;
123
 
    static code fixed[544];
124
 
 
125
 
    /* build fixed huffman tables if first call (may not be thread safe) */
126
 
    if (virgin) {
127
 
        unsigned sym, bits;
128
 
        static code *next;
129
 
 
130
 
        /* literal/length table */
131
 
        sym = 0;
132
 
        while (sym < 144) state->lens[sym++] = 8;
133
 
        while (sym < 256) state->lens[sym++] = 9;
134
 
        while (sym < 280) state->lens[sym++] = 7;
135
 
        while (sym < 288) state->lens[sym++] = 8;
136
 
        next = fixed;
137
 
        lenfix = next;
138
 
        bits = 9;
139
 
        inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
140
 
 
141
 
        /* distance table */
142
 
        sym = 0;
143
 
        while (sym < 32) state->lens[sym++] = 5;
144
 
        distfix = next;
145
 
        bits = 5;
146
 
        inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
147
 
 
148
 
        /* do this just once */
149
 
        virgin = 0;
150
 
    }
151
 
#else /* !BUILDFIXED */
152
 
#   include "inffixed64.h"
153
 
#endif /* BUILDFIXED */
154
 
    state->lencode = lenfix;
155
 
    state->lenbits = 9;
156
 
    state->distcode = distfix;
157
 
    state->distbits = 5;
158
 
}
159
 
 
160
 
/*
161
 
   Update the window with the last wsize (normally 32K) bytes written before
162
 
   returning.  If window does not exist yet, create it.  This is only called
163
 
   when a window is already in use, or when output has been written during this
164
 
   inflate call, but the end of the deflate stream has not been reached yet.
165
 
   It is also called to create a window for dictionary data when a dictionary
166
 
   is loaded.
167
 
 
168
 
   Providing output buffers larger than 32K to inflate() should provide a speed
169
 
   advantage, since only the last 32K of output is copied to the sliding window
170
 
   upon return from inflate(), and since all distances after the first 32K of
171
 
   output will fall in the output data, making match copies simpler and faster.
172
 
   The advantage may be dependent on the size of the processor's data caches.
173
 
 */
174
 
local int updatewindow(strm, out)
175
 
z_stream64p strm;
176
 
unsigned out;
177
 
{
178
 
    struct inflate_state FAR *state;
179
 
    unsigned copy, dist;
180
 
 
181
 
    state = (struct inflate_state FAR *)strm->state;
182
 
 
183
 
    /* if it hasn't been done already, allocate space for the window */
184
 
    if (state->window == Z_NULL) {
185
 
        state->window = (unsigned char FAR *)cli_calloc(1U << state->wbits, sizeof(unsigned char));
186
 
        if (state->window == Z_NULL) return 1;
187
 
    }
188
 
 
189
 
    /* if window not in use yet, initialize */
190
 
    if (state->wsize == 0) {
191
 
        state->wsize = 1U << state->wbits;
192
 
        state->write = 0;
193
 
        state->whave = 0;
194
 
    }
195
 
 
196
 
    /* copy state->wsize or less output bytes into the circular window */
197
 
    copy = out - strm->avail_out;
198
 
    if (copy >= state->wsize) {
199
 
        memcpy(state->window, strm->next_out - state->wsize, state->wsize);
200
 
        state->write = 0;
201
 
        state->whave = state->wsize;
202
 
    }
203
 
    else {
204
 
        dist = state->wsize - state->write;
205
 
        if (dist > copy) dist = copy;
206
 
        memcpy(state->window + state->write, strm->next_out - copy, dist);
207
 
        copy -= dist;
208
 
        if (copy) {
209
 
            memcpy(state->window, strm->next_out - copy, copy);
210
 
            state->write = copy;
211
 
            state->whave = state->wsize;
212
 
        }
213
 
        else {
214
 
            state->write += dist;
215
 
            if (state->write == state->wsize) state->write = 0;
216
 
            if (state->whave < state->wsize) state->whave += dist;
217
 
        }
218
 
    }
219
 
    return 0;
220
 
}
221
 
 
222
 
/* Macros for inflate(): */
223
 
 
224
 
/* check function to use adler32() for zlib or crc32() for gzip */
225
 
#  define UPDATE(check, buf, len) adler32(check, buf, len)
226
 
 
227
 
/* Load registers with state in inflate() for speed */
228
 
#define LOAD() \
229
 
    do { \
230
 
        put = strm->next_out; \
231
 
        left = strm->avail_out; \
232
 
        next = strm->next_in; \
233
 
        have = strm->avail_in; \
234
 
        hold = state->hold; \
235
 
        bits = state->bits; \
236
 
    } while (0)
237
 
 
238
 
/* Restore state from registers in inflate() */
239
 
#define RESTORE() \
240
 
    do { \
241
 
        strm->next_out = put; \
242
 
        strm->avail_out = left; \
243
 
        strm->next_in = next; \
244
 
        strm->avail_in = have; \
245
 
        state->hold = hold; \
246
 
        state->bits = bits; \
247
 
    } while (0)
248
 
 
249
 
/* Clear the input bit accumulator */
250
 
#define INITBITS() \
251
 
    do { \
252
 
        hold = 0; \
253
 
        bits = 0; \
254
 
    } while (0)
255
 
 
256
 
/* Get a byte of input into the bit accumulator, or return from inflate()
257
 
   if there is no input available. */
258
 
#define PULLBYTE() \
259
 
    do { \
260
 
        if (have == 0) goto inf_leave; \
261
 
        have--; \
262
 
        hold += (unsigned long)(*next++) << bits; \
263
 
        bits += 8; \
264
 
    } while (0)
265
 
 
266
 
/* Assure that there are at least n bits in the bit accumulator.  If there is
267
 
   not enough available input to do that, then return from inflate(). */
268
 
#define NEEDBITS(n) \
269
 
    do { \
270
 
        while (bits < (unsigned)(n)) \
271
 
            PULLBYTE(); \
272
 
    } while (0)
273
 
 
274
 
/* Return the low n bits of the bit accumulator (n < 16) */
275
 
#define BITS(n) \
276
 
    ((unsigned)hold & ((1U << (n)) - 1))
277
 
 
278
 
/* Remove n bits from the bit accumulator */
279
 
#define DROPBITS(n) \
280
 
    do { \
281
 
        hold >>= (n); \
282
 
        bits -= (unsigned)(n); \
283
 
    } while (0)
284
 
 
285
 
/* Remove zero to seven bits as needed to go to a byte boundary */
286
 
#define BYTEBITS() \
287
 
    do { \
288
 
        hold >>= bits & 7; \
289
 
        bits -= bits & 7; \
290
 
    } while (0)
291
 
 
292
 
/* Reverse the bytes in a 32-bit value */
293
 
#define REVERSE(q) \
294
 
    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
295
 
     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
296
 
 
297
 
/*
298
 
   inflate() uses a state machine to process as much input data and generate as
299
 
   much output data as possible before returning.  The state machine is
300
 
   structured roughly as follows:
301
 
 
302
 
    for (;;) switch (state) {
303
 
    ...
304
 
    case STATEn:
305
 
        if (not enough input data or output space to make progress)
306
 
            return;
307
 
        ... make progress ...
308
 
        state = STATEm;
309
 
        break;
310
 
    ...
311
 
    }
312
 
 
313
 
   so when inflate() is called again, the same case is attempted again, and
314
 
   if the appropriate resources are provided, the machine proceeds to the
315
 
   next state.  The NEEDBITS() macro is usually the way the state evaluates
316
 
   whether it can proceed or should return.  NEEDBITS() does the return if
317
 
   the requested bits are not available.  The typical use of the BITS macros
318
 
   is:
319
 
 
320
 
        NEEDBITS(n);
321
 
        ... do something with BITS(n) ...
322
 
        DROPBITS(n);
323
 
 
324
 
   where NEEDBITS(n) either returns from inflate() if there isn't enough
325
 
   input left to load n bits into the accumulator, or it continues.  BITS(n)
326
 
   gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
327
 
   the low n bits off the accumulator.  INITBITS() clears the accumulator
328
 
   and sets the number of available bits to zero.  BYTEBITS() discards just
329
 
   enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
330
 
   and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
331
 
 
332
 
   NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
333
 
   if there is no input available.  The decoding of variable length codes uses
334
 
   PULLBYTE() directly in order to pull just enough bytes to decode the next
335
 
   code, and no more.
336
 
 
337
 
   Some states loop until they get enough input, making sure that enough
338
 
   state information is maintained to continue the loop where it left off
339
 
   if NEEDBITS() returns in the loop.  For example, want, need, and keep
340
 
   would all have to actually be part of the saved state in case NEEDBITS()
341
 
   returns:
342
 
 
343
 
    case STATEw:
344
 
        while (want < need) {
345
 
            NEEDBITS(n);
346
 
            keep[want++] = BITS(n);
347
 
            DROPBITS(n);
348
 
        }
349
 
        state = STATEx;
350
 
    case STATEx:
351
 
 
352
 
   As shown above, if the next state is also the next case, then the break
353
 
   is omitted.
354
 
 
355
 
   A state may also return if there is not enough output space available to
356
 
   complete that state.  Those states are copying stored data, writing a
357
 
   literal byte, and copying a matching string.
358
 
 
359
 
   When returning, a "goto inf_leave" is used to update the total counters,
360
 
   update the check value, and determine whether any progress has been made
361
 
   during that inflate() call in order to return the proper return code.
362
 
   Progress is defined as a change in either strm->avail_in or strm->avail_out.
363
 
   When there is a window, goto inf_leave will update the window with the last
364
 
   output written.  If a goto inf_leave occurs in the middle of decompression
365
 
   and there is no window currently, goto inf_leave will create one and copy
366
 
   output to the window for the next call of inflate().
367
 
 
368
 
   In this implementation, the flush parameter of inflate() only affects the
369
 
   return code (per zlib.h).  inflate() always writes as much as possible to
370
 
   strm->next_out, given the space available and the provided input--the effect
371
 
   documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
372
 
   the allocation of and copying into a sliding window until necessary, which
373
 
   provides the effect documented in zlib.h for Z_FINISH when the entire input
374
 
   stream available.  So the only thing the flush parameter actually does is:
375
 
   when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
376
 
   will return Z_BUF_ERROR if it has not reached the end of the stream.
377
 
 */
378
 
 
379
 
int ZEXPORT inflate64(strm, flush)
380
 
z_stream64p strm;
381
 
int flush;
382
 
{
383
 
    struct inflate_state FAR *state;
384
 
    unsigned char FAR *next;    /* next input */
385
 
    unsigned char FAR *put;     /* next output */
386
 
    unsigned have, left;        /* available input and output */
387
 
    unsigned long hold;         /* bit buffer */
388
 
    unsigned bits;              /* bits in bit buffer */
389
 
    unsigned in, out;           /* save starting available input and output */
390
 
    unsigned copy;              /* number of stored or match bytes to copy */
391
 
    unsigned char FAR *from;    /* where to copy match bytes from */
392
 
    code this;                  /* current decoding table entry */
393
 
    code last;                  /* parent table entry */
394
 
    unsigned len;               /* length to copy for repeats, bits to drop */
395
 
    int ret;                    /* return code */
396
 
    static const unsigned short order[19] = /* permutation of code lengths */
397
 
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
398
 
 
399
 
    if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
400
 
        (strm->next_in == Z_NULL && strm->avail_in != 0))
401
 
        return Z_STREAM_ERROR;
402
 
 
403
 
    state = (struct inflate_state FAR *)strm->state;
404
 
    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
405
 
    LOAD();
406
 
    in = have;
407
 
    out = left;
408
 
    ret = Z_OK;
409
 
    for (;;)
410
 
        switch (state->mode) {
411
 
        case HEAD:
412
 
            if (state->wrap == 0) {
413
 
                state->mode = TYPEDO;
414
 
                break;
415
 
            }
416
 
            NEEDBITS(16);
417
 
            if (
418
 
                ((BITS(8) << 8) + (hold >> 8)) % 31) {
419
 
                state->mode = ACAB_BAD;
420
 
                break;
421
 
            }
422
 
            if (BITS(4) != Z_DEFLATED) {
423
 
                state->mode = ACAB_BAD;
424
 
                break;
425
 
            }
426
 
            DROPBITS(4);
427
 
            len = BITS(4) + 8;
428
 
            if (len > state->wbits) {
429
 
                state->mode = ACAB_BAD;
430
 
                break;
431
 
            }
432
 
            state->dmax = 1U << len;
433
 
            Tracev((stderr, "inflate:   zlib header ok\n"));
434
 
            strm->adler = state->check = adler32(0L, Z_NULL, 0);
435
 
            state->mode = hold & 0x200 ? DICTID : TYPE;
436
 
            INITBITS();
437
 
            break;
438
 
        case DICTID:
439
 
            NEEDBITS(32);
440
 
            strm->adler = state->check = REVERSE(hold);
441
 
            INITBITS();
442
 
            state->mode = DICT;
443
 
        case DICT:
444
 
            RESTORE();
445
 
            return Z_NEED_DICT;
446
 
        case TYPE:
447
 
            if (flush == Z_BLOCK) goto inf_leave;
448
 
        case TYPEDO:
449
 
            if (state->last) {
450
 
                BYTEBITS();
451
 
                state->mode = CHECK;
452
 
                break;
453
 
            }
454
 
            NEEDBITS(3);
455
 
            state->last = BITS(1);
456
 
            DROPBITS(1);
457
 
            switch (BITS(2)) {
458
 
            case 0:                             /* stored block */
459
 
                Tracev((stderr, "inflate:     stored block%s\n",
460
 
                        state->last ? " (last)" : ""));
461
 
                state->mode = STORED;
462
 
                break;
463
 
            case 1:                             /* fixed block */
464
 
                fixedtables(state);
465
 
                Tracev((stderr, "inflate:     fixed codes block%s\n",
466
 
                        state->last ? " (last)" : ""));
467
 
                state->mode = LEN;              /* decode codes */
468
 
                break;
469
 
            case 2:                             /* dynamic block */
470
 
                Tracev((stderr, "inflate:     dynamic codes block%s\n",
471
 
                        state->last ? " (last)" : ""));
472
 
                state->mode = TABLE;
473
 
                break;
474
 
            case 3:
475
 
                state->mode = ACAB_BAD;
476
 
            }
477
 
            DROPBITS(2);
478
 
            break;
479
 
        case STORED:
480
 
            BYTEBITS();                         /* go to byte boundary */
481
 
            NEEDBITS(32);
482
 
            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
483
 
                state->mode = ACAB_BAD;
484
 
                break;
485
 
            }
486
 
            state->length = (unsigned)hold & 0xffff;
487
 
            Tracev((stderr, "inflate:       stored length %u\n",
488
 
                    state->length));
489
 
            INITBITS();
490
 
            state->mode = COPY;
491
 
        case COPY:
492
 
            copy = state->length;
493
 
            if (copy) {
494
 
                if (copy > have) copy = have;
495
 
                if (copy > left) copy = left;
496
 
                if (copy == 0) goto inf_leave;
497
 
                memcpy(put, next, copy);
498
 
                have -= copy;
499
 
                next += copy;
500
 
                left -= copy;
501
 
                put += copy;
502
 
                state->length -= copy;
503
 
                break;
504
 
            }
505
 
            Tracev((stderr, "inflate:       stored end\n"));
506
 
            state->mode = TYPE;
507
 
            break;
508
 
        case TABLE:
509
 
            NEEDBITS(14);
510
 
            state->nlen = BITS(5) + 257;
511
 
            DROPBITS(5);
512
 
            state->ndist = BITS(5) + 1;
513
 
            DROPBITS(5);
514
 
            state->ncode = BITS(4) + 4;
515
 
            DROPBITS(4);
516
 
#ifndef PKZIP_BUG_WORKAROUND
517
 
            if (state->nlen > 286 || state->ndist > 30) {
518
 
                state->mode = ACAB_BAD;
519
 
                break;
520
 
            }
521
 
#endif
522
 
            Tracev((stderr, "inflate:       table sizes ok\n"));
523
 
            state->have = 0;
524
 
            state->mode = LENLENS;
525
 
        case LENLENS:
526
 
            while (state->have < state->ncode) {
527
 
                NEEDBITS(3);
528
 
                state->lens[order[state->have++]] = (unsigned short)BITS(3);
529
 
                DROPBITS(3);
530
 
            }
531
 
            while (state->have < 19)
532
 
                state->lens[order[state->have++]] = 0;
533
 
            state->next = state->codes;
534
 
            state->lencode = (code const FAR *)(state->next);
535
 
            state->lenbits = 7;
536
 
            ret = inflate_table(CODES, state->lens, 19, &(state->next),
537
 
                                &(state->lenbits), state->work);
538
 
            if (ret) {
539
 
                state->mode = ACAB_BAD;
540
 
                break;
541
 
            }
542
 
            Tracev((stderr, "inflate:       code lengths ok\n"));
543
 
            state->have = 0;
544
 
            state->mode = CODELENS;
545
 
        case CODELENS:
546
 
            while (state->have < state->nlen + state->ndist) {
547
 
                for (;;) {
548
 
                    this = state->lencode[BITS(state->lenbits)];
549
 
                    if ((unsigned)(this.bits) <= bits) break;
550
 
                    PULLBYTE();
551
 
                }
552
 
                if (this.val < 16) {
553
 
                    NEEDBITS(this.bits);
554
 
                    DROPBITS(this.bits);
555
 
                    state->lens[state->have++] = this.val;
556
 
                }
557
 
                else {
558
 
                    if (this.val == 16) {
559
 
                        NEEDBITS(this.bits + 2);
560
 
                        DROPBITS(this.bits);
561
 
                        if (state->have == 0) {
562
 
                            state->mode = ACAB_BAD;
563
 
                            break;
564
 
                        }
565
 
                        len = state->lens[state->have - 1];
566
 
                        copy = 3 + BITS(2);
567
 
                        DROPBITS(2);
568
 
                    }
569
 
                    else if (this.val == 17) {
570
 
                        NEEDBITS(this.bits + 3);
571
 
                        DROPBITS(this.bits);
572
 
                        len = 0;
573
 
                        copy = 3 + BITS(3);
574
 
                        DROPBITS(3);
575
 
                    }
576
 
                    else {
577
 
                        NEEDBITS(this.bits + 7);
578
 
                        DROPBITS(this.bits);
579
 
                        len = 0;
580
 
                        copy = 11 + BITS(7);
581
 
                        DROPBITS(7);
582
 
                    }
583
 
                    if (state->have + copy > state->nlen + state->ndist) {
584
 
                        state->mode = ACAB_BAD;
585
 
                        break;
586
 
                    }
587
 
                    while (copy--)
588
 
                        state->lens[state->have++] = (unsigned short)len;
589
 
                }
590
 
            }
591
 
 
592
 
            /* handle error breaks in while */
593
 
            if (state->mode == ACAB_BAD) break;
594
 
 
595
 
            /* build code tables */
596
 
            state->next = state->codes;
597
 
            state->lencode = (code const FAR *)(state->next);
598
 
            state->lenbits = 9;
599
 
            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
600
 
                                &(state->lenbits), state->work);
601
 
            if (ret) {
602
 
                state->mode = ACAB_BAD;
603
 
                break;
604
 
            }
605
 
            state->distcode = (code const FAR *)(state->next);
606
 
            state->distbits = 6;
607
 
            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
608
 
                            &(state->next), &(state->distbits), state->work);
609
 
            if (ret) {
610
 
                state->mode = ACAB_BAD;
611
 
                break;
612
 
            }
613
 
            Tracev((stderr, "inflate:       codes ok\n"));
614
 
            state->mode = LEN;
615
 
        case LEN:
616
 
                /*            if (have >= 6 && left >= 258) {
617
 
                RESTORE();
618
 
                inflate_fast(strm, out);
619
 
                LOAD();
620
 
                break;
621
 
                }*/
622
 
            for (;;) {
623
 
                this = state->lencode[BITS(state->lenbits)];
624
 
                if ((unsigned)(this.bits) <= bits) break;
625
 
                PULLBYTE();
626
 
            }
627
 
            if (this.op && (this.op & 0xf0) == 0) {
628
 
                last = this;
629
 
                for (;;) {
630
 
                    this = state->lencode[last.val +
631
 
                            (BITS(last.bits + last.op) >> last.bits)];
632
 
                    if ((unsigned)(last.bits + this.bits) <= bits) break;
633
 
                    PULLBYTE();
634
 
                }
635
 
                DROPBITS(last.bits);
636
 
            }
637
 
            DROPBITS(this.bits);
638
 
            state->length = (unsigned)this.val;
639
 
            if ((int)(this.op) == 0) {
640
 
                Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
641
 
                        "inflate:         literal '%c'\n" :
642
 
                        "inflate:         literal 0x%02x\n", this.val));
643
 
                state->mode = LIT;
644
 
                break;
645
 
            }
646
 
            Tracevv((stderr, "inflate:         op %u\n", this.op));
647
 
            if (this.op & 32) {
648
 
                Tracevv((stderr, "inflate:         end of block\n"));
649
 
                state->mode = TYPE;
650
 
                break;
651
 
            }
652
 
            if (this.op & 64) {
653
 
                state->mode = ACAB_BAD;
654
 
                break;
655
 
            }
656
 
            state->extra = (unsigned)(this.op) & 31;
657
 
            state->mode = LENEXT;
658
 
        case LENEXT:
659
 
            if (state->extra) {
660
 
                NEEDBITS(state->extra);
661
 
                state->length += BITS(state->extra);
662
 
                DROPBITS(state->extra);
663
 
            }
664
 
            Tracevv((stderr, "inflate:         length %u\n", state->length));
665
 
            state->mode = DIST;
666
 
        case DIST:
667
 
            for (;;) {
668
 
                this = state->distcode[BITS(state->distbits)];
669
 
                if ((unsigned)(this.bits) <= bits) break;
670
 
                PULLBYTE();
671
 
            }
672
 
            if ((this.op & 0xf0) == 0) {
673
 
                last = this;
674
 
                for (;;) {
675
 
                    this = state->distcode[last.val +
676
 
                            (BITS(last.bits + last.op) >> last.bits)];
677
 
                    if ((unsigned)(last.bits + this.bits) <= bits) break;
678
 
                    PULLBYTE();
679
 
                }
680
 
                DROPBITS(last.bits);
681
 
            }
682
 
            DROPBITS(this.bits);
683
 
            if (this.op & 64) {
684
 
                state->mode = ACAB_BAD;
685
 
                break;
686
 
            }
687
 
            Tracevv((stderr, "inflate:        val %u\n", state->offset));
688
 
            state->offset = (unsigned)this.val;
689
 
            state->extra = (unsigned)(this.op) & 15;
690
 
            state->mode = DISTEXT;
691
 
        case DISTEXT:
692
 
            if (state->extra) {
693
 
                NEEDBITS(state->extra);
694
 
                state->offset += BITS(state->extra);
695
 
                DROPBITS(state->extra);
696
 
            }
697
 
#ifdef INFLATE_STRICT
698
 
            if (state->offset > state->dmax) {
699
 
                state->mode = ACAB_BAD;
700
 
                break;
701
 
            }
702
 
#endif
703
 
            if (state->offset > state->whave + out - left) {
704
 
                state->mode = ACAB_BAD;
705
 
                break;
706
 
            }
707
 
            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
708
 
            state->mode = MATCH;
709
 
        case MATCH:
710
 
            if (left == 0) goto inf_leave;
711
 
            copy = out - left;
712
 
            if (state->offset > copy) {         /* copy from window */
713
 
                copy = state->offset - copy;
714
 
                if (copy > state->write) {
715
 
                    copy -= state->write;
716
 
                    from = state->window + (state->wsize - copy);
717
 
                }
718
 
                else
719
 
                    from = state->window + (state->write - copy);
720
 
                if (copy > state->length) copy = state->length;
721
 
            }
722
 
            else {                              /* copy from output */
723
 
                from = put - state->offset;
724
 
                copy = state->length;
725
 
            }
726
 
            if (copy > left) copy = left;
727
 
            left -= copy;
728
 
            state->length -= copy;
729
 
            do {
730
 
                *put++ = *from++;
731
 
            } while (--copy);
732
 
            if (state->length == 0) state->mode = LEN;
733
 
            break;
734
 
        case LIT:
735
 
            if (left == 0) goto inf_leave;
736
 
            *put++ = (unsigned char)(state->length);
737
 
            left--;
738
 
            state->mode = LEN;
739
 
            break;
740
 
        case CHECK:
741
 
            if (state->wrap) {
742
 
                NEEDBITS(32);
743
 
                out -= left;
744
 
                strm->total_out += out;
745
 
                state->total += out;
746
 
                if (out)
747
 
                    strm->adler = state->check =
748
 
                        UPDATE(state->check, put - out, out);
749
 
                out = left;
750
 
                if ((
751
 
                     REVERSE(hold)) != state->check) {
752
 
                    state->mode = ACAB_BAD;
753
 
                    break;
754
 
                }
755
 
                INITBITS();
756
 
                Tracev((stderr, "inflate:   check matches trailer\n"));
757
 
            }
758
 
            state->mode = DONE;
759
 
        case DONE:
760
 
            ret = Z_STREAM_END;
761
 
            goto inf_leave;
762
 
        case ACAB_BAD:
763
 
            ret = Z_DATA_ERROR;
764
 
            goto inf_leave;
765
 
        case MEM:
766
 
            return Z_MEM_ERROR;
767
 
        case SYNC:
768
 
        default:
769
 
            return Z_STREAM_ERROR;
770
 
        }
771
 
 
772
 
    /*
773
 
       Return from inflate(), updating the total counts and the check value.
774
 
       If there was no progress during the inflate() call, return a buffer
775
 
       error.  Call updatewindow() to create and/or update the window state.
776
 
       Note: a memory error from inflate() is non-recoverable.
777
 
     */
778
 
  inf_leave:
779
 
    RESTORE();
780
 
    if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
781
 
        if (updatewindow(strm, out)) {
782
 
            state->mode = MEM;
783
 
            return Z_MEM_ERROR;
784
 
        }
785
 
    in -= strm->avail_in;
786
 
    out -= strm->avail_out;
787
 
    strm->total_in += in;
788
 
    strm->total_out += out;
789
 
    state->total += out;
790
 
    if (state->wrap && out)
791
 
        strm->adler = state->check =
792
 
            UPDATE(state->check, strm->next_out - out, out);
793
 
    strm->data_type = state->bits + (state->last ? 64 : 0) +
794
 
                      (state->mode == TYPE ? 128 : 0);
795
 
    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
796
 
        ret = Z_BUF_ERROR;
797
 
    return ret;
798
 
}
799
 
 
800
 
int ZEXPORT inflate64End(strm)
801
 
z_stream64p strm;
802
 
{
803
 
    struct inflate_state FAR *state;
804
 
    if (strm == Z_NULL || strm->state == Z_NULL)
805
 
        return Z_STREAM_ERROR;
806
 
    state = (struct inflate_state FAR *)strm->state;
807
 
    if (state->window != Z_NULL) free(state->window);
808
 
    free(strm->state);
809
 
    strm->state = Z_NULL;
810
 
    Tracev((stderr, "inflate: end\n"));
811
 
    return Z_OK;
812
 
}
813
 
 
814
 
 
815
 
/*
816
 
   Build a set of tables to decode the provided canonical Huffman code.
817
 
   The code lengths are lens[0..codes-1].  The result starts at *table,
818
 
   whose indices are 0..2^bits-1.  work is a writable array of at least
819
 
   lens shorts, which is used as a work area.  type is the type of code
820
 
   to be generated, CODES, LENS, or DISTS.  On return, zero is success,
821
 
   -1 is an invalid code, and +1 means that ENOUGH isn't enough.  table
822
 
   on return points to the next available entry's address.  bits is the
823
 
   requested root table index bits, and on return it is the actual root
824
 
   table index bits.  It will differ if the request is greater than the
825
 
   longest code or if it is less than the shortest code.
826
 
 */
827
 
local int inflate_table(type, lens, codes, table, bits, work)
828
 
codetype type;
829
 
unsigned short FAR *lens;
830
 
unsigned codes;
831
 
code FAR * FAR *table;
832
 
unsigned FAR *bits;
833
 
unsigned short FAR *work;
834
 
{
835
 
    unsigned len;               /* a code's length in bits */
836
 
    unsigned sym;               /* index of code symbols */
837
 
    unsigned min, max;          /* minimum and maximum code lengths */
838
 
    unsigned root;              /* number of index bits for root table */
839
 
    unsigned curr;              /* number of index bits for current table */
840
 
    unsigned drop;              /* code bits to drop for sub-table */
841
 
    int left;                   /* number of prefix codes available */
842
 
    unsigned used;              /* code entries in table used */
843
 
    unsigned huff;              /* Huffman code */
844
 
    unsigned incr;              /* for incrementing code, index */
845
 
    unsigned fill;              /* index for replicating entries */
846
 
    unsigned low;               /* low bits for current root entry */
847
 
    unsigned mask;              /* mask for low root bits */
848
 
    code this;                  /* table entry for duplication */
849
 
    code FAR *next;             /* next available space in table */
850
 
    const unsigned short FAR *base;     /* base value table to use */
851
 
    const unsigned short FAR *extra;    /* extra bits table to use */
852
 
    int end;                    /* use base and extra for symbol > end */
853
 
    unsigned short count[MAXBITS+1];    /* number of codes of each length */
854
 
    unsigned short offs[MAXBITS+1];     /* offsets in table for each length */
855
 
    static const unsigned short lbase[31] = { /* Length codes 257..285 base */
856
 
        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
857
 
        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, /* 258 */ 3, 0, 0};
858
 
    static const unsigned short lext[31] = { /* Length codes 257..285 extra */
859
 
/*         16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, */
860
 
/*         19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 31, 201, 196}; */
861
 
        128, 128, 128, 128, 128, 128, 128, 128, 129, 129, 129, 129, 130, 130, 130, 130,
862
 
        131, 131, 131, 131, 132, 132, 132, 132, 133, 133, 133, 133, 144, 201, 196};
863
 
    static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
864
 
        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
865
 
        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
866
 
        8193, 12289, 16385, 24577, 32769, 49153};
867
 
    static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
868
 
        16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
869
 
        23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
870
 
        28, 28, 29, 29, 30, 30};
871
 
 
872
 
    /*
873
 
       Process a set of code lengths to create a canonical Huffman code.  The
874
 
       code lengths are lens[0..codes-1].  Each length corresponds to the
875
 
       symbols 0..codes-1.  The Huffman code is generated by first sorting the
876
 
       symbols by length from short to long, and retaining the symbol order
877
 
       for codes with equal lengths.  Then the code starts with all zero bits
878
 
       for the first code of the shortest length, and the codes are integer
879
 
       increments for the same length, and zeros are appended as the length
880
 
       increases.  For the deflate format, these bits are stored backwards
881
 
       from their more natural integer increment ordering, and so when the
882
 
       decoding tables are built in the large loop below, the integer codes
883
 
       are incremented backwards.
884
 
 
885
 
       This routine assumes, but does not check, that all of the entries in
886
 
       lens[] are in the range 0..MAXBITS.  The caller must assure this.
887
 
       1..MAXBITS is interpreted as that code length.  zero means that that
888
 
       symbol does not occur in this code.
889
 
 
890
 
       The codes are sorted by computing a count of codes for each length,
891
 
       creating from that a table of starting indices for each length in the
892
 
       sorted table, and then entering the symbols in order in the sorted
893
 
       table.  The sorted table is work[], with that space being provided by
894
 
       the caller.
895
 
 
896
 
       The length counts are used for other purposes as well, i.e. finding
897
 
       the minimum and maximum length codes, determining if there are any
898
 
       codes at all, checking for a valid set of lengths, and looking ahead
899
 
       at length counts to determine sub-table sizes when building the
900
 
       decoding tables.
901
 
     */
902
 
 
903
 
    /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
904
 
    for (len = 0; len <= MAXBITS; len++)
905
 
        count[len] = 0;
906
 
    for (sym = 0; sym < codes; sym++)
907
 
        count[lens[sym]]++;
908
 
 
909
 
    /* bound code lengths, force root to be within code lengths */
910
 
    root = *bits;
911
 
    for (max = MAXBITS; max >= 1; max--)
912
 
        if (count[max] != 0) break;
913
 
    if (root > max) root = max;
914
 
    if (max == 0) {                     /* no symbols to code at all */
915
 
        this.op = (unsigned char)64;    /* invalid code marker */
916
 
        this.bits = (unsigned char)1;
917
 
        this.val = (unsigned short)0;
918
 
        *(*table)++ = this;             /* make a table to force an error */
919
 
        *(*table)++ = this;
920
 
        *bits = 1;
921
 
        return 0;     /* no symbols, but wait for decoding to report error */
922
 
    }
923
 
    for (min = 1; min <= MAXBITS; min++)
924
 
        if (count[min] != 0) break;
925
 
    if (root < min) root = min;
926
 
 
927
 
    /* check for an over-subscribed or incomplete set of lengths */
928
 
    left = 1;
929
 
    for (len = 1; len <= MAXBITS; len++) {
930
 
        left <<= 1;
931
 
        left -= count[len];
932
 
        if (left < 0) return -1;        /* over-subscribed */
933
 
    }
934
 
    if (left > 0 && (type == CODES || max != 1))
935
 
        return -1;                      /* incomplete set */
936
 
 
937
 
    /* generate offsets into symbol table for each length for sorting */
938
 
    offs[1] = 0;
939
 
    for (len = 1; len < MAXBITS; len++)
940
 
        offs[len + 1] = offs[len] + count[len];
941
 
 
942
 
    /* sort symbols by length, by symbol order within each length */
943
 
    for (sym = 0; sym < codes; sym++)
944
 
        if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
945
 
 
946
 
    /*
947
 
       Create and fill in decoding tables.  In this loop, the table being
948
 
       filled is at next and has curr index bits.  The code being used is huff
949
 
       with length len.  That code is converted to an index by dropping drop
950
 
       bits off of the bottom.  For codes where len is less than drop + curr,
951
 
       those top drop + curr - len bits are incremented through all values to
952
 
       fill the table with replicated entries.
953
 
 
954
 
       root is the number of index bits for the root table.  When len exceeds
955
 
       root, sub-tables are created pointed to by the root entry with an index
956
 
       of the low root bits of huff.  This is saved in low to check for when a
957
 
       new sub-table should be started.  drop is zero when the root table is
958
 
       being filled, and drop is root when sub-tables are being filled.
959
 
 
960
 
       When a new sub-table is needed, it is necessary to look ahead in the
961
 
       code lengths to determine what size sub-table is needed.  The length
962
 
       counts are used for this, and so count[] is decremented as codes are
963
 
       entered in the tables.
964
 
 
965
 
       used keeps track of how many table entries have been allocated from the
966
 
       provided *table space.  It is checked when a LENS table is being made
967
 
       against the space in *table, ENOUGH, minus the maximum space needed by
968
 
       the worst case distance code, MAXD.  This should never happen, but the
969
 
       sufficiency of ENOUGH has not been proven exhaustively, hence the check.
970
 
       This assumes that when type == LENS, bits == 9.
971
 
 
972
 
       sym increments through all symbols, and the loop terminates when
973
 
       all codes of length max, i.e. all codes, have been processed.  This
974
 
       routine permits incomplete codes, so another loop after this one fills
975
 
       in the rest of the decoding tables with invalid code markers.
976
 
     */
977
 
 
978
 
    /* set up for code type */
979
 
    switch (type) {
980
 
    case CODES:
981
 
        base = extra = work;    /* dummy value--not used */
982
 
        end = 19;
983
 
        break;
984
 
    case LENS:
985
 
        base = lbase;
986
 
        base -= 257;
987
 
        extra = lext;
988
 
        extra -= 257;
989
 
        end = 256;
990
 
        break;
991
 
    default:            /* DISTS */
992
 
        base = dbase;
993
 
        extra = dext;
994
 
        end = -1;
995
 
    }
996
 
 
997
 
    /* initialize state for loop */
998
 
    huff = 0;                   /* starting code */
999
 
    sym = 0;                    /* starting code symbol */
1000
 
    len = min;                  /* starting code length */
1001
 
    next = *table;              /* current table to fill in */
1002
 
    curr = root;                /* current table index bits */
1003
 
    drop = 0;                   /* current bits to drop from code for index */
1004
 
    low = (unsigned)(-1);       /* trigger new sub-table when len > root */
1005
 
    used = 1U << root;          /* use root table entries */
1006
 
    mask = used - 1;            /* mask for comparing low */
1007
 
 
1008
 
    /* check available table space */
1009
 
    if (type == LENS && used >= ENOUGH - MAXD)
1010
 
        return 1;
1011
 
 
1012
 
    /* process all codes and make table entries */
1013
 
    for (;;) {
1014
 
        /* create table entry */
1015
 
        this.bits = (unsigned char)(len - drop);
1016
 
        if ((int)(work[sym]) < end) {
1017
 
            this.op = (unsigned char)0;
1018
 
            this.val = work[sym];
1019
 
        }
1020
 
        else if ((int)(work[sym]) > end) {
1021
 
            this.op = (unsigned char)(extra[work[sym]]);
1022
 
            this.val = base[work[sym]];
1023
 
        }
1024
 
        else {
1025
 
            this.op = (unsigned char)(32 + 64);         /* end of block */
1026
 
            this.val = 0;
1027
 
        }
1028
 
 
1029
 
        /* replicate for those indices with low len bits equal to huff */
1030
 
        incr = 1U << (len - drop);
1031
 
        fill = 1U << curr;
1032
 
        min = fill;                 /* save offset to next table */
1033
 
        do {
1034
 
            fill -= incr;
1035
 
            next[(huff >> drop) + fill] = this;
1036
 
        } while (fill != 0);
1037
 
 
1038
 
        /* backwards increment the len-bit code huff */
1039
 
        incr = 1U << (len - 1);
1040
 
        while (huff & incr)
1041
 
            incr >>= 1;
1042
 
        if (incr != 0) {
1043
 
            huff &= incr - 1;
1044
 
            huff += incr;
1045
 
        }
1046
 
        else
1047
 
            huff = 0;
1048
 
 
1049
 
        /* go to next symbol, update count, len */
1050
 
        sym++;
1051
 
        if (--(count[len]) == 0) {
1052
 
            if (len == max) break;
1053
 
            len = lens[work[sym]];
1054
 
        }
1055
 
 
1056
 
        /* create new sub-table if needed */
1057
 
        if (len > root && (huff & mask) != low) {
1058
 
            /* if first time, transition to sub-tables */
1059
 
            if (drop == 0)
1060
 
                drop = root;
1061
 
 
1062
 
            /* increment past last table */
1063
 
            next += min;            /* here min is 1 << curr */
1064
 
 
1065
 
            /* determine length of next table */
1066
 
            curr = len - drop;
1067
 
            left = (int)(1 << curr);
1068
 
            while (curr + drop < max) {
1069
 
                left -= count[curr + drop];
1070
 
                if (left <= 0) break;
1071
 
                curr++;
1072
 
                left <<= 1;
1073
 
            }
1074
 
 
1075
 
            /* check for enough space */
1076
 
            used += 1U << curr;
1077
 
            if (type == LENS && used >= ENOUGH - MAXD)
1078
 
                return 1;
1079
 
 
1080
 
            /* point entry in root table to sub-table */
1081
 
            low = huff & mask;
1082
 
            (*table)[low].op = (unsigned char)curr;
1083
 
            (*table)[low].bits = (unsigned char)root;
1084
 
            (*table)[low].val = (unsigned short)(next - *table);
1085
 
        }
1086
 
    }
1087
 
 
1088
 
    /*
1089
 
       Fill in rest of table for incomplete codes.  This loop is similar to the
1090
 
       loop above in incrementing huff for table indices.  It is assumed that
1091
 
       len is equal to curr + drop, so there is no loop needed to increment
1092
 
       through high index bits.  When the current sub-table is filled, the loop
1093
 
       drops back to the root table to fill in any remaining entries there.
1094
 
     */
1095
 
    this.op = (unsigned char)64;                /* invalid code marker */
1096
 
    this.bits = (unsigned char)(len - drop);
1097
 
    this.val = (unsigned short)0;
1098
 
    while (huff != 0) {
1099
 
        /* when done with sub-table, drop back to root table */
1100
 
        if (drop != 0 && (huff & mask) != low) {
1101
 
            drop = 0;
1102
 
            len = root;
1103
 
            next = *table;
1104
 
            this.bits = (unsigned char)len;
1105
 
        }
1106
 
 
1107
 
        /* put invalid code marker in table */
1108
 
        next[huff >> drop] = this;
1109
 
 
1110
 
        /* backwards increment the len-bit code huff */
1111
 
        incr = 1U << (len - 1);
1112
 
        while (huff & incr)
1113
 
            incr >>= 1;
1114
 
        if (incr != 0) {
1115
 
            huff &= incr - 1;
1116
 
            huff += incr;
1117
 
        }
1118
 
        else
1119
 
            huff = 0;
1120
 
    }
1121
 
 
1122
 
    /* set return parameters */
1123
 
    *table += used;
1124
 
    *bits = root;
1125
 
    return 0;
1126
 
}