Skip to content

Commit 549bd15

Browse files
author
me
committed
optimization
1 parent 4c53fd2 commit 549bd15

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/http.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -744,32 +744,32 @@ namespace http
744744
std::vector<uint8_t> base64_decode(std::string_view data)
745745
{
746746
std::vector<uint8_t> ret;
747-
ret.reserve(data.size() / 4 * 3);
748-
uint8_t word{0};
749-
uint8_t off{8};
747+
ret.resize((data.size() + 3)/ 4 * 3);
748+
uint8_t* out{&ret[0]};
750749

751-
for (size_t i = 0 ; i < data.size() ; ++i)
750+
for (size_t i = 0 ; i < data.size() ; i += 4)
752751
{
753-
if (data[i] == '=')
754-
continue;
752+
const uint8_t a0 = base64_decoded_table[data[i+0]];
753+
const uint8_t a1 = base64_decoded_table[data[i+1]];
755754

756-
const uint8_t sixtet = base64_decoded_table[data[i]];
755+
*out++ = (a0 << 2) | (a1 >> 4);
757756

758-
for (int j = 5 ; j >= 0 ; --j)
757+
if ((i+2) < data.size() && data[i+2] != '=')
759758
{
760-
const uint8_t bit = (sixtet >> j) & 0x1;
759+
const uint8_t a2 = base64_decoded_table[data[i+2]];
761760

762-
word |= (bit << --off);
761+
*out++ = ((a1 & 0xf) << 4) | ((a2 & 0x3c) >> 2);
763762

764-
if (off == 0)
763+
if ((i+3) < data.size() && data[i+3] != '=')
765764
{
766-
ret.push_back(word);
767-
off = 8;
768-
word = 0;
765+
const uint8_t a3 = base64_decoded_table[data[i+3]];
766+
767+
*out++ = (a2 & 0x03) << 6 | a3;
769768
}
770769
}
771770
}
772771

772+
ret.resize(std::distance(&ret[0], out));
773773
return ret;
774774
}
775775

0 commit comments

Comments
 (0)