rEFIt_UEFI/libeg/lodepng.cpp
/*inflate a block with dynamic of fixed Huffman tree. btype must be 1 or 2.*/
inflate a block with dynamic of fixed Huffman tree. btype must be 1 or 2.
inflate a block with dynamic of fixed Huffman tree . btype must be 1 or 2 .
static unsigned inflateHuffmanBlock(ucvector* out, size_t* pos, LodePNGBitReader* reader,
                                    unsigned btype) {
  unsigned error = 0;
  HuffmanTree tree_ll; 
  HuffmanTree tree_d; 
  HuffmanTree_init(&tree_ll);
  HuffmanTree_init(&tree_d);
  if(btype == 1) error = getTreeInflateFixed(&tree_ll, &tree_d);
  else  error = getTreeInflateDynamic(&tree_ll, &tree_d, reader);
  while(!error)  {
    unsigned code_ll;
    ensureBits25(reader, 20); 
    code_ll = huffmanDecodeSymbol(reader, &tree_ll);
    if(code_ll <= 255)  {
      if(!ucvector_resize(out, (*pos) + 1)) ERROR_BREAK(83 );
      out->data[*pos] = (unsigned char)code_ll;
      ++(*pos);
    } else if(code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX)  {
      unsigned code_d, distance;
      unsigned numextrabits_l, numextrabits_d; 
      size_t start, backward, length;
      length = LENGTHBASE[code_ll - FIRST_LENGTH_CODE_INDEX];
      numextrabits_l = LENGTHEXTRA[code_ll - FIRST_LENGTH_CODE_INDEX];
      if(numextrabits_l != 0) {
        length += readBits(reader, numextrabits_l);
      }
      ensureBits32(reader, 28); 
      code_d = huffmanDecodeSymbol(reader, &tree_d);
      if(code_d > 29) {
        if(code_d <= 31) {
          ERROR_BREAK(18); 
        } else {
          ERROR_BREAK(16); 
        }
      }
      distance = DISTANCEBASE[code_d];
      numextrabits_d = DISTANCEEXTRA[code_d];
      if(numextrabits_d != 0) {
        distance += readBits(reader, numextrabits_d);
      }
      start = (*pos);
      if(distance > start) ERROR_BREAK(52); 
      backward = start - distance;
      if(!ucvector_resize(out, (*pos) + length)) ERROR_BREAK(83 );
      if(distance < length) {
        size_t forward;
        lodepng_memcpy(out->data + *pos, out->data + backward, distance);
        *pos += distance;
        for(forward = distance; forward < length; ++forward) {
          out->data[(*pos)++] = out->data[backward++];
        }
      } else {
        lodepng_memcpy(out->data + *pos, out->data + backward, length);
        *pos += length;
      }
    } else if(code_ll == 256) {
      break; 
    } else  {
      ERROR_BREAK(16); 
    }
    if(reader->bp > reader->bitsize) {
      ERROR_BREAK(51); 
    }
  }
  HuffmanTree_cleanup(&tree_ll);
  HuffmanTree_cleanup(&tree_d);
  return error;
}
static unsigned inflateHuffmanBlock ( ucvector * out , size_t * pos , LodePNGBitReader * reader , unsigned btype ) { unsigned error = 0 ; HuffmanTree tree_ll ; HuffmanTree tree_d ; HuffmanTree_init ( & tree_ll ) ; HuffmanTree_init ( & tree_d ) ; if ( btype == 1 ) error = getTreeInflateFixed ( & tree_ll , & tree_d ) ; else error = getTreeInflateDynamic ( & tree_ll , & tree_d , reader ) ; while ( ! error ) { unsigned code_ll ; ensureBits25 ( reader , 20 ) ; code_ll = huffmanDecodeSymbol ( reader , & tree_ll ) ; if ( code_ll <= 255 ) { if ( ! ucvector_resize ( out , ( * pos ) + 1 ) ) ERROR_BREAK ( 83 ) ; out -> data [ * pos ] = ( unsigned char ) code_ll ; ++ ( * pos ) ; } else if ( code_ll >= FIRST_LENGTH_CODE_INDEX && code_ll <= LAST_LENGTH_CODE_INDEX ) { unsigned code_d , distance ; unsigned numextrabits_l , numextrabits_d ; size_t start , backward , length ; length = LENGTHBASE [ code_ll - FIRST_LENGTH_CODE_INDEX ] ; numextrabits_l = LENGTHEXTRA [ code_ll - FIRST_LENGTH_CODE_INDEX ] ; if ( numextrabits_l != 0 ) { length += readBits ( reader , numextrabits_l ) ; } ensureBits32 ( reader , 28 ) ; code_d = huffmanDecodeSymbol ( reader , & tree_d ) ; if ( code_d > 29 ) { if ( code_d <= 31 ) { ERROR_BREAK ( 18 ) ; } else { ERROR_BREAK ( 16 ) ; } } distance = DISTANCEBASE [ code_d ] ; numextrabits_d = DISTANCEEXTRA [ code_d ] ; if ( numextrabits_d != 0 ) { distance += readBits ( reader , numextrabits_d ) ; } start = ( * pos ) ; if ( distance > start ) ERROR_BREAK ( 52 ) ; backward = start - distance ; if ( ! ucvector_resize ( out , ( * pos ) + length ) ) ERROR_BREAK ( 83 ) ; if ( distance < length ) { size_t forward ; lodepng_memcpy ( out -> data + * pos , out -> data + backward , distance ) ; * pos += distance ; for ( forward = distance ; forward < length ; ++ forward ) { out -> data [ ( * pos ) ++ ] = out -> data [ backward ++ ] ; } } else { lodepng_memcpy ( out -> data + * pos , out -> data + backward , length ) ; * pos += length ; } } else if ( code_ll == 256 ) { break ; } else { ERROR_BREAK ( 16 ) ; } if ( reader -> bp > reader -> bitsize ) { ERROR_BREAK ( 51 ) ; } } HuffmanTree_cleanup ( & tree_ll ) ; HuffmanTree_cleanup ( & tree_d ) ; return error ; }
inflate a block with dynamic of fixed Huffman tree.
inflate a block with dynamic of fixed Huffman tree .
/*the huffman tree for literal and length codes*/ /*the huffman tree for distance codes*/ /*if(btype == 2)*/ /*decode all symbols until end reached, breaks at end code*/ /*code_ll is literal, length or end code*/ /* up to 15 for the huffman symbol, up to 5 for the length extra bits */ /*literal symbol*/ /*ucvector_push_back would do the same, but for some reason the two lines below run 10% faster*/ /*alloc fail*/ /*length code*/ /*extra bits for length and distance*/ /*part 1: get length base*/ /*part 2: get extra bits and add the value of that to length*/ /* bits already ensured above */ /*part 3: get distance code*/ /* up to 15 for the huffman symbol, up to 13 for the extra bits */ /*error: invalid distance code (30-31 are never used)*/ /* if(code_d == INVALIDSYMBOL) */ /*error: tried to read disallowed huffman symbol*/ /*part 4: get extra bits from distance*/ /* bits already ensured above */ /*part 5: fill in all the out[n] values based on the length and dist*/ /*too long backward distance*/ /*alloc fail*/ /*end code, break the loop*/ /*if(code_ll == INVALIDSYMBOL)*/ /*error: tried to read disallowed huffman symbol*/ /*check if any of the ensureBits above went out of bounds*/ /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol
      (10=no endcode, 11=wrong jump outside of tree)*/ /* TODO: revise error codes 10,11,50: the above comment is no longer valid */ /*error, bit pointer jumps past memory*/