/src/abseil-cpp/absl/strings/internal/escaping.cc
Line | Count | Source |
1 | | // Copyright 2020 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "absl/strings/internal/escaping.h" |
16 | | |
17 | | #include <limits> |
18 | | |
19 | | #include "absl/base/internal/endian.h" |
20 | | #include "absl/base/internal/raw_logging.h" |
21 | | |
22 | | namespace absl { |
23 | | ABSL_NAMESPACE_BEGIN |
24 | | namespace strings_internal { |
25 | | |
26 | 0 | size_t CalculateBase64EscapedLenInternal(size_t input_len, bool do_padding) { |
27 | | // Base64 encodes three bytes of input at a time. If the input is not |
28 | | // divisible by three, we pad as appropriate. |
29 | | // |
30 | | // Base64 encodes each three bytes of input into four bytes of output. |
31 | 0 | constexpr size_t kMaxSize = (std::numeric_limits<size_t>::max() - 1) / 4 * 3; |
32 | 0 | ABSL_INTERNAL_CHECK(input_len <= kMaxSize, |
33 | 0 | "CalculateBase64EscapedLenInternal() overflow"); |
34 | 0 | size_t len = (input_len / 3) * 4; |
35 | | |
36 | | // Since all base 64 input is an integral number of octets, only the following |
37 | | // cases can arise: |
38 | 0 | if (input_len % 3 == 0) { |
39 | | // (from https://tools.ietf.org/html/rfc3548) |
40 | | // (1) the final quantum of encoding input is an integral multiple of 24 |
41 | | // bits; here, the final unit of encoded output will be an integral |
42 | | // multiple of 4 characters with no "=" padding, |
43 | 0 | } else if (input_len % 3 == 1) { |
44 | | // (from https://tools.ietf.org/html/rfc3548) |
45 | | // (2) the final quantum of encoding input is exactly 8 bits; here, the |
46 | | // final unit of encoded output will be two characters followed by two |
47 | | // "=" padding characters, or |
48 | 0 | len += 2; |
49 | 0 | if (do_padding) { |
50 | 0 | len += 2; |
51 | 0 | } |
52 | 0 | } else { // (input_len % 3 == 2) |
53 | | // (from https://tools.ietf.org/html/rfc3548) |
54 | | // (3) the final quantum of encoding input is exactly 16 bits; here, the |
55 | | // final unit of encoded output will be three characters followed by one |
56 | | // "=" padding character. |
57 | 0 | len += 3; |
58 | 0 | if (do_padding) { |
59 | 0 | len += 1; |
60 | 0 | } |
61 | 0 | } |
62 | |
|
63 | 0 | return len; |
64 | 0 | } |
65 | | |
66 | | } // namespace strings_internal |
67 | | ABSL_NAMESPACE_END |
68 | | } // namespace absl |