40 #ifndef _binarytool_cpp
41 #define _binarytool_cpp
46 #include "binarybuffer.h"
91 static const String Base64Chars =
92 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
93 "abcdefghijklmnopqrstuvwxyz"
99 void Base64DecodeImpl(
const String& EncodedString, BinaryBuffer& Results)
101 if(Results.Binary==0 || Results.Size==0)
104 String::const_iterator Progress = EncodedString.begin();
112 unsigned char Second;
114 unsigned char Fourth;
116 while(Progress<EncodedString.end())
120 if(!IsBase64(*Progress) || !IsBase64(*(Progress+1)) || !IsBase64(*(Progress+2)) || !IsBase64(*(Progress+3)))
123 First = Base64Chars.find(*(Progress+0));
124 Second = Base64Chars.find(*(Progress+1));
125 Third = *(Progress+2)==
'=' ? 0 : Base64Chars.find(*(Progress+2));
128 if(Output+1>Results.Size)
132 *(Results.Binary+Output+0) = (First << 2) + ((Second & 0x30) >> 4);
133 *(Results.Binary+Output+1) = ((Second & 0xf) << 4) + ((Third & 0x3c) >> 2);
134 if(*(Progress+3)!=
'=')
137 if(Output+2>Results.Size)
140 Fourth = Base64Chars.find(*(Progress+3));
141 *(Results.Binary+Output+2) = ((Third & 0x3) << 6) + Fourth;
145 if(Progress>EncodedString.end())
177 Size(PredeterminedSize),
178 Binary(new
Byte[PredeterminedSize])
182 Size(PredeterminedSize),
183 Binary(BinaryPointer ? BinaryPointer : new
Byte[PredeterminedSize])
192 this->
Size = DataString.size();
194 memcpy(this->
Binary, DataString.c_str(), this->
Size);
216 {
delete[] this->
Binary; }
218 this->
Size = NewSize;
225 this->
Size = PredictBinarySizeFromBase64String(EncodedBinaryData);
227 Base64DecodeImpl(EncodedBinaryData,*
this);
241 Byte* TargetBuffer =
new Byte[NewSize];
243 memcpy(TargetBuffer, this->
Binary, this->
Size);
244 memcpy(TargetBuffer+this->
Size, OtherBuffer, ByteSize);
247 this->
Binary = TargetBuffer;
262 {
return this->
Size; }
269 if( RH.
Binary == this->Binary )
291 if( Index >= this->
Size )
294 return *(this->
Binary + Index);
301 Boole IsBase64(
unsigned char Character)
302 {
return (isalnum(Character) || (Character ==
'+') || (Character ==
'/') || (Character ==
'=')); }
305 {
return Base64Encode((
UInt8 const*)Unencoded.c_str(), Unencoded.size()); }
307 String Base64Encode(
const BinaryBuffer &Buffer)
308 {
return Base64Encode((
const UInt8*) Buffer.Binary,Buffer.Size); }
311 String Base64Encode(
UInt8 const* BytesToEncode,
unsigned int Length)
314 Results.reserve(PredictBase64StringSizeFromBinarySize(Length));
318 unsigned char char_array_3[3];
319 unsigned char char_array_4[4];
323 char_array_3[i++] = *(BytesToEncode++);
326 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
327 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
328 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
329 char_array_4[3] = char_array_3[2] & 0x3f;
331 for(i = 0; (i <4) ; i++)
332 { Results += Base64Chars[char_array_4[i]]; }
339 for(j = i; j < 3; j++)
340 { char_array_3[j] =
'\0'; }
342 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
343 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
344 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
345 char_array_4[3] = char_array_3[2] & 0x3f;
347 for (j = 0; (j < i + 1); j++)
348 { Results += Base64Chars[char_array_4[j]]; }
359 BinaryBuffer Base64Decode(
String const& EncodedString)
361 BinaryBuffer Results(PredictBinarySizeFromBase64String(EncodedString));
363 Base64DecodeImpl(EncodedString, Results);
368 Whole PredictBinarySizeFromBase64String(
String const& EncodedString)
370 if(EncodedString.size()<4)
373 return EncodedString.size()/4*3 - ( EncodedString.at(EncodedString.size()-2)==
'=' ?1:0) - ( EncodedString.at(EncodedString.size()-1)==
'=' ?1:0);
376 Whole PredictBase64StringSizeFromBinarySize(
Whole Length)
377 {
return (Length+2)/3*4; }
bool Boole
Generally acts a single bit, true or false.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
uint8_t UInt8
An 8-bit unsigned integer.
Thrown when a complex class is assigned to itself or other invalid assignments occur.
This implements the exception hiearchy for Mezzanine.
Thrown when the available information should have worked but failed for unknown reasons.
Thrown when parameters are checked at runtime and found invalid.
The bulk of the engine components go in this namspace.
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Thrown when attempted to access something that really should note be accessed.
std::string String
A datatype used to a series of characters.