| 1 | /* Copyright 2015 OpenMarket Ltd |
| 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 | * http://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 | */ |
| 15 | #ifndef OLM_BASE64_HH_ |
| 16 | #define OLM_BASE64_HH_ |
| 17 | |
| 18 | #include <cstddef> |
| 19 | #include <cstdint> |
| 20 | |
| 21 | // Note: exports in this file are only for unit tests. Nobody else should be |
| 22 | // using this externally |
| 23 | #include "olm/olm_export.h" |
| 24 | |
| 25 | namespace olm { |
| 26 | |
| 27 | /** |
| 28 | * The number of bytes of unpadded base64 needed to encode a length of input. |
| 29 | */ |
| 30 | OLM_EXPORT std::size_t encode_base64_length( |
| 31 | std::size_t input_length |
| 32 | ); |
| 33 | |
| 34 | /** |
| 35 | * Encode the raw input as unpadded base64. |
| 36 | * Writes encode_base64_length(input_length) bytes to the output buffer. |
| 37 | * The input can overlap with the last three quarters of the output buffer. |
| 38 | * That is, the input pointer may be output + output_length - input_length. |
| 39 | */ |
| 40 | OLM_EXPORT std::uint8_t * encode_base64( |
| 41 | std::uint8_t const * input, std::size_t input_length, |
| 42 | std::uint8_t * output |
| 43 | ); |
| 44 | |
| 45 | /** |
| 46 | * The number of bytes of raw data a length of unpadded base64 will encode to. |
| 47 | * Returns std::size_t(-1) if the length is not a valid length for base64. |
| 48 | */ |
| 49 | OLM_EXPORT std::size_t decode_base64_length( |
| 50 | std::size_t input_length |
| 51 | ); |
| 52 | |
| 53 | /** |
| 54 | * Decodes the unpadded base64 input to raw bytes. |
| 55 | * Writes decode_base64_length(input_length) bytes to the output buffer. |
| 56 | * The output can overlap with the first three quarters of the input buffer. |
| 57 | * That is, the input pointers and output pointer may be the same. |
| 58 | * |
| 59 | * Returns the number of bytes of raw data the base64 input decoded to. If the |
| 60 | * input length supplied is not a valid length for base64, returns |
| 61 | * std::size_t(-1) and does not decode. |
| 62 | */ |
| 63 | OLM_EXPORT std::size_t decode_base64( |
| 64 | std::uint8_t const * input, std::size_t input_length, |
| 65 | std::uint8_t * output |
| 66 | ); |
| 67 | |
| 68 | } // namespace olm |
| 69 | |
| 70 | |
| 71 | #endif /* OLM_BASE64_HH_ */ |
| 72 | |