Flutter Engine
base32.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/fml/base32.h"
6 
7 #include <limits>
8 #include <string>
9 
10 namespace fml {
11 
12 static constexpr char kEncoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
13 
14 std::pair<bool, std::string> Base32Encode(std::string_view input) {
15  if (input.empty()) {
16  return {true, ""};
17  }
18 
19  if (input.size() > std::numeric_limits<size_t>::max() / 8) {
20  return {false, ""};
21  }
22 
23  std::string output;
24  const size_t encoded_length = (input.size() * 8 + 4) / 5;
25  output.reserve(encoded_length);
26 
27  Base32EncodeConverter converter;
28  converter.Append(input[0]);
29  size_t next_byte_index = 1;
30 
31  while (converter.CanExtract()) {
32  output.push_back(kEncoding[converter.Extract()]);
33  if (converter.CanAppend() && next_byte_index < input.size()) {
34  converter.Append(static_cast<uint8_t>(input[next_byte_index++]));
35  }
36  }
37 
38  if (converter.BitsAvailable() > 0) {
39  output.push_back(kEncoding[converter.Peek()]);
40  }
41 
42  return {true, output};
43 }
44 
45 static constexpr signed char kDecodeMap[] = {
46  // starting from ASCII 50 '2'
47  26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1,
48  -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
49  13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
50 
51 static constexpr int kDecodeMapSize =
52  sizeof(kDecodeMap) / sizeof(kDecodeMap[0]);
53 
54 std::pair<bool, std::string> Base32Decode(const std::string& input) {
55  std::string result;
56  Base32DecodeConverter converter;
57  for (char c : input) {
58  int map_index = c - '2';
59  if (map_index < 0 || map_index >= kDecodeMapSize ||
60  kDecodeMap[map_index] == -1) {
61  return {false, result};
62  }
63  converter.Append(kDecodeMap[map_index]);
64  if (converter.CanExtract()) {
65  result.push_back(converter.Extract());
66  }
67  }
68  if (converter.Peek() != 0) {
69  // The padding should always be zero. Return false if not.
70  return {false, result};
71  }
72  return {true, result};
73 }
74 
75 } // namespace fml
bool CanExtract() const
Definition: base32.h:36
int BitsAvailable() const
Definition: base32.h:34
static constexpr int kDecodeMapSize
Definition: base32.cc:51
static constexpr signed char kDecodeMap[]
Definition: base32.cc:45
int Extract()
Definition: base32.h:25
Definition: ascii_trie.cc:9
void Append(int bits)
Definition: base32.h:18
std::pair< bool, std::string > Base32Encode(std::string_view input)
Definition: base32.cc:14
std::pair< bool, std::string > Base32Decode(const std::string &input)
Definition: base32.cc:54
int Peek() const
Definition: base32.h:33
static constexpr char kEncoding[]
Definition: base32.cc:12
bool CanAppend() const
Definition: base32.h:35