Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cBase64.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cBase64_H
7 #define _INC_cBase64_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
14 #include "GrayCore/include/cMem.h"
16 
17 namespace GrayLib
18 {
19  class cLogProcessor;
20 
21  struct GRAYLIB_LINK cBase64Coder // static
22  {
25  static inline StrLen_t GetTextSizeFromBinarySize(size_t nSizeBin) noexcept // GetEncodeDestSize
26  {
30  return (StrLen_t)(((nSizeBin + 2) / 3) * 4); // round up to allow padding it out with ='s
31  }
32  static inline size_t GetBinarySizeFromTextSize(StrLen_t nLenText) noexcept // GetDecodeDestSize
33  {
36  return ((nLenText * 3) / 4);
37  }
38 
40  };
41 
42  struct GRAYLIB_LINK cBase64Char : public cBase64Coder // static
43  {
47 
48  static const char k_chFill = '=';
49  static const char k_Encode[65]; // = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
50 
51  static inline char ENC(BYTE b)
52  {
53  ASSERT(b < 64);
54  return k_Encode[b];
55  }
56  static inline BYTE DEC(char ch) noexcept
57  {
59  if (ch >= 'A' && ch <= 'Z')
60  return (ch - 'A');
61  if (ch >= 'a' && ch <= 'z')
62  return (ch - 'a') + 26;
63  if (ch >= '0' && ch <= '9')
64  return (ch - '0') + 52;
65  if (ch == '+')
66  return 62;
67  if (ch == '-') // alternate +
68  return 62;
69  if (ch == '/')
70  return 63;
71  if (ch == '_') // alternate
72  return 63;
73  return 64; // '=' padding char is outside range.
74  }
75  };
76 
77  struct GRAYLIB_LINK cUUCodeChar : public cBase64Coder // static
78  {
82 
83  static const char k_chFill = (char)(' ' + 64); // junk char for padding.
84 
85  static inline char ENC(BYTE b)
86  {
87  ASSERT(b < 64);
88  return(b + ' ');
89  }
90  static inline BYTE DEC(char ch) noexcept
91  {
94  return(ch - ' ');
95  }
96  };
97 
98  template < class _TYPE_CH = cUUCodeChar >
99  class cBase64CoderT : public _TYPE_CH
100  {
105 
106  typedef _TYPE_CH SUPER_t; // the character encoder
107  public:
108  static inline bool IsValidChar(char ch) noexcept
109  {
112  return SUPER_t::DEC(ch) < 64 ;
113  }
114 
115  static inline void EncodeBlock(char* pDstText, const BYTE* pSrcBin)
116  {
119 
120  BYTE s0 = pSrcBin[0];
121  BYTE s1 = pSrcBin[1];
122  BYTE s2 = pSrcBin[2];
123 
124  BYTE n = (s0 >> 2);
125  pDstText[0] = SUPER_t::ENC(n);
126  n = ((s0 << 4) & 0x30) | ((s1 >> 4) & 0x0f);
127  pDstText[1] = SUPER_t::ENC(n);
128  n = ((s1 << 2) & 0x3c) | ((s2 >> 6) & 0x03);
129  pDstText[2] = SUPER_t::ENC(n);
130  n = s2 & 0x3f;
131  pDstText[3] = SUPER_t::ENC(n);
132  ASSERT(IsValidChar(pDstText[3]));
133  }
134 
135  static inline void DecodeBlock(BYTE* pDstBin, const char* pSrcText) noexcept
136  {
140 
142  // ASSERT( IsValidChar(pSrc[0]));
143 
144  BYTE a = SUPER_t::DEC(pSrcText[0]);
145  BYTE b = SUPER_t::DEC(pSrcText[1]);
146  pDstBin[0] = (a << 2) | (b >> 4);
147  BYTE c = SUPER_t::DEC(pSrcText[2]);
148  pDstBin[1] = (b << 4) | (c >> 2);
149  BYTE d = SUPER_t::DEC(pSrcText[3]);
150  pDstBin[2] = (c << 6) | d;
151  }
152 
153  static inline void EncodeBlockEnd(char* pDst, const BYTE* pSrc, size_t nSizeLeft)
154  {
155  // Encode misaligned stuff at the end.
156  // must pad this special with 0's. translated extra as '=' sm_chFill
157  BYTE tmp[3];
158  cMem::Copy(tmp, pSrc, nSizeLeft);
159  cMem::Zero(tmp + nSizeLeft, 3 - nSizeLeft);
160  EncodeBlock(pDst, tmp);
161  cValArray::FillSize(pDst + (nSizeLeft + 1), 4 - (nSizeLeft + 1), SUPER_t::k_chFill); // pad out with =
162  }
163 
165  static StrLen_t GRAYCALL Encode(char* pDstText, StrLen_t nLenDstMax, const BYTE* pSrcBin, size_t nSizeSrc);
166 
168  static size_t GRAYCALL Decode(BYTE* pDstBin, size_t nSizeDst, const char* pSrcText, StrLen_t nLenSrc = -1);
169  };
170 
173 }
174 #endif // _INC_cBase64_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cBase64.h:100
static bool IsValidChar(char ch) noexcept
Definition: cBase64.h:108
static void EncodeBlockEnd(char *pDst, const BYTE *pSrc, size_t nSizeLeft)
Definition: cBase64.h:153
static size_t GRAYCALL Decode(BYTE *pDstBin, size_t nSizeDst, const char *pSrcText, StrLen_t nLenSrc=-1)
Decode an encoded text string to a binary blob. in place is OK.
Definition: cBase64.cpp:54
static StrLen_t GRAYCALL Encode(char *pDstText, StrLen_t nLenDstMax, const BYTE *pSrcBin, size_t nSizeSrc)
Encode a binary blob to text string.
Definition: cBase64.cpp:15
static void DecodeBlock(BYTE *pDstBin, const char *pSrcText) noexcept
Definition: cBase64.h:135
static void EncodeBlock(char *pDstText, const BYTE *pSrcBin)
Definition: cBase64.h:115
Definition: cMesh.h:22
cBase64CoderT< cBase64Char > cBase64
Definition: cBase64.h:171
cBase64CoderT< cUUCodeChar > cUUCode
Definition: cBase64.h:172
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
Definition: cBase64.h:43
static BYTE DEC(char ch) noexcept
Definition: cBase64.h:56
static char ENC(BYTE b)
Definition: cBase64.h:51
Definition: cBase64.h:22
static StrLen_t GetTextSizeFromBinarySize(size_t nSizeBin) noexcept
Definition: cBase64.h:25
static size_t GetBinarySizeFromTextSize(StrLen_t nLenText) noexcept
Definition: cBase64.h:32
Definition: cBase64.h:78
static BYTE DEC(char ch) noexcept
Definition: cBase64.h:90
static char ENC(BYTE b)
Definition: cBase64.h:85
static void Copy(void *pDst, const void *pSrc, size_t nSizeBlock) noexcept
Definition: cMem.h:132
static void Zero(void *pData, size_t nSizeBlock) noexcept
Definition: cMem.h:100
static void FillSize(void *pArray, size_t nArraySizeBytes, TYPE nFillValue) noexcept
Definition: cValT.h:132