Skip to content

Commit

Permalink
Introduce utility class for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe-Abraham committed Jul 12, 2024
1 parent 3e7ff98 commit a17586d
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 152 deletions.
62 changes: 21 additions & 41 deletions velox/common/encode/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@ constexpr static int kBinaryBlockByteSize = 3;
// Size of an encoded block in bytes (4 bytes = 24 bits)
constexpr static int kEncodedBlockByteSize = 4;

constexpr static int kBase = 64; // Encoding base

// Character sets for Base64 and Base64 URL encoding
constexpr const Base64::Charset kBase64Charset = {
constexpr const Charset kBase64Charset = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};

constexpr const Base64::Charset kBase64UrlCharset = {
constexpr const Charset kBase64UrlCharset = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'};

// Reverse lookup tables for decoding
constexpr const Base64::ReverseIndex kBase64ReverseIndexTable = {
constexpr const ReverseIndex kBase64ReverseIndexTable = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255,
Expand All @@ -67,7 +69,7 @@ constexpr const Base64::ReverseIndex kBase64ReverseIndexTable = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255};

constexpr const Base64::ReverseIndex kBase64UrlReverseIndexTable = {
constexpr const ReverseIndex kBase64UrlReverseIndexTable = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255,
Expand All @@ -90,7 +92,7 @@ constexpr const Base64::ReverseIndex kBase64UrlReverseIndexTable = {
// Verify that for every entry in kBase64Charset, the corresponding entry
// in kBase64ReverseIndexTable is correct.
static_assert(
Base64::checkForwardIndex(
checkForwardIndex(
sizeof(kBase64Charset) - 1,
kBase64Charset,
kBase64ReverseIndexTable),
Expand All @@ -99,39 +101,28 @@ static_assert(
// Verify that for every entry in kBase64UrlCharset, the corresponding entry
// in kBase64UrlReverseIndexTable is correct.
static_assert(
Base64::checkForwardIndex(
checkForwardIndex(
sizeof(kBase64UrlCharset) - 1,
kBase64UrlCharset,
kBase64UrlReverseIndexTable),
"kBase64UrlCharset has incorrect entries");

// static
const bool Base64::findCharacterInCharSet(
const Base64::Charset& charset,
uint8_t idx,
const char c) {
for (; idx < charset.size(); ++idx) {
if (charset[idx] == c) {
return true;
}
}
return false;
}

// Verify that for every entry in kBase64ReverseIndexTable, the corresponding
// entry in kBase64Charset is correct.
static_assert(
Base64::checkReverseIndex(
checkReverseIndex(
sizeof(kBase64ReverseIndexTable) - 1,
kBase,
kBase64Charset,
kBase64ReverseIndexTable),
"kBase64ReverseIndexTable has incorrect entries.");

// Verify that for every entry in kBase64ReverseIndexTable, the corresponding
// entry in kBase64Charset is correct.
static_assert(
Base64::checkReverseIndex(
checkReverseIndex(
sizeof(kBase64UrlReverseIndexTable) - 1,
kBase,
kBase64UrlCharset,
kBase64UrlReverseIndexTable),
"kBase64UrlReverseIndexTable has incorrect entries.");
Expand Down Expand Up @@ -175,7 +166,7 @@ void Base64::encodeUrl(const char* data, size_t len, char* output) {
template <class T>
/* static */ void Base64::encodeImpl(
const T& data,
const Base64::Charset& charset,
const Charset& charset,
bool include_pad,
char* out) {
auto len = data.size();
Expand Down Expand Up @@ -305,17 +296,6 @@ void Base64::decode(const char* data, size_t size, char* output) {
Base64::decode(data, size, output, out_len);
}

// static
uint8_t Base64::base64ReverseLookup(
char p,
const Base64::ReverseIndex& reverseIndex) {
auto curr = reverseIndex[(uint8_t)p];
if (curr >= 0x40) {
VELOX_USER_FAIL("decode() - invalid input string: invalid characters");
}
return curr;
}

// static
size_t
Base64::decode(const char* src, size_t src_len, char* dst, size_t dst_len) {
Expand Down Expand Up @@ -388,10 +368,10 @@ size_t Base64::decodeImpl(
// Each character of the 4 encode 6 bits of the original, grab each with
// the appropriate shifts to rebuild the original and then split that back
// into the original 8 bit bytes.
uint32_t last = (base64ReverseLookup(src[0], reverseIndex) << 18) |
(base64ReverseLookup(src[1], reverseIndex) << 12) |
(base64ReverseLookup(src[2], reverseIndex) << 6) |
base64ReverseLookup(src[3], reverseIndex);
uint32_t last = (baseReverseLookup(kBase, src[0], reverseIndex) << 18) |
(baseReverseLookup(kBase, src[1], reverseIndex) << 12) |
(baseReverseLookup(kBase, src[2], reverseIndex) << 6) |
baseReverseLookup(kBase, src[3], reverseIndex);
dst[0] = (last >> 16) & 0xff;
dst[1] = (last >> 8) & 0xff;
dst[2] = last & 0xff;
Expand All @@ -400,14 +380,14 @@ size_t Base64::decodeImpl(
// Handle the last 2-4 characters. This is similar to the above, but the
// last 2 characters may or may not exist.
DCHECK(src_len >= 2);
uint32_t last = (base64ReverseLookup(src[0], reverseIndex) << 18) |
(base64ReverseLookup(src[1], reverseIndex) << 12);
uint32_t last = (baseReverseLookup(kBase, src[0], reverseIndex) << 18) |
(baseReverseLookup(kBase, src[1], reverseIndex) << 12);
dst[0] = (last >> 16) & 0xff;
if (src_len > 2) {
last |= base64ReverseLookup(src[2], reverseIndex) << 6;
last |= baseReverseLookup(kBase, src[2], reverseIndex) << 6;
dst[1] = (last >> 8) & 0xff;
if (src_len > 3) {
last |= base64ReverseLookup(src[3], reverseIndex);
last |= baseReverseLookup(kBase, src[3], reverseIndex);
dst[2] = last & 0xff;
}
}
Expand Down
60 changes: 2 additions & 58 deletions velox/common/encode/Base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <folly/Range.h>
#include <folly/io/IOBuf.h>

#include "velox/common/encode/EncoderUtils.h"

namespace facebook::velox::encoding {

class Base64 {
Expand Down Expand Up @@ -109,64 +111,6 @@ class Base64 {
static void
decodeUrl(const char* src, size_t src_len, char* dst, size_t dst_len);

/// Checks if there is padding in encoded data.
static inline bool isPadded(const char* data, size_t len) {
return (len > 0 && data[len - 1] == kPadding);
}

/// Counts the number of padding characters in encoded data.
static inline size_t numPadding(const char* src, size_t len) {
size_t numPadding{0};
while (len > 0 && src[len - 1] == kPadding) {
numPadding++;
len--;
}
return numPadding;
}

/// Performs a reverse lookup in the reverse index to retrieve the original
/// index of a character in the base.
static uint8_t base64ReverseLookup(char p, const ReverseIndex& reverseIndex);

/// Searches for a character within a charset up to a certain index.
static const bool
findCharacterInCharSet(const Charset& charset, uint8_t idx, const char c);

// Validate the character in charset with ReverseIndex table
static constexpr bool checkForwardIndex(
uint8_t idx,
const Charset& charset,
const ReverseIndex& reverseIndex) {
for (uint8_t i = 0; i <= idx; ++i) {
if (!(reverseIndex[static_cast<uint8_t>(charset[i])] == i)) {
return false;
}
}
return true;
}

/// Checks the consistency of a reverse index mapping for a given character
/// set.
static constexpr bool checkReverseIndex(
uint8_t idx,
const Charset& charset,
const ReverseIndex& reverseIndex) {
for (uint8_t currentIdx = idx; currentIdx != static_cast<uint8_t>(-1);
--currentIdx) {
if (reverseIndex[currentIdx] == 255) {
if (Base64::findCharacterInCharSet(
charset, 0, static_cast<char>(currentIdx))) {
return false;
}
} else {
if (!(charset[reverseIndex[currentIdx]] == currentIdx)) {
return false;
}
}
}
return true;
}

private:
/// Encodes the specified data using the provided charset.
template <class T>
Expand Down
117 changes: 117 additions & 0 deletions velox/common/encode/EncoderUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <exception>
#include <map>
#include <string>

#include <folly/Range.h>

#include "velox/common/base/Exceptions.h"

namespace facebook::velox::encoding {

const size_t kCharsetSize = 64;
const size_t kReverseIndexSize = 256;

/// Character set used for encoding purposes.
/// Contains specific characters that form the encoding scheme.
using Charset = std::array<char, kCharsetSize>;

/// Reverse lookup table for decoding purposes.
/// Maps each possible encoded character to its corresponding numeric value
/// within the encoding base.
using ReverseIndex = std::array<uint8_t, kReverseIndexSize>;

/// Padding character used in encoding.
const static char kPadding = '=';
/// Checks if there is padding in encoded data.
static inline bool isPadded(const char* data, size_t len) {
return (len > 0 && data[len - 1] == kPadding) ? true : false;
}

/// Counts the number of padding characters in encoded data.
static inline size_t numPadding(const char* src, size_t len) {
size_t numPadding{0};
while (len > 0 && src[len - 1] == kPadding) {
numPadding++;
len--;
}
return numPadding;
}
/// Performs a reverse lookup in the reverse index to retrieve the original
/// index of a character in the base.
inline uint8_t
baseReverseLookup(int base, char p, const ReverseIndex& reverseIndex) {
auto curr = reverseIndex[(uint8_t)p];
if (curr >= base) {
VELOX_USER_FAIL("decode() - invalid input string: invalid characters");
}
return curr;
}

// Validate the character in charset with ReverseIndex table
static constexpr bool checkForwardIndex(
uint8_t idx,
const Charset& charset,
const ReverseIndex& reverseIndex) {
for (uint8_t i = 0; i <= idx; ++i) {
if (!(reverseIndex[static_cast<uint8_t>(charset[i])] == i)) {
return false;
}
}
return true;
}

/// Searches for a character within a charset up to a certain index.
static const bool findCharacterInCharSet(
const Charset& charset,
int base,
uint8_t idx,
const char c) {
for (; idx < base; ++idx) {
if (charset[idx] == c) {
return true;
}
}
return false;
}

/// Checks the consistency of a reverse index mapping for a given character
/// set.
static constexpr bool checkReverseIndex(
uint8_t idx,
int base,
const Charset& charset,
const ReverseIndex& reverseIndex) {
for (uint8_t currentIdx = idx; currentIdx != static_cast<uint8_t>(-1);
--currentIdx) {
if (reverseIndex[currentIdx] == 255) {
if (findCharacterInCharSet(
charset, base, 0, static_cast<char>(currentIdx))) {
return false;
}
} else {
if (!(charset[reverseIndex[currentIdx]] == currentIdx)) {
return false;
}
}
}
return true;
}

} // namespace facebook::velox::encoding
Loading

0 comments on commit a17586d

Please sign in to comment.