Skip to content

Commit

Permalink
Minor base64 decode cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
abellgithub committed Apr 9, 2021
1 parent a00819d commit 11c70e8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
21 changes: 6 additions & 15 deletions pdal/util/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,7 @@ std::vector<uint8_t> Utils::base64_decode(std::string const& encoded_string)
unsigned char char_array_4[4], char_array_3[3];
std::vector<uint8_t> ret;

if (in_len % 4)
throw std::runtime_error("Can't decode base64 string whose length "
"is not divisible by 4");

while (in_len-- && (encoded_string[in_] != '=') &&
is_base64(encoded_string[in_]))
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
{
char_array_4[i++] = encoded_string[in_];
in_++;
Expand Down Expand Up @@ -305,15 +300,11 @@ std::vector<uint8_t> Utils::base64_decode(std::string const& encoded_string)
char_array_4[j] = 0;

for (j = 0; j <4; j++)
char_array_4[j] =
static_cast<unsigned char>(base64_chars.find(char_array_4[j]));

char_array_3[0] = (char_array_4[0] << 2) +
((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) +
((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) +
char_array_4[3];
char_array_4[j] = static_cast<unsigned char>(base64_chars.find(char_array_4[j]));

char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

for (j = 0; (j < i - 1); j++)
ret.push_back(char_array_3[j]);
Expand Down
2 changes: 0 additions & 2 deletions test/unit/UtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ TEST(UtilsTest, test_base64)

EXPECT_EQ(decoded.size(), data.size());
EXPECT_EQ(size, begin_size);

EXPECT_THROW(Utils::base64_decode("Thisisatest"), std::runtime_error);
}

TEST(UtilsTest, blanks)
Expand Down

0 comments on commit 11c70e8

Please sign in to comment.