Skip to content

Commit 83a66f4

Browse files
author
Peter Thorson
committed
Update base64_encode length parameter to more sane type references XRPLF#358
Also adds documentation and cleans up formatting
1 parent 2e7a902 commit 83a66f4

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

changelog.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ HEAD
1010
std::ostreams. This allows writing logging policies that do not involve the
1111
use of std::ostream. This does not affect anyone using the built in logging
1212
policies.
13-
- BREAKING UTILITY CHANGE: websocketpp::lib::net::htonll and
14-
websocketpp::lib::net::ntohll have been prefixed with an underscore to avoid
13+
- BREAKING UTILITY CHANGE: `websocketpp::lib::net::htonll` and
14+
`websocketpp::lib::net::ntohll` have been prefixed with an underscore to avoid
1515
conflicts with similarly named macros in some operating systems. If you are
1616
using the WebSocket++ provided 64 bit host/network byte order functions you
1717
will need to switch to the prefixed versions.
18+
- BREAKING UTILITY CHANGE: The signature of the `base64_encode` has changed from
19+
`websocketpp::base64_encode(unsigned char const *, unsigned int)` to
20+
`websocketpp::base64_encode(unsigned char const *, size_t)`.
1821
- Feature: Adds incomplete `minimal_server` and `minimal_client` configs that
1922
can be used to build custom configs without pulling in the dependencies of
2023
`core` or `core_client`. These configs will offer a stable base config to

websocketpp/base64/base64.hpp

+35-13
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,33 @@ static std::string const base64_chars =
4545
"abcdefghijklmnopqrstuvwxyz"
4646
"0123456789+/";
4747

48+
/// Test whether a character is a valid base64 character
49+
/**
50+
* @param c The character to test
51+
* @return true if c is a valid base64 character
52+
*/
4853
static inline bool is_base64(unsigned char c) {
4954
return (c == 43 || // +
5055
(c >= 47 && c <= 57) || // /-9
5156
(c >= 65 && c <= 90) || // A-Z
5257
(c >= 97 && c <= 122)); // a-z
5358
}
5459

55-
inline std::string base64_encode(unsigned char const * bytes_to_encode, unsigned
56-
int in_len)
57-
{
60+
/// Encode a char buffer into a base64 string
61+
/**
62+
* @param input The input data
63+
* @param len The length of input in bytes
64+
* @return A base64 encoded string representing input
65+
*/
66+
inline std::string base64_encode(unsigned char const * input, size_t len) {
5867
std::string ret;
5968
int i = 0;
6069
int j = 0;
6170
unsigned char char_array_3[3];
6271
unsigned char char_array_4[4];
6372

64-
while (in_len--) {
65-
char_array_3[i++] = *(bytes_to_encode++);
73+
while (len--) {
74+
char_array_3[i++] = *(input++);
6675
if (i == 3) {
6776
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
6877
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
@@ -97,25 +106,38 @@ inline std::string base64_encode(unsigned char const * bytes_to_encode, unsigned
97106
while((i++ < 3)) {
98107
ret += '=';
99108
}
100-
}
109+
}
101110

102-
return ret;
111+
return ret;
103112
}
104113

105-
inline std::string base64_encode(std::string const & data) {
106-
return base64_encode(reinterpret_cast<const unsigned char *>(data.data()),data.size());
114+
/// Encode a string into a base64 string
115+
/**
116+
* @param input The input data
117+
* @return A base64 encoded string representing input
118+
*/
119+
inline std::string base64_encode(std::string const & input) {
120+
return base64_encode(
121+
reinterpret_cast<const unsigned char *>(input.data()),
122+
input.size()
123+
);
107124
}
108125

109-
inline std::string base64_decode(std::string const & encoded_string) {
110-
size_t in_len = encoded_string.size();
126+
/// Decode a base64 encoded string into a string of raw bytes
127+
/**
128+
* @param input The base64 encoded input data
129+
* @return A string representing the decoded raw bytes
130+
*/
131+
inline std::string base64_decode(std::string const & input) {
132+
size_t in_len = input.size();
111133
int i = 0;
112134
int j = 0;
113135
int in_ = 0;
114136
unsigned char char_array_4[4], char_array_3[3];
115137
std::string ret;
116138

117-
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
118-
char_array_4[i++] = encoded_string[in_]; in_++;
139+
while (in_len-- && ( input[in_] != '=') && is_base64(input[in_])) {
140+
char_array_4[i++] = input[in_]; in_++;
119141
if (i ==4) {
120142
for (i = 0; i <4; i++) {
121143
char_array_4[i] = static_cast<unsigned char>(base64_chars.find(char_array_4[i]));

0 commit comments

Comments
 (0)