Subversion Repositories Kolibri OS

Rev

Rev 1896 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1896 Rev 3926
Line 1... Line 1...
1
/* inflate.c -- zlib decompression
1
/* inflate.c -- zlib decompression
2
 * Copyright (C) 1995-2010 Mark Adler
2
 * Copyright (C) 1995-2012 Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
4
 */
Line 5... Line 5...
5
 
5
 
6
/*
6
/*
Line 91... Line 91...
91
#  endif
91
#  endif
92
#endif
92
#endif
Line 93... Line 93...
93
 
93
 
94
/* function prototypes */
94
/* function prototypes */
95
local void fixedtables OF((struct inflate_state FAR *state));
95
local void fixedtables OF((struct inflate_state FAR *state));
-
 
96
local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
96
local int updatewindow OF((z_streamp strm, unsigned out));
97
                           unsigned copy));
97
#ifdef BUILDFIXED
98
#ifdef BUILDFIXED
98
   void makefixed OF((void));
99
   void makefixed OF((void));
99
#endif
100
#endif
100
local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
101
local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
Line 101... Line 102...
101
                              unsigned len));
102
                              unsigned len));
102
 
103
 
103
int ZEXPORT inflateReset(strm)
104
int ZEXPORT inflateResetKeep(strm)
104
z_streamp strm;
105
z_streamp strm;
Line 105... Line 106...
105
{
106
{
106
    struct inflate_state FAR *state;
107
    struct inflate_state FAR *state;
107
 
108
 
108
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
109
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
109
    state = (struct inflate_state FAR *)strm->state;
110
    state = (struct inflate_state FAR *)strm->state;
-
 
111
    strm->total_in = strm->total_out = state->total = 0;
110
    strm->total_in = strm->total_out = state->total = 0;
112
    strm->msg = Z_NULL;
111
    strm->msg = Z_NULL;
113
    if (state->wrap)        /* to support ill-conceived Java test suite */
112
    strm->adler = 1;        /* to support ill-conceived Java test suite */
114
        strm->adler = state->wrap & 1;
113
    state->mode = HEAD;
115
    state->mode = HEAD;
114
    state->last = 0;
116
    state->last = 0;
115
    state->havedict = 0;
-
 
116
    state->dmax = 32768U;
-
 
117
    state->head = Z_NULL;
-
 
118
    state->wsize = 0;
117
    state->havedict = 0;
119
    state->whave = 0;
118
    state->dmax = 32768U;
120
    state->wnext = 0;
119
    state->head = Z_NULL;
121
    state->hold = 0;
120
    state->hold = 0;
122
    state->bits = 0;
121
    state->bits = 0;
123
    state->lencode = state->distcode = state->next = state->codes;
122
    state->lencode = state->distcode = state->next = state->codes;
124
    state->sane = 1;
123
    state->sane = 1;
125
    state->back = -1;
124
    state->back = -1;
Line -... Line 125...
-
 
125
    Tracev((stderr, "inflate: reset\n"));
-
 
126
    return Z_OK;
-
 
127
}
-
 
128
 
-
 
129
int ZEXPORT inflateReset(strm)
-
 
130
z_streamp strm;
-
 
131
{
-
 
132
    struct inflate_state FAR *state;
-
 
133
 
-
 
134
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
-
 
135
    state = (struct inflate_state FAR *)strm->state;
-
 
136
    state->wsize = 0;
-
 
137
    state->whave = 0;
126
    Tracev((stderr, "inflate: reset\n"));
138
    state->wnext = 0;
127
    return Z_OK;
139
    return inflateResetKeep(strm);
128
}
140
}
129
 
141
 
130
int ZEXPORT inflateReset2(strm, windowBits)
142
int ZEXPORT inflateReset2(strm, windowBits)
Line 178... Line 190...
178
        stream_size != (int)(sizeof(z_stream)))
190
        stream_size != (int)(sizeof(z_stream)))
179
        return Z_VERSION_ERROR;
191
        return Z_VERSION_ERROR;
180
    if (strm == Z_NULL) return Z_STREAM_ERROR;
192
    if (strm == Z_NULL) return Z_STREAM_ERROR;
181
    strm->msg = Z_NULL;                 /* in case we return an error */
193
    strm->msg = Z_NULL;                 /* in case we return an error */
