示例#1
0
/* inflate stream from source to dest */
int tinf_uncompress(void *dest, unsigned int *destLen,
                    const void *source, unsigned int sourceLen)
{
    TINF_DATA d;
    int bfinal;

    d.source = (const unsigned char *)source;
    d.bitcount = 0;

    d.dest = (unsigned char *)dest;
    d.destLen = destLen;

    *destLen = 0;

    do {

       unsigned int btype;
       int res;

       /* read final block flag */
       bfinal = tinf_getbit(&d);

       /* read block type (2 bits) */
       btype = tinf_read_bits(&d, 2, 0);

       /* decompress block */
       switch (btype)
       {
       case 0:
          /* decompress uncompressed block */
          res = tinf_inflate_uncompressed_block(&d);
          break;
        case 1:
          /* decompress block with fixed huffman trees */
          res = tinf_inflate_fixed_block(&d,&tbl);
          break;
       case 2:
          /* decompress block with dynamic huffman trees */
          res = tinf_inflate_dynamic_block(&d,&tbl);
          break;
       default:
          return TINF_DATA_ERROR;
       }

       if (res != TINF_OK) return TINF_DATA_ERROR;

       if (d.source > (unsigned char *)source + sourceLen)
           return TINF_DATA_ERROR;
    } while (!bfinal);

    return TINF_OK;
}
示例#2
0
/* inflate stream from source to dest */
int tinf_uncompress(void *dest, unsigned int *destLen,
                    const void *source, unsigned int sourceLen,
                    const unsigned int swapped_btype[3])
{
    TINF_DATA d;
    int bfinal;

    /* initialise data */
    d.source = (const unsigned char *)source;
    d.bitcount = 0;

    d.dest = (unsigned char *)dest;
    d.destLen = destLen;

    d.sourceLen = 0;
    d.sourceSize = sourceLen;
    d.destSize = *destLen;

    *destLen = 0;

    do {

        unsigned int btype;
        int res;

        /* read final block flag */
        bfinal = tinf_getbit(&d);

        /* read block type (2 bits) */
        btype = tinf_read_bits(&d, 2, 0);

        /* decompress block */
        if(btype == swapped_btype[0]) {
            /* decompress uncompressed block */
            res = tinf_inflate_uncompressed_block(&d);
        } else if(btype == swapped_btype[1]) {
            /* decompress block with fixed huffman trees */
            res = tinf_inflate_fixed_block(&d);
        } else if(btype == swapped_btype[2]) {
            /* decompress block with dynamic huffman trees */
            res = tinf_inflate_dynamic_block(&d);
        } else {
            return TINF_DATA_ERROR;
        }

        if (res != TINF_OK) return TINF_DATA_ERROR;

    } while (!bfinal);

    return TINF_OK;
}
示例#3
0
/* inflate stream from source to dest */
int tinf_uncompress(void *dest, unsigned int *destLen,
                    const void *source, unsigned int sourceLen)
{
   TINF_DATA d;
   int bfinal;

   /* initialise data */
   d.source = (const unsigned char *)source;
   d.bitcount = 0;

   d.dest = (unsigned char *)dest;
   d.destLen = destLen;

   *destLen = 0;

   do {

      unsigned int btype;
      int res;

      /* read block type (2 bits) */
      btype = tinf_read_bits(&d, 3, 0);
      
      /* read final block flag */
      bfinal = tinf_getbit(&d);

      /* decompress block */
      switch (btype)
      {
      case 7:
         /* decompress uncompressed block */
         res = tinf_inflate_uncompressed_block(&d);
         break;
      case 5:
         /* decompress block with fixed huffman trees */
         res = tinf_inflate_fixed_block(&d);
         break;
      case 6:
         /* decompress block with dynamic huffman trees */
         res = tinf_inflate_dynamic_block(&d);
         break;
      default:
         return -1;
      }

      if (res != TINF_OK) return -1;

   } while (!bfinal);

   return ((char*)d.source - (char *)source);
}