Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| 10 namespace base32 { | |
| 11 | |
| 12 namespace { | |
| 13 const char kEncoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; | |
|
gab
2016/06/03 18:46:11
constexpr
Rob Percival
2016/06/04 06:20:25
Done.
| |
| 14 } // namespace | |
| 15 | |
| 16 std::string Base32Encode(base::StringPiece input, Base32EncodePolicy policy) { | |
| 17 std::string output; | |
|
gab
2016/06/03 18:46:11
if (input.size() > std::numeric_limits<size_t>::ma
Rob Percival
2016/06/04 06:20:25
Done. An alternative would be letting the caller r
| |
| 18 | |
| 19 // Transform 5 input bytes into 8 output bytes at a time. | |
|
gab
2016/06/03 18:46:11
s/output bytes/characters/ (or "encoded characters
Rob Percival
2016/06/04 06:20:25
Acknowledged.
| |
| 20 // Based on the encoding process described in RFC4648, section 6. | |
| 21 for (size_t input_offset = 0; input_offset < input.size(); | |
|
gab
2016/06/03 18:46:11
0U
Rob Percival
2016/06/04 06:20:25
What effect does this have?
gab
2016/06/07 01:24:10
Explicitly declares this constant as unsigned. I g
| |
| 22 input_offset += 5) { | |
|
gab
2016/06/03 18:46:11
5U
Rob Percival
2016/06/04 06:20:25
What effect does this have?
| |
| 23 // Bits of input left to encode. | |
|
gab
2016/06/03 18:46:11
// Bits of input left to encode in the current 40
Rob Percival
2016/06/04 06:20:25
Acknowledged.
| |
| 24 size_t bits_remaining = | |
| 25 std::min(size_t(5), input.size() - input_offset) * 8; | |
|
gab
2016/06/03 18:46:11
s/size_t(5)/5U/
Rob Percival
2016/06/04 06:20:25
Is this semantically better or just shorter?
gab
2016/06/07 01:24:10
It's more readable IMO because it's clearly a cons
| |
| 26 // A buffer for up to 40 bits of input that is consumed 5 bits at a time. | |
| 27 // 40 bits is used because it is the lowest common multiple of 5 and 8. | |
|
gab
2016/06/03 18:46:11
I'd remove the second sentence, no need to justify
Rob Percival
2016/06/04 06:20:25
Acknowledged.
| |
| 28 uint64_t input_group = 0; | |
| 29 | |
| 30 // Load 8 bits at a time into the buffer. | |
|
gab
2016/06/03 18:46:11
s/Load 8 bits at a time./Fill the buffer, 8 bits a
Rob Percival
2016/06/04 06:20:25
Acknowledged.
| |
| 31 // Can't simply memcpy because of endianness. | |
| 32 for (size_t i = 0; i < bits_remaining / 8; ++i) { | |
|
gab
2016/06/03 18:46:11
If |bits_remaining == 39| then 39 / 8 is 4 and you
Rob Percival
2016/06/04 06:20:24
|bits_remaining| is always a multiple of 8 at this
| |
| 33 input_group <<= 8; | |
| 34 input_group |= static_cast<uint8_t>(input[input_offset + i]); | |
| 35 } | |
| 36 // Shift the bits we've set to start at the MSB, for ease of access. | |
| 37 input_group <<= (64 - bits_remaining); | |
| 38 | |
| 39 // Output the |input_group| as up to 8 bytes in base32. | |
| 40 // If we run out of bits, padding will make up the remaining bytes | |
| 41 // (unless |policy| dictates otherwise). | |
| 42 for (size_t output_count = 0; output_count < 8; ++output_count) { | |
| 43 if (bits_remaining == 0) { | |
| 44 if (policy == Base32EncodePolicy::INCLUDE_PADDING) | |
| 45 output.append(8 - output_count, '='); | |
| 46 break; | |
| 47 } | |
| 48 // Encode the first 5 bits, then shift them out of the buffer. | |
| 49 output.push_back(kEncoding[input_group >> 59]); | |
| 50 input_group <<= 5; | |
| 51 bits_remaining -= std::min(size_t(5), bits_remaining); | |
| 52 } | |
| 53 } | |
| 54 | |
|
gab
2016/06/03 18:46:11
#include "base/logging.h"
DCHECK_EQ(encoded_lengt
Rob Percival
2016/06/04 06:20:25
Done.
| |
| 55 return output; | |
| 56 } | |
| 57 | |
| 58 } // namespace base32 | |
| OLD | NEW |