Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: components/base32/base32.cc

Issue 2547863002: Move base32 into base so that it can be used within base.
Patch Set: sync to position 437832 Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/base32/base32.h ('k') | components/base32/base32_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium 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 "components/base32/base32.h"
6
7 #include <stddef.h>
8 #include <algorithm>
9 #include <limits>
10
11 #include "base/logging.h"
12
13 namespace base32 {
14
15 namespace {
16 constexpr char kEncoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
17 } // namespace
18
19 std::string Base32Encode(base::StringPiece input, Base32EncodePolicy policy) {
20 if (input.empty())
21 return std::string();
22
23 if (input.size() > std::numeric_limits<size_t>::max() / 8) {
24 NOTREACHED()
25 << "Input is too large and would overflow encoded size computation.";
26 return std::string();
27 }
28
29 // Per RFC4648, the output is formed of 8 characters per 40 bits of input and
30 // another 8 characters for the last group of [1,39] bits in the input.
31 // That is: ceil(input.size() * 8.0 / 40.0) * 8 ==
32 // ceil(input.size() / 5.0) * 8 ==
33 // ((input.size() + 4) / 5) * 8.
34 const size_t padded_length = ((input.size() + 4) / 5) * 8;
35
36 // When no padding is used, the output is exactly 1 character per 5 bits of
37 // input and one more for the last [1,4] bits.
38 // That is: ceil(input.size() * 8.0 / 5.0) ==
39 // (input.size() * 8 + 4) / 5.
40 const size_t unpadded_length = (input.size() * 8 + 4) / 5;
41
42 std::string output;
43 const size_t encoded_length = policy == Base32EncodePolicy::INCLUDE_PADDING
44 ? padded_length
45 : unpadded_length;
46 output.reserve(encoded_length);
47
48 // A bit stream which will be read from the left and appended to from the
49 // right as it's emptied.
50 uint16_t bit_stream = (static_cast<uint8_t>(input[0]) << 8);
51 size_t next_byte_index = 1;
52 int free_bits = 8;
53 while (free_bits < 16) {
54 // Extract the 5 leftmost bits in the stream
55 output.push_back(kEncoding[(bit_stream & 0xf800) >> 11]);
56 bit_stream <<= 5;
57 free_bits += 5;
58
59 // If there is enough room in the bit stream, inject another byte (if there
60 // are any left...).
61 if (free_bits >= 8 && next_byte_index < input.size()) {
62 free_bits -= 8;
63 bit_stream += static_cast<uint8_t>(input[next_byte_index++]) << free_bits;
64 }
65 }
66
67 if (policy == Base32EncodePolicy::INCLUDE_PADDING) {
68 output.append(padded_length - unpadded_length, '=');
69 }
70
71 DCHECK_EQ(encoded_length, output.size());
72 return output;
73 }
74
75 } // namespace base32
OLDNEW
« no previous file with comments | « components/base32/base32.h ('k') | components/base32/base32_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698