182
    if (strm->zalloc == (alloc_func)0) {
194
    if (strm->zalloc == (alloc_func)0) {
-
 
195
#ifdef Z_SOLO
-
 
196
        return Z_STREAM_ERROR;
-
 
197
#else
183
        strm->zalloc = zcalloc;
198
        strm->zalloc = zcalloc;
184
        strm->opaque = (voidpf)0;
199
        strm->opaque = (voidpf)0;
-
 
200
#endif
185
    }
201
    }
186
    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
202
    if (strm->zfree == (free_func)0)
-
 
203
#ifdef Z_SOLO
-
 
204
        return Z_STREAM_ERROR;
-
 
205
#else
-
 
206
        strm->zfree = zcfree;
-
 
207
#endif
187
    state = (struct inflate_state FAR *)
208
    state = (struct inflate_state FAR *)
188
            ZALLOC(strm, 1, sizeof(struct inflate_state));
209
            ZALLOC(strm, 1, sizeof(struct inflate_state));
189
    if (state == Z_NULL) return Z_MEM_ERROR;
210
    if (state == Z_NULL) return Z_MEM_ERROR;
190
    Tracev((stderr, "inflate: allocated\n"));
211
    Tracev((stderr, "inflate: allocated\n"));
191
    strm->state = (struct internal_state FAR *)state;
212
    strm->state = (struct internal_state FAR *)state;
Line 319... Line 340...
319
    size = 1U << 9;
340
    size = 1U << 9;
320
    printf("    static const code lenfix[%u] = {", size);
341
    printf("    static const code lenfix[%u] = {", size);
321
    low = 0;
342
    low = 0;
322
    for (;;) {
343
    for (;;) {
323
        if ((low % 7) == 0) printf("\n        ");
344
        if ((low % 7) == 0) printf("\n        ");
324
        printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
345
        printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
325
               state.lencode[low].val);
346
               state.lencode[low].bits, state.lencode[low].val);
326
        if (++low == size) break;
347
        if (++low == size) break;
327
        putchar(',');
348
        putchar(',');
328
    }
349
    }
329
    puts("\n    };");
350
    puts("\n    };");
330
    size = 1U << 5;
351
    size = 1U << 5;
Line 353... Line 374...
353
   advantage, since only the last 32K of output is copied to the sliding window
374
   advantage, since only the last 32K of output is copied to the sliding window
354
   upon return from inflate(), and since all distances after the first 32K of
375
   upon return from inflate(), and since all distances after the first 32K of
355
   output will fall in the output data, making match copies simpler and faster.
376
   output will fall in the output data, making match copies simpler and faster.
356
   The advantage may be dependent on the size of the processor's data caches.
377
   The advantage may be dependent on the size of the processor's data caches.
357
 */
378
 */
358
local int updatewindow(strm, out)
379
local int updatewindow(strm, end, copy)
359
z_streamp strm;
380
z_streamp strm;
-
 
381
const Bytef *end;
360
unsigned out;
382
unsigned copy;
361
{
383
{
362
    struct inflate_state FAR *state;
384
    struct inflate_state FAR *state;
363
    unsigned copy, dist;
385
    unsigned dist;
Line 364... Line 386...
364
 
386
 
Line 365... Line 387...
365
    state = (struct inflate_state FAR *)strm->state;
387
    state = (struct inflate_state FAR *)strm->state;
366
 
388
 
Line 378... Line 400...
378
        state->wnext = 0;
400
        state->wnext = 0;
379
        state->whave = 0;
401
        state->whave = 0;
380
    }
402
    }
Line 381... Line 403...
381
 
403
 
382
    /* copy state->wsize or less output bytes into the circular window */
-
 
383
    copy = out - strm->avail_out;
404
    /* copy state->wsize or less output bytes into the circular window */
384
    if (copy >= state->wsize) {
405
    if (copy >= state->wsize) {
385
        zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
406
        zmemcpy(state->window, end - state->wsize, state->wsize);
386
        state->wnext = 0;
407
        state->wnext = 0;
387
        state->whave = state->wsize;
408
        state->whave = state->wsize;
388
    }
409
    }
