From ba4338c4fad65f4efc7c3a60f7bcc0012aaaac35 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 25 Jan 2024 02:18:50 +0300 Subject: [PATCH] small util::base64_decode optimization --- src/util/stringutil.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/util/stringutil.cpp b/src/util/stringutil.cpp index 7c5f2ff9..19bc104d 100644 --- a/src/util/stringutil.cpp +++ b/src/util/stringutil.cpp @@ -229,15 +229,16 @@ std::string util::base64_encode(const ubyte* data, size_t size) { } std::vector util::base64_decode(const char* str, size_t size) { - std::vector bytes; + std::vector bytes((size/4)*3); + ubyte* dst = bytes.data(); for (size_t i = 0; i < size;) { ubyte a = base64_decode_char(ubyte(str[i++])); ubyte b = base64_decode_char(ubyte(str[i++])); ubyte c = base64_decode_char(ubyte(str[i++])); ubyte d = base64_decode_char(ubyte(str[i++])); - bytes.push_back((a << 2) | ((b & 0b110000) >> 4)); - bytes.push_back(((b & 0b1111) << 4) | ((c & 0b111100) >> 2)); - bytes.push_back(((c & 0b11) << 6) | d); + *(dst++) = ((a << 2) | ((b & 0b110000) >> 4)); + *(dst++) = (((b & 0b1111) << 4) | ((c & 0b111100) >> 2)); + *(dst++) = (((c & 0b11) << 6) | d); } if (size >= 2) { size_t outsize = bytes.size();