~ubuntu-branches/ubuntu/trusty/zlib/trusty

« back to all changes in this revision

Viewing changes to inflate.c

  • Committer: Package Import Robot
  • Author(s): Mark Brown
  • Date: 2012-06-22 16:55:56 UTC
  • mfrom: (1.1.23 sid)
  • Revision ID: package-import@ubuntu.com-20120622165556-9xuc7gnq4w25b3i0
Yet more s390x cleanup.  Thanks to the s390x porters for thei
prompt an efficient buildd monitoring (closes: #678511).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* inflate.c -- zlib decompression
2
 
 * Copyright (C) 1995-2009 Mark Adler
 
2
 * Copyright (C) 1995-2012 Mark Adler
3
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
4
 */
5
5
 
45
45
 * - Rearrange window copies in inflate_fast() for speed and simplification
46
46
 * - Unroll last copy for window match in inflate_fast()
47
47
 * - Use local copies of window variables in inflate_fast() for speed
48
 
 * - Pull out common write == 0 case for speed in inflate_fast()
 
48
 * - Pull out common wnext == 0 case for speed in inflate_fast()
49
49
 * - Make op and len in inflate_fast() unsigned for consistency
50
50
 * - Add FAR to lcode and dcode declarations in inflate_fast()
51
51
 * - Simplified bad distance check in inflate_fast()
100
100
local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
101
101
                              unsigned len));
102
102
 
103
 
int ZEXPORT inflateReset(strm)
 
103
int ZEXPORT inflateResetKeep(strm)
104
104
z_streamp strm;
105
105
{
106
106
    struct inflate_state FAR *state;
109
109
    state = (struct inflate_state FAR *)strm->state;
110
110
    strm->total_in = strm->total_out = state->total = 0;
111
111
    strm->msg = Z_NULL;
112
 
    strm->adler = 1;        /* to support ill-conceived Java test suite */
 
112
    if (state->wrap)        /* to support ill-conceived Java test suite */
 
113
        strm->adler = state->wrap & 1;
113
114
    state->mode = HEAD;
114
115
    state->last = 0;
115
116
    state->havedict = 0;
116
117
    state->dmax = 32768U;
117
118
    state->head = Z_NULL;
118
 
    state->wsize = 0;
119
 
    state->whave = 0;
120
 
    state->write = 0;
121
119
    state->hold = 0;
122
120
    state->bits = 0;
123
121
    state->lencode = state->distcode = state->next = state->codes;
127
125
    return Z_OK;
128
126
}
129
127
 
 
128
int ZEXPORT inflateReset(strm)
 
129
z_streamp strm;
 
130
{
 
131
    struct inflate_state FAR *state;
 
132
 
 
133
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
 
134
    state = (struct inflate_state FAR *)strm->state;
 
135
    state->wsize = 0;
 
136
    state->whave = 0;
 
137
    state->wnext = 0;
 
138
    return inflateResetKeep(strm);
 
139
}
 
140
 
130
141
int ZEXPORT inflateReset2(strm, windowBits)
131
142
z_streamp strm;
132
143
int windowBits;
152
163
    }
153
164
 
154
165
    /* set number of window bits, free window if different */
155
 
    if (windowBits < 8 || windowBits > 15)
 
166
    if (windowBits && (windowBits < 8 || windowBits > 15))
156
167
        return Z_STREAM_ERROR;
157
 
    if (state->wbits != windowBits && state->window != Z_NULL) {
 
168
    if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
158
169
        ZFREE(strm, state->window);
159
170
        state->window = Z_NULL;
160
171
    }
180
191
    if (strm == Z_NULL) return Z_STREAM_ERROR;
181
192
    strm->msg = Z_NULL;                 /* in case we return an error */
182
193
    if (strm->zalloc == (alloc_func)0) {
 
194
#ifdef Z_SOLO
 
195
        return Z_STREAM_ERROR;
 
196
#else
183
197
        strm->zalloc = zcalloc;
184
198
        strm->opaque = (voidpf)0;
 
199
#endif
185
200
    }
186
 
    if (strm->zfree == (free_func)0) strm->zfree = zcfree;
 
