/src/abseil-cpp/absl/strings/internal/escaping.h
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 | | #ifndef ABSL_STRINGS_INTERNAL_ESCAPING_H_ |
16 | | #define ABSL_STRINGS_INTERNAL_ESCAPING_H_ |
17 | | |
18 | | #include <cassert> |
19 | | |
20 | | #include "absl/strings/resize_and_overwrite.h" |
21 | | |
22 | | namespace absl { |
23 | | ABSL_NAMESPACE_BEGIN |
24 | | namespace strings_internal { |
25 | | |
26 | | ABSL_CONST_INIT extern const char kBase64Chars[]; |
27 | | ABSL_CONST_INIT extern const char kWebSafeBase64Chars[]; |
28 | | |
29 | | // Calculates the length of a Base64 encoding (RFC 4648) of a string of length |
30 | | // `input_len`, with or without padding per `do_padding`. Note that 'web-safe' |
31 | | // encoding (section 5 of the RFC) does not change this length. |
32 | | size_t CalculateBase64EscapedLenInternal(size_t input_len, bool do_padding); |
33 | | |
34 | | // Base64-encodes `src` using the alphabet provided in `base64` (which |
35 | | // determines whether to do web-safe encoding or not) and writes the result to |
36 | | // `dest`. If `do_padding` is true, `dest` is padded with '=' chars until its |
37 | | // length is a multiple of 3. Returns the length of `dest`. |
38 | | size_t Base64EscapeInternal(const unsigned char* src, size_t szsrc, char* dest, |
39 | | size_t szdest, const char* base64, bool do_padding); |
40 | | template <typename String> |
41 | | void Base64EscapeInternal(const unsigned char* src, size_t szsrc, String* dest, |
42 | 0 | bool do_padding, const char* base64_chars) { |
43 | 0 | const size_t calc_escaped_size = |
44 | 0 | CalculateBase64EscapedLenInternal(szsrc, do_padding); |
45 | 0 | StringResizeAndOverwrite( |
46 | 0 | *dest, calc_escaped_size, |
47 | 0 | [src, szsrc, base64_chars, do_padding](char* buf, size_t buf_size) { |
48 | 0 | const size_t escaped_len = Base64EscapeInternal( |
49 | 0 | src, szsrc, buf, buf_size, base64_chars, do_padding); |
50 | 0 | assert(escaped_len == buf_size); |
51 | 0 | return escaped_len; |
52 | 0 | }); |
53 | 0 | } |
54 | | |
55 | | } // namespace strings_internal |
56 | | ABSL_NAMESPACE_END |
57 | | } // namespace absl |
58 | | |
59 | | #endif // ABSL_STRINGS_INTERNAL_ESCAPING_H_ |