n2b_d.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* n2b_d.c -- implementation of the NRV2B 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. do {
  61. m_off = m_off*2 + getbit(bb);
  62. fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
  63. fail(m_off > UCL_UINT32_C(0xffffff) + 3, UCL_E_LOOKBEHIND_OVERRUN);
  64. } while (!getbit(bb));
  65. if (m_off == 2)
  66. {
  67. m_off = last_m_off;
  68. }
  69. else
  70. {
  71. fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
  72. m_off = (m_off-3)*256 + src[ilen++];
  73. if (m_off == UCL_UINT32_C(0xffffffff))
  74. break;
  75. last_m_off = ++m_off;
  76. }
  77. m_len = getbit(bb);
  78. m_len = m_len*2 + getbit(bb);
  79. if (m_len == 0)
  80. {
  81. m_len++;
  82. do {
  83. m_len = m_len*2 + getbit(bb);
  84. fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
  85. fail(m_len >= oend, UCL_E_OUTPUT_OVERRUN);
  86. } while (!getbit(bb));
  87. m_len += 2;
  88. }
  89. m_len += (m_off > 0xd00);
  90. fail(olen + m_len > oend, UCL_E_OUTPUT_OVERRUN);
  91. fail(m_off > olen, UCL_E_LOOKBEHIND_OVERRUN);
  92. #ifdef TEST_OVERLAP
  93. olen += m_len + 1;
  94. fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
  95. #else
  96. {
  97. const ucl_byte *m_pos;
  98. m_pos = dst + olen - m_off;
  99. dst[olen++] = *m_pos++;
  100. do dst[olen++] = *m_pos++; while (--m_len > 0);
  101. }
  102. #endif
  103. }
  104. *dst_len = olen;
  105. return ilen == src_len ? UCL_E_OK : (ilen < src_len ? UCL_E_INPUT_NOT_CONSUMED : UCL_E_INPUT_OVERRUN);
  106. }
  107. #undef fail
  108. #endif /* getbit */
  109. /***********************************************************************
  110. // decompressor entries for the different bit-buffer sizes
  111. ************************************************************************/
  112. #ifndef getbit
  113. #include "ucl.h"
  114. #include "ucl_conf.h"
  115. #include "getbit.h"
  116. UCL_PUBLIC(int)
  117. ucl_nrv2b_decompress_8 ( const ucl_bytep src, ucl_uint src_len,
  118. ucl_bytep dst, ucl_uintp dst_len,
  119. ucl_voidp wrkmem )
  120. {
  121. #define getbit(bb) getbit_8(bb,src,ilen)
  122. #include "n2b_d.c"
  123. #undef getbit
  124. }
  125. UCL_PUBLIC(int)
  126. ucl_nrv2b_decompress_le16 ( const ucl_bytep src, ucl_uint src_len,
  127. ucl_bytep dst, ucl_uintp dst_len,
  128. ucl_voidp wrkmem )
  129. {
  130. #define getbit(bb) getbit_le16(bb,src,ilen)
  131. #include "n2b_d.c"
  132. #undef getbit
  133. }
  134. UCL_PUBLIC(int)
  135. ucl_nrv2b_decompress_le32 ( const ucl_bytep src, ucl_uint src_len,
  136. ucl_bytep dst, ucl_uintp dst_len,
  137. ucl_voidp wrkmem )
  138. {
  139. unsigned bc = 0;
  140. #define getbit(bb) getbit_le32(bb,bc,src,ilen)
  141. #include "n2b_d.c"
  142. #undef getbit
  143. }
  144. #endif /* !getbit */
  145. /*
  146. vi:ts=4:et
  147. */