Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:25

0001 // Copyright 2020 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_STRINGS_INTERNAL_ESCAPING_H_
0016 #define ABSL_STRINGS_INTERNAL_ESCAPING_H_
0017 
0018 #include <cassert>
0019 
0020 #include "absl/strings/internal/resize_uninitialized.h"
0021 
0022 namespace absl {
0023 ABSL_NAMESPACE_BEGIN
0024 namespace strings_internal {
0025 
0026 ABSL_CONST_INIT extern const char kBase64Chars[];
0027 ABSL_CONST_INIT extern const char kWebSafeBase64Chars[];
0028 
0029 // Calculates the length of a Base64 encoding (RFC 4648) of a string of length
0030 // `input_len`, with or without padding per `do_padding`. Note that 'web-safe'
0031 // encoding (section 5 of the RFC) does not change this length.
0032 size_t CalculateBase64EscapedLenInternal(size_t input_len, bool do_padding);
0033 
0034 // Base64-encodes `src` using the alphabet provided in `base64` (which
0035 // determines whether to do web-safe encoding or not) and writes the result to
0036 // `dest`. If `do_padding` is true, `dest` is padded with '=' chars until its
0037 // length is a multiple of 3. Returns the length of `dest`.
0038 size_t Base64EscapeInternal(const unsigned char* src, size_t szsrc, char* dest,
0039                             size_t szdest, const char* base64, bool do_padding);
0040 template <typename String>
0041 void Base64EscapeInternal(const unsigned char* src, size_t szsrc, String* dest,
0042                           bool do_padding, const char* base64_chars) {
0043   const size_t calc_escaped_size =
0044       CalculateBase64EscapedLenInternal(szsrc, do_padding);
0045   STLStringResizeUninitialized(dest, calc_escaped_size);
0046 
0047   const size_t escaped_len = Base64EscapeInternal(
0048       src, szsrc, &(*dest)[0], dest->size(), base64_chars, do_padding);
0049   assert(calc_escaped_size == escaped_len);
0050   dest->erase(escaped_len);
0051 }
0052 
0053 }  // namespace strings_internal
0054 ABSL_NAMESPACE_END
0055 }  // namespace absl
0056 
0057 #endif  // ABSL_STRINGS_INTERNAL_ESCAPING_H_