3 * Copyright (c) 2021 Project CHIP Authors
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.
20 #include <core/CHIPError.h>
30 // Use uppercase A-F if set otherwise, lowercase a-f
31 kUppercase = (1u << 0),
32 // Null-terminate buffer
33 kNullTerminate = (1u << 1),
34 // Both use uppercase and null-termination.
35 // Separately stated to avoid casts for common case.
36 kUppercaseAndNullTerminate = ((1u << 0) | (1u << 1))
40 * Encode a buffer of bytes into hexadecimal, with or without null-termination
41 * and using either lowercase or uppercase hex.
43 * Default is lowercase output, not null-terminated.
45 * If `flags` has `HexFlags::kNullTerminate` set, treat `dest_hex` as a
46 * null-terminated string buffer. The function returns CHIP_ERROR_BUFFER_TOO_SMALL
47 * if `dest_size_max` can't fit the entire encoded buffer, and the
48 * null-terminator if enabled. This function will never output truncated data.
49 * The result either fits and is written, or does not fit and nothing is written
52 * If `src_bytes` and `dest_hex` overlap, the results may be incorrect, depending
53 * on overlap, but only the core validity checks are done and it's possible to
54 * get CHIP_NO_ERROR with erroneous output.
56 * On success, number of bytes written to destination is always:
57 * output_size = (src_size * 2) + ((flags & HexFlags::kNullTerminate) ? 1 : 0);
59 * @param src_bytes Pointer to non-null buffer to convert
60 * @param src_size Number of bytes to convert from src_bytes
61 * @param [out] dest_hex Destination buffer to receive hex encoding
62 * @param dest_size_max Maximum buffer size for the hex encoded `dest_hex` buffer
63 * including null-terminator if needed.
64 * @param flags Flags from `HexFlags` for formatting options
66 * @return CHIP_ERROR_BUFFER_TOO_SMALL on dest_max_size too small to fit output
67 * @return CHIP_ERROR_INVALID_ARGUMENT if either src_bytes or dest_hex is nullptr
68 * @return CHIP_NO_ERROR on success
71 CHIP_ERROR BytesToHex(const uint8_t * src_bytes, size_t src_size, char * dest_hex, size_t dest_size_max, HexFlags flags);
73 // Alias for Uppercase option, no null-termination
74 inline CHIP_ERROR BytesToUppercaseHexBuffer(const uint8_t * src_bytes, size_t src_size, char * dest_hex_buf, size_t dest_size_max)
76 return BytesToHex(src_bytes, src_size, dest_hex_buf, dest_size_max, HexFlags::kUppercase);
79 // Alias for Lowercase option, no null-termination
80 inline CHIP_ERROR BytesToLowercaseHexBuffer(const uint8_t * src_bytes, size_t src_size, char * dest_hex_buf, size_t dest_size_max)
82 return BytesToHex(src_bytes, src_size, dest_hex_buf, dest_size_max, HexFlags::kNone);
85 // Alias for Uppercase option, with null-termination
86 inline CHIP_ERROR BytesToUppercaseHexString(const uint8_t * src_bytes, size_t src_size, char * dest_hex_str, size_t dest_size_max)
88 return BytesToHex(src_bytes, src_size, dest_hex_str, dest_size_max, HexFlags::kUppercaseAndNullTerminate);
91 // Alias for Lowercase option, with null-termination
92 inline CHIP_ERROR BytesToLowercaseHexString(const uint8_t * src_bytes, size_t src_size, char * dest_hex_str, size_t dest_size_max)
94 return BytesToHex(src_bytes, src_size, dest_hex_str, dest_size_max, HexFlags::kNullTerminate);
97 } // namespace Encoding