201
    if (strm->zfree == (free_func)0)
 
202
#ifdef Z_SOLO
 
203
        return Z_STREAM_ERROR;
 
204
#else
 
205
        strm->zfree = zcfree;
 
206
#endif
187
207
    state = (struct inflate_state FAR *)
188
208
            ZALLOC(strm, 1, sizeof(struct inflate_state));
189
209
    if (state == Z_NULL) return Z_MEM_ERROR;
321
341
    low = 0;
322
342
    for (;;) {
323
343
        if ((low % 7) == 0) printf("\n        ");
324
 
        printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
325
 
               state.lencode[low].val);
 
344
        printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
 
345
               state.lencode[low].bits, state.lencode[low].val);
326
346
        if (++low == size) break;
327
347
        putchar(',');
328
348
    }
375
395
    /* if window not in use yet, initialize */
376
396
    if (state->wsize == 0) {
377
397
        state->wsize = 1U << state->wbits;
378
 
        state->write = 0;
 
398
        state->wnext = 0;
379
399
        state->whave = 0;
380
400
    }
381
401
 
383
403
    copy = out - strm->avail_out;
384
404
    if (copy >= state->wsize) {
385
405
        zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
386
 
        state->write = 0;
 
406
        state->wnext = 0;
387
407
        state->whave = state->wsize;
388
408
    }
389
409
    else {
390
 
        dist = state->wsize - state->write;
 
410
        dist = state->wsize - state->wnext;
391
411
        if (dist > copy) dist = copy;
392
 
        zmemcpy(state->window + state->write, strm->next_out - copy, dist);
 
412
        zmemcpy(state->window + state->wnext, strm->next_out - copy, dist);
393
413
        copy -= dist;
394
414
        if (copy) {
395
415
            zmemcpy(state->window, strm->next_out - copy, copy);
396
 
            state->write = copy;
 
416
            state->wnext = copy;
397
417
            state->whave = state->wsize;
398
418
        }
399
419
        else {
400
 
            state->write += dist;
401
 
            if (state->write == state->wsize) state->write = 0;
 
420
            state->wnext += dist;
 
421
            if (state->wnext == state->wsize) state->wnext = 0;
402
422
            if (state->whave < state->wsize) state->whave += dist;
403
423
        }
404
424
    }
499
519
        bits -= bits & 7; \
500
520
    } while (0)
501
521
 
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
 
 
507
522
/*
508
523
   inflate() uses a state machine to process as much input data and generate as
509
524
   much output data as possible before returning.  The state machine is
654
669
            }
655
670
            DROPBITS(4);
656
671
            len = BITS(4) + 8;
657
 
            if (len > state->wbits) {
 
672
            if (state->wbits == 0)
 
673
                state->wbits = len;
 
674
            else if (len > state->wbits) {
658
675
                strm->msg = (char *)"invalid window size";
659
676
                state->mode = BAD;
660
677
                break;
795
812
#endif
796
813
        case DICTID:
797
814
            NEEDBITS(32);
798
 
            strm->adler = state->check = REVERSE(hold);
 
815
            strm->adler = state->check = ZSWAP32(hold);
799
816
            INITBITS();
800
817
            state->mode = DICT;
801
818
        case DICT:
923
940
                    PULLBYTE();
924
941
                }
925
942
                if (here.val < 16) {
926
 
                    NEEDBITS(here.bits);
927
943
                    DROPBITS(here.bits);
928
944
                    state->lens[state->have++] = here.val;
929
945
                }
1128
1144
                    break;
1129
1145
#endif
1130
1146
                }
1131
 
                if (copy > state->write) {
1132
 
                    copy -= state->write;
1133
 
                    /* %% problem here if copy > state->wsize -- avoid? */
1134
 
                    /* %% or can (state->window + state->wsize) - copy */
1135
 
                    /* %% but really should detect and reject this case */
 
1147
                if (copy > state->wnext) {
 
1148
                    copy -= state->wnext;
1136
1149
                    from = state->window + (state->wsize - copy);
1137
1150
                }
1138
1151
                else
1139
 
                    from = state->window + (state->write - copy);
 
1152
                    from = state->window + (state->wnext - copy);
