home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 March / PCO3_97.ISO / filesbbs / os2 / lzo026.arj / LZO026.ZIP / lzo-0.26 / src / lzo2a_d.ch < prev    next >
Encoding:
Text File  |  1997-01-14  |  3.9 KB  |  187 lines

  1. /* lzo2a_d.ch -- implementation of the LZO2A decompression algorithm
  2.  
  3.    This file is part of the LZO real-time data compression library.
  4.  
  5.    Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
  6.    Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
  7.  
  8.    The LZO library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of
  11.    the License, or (at your option) any later version.
  12.  
  13.    The LZO library is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with the LZO library; see the file COPYING.
  20.    If not, write to the Free Software Foundation, Inc.,
  21.    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  
  23.    Markus F.X.J. Oberhumer
  24.    markus.oberhumer@jk.uni-linz.ac.at
  25.  */
  26.  
  27.  
  28. #include "lzo1_d.h"
  29.  
  30.  
  31. #if defined(HAVE_ANY_IP) && defined(HAVE_ANY_OP)
  32.    /* too many local variables, cannot allocate registers */
  33. #  undef LZO_OPTIMIZE_GNUC_i386
  34. #endif
  35.  
  36.  
  37. /***********************************************************************
  38. // decompress a block of data.
  39. ************************************************************************/
  40.  
  41. #define _NEEDBYTE    NEED_IP(1)
  42. #define _NEXTBYTE    (*ip++)
  43.  
  44. LZO_PUBLIC(int)
  45. DO_DECOMPRESS    ( const lzo_byte *in , lzo_uint  in_len,
  46.                          lzo_byte *out, lzo_uint *out_len,
  47.                          lzo_voidp LZO_UNUSED(wrkmem) )
  48. {
  49. #if defined(LZO_OPTIMIZE_GNUC_i386)
  50.     register lzo_byte *op __asm__("%edi");
  51.     register const lzo_byte *ip __asm__("%esi");
  52.     register const lzo_byte *m_pos __asm__("%ebx");
  53. #else
  54.     register lzo_byte *op;
  55.     register const lzo_byte *ip;
  56.     register const lzo_byte *m_pos;
  57. #endif
  58.  
  59.     lzo_uint t;
  60.     const lzo_byte * const ip_end = in + in_len;
  61. #if defined(HAVE_ANY_OP)
  62.     lzo_byte * const op_end = out + *out_len;
  63. #endif
  64.  
  65.     lzo_uint32 b = 0;        /* bit buffer */
  66.     unsigned k = 0;            /* bits in bit buffer */
  67.  
  68.  
  69.     op = out;
  70.     ip = in;
  71.  
  72.     while (TEST_IP && TEST_OP)
  73.     {
  74.         NEEDBITS(1);
  75.         if (MASKBITS(1) == 0)
  76.         {
  77.             DUMPBITS(1);
  78.             /* a literal */
  79.             NEED_IP(1); NEED_OP(1);
  80.             *op++ = *ip++;
  81.             continue;
  82.         }
  83.         DUMPBITS(1);
  84.  
  85.         NEEDBITS(1);
  86.         if (MASKBITS(1) == 0)
  87.         {
  88.             DUMPBITS(1);
  89.             /* a M1 match */
  90.             NEEDBITS(2);
  91.             t = M1_MIN_LEN + MASKBITS(2);
  92.             DUMPBITS(2);
  93.             NEED_IP(1); NEED_OP(t);
  94.             m_pos = op - 1 - *ip++;
  95.             assert(m_pos >= out); assert(m_pos < op);
  96.             TEST_LOOKBEHIND(m_pos,out); 
  97.             MEMMOVE_DS(op,m_pos,t);
  98.             continue;
  99.         }
  100.         DUMPBITS(1);
  101.  
  102.         NEED_IP(2);
  103.         t = *ip++;
  104.         m_pos = op;
  105.         m_pos -= (t & 31) | (((lzo_uint) *ip++) << 5);
  106.         t >>= 5;
  107.         if (t == 0)
  108.         {
  109. #if (N >= 8192)
  110.             NEEDBITS(1);
  111.             t = MASKBITS(1);
  112.             DUMPBITS(1);
  113.             if (t == 0)
  114.                 t = 10 - 1;
  115.             else
  116.             {
  117.                 /* a M3 match */
  118.                 m_pos -= 8192;        /* t << 13 */
  119.                 t = M3_MIN_LEN - 1;
  120.             }
  121. #else
  122.             t = 10 - 1;
  123. #endif
  124.             NEED_IP(1);
  125.             while (*ip == 0)
  126.             {
  127.                 t += 255;
  128.                 ip++;
  129.                 NEED_IP(1);
  130.             }
  131.             t += *ip++;
  132.         }
  133.         else
  134.         {
  135. #if defined(LZO_EOF_CODE)
  136.             if (m_pos == op)
  137.                 goto eof_found;
  138. #endif
  139.             t += 2;
  140.         }
  141.         assert(m_pos >= out); assert(m_pos < op);
  142.         TEST_LOOKBEHIND(m_pos,out); 
  143.         NEED_OP(t);
  144.         MEMMOVE_DS(op,m_pos,t);
  145.     }
  146.  
  147.  
  148. #if defined(LZO_EOF_CODE)
  149.     /* ip == ip_end and no EOF code was found */
  150.     *out_len = op - out;
  151.     return LZO_E_EOF_NOT_FOUND;
  152.  
  153. eof_found:
  154.     assert(t == 1);
  155. #endif
  156.  
  157.     *out_len = op - out;
  158.  
  159.     /* the next line is the only check in the decompressor */
  160.     return (ip == ip_end ? LZO_E_OK : LZO_E_ERROR);
  161.  
  162.  
  163. #if defined(HAVE_NEED_IP)
  164. input_overrun:
  165.     *out_len = op - out;
  166.     return LZO_E_INPUT_OVERRUN;
  167. #endif
  168.  
  169. #if defined(HAVE_NEED_OP)
  170. output_overrun:
  171.     *out_len = op - out;
  172.     return LZO_E_OUTPUT_OVERRUN;
  173. #endif
  174.  
  175. #if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
  176. lookbehind_overrun:
  177.     *out_len = op - out;
  178.     return LZO_E_LOOKBEHIND_OVERRUN;
  179. #endif
  180. }
  181.  
  182.  
  183. /*
  184. vi:ts=4
  185. */
  186.  
  187.