Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 26 | pmbaty | 1 | /* n2b_d.c -- implementation of the NRV2B decompression algorithm |
| 2 | |||
| 3 | This file is part of the UCL data compression library. |
||
| 4 | |||
| 5 | Copyright (C) 1996-2004 Markus Franz Xaver Johannes Oberhumer |
||
| 6 | All Rights Reserved. |
||
| 7 | |||
| 8 | The UCL 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 UCL 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 UCL 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.com> |
||
| 25 | http://www.oberhumer.com/opensource/ucl/ |
||
| 26 | */ |
||
| 27 | |||
| 28 | |||
| 29 | /*********************************************************************** |
||
| 30 | // actual implementation used by a recursive #include |
||
| 31 | ************************************************************************/ |
||
| 32 | |||
| 33 | #ifdef getbit |
||
| 34 | |||
| 35 | #ifdef SAFE |
||
| 36 | #define fail(x,r) if (x) { *dst_len = olen; return r; } |
||
| 37 | #else |
||
| 38 | #define fail(x,r) |
||
| 39 | #endif |
||
| 40 | |||
| 41 | { |
||
| 42 | ucl_uint32 bb = 0; |
||
| 43 | #ifdef TEST_OVERLAP |
||
| 44 | ucl_uint ilen = src_off, olen = 0, last_m_off = 1; |
||
| 45 | #else |
||
| 46 | ucl_uint ilen = 0, olen = 0, last_m_off = 1; |
||
| 47 | #endif |
||
| 48 | #ifdef SAFE |
||
| 49 | const ucl_uint oend = *dst_len; |
||
| 50 | #endif |
||
| 51 | ACC_UNUSED(wrkmem); |
||
| 52 | |||
| 53 | #ifdef TEST_OVERLAP |
||
| 54 | src_len += src_off; |
||
| 55 | fail(oend >= src_len, UCL_E_OVERLAP_OVERRUN); |
||
| 56 | #endif |
||
| 57 | |||
| 58 | for (;;) |
||
| 59 | { |
||
| 60 | ucl_uint m_off, m_len; |
||
| 61 | |||
| 62 | while (getbit(bb)) |
||
| 63 | { |
||
| 64 | fail(ilen >= src_len, UCL_E_INPUT_OVERRUN); |
||
| 65 | fail(olen >= oend, UCL_E_OUTPUT_OVERRUN); |
||
| 66 | #ifdef TEST_OVERLAP |
||
| 67 | fail(olen > ilen, UCL_E_OVERLAP_OVERRUN); |
||
| 68 | olen++; ilen++; |
||
| 69 | #else |
||
| 70 | dst[olen++] = src[ilen++]; |
||
| 71 | #endif |
||
| 72 | } |
||
| 73 | m_off = 1; |
||
| 74 | do { |
||
| 75 | m_off = m_off*2 + getbit(bb); |
||
| 76 | fail(ilen >= src_len, UCL_E_INPUT_OVERRUN); |
||
| 77 | fail(m_off > UCL_UINT32_C(0xffffff) + 3, UCL_E_LOOKBEHIND_OVERRUN); |
||
| 78 | } while (!getbit(bb)); |
||
| 79 | if (m_off == 2) |
||
| 80 | { |
||
| 81 | m_off = last_m_off; |
||
| 82 | } |
||
| 83 | else |
||
| 84 | { |
||
| 85 | fail(ilen >= src_len, UCL_E_INPUT_OVERRUN); |
||
| 86 | m_off = (m_off-3)*256 + src[ilen++]; |
||
| 87 | if (m_off == UCL_UINT32_C(0xffffffff)) |
||
| 88 | break; |
||
| 89 | last_m_off = ++m_off; |
||
| 90 | } |
||
| 91 | m_len = getbit(bb); |
||
| 92 | m_len = m_len*2 + getbit(bb); |
||
| 93 | if (m_len == 0) |
||
| 94 | { |
||
| 95 | m_len++; |
||
| 96 | do { |
||
| 97 | m_len = m_len*2 + getbit(bb); |
||
| 98 | fail(ilen >= src_len, UCL_E_INPUT_OVERRUN); |
||
| 99 | fail(m_len >= oend, UCL_E_OUTPUT_OVERRUN); |
||
| 100 | } while (!getbit(bb)); |
||
| 101 | m_len += 2; |
||
| 102 | } |
||
| 103 | m_len += (m_off > 0xd00); |
||
| 104 | fail(olen + m_len > oend, UCL_E_OUTPUT_OVERRUN); |
||
| 105 | fail(m_off > olen, UCL_E_LOOKBEHIND_OVERRUN); |
||
| 106 | #ifdef TEST_OVERLAP |
||
| 107 | olen += m_len + 1; |
||
| 108 | fail(olen > ilen, UCL_E_OVERLAP_OVERRUN); |
||
| 109 | #else |
||
| 110 | { |
||
| 111 | const ucl_bytep m_pos; |
||
| 112 | m_pos = dst + olen - m_off; |
||
| 113 | dst[olen++] = *m_pos++; |
||
| 114 | do dst[olen++] = *m_pos++; while (--m_len > 0); |
||
| 115 | } |
||
| 116 | #endif |
||
| 117 | } |
||
| 118 | *dst_len = olen; |
||
| 119 | return ilen == src_len ? UCL_E_OK : (ilen < src_len ? UCL_E_INPUT_NOT_CONSUMED : UCL_E_INPUT_OVERRUN); |
||
| 120 | } |
||
| 121 | |||
| 122 | #undef fail |
||
| 123 | |||
| 124 | #endif /* getbit */ |
||
| 125 | |||
| 126 | |||
| 127 | /*********************************************************************** |
||
| 128 | // decompressor entries for the different bit-buffer sizes |
||
| 129 | ************************************************************************/ |
||
| 130 | |||
| 131 | #ifndef getbit |
||
| 132 | |||
| 133 | #include "ucl_conf.h" |
||
| 134 | #include <ucl/ucl.h> |
||
| 135 | #include "getbit.h" |
||
| 136 | |||
| 137 | |||
| 138 | UCL_PUBLIC(int) |
||
| 139 | ucl_nrv2b_decompress_8 ( const ucl_bytep src, ucl_uint src_len, |
||
| 140 | ucl_bytep dst, ucl_uintp dst_len, |
||
| 141 | ucl_voidp wrkmem ) |
||
| 142 | { |
||
| 143 | #define getbit(bb) getbit_8(bb,src,ilen) |
||
| 144 | #include "n2b_d.c" |
||
| 145 | #undef getbit |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | UCL_PUBLIC(int) |
||
| 150 | ucl_nrv2b_decompress_le16 ( const ucl_bytep src, ucl_uint src_len, |
||
| 151 | ucl_bytep dst, ucl_uintp dst_len, |
||
| 152 | ucl_voidp wrkmem ) |
||
| 153 | { |
||
| 154 | #define getbit(bb) getbit_le16(bb,src,ilen) |
||
| 155 | #include "n2b_d.c" |
||
| 156 | #undef getbit |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | UCL_PUBLIC(int) |
||
| 161 | ucl_nrv2b_decompress_le32 ( const ucl_bytep src, ucl_uint src_len, |
||
| 162 | ucl_bytep dst, ucl_uintp dst_len, |
||
| 163 | ucl_voidp wrkmem ) |
||
| 164 | { |
||
| 165 | unsigned bc = 0; |
||
| 166 | #define getbit(bb) getbit_le32(bb,bc,src,ilen) |
||
| 167 | #include "n2b_d.c" |
||
| 168 | #undef getbit |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | #endif /* !getbit */ |
||
| 173 | |||
| 174 | |||
| 175 | /* |
||
| 176 | vi:ts=4:et |
||
| 177 | */ |
||
| 178 |