1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 **************************************************************************/
19 #ifndef REALM_UTIL_BASE64_HPP
20 #define REALM_UTIL_BASE64_HPP
23 #include <realm/string_data.hpp>
24 #include <realm/util/optional.hpp>
30 /// base64_encode() encodes the bnary data in \param in_buffer of size \param in_buffer_size.
31 /// The encoded data is placed in \param out_buffer. The size of \param \out_buffer is passed in
32 /// \param out_buffer_size. The output buffer \param out_buffer must be
33 /// large enough to hold the base64 encoded data. The size can be obtained from the function
34 /// base64_encoded_size. \param out_buffer_size is only used to assert that the output buffer is
36 size_t base64_encode(const char *in_buffer, size_t in_buffer_size, char* out_buffer, size_t out_buffer_size) noexcept;
38 /// base64_encoded_size() returns the exact size of the base64 encoded
39 /// data as a function of the size of the input data.
40 inline size_t base64_encoded_size(size_t in_buffer_size) noexcept
42 return 4 * ((in_buffer_size + 2) / 3);
46 /// Decode base64-encoded string in input, and places the result in out_buffer.
47 /// The length of the out_buffer must be at least 3 * input.size() / 4.
49 /// The input must be padded base64 (i.e. the number of non-whitespace
50 /// characters in the input must be a multiple of 4). Whitespace (spaces, tabs,
51 /// newlines) is ignored.
53 /// The algorithm stops when the first character not in the base64 character
54 /// set is encountered, or when the end of the input is reached.
56 /// \returns the number of successfully decoded bytes written to out_buffer, or
57 /// none if the whole input was not valid base64.
58 Optional<size_t> base64_decode(StringData input, char* out_buffer, size_t out_buffer_len) noexcept;
60 /// Return an upper bound on the decoded size of a Base64-encoded data
61 /// stream of length \a base64_size. The returned value is suitable for
62 /// allocation of buffers containing decoded data.
63 inline size_t base64_decoded_size(size_t base64_size) noexcept
65 return (base64_size * 3 + 3) / 4;
70 /// base64_decode_to_vector() is a convenience function that decodes \param
71 /// encoded and returns the result in a std::vector<char> with the correct size.
72 /// This function returns none if the input is invalid.
73 Optional<std::vector<char>> base64_decode_to_vector(StringData encoded);
78 #endif // REALM_UTIL_BASE64_HPP