You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

escaping.h 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef ABSL_STRINGS_INTERNAL_ESCAPING_H_
  15. #define ABSL_STRINGS_INTERNAL_ESCAPING_H_
  16. #include <cassert>
  17. #include "absl/strings/internal/resize_uninitialized.h"
  18. namespace absl
  19. {
  20. ABSL_NAMESPACE_BEGIN
  21. namespace strings_internal
  22. {
  23. ABSL_CONST_INIT extern const char kBase64Chars[];
  24. // Calculates how long a string will be when it is base64 encoded given its
  25. // length and whether or not the result should be padded.
  26. size_t CalculateBase64EscapedLenInternal(size_t input_len, bool do_padding);
  27. // Base64-encodes `src` using the alphabet provided in `base64` and writes the
  28. // result to `dest`. If `do_padding` is true, `dest` is padded with '=' chars
  29. // until its length is a multiple of 3. Returns the length of `dest`.
  30. size_t Base64EscapeInternal(const unsigned char* src, size_t szsrc, char* dest, size_t szdest, const char* base64, bool do_padding);
  31. // Base64-encodes `src` using the alphabet provided in `base64` and writes the
  32. // result to `dest`. If `do_padding` is true, `dest` is padded with '=' chars
  33. // until its length is a multiple of 3.
  34. template<typename String>
  35. void Base64EscapeInternal(const unsigned char* src, size_t szsrc, String* dest, bool do_padding, const char* base64_chars)
  36. {
  37. const size_t calc_escaped_size =
  38. CalculateBase64EscapedLenInternal(szsrc, do_padding);
  39. STLStringResizeUninitialized(dest, calc_escaped_size);
  40. const size_t escaped_len = Base64EscapeInternal(
  41. src, szsrc, &(*dest)[0], dest->size(), base64_chars, do_padding
  42. );
  43. assert(calc_escaped_size == escaped_len);
  44. dest->erase(escaped_len);
  45. }
  46. } // namespace strings_internal
  47. ABSL_NAMESPACE_END
  48. } // namespace absl
  49. #endif // ABSL_STRINGS_INTERNAL_ESCAPING_H_