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.

streams.h 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef STARWELS_STREAMS_H
  6. #define STARWELS_STREAMS_H
  7. #include "support/allocators/zeroafterfree.h"
  8. #include "serialize.h"
  9. #include <algorithm>
  10. #include <assert.h>
  11. #include <ios>
  12. #include <limits>
  13. #include <map>
  14. #include <set>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <string>
  18. #include <string.h>
  19. #include <utility>
  20. #include <vector>
  21. template<typename Stream>
  22. class OverrideStream
  23. {
  24. Stream* stream;
  25. const int nType;
  26. const int nVersion;
  27. public:
  28. OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
  29. template<typename T>
  30. OverrideStream<Stream>& operator<<(const T& obj)
  31. {
  32. // Serialize to this stream
  33. ::Serialize(*this, obj);
  34. return (*this);
  35. }
  36. template<typename T>
  37. OverrideStream<Stream>& operator>>(T& obj)
  38. {
  39. // Unserialize from this stream
  40. ::Unserialize(*this, obj);
  41. return (*this);
  42. }
  43. void write(const char* pch, size_t nSize)
  44. {
  45. stream->write(pch, nSize);
  46. }
  47. void read(char* pch, size_t nSize)
  48. {
  49. stream->read(pch, nSize);
  50. }
  51. int GetVersion() const { return nVersion; }
  52. int GetType() const { return nType; }
  53. };
  54. template<typename S>
  55. OverrideStream<S> WithOrVersion(S* s, int nVersionFlag)
  56. {
  57. return OverrideStream<S>(s, s->GetType(), s->GetVersion() | nVersionFlag);
  58. }
  59. /* Minimal stream for overwriting and/or appending to an existing byte vector
  60. *
  61. * The referenced vector will grow as necessary
  62. */
  63. class CVectorWriter
  64. {
  65. public:
  66. /*
  67. * @param[in] nTypeIn Serialization Type
  68. * @param[in] nVersionIn Serialization Version (including any flags)
  69. * @param[in] vchDataIn Referenced byte vector to overwrite/append
  70. * @param[in] nPosIn Starting position. Vector index where writes should start. The vector will initially
  71. * grow as necessary to max(index, vec.size()). So to append, use vec.size().
  72. */
  73. CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn) : nType(nTypeIn), nVersion(nVersionIn), vchData(vchDataIn), nPos(nPosIn)
  74. {
  75. if(nPos > vchData.size())
  76. vchData.resize(nPos);
  77. }
  78. /*
  79. * (other params same as above)
  80. * @param[in] args A list of items to serialize starting at nPos.
  81. */
  82. template <typename... Args>
  83. CVectorWriter(int nTypeIn, int nVersionIn, std::vector<unsigned char>& vchDataIn, size_t nPosIn, Args&&... args) : CVectorWriter(nTypeIn, nVersionIn, vchDataIn, nPosIn)
  84. {
  85. ::SerializeMany(*this, std::forward<Args>(args)...);
  86. }
  87. void write(const char* pch, size_t nSize)
  88. {
  89. assert(nPos <= vchData.size());
  90. size_t nOverwrite = std::min(nSize, vchData.size() - nPos);
  91. if (nOverwrite) {
  92. memcpy(vchData.data() + nPos, reinterpret_cast<const unsigned char*>(pch), nOverwrite);
  93. }
  94. if (nOverwrite < nSize) {
  95. vchData.insert(vchData.end(), reinterpret_cast<const unsigned char*>(pch) + nOverwrite, reinterpret_cast<const unsigned char*>(pch) + nSize);
  96. }
  97. nPos += nSize;
  98. }
  99. template<typename T>
  100. CVectorWriter& operator<<(const T& obj)
  101. {
  102. // Serialize to this stream
  103. ::Serialize(*this, obj);
  104. return (*this);
  105. }
  106. int GetVersion() const
  107. {
  108. return nVersion;
  109. }
  110. int GetType() const
  111. {
  112. return nType;
  113. }
  114. void seek(size_t nSize)
  115. {
  116. nPos += nSize;
  117. if(nPos > vchData.size())
  118. vchData.resize(nPos);
  119. }
  120. private:
  121. const int nType;
  122. const int nVersion;
  123. std::vector<unsigned char>& vchData;
  124. size_t nPos;
  125. };
  126. /** Double ended buffer combining vector and stream-like interfaces.
  127. *
  128. * >> and << read and write unformatted data using the above serialization templates.
  129. * Fills with data in linear time; some stringstream implementations take N^2 time.
  130. */
  131. class CDataStream
  132. {
  133. protected:
  134. typedef CSerializeData vector_type;
  135. vector_type vch;
  136. unsigned int nReadPos;
  137. int nType;
  138. int nVersion;
  139. public:
  140. typedef vector_type::allocator_type allocator_type;
  141. typedef vector_type::size_type size_type;
  142. typedef vector_type::difference_type difference_type;
  143. typedef vector_type::reference reference;
  144. typedef vector_type::const_reference const_reference;
  145. typedef vector_type::value_type value_type;
  146. typedef vector_type::iterator iterator;
  147. typedef vector_type::const_iterator const_iterator;
  148. typedef vector_type::reverse_iterator reverse_iterator;
  149. explicit CDataStream(int nTypeIn, int nVersionIn)
  150. {
  151. Init(nTypeIn, nVersionIn);
  152. }
  153. CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
  154. {
  155. Init(nTypeIn, nVersionIn);
  156. }
  157. CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
  158. {
  159. Init(nTypeIn, nVersionIn);
  160. }
  161. CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
  162. {
  163. Init(nTypeIn, nVersionIn);
  164. }
  165. CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
  166. {
  167. Init(nTypeIn, nVersionIn);
  168. }
  169. CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
  170. {
  171. Init(nTypeIn, nVersionIn);
  172. }
  173. template <typename... Args>
  174. CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
  175. {
  176. Init(nTypeIn, nVersionIn);
  177. ::SerializeMany(*this, std::forward<Args>(args)...);
  178. }
  179. void Init(int nTypeIn, int nVersionIn)
  180. {
  181. nReadPos = 0;
  182. nType = nTypeIn;
  183. nVersion = nVersionIn;
  184. }
  185. CDataStream& operator+=(const CDataStream& b)
  186. {
  187. vch.insert(vch.end(), b.begin(), b.end());
  188. return *this;
  189. }
  190. friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
  191. {
  192. CDataStream ret = a;
  193. ret += b;
  194. return (ret);
  195. }
  196. std::string str() const
  197. {
  198. return (std::string(begin(), end()));
  199. }
  200. //
  201. // Vector subset
  202. //
  203. const_iterator begin() const { return vch.begin() + nReadPos; }
  204. iterator begin() { return vch.begin() + nReadPos; }
  205. const_iterator end() const { return vch.end(); }
  206. iterator end() { return vch.end(); }
  207. size_type size() const { return vch.size() - nReadPos; }
  208. bool empty() const { return vch.size() == nReadPos; }
  209. void resize(size_type n, value_type c=0) { vch.resize(n + nReadPos, c); }
  210. void reserve(size_type n) { vch.reserve(n + nReadPos); }
  211. const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
  212. reference operator[](size_type pos) { return vch[pos + nReadPos]; }
  213. void clear() { vch.clear(); nReadPos = 0; }
  214. iterator insert(iterator it, const char& x=char()) { return vch.insert(it, x); }
  215. void insert(iterator it, size_type n, const char& x) { vch.insert(it, n, x); }
  216. value_type* data() { return vch.data() + nReadPos; }
  217. const value_type* data() const { return vch.data() + nReadPos; }
  218. void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
  219. {
  220. if (last == first) return;
  221. assert(last - first > 0);
  222. if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
  223. {
  224. // special case for inserting at the front when there's room
  225. nReadPos -= (last - first);
  226. memcpy(&vch[nReadPos], &first[0], last - first);
  227. }
  228. else
  229. vch.insert(it, first, last);
  230. }
  231. void insert(iterator it, const char* first, const char* last)
  232. {
  233. if (last == first) return;
  234. assert(last - first > 0);
  235. if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
  236. {
  237. // special case for inserting at the front when there's room
  238. nReadPos -= (last - first);
  239. memcpy(&vch[nReadPos], &first[0], last - first);
  240. }
  241. else
  242. vch.insert(it, first, last);
  243. }
  244. iterator erase(iterator it)
  245. {
  246. if (it == vch.begin() + nReadPos)
  247. {
  248. // special case for erasing from the front
  249. if (++nReadPos >= vch.size())
  250. {
  251. // whenever we reach the end, we take the opportunity to clear the buffer
  252. nReadPos = 0;
  253. return vch.erase(vch.begin(), vch.end());
  254. }
  255. return vch.begin() + nReadPos;
  256. }
  257. else
  258. return vch.erase(it);
  259. }
  260. iterator erase(iterator first, iterator last)
  261. {
  262. if (first == vch.begin() + nReadPos)
  263. {
  264. // special case for erasing from the front
  265. if (last == vch.end())
  266. {
  267. nReadPos = 0;
  268. return vch.erase(vch.begin(), vch.end());
  269. }
  270. else
  271. {
  272. nReadPos = (last - vch.begin());
  273. return last;
  274. }
  275. }
  276. else
  277. return vch.erase(first, last);
  278. }
  279. inline void Compact()
  280. {
  281. vch.erase(vch.begin(), vch.begin() + nReadPos);
  282. nReadPos = 0;
  283. }
  284. bool Rewind(size_type n)
  285. {
  286. // Rewind by n characters if the buffer hasn't been compacted yet
  287. if (n > nReadPos)
  288. return false;
  289. nReadPos -= n;
  290. return true;
  291. }
  292. //
  293. // Stream subset
  294. //
  295. bool eof() const { return size() == 0; }
  296. CDataStream* rdbuf() { return this; }
  297. int in_avail() { return size(); }
  298. void SetType(int n) { nType = n; }
  299. int GetType() const { return nType; }
  300. void SetVersion(int n) { nVersion = n; }
  301. int GetVersion() const { return nVersion; }
  302. void read(char* pch, size_t nSize)
  303. {
  304. if (nSize == 0) return;
  305. // Read from the beginning of the buffer
  306. unsigned int nReadPosNext = nReadPos + nSize;
  307. if (nReadPosNext >= vch.size())
  308. {
  309. if (nReadPosNext > vch.size())
  310. {
  311. throw std::ios_base::failure("CDataStream::read(): end of data");
  312. }
  313. memcpy(pch, &vch[nReadPos], nSize);
  314. nReadPos = 0;
  315. vch.clear();
  316. return;
  317. }
  318. memcpy(pch, &vch[nReadPos], nSize);
  319. nReadPos = nReadPosNext;
  320. }
  321. void ignore(int nSize)
  322. {
  323. // Ignore from the beginning of the buffer
  324. if (nSize < 0) {
  325. throw std::ios_base::failure("CDataStream::ignore(): nSize negative");
  326. }
  327. unsigned int nReadPosNext = nReadPos + nSize;
  328. if (nReadPosNext >= vch.size())
  329. {
  330. if (nReadPosNext > vch.size())
  331. throw std::ios_base::failure("CDataStream::ignore(): end of data");
  332. nReadPos = 0;
  333. vch.clear();
  334. return;
  335. }
  336. nReadPos = nReadPosNext;
  337. }
  338. void write(const char* pch, size_t nSize)
  339. {
  340. // Write to the end of the buffer
  341. vch.insert(vch.end(), pch, pch + nSize);
  342. }
  343. template<typename Stream>
  344. void Serialize(Stream& s) const
  345. {
  346. // Special case: stream << stream concatenates like stream += stream
  347. if (!vch.empty())
  348. s.write((char*)vch.data(), vch.size() * sizeof(value_type));
  349. }
  350. template<typename T>
  351. CDataStream& operator<<(const T& obj)
  352. {
  353. // Serialize to this stream
  354. ::Serialize(*this, obj);
  355. return (*this);
  356. }
  357. template<typename T>
  358. CDataStream& operator>>(T& obj)
  359. {
  360. // Unserialize from this stream
  361. ::Unserialize(*this, obj);
  362. return (*this);
  363. }
  364. void GetAndClear(CSerializeData &d) {
  365. d.insert(d.end(), begin(), end());
  366. clear();
  367. }
  368. /**
  369. * XOR the contents of this stream with a certain key.
  370. *
  371. * @param[in] key The key used to XOR the data in this stream.
  372. */
  373. void Xor(const std::vector<unsigned char>& key)
  374. {
  375. if (key.size() == 0) {
  376. return;
  377. }
  378. for (size_type i = 0, j = 0; i != size(); i++) {
  379. vch[i] ^= key[j++];
  380. // This potentially acts on very many bytes of data, so it's
  381. // important that we calculate `j`, i.e. the `key` index in this
  382. // way instead of doing a %, which would effectively be a division
  383. // for each byte Xor'd -- much slower than need be.
  384. if (j == key.size())
  385. j = 0;
  386. }
  387. }
  388. };
  389. /** Non-refcounted RAII wrapper for FILE*
  390. *
  391. * Will automatically close the file when it goes out of scope if not null.
  392. * If you're returning the file pointer, return file.release().
  393. * If you need to close the file early, use file.fclose() instead of fclose(file).
  394. */
  395. class CAutoFile
  396. {
  397. private:
  398. // Disallow copies
  399. CAutoFile(const CAutoFile&);
  400. CAutoFile& operator=(const CAutoFile&);
  401. const int nType;
  402. const int nVersion;
  403. FILE* file;
  404. public:
  405. CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
  406. {
  407. file = filenew;
  408. }
  409. ~CAutoFile()
  410. {
  411. fclose();
  412. }
  413. void fclose()
  414. {
  415. if (file) {
  416. ::fclose(file);
  417. file = nullptr;
  418. }
  419. }
  420. /** Get wrapped FILE* with transfer of ownership.
  421. * @note This will invalidate the CAutoFile object, and makes it the responsibility of the caller
  422. * of this function to clean up the returned FILE*.
  423. */
  424. FILE* release() { FILE* ret = file; file = nullptr; return ret; }
  425. /** Get wrapped FILE* without transfer of ownership.
  426. * @note Ownership of the FILE* will remain with this class. Use this only if the scope of the
  427. * CAutoFile outlives use of the passed pointer.
  428. */
  429. FILE* Get() const { return file; }
  430. /** Return true if the wrapped FILE* is nullptr, false otherwise.
  431. */
  432. bool IsNull() const { return (file == nullptr); }
  433. //
  434. // Stream subset
  435. //
  436. int GetType() const { return nType; }
  437. int GetVersion() const { return nVersion; }
  438. void read(char* pch, size_t nSize)
  439. {
  440. if (!file)
  441. throw std::ios_base::failure("CAutoFile::read: file handle is nullptr");
  442. if (fread(pch, 1, nSize, file) != nSize)
  443. throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
  444. }
  445. void ignore(size_t nSize)
  446. {
  447. if (!file)
  448. throw std::ios_base::failure("CAutoFile::ignore: file handle is nullptr");
  449. unsigned char data[4096];
  450. while (nSize > 0) {
  451. size_t nNow = std::min<size_t>(nSize, sizeof(data));
  452. if (fread(data, 1, nNow, file) != nNow)
  453. throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
  454. nSize -= nNow;
  455. }
  456. }
  457. void write(const char* pch, size_t nSize)
  458. {
  459. if (!file)
  460. throw std::ios_base::failure("CAutoFile::write: file handle is nullptr");
  461. if (fwrite(pch, 1, nSize, file) != nSize)
  462. throw std::ios_base::failure("CAutoFile::write: write failed");
  463. }
  464. template<typename T>
  465. CAutoFile& operator<<(const T& obj)
  466. {
  467. // Serialize to this stream
  468. if (!file)
  469. throw std::ios_base::failure("CAutoFile::operator<<: file handle is nullptr");
  470. ::Serialize(*this, obj);
  471. return (*this);
  472. }
  473. template<typename T>
  474. CAutoFile& operator>>(T& obj)
  475. {
  476. // Unserialize from this stream
  477. if (!file)
  478. throw std::ios_base::failure("CAutoFile::operator>>: file handle is nullptr");
  479. ::Unserialize(*this, obj);
  480. return (*this);
  481. }
  482. };
  483. /** Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to
  484. * deserialize from. It guarantees the ability to rewind a given number of bytes.
  485. *
  486. * Will automatically close the file when it goes out of scope if not null.
  487. * If you need to close the file early, use file.fclose() instead of fclose(file).
  488. */
  489. class CBufferedFile
  490. {
  491. private:
  492. // Disallow copies
  493. CBufferedFile(const CBufferedFile&);
  494. CBufferedFile& operator=(const CBufferedFile&);
  495. const int nType;
  496. const int nVersion;
  497. FILE *src; // source file
  498. uint64_t nSrcPos; // how many bytes have been read from source
  499. uint64_t nReadPos; // how many bytes have been read from this
  500. uint64_t nReadLimit; // up to which position we're allowed to read
  501. uint64_t nRewind; // how many bytes we guarantee to rewind
  502. std::vector<char> vchBuf; // the buffer
  503. protected:
  504. // read data from the source to fill the buffer
  505. bool Fill() {
  506. unsigned int pos = nSrcPos % vchBuf.size();
  507. unsigned int readNow = vchBuf.size() - pos;
  508. unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind;
  509. if (nAvail < readNow)
  510. readNow = nAvail;
  511. if (readNow == 0)
  512. return false;
  513. size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
  514. if (nBytes == 0) {
  515. throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed");
  516. } else {
  517. nSrcPos += nBytes;
  518. return true;
  519. }
  520. }
  521. public:
  522. CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
  523. nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
  524. {
  525. src = fileIn;
  526. }
  527. ~CBufferedFile()
  528. {
  529. fclose();
  530. }
  531. int GetVersion() const { return nVersion; }
  532. int GetType() const { return nType; }
  533. void fclose()
  534. {
  535. if (src) {
  536. ::fclose(src);
  537. src = nullptr;
  538. }
  539. }
  540. // check whether we're at the end of the source file
  541. bool eof() const {
  542. return nReadPos == nSrcPos && feof(src);
  543. }
  544. // read a number of bytes
  545. void read(char *pch, size_t nSize) {
  546. if (nSize + nReadPos > nReadLimit)
  547. throw std::ios_base::failure("Read attempted past buffer limit");
  548. if (nSize + nRewind > vchBuf.size())
  549. throw std::ios_base::failure("Read larger than buffer size");
  550. while (nSize > 0) {
  551. if (nReadPos == nSrcPos)
  552. Fill();
  553. unsigned int pos = nReadPos % vchBuf.size();
  554. size_t nNow = nSize;
  555. if (nNow + pos > vchBuf.size())
  556. nNow = vchBuf.size() - pos;
  557. if (nNow + nReadPos > nSrcPos)
  558. nNow = nSrcPos - nReadPos;
  559. memcpy(pch, &vchBuf[pos], nNow);
  560. nReadPos += nNow;
  561. pch += nNow;
  562. nSize -= nNow;
  563. }
  564. }
  565. // return the current reading position
  566. uint64_t GetPos() {
  567. return nReadPos;
  568. }
  569. // rewind to a given reading position
  570. bool SetPos(uint64_t nPos) {
  571. nReadPos = nPos;
  572. if (nReadPos + nRewind < nSrcPos) {
  573. nReadPos = nSrcPos - nRewind;
  574. return false;
  575. } else if (nReadPos > nSrcPos) {
  576. nReadPos = nSrcPos;
  577. return false;
  578. } else {
  579. return true;
  580. }
  581. }
  582. bool Seek(uint64_t nPos) {
  583. long nLongPos = nPos;
  584. if (nPos != (uint64_t)nLongPos)
  585. return false;
  586. if (fseek(src, nLongPos, SEEK_SET))
  587. return false;
  588. nLongPos = ftell(src);
  589. nSrcPos = nLongPos;
  590. nReadPos = nLongPos;
  591. return true;
  592. }
  593. // prevent reading beyond a certain position
  594. // no argument removes the limit
  595. bool SetLimit(uint64_t nPos = (uint64_t)(-1)) {
  596. if (nPos < nReadPos)
  597. return false;
  598. nReadLimit = nPos;
  599. return true;
  600. }
  601. template<typename T>
  602. CBufferedFile& operator>>(T& obj) {
  603. // Unserialize from this stream
  604. ::Unserialize(*this, obj);
  605. return (*this);
  606. }
  607. // search for a given byte in the stream, and remain positioned on it
  608. void FindByte(char ch) {
  609. while (true) {
  610. if (nReadPos == nSrcPos)
  611. Fill();
  612. if (vchBuf[nReadPos % vchBuf.size()] == ch)
  613. break;
  614. nReadPos++;
  615. }
  616. }
  617. };
  618. #endif // STARWELS_STREAMS_H