Skip to content

Commit f322436

Browse files
Add base64 encoding and decoding functions with tests (ros2#533)
Signed-off-by: Barry Xu <barry.xu@sony.com> Signed-off-by: Alejandro Hernández Cordero <ahcorde@gmail.com> Co-authored-by: Alejandro Hernández Cordero <ahcorde@gmail.com>
1 parent fa67866 commit f322436

4 files changed

Lines changed: 877 additions & 0 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ endif()
4747
set(rcutils_sources
4848
src/allocator.c
4949
src/array_list.c
50+
src/base64.c
5051
src/char_array.c
5152
src/cmdline_parser.c
5253
src/env.c
@@ -539,6 +540,13 @@ if(BUILD_TESTING)
539540
if(TARGET benchmark_err_handle)
540541
target_link_libraries(benchmark_err_handle ${PROJECT_NAME})
541542
endif()
543+
544+
ament_add_gtest(test_base64
545+
test/test_base64.cpp
546+
)
547+
if(TARGET test_base64)
548+
target_link_libraries(test_base64 ${PROJECT_NAME})
549+
endif()
542550
endif()
543551

544552
# Export old-style CMake variables

‎include/rcutils/base64.h‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2026 Sony Group Corporation.
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 RCUTILS__BASE64_H_
16+
#define RCUTILS__BASE64_H_
17+
18+
#ifdef __cplusplus
19+
extern "C"
20+
{
21+
#endif
22+
23+
#include "rcutils/allocator.h"
24+
#include "rcutils/visibility_control.h"
25+
#include "rcutils/types.h"
26+
27+
#define RCUTILS_BASE64_ENCODED_MAX_LENGTH (64 * 1024 * 1024) // 64 MiB
28+
29+
/**
30+
* \brief Decode a base64-encoded string into a byte array.
31+
*
32+
* This function converts a base64-encoded string into its original binary representation.
33+
* Base64 encoding uses 4 ASCII characters to represent 3 bytes of data.
34+
*
35+
* \param base64_str The null-terminated base64 string to decode.
36+
* \param byte_array Pointer to rcutils_uint8_array_t to store the decoded byte array (output).
37+
* The array must be zero-initialized by rcutils_get_zero_initialized_uint8_array
38+
* before calling this function.
39+
* \param allocator The allocator to use for memory allocation.
40+
* \return RCUTILS_RET_OK on success.
41+
* \return RCUTILS_RET_INVALID_ARGUMENT if base64_str is NULL, or
42+
* if byte_array is NULL, or
43+
* if allocator is NULL or invalid, or
44+
* if byte_array is not zero-initialized.
45+
* \return RCUTILS_RET_ERROR if base64 string length exceeds RCUTILS_BASE64_ENCODED_MAX_LENGTH, or
46+
* if base64 string length is not a multiple of 4, or
47+
* if base64 string contains invalid characters, or
48+
* if base64 string has invalid padding.
49+
* \return RCUTILS_RET_BAD_ALLOC if memory allocation fails.
50+
*/
51+
RCUTILS_PUBLIC
52+
RCUTILS_WARN_UNUSED
53+
rcutils_ret_t rcutils_decode_base64(
54+
const char * base64_str,
55+
rcutils_uint8_array_t * byte_array,
56+
const rcutils_allocator_t * allocator);
57+
58+
/**
59+
* \brief Encode a byte array into a base64-encoded string.
60+
*
61+
* This function converts a binary byte array into its base64-encoded representation.
62+
* Base64 encoding uses 4 ASCII characters to represent 3 bytes of data.
63+
*
64+
* The output string should be freed by the caller using the provided allocator.
65+
*
66+
* \param byte_array The rcutils_uint8_array_t structure containing the byte array to encode.
67+
* \param base64_str Pointer to store the allocated base64 string (output).
68+
* \param allocator The allocator to use for memory allocation.
69+
* \return RCUTILS_RET_OK on success.
70+
* \return RCUTILS_RET_INVALID_ARGUMENT if byte_array is NULL, or
71+
* if byte_array->buffer is NULL, or
72+
* if byte_array->buffer_length is zero, or
73+
* if base64_str is NULL, or
74+
* if allocator is NULL or invalid.
75+
* \return RCUTILS_RET_ERROR if byte_array length exceeds limit for base64 encoding.
76+
* \return RCUTILS_RET_BAD_ALLOC if memory allocation fails.
77+
*/
78+
RCUTILS_PUBLIC
79+
RCUTILS_WARN_UNUSED
80+
rcutils_ret_t rcutils_encode_base64(
81+
const rcutils_uint8_array_t * byte_array,
82+
char ** base64_str,
83+
const rcutils_allocator_t * allocator);
84+
85+
#ifdef __cplusplus
86+
}
87+
#endif
88+
#endif // RCUTILS__BASE64_H_

0 commit comments

Comments
 (0)