Team Fortress 2 Source Code as on 22/4/2020
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.

574 lines
12 KiB

  1. // queue.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "queue.h"
  5. #include "filters.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. static const unsigned int s_maxAutoNodeSize = 16*1024;
  8. // this class for use by ByteQueue only
  9. class ByteQueueNode
  10. {
  11. public:
  12. ByteQueueNode(size_t maxSize)
  13. : buf(maxSize)
  14. {
  15. m_head = m_tail = 0;
  16. next = 0;
  17. }
  18. inline size_t MaxSize() const {return buf.size();}
  19. inline size_t CurrentSize() const
  20. {
  21. return m_tail-m_head;
  22. }
  23. inline bool UsedUp() const
  24. {
  25. return (m_head==MaxSize());
  26. }
  27. inline void Clear()
  28. {
  29. m_head = m_tail = 0;
  30. }
  31. inline size_t Put(const byte *begin, size_t length)
  32. {
  33. // Avoid passing NULL to memcpy
  34. if (!begin || !length) return length;
  35. size_t l = STDMIN(length, MaxSize()-m_tail);
  36. if (buf+m_tail != begin)
  37. memcpy(buf+m_tail, begin, l);
  38. m_tail += l;
  39. return l;
  40. }
  41. inline size_t Peek(byte &outByte) const
  42. {
  43. if (m_tail==m_head)
  44. return 0;
  45. outByte=buf[m_head];
  46. return 1;
  47. }
  48. inline size_t Peek(byte *target, size_t copyMax) const
  49. {
  50. size_t len = STDMIN(copyMax, m_tail-m_head);
  51. memcpy(target, buf+m_head, len);
  52. return len;
  53. }
  54. inline size_t CopyTo(BufferedTransformation &target, const std::string &channel=DEFAULT_CHANNEL) const
  55. {
  56. size_t len = m_tail-m_head;
  57. target.ChannelPut(channel, buf+m_head, len);
  58. return len;
  59. }
  60. inline size_t CopyTo(BufferedTransformation &target, size_t copyMax, const std::string &channel=DEFAULT_CHANNEL) const
  61. {
  62. size_t len = STDMIN(copyMax, m_tail-m_head);
  63. target.ChannelPut(channel, buf+m_head, len);
  64. return len;
  65. }
  66. inline size_t Get(byte &outByte)
  67. {
  68. size_t len = Peek(outByte);
  69. m_head += len;
  70. return len;
  71. }
  72. inline size_t Get(byte *outString, size_t getMax)
  73. {
  74. size_t len = Peek(outString, getMax);
  75. m_head += len;
  76. return len;
  77. }
  78. inline size_t TransferTo(BufferedTransformation &target, const std::string &channel=DEFAULT_CHANNEL)
  79. {
  80. size_t len = m_tail-m_head;
  81. target.ChannelPutModifiable(channel, buf+m_head, len);
  82. m_head = m_tail;
  83. return len;
  84. }
  85. inline size_t TransferTo(BufferedTransformation &target, lword transferMax, const std::string &channel=DEFAULT_CHANNEL)
  86. {
  87. size_t len = UnsignedMin(m_tail-m_head, transferMax);
  88. target.ChannelPutModifiable(channel, buf+m_head, len);
  89. m_head += len;
  90. return len;
  91. }
  92. inline size_t Skip(size_t skipMax)
  93. {
  94. size_t len = STDMIN(skipMax, m_tail-m_head);
  95. m_head += len;
  96. return len;
  97. }
  98. inline byte operator[](size_t i) const
  99. {
  100. return buf[m_head+i];
  101. }
  102. ByteQueueNode *next;
  103. SecByteBlock buf;
  104. size_t m_head, m_tail;
  105. };
  106. // ********************************************************
  107. ByteQueue::ByteQueue(size_t nodeSize)
  108. : Bufferless<BufferedTransformation>(), m_autoNodeSize(!nodeSize), m_nodeSize(nodeSize)
  109. , m_head(NULL), m_tail(NULL), m_lazyString(NULL), m_lazyLength(0), m_lazyStringModifiable(false)
  110. {
  111. SetNodeSize(nodeSize);
  112. m_head = m_tail = new ByteQueueNode(m_nodeSize);
  113. }
  114. void ByteQueue::SetNodeSize(size_t nodeSize)
  115. {
  116. m_autoNodeSize = !nodeSize;
  117. m_nodeSize = m_autoNodeSize ? 256 : nodeSize;
  118. }
  119. ByteQueue::ByteQueue(const ByteQueue &copy)
  120. : Bufferless<BufferedTransformation>(copy), m_lazyString(NULL), m_lazyLength(0)
  121. {
  122. CopyFrom(copy);
  123. }
  124. void ByteQueue::CopyFrom(const ByteQueue &copy)
  125. {
  126. m_lazyLength = 0;
  127. m_autoNodeSize = copy.m_autoNodeSize;
  128. m_nodeSize = copy.m_nodeSize;
  129. m_head = m_tail = new ByteQueueNode(*copy.m_head);
  130. for (ByteQueueNode *current=copy.m_head->next; current; current=current->next)
  131. {
  132. m_tail->next = new ByteQueueNode(*current);
  133. m_tail = m_tail->next;
  134. }
  135. m_tail->next = NULL;
  136. Put(copy.m_lazyString, copy.m_lazyLength);
  137. }
  138. ByteQueue::~ByteQueue()
  139. {
  140. Destroy();
  141. }
  142. void ByteQueue::Destroy()
  143. {
  144. for (ByteQueueNode *next, *current=m_head; current; current=next)
  145. {
  146. next=current->next;
  147. delete current;
  148. }
  149. }
  150. void ByteQueue::IsolatedInitialize(const NameValuePairs &parameters)
  151. {
  152. m_nodeSize = parameters.GetIntValueWithDefault("NodeSize", 256);
  153. Clear();
  154. }
  155. lword ByteQueue::CurrentSize() const
  156. {
  157. lword size=0;
  158. for (ByteQueueNode *current=m_head; current; current=current->next)
  159. size += current->CurrentSize();
  160. return size + m_lazyLength;
  161. }
  162. bool ByteQueue::IsEmpty() const
  163. {
  164. return m_head==m_tail && m_head->CurrentSize()==0 && m_lazyLength==0;
  165. }
  166. void ByteQueue::Clear()
  167. {
  168. for (ByteQueueNode *next, *current=m_head->next; current; current=next)
  169. {
  170. next=current->next;
  171. delete current;
  172. }
  173. m_tail = m_head;
  174. m_head->Clear();
  175. m_head->next = NULL;
  176. m_lazyLength = 0;
  177. }
  178. size_t ByteQueue::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
  179. {
  180. CRYPTOPP_UNUSED(messageEnd), CRYPTOPP_UNUSED(blocking);
  181. if (m_lazyLength > 0)
  182. FinalizeLazyPut();
  183. size_t len;
  184. while ((len=m_tail->Put(inString, length)) < length)
  185. {
  186. inString += len;
  187. length -= len;
  188. if (m_autoNodeSize && m_nodeSize < s_maxAutoNodeSize)
  189. do
  190. {
  191. m_nodeSize *= 2;
  192. }
  193. while (m_nodeSize < length && m_nodeSize < s_maxAutoNodeSize);
  194. m_tail->next = new ByteQueueNode(STDMAX(m_nodeSize, length));
  195. m_tail = m_tail->next;
  196. }
  197. return 0;
  198. }
  199. void ByteQueue::CleanupUsedNodes()
  200. {
  201. // Test for m_head due to Enterprise Anlysis finding
  202. while (m_head && m_head != m_tail && m_head->UsedUp())
  203. {
  204. ByteQueueNode *temp=m_head;
  205. m_head=m_head->next;
  206. delete temp;
  207. }
  208. // Test for m_head due to Enterprise Anlysis finding
  209. if (m_head && m_head->CurrentSize() == 0)
  210. m_head->Clear();
  211. }
  212. void ByteQueue::LazyPut(const byte *inString, size_t size)
  213. {
  214. if (m_lazyLength > 0)
  215. FinalizeLazyPut();
  216. if (inString == m_tail->buf+m_tail->m_tail)
  217. Put(inString, size);
  218. else
  219. {
  220. m_lazyString = const_cast<byte *>(inString);
  221. m_lazyLength = size;
  222. m_lazyStringModifiable = false;
  223. }
  224. }
  225. void ByteQueue::LazyPutModifiable(byte *inString, size_t size)
  226. {
  227. if (m_lazyLength > 0)
  228. FinalizeLazyPut();
  229. m_lazyString = inString;
  230. m_lazyLength = size;
  231. m_lazyStringModifiable = true;
  232. }
  233. void ByteQueue::UndoLazyPut(size_t size)
  234. {
  235. if (m_lazyLength < size)
  236. throw InvalidArgument("ByteQueue: size specified for UndoLazyPut is too large");
  237. m_lazyLength -= size;
  238. }
  239. void ByteQueue::FinalizeLazyPut()
  240. {
  241. size_t len = m_lazyLength;
  242. m_lazyLength = 0;
  243. if (len)
  244. Put(m_lazyString, len);
  245. }
  246. size_t ByteQueue::Get(byte &outByte)
  247. {
  248. if (m_head->Get(outByte))
  249. {
  250. if (m_head->UsedUp())
  251. CleanupUsedNodes();
  252. return 1;
  253. }
  254. else if (m_lazyLength > 0)
  255. {
  256. outByte = *m_lazyString++;
  257. m_lazyLength--;
  258. return 1;
  259. }
  260. else
  261. return 0;
  262. }
  263. size_t ByteQueue::Get(byte *outString, size_t getMax)
  264. {
  265. ArraySink sink(outString, getMax);
  266. return (size_t)TransferTo(sink, getMax);
  267. }
  268. size_t ByteQueue::Peek(byte &outByte) const
  269. {
  270. if (m_head->Peek(outByte))
  271. return 1;
  272. else if (m_lazyLength > 0)
  273. {
  274. outByte = *m_lazyString;
  275. return 1;
  276. }
  277. else
  278. return 0;
  279. }
  280. size_t ByteQueue::Peek(byte *outString, size_t peekMax) const
  281. {
  282. ArraySink sink(outString, peekMax);
  283. return (size_t)CopyTo(sink, peekMax);
  284. }
  285. size_t ByteQueue::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
  286. {
  287. if (blocking)
  288. {
  289. lword bytesLeft = transferBytes;
  290. for (ByteQueueNode *current=m_head; bytesLeft && current; current=current->next)
  291. bytesLeft -= current->TransferTo(target, bytesLeft, channel);
  292. CleanupUsedNodes();
  293. size_t len = (size_t)STDMIN(bytesLeft, (lword)m_lazyLength);
  294. if (len)
  295. {
  296. if (m_lazyStringModifiable)
  297. target.ChannelPutModifiable(channel, m_lazyString, len);
  298. else
  299. target.ChannelPut(channel, m_lazyString, len);
  300. m_lazyString += len;
  301. m_lazyLength -= len;
  302. bytesLeft -= len;
  303. }
  304. transferBytes -= bytesLeft;
  305. return 0;
  306. }
  307. else
  308. {
  309. Walker walker(*this);
  310. size_t blockedBytes = walker.TransferTo2(target, transferBytes, channel, blocking);
  311. Skip(transferBytes);
  312. return blockedBytes;
  313. }
  314. }
  315. size_t ByteQueue::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
  316. {
  317. Walker walker(*this);
  318. walker.Skip(begin);
  319. lword transferBytes = end-begin;
  320. size_t blockedBytes = walker.TransferTo2(target, transferBytes, channel, blocking);
  321. begin += transferBytes;
  322. return blockedBytes;
  323. }
  324. void ByteQueue::Unget(byte inByte)
  325. {
  326. Unget(&inByte, 1);
  327. }
  328. void ByteQueue::Unget(const byte *inString, size_t length)
  329. {
  330. size_t len = STDMIN(length, m_head->m_head);
  331. length -= len;
  332. m_head->m_head -= len;
  333. memcpy(m_head->buf + m_head->m_head, inString + length, len);
  334. if (length > 0)
  335. {
  336. ByteQueueNode *newHead = new ByteQueueNode(length);
  337. newHead->next = m_head;
  338. m_head = newHead;
  339. m_head->Put(inString, length);
  340. }
  341. }
  342. const byte * ByteQueue::Spy(size_t &contiguousSize) const
  343. {
  344. contiguousSize = m_head->m_tail - m_head->m_head;
  345. if (contiguousSize == 0 && m_lazyLength > 0)
  346. {
  347. contiguousSize = m_lazyLength;
  348. return m_lazyString;
  349. }
  350. else
  351. return m_head->buf + m_head->m_head;
  352. }
  353. byte * ByteQueue::CreatePutSpace(size_t &size)
  354. {
  355. if (m_lazyLength > 0)
  356. FinalizeLazyPut();
  357. if (m_tail->m_tail == m_tail->MaxSize())
  358. {
  359. m_tail->next = new ByteQueueNode(STDMAX(m_nodeSize, size));
  360. m_tail = m_tail->next;
  361. }
  362. size = m_tail->MaxSize() - m_tail->m_tail;
  363. return m_tail->buf + m_tail->m_tail;
  364. }
  365. ByteQueue & ByteQueue::operator=(const ByteQueue &rhs)
  366. {
  367. Destroy();
  368. CopyFrom(rhs);
  369. return *this;
  370. }
  371. bool ByteQueue::operator==(const ByteQueue &rhs) const
  372. {
  373. const lword currentSize = CurrentSize();
  374. if (currentSize != rhs.CurrentSize())
  375. return false;
  376. Walker walker1(*this), walker2(rhs);
  377. byte b1, b2;
  378. while (walker1.Get(b1) && walker2.Get(b2))
  379. if (b1 != b2)
  380. return false;
  381. return true;
  382. }
  383. byte ByteQueue::operator[](lword i) const
  384. {
  385. for (ByteQueueNode *current=m_head; current; current=current->next)
  386. {
  387. if (i < current->CurrentSize())
  388. return (*current)[(size_t)i];
  389. i -= current->CurrentSize();
  390. }
  391. assert(i < m_lazyLength);
  392. return m_lazyString[i];
  393. }
  394. void ByteQueue::swap(ByteQueue &rhs)
  395. {
  396. std::swap(m_autoNodeSize, rhs.m_autoNodeSize);
  397. std::swap(m_nodeSize, rhs.m_nodeSize);
  398. std::swap(m_head, rhs.m_head);
  399. std::swap(m_tail, rhs.m_tail);
  400. std::swap(m_lazyString, rhs.m_lazyString);
  401. std::swap(m_lazyLength, rhs.m_lazyLength);
  402. std::swap(m_lazyStringModifiable, rhs.m_lazyStringModifiable);
  403. }
  404. // ********************************************************
  405. void ByteQueue::Walker::IsolatedInitialize(const NameValuePairs &parameters)
  406. {
  407. CRYPTOPP_UNUSED(parameters);
  408. m_node = m_queue.m_head;
  409. m_position = 0;
  410. m_offset = 0;
  411. m_lazyString = m_queue.m_lazyString;
  412. m_lazyLength = m_queue.m_lazyLength;
  413. }
  414. size_t ByteQueue::Walker::Get(byte &outByte)
  415. {
  416. ArraySink sink(&outByte, 1);
  417. return (size_t)TransferTo(sink, 1);
  418. }
  419. size_t ByteQueue::Walker::Get(byte *outString, size_t getMax)
  420. {
  421. ArraySink sink(outString, getMax);
  422. return (size_t)TransferTo(sink, getMax);
  423. }
  424. size_t ByteQueue::Walker::Peek(byte &outByte) const
  425. {
  426. ArraySink sink(&outByte, 1);
  427. return (size_t)CopyTo(sink, 1);
  428. }
  429. size_t ByteQueue::Walker::Peek(byte *outString, size_t peekMax) const
  430. {
  431. ArraySink sink(outString, peekMax);
  432. return (size_t)CopyTo(sink, peekMax);
  433. }
  434. size_t ByteQueue::Walker::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
  435. {
  436. lword bytesLeft = transferBytes;
  437. size_t blockedBytes = 0;
  438. while (m_node)
  439. {
  440. size_t len = (size_t)STDMIN(bytesLeft, (lword)m_node->CurrentSize()-m_offset);
  441. blockedBytes = target.ChannelPut2(channel, m_node->buf+m_node->m_head+m_offset, len, 0, blocking);
  442. if (blockedBytes)
  443. goto done;
  444. m_position += len;
  445. bytesLeft -= len;
  446. if (!bytesLeft)
  447. {
  448. m_offset += len;
  449. goto done;
  450. }
  451. m_node = m_node->next;
  452. m_offset = 0;
  453. }
  454. if (bytesLeft && m_lazyLength)
  455. {
  456. size_t len = (size_t)STDMIN(bytesLeft, (lword)m_lazyLength);
  457. blockedBytes = target.ChannelPut2(channel, m_lazyString, len, 0, blocking);
  458. if (blockedBytes)
  459. goto done;
  460. m_lazyString += len;
  461. m_lazyLength -= len;
  462. bytesLeft -= len;
  463. }
  464. done:
  465. transferBytes -= bytesLeft;
  466. return blockedBytes;
  467. }
  468. size_t ByteQueue::Walker::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
  469. {
  470. Walker walker(*this);
  471. walker.Skip(begin);
  472. lword transferBytes = end-begin;
  473. size_t blockedBytes = walker.TransferTo2(target, transferBytes, channel, blocking);
  474. begin += transferBytes;
  475. return blockedBytes;
  476. }
  477. NAMESPACE_END
  478. #endif