diff --git a/build/WinStd.vcxproj b/build/WinStd.vcxproj index 6e48c6c9..1e8085c9 100644 --- a/build/WinStd.vcxproj +++ b/build/WinStd.vcxproj @@ -72,6 +72,7 @@ <_ProjectFileVersion>10.0.40219.1 + Create Create @@ -80,6 +81,7 @@ + diff --git a/build/WinStd.vcxproj.filters b/build/WinStd.vcxproj.filters index df0991a4..6136d96a 100644 --- a/build/WinStd.vcxproj.filters +++ b/build/WinStd.vcxproj.filters @@ -14,6 +14,9 @@ Source Files + + Source Files + @@ -43,5 +46,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/include/WinStd/Base64.h b/include/WinStd/Base64.h new file mode 100644 index 00000000..0724a201 --- /dev/null +++ b/include/WinStd/Base64.h @@ -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 . +*/ + +#pragma once + +#include "Common.h" + +#include +#include + + +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 &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 &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]; + }; + + /// @} +} + diff --git a/include/WinStd/Common.h b/include/WinStd/Common.h index 28e33bc0..336205fa 100644 --- a/include/WinStd/Common.h +++ b/include/WinStd/Common.h @@ -29,6 +29,18 @@ #include +/// +/// 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 diff --git a/src/Base64.cpp b/src/Base64.cpp new file mode 100644 index 00000000..4c3c07fe --- /dev/null +++ b/src/Base64.cpp @@ -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 . +*/ + +#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 &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++; + } +} diff --git a/src/StdAfx.h b/src/StdAfx.h index 90eab454..56836c6b 100644 --- a/src/StdAfx.h +++ b/src/StdAfx.h @@ -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"