From 510d41ff7d12c8a8ba230d3b3f732b19a20f15e3 Mon Sep 17 00:00:00 2001 From: gcontini <1121667+gcontini@users.noreply.github.com> Date: 周六, 31 10月 2020 10:28:34 +0800 Subject: [PATCH] doc & miscellaneous changes --- src/library/base/base64.cpp | 41 +++++++++++++++++++---------------------- 1 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/library/base/base64.cpp b/src/library/base/base64.cpp index af66658..caa0ddc 100644 --- a/src/library/base/base64.cpp +++ b/src/library/base/base64.cpp @@ -1,5 +1,6 @@ #include <stdio.h> #include <stdlib.h> +#include <algorithm> #include "base64.h" namespace license { @@ -38,7 +39,7 @@ }; // This array has 255 elements // review api -void add_CR_if_needed(string& encodeBuffer, int lineLenght) { +static void add_CR_if_needed(string& encodeBuffer, int lineLenght) { if (lineLenght > 0 && ((encodeBuffer.size() + 1) % lineLenght) == 0) { encodeBuffer += '\n'; } @@ -48,7 +49,7 @@ const unsigned char* bin = (const unsigned char*)binaryData; int rc = 0; // result counter - int byteNo; // I need this after the loop + unsigned int byteNo; // I need this after the loop int modulusLen = len % 3; int pad = ((modulusLen & 1) << 1) + ((modulusLen & 2) >> 1); // 2 gives 1 and 1 gives 2, but 0 gives 0. @@ -102,28 +103,26 @@ return encodeBuffer; } -unsigned char* unbase64(const char* ascii, int len, int* flen) { - const unsigned char* safeAsciiPtr = (const unsigned char*)ascii; - unsigned char* bin; +std::vector<uint8_t> unbase64(const std::string& base64_data) { + string tmp_str(base64_data); + tmp_str.erase(std::remove(tmp_str.begin(), tmp_str.end(), '\n'), tmp_str.end()); + const unsigned char* safeAsciiPtr = (const unsigned char*)tmp_str.c_str(); + std::vector<uint8_t> bin; int cb = 0; - int charNo; + unsigned int charNo; int pad = 0; + size_t len = tmp_str.size(); if (len < 2) { // 2 accesses below would be OOB. // catch empty string, return NULL as result. puts("ERROR: You passed an invalid base64 string (too short). You get NULL back."); - *flen = 0; - return 0; + return bin; } if (safeAsciiPtr[len - 1] == '=') ++pad; if (safeAsciiPtr[len - 2] == '=') ++pad; - *flen = 3 * len / 4 - pad; - bin = (unsigned char*)malloc(*flen); - if (!bin) { - puts("ERROR: unbase64 could not allocate enough memory."); - return 0; - } + size_t flen = 3 * len / 4 - pad; + bin.reserve(flen); for (charNo = 0; charNo <= len - 4 - pad; charNo += 4) { int A = unb64[safeAsciiPtr[charNo]]; @@ -131,23 +130,21 @@ int C = unb64[safeAsciiPtr[charNo + 2]]; int D = unb64[safeAsciiPtr[charNo + 3]]; - bin[cb++] = (A << 2) | (B >> 4); - bin[cb++] = (B << 4) | (C >> 2); - bin[cb++] = (C << 6) | (D); + bin.push_back((A << 2) | (B >> 4)); + bin.push_back((B << 4) | (C >> 2)); + bin.push_back((C << 6) | (D)); } if (pad == 1) { int A = unb64[safeAsciiPtr[charNo]]; int B = unb64[safeAsciiPtr[charNo + 1]]; int C = unb64[safeAsciiPtr[charNo + 2]]; - - bin[cb++] = (A << 2) | (B >> 4); - bin[cb++] = (B << 4) | (C >> 2); + bin.push_back((A << 2) | (B >> 4)); + bin.push_back((B << 4) | (C >> 2)); } else if (pad == 2) { int A = unb64[safeAsciiPtr[charNo]]; int B = unb64[safeAsciiPtr[charNo + 1]]; - - bin[cb++] = (A << 2) | (B >> 4); + bin.push_back((A << 2) | (B >> 4)); } return bin; -- Gitblit v1.9.1