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 void 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
00219 class CharWriter
00220 {
00221 private:
00222 Stream *m_Stream;
00223 std::vector<char> m_Buf;
00224 uint m_BufIndex;
00225
00226
00227
00228 void DoFlush() { m_Stream->Write(&m_Buf[0], m_BufIndex); m_BufIndex = 0; }
00229
00230 public:
00231 CharWriter(Stream *a_Stream) : m_Stream(a_Stream), m_BufIndex(0) { m_Buf.resize(BUFFER_SIZE); }
00232 ~CharWriter();
00233
00235 void WriteChar(char Ch) { if (m_BufIndex == BUFFER_SIZE) DoFlush(); m_Buf[m_BufIndex++] = Ch; }
00237 void WriteString(const string &s);
00239 void WriteString(const char *s) { WriteString(s, strlen(s)); }
00241 void WriteString(const char *s, size_t NumChars) { WriteData(s, NumChars); }
00243 void WriteData(const void *Data, size_t Length);
00245 template <typename T> void WriteEx(const T &x) { string s; SthToStr<T>(&s, x); WriteString(s); }
00247 void Flush() { if (m_BufIndex > 0) DoFlush(); }
00248 };
00249
00250 #ifdef WIN32
00252
00253 class WCharWriter
00254 {
00255 private:
00256 Stream *m_Stream;
00257 std::vector<wchar_t> m_Buf;
00258 uint m_BufIndex;
00259
00260
00261
00262 void DoFlush() { m_Stream->Write(&m_Buf[0], m_BufIndex * sizeof(wchar_t)); m_BufIndex = 0; }
00263
00264 public:
00265 WCharWriter(Stream *a_Stream) : m_Stream(a_Stream), m_BufIndex(0) { m_Buf.resize(BUFFER_SIZE); }
00266 ~WCharWriter();
00267
00269 void WriteChar(wchar_t Ch) { if (m_BufIndex == BUFFER_SIZE) DoFlush(); m_Buf[m_BufIndex++] = Ch; }
00271 void WriteString(const wstring &s);
00273 void WriteString(const wchar_t *s) { WriteString(s, wcslen(s)); }
00275 void WriteString(const wchar_t *s, size_t NumChars);
00277 void Flush() { if (m_BufIndex > 0) DoFlush(); }
00278 };
00279 #endif
00280
00282
00291 class CharReader
00292 {
00293 private:
00294 Stream *m_Stream;
00295 std::vector<char> m_Buf;
00296
00297 uint m_BufBeg;
00298
00299 uint m_BufEnd;
00300
00301
00302
00303
00304
00305 bool EnsureNewChars();
00306
00307 public:
00308 CharReader(Stream *a_Stream) : m_Stream(a_Stream), m_BufBeg(0), m_BufEnd(0) { m_Buf.resize(BUFFER_SIZE); }
00309
00311 bool End() {
00312 return (m_BufBeg == m_BufEnd) && m_Stream->End();
00313 }
00315
00316 bool ReadChar(char *Out) { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } *Out = m_Buf[m_BufBeg++]; return true; }
00318
00319 char MustReadChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } return m_Buf[m_BufBeg++]; }
00321
00322 bool PeekChar(char *Out) { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } *Out = m_Buf[m_BufBeg]; return true; }
00324
00325 char MustPeekChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } return m_Buf[m_BufBeg]; }
00327
00329 size_t ReadString(string *Out, size_t MaxLength);
00331
00333 void MustReadString(string *Out, size_t Length);
00335
00337 size_t ReadString(char *Out, size_t MaxLength);
00339
00341 void MustReadString(char *Out, size_t Length);
00343
00345 size_t ReadData(void *Out, size_t MaxLength);
00347
00349 void MustReadData(void *Out, size_t Length);
00351
00352 bool SkipChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } m_BufBeg++; return true; }
00354
00355 void MustSkipChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } m_BufBeg++; }
00357
00358 size_t Skip(size_t MaxLength);
00360
00361 void MustSkip(size_t Length);
00363
00366 bool ReadLine(string *Out);
00368
00371 void MustReadLine(string *Out);
00372 };
00373
00374 #ifdef WIN32
00376
00377 class WCharReader
00378 {
00379 private:
00380 Stream *m_Stream;
00381 std::vector<wchar_t> m_Buf;
00382
00383 uint m_BufBeg;
00384
00385 uint m_BufEnd;
00386
00387
00388
00389
00390
00391 bool EnsureNewChars();
00392
00393 public:
00394 WCharReader(Stream *a_Stream) : m_Stream(a_Stream), m_BufBeg(0), m_BufEnd(0) { m_Buf.resize(BUFFER_SIZE); }
00395
00397 bool End() {
00398 return (m_BufBeg == m_BufEnd) && m_Stream->End();
00399 }
00401
00402 bool ReadChar(wchar_t *Out) { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } *Out = m_Buf[m_BufBeg++]; return true; }
00404
00405 wchar_t MustReadChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } return m_Buf[m_BufBeg++]; }
00407
00408 bool PeekChar(wchar_t *Out) { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } *Out = m_Buf[m_BufBeg]; return true; }
00410
00411 wchar_t MustPeekChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } return m_Buf[m_BufBeg]; }
00413
00415 size_t ReadString(wstring *Out, size_t MaxLength);
00417
00419 void MustReadString(wstring *Out, size_t Length);
00421
00423 size_t ReadString(wchar_t *Out, size_t MaxLength);
00425
00427 void MustReadString(wchar_t *Out, size_t Length);
00429
00430 bool SkipChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) return false; } m_BufBeg++; return true; }
00432
00433 void MustSkipChar() { if (m_BufBeg == m_BufEnd) { if (!EnsureNewChars()) _ThrowBufEndError(__TFILE__, __LINE__); } m_BufBeg++; }
00435
00436 size_t Skip(size_t MaxLength);
00438
00439 void MustSkip(size_t Length);
00441
00444 bool ReadLine(wstring *Out);
00446
00449 void MustReadLine(wstring *Out);
00450 };
00451 #endif
00452
00453 #ifdef _UNICODE
00454 #define TCharWriter WCharWriter
00455 #define TCharReader WCharReader
00456 #else
00457 #define TCharWriter CharWriter
00458 #define TCharReader CharReader
00459 #endif
00460
00462
00463 class MemoryStream : public SeekableStream
00464 {
00465 private:
00466 char *m_Data;
00467 size_t m_Size;
00468 int m_Pos;
00469 bool m_InternalAlloc;
00470
00471 public:
00475 MemoryStream(size_t Size, void *Data = 0);
00476 virtual ~MemoryStream();
00477
00478 virtual void Write(const void *Data, size_t Size);
00479 virtual size_t Read(void *Data, size_t Size);
00480 virtual void MustRead(void *Data, size_t Size);
00481
00482 virtual size_t GetSize();
00483 virtual int GetPos();
00484 virtual void SetPos(int pos);
00485 virtual void Rewind();
00486
00487 char *Data() { return m_Data; }
00488 };
00489
00491
00492 class VectorStream : public SeekableStream
00493 {
00494 private:
00495 char *m_Data;
00496 size_t m_Size;
00497 size_t m_Capacity;
00498 int m_Pos;
00499
00500
00501
00502 void Reserve(size_t NewCapacity);
00503
00504 public:
00505 VectorStream();
00506 virtual ~VectorStream();
00507
00508 virtual void Write(const void *Data, size_t Size);
00509 virtual size_t Read(void *Data, size_t Size);
00510 virtual void MustRead(void *Data, size_t Size);
00511 virtual size_t GetSize() { return m_Size; }
00512 virtual int GetPos() { return m_Pos; }
00513 virtual void SetPos(int pos) { m_Pos = pos; }
00514 virtual void Rewind() { m_Pos = 0; }
00515 virtual void SetSize(size_t Size);
00516
00517 size_t GetCapacity() { return m_Capacity; }
00518 void SetCapacity(size_t Capacity);
00521 char *Data() { return m_Data; }
00522 };
00523
00525
00526 class StringStream : public SeekableStream
00527 {
00528 private:
00529 std::string *m_Data;
00530 int m_Pos;
00531 bool m_InternalAlloc;
00532
00533 public:
00536 StringStream(string *Data = 0);
00537 virtual ~StringStream();
00538
00539 virtual void Write(const void *Data, size_t Size);
00540 virtual size_t Read(void *Data, size_t Size);
00541 virtual void MustRead(void *Data, size_t Size);
00542 virtual size_t GetSize() { return m_Data->length(); }
00543 virtual int GetPos() { return m_Pos; }
00544 virtual void SetPos(int pos) { m_Pos = pos; }
00545 virtual void Rewind() { m_Pos = 0; }
00546 virtual void SetSize(size_t Size) { m_Data->resize(Size); }
00547 virtual void Clear() { m_Data->clear(); }
00548
00549 size_t GetCapacity() { return m_Data->capacity(); }
00550 void SetCapacity(size_t Capacity) { m_Data->reserve(Capacity); }
00551 string *Data() { return m_Data; }
00552 };
00553
00555 class OverlayStream : public Stream
00556 {
00557 private:
00558 Stream *m_Stream;
00559
00560 public:
00561 OverlayStream(Stream *a_Stream) : m_Stream(a_Stream) { }
00562
00563 Stream * GetStream() { return m_Stream; }
00564
00565
00566 virtual bool End() { return m_Stream->End(); }
00567 virtual void Flush() { m_Stream->Flush(); }
00568 };
00569
00571 class CounterOverlayStream : public OverlayStream
00572 {
00573 private:
00574 uint4 m_WriteCounter;
00575 uint4 m_ReadCounter;
00576
00577 public:
00578 CounterOverlayStream(Stream *a_Stream);
00579
00580
00581 virtual void Write(const void *Data, size_t Size);
00582 virtual size_t Read(void *Data, size_t Size);
00583 virtual void MustRead(void *Data, size_t Size);
00584 virtual void Flush();
00585
00586 uint4 GetWriteCounter() { return m_WriteCounter; }
00587 uint4 GetReadCounter() { return m_ReadCounter; }
00588 void ResetWriteCounter() { m_WriteCounter = 0; }
00589 void ResetReadCounter() { m_ReadCounter = 0; }
00590 };
00591
00593
00595 class LimitOverlayStream : public OverlayStream
00596 {
00597 private:
00598
00599
00600 uint4 m_WriteLimit;
00601 uint4 m_ReadLimit;
00602
00603 public:
00604 LimitOverlayStream(Stream *a_Stream, uint4 WriteLimit, uint4 ReadLimit);
00605
00606
00607 virtual void Write(const void *Data, size_t Size);
00608 virtual size_t Read(void *Data, size_t Size);
00609 virtual void Flush();
00610
00612 void SetWriteLimit(uint4 WriteLimit);
00614 void SetReadLimit(uint4 ReadLimit);
00615 };
00616
00618 class MultiWriterStream : public Stream
00619 {
00620 private:
00621 std::vector<Stream*> m_Streams;
00622
00623 public:
00624 MultiWriterStream() { }
00625 MultiWriterStream(Stream *a_Streams[], uint StreamCount);
00626
00628 void AddStream(Stream *a_Stream) { m_Streams.push_back(a_Stream); }
00630 void RemoveStream(Stream *a_Stream);
00632 void ClearStreams() { m_Streams.clear(); }
00633 size_t GetStreamCount() { return m_Streams.size(); }
00634
00635
00636 virtual void Write(const void *Data, size_t Size);
00637 virtual void Flush();
00638 };
00639
00641
00646 class Hash_Calc : public Stream
00647 {
00648 private:
00649 uint4 m_Hash;
00650
00651 public:
00652 Hash_Calc() : m_Hash(0) { }
00653
00654
00655 virtual void Write(const void *Data, size_t Size);
00656
00658 uint4 Finish();
00660 void Reset() { m_Hash = 0; }
00661
00662
00664 static uint4 Calc(const void *Buf, uint4 BufLen);
00665 static uint4 Calc(const string &s);
00666 #ifdef WIN32
00667 static uint4 Calc(const wstring &s);
00668 #endif
00669 };
00670
00672
00675 class CRC32_Calc : public Stream
00676 {
00677 private:
00678 uint m_CRC;
00679
00680 public:
00681 CRC32_Calc() { m_CRC = 0xFFFFFFFF; }
00682
00683
00684 virtual void Write(const void *Data, size_t Size);
00685
00687 uint GetResult() { return ~m_CRC; }
00689 void Reset() { m_CRC = 0xFFFFFFFF; }
00690
00691
00693 static uint Calc(const void *Data, size_t DataLength);
00694 };
00695
00697
00698 struct MD5_SUM
00699 {
00700 uint1 Data[16];
00701
00702 uint1 & operator [] (size_t i) { return Data[i]; }
00703 uint1 operator [] (size_t i) const { return Data[i]; }
00704
00705 bool operator == (const MD5_SUM &s) const
00706 {
00707 return
00708 Data[ 0] == s.Data[ 0] && Data[ 1] == s.Data[ 1] && Data[ 2] == s.Data[ 2] && Data[ 3] == s.Data[ 3] &&
00709 Data[ 4] == s.Data[ 4] && Data[ 5] == s.Data[ 5] && Data[ 6] == s.Data[ 6] && Data[ 7] == s.Data[ 7] &&
00710 Data[ 8] == s.Data[ 8] && Data[ 9] == s.Data[ 9] && Data[10] == s.Data[10] && Data[11] == s.Data[11] &&
00711 Data[12] == s.Data[12] && Data[13] == s.Data[13] && Data[14] == s.Data[14] && Data[15] == s.Data[15];
00712 }
00713 bool operator != (const MD5_SUM &s) const
00714 {
00715 return
00716 Data[ 0] != s.Data[ 0] || Data[ 1] != s.Data[ 1] || Data[ 2] != s.Data[ 2] || Data[ 3] != s.Data[ 3] ||
00717 Data[ 4] != s.Data[ 4] || Data[ 5] != s.Data[ 5] || Data[ 6] != s.Data[ 6] || Data[ 7] != s.Data[ 7] ||
00718 Data[ 8] != s.Data[ 8] || Data[ 9] != s.Data[ 9] || Data[10] != s.Data[10] || Data[11] != s.Data[11] ||
00719 Data[12] != s.Data[12] || Data[13] != s.Data[13] || Data[14] != s.Data[14] || Data[15] != s.Data[15];
00720 }
00721 bool operator < (const MD5_SUM &s) const;
00722 bool operator > (const MD5_SUM &s) const;
00723 bool operator <= (const MD5_SUM &s) const;
00724 bool operator >= (const MD5_SUM &s) const;
00725 };
00726
00727 void MD5ToStr(tstring *Out, const MD5_SUM MD5);
00728 bool StrToMD5(MD5_SUM *Out, const tstring &s);
00729
00731
00735 class MD5_Calc : public Stream
00736 {
00737 private:
00738 uint4 total[2];
00739 uint4 state[4];
00740 uint1 buffer[64];
00741
00742 void Process(uint1 data[64]);
00743
00744 public:
00745 MD5_Calc();
00746
00747
00748 virtual void Write(const void *Data, size_t Size);
00749
00751 void Finish(MD5_SUM *Out);
00753 void Reset();
00754
00755
00757 static void Calc(MD5_SUM *Out, const void *Buf, uint4 BufLen);
00758 };
00759
00761
00763 class XorCoder : public OverlayStream
00764 {
00765 private:
00766 std::vector<char> m_Buf;
00767 std::vector<char> m_Key;
00768 uint m_EncodeKeyIndex;
00769 uint m_DecodeKeyIndex;
00770
00771 public:
00772 XorCoder(Stream *a_Stream, char KeyByte);
00773 XorCoder(Stream *a_Stream, const void *KeyData, size_t KeyLength);
00774 XorCoder(Stream *a_Stream, const string &Key);
00775
00776
00777 virtual void Write(const void *Data, size_t Size);
00778 virtual size_t Read(void *Data, size_t Size);
00779
00781 void Reset() { m_EncodeKeyIndex = m_DecodeKeyIndex = 0; }
00782
00783
00785 static void Code(void *Out, const void *Data, size_t DataLength, const void *Key, size_t KeyLength);
00787 static void Code(string *Out, const string &Data, const string &Key);
00788 #ifdef WIN32
00790 static void Code(wstring *Out, const wstring &Data, const wstring &Key);
00791 #endif
00792 };
00793
00795 class BinEncoder : public OverlayStream
00796 {
00797 private:
00798 CharWriter m_CharWriter;
00799
00800 public:
00801 BinEncoder(Stream *a_Stream) : OverlayStream(a_Stream), m_CharWriter(a_Stream) { }
00802
00803
00804 virtual void Write(const void *Data, size_t Size);
00805 virtual void Flush() { m_CharWriter.Flush(); OverlayStream::Flush(); }
00806
00807
00809
00810 static void Encode(char *Out, const void *Data, size_t DataLength);
00812
00814 static void Encode(string *Out, const void *Data, size_t DataLength);
00815 };
00816
00818 class BinDecoder : public OverlayStream
00819 {
00820 private:
00821 CharReader m_CharReader;
00822 DECODE_TOLERANCE m_Tolerance;
00823
00824 public:
00825 BinDecoder(Stream *a_Stream, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00826
00827 DECODE_TOLERANCE GetTolerance() { return m_Tolerance; }
00828
00829
00830 virtual size_t Read(void *Out, size_t Size);
00831 virtual bool End() { return m_CharReader.End(); }
00832
00833
00835
00836 static bool DecodeLength(uint *OutLength, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00837 static bool DecodeLength(uint *OutLength, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00839
00842 static size_t Decode(void *OutData, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00843 static size_t Decode(void *OutData, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00844 };
00845
00847 class HexEncoder : public OverlayStream
00848 {
00849 private:
00850 CharWriter m_CharWriter;
00851 bool m_UpperCase;
00852
00853 public:
00854 HexEncoder(Stream *a_Stream, bool UpperCase = true) : OverlayStream(a_Stream), m_CharWriter(a_Stream), m_UpperCase(UpperCase) { }
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, bool UpperCase = true);
00865
00867 static void Encode(string *Out, const void *Data, size_t DataLength, bool UpperCase = true);
00868 #ifdef WIN32
00869 static void Encode(wchar_t *Out, const void *Data, size_t DataLength, bool UpperCase = true);
00870 static void Encode(wstring *Out, const void *Data, size_t DataLength, bool UpperCase = true);
00871 #endif
00872 };
00873
00875
00876 class HexDecoder : public OverlayStream
00877 {
00878 private:
00879 CharReader m_CharReader;
00880 DECODE_TOLERANCE m_Tolerance;
00881
00882 public:
00883 HexDecoder(Stream *a_Stream, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00884
00885 DECODE_TOLERANCE GetTolerance() { return m_Tolerance; }
00886
00887
00888 virtual size_t Read(void *Out, size_t Size);
00889 virtual bool End() { return m_CharReader.End(); }
00890
00891
00893
00894 static bool DecodeLength(uint *OutLength, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00895 static bool DecodeLength(uint *OutLength, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00897
00900 static size_t Decode(void *OutData, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00902 static size_t Decode(void *OutData, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00903 #ifdef WIN32
00904 static size_t Decode(void *OutData, const wstring &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00905 static size_t Decode(void *OutData, const wchar_t *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00906 #endif
00907 };
00908
00910
00915 class Base64Encoder : public OverlayStream
00916 {
00917 private:
00918 CharWriter m_CharWriter;
00919 bool m_Finished;
00920 uint1 m_Buf[2];
00921 uint m_BufIndex;
00922
00923 void DoFinish();
00924
00925 public:
00926 Base64Encoder(Stream *a_Stream) : OverlayStream(a_Stream), m_CharWriter(a_Stream), m_Finished(false), m_BufIndex(0) { }
00927 ~Base64Encoder();
00928
00929 void Finish() { if (!m_Finished) { DoFinish(); m_Finished = true; } }
00930
00931
00932 virtual void Write(const void *Data, size_t Size);
00933 virtual void Flush() { m_CharWriter.Flush(); OverlayStream::Flush(); }
00934
00935
00937 static size_t EncodeLength(size_t DataLength);
00939
00941 static size_t Encode(char *Out, const void *Data, size_t DataLength);
00943
00945 static size_t Encode(string *Out, const void *Data, size_t DataLength);
00946 };
00947
00949
00950
00951
00952
00953
00954
00955 class Base64Decoder : public OverlayStream
00956 {
00957 private:
00958 CharReader m_CharReader;
00959 DECODE_TOLERANCE m_Tolerance;
00960
00961 uint1 m_Buf[3];
00962
00963 uint m_BufLength;
00964
00965 bool m_Finished;
00966
00967
00968
00969 bool ReadNextBuf();
00970
00971 bool GetNextByte(uint1 *OutByte) { if (m_BufLength == 0) { if (!ReadNextBuf()) return false; } *OutByte = m_Buf[--m_BufLength]; return true; }
00972
00973 public:
00974 Base64Decoder(Stream *a_Stream, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00975
00976 DECODE_TOLERANCE GetTolerance() { return m_Tolerance; }
00977
00978
00979 virtual size_t Read(void *Out, size_t Size);
00980 virtual bool End();
00981
00982
00984
00985 static bool DecodeLength(uint *OutLength, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00986 static bool DecodeLength(uint *OutLength, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00988
00990 static size_t Decode(void *OutData, const string &s, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00991 static size_t Decode(void *OutData, const char *s, uint s_Length, DECODE_TOLERANCE Tolerance = DECODE_TOLERANCE_NONE);
00992 };
00993
00995
00996
00997
00998
00999
01000 class RingBuffer : public Stream
01001 {
01002 public:
01003 RingBuffer(uint Capacity);
01004 virtual ~RingBuffer();
01005
01007 uint GetSize() { return m_Size; }
01009 uint GetCapacity() { return m_Capacity; }
01011 bool IsEmpty() { return GetSize() == 0; }
01013 bool IsFull() { return GetSize() == GetCapacity(); }
01014
01015
01016 virtual void Write(const void *Data, size_t Size);
01017 virtual size_t Read(void *Out, size_t MaxLength);
01018 virtual void MustRead(void *Out, size_t Length);
01019 virtual bool End();
01020 virtual size_t Skip(size_t MaxLength);
01021
01022 private:
01023 uint m_Capacity;
01024 uint m_Size;
01025 std::vector<char> m_Buf;
01026 uint m_BegIndex;
01027 uint m_EndIndex;
01028 };
01029
01030
01032
01033 inline size_t Copy(Stream *Dst, Stream *Src, size_t MaxLength) { return Dst->CopyFrom(Src, MaxLength); }
01035
01036 inline void MustCopy(Stream *Dst, Stream *Src, size_t Length) { return Dst->MustCopyFrom(Src, Length); }
01038 inline void CopyToEnd(Stream *Dst, Stream *Src) { Dst->CopyFromToEnd(Src); }
01039
01041
01042
01043 }
01044
01047
01048 template <>
01049 struct SthToStr_obj<common::MD5_SUM>
01050 {
01051 void operator () (tstring *Str, const common::MD5_SUM &Sth)
01052 {
01053 common::MD5ToStr(Str, Sth);
01054 }
01055 static inline bool IsSupported() { return true; }
01056 };
01057
01058 template <>
01059 struct StrToSth_obj<common::MD5_SUM>
01060 {
01061 bool operator () (common::MD5_SUM *Sth, const tstring &Str)
01062 {
01063 return common::StrToMD5(Sth, Str);
01064 }
01065 static inline bool IsSupported() { return true; }
01066 };
01067
01069
01070
01071 #endif