n2e_d.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* n2e_d.c -- implementation of the NRV2E decompression algorithm
  2. This file is part of the UCL data compression library.
  3. Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
  4. All Rights Reserved.
  5. The UCL library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. The UCL library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with the UCL library; see the file COPYING.
  15. If not, write to the Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. Markus F.X.J. Oberhumer
  18. <markus@oberhumer.com>
  19. http://www.oberhumer.com/opensource/ucl/
  20. */
  21. /***********************************************************************
  22. // actual implementation used by a recursive #include
  23. ************************************************************************/
  24. #ifdef getbit
  25. #ifdef SAFE
  26. #define fail(x,r) if (x) { *dst_len = olen; return r; }
  27. #else
  28. #define fail(x,r)
  29. #endif
  30. {
  31. ucl_uint32 bb = 0;
  32. #ifdef TEST_OVERLAP
  33. ucl_uint ilen = src_off, olen = 0, last_m_off = 1;
  34. #else
  35. ucl_uint ilen = 0, olen = 0, last_m_off = 1;
  36. #endif
  37. #ifdef SAFE
  38. const ucl_uint oend = *dst_len;
  39. #endif
  40. UCL_UNUSED(wrkmem);
  41. #ifdef TEST_OVERLAP
  42. src_len += src_off;
  43. fail(oend >= src_len, UCL_E_OVERLAP_OVERRUN);
  44. #endif
  45. for (;;)
  46. {
  47. ucl_uint m_off, m_len;
  48. while (getbit(bb))
  49. {
  50. fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
  51. fail(olen >= oend, UCL_E_OUTPUT_OVERRUN);
  52. #ifdef TEST_OVERLAP
  53. fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
  54. olen++; ilen++;
  55. #else
  56. dst[olen++] = src[ilen++];
  57. #endif
  58. }
  59. m_off = 1;
  60. for (;;)
  61. {
  62. m_off = m_off*2 + getbit(bb);
  63. fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
  64. fail(m_off > UCL_UINT32_C(0xffffff) + 3, UCL_E_LOOKBEHIND_OVERRUN);
  65. if (getbit(bb)) break;
  66. m_off = (m_off-1)*2 + getbit(bb);
  67. }
  68. if (m_off == 2)
  69. {
  70. m_off = last_m_off;
  71. m_len = getbit(bb);
  72. }
  73. else
  74. {
  75. fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
  76. m_off = (m_off-3)*256 + src[ilen++];
  77. if (m_off == UCL_UINT32_C(0xffffffff))
  78. break;
  79. m_len = (m_off ^ UCL_UINT32_C(0xffffffff)) & 1;
  80. m_off >>= 1;
  81. last_m_off = ++m_off;
  82. }
  83. if (m_len)
  84. m_len = 1 + getbit(bb);
  85. else if (getbit(bb))
  86. m_len = 3 + getbit(bb);
  87. else
  88. {
  89. m_len++;
  90. do {
  91. m_len = m_len*2 + getbit(bb);
  92. fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
  93. fail(m_len >= oend, UCL_E_OUTPUT_OVERRUN);
  94. } while (!getbit(bb));
  95. m_len += 3;
  96. }
  97. m_len += (m_off > 0x500);
  98. fail(olen + m_len > oend, UCL_E_OUTPUT_OVERRUN);
  99. fail(m_off > olen, UCL_E_LOOKBEHIND_OVERRUN);
  100. #ifdef TEST_OVERLAP
  101. olen += m_len + 1;
  102. fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
  103. #else
  104. {
  105. const ucl_byte *m_pos;
  106. m_pos = dst + olen - m_off;
  107. dst[olen++] = *m_pos++;
  108. do dst[olen++] = *m_pos++; while (--m_len > 0);
  109. }
  110. #endif
  111. }
  112. *dst_len = olen;
  113. return ilen == src_len ? UCL_E_OK : (ilen < src_len ? UCL_E_INPUT_NOT_CONSUMED : UCL_E_INPUT_OVERRUN);
  114. }
  115. #undef fail
  116. #endif /* getbit */
  117. /***********************************************************************
  118. // decompressor entries for the different bit-buffer sizes
  119. ************************************************************************/
  120. #ifndef getbit
  121. #include "ucl.h"
  122. #include "ucl_conf.h"
  123. #include "getbit.h"
  124. UCL_PUBLIC(int)
  125. ucl_nrv2e_decompress_8 ( const ucl_byte *src, ucl_uint src_len,
  126. ucl_byte *dst, ucl_uintp dst_len,
  127. ucl_voidp wrkmem )
  128. {
  129. #define getbit(bb) getbit_8(bb,src,ilen)
  130. #include "n2e_d.c"
  131. #undef getbit
  132. }
  133. UCL_PUBLIC(int)
  134. ucl_nrv2e_decompress_le16 ( const ucl_bytep src, ucl_uint src_len,
  135. ucl_bytep dst, ucl_uintp dst_len,
  136. ucl_voidp wrkmem )
  137. {
  138. #define getbit(bb) getbit_le16(bb,src,ilen)
  139. #include "n2e_d.c"
  140. #undef getbit
  141. }
  142. UCL_PUBLIC(int)
  143. ucl_nrv2e_decompress_le32 ( const ucl_bytep src, ucl_uint src_len,
  144. ucl_bytep dst, ucl_uintp dst_len,
  145. ucl_voidp wrkmem )
  146. {
  147. unsigned bc = 0;
  148. #define getbit(bb) getbit_le32(bb,bc,src,ilen)
  149. #include "n2e_d.c"
  150. #undef getbit
  151. }
  152. #endif /* !getbit */
  153. /*
  154. vi:ts=4:et
  155. */