Ocean
Base64.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_IO_BASE_64_H
9 #define META_OCEAN_IO_BASE_64_H
10 
11 #include "ocean/io/IO.h"
12 
13 namespace Ocean
14 {
15 
16 namespace IO
17 {
18 
19 /**
20  * This class implements function to encode binary information to text encoding and vice versa.
21  */
22 class OCEAN_IO_EXPORT Base64
23 {
24  public:
25 
26  /**
27  * Definition of a vector holding characters.
28  */
29  typedef std::vector<uint8_t> Buffer;
30 
31  public:
32 
33  /**
34  * Encodes binary information by application of Base64 to a text encoding.
35  * @param buffer The buffer to encode
36  * @param bufferSize The size of the buffer to encode, in bytes, with range [1, infinity)
37  * @param encodedText The resulting encoded text
38  */
39  static void encode(const uint8_t* buffer, const size_t bufferSize, Buffer& encodedText);
40 
41  /**
42  * Decodes a text encoding by application of an inverse Base64 to binary information.
43  * @param encodedText The encoded text to decode
44  * @param encodedTextSize The size of the text to decode, in characters, with range [4, infinity)
45  * @param buffer The resulting binary information
46  * @return True, if the given encoded text was valid
47  */
48  static bool decode(const uint8_t* encodedText, const size_t encodedTextSize, Buffer& buffer);
49 
50  /**
51  * Encodes 3 bytes of binary information to 4 bytes with text encoding.
52  * @param bytes3 The three bytes to encode
53  * @param encoded4 The resulting four encoded bytes
54  */
55  static inline void encode3(const uint8_t bytes3[3], uint8_t encoded4[4]);
56 
57  /**
58  * Decodes 4 bytes with text encoding to 3 bytes of binary information.
59  * @param encoded4 The four encoded byte to decode
60  * @param bytes3 The resulting three text encoded bytes
61  * @return True, if succeeded
62  */
63  static inline bool decode4(const uint8_t encoded4[4], uint8_t bytes3[3]);
64 
65  /**
66  * Returns whether a given byte is text encoded.
67  * @param encodedValue The text encoded value to check
68  * @return The corresponding value with range [0, 59] if the value is text encoded, 64 otherwise
69  */
70  static inline uint8_t isEncoded(const uint8_t encodedValue);
71 
72  protected:
73 
74  /// The possible encoded characters.
75  static constexpr const char* encodedCharacters_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
76 };
77 
78 inline void Base64::encode3(const uint8_t bytes3[3], uint8_t encoded4[4])
79 {
80  ocean_assert(strlen(encodedCharacters_) == 64);
81 
82  uint8_t value = (bytes3[0] & 0xFCu) >> 2;
83  ocean_assert(value < 64u);
84  encoded4[0] = uint8_t(encodedCharacters_[value]);
85 
86  value = uint8_t(((bytes3[0] & 0x03u) << 4) + ((bytes3[1] & 0xF0u) >> 4));
87  ocean_assert(value < 64u);
88  encoded4[1] = uint8_t(encodedCharacters_[value]);
89 
90  value = uint8_t(((bytes3[1] & 0x0Fu) << 2) + ((bytes3[2] & 0xC0u) >> 6));
91  ocean_assert(value < 64u);
92  encoded4[2] = uint8_t(encodedCharacters_[value]);
93 
94  value = bytes3[2] & 0x3Fu;
95  ocean_assert(value < 64u);
96  encoded4[3] = uint8_t(encodedCharacters_[value]);
97 }
98 
99 inline bool Base64::decode4(const uint8_t encoded4[4], uint8_t bytes3[3])
100 {
101  const uint8_t value0 = isEncoded(encoded4[0]);
102  const uint8_t value1 = isEncoded(encoded4[1]);
103  const uint8_t value2 = isEncoded(encoded4[2]);
104  const uint8_t value3 = isEncoded(encoded4[3]);
105 
106  if (value0 >= 64u || value1 >= 64u || value2 >= 64u || value3 >= 64u)
107  {
108  return false;
109  }
110 
111  bytes3[0] = uint8_t((value0 << 2) | ((value1 & 0x30u) >> 4));
112  bytes3[1] = uint8_t(((value1 & 0x0Fu) << 4) | ((value2 & 0x3Cu) >> 2));
113  bytes3[2] = uint8_t(((value2 & 0x03u) << 6) | value3);
114 
115  return true;
116 }
117 
118 inline unsigned char Base64::isEncoded(const uint8_t encodedValue)
119 {
120  // 0 26 52 62 63
121  // ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 + /
122 
123  if (encodedValue >= 'A' && encodedValue <= 'Z')
124  {
125  return encodedValue - 'A';
126  }
127  else if (encodedValue >= 'a' && encodedValue <= 'z')
128  {
129  return encodedValue - 'a' + 26u;
130  }
131  else if (encodedValue >= '0' && encodedValue <= '9')
132  {
133  return encodedValue - '0' + 52u;
134  }
135  else if (encodedValue == '+')
136  {
137  return 62u;
138  }
139  else if (encodedValue == '/')
140  {
141  return 63u;
142  }
143  else if (encodedValue == 0)
144  {
145  return 0u; // special handling for end of string characters
146  }
147 
148  return 64u;
149 }
150 
151 }
152 
153 }
154 
155 #endif // META_OCEAN_IO_BASE_64_H
This class implements function to encode binary information to text encoding and vice versa.
Definition: Base64.h:23
static void encode3(const uint8_t bytes3[3], uint8_t encoded4[4])
Encodes 3 bytes of binary information to 4 bytes with text encoding.
Definition: Base64.h:78
std::vector< uint8_t > Buffer
Definition of a vector holding characters.
Definition: Base64.h:29
static bool decode4(const uint8_t encoded4[4], uint8_t bytes3[3])
Decodes 4 bytes with text encoding to 3 bytes of binary information.
Definition: Base64.h:99
static bool decode(const uint8_t *encodedText, const size_t encodedTextSize, Buffer &buffer)
Decodes a text encoding by application of an inverse Base64 to binary information.
static void encode(const uint8_t *buffer, const size_t bufferSize, Buffer &encodedText)
Encodes binary information by application of Base64 to a text encoding.
static constexpr const char * encodedCharacters_
The possible encoded characters.
Definition: Base64.h:75
static uint8_t isEncoded(const uint8_t encodedValue)
Returns whether a given byte is text encoded.
Definition: Base64.h:118
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15