mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-22 00:56:18 +00:00
Clean up borrowed base64 code to match repo standards
This commit is contained in:
+10
-6
@@ -3,22 +3,26 @@
|
|||||||
#include <torrent/exceptions.h>
|
#include <torrent/exceptions.h>
|
||||||
|
|
||||||
namespace utils {
|
namespace utils {
|
||||||
constexpr static char padCharacter = '=';
|
|
||||||
|
// Modified from the public domain code in
|
||||||
|
// https://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#C++_2
|
||||||
|
constexpr char base64_pad_character = '=';
|
||||||
|
|
||||||
static std::string
|
static std::string
|
||||||
base64decode(const std::string& input) {
|
base64decode(const std::string& input) {
|
||||||
if (input.length() % 4) // Sanity check
|
if (input.length() % 4) // Sanity check
|
||||||
throw torrent::input_error("Invalid base64.");
|
throw torrent::input_error("Invalid base64.");
|
||||||
size_t padding = 0;
|
size_t padding = 0;
|
||||||
if (input.length()) {
|
if (input.length()) {
|
||||||
if (input[input.length() - 1] == padCharacter)
|
if (input[input.length() - 1] == base64_pad_character)
|
||||||
padding++;
|
padding++;
|
||||||
if (input[input.length() - 2] == padCharacter)
|
if (input[input.length() - 2] == base64_pad_character)
|
||||||
padding++;
|
padding++;
|
||||||
}
|
}
|
||||||
// Setup a vector to hold the result
|
// Setup a vector to hold the result
|
||||||
std::string decodedBytes;
|
std::string decodedBytes;
|
||||||
decodedBytes.reserve(((input.length() / 4) * 3) - padding);
|
decodedBytes.reserve(((input.length() / 4) * 3) - padding);
|
||||||
long temp = 0; // Holds decoded quanta
|
long temp = 0; // Holds decoded quanta
|
||||||
std::string::const_iterator cursor = input.begin();
|
std::string::const_iterator cursor = input.begin();
|
||||||
while (cursor < input.end()) {
|
while (cursor < input.end()) {
|
||||||
for (size_t quantumPosition = 0; quantumPosition < 4; quantumPosition++) {
|
for (size_t quantumPosition = 0; quantumPosition < 4; quantumPosition++) {
|
||||||
@@ -32,8 +36,8 @@ base64decode(const std::string& input) {
|
|||||||
else if (*cursor == 0x2B)
|
else if (*cursor == 0x2B)
|
||||||
temp |= 0x3E; // change to 0x2D for URL alphabet
|
temp |= 0x3E; // change to 0x2D for URL alphabet
|
||||||
else if (*cursor == 0x2F)
|
else if (*cursor == 0x2F)
|
||||||
temp |= 0x3F; // change to 0x5F for URL alphabet
|
temp |= 0x3F; // change to 0x5F for URL alphabet
|
||||||
else if (*cursor == padCharacter) // pad
|
else if (*cursor == base64_pad_character) // pad
|
||||||
{
|
{
|
||||||
switch (input.end() - cursor) {
|
switch (input.end() - cursor) {
|
||||||
case 1: // One pad character
|
case 1: // One pad character
|
||||||
|
|||||||
Reference in New Issue
Block a user