b62617b5250572ee23118634358656f4f87d832f
[platform/upstream/connectedhomeip.git] / src / lib / support / BytesToHex.h
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #pragma once
19
20 #include <core/CHIPError.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23
24 namespace chip {
25 namespace Encoding {
26
27 enum HexFlags : int
28 {
29     kNone = 0u,
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))
37 };
38
39 /**
40  * Encode a buffer of bytes into hexadecimal, with or without null-termination
41  * and using either lowercase or uppercase hex.
42  *
43  * Default is lowercase output, not null-terminated.
44  *
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
50  * to `dest_hex`.
51  *
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.
55  *
56  * On success, number of bytes written to destination is always:
57  *   output_size = (src_size * 2) + ((flags & HexFlags::kNullTerminate) ? 1 : 0);
58  *
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
65  *
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
69  */
70
71 CHIP_ERROR BytesToHex(const uint8_t * src_bytes, size_t src_size, char * dest_hex, size_t dest_size_max, HexFlags flags);
72
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)
75 {
76     return BytesToHex(src_bytes, src_size, dest_hex_buf, dest_size_max, HexFlags::kUppercase);
77 }
78
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)
81 {
82     return BytesToHex(src_bytes, src_size, dest_hex_buf, dest_size_max, HexFlags::kNone);
83 }
84
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)
87 {
88     return BytesToHex(src_bytes, src_size, dest_hex_str, dest_size_max, HexFlags::kUppercaseAndNullTerminate);
89 }
90
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)
93 {
94     return BytesToHex(src_bytes, src_size, dest_hex_str, dest_size_max, HexFlags::kNullTerminate);
95 }
96
97 } // namespace Encoding
98 } // namespace chip