n2d_d.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* n2d_d.c -- implementation of the NRV2D 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. m_len = m_len*2 + getbit(bb);
  84. if (m_len == 0)
  85. {
  86. m_len++;
  87. do {
  88. m_len = m_len*2 + getbit(bb);
  89. fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
  90. fail(m_len >= oend, UCL_E_OUTPUT_OVERRUN);
  91. } while (!getbit(bb));
  92. m_len += 2;
  93. }
  94. m_len += (m_off > 0x500);
  95. fail(olen + m_len > oend, UCL_E_OUTPUT_OVERRUN);
  96. fail(m_off > olen, UCL_E_LOOKBEHIND_OVERRUN);
  97. #ifdef TEST_OVERLAP
  98. olen += m_len + 1;
  99. fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
  100. #else
  101. {
  102. const ucl_byte *m_pos;
  103. m_pos = dst + olen - m_off;
  104. dst[olen++] = *m_pos++;
  105. do dst[olen++] = *m_pos++; while (--m_len > 0);
  106. }
  107. #endif
  108. }
  109. *dst_len = olen;
  110. return ilen == src_len ? UCL_E_OK : (ilen < src_len ? UCL_E_INPUT_NOT_CONSUMED : UCL_E_INPUT_OVERRUN);
  111. }
  112. #undef fail
  113. #endif /* getbit */
  114. /***********************************************************************
  115. // decompressor entries for the different bit-buffer sizes
  116. ************************************************************************/
  117. #ifndef getbit
  118. #include "ucl.h"
  119. #include "ucl_conf.h"
  120. #include "getbit.h"
  121. UCL_PUBLIC(int)
  122. ucl_nrv2d_decompress_8 ( const ucl_bytep src, ucl_uint src_len,
  123. ucl_bytep dst, ucl_uintp dst_len,
  124. ucl_voidp wrkmem )
  125. {
  126. #define getbit(bb) getbit_8(bb,src,ilen)
  127. #include "n2d_d.c"
  128. #undef getbit
  129. }
  130. UCL_PUBLIC(int)
  131. ucl_nrv2d_decompress_le16 ( const ucl_bytep src, ucl_uint src_len,
  132. ucl_bytep dst, ucl_uintp dst_len,
  133. ucl_voidp wrkmem )
  134. {
  135. #define getbit(bb) getbit_le16(bb,src,ilen)
  136. #include "n2d_d.c"
  137. #undef getbit
  138. }
  139. UCL_PUBLIC(int)
  140. ucl_nrv2d_decompress_le32 ( const ucl_bytep src, ucl_uint src_len,
  141. ucl_bytep dst, ucl_uintp dst_len,
  142. ucl_voidp wrkmem )
  143. {
  144. unsigned bc = 0;
  145. #define getbit(bb) getbit_le32(bb,bc,src,ilen)
  146. #include "n2d_d.c"
  147. #undef getbit
  148. }
  149. #endif /* !getbit */
  150. /*
  151. vi:ts=4:et
  152. */