00001
00011 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00012 #pragma once
00013 #endif
00014 #ifndef COMMON_STREAM_H_
00015 #define COMMON_STREAM_H_
00016
00017 namespace common
00018 {
00019
00024
00025 extern const size_t BUFFER_SIZE;
00026
00028 void _ThrowBufEndError(const tchar *File, int Line);
00029
00031 enum DECODE_TOLERANCE
00032 {
00033 DECODE_TOLERANCE_NONE,
00034 DECODE_TOLERANCE_WHITESPACE,
00035 DECODE_TOLERANCE_ALL,
00036 };
00037
00038
00040 class Stream
00041 {
00042 public:
00043 virtual ~Stream() { }
00044
00047
00049
00050 virtual void Write(const void *Data, size_t Size);
00051 virtual void Flush() { }
00052
00054 template <typename T>
00055 void WriteEx(const T &x) { return Write(&x, sizeof(x)); }
00057
00058 void WriteString1(const string &s);
00059 void WriteString1(const char *s);
00060 void WriteString1(const char *s, unsigned NumChars);
00061 #ifdef WIN32
00062 void WriteString1(const wstring &s);
00063 void WriteString1(const wchar_t *s);
00064 void WriteString1(const wchar_t *s, unsigned NumChars);
00065 #endif
00067
00068 void WriteString2(const string &s);
00069 void WriteString2(const char *s);
00070 void WriteString2(const char *s, unsigned NumChars);
00071 #ifdef WIN32
00072 void WriteString2(const wstring &s);
00073 void WriteString2(const wchar_t *s);
00074 void WriteString2(const wchar_t *s, unsigned NumChars);
00075 #endif
00077 void WriteString4(const string &s);
00078 void WriteString4(const char *s);
00079 void WriteString4(const char *s, unsigned NumChars);
00080 #ifdef WIN32
00081 void WriteString4(const wstring &s);
00082 void WriteString4(const wchar_t *s);
00083 void WriteString4(const wchar_t *s, unsigned NumChars);
00084 #endif
00086 void WriteStringF(const string &s);
00087 #ifdef WIN32
00088 void WriteStringF(const wstring &s);
00089 #endif
00091 void WriteBool(bool b);
00092
00094
00097
00099
00104 virtual size_t Read(void *Data, size_t MaxLength);
00106
00109 virtual void MustRead(void *Data, size_t Length);
00111
00114 virtual bool End();
00116
00119 virtual size_t Skip(size_t MaxLength);
00120
00122 template <typename T>
00123 void ReadEx(T *x) { MustRead(x, sizeof(*x)); }
00125 void ReadString1(string *s);
00127 void ReadString2(string *s);
00129 void ReadString4(string *s);
00131 void ReadStringF(string *s, size_t NumChars);
00133 void ReadStringToEnd(string *s);
00135 #ifdef WIN32
00136 void ReadString1(wstring *s);
00137 void ReadString2(wstring *s);
00138 void ReadString4(wstring *s);
00139 void ReadStringF(wstring *s, size_t NumChars);
00140 void ReadStringToEnd(wstring *s);
00141 #endif
00143 void ReadBool(bool *b);
00145
00146 void MustSkip(size_t Length);
00147
00149
00152
00153
00154 size_t CopyFrom(Stream *s, size_t Size);
00156
00157 void MustCopyFrom(Stream *s, size_t Size);
00159 size_t CopyFromToEnd(Stream *s);
00161 };
00162
00164 class SeekableStream : public Stream
00165 {
00166 public:
00167
00169 virtual bool End();
00171 virtual size_t Skip(size_t MaxLength);
00172
00174
00175 virtual size_t GetSize();
00177
00178 virtual int GetPos();
00180
00182 virtual void SetPos(int pos);
00184
00186 virtual void SetPosFromCurrent(int pos);
00188
00190 virtual void SetPosFromEnd(int pos);
00192
00193 virtual void Rewind();
00195
00197 virtual void SetSize(size_t Size);
00199
00200 virtual void Truncate();
00202
00203 virtual void Clear();
00204 };
00205
00207
00212 class NullStream : public SeekableStream
00213 {
00214 public:
00215 virtual void Write(const void *Data, size_t Size) { }
00216 virtual size_t Read(void *Data, size_t MaxLength) { return 0; }
00217 virtual bool End() { return true; }
00218 virtual size_t Skip(size_t MaxLength) { return 0; }
00219 virtual size_t GetSize() { return 0; }
00220 virtual int GetPos() { return 0; }
00221 virtual void SetPos(int pos) { }
00222 virtual void SetPosFromCurrent(int pos) { }
00223 virtual void SetPosFromEnd(int pos) { }
00224 virtual void Rewind() { }
00225 virtual void SetSize(size_t Size) { }
00226 virtual void Truncate() { }
00227 virtual void Clear() { }
00228 };
00229
00231
00243 class CharWriter
00244 {
00245 private:
00246 Stream *m_Stream;
00247 std::vector<char> m_Buf;
00248 uint m_BufIndex;
00249
00250
00251
00252 void DoFlush() { m_Stream->Write(&m_Buf[0], m_BufIndex); m_BufIndex = 0; }
00253
00254 public:
00255 CharWriter(Stream *a_Stream) : m_Stream(a_Stream), m_BufIndex(0) { m_Buf.resize(BUFFER_SIZE); }
00256 ~CharWriter();
00257
00259 void WriteChar(char Ch) { if (m_BufIndex == BUFFER_SIZE) DoFlush(); m_Buf[m_BufIndex++] = Ch; }
00261 void WriteString(const string &s);
00263 void WriteString(const char *s) { WriteString(s, strlen(s)); }
00265 void WriteString(const char *s, size_t NumChars) { WriteData(s, NumChars); }
00267 void WriteData(const void *Data, size_t Length);
00269 template <typename T> void WriteEx(const T &x) { string s; SthToStr<T>(&s, x); WriteString(s); }
00271 void Flush() { if (m_BufIndex > 0) DoFlush(); }
00272 };
00273
00274 #ifdef WIN32
00276
00277 class WCharWriter
00278 {
00279 private:
00280 Stream *m_Stream;
00281 std::vector<wchar_t> m_Buf;
00282 uint m_BufIndex;
00283
00284
00285
00286 void DoFlush() { m_Stream->Write(&m_Buf[0], m_BufIndex * sizeof(wchar_t)); m_BufIndex = 0; }
00287
00288 public:
00289 WCharWriter(Stream *a_Stream) : m_Stream(a_Stream), m_BufIndex(0) { m_Buf.resize(BUFFER_SIZE); }
00290 ~WCharWriter();
00291
00293 void WriteChar(wchar_t Ch) { if (m_BufIndex == BUFFER_SIZE) DoFlush(); m_Buf[m_BufIndex++] = Ch; }
00295 void WriteString(const wstring &s);
00297 void WriteString(const wchar_t *s) { WriteString(s, wcslen(s)); }
00299 void WriteString(const wchar_t *s, size_t NumChars);
00301 void Flush() { if (m_BufIndex > 0) DoFlush(); }
00302 };
00303 #endif
00304
00306
00315 class CharReader
00316 {
00317 private:
00318 Stream *m_Stream;
00319 std::vector<char> m_Buf;
00320
00321 uint m_BufBeg;
00322
00323 uint m_BufEnd;
00324
00325
00326
00327
00328
00329 bool EnsureNewChars();
00330
00331 public:
00332 CharReader(Stream *a_Stream) : m_Stream(a_Stream), m_BufBeg(0), m_BufEnd(0) { m_Buf.resize(BUFFER_SIZE); }
00333
00335 bool End() {
00336 return (m_BufBeg == m_BufEnd) && m_Stream->End();
00337 }
00339
00340 bool ReadChar(char *Out) { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } *Out = m_Buf[m_BufBeg++]; return true; }
00342
00343 char MustReadChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } return m_Buf[m_BufBeg++]; }
00345
00346 bool PeekChar(char *Out) { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } *Out = m_Buf[m_BufBeg]; return true; }
00348
00349 char MustPeekChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } return m_Buf[m_BufBeg]; }
00351
00353 size_t ReadString(string *Out, size_t MaxLength);
00355
00357 void MustReadString(string *Out, size_t Length);
00359
00361 size_t ReadString(char *Out, size_t MaxLength);
00363
00365 void MustReadString(char *Out, size_t Length);
00367
00369 size_t ReadData(void *Out, size_t MaxLength);
00371
00373 void MustReadData(void *Out, size_t Length);
00375
00376 bool SkipChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } m_BufBeg++; return true; }
00378
00379 void MustSkipChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } m_BufBeg++; }
00381
00382 size_t Skip(size_t MaxLength);
00384
00385 void MustSkip(size_t Length);
00387
00390 bool ReadLine(string *Out);
00392
00395 void MustReadLine(string *Out);
00396 };
00397
00398 #ifdef WIN32
00400
00401 class WCharReader
00402 {
00403 private:
00404 Stream *m_Stream;
00405 std::vector<wchar_t> m_Buf;
00406
00407 uint m_BufBeg;
00408
00409 uint m_BufEnd;
00410
00411
00412
00413
00414
00415 bool EnsureNewChars();
00416
00417 public:
00418 WCharReader(Stream *a_Stream) : m_Stream(a_Stream), m_BufBeg(0), m_BufEnd(0) { m_Buf.resize(BUFFER_SIZE); }
00419
00421 bool End() {
00422 return (m_BufBeg == m_BufEnd) && m_Stream->End();
00423 }
00425
00426 bool ReadChar(wchar_t *Out) { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } *Out = m_Buf[m_BufBeg++]; return true; }
00428
00429 wchar_t MustReadChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } return m_Buf[m_BufBeg++]; }
00431
00432 bool PeekChar(wchar_t *Out) { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } *Out = m_Buf[m_BufBeg]; return true; }
00434
00435 wchar_t MustPeekChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } return m_Buf[m_BufBeg]; }
00437
00439 size_t ReadString(wstring *Out, size_t MaxLength);
00441
00443 void MustReadString(wstring *Out, size_t Length);
00445
00447 size_t ReadString(wchar_t *Out, size_t MaxLength);
00449
00451 void MustReadString(wchar_t *Out, size_t Length);
00453
00454 bool SkipChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } m_BufBeg++; return true; }
00456
00457 void MustSkipChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } m_BufBeg++; }
00459
00460 size_t Skip(size_t MaxLength);
00462
00463 void MustSkip(size_t Length);
00465
00468 bool ReadLine(wstring *Out);
00470
00473 void MustReadLine(wstring *Out);
00474 };
00475 #endif
00476
00477 #ifdef _UNICODE
00478 #define TCharWriter WCharWriter
00479 #define TCharReader WCharReader
00480 #else
00481 #define TCharWriter CharWriter
00482 #define TCharReader CharReader
00483 #endif
00484
00486
00487 class MemoryStream : public SeekableStream
00488 {
00489 private:
00490 char *m_Data;
00491 size_t m_Size;
00492 int m_Pos;
00493 bool m_InternalAlloc;
00494
00495 public:
00499 MemoryStream(size_t Size, void *Data = 0);
00500 virtual ~MemoryStream();
00501
00502 virtual void Write(const void *Data, size_t Size);
00503 virtual size_t Read(void *Data, size_t Size);
00504 virtual void MustRead(void *Data, size_t Size);
00505
00506 virtual size_t GetSize();
00507 virtual int GetPos();
00508 virtual void SetPos(int pos);
00509 virtual void Rewind();
00510
00511 char *Data() { return m_Data; }
00512 };
00513
00515
00516 class VectorStream : public SeekableStream
00517 {
00518 private:
00519 char *m_Data;
00520 size_t m_Size;
00521 size_t m_Capacity;
00522 int m_Pos;
00523
00524
00525
00526 void Reserve(size_t NewCapacity);
00527
00528 public:
00529 VectorStream();
00530 virtual ~VectorStream();
00531
00532 virtual void Write(const void *Data, size_t Size);
00533 virtual size_t Read(void *Data, size_t Size);
00534 virtual void MustRead(void *Data, size_t Size);
00535 virtual size_t GetSize() { return m_Size; }
00536 virtual int GetPos() { return m_Pos; }
00537 virtual void SetPos(int pos) { m_Pos = pos; }
00538 virtual void Rewind() { m_Pos = 0; }
00539 virtual void SetSize(size_t Size);
00540
00541 size_t GetCapacity() { return m_Capacity; }
00542 void SetCapacity(size_t Capacity);
00545 char *Data() { return m_Data; }
00546 };
00547
00549
00550 class StringStream : public SeekableStream
00551 {
00552 private:
00553 std::string *m_Data;
00554 int m_Pos;
00555 bool m_InternalAlloc;
00556
00557 public:
00560 StringStream(string *Data = 0);
00561 virtual ~StringStream();
00562
00563 virtual void Write(const void *Data, size_t Size);
00564 virtual size_t Read(void *Data, size_t Size);
00565 virtual void MustRead(void *Data, size_t Size);
00566 virtual size_t GetSize() { return m_Data->length(); }
00567 virtual int GetPos() { return m_Pos; }
00568 virtual void SetPos(int pos) { m_Pos = pos; }
00569 virtual void Rewind() { m_Pos = 0; }
00570 virtual void SetSize(size_t Size) { m_Data->resize(Size); }
00571 virtual void Clear() { m_Data->clear(); }
00572
00573 size_t GetCapacity() { return m_Data->capacity(); }
00574 void SetCapacity(size_t Capacity) { m_Data->reserve(Capacity); }
00575 string *Data() { return m_Data; }
00576 };
00577
00579 class OverlayStream : public Stream
00580 {
00581 private:
00582 Stream *m_Stream;
00583
00584 public:
00585 OverlayStream(Stream *a_Stream) : m_Stream(a_Stream) { }
00586
00587 Stream * GetStream() { return m_Stream; }
00588
00589
00590 virtual bool End() { return m_Stream->End(); }
00591 virtual void Flush() { m_Stream->Flush(); }
00592 };
00593
00595 class CounterOverlayStream : public OverlayStream
00596 {
00597 private:
00598 uint4 m_WriteCounter;
00599 uint4 m_ReadCounter;
00600
00601 public:
00602 CounterOverlayStream(Stream *a_Stream);
00603
00604
00605 virtual void Write(const void *Data, size_t Size);
00606 virtual size_t Read(void *Data, size_t Size);
00607 virtual void MustRead(void *Data, size_t Size);
00608 virtual void Flush();
00609
00610 uint4 GetWriteCounter() { return m_WriteCounter; }
00611 uint4 GetReadCounter() { return m_ReadCounter; }
00612 void ResetWriteCounter() { m_WriteCounter = 0; }
00613 void ResetReadCounter() { m_ReadCounter = 0; }
00614 };
00615
00617
00619 class LimitOverlayStream : public OverlayStream
00620 {
00621 private:
00622
00623
00624 uint4 m_WriteLimit;
00625 uint4 m_ReadLimit;
00626
00627 public:
00628 LimitOverlayStream(Stream *a_Stream, uint4 WriteLimit, uint4 ReadLimit);
00629
00630
00631 virtual void Write(const void *Data, size_t Size);
00632 virtual size_t Read(void *Data, size_t Size);
00633 virtual void Flush();
00634
00636 void SetWriteLimit(uint4 WriteLimit);
00638 void SetReadLimit(uint4 ReadLimit);
00639 };
00640
00641 class BufferingStream : public OverlayStream
00642 {
00643 public:
00644 BufferingStream(Stream *stream, uint readBufSize, uint writeBufSize);
00645 ~BufferingStream();
00646
00647
00648 virtual void Write(const void *Data, size_t Size);
00649 virtual void Flush();
00650 virtual size_t Read(void *Data, size_t MaxLength);
00651 virtual bool End();
00652 virtual size_t Skip(size_t MaxLength);
00653
00655 void WriteChar(char ch) { if (m_WriteBufIndex == m_WriteBufSize) DoFlush(); m_WriteBuf[m_WriteBufIndex++] = ch; }
00656
00657 private:
00658 uint m_ReadBufSize, m_WriteBufSize;
00659 std::vector<char> m_ReadBuf, m_WriteBuf;
00660 uint m_ReadBufBeg, m_ReadBufEnd, m_WriteBufIndex;
00661
00662 void DoFlush();
00663
00664
00665
00666
00667 bool EnsureNewChars();
00668 };
00669
00671 class MultiWriterStream : public Stream
00672 {
00673 private:
00674 std::vector<Stream*> m_Streams;
00675
00676 public:
00677 MultiWriterStream() { }
00678 MultiWriterStream(Stream *a_Streams[], uint StreamCount);
00679
00681 void AddStream(Stream *a_Stream) { m_Streams.push_back(a_Stream); }
00683 void RemoveStream(Stream *a_Stream);
00685 void ClearStreams() { m_Streams.clear(); }
00686 size_t GetStreamCount() { return m_Streams.size(); }
00687
00688
00689 virtual void Write(const void *Data, size_t Size);
00690 virtual void Flush();
00691 };
00692
00694
00699 class Hash_Calc : public Stream
00700 {
00701 private:
00702 uint4 m_Hash;
00703
00704 public:
00705 Hash_Calc() : m_Hash(0) { }
00706
00707
00708 virtual void Write(const void *Data, size_t Size);
00709
00711 uint4 Finish();
00713 void Reset() { m_Hash = 0; }
00714
00715
00717 static uint4 Calc(const void *Buf, uint4 BufLen);
00718 static uint4 Calc(const string &s);
00719 #ifdef WIN32
00720 static uint4 Calc(const wstring &s);
00721 #endif
00722 };
00723
00725
00728 class CRC32_Calc : public Stream
00729 {
00730 private:
00731 uint m_CRC;
00732
00733 public:
00734 CRC32_Calc() { m_CRC = 0xFFFFFFFF; }
00735
00736
00737 virtual void Write(const void *Data, size_t Size);
00738
00740 uint GetResult() { return ~m_CRC; }
00742 void Reset() { m_CRC = 0xFFFFFFFF; }
00743
00744
00746 static uint Calc(const void *Data, size_t DataLength);
00747 };
00748
00750
00751 struct MD5_SUM
00752 {
00753 uint1 Data[16];
00754
00755 uint1 & operator [] (size_t i) { return Data[i]; }
00756 uint1 operator [] (size_t i) const { return Data[i]; }
00757
00758 bool operator == (const MD5_SUM &s) const
00759 {
00760 return
00761 Data[ 0] == s.Data[ 0] && Data[ 1] == s.Data[ 1] && Data[ 2] == s.Data[ 2] && Data[ 3] == s.Data[ 3] &&
00762 Data[ 4] == s.Data[ 4] && Data[ 5] == s.Data[ 5] && Data[ 6] == s.Data[ 6] && Data[ 7] == s.Data[ 7] &&
00763 Data[ 8] == s.Data[ 8] && Data[ 9] == s.Data[ 9] && Data[10] == s.Data[10] && Data[11] == s.Data[11] &&
00764 Data[12] == s.Data[12] && Data[13] == s.Data[13] && Data[14] == s.Data[14] && Data[15] == s.Data[15];
00765 }
00766 bool operator != (const MD5_SUM &s) const
00767 {
00768 return
00769 Data[ 0] != s.Data[ 0] || Data[ 1] != s.Data[ 1] || Data[ 2] != s.Data[ 2] || Data[ 3] != s.Data[ 3] ||
00770 Data[ 4] != s.Data[ 4] || Data[ 5] != s.Data[ 5] || Data[ 6] != s.Data[ 6] || Data[ 7] != s.Data[ 7] ||
00771 Data[ 8] != s.Data[ 8] || Data[ 9] != s.Data[ 9] || Data[10] != s.Data[10] || Data[11] != s.Data[11] ||
00772 Data[12] != s.Data[12] || Data[13] != s.Data[13] || Data[14] != s.Data[14] || Data[15] != s.Data[15];
00773 }
00774 bool operator < (const MD5_SUM &s) const;
00775 bool operator > (const MD5_SUM &s) const;
00776 bool operator <= (const MD5_SUM &s) const;
00777 bool operator >= (const MD5_SUM &s) const;
00778 };
00779
00780 void MD5ToStr(tstring *Out, const MD5_SUM MD5);
00781 bool StrToMD5(MD5_SUM *Out, const tstring &s);
00782
00784
00788 class MD5_Calc : public Stream
00789 {
00790 private:
00791 uint4 total[2];
00792 uint4 state[4];
00793 uint1 buffer[64];
00794
00795 void Process(uint1 data[64]);
00796
00797 public:
00798 MD5_Calc();
00799
00800
00801 virtual void Write(const void *Data, size_t Size);
00802
00804 void Finish(MD5_SUM *Out);
00806 void Reset();
00807
00808
00810 static void Calc(MD5_SUM *Out, const void *Buf, uint4 BufLen);
00811 };
00812
00814
00816 class XorCoder : public OverlayStream
00817 {
00818 private:
00819 std::vector<char> m_Buf;
00820 std::vector<char> m_Key;
00821 uint m_EncodeKeyIndex;
00822 uint m_DecodeKeyIndex;
00823
00824 public:
00825 XorCoder(Stream *a_Stream, char KeyByte);
00826 XorCoder(Stream *a_Stream, const void *KeyData, size_t KeyLength);
00827 XorCoder(Stream *a_Stream, const string &Key);
00828
00829
00830 virtual void Write(const void *Data, size_t Size);
00831 virtual size_t Read(void *Data, size_t Size);
00832
00834 void Reset() { m_EncodeKeyIndex = m_DecodeKeyIndex = 0; }
00835
00836
00838 static void Code(void *Out, const void *Data, size_t DataLength, const void *Key, size_t KeyLength);
00840 static void Code(string *Out, const string &Data, const string &Key);
00841 #ifdef WIN32
00843 static void Code(wstring *Out, const wstring &Data, const wstring &Key);
00844 #endif
00845 };
00846
00848 class BinEncoder : public OverlayStream
00849 {
00850 private:
00851 CharWriter m_CharWriter;
00852
00853 public:
00854 BinEncoder(Stream *a_Stream) : OverlayStream(a_Stream), m_CharWriter(a_Stream) { }
00855
00856
00857 virtual void Write(const void *Data, size_t Size);
00858 virtual void Flush() { m_CharWriter.Flush(); OverlayStream::Flush(); }
00859
00860
00862
00863 static void Encode(char *Out, const void *Data, size_t DataLength);
00865
00867 static void Encode(string *Out, const void *Data, size_t DataLength);
00868 };
00869
00871 class BinDecoder : public OverlayStream
00872 {
00873 private:
00874 CharReader m_CharReader;
00875 DECODE_TOLERANCE m_Tolerance;
00876
00877 public:
00878 BinDecoder(Stream *a_Stream, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00879
00880 DECODE_TOLERANCE GetTolerance() { return m_Tolerance; }
00881
00882
00883 virtual size_t Read(void *Out, size_t Size);
00884 virtual bool End() { return m_CharReader.End(); }
00885
00886
00888
00889 static bool DecodeLength(uint *OutLength, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00890 static bool DecodeLength(uint *OutLength, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00892
00895 static size_t Decode(void *OutData, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00896 static size_t Decode(void *OutData, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00897 };
00898
00900 class HexEncoder : public OverlayStream
00901 {
00902 private:
00903 CharWriter m_CharWriter;
00904 bool m_UpperCase;
00905
00906 public:
00907 HexEncoder(Stream *a_Stream, bool UpperCase = true) : OverlayStream(a_Stream), m_CharWriter(a_Stream), m_UpperCase(UpperCase) { }
00908
00909
00910 virtual void Write(const void *Data, size_t Size);
00911 virtual void Flush() { m_CharWriter.Flush(); OverlayStream::Flush(); }
00912
00913
00915
00916 static void Encode(char *Out, const void *Data, size_t DataLength, bool UpperCase = true);
00918
00920 static void Encode(string *Out, const void *Data, size_t DataLength, bool UpperCase = true);
00921 #ifdef WIN32
00922 static void Encode(wchar_t *Out, const void *Data, size_t DataLength, bool UpperCase = true);
00923 static void Encode(wstring *Out, const void *Data, size_t DataLength, bool UpperCase = true);
00924 #endif
00925 };
00926
00928
00929 class HexDecoder : public OverlayStream
00930 {
00931 private:
00932 CharReader m_CharReader;
00933 DECODE_TOLERANCE m_Tolerance;
00934
00935 public:
00936 HexDecoder(Stream *a_Stream, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00937
00938 DECODE_TOLERANCE GetTolerance() { return m_Tolerance; }
00939
00940
00941 virtual size_t Read(void *Out, size_t Size);
00942 virtual bool End() { return m_CharReader.End(); }
00943
00944
00946
00947 static bool DecodeLength(uint *OutLength, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00948 static bool DecodeLength(uint *OutLength, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00950
00953 static size_t Decode(void *OutData, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00955 static size_t Decode(void *OutData, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00956 #ifdef WIN32
00957 static size_t Decode(void *OutData, const wstring &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00958 static size_t Decode(void *OutData, const wchar_t *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00959 #endif
00960 };
00961
00963
00968 class Base64Encoder : public OverlayStream
00969 {
00970 private:
00971 CharWriter m_CharWriter;
00972 bool m_Finished;
00973 uint1 m_Buf[2];
00974 uint m_BufIndex;
00975
00976 void DoFinish();
00977
00978 public:
00979 Base64Encoder(Stream *a_Stream) : OverlayStream(a_Stream), m_CharWriter(a_Stream), m_Finished(false), m_BufIndex(0) { }
00980 ~Base64Encoder();
00981
00982 void Finish() { if (!m_Finished) { DoFinish(); m_Finished = true; } }
00983
00984
00985 virtual void Write(const void *Data, size_t Size);
00986 virtual void Flush() { m_CharWriter.Flush(); OverlayStream::Flush(); }
00987
00988
00990 static size_t EncodeLength(size_t DataLength);
00992
00994 static size_t Encode(char *Out, const void *Data, size_t DataLength);
00996
00998 static size_t Encode(string *Out, const void *Data, size_t DataLength);
00999 };
01000
01002
01003
01004
01005
01006
01007
01008 class Base64Decoder : public OverlayStream
01009 {
01010 private:
01011 CharReader m_CharReader;
01012 DECODE_TOLERANCE m_Tolerance;
01013
01014 uint1 m_Buf[3];
01015
01016 uint m_BufLength;
01017
01018 bool m_Finished;
01019
01020
01021
01022 bool ReadNextBuf();
01023
01024 bool GetNextByte(uint1 *OutByte) { if (m_BufLength == 0) { if (!ReadNextBuf()) return false; } *OutByte = m_Buf[--m_BufLength]; return true; }
01025
01026 public:
01027 Base64Decoder(Stream *a_Stream, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
01028
01029 DECODE_TOLERANCE GetTolerance() { return m_Tolerance; }
01030
01031
01032 virtual size_t Read(void *Out, size_t Size);
01033 virtual bool End();
01034
01035
01037
01038 static bool DecodeLength(uint *OutLength, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
01039 static bool DecodeLength(uint *OutLength, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
01041
01043 static size_t Decode(void *OutData, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
01044 static size_t Decode(void *OutData, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
01045 };
01046
01048
01049
01050
01051
01052
01053 class RingBuffer : public Stream
01054 {
01055 public:
01056 RingBuffer(uint Capacity);
01057 virtual ~RingBuffer();
01058
01060 uint GetSize() { return m_Size; }
01062 uint GetCapacity() { return m_Capacity; }
01064 bool IsEmpty() { return GetSize() == 0; }
01066 bool IsFull() { return GetSize() == GetCapacity(); }
01067
01068
01069 virtual void Write(const void *Data, size_t Size);
01070 virtual size_t Read(void *Out, size_t MaxLength);
01071 virtual void MustRead(void *Out, size_t Length);
01072 virtual bool End();
01073 virtual size_t Skip(size_t MaxLength);
01074
01075 private:
01076 uint m_Capacity;
01077 uint m_Size;
01078 std::vector<char> m_Buf;
01079 uint m_BegIndex;
01080 uint m_EndIndex;
01081 };
01082
01083
01085
01086 inline size_t Copy(Stream *Dst, Stream *Src, size_t MaxLength) { return Dst->CopyFrom(Src, MaxLength); }
01088
01089 inline void MustCopy(Stream *Dst, Stream *Src, size_t Length) { return Dst->MustCopyFrom(Src, Length); }
01091 inline size_t CopyToEnd(Stream *Dst, Stream *Src) { return Dst->CopyFromToEnd(Src); }
01092
01094
01095
01096 }
01097
01100
01101 template <>
01102 struct SthToStr_obj<common::MD5_SUM>
01103 {
01104 void operator () (tstring *Str, const common::MD5_SUM &Sth)
01105 {
01106 common::MD5ToStr(Str, Sth);
01107 }
01108 static inline bool IsSupported() { return true; }
01109 };
01110
01111 template <>
01112 struct StrToSth_obj<common::MD5_SUM>
01113 {
01114 bool operator () (common::MD5_SUM *Sth, const tstring &Str)
01115 {
01116 return common::StrToMD5(Sth, Str);
01117 }
01118 static inline bool IsSupported() { return true; }
01119 };
01120
01122
01123
01124 #endif