Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // 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, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_tokenizer/base64.h" |
| 16 | |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 17 | namespace pw::tokenizer { |
| 18 | |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 19 | extern "C" size_t pw_tokenizer_PrefixedBase64Encode( |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 20 | const void* binary_message, |
| 21 | size_t binary_size_bytes, |
| 22 | void* output_buffer, |
| 23 | size_t output_buffer_size_bytes) { |
Wyatt Hepler | cdafbb4 | 2020-10-05 11:51:05 -0700 | [diff] [blame] | 24 | char* output = static_cast<char*>(output_buffer); |
Wyatt Hepler | 9eb1695 | 2020-10-09 17:36:27 -0700 | [diff] [blame] | 25 | const size_t encoded_size = Base64EncodedBufferSize(binary_size_bytes); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 26 | |
Wyatt Hepler | 9eb1695 | 2020-10-09 17:36:27 -0700 | [diff] [blame] | 27 | if (output_buffer_size_bytes < encoded_size) { |
Wyatt Hepler | cdafbb4 | 2020-10-05 11:51:05 -0700 | [diff] [blame] | 28 | if (output_buffer_size_bytes > 0u) { |
| 29 | output[0] = '\0'; |
| 30 | } |
| 31 | |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 32 | return 0; |
| 33 | } |
| 34 | |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 35 | output[0] = kBase64Prefix; |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 36 | base64::Encode(std::span(static_cast<const std::byte*>(binary_message), |
| 37 | binary_size_bytes), |
| 38 | &output[1]); |
Wyatt Hepler | 9eb1695 | 2020-10-09 17:36:27 -0700 | [diff] [blame] | 39 | output[encoded_size - 1] = '\0'; |
| 40 | return encoded_size - sizeof('\0'); // exclude the null terminator |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Wyatt Hepler | 7a5e4d6 | 2020-08-31 08:39:16 -0700 | [diff] [blame] | 43 | extern "C" size_t pw_tokenizer_PrefixedBase64Decode(const void* base64_message, |
| 44 | size_t base64_size_bytes, |
| 45 | void* output_buffer, |
| 46 | size_t output_buffer_size) { |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 47 | const char* base64 = static_cast<const char*>(base64_message); |
| 48 | |
| 49 | if (base64_size_bytes == 0 || base64[0] != kBase64Prefix) { |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | return base64::Decode( |
| 54 | std::string_view(&base64[1], base64_size_bytes - 1), |
Wyatt Hepler | e2cbadf | 2020-06-22 11:21:45 -0700 | [diff] [blame] | 55 | std::span(static_cast<std::byte*>(output_buffer), output_buffer_size)); |
Wyatt Hepler | 3c2e952 | 2020-01-09 16:08:54 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | } // namespace pw::tokenizer |