1140
1153
                if (copy > state->length) copy = state->length;
1141
1154
            }
1142
1155
            else {                              /* copy from output */
1171
1184
#ifdef GUNZIP
1172
1185
                     state->flags ? hold :
1173
1186
#endif
1174
 
                     REVERSE(hold)) != state->check) {
 
1187
                     ZSWAP32(hold)) != state->check) {
1175
1188
                    strm->msg = (char *)"incorrect data check";
1176
1189
                    state->mode = BAD;
1177
1190
                    break;
1215
1228
     */
1216
1229
  inf_leave:
1217
1230
    RESTORE();
1218
 
    if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
 
1231
    if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
 
1232
            (state->mode < CHECK || flush != Z_FINISH)))
1219
1233
        if (updatewindow(strm, out)) {
1220
1234
            state->mode = MEM;
1221
1235
            return Z_MEM_ERROR;
1256
1270
uInt dictLength;
1257
1271
{
1258
1272
    struct inflate_state FAR *state;
1259
 
    unsigned long id;
 
1273
    unsigned long dictid;
 
1274
    unsigned char *next;
 
1275
    unsigned avail;
 
1276
    int ret;
1260
1277
 
1261
1278
    /* check state */
1262
1279
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1264
1281
    if (state->wrap != 0 && state->mode != DICT)
1265
1282
        return Z_STREAM_ERROR;
1266
1283
 
1267
 
    /* check for correct dictionary id */
 
1284
    /* check for correct dictionary identifier */
1268
1285
    if (state->mode == DICT) {
1269
 
        id = adler32(0L, Z_NULL, 0);
1270
 
        id = adler32(id, dictionary, dictLength);
1271
 
        if (id != state->check)
 
1286
        dictid = adler32(0L, Z_NULL, 0);
 
1287
        dictid = adler32(dictid, dictionary, dictLength);
 
1288
        if (dictid != state->check)
1272
1289
            return Z_DATA_ERROR;
1273
1290
    }
1274
1291
 
1275
 
    /* copy dictionary to window */
1276
 
    if (updatewindow(strm, strm->avail_out)) {
 
1292
    /* copy dictionary to window using updatewindow(), which will amend the
 
1293
       existing dictionary if appropriate */
 
1294
    next = strm->next_out;
 
1295
    avail = strm->avail_out;
 
1296
    strm->next_out = (Bytef *)dictionary + dictLength;
 
1297
    strm->avail_out = 0;
 
1298
    ret = updatewindow(strm, dictLength);
 
1299
    strm->avail_out = avail;
 
1300
    strm->next_out = next;
 
1301
    if (ret) {
1277
1302
        state->mode = MEM;
1278
1303
        return Z_MEM_ERROR;
1279
1304
    }
1280
 
    if (dictLength > state->wsize) {
1281
 
        zmemcpy(state->window, dictionary + dictLength - state->wsize,
1282
 
                state->wsize);
1283
 
        state->whave = state->wsize;
1284
 
    }
1285
 
    else {
1286
 
        zmemcpy(state->window + state->wsize - dictLength, dictionary,
1287
 
                dictLength);
1288
 
        state->whave = dictLength;
1289
 
    }
1290
1305
    state->havedict = 1;
1291
1306
    Tracev((stderr, "inflate:   dictionary set\n"));
1292
1307
    return Z_OK;
1434
1449
    }
1435
1450
 
1436
1451
    /* copy state */
1437
 
    zmemcpy(dest, source, sizeof(z_stream));
1438
 
    zmemcpy(copy, state, sizeof(struct inflate_state));
 
1452
    zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
 
1453
    zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1439
1454
    if (state->lencode >= state->codes &&
1440
1455
        state->lencode <= state->codes + ENOUGH - 1) {
1441
1456
        copy->lencode = copy->codes + (state->lencode - state->codes);
1459
1474
 
1460
1475
    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1461
1476
    state = (struct inflate_state FAR *)strm->state;
1462
 
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1463
1477
    state->sane = !subvert;
 
1478
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1464
1479
    return Z_OK;
1465
1480
#else
1466
1481
    state->sane = 1;