xref: /aosp_15_r20/external/abseil-cpp/absl/strings/internal/escaping.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2020 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_STRINGS_INTERNAL_ESCAPING_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_STRINGS_INTERNAL_ESCAPING_H_
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker #include <cassert>
19*9356374aSAndroid Build Coastguard Worker 
20*9356374aSAndroid Build Coastguard Worker #include "absl/strings/internal/resize_uninitialized.h"
21*9356374aSAndroid Build Coastguard Worker 
22*9356374aSAndroid Build Coastguard Worker namespace absl {
23*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
24*9356374aSAndroid Build Coastguard Worker namespace strings_internal {
25*9356374aSAndroid Build Coastguard Worker 
26*9356374aSAndroid Build Coastguard Worker ABSL_CONST_INIT extern const char kBase64Chars[];
27*9356374aSAndroid Build Coastguard Worker ABSL_CONST_INIT extern const char kWebSafeBase64Chars[];
28*9356374aSAndroid Build Coastguard Worker 
29*9356374aSAndroid Build Coastguard Worker // Calculates the length of a Base64 encoding (RFC 4648) of a string of length
30*9356374aSAndroid Build Coastguard Worker // `input_len`, with or without padding per `do_padding`. Note that 'web-safe'
31*9356374aSAndroid Build Coastguard Worker // encoding (section 5 of the RFC) does not change this length.
32*9356374aSAndroid Build Coastguard Worker size_t CalculateBase64EscapedLenInternal(size_t input_len, bool do_padding);
33*9356374aSAndroid Build Coastguard Worker 
34*9356374aSAndroid Build Coastguard Worker // Base64-encodes `src` using the alphabet provided in `base64` (which
35*9356374aSAndroid Build Coastguard Worker // determines whether to do web-safe encoding or not) and writes the result to
36*9356374aSAndroid Build Coastguard Worker // `dest`. If `do_padding` is true, `dest` is padded with '=' chars until its
37*9356374aSAndroid Build Coastguard Worker // length is a multiple of 3. Returns the length of `dest`.
38*9356374aSAndroid Build Coastguard Worker size_t Base64EscapeInternal(const unsigned char* src, size_t szsrc, char* dest,
39*9356374aSAndroid Build Coastguard Worker                             size_t szdest, const char* base64, bool do_padding);
40*9356374aSAndroid Build Coastguard Worker template <typename String>
Base64EscapeInternal(const unsigned char * src,size_t szsrc,String * dest,bool do_padding,const char * base64_chars)41*9356374aSAndroid Build Coastguard Worker void Base64EscapeInternal(const unsigned char* src, size_t szsrc, String* dest,
42*9356374aSAndroid Build Coastguard Worker                           bool do_padding, const char* base64_chars) {
43*9356374aSAndroid Build Coastguard Worker   const size_t calc_escaped_size =
44*9356374aSAndroid Build Coastguard Worker       CalculateBase64EscapedLenInternal(szsrc, do_padding);
45*9356374aSAndroid Build Coastguard Worker   STLStringResizeUninitialized(dest, calc_escaped_size);
46*9356374aSAndroid Build Coastguard Worker 
47*9356374aSAndroid Build Coastguard Worker   const size_t escaped_len = Base64EscapeInternal(
48*9356374aSAndroid Build Coastguard Worker       src, szsrc, &(*dest)[0], dest->size(), base64_chars, do_padding);
49*9356374aSAndroid Build Coastguard Worker   assert(calc_escaped_size == escaped_len);
50*9356374aSAndroid Build Coastguard Worker   dest->erase(escaped_len);
51*9356374aSAndroid Build Coastguard Worker }
52*9356374aSAndroid Build Coastguard Worker 
53*9356374aSAndroid Build Coastguard Worker }  // namespace strings_internal
54*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
55*9356374aSAndroid Build Coastguard Worker }  // namespace absl
56*9356374aSAndroid Build Coastguard Worker 
57*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_STRINGS_INTERNAL_ESCAPING_H_
58