forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base32.cc
46 lines (33 loc) · 1.1 KB
/
base32.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/base32.h"
#include <limits>
#include "flutter/fml/macros.h"
namespace fml {
static constexpr char kEncoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
std::pair<bool, std::string> Base32Encode(StringView input) {
if (input.empty()) {
return {true, ""};
}
if (input.size() > std::numeric_limits<size_t>::max() / 8) {
return {false, ""};
}
std::string output;
const size_t encoded_length = (input.size() * 8 + 4) / 5;
output.reserve(encoded_length);
uint16_t bit_stream = (static_cast<uint8_t>(input[0]) << 8);
size_t next_byte_index = 1;
int free_bits = 8;
while (free_bits < 16) {
output.push_back(kEncoding[(bit_stream & 0xf800) >> 11]);
bit_stream <<= 5;
free_bits += 5;
if (free_bits >= 8 && next_byte_index < input.size()) {
free_bits -= 8;
bit_stream += static_cast<uint8_t>(input[next_byte_index++]) << free_bits;
}
}
return {true, output};
}
} // namespace fml