Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/compress/inflate.c

    r5a6cc679 ra35b458  
    103103        size_t destlen;   /**< Output buffer size */
    104104        size_t destcnt;   /**< Position in the output buffer */
    105        
     105
    106106        uint8_t *src;     /**< Input buffer */
    107107        size_t srclen;    /**< Input buffer size */
    108108        size_t srccnt;    /**< Position in the input buffer */
    109        
     109
    110110        uint16_t bitbuf;  /**< Bit buffer */
    111111        size_t bitlen;    /**< Number of bits in the bit buffer */
    112        
     112
    113113        bool overrun;     /**< Overrun condition */
    114114} inflate_state_t;
     
    240240        /* Bit accumulator for at least 20 bits */
    241241        uint32_t val = state->bitbuf;
    242        
     242
    243243        while (state->bitlen < cnt) {
    244244                if (state->srccnt == state->srclen) {
     
    246246                        return 0;
    247247                }
    248                
     248
    249249                /* Load 8 more bits */
    250250                val |= ((uint32_t) state->src[state->srccnt]) << state->bitlen;
     
    252252                state->bitlen += 8;
    253253        }
    254        
     254
    255255        /* Update bits in the buffer */
    256256        state->bitbuf = (uint16_t) (val >> cnt);
    257257        state->bitlen -= cnt;
    258        
     258
    259259        return ((uint16_t) (val & ((1 << cnt) - 1)));
    260260}
     
    275275        state->bitbuf = 0;
    276276        state->bitlen = 0;
    277        
     277
    278278        if (state->srccnt + 4 > state->srclen)
    279279                return ELIMIT;
    280        
     280
    281281        uint16_t len =
    282282            state->src[state->srccnt] | (state->src[state->srccnt + 1] << 8);
    283283        uint16_t len_compl =
    284284            state->src[state->srccnt + 2] | (state->src[state->srccnt + 3] << 8);
    285        
     285
    286286        /* Check block length and its complement */
    287287        if (((int16_t) len) != ~((int16_t) len_compl))
    288288                return EINVAL;
    289        
     289
    290290        state->srccnt += 4;
    291        
     291
    292292        /* Check input buffer size */
    293293        if (state->srccnt + len > state->srclen)
    294294                return ELIMIT;
    295        
     295
    296296        /* Check output buffer size */
    297297        if (state->destcnt + len > state->destlen)
    298298                return ENOMEM;
    299        
     299
    300300        /* Copy data */
    301301        memcpy(state->dest + state->destcnt, state->src + state->srccnt, len);
    302302        state->srccnt += len;
    303303        state->destcnt += len;
    304        
     304
    305305        return EOK;
    306306}
     
    323323        size_t index = 0;  /* Index of the first code of the given length
    324324                              in the symbol table */
    325        
     325
    326326        size_t len;  /* Current number of bits in the code */
    327327        for (len = 1; len <= MAX_HUFFMAN_BIT; len++) {
     
    329329                code |= get_bits(state, 1);
    330330                CHECK_OVERRUN(*state);
    331                
     331
    332332                uint16_t count = huffman->count[len];
    333333                if (code < first + count) {
     
    336336                        return EOK;
    337337                }
    338                
     338
    339339                /* Update for next length */
    340340                index += count;
     
    343343                code <<= 1;
    344344        }
    345        
     345
    346346        return EINVAL;
    347347}
     
    364364        for (len = 0; len <= MAX_HUFFMAN_BIT; len++)
    365365                huffman->count[len] = 0;
    366        
     366
    367367        /* We assume that the lengths are within bounds */
    368368        size_t symbol;
    369369        for (symbol = 0; symbol < n; symbol++)
    370370                huffman->count[length[symbol]]++;
    371        
     371
    372372        if (huffman->count[0] == n) {
    373373                /* The code is complete, but decoding will fail */
    374374                return 0;
    375375        }
    376        
     376
    377377        /* Check for an over-subscribed or incomplete set of lengths */
    378378        int16_t left = 1;
     
    385385                }
    386386        }
    387        
     387
    388388        /* Generate offsets into symbol table */
    389389        uint16_t offs[MAX_HUFFMAN_BIT + 1];
    390        
     390
    391391        offs[1] = 0;
    392392        for (len = 1; len < MAX_HUFFMAN_BIT; len++)
    393393                offs[len + 1] = offs[len] + huffman->count[len];
    394        
     394
    395395        for (symbol = 0; symbol < n; symbol++) {
    396396                if (length[symbol] != 0) {
     
    399399                }
    400400        }
    401        
     401
    402402        return left;
    403403}
     
    422422{
    423423        uint16_t symbol;
    424        
     424
    425425        do {
    426426                errno_t err = huffman_decode(state, len_code, &symbol);
     
    429429                        return err;
    430430                }
    431                
     431
    432432                if (symbol < 256) {
    433433                        /* Write out literal */
    434434                        if (state->destcnt == state->destlen)
    435435                                return ENOMEM;
    436                        
     436
    437437                        state->dest[state->destcnt] = (uint8_t) symbol;
    438438                        state->destcnt++;
     
    442442                        if (symbol >= 29)
    443443                                return EINVAL;
    444                        
     444
    445445                        size_t len = lens[symbol] + get_bits(state, lens_ext[symbol]);
    446446                        CHECK_OVERRUN(*state);
    447                        
     447
    448448                        /* Get distance */
    449449                        err = huffman_decode(state, dist_code, &symbol);
    450450                        if (err != EOK)
    451451                                return err;
    452                        
     452
    453453                        size_t dist = dists[symbol] + get_bits(state, dists_ext[symbol]);
    454454                        if (dist > state->destcnt)
    455455                                return ENOENT;
    456                        
     456
    457457                        if (state->destcnt + len > state->destlen)
    458458                                return ENOMEM;
    459                        
     459
    460460                        while (len > 0) {
    461461                                /* Copy len bytes from distance bytes back */
     
    467467                }
    468468        } while (symbol != 256);
    469        
     469
    470470        return EOK;
    471471}
     
    510510        huffman_t dyn_len_code;
    511511        huffman_t dyn_dist_code;
    512        
     512
    513513        dyn_len_code.count = dyn_len_count;
    514514        dyn_len_code.symbol = dyn_len_symbol;
    515        
     515
    516516        dyn_dist_code.count = dyn_dist_count;
    517517        dyn_dist_code.symbol = dyn_dist_symbol;
    518        
     518
    519519        /* Get number of bits in each table */
    520520        uint16_t nlen = get_bits(state, 5) + 257;
    521521        CHECK_OVERRUN(*state);
    522        
     522
    523523        uint16_t ndist = get_bits(state, 5) + 1;
    524524        CHECK_OVERRUN(*state);
    525        
     525
    526526        uint16_t ncode = get_bits(state, 4) + 4;
    527527        CHECK_OVERRUN(*state);
    528        
     528
    529529        if ((nlen > MAX_LITLEN) || (ndist > MAX_DIST)
    530530            || (ncode > MAX_ORDER))
    531531                return EINVAL;
    532        
     532
    533533        /* Read code length code lengths */
    534534        uint16_t index;
     
    537537                CHECK_OVERRUN(*state);
    538538        }
    539        
     539
    540540        /* Set missing lengths to zero */
    541541        for (index = ncode; index < MAX_ORDER; index++)
    542542                length[order[index]] = 0;
    543        
     543
    544544        /* Build Huffman code */
    545545        int16_t rc = huffman_construct(&dyn_len_code, length, MAX_ORDER);
    546546        if (rc != 0)
    547547                return EINVAL;
    548        
     548
    549549        /* Read length/literal and distance code length tables */
    550550        index = 0;
     
    554554                if (err != EOK)
    555555                        return EOK;
    556                
     556
    557557                if (symbol < 16) {
    558558                        length[index] = symbol;
     
    560560                } else {
    561561                        uint16_t len = 0;
    562                        
     562
    563563                        if (symbol == 16) {
    564564                                if (index == 0)
    565565                                        return EINVAL;
    566                                
     566
    567567                                len = length[index - 1];
    568568                                symbol = get_bits(state, 2) + 3;
     
    575575                                CHECK_OVERRUN(*state);
    576576                        }
    577                        
     577
    578578                        if (index + symbol > nlen + ndist)
    579579                                return EINVAL;
    580                        
     580
    581581                        while (symbol > 0) {
    582582                                length[index] = len;
     
    586586                }
    587587        }
    588        
     588
    589589        /* Check for end-of-block code */
    590590        if (length[256] == 0)
    591591                return EINVAL;
    592        
     592
    593593        /* Build Huffman tables for literal/length codes */
    594594        rc = huffman_construct(&dyn_len_code, length, nlen);
    595595        if ((rc < 0) || ((rc > 0) && (dyn_len_code.count[0] + 1 != nlen)))
    596596                return EINVAL;
    597        
     597
    598598        /* Build Huffman tables for distance codes */
    599599        rc = huffman_construct(&dyn_dist_code, length + nlen, ndist);
    600600        if ((rc < 0) || ((rc > 0) && (dyn_dist_code.count[0] + 1 != ndist)))
    601601                return EINVAL;
    602        
     602
    603603        return inflate_codes(state, &dyn_len_code, &dyn_dist_code);
    604604}
     
    622622        /* Initialize the state */
    623623        inflate_state_t state;
    624        
     624
    625625        state.dest = (uint8_t *) dest;
    626626        state.destlen = destlen;
    627627        state.destcnt = 0;
    628        
     628
    629629        state.src = (uint8_t *) src;
    630630        state.srclen = srclen;
    631631        state.srccnt = 0;
    632        
     632
    633633        state.bitbuf = 0;
    634634        state.bitlen = 0;
    635        
     635
    636636        state.overrun = false;
    637        
     637
    638638        uint16_t last;
    639639        errno_t ret = EOK;
    640        
     640
    641641        do {
    642642                /* Last block is indicated by a non-zero bit */
    643643                last = get_bits(&state, 1);
    644644                CHECK_OVERRUN(state);
    645                
     645
    646646                /* Block type */
    647647                uint16_t type = get_bits(&state, 2);
    648648                CHECK_OVERRUN(state);
    649                
     649
    650650                switch (type) {
    651651                case 0:
     
    662662                }
    663663        } while ((!last) && (ret == 0));
    664        
     664
    665665        return ret;
    666666}
Note: See TracChangeset for help on using the changeset viewer.