Base64 encoding/decoding added
This commit is contained in:
parent
0a2a6e50b9
commit
776c9eb8a7
@ -72,6 +72,7 @@
|
||||
<_ProjectFileVersion>10.0.40219.1</_ProjectFileVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\src\Base64.cpp" />
|
||||
<ClCompile Include="..\src\StdAfx.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
|
||||
@ -80,6 +81,7 @@
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\include\WinStd\Base64.h" />
|
||||
<ClInclude Include="..\include\WinStd\Crypt.h" />
|
||||
<ClInclude Include="..\include\WinStd\EAP.h" />
|
||||
<ClInclude Include="..\include\WinStd\Common.h" />
|
||||
|
@ -14,6 +14,9 @@
|
||||
<ClCompile Include="..\src\StdAfx.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\Base64.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\src\StdAfx.h">
|
||||
@ -43,5 +46,8 @@
|
||||
<ClInclude Include="..\include\WinStd\Sec.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\include\WinStd\Base64.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
186
include/WinStd/Base64.h
Normal file
186
include/WinStd/Base64.h
Normal file
@ -0,0 +1,186 @@
|
||||
/*
|
||||
Copyright 1991-2016 Amebis
|
||||
Copyright 2016 GÉANT
|
||||
|
||||
This file is part of WinStd.
|
||||
|
||||
Setup is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Setup is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Setup. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Common.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace winstd
|
||||
{
|
||||
///
|
||||
/// \defgroup WinStdBase64 Base64 conversion
|
||||
/// Provides Base64 conversion for WinStd classes
|
||||
///
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// Base64 encoding session
|
||||
///
|
||||
class base64_enc
|
||||
{
|
||||
public:
|
||||
base64_enc() : num(0) {};
|
||||
|
||||
|
||||
///
|
||||
/// Encodes one block of information
|
||||
///
|
||||
/// \param[out] out Output
|
||||
/// \param[in ] data Data to encode
|
||||
/// \param[in ] size Length of `data` in bytes
|
||||
/// \param[in ] is_last Is this the last block of data?
|
||||
///
|
||||
void encode(_Out_ std::string &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last = true);
|
||||
|
||||
|
||||
///
|
||||
/// Resets encoding session
|
||||
///
|
||||
inline void clear()
|
||||
{
|
||||
num = 0;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Returns maximum encoded size
|
||||
///
|
||||
/// \param size Number of bytes to encode
|
||||
///
|
||||
/// \returns Maximum number of bytes for the encoded data of `size` length
|
||||
///
|
||||
inline size_t enc_size(size_t size) const
|
||||
{
|
||||
return ((num + size + 2)/3)*4;
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
static inline void encode(_Inout_ std::string &out, _In_ const unsigned char buf[3])
|
||||
{
|
||||
out += lookup[ buf[0] >> 2 ];
|
||||
out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
|
||||
out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
|
||||
out += lookup[ buf[2] & 0x3f];
|
||||
}
|
||||
|
||||
|
||||
static inline void encode(_Inout_ std::string &out, _In_count_(size) const unsigned char buf[3], _In_ size_t size)
|
||||
{
|
||||
if (size > 0) {
|
||||
out += lookup[buf[0] >> 2];
|
||||
if (size > 1) {
|
||||
out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
|
||||
if (size > 2) {
|
||||
out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
|
||||
out += lookup[buf[2] & 0x3f];
|
||||
} else {
|
||||
out += lookup[(buf[1] << 2) & 0x3f];
|
||||
out += '=';
|
||||
}
|
||||
} else {
|
||||
out += lookup[(buf[0] << 4) & 0x3f];
|
||||
out += "==";
|
||||
}
|
||||
} else
|
||||
out += "====";
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
unsigned char buf[3];
|
||||
size_t num;
|
||||
|
||||
static const char lookup[64];
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// Base64 decoding session
|
||||
///
|
||||
class WINSTD_API base64_dec
|
||||
{
|
||||
public:
|
||||
inline base64_dec() : num(0) {};
|
||||
|
||||
|
||||
///
|
||||
/// Decodes one block of information
|
||||
///
|
||||
/// \param[out] out Output
|
||||
/// \param[in ] data Data to decode
|
||||
/// \param[in ] size Length of `data` in bytes
|
||||
/// \param[in ] is_last Was this the last block of data?
|
||||
///
|
||||
void decode(_Out_ std::vector<unsigned char> &out, _Out_ bool &is_last, _In_z_count_(size) const char *data, _In_ size_t size);
|
||||
|
||||
|
||||
///
|
||||
/// Resets decoding session
|
||||
///
|
||||
inline void clear()
|
||||
{
|
||||
num = 0;
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Returns maximum encoded size
|
||||
///
|
||||
/// \param size Number of bytes to encode
|
||||
///
|
||||
/// \returns Maximum number of bytes for the encoded data of `size` length
|
||||
///
|
||||
inline size_t dec_size(size_t size) const
|
||||
{
|
||||
return ((num + size + 3)/4)*3;
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
static inline size_t decode(_Inout_ std::vector<unsigned char> &out, _In_ const unsigned char buf[4])
|
||||
{
|
||||
out.push_back((unsigned char)(((buf[0] << 2) | (buf[1] >> 4)) & 0xff));
|
||||
if (buf[2] < 64) {
|
||||
out.push_back((unsigned char)(((buf[1] << 4) | (buf[2] >> 2)) & 0xff));
|
||||
if (buf[3] < 64) {
|
||||
out.push_back((unsigned char)(((buf[2] << 6) | buf[3]) & 0xff));
|
||||
return 3;
|
||||
} else
|
||||
return 2;
|
||||
} else
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
unsigned char buf[4];
|
||||
size_t num;
|
||||
|
||||
static const unsigned char lookup[256];
|
||||
};
|
||||
|
||||
/// @}
|
||||
}
|
||||
|
@ -29,6 +29,18 @@
|
||||
#include <string>
|
||||
|
||||
|
||||
///
|
||||
/// Public function calling convention
|
||||
///
|
||||
#if defined(WINSTD_DLL)
|
||||
#define WINSTD_API __declspec(dllexport)
|
||||
#elif defined(WINSTD_DLLIMP)
|
||||
#define WINSTD_API __declspec(dllimport)
|
||||
#else
|
||||
#define WINSTD_API
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef WINSTD_STACK_BUFFER_BYTES
|
||||
///
|
||||
/// Size of the stack buffer in bytes used for initial system function call
|
||||
|
115
src/Base64.cpp
Normal file
115
src/Base64.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
/*
|
||||
Copyright 1991-2016 Amebis
|
||||
Copyright 2016 GÉANT
|
||||
|
||||
This file is part of WinStd.
|
||||
|
||||
Setup is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Setup is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Setup. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "StdAfx.h"
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// winstd::base64_enc
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
const char winstd::base64_enc::lookup[64] = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
|
||||
};
|
||||
|
||||
|
||||
void winstd::base64_enc::encode(_Out_ std::string &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last)
|
||||
{
|
||||
assert(data || !size);
|
||||
|
||||
size_t i = 0, j = 0;
|
||||
|
||||
// Convert data character by character.
|
||||
for (;;) {
|
||||
if (num >= 3) {
|
||||
encode(out, buf);
|
||||
num = 0;
|
||||
j += 4;
|
||||
}
|
||||
|
||||
if (i >= size)
|
||||
break;
|
||||
|
||||
buf[num++] = ((unsigned char*)data)[i++];
|
||||
}
|
||||
|
||||
// If this is the last block, flush the buffer.
|
||||
if (is_last && num) {
|
||||
encode(out, buf, num);
|
||||
num = 0;
|
||||
j += 4;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// winstd::base64_dec
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
const unsigned char winstd::base64_dec::lookup[256] = {
|
||||
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
||||
/* 0 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* 1 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* 2 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
|
||||
/* 3 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 64, 255, 255,
|
||||
/* 4 */ 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||
/* 5 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
|
||||
/* 6 */ 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
/* 7 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255,
|
||||
/* 8 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* 9 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* A */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* B */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* C */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* D */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* E */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
/* F */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
|
||||
};
|
||||
|
||||
|
||||
void winstd::base64_dec::decode(_Out_ std::vector<unsigned char> &out, _Out_ bool &is_last, _In_z_count_(size) const char *data, _In_ size_t size)
|
||||
{
|
||||
size_t i = 0, j = 0;
|
||||
size_t nibbles;
|
||||
|
||||
is_last = false;
|
||||
|
||||
for (;;) {
|
||||
if (num >= 4) {
|
||||
// Buffer full; decode it.
|
||||
nibbles = decode(out, buf);
|
||||
j += nibbles;
|
||||
num = 0;
|
||||
if (nibbles < 3) {
|
||||
is_last = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i >= size || !data[i])
|
||||
break;
|
||||
|
||||
if ((buf[num] = lookup[(unsigned char)data[i++]]) != 255)
|
||||
num++;
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../include/WinStd/Base64.h"
|
||||
#include "../include/WinStd/Crypt.h"
|
||||
#include "../include/WinStd/EAP.h"
|
||||
#include "../include/WinStd/Common.h"
|
||||
|
Loading…
x
Reference in New Issue
Block a user