base64.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*************************************************************************
  2. *
  3. * Copyright 2016 Realm Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. **************************************************************************/
  18. #ifndef REALM_UTIL_BASE64_HPP
  19. #define REALM_UTIL_BASE64_HPP
  20. #include <vector>
  21. #include <realm/string_data.hpp>
  22. #include <realm/util/optional.hpp>
  23. namespace realm {
  24. namespace util {
  25. /// base64_encode() encodes the bnary data in \param in_buffer of size \param in_buffer_size .
  26. /// The encoded data is placed in \param out_buffer . The size of \param \out_buffer is passed in
  27. /// \param out_buffer_size . The output buffer out_buffer must be
  28. /// large enough to hold the base64 encoded data. The size can be obtained from the function
  29. /// base64_encoded_size. out_buffer_size is only used to assert that the output buffer is
  30. /// large enough.
  31. size_t base64_encode(const char *in_buffer, size_t in_buffer_size, char* out_buffer, size_t out_buffer_size) noexcept;
  32. /// base64_encoded_size() returns the exact size of the base64 encoded
  33. /// data as a function of the size of the input data.
  34. inline size_t base64_encoded_size(size_t in_buffer_size) noexcept
  35. {
  36. return 4 * ((in_buffer_size + 2) / 3);
  37. }
  38. /// Decode base64-encoded string in input, and places the result in out_buffer.
  39. /// The length of the out_buffer must be at least 3 * input.size() / 4.
  40. ///
  41. /// The input must be padded base64 (i.e. the number of non-whitespace
  42. /// characters in the input must be a multiple of 4). Whitespace (spaces, tabs,
  43. /// newlines) is ignored.
  44. ///
  45. /// The algorithm stops when the first character not in the base64 character
  46. /// set is encountered, or when the end of the input is reached.
  47. ///
  48. /// \returns the number of successfully decoded bytes written to out_buffer, or
  49. /// none if the whole input was not valid base64.
  50. Optional<size_t> base64_decode(StringData input, char* out_buffer, size_t out_buffer_len) noexcept;
  51. /// Return an upper bound on the decoded size of a Base64-encoded data
  52. /// stream of length \a base64_size. The returned value is suitable for
  53. /// allocation of buffers containing decoded data.
  54. inline size_t base64_decoded_size(size_t base64_size) noexcept
  55. {
  56. return (base64_size * 3 + 3) / 4;
  57. }
  58. /// base64_decode_to_vector() is a convenience function that decodes \param
  59. /// encoded and returns the result in a std::vector<char> with the correct size.
  60. /// This function returns none if the input is invalid.
  61. Optional<std::vector<char>> base64_decode_to_vector(StringData encoded);
  62. } // namespace util
  63. } // namespace realm
  64. #endif // REALM_UTIL_BASE64_HPP