Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
5.5 KiB

  1. // basecode.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "basecode.h"
  5. #include "fltrimpl.h"
  6. #include <ctype.h>
  7. NAMESPACE_BEGIN(CryptoPP)
  8. void BaseN_Encoder::IsolatedInitialize(const NameValuePairs &parameters)
  9. {
  10. parameters.GetRequiredParameter("BaseN_Encoder", Name::EncodingLookupArray(), m_alphabet);
  11. parameters.GetRequiredIntParameter("BaseN_Encoder", Name::Log2Base(), m_bitsPerChar);
  12. if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
  13. throw InvalidArgument("BaseN_Encoder: Log2Base must be between 1 and 7 inclusive");
  14. byte padding;
  15. bool pad;
  16. if (parameters.GetValue(Name::PaddingByte(), padding))
  17. pad = parameters.GetValueWithDefault(Name::Pad(), true);
  18. else
  19. pad = false;
  20. m_padding = pad ? padding : -1;
  21. m_bytePos = m_bitPos = 0;
  22. int i = 8;
  23. while (i%m_bitsPerChar != 0)
  24. i += 8;
  25. m_outputBlockSize = i/m_bitsPerChar;
  26. m_outBuf.New(m_outputBlockSize);
  27. }
  28. size_t BaseN_Encoder::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
  29. {
  30. FILTER_BEGIN;
  31. while (m_inputPosition < length)
  32. {
  33. if (m_bytePos == 0)
  34. memset(m_outBuf, 0, m_outputBlockSize);
  35. {
  36. unsigned int b = begin[m_inputPosition++], bitsLeftInSource = 8;
  37. while (true)
  38. {
  39. assert(m_bitPos < m_bitsPerChar);
  40. unsigned int bitsLeftInTarget = m_bitsPerChar-m_bitPos;
  41. m_outBuf[m_bytePos] |= b >> (8-bitsLeftInTarget);
  42. if (bitsLeftInSource >= bitsLeftInTarget)
  43. {
  44. m_bitPos = 0;
  45. ++m_bytePos;
  46. bitsLeftInSource -= bitsLeftInTarget;
  47. if (bitsLeftInSource == 0)
  48. break;
  49. b <<= bitsLeftInTarget;
  50. b &= 0xff;
  51. }
  52. else
  53. {
  54. m_bitPos += bitsLeftInSource;
  55. break;
  56. }
  57. }
  58. }
  59. assert(m_bytePos <= m_outputBlockSize);
  60. if (m_bytePos == m_outputBlockSize)
  61. {
  62. int i;
  63. for (i=0; i<m_bytePos; i++)
  64. {
  65. assert(m_outBuf[i] < (1 << m_bitsPerChar));
  66. m_outBuf[i] = m_alphabet[m_outBuf[i]];
  67. }
  68. FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
  69. m_bytePos = m_bitPos = 0;
  70. }
  71. }
  72. if (messageEnd)
  73. {
  74. if (m_bitPos > 0)
  75. ++m_bytePos;
  76. int i;
  77. for (i=0; i<m_bytePos; i++)
  78. m_outBuf[i] = m_alphabet[m_outBuf[i]];
  79. if (m_padding != -1 && m_bytePos > 0)
  80. {
  81. memset(m_outBuf+m_bytePos, m_padding, m_outputBlockSize-m_bytePos);
  82. m_bytePos = m_outputBlockSize;
  83. }
  84. FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
  85. m_bytePos = m_bitPos = 0;
  86. }
  87. FILTER_END_NO_MESSAGE_END;
  88. }
  89. void BaseN_Decoder::IsolatedInitialize(const NameValuePairs &parameters)
  90. {
  91. parameters.GetRequiredParameter("BaseN_Decoder", Name::DecodingLookupArray(), m_lookup);
  92. parameters.GetRequiredIntParameter("BaseN_Decoder", Name::Log2Base(), m_bitsPerChar);
  93. if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
  94. throw InvalidArgument("BaseN_Decoder: Log2Base must be between 1 and 7 inclusive");
  95. m_bytePos = m_bitPos = 0;
  96. int i = m_bitsPerChar;
  97. while (i%8 != 0)
  98. i += m_bitsPerChar;
  99. m_outputBlockSize = i/8;
  100. m_outBuf.New(m_outputBlockSize);
  101. }
  102. size_t BaseN_Decoder::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
  103. {
  104. FILTER_BEGIN;
  105. while (m_inputPosition < length)
  106. {
  107. unsigned int value;
  108. value = m_lookup[begin[m_inputPosition++]];
  109. if (value >= 256)
  110. continue;
  111. if (m_bytePos == 0 && m_bitPos == 0)
  112. memset(m_outBuf, 0, m_outputBlockSize);
  113. {
  114. int newBitPos = m_bitPos + m_bitsPerChar;
  115. if (newBitPos <= 8)
  116. m_outBuf[m_bytePos] |= value << (8-newBitPos);
  117. else
  118. {
  119. m_outBuf[m_bytePos] |= value >> (newBitPos-8);
  120. m_outBuf[m_bytePos+1] |= value << (16-newBitPos);
  121. }
  122. m_bitPos = newBitPos;
  123. while (m_bitPos >= 8)
  124. {
  125. m_bitPos -= 8;
  126. ++m_bytePos;
  127. }
  128. }
  129. if (m_bytePos == m_outputBlockSize)
  130. {
  131. FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
  132. m_bytePos = m_bitPos = 0;
  133. }
  134. }
  135. if (messageEnd)
  136. {
  137. FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
  138. m_bytePos = m_bitPos = 0;
  139. }
  140. FILTER_END_NO_MESSAGE_END;
  141. }
  142. void BaseN_Decoder::InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive)
  143. {
  144. std::fill(lookup, lookup+256, -1);
  145. for (unsigned int i=0; i<base; i++)
  146. {
  147. if (caseInsensitive && isalpha(alphabet[i]))
  148. {
  149. assert(lookup[toupper(alphabet[i])] == -1);
  150. lookup[toupper(alphabet[i])] = i;
  151. assert(lookup[tolower(alphabet[i])] == -1);
  152. lookup[tolower(alphabet[i])] = i;
  153. }
  154. else
  155. {
  156. assert(lookup[alphabet[i]] == -1);
  157. lookup[alphabet[i]] = i;
  158. }
  159. }
  160. }
  161. void Grouper::IsolatedInitialize(const NameValuePairs &parameters)
  162. {
  163. m_groupSize = parameters.GetIntValueWithDefault(Name::GroupSize(), 0);
  164. ConstByteArrayParameter separator, terminator;
  165. if (m_groupSize)
  166. parameters.GetRequiredParameter("Grouper", Name::Separator(), separator);
  167. else
  168. parameters.GetValue(Name::Separator(), separator);
  169. parameters.GetValue(Name::Terminator(), terminator);
  170. m_separator.Assign(separator.begin(), separator.size());
  171. m_terminator.Assign(terminator.begin(), terminator.size());
  172. m_counter = 0;
  173. }
  174. size_t Grouper::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
  175. {
  176. FILTER_BEGIN;
  177. if (m_groupSize)
  178. {
  179. while (m_inputPosition < length)
  180. {
  181. if (m_counter == m_groupSize)
  182. {
  183. FILTER_OUTPUT(1, m_separator, m_separator.size(), 0);
  184. m_counter = 0;
  185. }
  186. size_t len;
  187. FILTER_OUTPUT2(2, len = STDMIN(length-m_inputPosition, m_groupSize-m_counter),
  188. begin+m_inputPosition, len, 0);
  189. m_inputPosition += len;
  190. m_counter += len;
  191. }
  192. }
  193. else
  194. FILTER_OUTPUT(3, begin, length, 0);
  195. if (messageEnd)
  196. {
  197. FILTER_OUTPUT(4, m_terminator, m_terminator.size(), messageEnd);
  198. m_counter = 0;
  199. }
  200. FILTER_END_NO_MESSAGE_END
  201. }
  202. NAMESPACE_END
  203. #endif