OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/base32/base32.h" | 5 #include "base/base32.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 | 12 |
13 namespace base32 { | 13 namespace base { |
14 | 14 |
15 namespace { | 15 namespace { |
16 constexpr char kEncoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; | 16 constexpr char kEncoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; |
17 } // namespace | 17 } // namespace |
18 | 18 |
19 std::string Base32Encode(base::StringPiece input, Base32EncodePolicy policy) { | 19 std::string Base32Encode(StringPiece input, Base32EncodePolicy policy) { |
20 if (input.empty()) | 20 if (input.empty()) |
21 return std::string(); | 21 return std::string(); |
22 | 22 |
23 if (input.size() > std::numeric_limits<size_t>::max() / 8) { | 23 if (input.size() > std::numeric_limits<size_t>::max() / 8) { |
24 NOTREACHED() | 24 NOTREACHED() |
25 << "Input is too large and would overflow encoded size computation."; | 25 << "Input is too large and would overflow encoded size computation."; |
26 return std::string(); | 26 return std::string(); |
27 } | 27 } |
28 | 28 |
29 // Per RFC4648, the output is formed of 8 characters per 40 bits of input and | 29 // Per RFC4648, the output is formed of 8 characters per 40 bits of input and |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 } | 65 } |
66 | 66 |
67 if (policy == Base32EncodePolicy::INCLUDE_PADDING) { | 67 if (policy == Base32EncodePolicy::INCLUDE_PADDING) { |
68 output.append(padded_length - unpadded_length, '='); | 68 output.append(padded_length - unpadded_length, '='); |
69 } | 69 } |
70 | 70 |
71 DCHECK_EQ(encoded_length, output.size()); | 71 DCHECK_EQ(encoded_length, output.size()); |
72 return output; | 72 return output; |
73 } | 73 } |
74 | 74 |
75 } // namespace base32 | 75 } // namespace base |
OLD | NEW |