389
    else {
410
    else {
390
        dist = state->wsize - state->wnext;
411
        dist = state->wsize - state->wnext;
391
        if (dist > copy) dist = copy;
412
        if (dist > copy) dist = copy;
392
        zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
413
        zmemcpy(state->window + state->wnext, end - copy, dist);
393
        copy -= dist;
414
        copy -= dist;
394
        if (copy) {
415
        if (copy) {
395
            zmemcpy(state->window, strm->next_out - copy, copy);
416
            zmemcpy(state->window, end - copy, copy);
396
            state->wnext = copy;
417
            state->wnext = copy;
397
            state->whave = state->wsize;
418
            state->whave = state->wsize;
398
        }
419
        }
399
        else {
420
        else {
Line 497... Line 518...
497
    do { \
518
    do { \
498
        hold >>= bits & 7; \
519
        hold >>= bits & 7; \
499
        bits -= bits & 7; \
520
        bits -= bits & 7; \
500
    } while (0)
521
    } while (0)
Line 501... Line -...
501
 
-
 
502
/* Reverse the bytes in a 32-bit value */
-
 
503
#define REVERSE(q) \
-
 
504
    ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
-
 
505
     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
-
 
506
 
522
 
507
/*
523
/*
508
   inflate() uses a state machine to process as much input data and generate as
524
   inflate() uses a state machine to process as much input data and generate as
509
   much output data as possible before returning.  The state machine is
525
   much output data as possible before returning.  The state machine is
Line 589... Line 605...
589
int ZEXPORT inflate(strm, flush)
605
int ZEXPORT inflate(strm, flush)
590
z_streamp strm;
606
z_streamp strm;
591
int flush;
607
int flush;
592
{
608
{
593
    struct inflate_state FAR *state;
609
    struct inflate_state FAR *state;
594
    unsigned char FAR *next;    /* next input */
610
    z_const unsigned char FAR *next;    /* next input */
595
    unsigned char FAR *put;     /* next output */
611
    unsigned char FAR *put;     /* next output */
596
    unsigned have, left;        /* available input and output */
612
    unsigned have, left;        /* available input and output */
597
    unsigned long hold;         /* bit buffer */
613
    unsigned long hold;         /* bit buffer */
598
    unsigned bits;              /* bits in bit buffer */
614
    unsigned bits;              /* bits in bit buffer */
599
    unsigned in, out;           /* save starting available input and output */
615
    unsigned in, out;           /* save starting available input and output */
Line 795... Line 811...
795
            state->mode = TYPE;
811
            state->mode = TYPE;
796
            break;
812
            break;
797
#endif
813
#endif
798
        case DICTID:
814
        case DICTID:
799
            NEEDBITS(32);
815
            NEEDBITS(32);
800
            strm->adler = state->check = REVERSE(hold);
816
            strm->adler = state->check = ZSWAP32(hold);
801
            INITBITS();
817
            INITBITS();
802
            state->mode = DICT;
818
            state->mode = DICT;
803
        case DICT:
819
        case DICT:
804
            if (state->havedict == 0) {
820
            if (state->havedict == 0) {
805
                RESTORE();
821
                RESTORE();
Line 903... Line 919...
903
                DROPBITS(3);
919
                DROPBITS(3);
904
            }
920
            }
905
            while (state->have < 19)
921
            while (state->have < 19)
906
                state->lens[order[state->have++]] = 0;
922
                state->lens[order[state->have++]] = 0;
907
            state->next = state->codes;
923
            state->next = state->codes;
908
            state->lencode = (code const FAR *)(state->next);
924
            state->lencode = (const code FAR *)(state->next);
909
            state->lenbits = 7;
925
            state->lenbits = 7;
910
            ret = inflate_table(CODES, state->lens, 19, &(state->next),
926
            ret = inflate_table(CODES, state->lens, 19, &(state->next),
911
                                &(state->lenbits), state->work);
927
                                &(state->lenbits), state->work);
912
            if (ret) {
928
            if (ret) {
913
                strm->msg = (char *)"invalid code lengths set";
929
                strm->msg = (char *)"invalid code lengths set";
Line 923... Line 939...
923
                    here = state->lencode[BITS(state->lenbits)];
939
                    here = state->lencode[BITS(state->lenbits)];
924
                    if ((unsigned)(here.bits) <= bits) break;
940
                    if ((unsigned)(here.bits) <= bits) break;
925
                    PULLBYTE();
941
                    PULLBYTE();
926
                }
942
                }
927
                if (here.val < 16) {
943
                if (here.val < 16) {
928
                    NEEDBITS(here.bits);
-
 
929
                    DROPBITS(here.bits);
944
                    DROPBITS(here.bits);
930
                    state->lens[state->have++] = here.val;
945
                    state->lens[state->have++] = here.val;
931
                }
946
                }
932
                else {
947
                else {
933
                    if (here.val == 16) {
948
                    if (here.val == 16) {
Line 978... Line 993...
978
 
993
 
979
            /* build code tables -- note: do not change the lenbits or distbits
994
            /* build code tables -- note: do not change the lenbits or distbits
980
               values here (9 and 6) without reading the comments in inftrees.h
995
               values here (9 and 6) without reading the comments in inftrees.h
981
               concerning the ENOUGH constants, which depend on those values */
996
               concerning the ENOUGH constants, which depend on those values */
982
            state->next = state->codes;
997
            state->next = state->codes;
983
            state->lencode = (code const FAR *)(state->next);
998
            state->lencode = (const code FAR *)(state->next);
984
            state->lenbits = 9;
999
            state->lenbits = 9;
985
            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1000
            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
986
                                &(state->lenbits), state->work);
1001
                                &(state->lenbits), state->work);
987
            if (ret) {
1002
            if (ret) {
988
                strm->msg = (char *)"invalid literal/lengths set";
1003
                strm->msg = (char *)"invalid literal/lengths set";
989
                state->mode = BAD;
1004
                state->mode = BAD;
990
                break;
1005
                break;
991
            }
1006
            }
992
            state->distcode = (code const FAR *)(state->next);
1007
            state->distcode = (const code FAR *)(state->next);
993
            state->distbits = 6;
1008
            state->distbits = 6;
994
            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1009
            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
995
                            &(state->next), &(state->distbits), state->work);
1010
                            &(state->next), &(state->distbits), state->work);
996
            if (ret) {
1011
            if (ret) {
Line 1168... Line 1183...
1168
                out = left;
1183
                out = left;
1169
                if ((
1184
                if ((
1170
#ifdef GUNZIP
1185
#ifdef GUNZIP
1171
                     state->flags ? hold :
1186
                     state->flags ? hold :
1172
#endif
1187
#endif
1173
                     REVERSE(hold)) != state->check) {
1188
                     ZSWAP32(hold)) != state->check) {
1174
                    strm->msg = (char *)"incorrect data check";
1189
                    strm->msg = (char *)"incorrect data check";
1175
                    state->mode = BAD;
1190
                    state->mode = BAD;
1176
                    break;
1191
                    break;
1177
                }
1192
                }
1178
                INITBITS();
1193
                INITBITS();
Line 1212... Line 1227...
1212
       error.  Call updatewindow() to create and/or update the window state.
1227
       error.  Call updatewindow() to create and/or update the window state.
1213
       Note: a memory error from inflate() is non-recoverable.
1228
       Note: a memory error from inflate() is non-recoverable.
1214
     */
1229
     */
1215
  inf_leave:
1230
  inf_leave:
1216
    RESTORE();
1231
    RESTORE();
1217
    if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
1232
    if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
-
 
1233
            (state->mode < CHECK || flush != Z_FINISH)))
1218
        if (updatewindow(strm, out)) {
1234
        if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1219
            state->mode = MEM;
1235
            state->mode = MEM;
1220
            return Z_MEM_ERROR;
1236
            return Z_MEM_ERROR;
1221
        }
1237
        }
1222
    in -= strm->avail_in;
1238
    in -= strm->avail_in;
1223
    out -= strm->avail_out;
1239
    out -= strm->avail_out;
Line 1247... Line 1263...
1247
    strm->state = Z_NULL;
1263
    strm->state = Z_NULL;
1248
    Tracev((stderr, "inflate: end\n"));
1264
    Tracev((stderr, "inflate: end\n"));
1249
    return Z_OK;
1265
    return Z_OK;
1250
}
1266
}
Line -... Line 1267...
-
 
1267
 
-
 
1268
int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
-
 
1269
z_streamp strm;
-
 
1270
Bytef *dictionary;
-
 
1271
uInt *dictLength;
-
 
1272
{
-
 
1273
    struct inflate_state FAR *state;
-
 
1274
 
-
 
1275
    /* check state */
-
 
1276
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
-
 
1277
    state = (struct inflate_state FAR *)strm->state;
-
 
1278
 
-
 
1279
    /* copy dictionary */
-
 
1280
    if (state->whave && dictionary != Z_NULL) {
-
 
1281
        zmemcpy(dictionary, state->window + state->wnext,
-
 
1282
                state->whave - state->wnext);
-
 
1283
        zmemcpy(dictionary + state->whave - state->wnext,
-
 
1284
                state->window, state->wnext);
-
 
1285
    }
-
 
1286
    if (dictLength != Z_NULL)
-
 
1287
        *dictLength = state->whave;
-
 
1288
    return Z_OK;
-
 
1289
}
1251
 
1290
 
1252
int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1291
int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1253
z_streamp strm;
1292
z_streamp strm;
1254
const Bytef *dictionary;
1293
const Bytef *dictionary;
1255
uInt dictLength;
1294
uInt dictLength;
1256
{
1295
{
1257
    struct inflate_state FAR *state;
1296
    struct inflate_state FAR *state;
-
 
1297
    unsigned long dictid;
Line 1258... Line 1298...
1258
    unsigned long id;
1298
    int ret;
1259
 
1299
 
1260
    /* check state */
1300
    /* check state */
1261
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1301
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1262
    state = (struct inflate_state FAR *)strm->state;
1302
    state = (struct inflate_state FAR *)strm->state;
Line 1263... Line 1303...
1263
    if (state->wrap != 0 && state->mode != DICT)
1303
    if (state->wrap != 0 && state->mode != DICT)
1264
        return Z_STREAM_ERROR;
1304
        return Z_STREAM_ERROR;
1265
 
1305
 
1266
    /* check for correct dictionary id */
1306
    /* check for correct dictionary identifier */
1267
    if (state->mode == DICT) {
1307
    if (state->mode == DICT) {
1268
        id = adler32(0L, Z_NULL, 0);
1308
        dictid = adler32(0L, Z_NULL, 0);
1269
        id = adler32(id, dictionary, dictLength);
1309
        dictid = adler32(dictid, dictionary, dictLength);
Line -... Line 1310...
-
 
1310
        if (dictid != state->check)
1270
        if (id != state->check)
1311
            return Z_DATA_ERROR;
1271
            return Z_DATA_ERROR;
1312
    }
-
 
1313
 
1272
    }
1314
    /* copy dictionary to window using updatewindow(), which will amend the
1273
 
1315
       existing dictionary if appropriate */
1274
    /* copy dictionary to window */
1316
    ret = updatewindow(strm, dictionary + dictLength, dictLength);
1275
    if (updatewindow(strm, strm->avail_out)) {
-
 
1276
        state->mode = MEM;
-
 
1277
        return Z_MEM_ERROR;
-
 
1278
    }
-
 
1279
    if (dictLength > state->wsize) {
-
 
1280
        zmemcpy(state->window, dictionary + dictLength - state->wsize,
-
 
1281
                state->wsize);
-
 
1282
        state->whave = state->wsize;
-
 
1283
    }
-
 
1284
    else {
-
 
1285
        zmemcpy(state->window + state->wsize - dictLength, dictionary,
1317
    if (ret) {
1286
                dictLength);
1318
        state->mode = MEM;
1287
        state->whave = dictLength;
1319
        return Z_MEM_ERROR;
1288
    }
1320
    }
Line 1319... Line 1351...
1319
   called again with more data and the *have state.  *have is initialized to
1351
   called again with more data and the *have state.  *have is initialized to
1320
   zero for the first call.
1352
   zero for the first call.
1321
 */
1353
 */
1322
local unsigned syncsearch(have, buf, len)
1354
local unsigned syncsearch(have, buf, len)
1323
unsigned FAR *have;
1355
unsigned FAR *have;
1324
unsigned char FAR *buf;
1356
const unsigned char FAR *buf;
1325
unsigned len;
1357
unsigned len;
1326
{
1358
{
1327
    unsigned got;
1359
    unsigned got;
1328
    unsigned next;
1360
    unsigned next;
Line 1431... Line 1463...
1431
            return Z_MEM_ERROR;
1463
            return Z_MEM_ERROR;
1432
        }
1464
        }
1433
    }
1465
    }
Line 1434... Line 1466...
1434
 
1466
 
1435
    /* copy state */
1467
    /* copy state */
1436
    zmemcpy(dest, source, sizeof(z_stream));
1468
    zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1437
    zmemcpy(copy, state, sizeof(struct inflate_state));
1469
    zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1438
    if (state->lencode >= state->codes &&
1470
    if (state->lencode >= state->codes &&
1439
        state->lencode <= state->codes + ENOUGH - 1) {
1471
        state->lencode <= state->codes + ENOUGH - 1) {
1440
        copy->lencode = copy->codes + (state->lencode - state->codes);
1472
        copy->lencode = copy->codes + (state->lencode - state->codes);
1441
        copy->distcode = copy->codes + (state->distcode - state->codes);
1473
        copy->distcode = copy->codes + (state->distcode - state->codes);