MPEGPlayer: Fix leakage of file decriptors if file wasn't accepted by playback engine...
[kugel-rb.git] / tools / ucl / src / n2e_d.c
blob6c4586d75cf6003ff8a7ce442c6cebc8011614ce
1 /* n2e_d.c -- implementation of the NRV2E decompression algorithm
3 This file is part of the UCL data compression library.
5 Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
6 All Rights Reserved.
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.
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.
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.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/ucl/
29 /***********************************************************************
30 // actual implementation used by a recursive #include
31 ************************************************************************/
33 #ifdef getbit
35 #ifdef SAFE
36 #define fail(x,r) if (x) { *dst_len = olen; return r; }
37 #else
38 #define fail(x,r)
39 #endif
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 UCL_UNUSED(wrkmem);
53 #ifdef TEST_OVERLAP
54 src_len += src_off;
55 fail(oend >= src_len, UCL_E_OVERLAP_OVERRUN);
56 #endif
58 for (;;)
60 ucl_uint m_off, m_len;
62 while (getbit(bb))
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
73 m_off = 1;
74 for (;;)
76 m_off = m_off*2 + getbit(bb);
77 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
78 fail(m_off > UCL_UINT32_C(0xffffff) + 3, UCL_E_LOOKBEHIND_OVERRUN);
79 if (getbit(bb)) break;
80 m_off = (m_off-1)*2 + getbit(bb);
82 if (m_off == 2)
84 m_off = last_m_off;
85 m_len = getbit(bb);
87 else
89 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
90 m_off = (m_off-3)*256 + src[ilen++];
91 if (m_off == UCL_UINT32_C(0xffffffff))
92 break;
93 m_len = (m_off ^ UCL_UINT32_C(0xffffffff)) & 1;
94 m_off >>= 1;
95 last_m_off = ++m_off;
97 if (m_len)
98 m_len = 1 + getbit(bb);
99 else if (getbit(bb))
100 m_len = 3 + getbit(bb);
101 else
103 m_len++;
104 do {
105 m_len = m_len*2 + getbit(bb);
106 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
107 fail(m_len >= oend, UCL_E_OUTPUT_OVERRUN);
108 } while (!getbit(bb));
109 m_len += 3;
111 m_len += (m_off > 0x500);
112 fail(olen + m_len > oend, UCL_E_OUTPUT_OVERRUN);
113 fail(m_off > olen, UCL_E_LOOKBEHIND_OVERRUN);
114 #ifdef TEST_OVERLAP
115 olen += m_len + 1;
116 fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
117 #else
119 const ucl_byte *m_pos;
120 m_pos = dst + olen - m_off;
121 dst[olen++] = *m_pos++;
122 do dst[olen++] = *m_pos++; while (--m_len > 0);
124 #endif
126 *dst_len = olen;
127 return ilen == src_len ? UCL_E_OK : (ilen < src_len ? UCL_E_INPUT_NOT_CONSUMED : UCL_E_INPUT_OVERRUN);
130 #undef fail
132 #endif /* getbit */
135 /***********************************************************************
136 // decompressor entries for the different bit-buffer sizes
137 ************************************************************************/
139 #ifndef getbit
141 #include <ucl/ucl.h>
142 #include "ucl_conf.h"
143 #include "getbit.h"
146 UCL_PUBLIC(int)
147 ucl_nrv2e_decompress_8 ( const ucl_byte *src, ucl_uint src_len,
148 ucl_byte *dst, ucl_uintp dst_len,
149 ucl_voidp wrkmem )
151 #define getbit(bb) getbit_8(bb,src,ilen)
152 #include "n2e_d.c"
153 #undef getbit
157 UCL_PUBLIC(int)
158 ucl_nrv2e_decompress_le16 ( const ucl_bytep src, ucl_uint src_len,
159 ucl_bytep dst, ucl_uintp dst_len,
160 ucl_voidp wrkmem )
162 #define getbit(bb) getbit_le16(bb,src,ilen)
163 #include "n2e_d.c"
164 #undef getbit
168 UCL_PUBLIC(int)
169 ucl_nrv2e_decompress_le32 ( const ucl_bytep src, ucl_uint src_len,
170 ucl_bytep dst, ucl_uintp dst_len,
171 ucl_voidp wrkmem )
173 unsigned bc = 0;
174 #define getbit(bb) getbit_le32(bb,bc,src,ilen)
175 #include "n2e_d.c"
176 #undef getbit
180 #endif /* !getbit */
184 vi:ts=4:et