-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBase64.hpp
148 lines (138 loc) · 5.36 KB
/
Base64.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#pragma once
/*
Copyright (c) 2018 Victor Sheinmann, [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//---------------------------------------------------------------------------
// TODO: Preprocessor Flags to make it relocatable and static but with generated chars table to remove it from a binary // And make MiniString optional
class NBase64 // TODO: Derive this from CAlphaCoder <type,alphabet>
{
static const char PaddChar = '=';
static const int CharsNum = 64;
static const int IdxTblLen = 80;
static inline const char* CharsTbl(void){return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";} // 64 chars (Must always be 64, bound to algorythm)
static inline PBYTE IndexTbl(void){static BYTE IndexTbl[IdxTblLen]; return (PBYTE)&IndexTbl;}
public:
//--------------------------------------
static bool IsSkippableChar(BYTE Char)
{
return ((Char == 0x0D)||(Char == 0x0A)); // There is more?
}
//--------------------------------------
//#define BASE64_DECODED_SIZE(cb) (((cb)>>2)*3)
static UINT DecodedSize(UINT size)
{
return (size / 4) * 3;
}
//--------------------------------------
//#define BASE64_ENCODED_SIZE(cb) (((cb)*4+11)/12*4+1)
static UINT EncodedSize(UINT size)
{
return ((size / 3) + (bool)(size % 3)) * 4;
}
//--------------------------------------
static int GetByteFromIndex(BYTE IByte, int& padd) // For Decoding
{
if(IByte == PaddChar){padd--; return 0;}
if((IByte < 43)||(IByte > 122))return -1; // Not a Base64 char
IByte -= 43;
if(IByte >= IdxTblLen)return -2; // The index table is smaller!
return (NBase64::IndexTbl()[IByte] - 1);
}
//--------------------------------------
static bool IsCharBase64(BYTE Char)
{
const char* Chars = NBase64::CharsTbl();
if(Char == NBase64::PaddChar)return true;
for(;*Chars;Chars++)
{
if(*Chars == Char)return true;
}
return false;
}
//--------------------------------------
static void Initialize(void)
{
static bool DoOnce = true;
if(DoOnce) // static DoOnce
{
DoOnce = false;
// CharsTbl[62] = 45; // 0x2D '-'
// CharsTbl[63] = 95; // 0x5F '_'
PBYTE IndexTbl = NBase64::IndexTbl();
for (int i = 0; i < CharsNum; i++)
{
int j = -43 + CharsTbl()[i];
IndexTbl[j] = (BYTE)(i + 1);
}
IndexTbl[2] = 63;
IndexTbl[52] = 64;
}
}
//--------------------------------------
static int Encode(CMiniStr &str)
{
NBase64::Initialize();
int octr = 0;
int padd = 0;
CMiniStr SrcStr = str;
PBYTE CharsTbl = (PBYTE)NBase64::CharsTbl();
str.SetLength(EncodedSize(str.Length()),0);
if(SrcStr.Length() % 3)
{
padd = 3-(SrcStr.Length() % 3);
SrcStr.AddChars(0,padd);
}
for(UINT ictr=0;ictr < SrcStr.Length();ictr+=3,octr+=4)
{
BYTE CharA = SrcStr[ictr+0];
BYTE CharB = SrcStr[ictr+1];
BYTE CharC = SrcStr[ictr+2];
str[octr+0] = CharsTbl[ CharA >> 2]; // 11111100 -> 00111111
str[octr+1] = CharsTbl[(CharB >> 4) | ((CharA & 0x03) << 4)];
str[octr+2] = CharsTbl[(CharC >> 6) | ((CharB & 0x0F) << 2)];
str[octr+3] = CharsTbl[ CharC & 0x3F]; // 00111111
}
for(;padd > 0;padd--)str[octr-padd] = PaddChar; // RF3='{'
return octr;
}
//--------------------------------------
static int Decode(CMiniStr &str)
{
NBase64::Initialize();
int pos = 0;
int Padd = 0;
int Size = str.Length() & ~3;
if(!Size)return -1;
while((Size > 0) && (str.c_data()[Size-4] == PaddChar))Size -= 4; // Remove some excess padding (Prevents converting them into zeroes)
if(Size < 4)return -2; // Too small for a Base64 message! // TODO: Allow incomplete and unpadded segments
int PExt = (Size % 4);
if(PExt)
{
str.SetLength(Size + (4-PExt));
PExt = (PExt-1)-3; // 1,2,3 -> -3,-2,-1
}
for(int ctr = 0;ctr < Size;ctr+=4,pos+=3)
{
Padd = 0;
BYTE ByteA = GetByteFromIndex(str[ctr+0],Padd);
BYTE ByteB = GetByteFromIndex(str[ctr+1],Padd);
BYTE ByteC = GetByteFromIndex(str[ctr+2],Padd);
BYTE ByteD = GetByteFromIndex(str[ctr+3],Padd);
str[pos+0] = (ByteA << 2) | ((ByteB & 0x30) >> 4); // 2 chars make 1 complete byte
str[pos+1] = ((ByteB & 0x0F) << 4) | ((ByteC & 0x3C) >> 2); // 3 chars make 2 complete bytes
str[pos+2] = ((ByteC & 0x03) << 6) | ByteD; // 4 chars make 3 complete bytes
}
pos += Padd + PExt; //for(;pos > 0;pos--){if(str[pos-1]!=0)break;}
str.SetLength(pos);
return pos;
}
//--------------------------------------
};
//---------------------------------------------------------------------------