llbitpack.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @file bitpack.cpp
  3. * @brief Convert data to packed bit stream
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llbitpack.h"
  34. U32 LLBitPack::bitPack(U8* total_data, U32 total_dsize)
  35. {
  36. while (total_dsize > 0)
  37. {
  38. U32 dsize;
  39. if (total_dsize > MAX_DATA_BITS)
  40. {
  41. dsize = MAX_DATA_BITS;
  42. total_dsize -= MAX_DATA_BITS;
  43. }
  44. else
  45. {
  46. dsize = total_dsize;
  47. total_dsize = 0;
  48. }
  49. U8 data = *total_data++;
  50. data <<= (MAX_DATA_BITS - dsize);
  51. while (dsize > 0)
  52. {
  53. if (mLoadSize == MAX_DATA_BITS)
  54. {
  55. *(mBuffer + mBufferSize++) = mLoad;
  56. if (mBufferSize > mMaxSize)
  57. {
  58. llerrs << "mBufferSize exceeding mMaxSize !" << llendl;
  59. }
  60. mLoadSize = 0;
  61. mLoad = 0x00;
  62. }
  63. mLoad <<= 1;
  64. mLoad |= data >> (MAX_DATA_BITS - 1);
  65. data <<= 1;
  66. ++mLoadSize;
  67. ++mTotalBits;
  68. --dsize;
  69. }
  70. }
  71. return mBufferSize;
  72. }
  73. U32 LLBitPack::bitCopy(U8* total_data, U32 total_dsize)
  74. {
  75. while (total_dsize > 0)
  76. {
  77. U32 dsize;
  78. if (total_dsize > MAX_DATA_BITS)
  79. {
  80. dsize = MAX_DATA_BITS;
  81. total_dsize -= MAX_DATA_BITS;
  82. }
  83. else
  84. {
  85. dsize = total_dsize;
  86. total_dsize = 0;
  87. }
  88. U8 data = *total_data++;
  89. while (dsize > 0)
  90. {
  91. if (mLoadSize == MAX_DATA_BITS)
  92. {
  93. *(mBuffer + mBufferSize++) = mLoad;
  94. if (mBufferSize > mMaxSize)
  95. {
  96. llerrs << "mBufferSize exceeding mMaxSize !" << llendl;
  97. }
  98. mLoadSize = 0;
  99. mLoad = 0x00;
  100. }
  101. mLoad <<= 1;
  102. mLoad |= (data >> (MAX_DATA_BITS - 1));
  103. data <<= 1;
  104. ++mLoadSize;
  105. ++mTotalBits;
  106. --dsize;
  107. }
  108. }
  109. return mBufferSize;
  110. }
  111. U32 LLBitPack::bitUnpack(U8* total_retval, U32 total_dsize)
  112. {
  113. while (total_dsize > 0)
  114. {
  115. U32 dsize;
  116. if (total_dsize > MAX_DATA_BITS)
  117. {
  118. dsize = MAX_DATA_BITS;
  119. total_dsize -= MAX_DATA_BITS;
  120. }
  121. else
  122. {
  123. dsize = total_dsize;
  124. total_dsize = 0;
  125. }
  126. U8* retval = total_retval++;
  127. *retval = 0x00;
  128. while (dsize > 0)
  129. {
  130. if (mLoadSize == 0)
  131. {
  132. #if LL_DEBUG
  133. if (mBufferSize > mMaxSize)
  134. {
  135. llerrs << "mBufferSize exceeding mMaxSize" << llendl;
  136. llerrs << mBufferSize << " > " << mMaxSize << llendl;
  137. }
  138. #endif
  139. mLoad = *(mBuffer + mBufferSize++);
  140. mLoadSize = MAX_DATA_BITS;
  141. }
  142. *retval <<= 1;
  143. *retval |= (mLoad >> (MAX_DATA_BITS - 1));
  144. --mLoadSize;
  145. mLoad <<= 1;
  146. --dsize;
  147. }
  148. }
  149. return mBufferSize;
  150. }
  151. U32 LLBitPack::flushBitPack()
  152. {
  153. if (mLoadSize)
  154. {
  155. mLoad <<= (MAX_DATA_BITS - mLoadSize);
  156. *(mBuffer + mBufferSize++) = mLoad;
  157. if (mBufferSize > mMaxSize)
  158. {
  159. llerrs << "mBufferSize exceeding mMaxSize !" << llendl;
  160. }
  161. mLoadSize = 0;
  162. }
  163. return mBufferSize;
  164. }