|
| 1 | +// |
| 2 | +// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// Official repository: https://github.com/boostorg/beast |
| 8 | +// |
| 9 | + |
| 10 | +/* |
| 11 | + Portions from http://www.adp-gmbh.ch/cpp/common/base64.html |
| 12 | + Copyright notice: |
| 13 | +
|
| 14 | + base64.cpp and base64.h |
| 15 | +
|
| 16 | + Copyright (C) 2004-2008 Rene Nyffenegger |
| 17 | +
|
| 18 | + This source code is provided 'as-is', without any express or implied |
| 19 | + warranty. In no event will the author be held liable for any damages |
| 20 | + arising from the use of this software. |
| 21 | +
|
| 22 | + Permission is granted to anyone to use this software for any purpose, |
| 23 | + including commercial applications, and to alter it and redistribute it |
| 24 | + freely, subject to the following restrictions: |
| 25 | +
|
| 26 | + 1. The origin of this source code must not be misrepresented; you must not |
| 27 | + claim that you wrote the original source code. If you use this source code |
| 28 | + in a product, an acknowledgment in the product documentation would be |
| 29 | + appreciated but is not required. |
| 30 | +
|
| 31 | + 2. Altered source versions must be plainly marked as such, and must not be |
| 32 | + misrepresented as being the original source code. |
| 33 | +
|
| 34 | + 3. This notice may not be removed or altered from any source distribution. |
| 35 | +
|
| 36 | + Rene Nyffenegger rene.nyffenegger@adp-gmbh.ch |
| 37 | +*/ |
| 38 | + |
| 39 | +#include "src/impl/base64.hpp" |
| 40 | +#include <cctype> |
| 41 | +#include <string> |
| 42 | +#include <utility> |
| 43 | + |
| 44 | +namespace boost { |
| 45 | +namespace ws_proto { |
| 46 | + |
| 47 | +char const* |
| 48 | +base64_alphabet() noexcept |
| 49 | +{ |
| 50 | + static char constexpr tab[] = { |
| 51 | + "ABCDEFGHIJKLMNOP" |
| 52 | + "QRSTUVWXYZabcdef" |
| 53 | + "ghijklmnopqrstuv" |
| 54 | + "wxyz0123456789+/" |
| 55 | + }; |
| 56 | + return &tab[0]; |
| 57 | +} |
| 58 | + |
| 59 | +signed char const* |
| 60 | +base64_inverse() noexcept |
| 61 | +{ |
| 62 | + static signed char constexpr tab[] = { |
| 63 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15 |
| 64 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31 |
| 65 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 32-47 |
| 66 | + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 48-63 |
| 67 | + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79 |
| 68 | + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 80-95 |
| 69 | + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111 |
| 70 | + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 112-127 |
| 71 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128-143 |
| 72 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144-159 |
| 73 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160-175 |
| 74 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176-191 |
| 75 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192-207 |
| 76 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208-223 |
| 77 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239 |
| 78 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 240-255 |
| 79 | + }; |
| 80 | + return &tab[0]; |
| 81 | +} |
| 82 | + |
| 83 | +/** Encode a series of octets as a padded, base64 string. |
| 84 | +
|
| 85 | + The resulting string will not be null terminated. |
| 86 | +
|
| 87 | + @par Requires |
| 88 | +
|
| 89 | + The memory pointed to by `out` points to valid memory |
| 90 | + of at least `encoded_size(len)` bytes. |
| 91 | +
|
| 92 | + @return The number of characters written to `out`. This |
| 93 | + will exclude any null termination. |
| 94 | +*/ |
| 95 | +std::size_t |
| 96 | +base64_encode(void* dest, void const* src, std::size_t len) |
| 97 | +{ |
| 98 | + char* out = static_cast<char*>(dest); |
| 99 | + char const* in = static_cast<char const*>(src); |
| 100 | + auto const tab = base64_alphabet(); |
| 101 | + |
| 102 | + for(auto n = len / 3; n--;) |
| 103 | + { |
| 104 | + *out++ = tab[ (in[0] & 0xfc) >> 2]; |
| 105 | + *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)]; |
| 106 | + *out++ = tab[((in[2] & 0xc0) >> 6) + ((in[1] & 0x0f) << 2)]; |
| 107 | + *out++ = tab[ in[2] & 0x3f]; |
| 108 | + in += 3; |
| 109 | + } |
| 110 | + |
| 111 | + switch(len % 3) |
| 112 | + { |
| 113 | + case 2: |
| 114 | + *out++ = tab[ (in[0] & 0xfc) >> 2]; |
| 115 | + *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)]; |
| 116 | + *out++ = tab[ (in[1] & 0x0f) << 2]; |
| 117 | + *out++ = '='; |
| 118 | + break; |
| 119 | + |
| 120 | + case 1: |
| 121 | + *out++ = tab[ (in[0] & 0xfc) >> 2]; |
| 122 | + *out++ = tab[((in[0] & 0x03) << 4)]; |
| 123 | + *out++ = '='; |
| 124 | + *out++ = '='; |
| 125 | + break; |
| 126 | + |
| 127 | + case 0: |
| 128 | + break; |
| 129 | + } |
| 130 | + |
| 131 | + return out - static_cast<char*>(dest); |
| 132 | +} |
| 133 | + |
| 134 | +std::pair<std::size_t, std::size_t> |
| 135 | +base64_decode(void* dest, char const* src, std::size_t len) |
| 136 | +{ |
| 137 | + char* out = static_cast<char*>(dest); |
| 138 | + auto in = reinterpret_cast<unsigned char const*>(src); |
| 139 | + unsigned char c3[3], c4[4] = {0,0,0,0}; |
| 140 | + int i = 0; |
| 141 | + int j = 0; |
| 142 | + |
| 143 | + auto const inverse = base64_inverse(); |
| 144 | + |
| 145 | + while(len-- && *in != '=') |
| 146 | + { |
| 147 | + auto const v = inverse[*in]; |
| 148 | + if(v == -1) |
| 149 | + break; |
| 150 | + ++in; |
| 151 | + c4[i] = v; |
| 152 | + if(++i == 4) |
| 153 | + { |
| 154 | + c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4); |
| 155 | + c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2); |
| 156 | + c3[2] = ((c4[2] & 0x3) << 6) + c4[3]; |
| 157 | + |
| 158 | + for(i = 0; i < 3; i++) |
| 159 | + *out++ = c3[i]; |
| 160 | + i = 0; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if(i) |
| 165 | + { |
| 166 | + c3[0] = ( c4[0] << 2) + ((c4[1] & 0x30) >> 4); |
| 167 | + c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2); |
| 168 | + c3[2] = ((c4[2] & 0x3) << 6) + c4[3]; |
| 169 | + |
| 170 | + for(j = 0; j < i - 1; j++) |
| 171 | + *out++ = c3[j]; |
| 172 | + } |
| 173 | + |
| 174 | + return {out - static_cast<char*>(dest), |
| 175 | + in - reinterpret_cast<unsigned char const*>(src)}; |
| 176 | +} |
| 177 | + |
| 178 | +} // ws_proto |
| 179 | +} // boost |
0 commit comments