Leaked source code of windows server 2003
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.

129 lines
3.5 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. //
  4. // File: B64Octet.h
  5. //
  6. // Description: Octet-Stream based processing of UNICODE characters.
  7. //
  8. // Author: Mike Swafford (MikeSwa)
  9. //
  10. // History:
  11. // 10/21/98 - MikeSwa Created
  12. //
  13. // Copyright (C) 1998 Microsoft Corporation
  14. //
  15. //-----------------------------------------------------------------------------
  16. #ifndef __B64OCTET_H__
  17. #define __B64OCTET_H__
  18. #include <windows.h>
  19. #include <dbgtrace.h>
  20. //Buffer size to store conversions...
  21. //should be no less than 6... multples of 8
  22. //Reasons... we need to be able to fit an entire wide character in buffer.
  23. //Each wide character coverts to 2 1/3 base64 'digits'... plus the possibility
  24. //of 2 more filler characters. We need 5 characters plus a buffer char to
  25. //store a single wide char. A 'even' number of WCHAR is 3 (2 1/3 * 3 = 7),
  26. //which requires 7 base64 digits to encode (plus buffer).
  27. #define BASE64_OCTET_BUFFER_SIZE 9
  28. #define BASE64_OCTET_SIG '46Bc'
  29. #define BASE64_INVALID_FILL_CHAR '!'
  30. class CBase64CircularBuffer
  31. {
  32. private:
  33. DWORD m_iHead;
  34. DWORD m_iTail;
  35. CHAR m_rgchBuffer[BASE64_OCTET_BUFFER_SIZE];
  36. public:
  37. CBase64CircularBuffer();
  38. DWORD cSize();
  39. DWORD cSpaceLeft();
  40. BOOL fIsFull();
  41. BOOL fIsEmpty();
  42. BOOL fPushChar(CHAR ch);
  43. BOOL fPopChar(CHAR *pch);
  44. };
  45. class CBase64OctetStream
  46. {
  47. protected:
  48. DWORD m_dwSignature;
  49. DWORD m_dwCurrentState;
  50. BYTE m_bCurrentLeftOver;
  51. CBase64CircularBuffer m_CharBuffer;
  52. void NextState();
  53. void ResetState();
  54. public:
  55. CBase64OctetStream();
  56. //returns FALSE when buffer is full
  57. BOOL fProcessWideChar(WCHAR wch);
  58. BOOL fProcessSingleByte(BYTE b);
  59. //The following will terminate the stream a zero-fill any remaining chars
  60. BOOL fTerminateStream(BOOL fUTF7Encoded);
  61. //returns FALSE when buffer is empty
  62. BOOL fNextValidChar(CHAR *pch) ;
  63. };
  64. //inline functions that implement circular buffer
  65. inline CBase64CircularBuffer::CBase64CircularBuffer()
  66. {
  67. m_iHead = 0;
  68. m_iTail = 0;
  69. memset(&m_rgchBuffer, BASE64_INVALID_FILL_CHAR, BASE64_OCTET_BUFFER_SIZE);
  70. }
  71. inline DWORD CBase64CircularBuffer::cSize()
  72. {
  73. if (m_iHead <= m_iTail)
  74. return m_iTail - m_iHead;
  75. else
  76. return m_iTail + BASE64_OCTET_BUFFER_SIZE - m_iHead;
  77. }
  78. inline DWORD CBase64CircularBuffer::cSpaceLeft()
  79. {
  80. return BASE64_OCTET_BUFFER_SIZE - cSize() - 1;
  81. }
  82. inline BOOL CBase64CircularBuffer::fIsFull()
  83. {
  84. return ((BASE64_OCTET_BUFFER_SIZE-1) == cSize());
  85. }
  86. inline BOOL CBase64CircularBuffer::fIsEmpty()
  87. {
  88. return (m_iHead == m_iTail);
  89. }
  90. inline BOOL CBase64CircularBuffer::fPushChar(CHAR ch)
  91. {
  92. if (fIsFull())
  93. return FALSE;
  94. m_rgchBuffer[m_iTail] = ch;
  95. m_iTail++;
  96. m_iTail %= BASE64_OCTET_BUFFER_SIZE;
  97. return TRUE;
  98. }
  99. inline BOOL CBase64CircularBuffer::fPopChar(CHAR *pch)
  100. {
  101. _ASSERT(pch);
  102. if (fIsEmpty())
  103. return FALSE;
  104. *pch = m_rgchBuffer[m_iHead];
  105. _ASSERT(BASE64_INVALID_FILL_CHAR != *pch);
  106. m_rgchBuffer[m_iHead] = BASE64_INVALID_FILL_CHAR;
  107. m_iHead++;
  108. m_iHead %= BASE64_OCTET_BUFFER_SIZE;
  109. return TRUE;
  110. }
  111. #endif //__B64OCTET_H__