Patrick Williams | 691668f | 2023-11-01 08:19:10 -0500 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 2 | #ifndef PLDM_MSGBUF_H |
| 3 | #define PLDM_MSGBUF_H |
| 4 | |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 5 | #include "compiler.h" |
| 6 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 7 | /* |
| 8 | * Historically, many of the structs exposed in libpldm's public headers are |
| 9 | * defined with __attribute__((packed)). This is unfortunate: it gives the |
| 10 | * impression that a wire-format buffer can be cast to the message type to make |
| 11 | * the message's fields easily accessible. As it turns out, that's not |
| 12 | * that's valid for several reasons: |
| 13 | * |
| 14 | * 1. Casting the wire-format buffer to a struct of the message type doesn't |
| 15 | * abstract the endianness of message field values |
| 16 | * |
| 17 | * 2. Some messages contain packed tagged union fields which cannot be properly |
| 18 | * described in a C struct. |
| 19 | * |
| 20 | * The msgbuf APIs exist to assist with (un)packing the wire-format in a way |
| 21 | * that is type-safe, spatially memory-safe, endian-safe, performant, and |
| 22 | * free of undefined-behaviour. Message structs that are added to the public |
| 23 | * library API should no-longer be marked __attribute__((packed)), and the |
| 24 | * implementation of their encode and decode functions must exploit the msgbuf |
| 25 | * API. |
| 26 | * |
| 27 | * However, we would like to allow implementation of codec functions in terms of |
| 28 | * msgbuf APIs even if they're decoding a message into a (historically) packed |
| 29 | * struct. Some of the complexity that follows is a consequence of the packed/ |
| 30 | * unpacked conflict. |
| 31 | */ |
| 32 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 33 | #ifdef __cplusplus |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 34 | /* |
| 35 | * Fix up C11's _Static_assert() vs C++'s static_assert(). |
| 36 | * |
| 37 | * Can we please have nice things for once. |
| 38 | */ |
| 39 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
| 40 | #define _Static_assert(...) static_assert(__VA_ARGS__) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 41 | extern "C" { |
| 42 | #endif |
| 43 | |
Andrew Jeffery | b0c1d20 | 2023-11-07 22:08:44 +1030 | [diff] [blame] | 44 | #include <libpldm/base.h> |
| 45 | #include <libpldm/pldm_types.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 46 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 47 | #include "compiler.h" |
| 48 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 49 | #include <assert.h> |
| 50 | #include <endian.h> |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 51 | #include <errno.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 52 | #include <limits.h> |
| 53 | #include <stdbool.h> |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 54 | #include <stdint.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 55 | #include <string.h> |
| 56 | #include <sys/types.h> |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 57 | #include <uchar.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 58 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 59 | /* |
| 60 | * We can't use static_assert() outside of some other C construct. Deal |
| 61 | * with high-level global assertions by burying them in an unused struct |
| 62 | * declaration, that has a sole member for compliance with the requirement that |
| 63 | * types must have a size. |
| 64 | */ |
| 65 | static struct { |
| 66 | static_assert( |
| 67 | INTMAX_MAX != SIZE_MAX, |
| 68 | "Extraction and insertion value comparisons may be broken"); |
| 69 | static_assert(INTMAX_MIN + INTMAX_MAX <= 0, |
| 70 | "Extraction and insertion arithmetic may be broken"); |
| 71 | int compliance; |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 72 | } build_assertions LIBPLDM_CC_UNUSED; |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 73 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 74 | struct pldm_msgbuf { |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 75 | uint8_t *cursor; |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 76 | intmax_t remaining; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 77 | }; |
| 78 | |
Andrew Jeffery | d861a68 | 2024-06-03 21:43:09 +0930 | [diff] [blame] | 79 | /** |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 80 | * @brief Initialize pldm buf struct for buf extractor |
| 81 | * |
| 82 | * @param[out] ctx - pldm_msgbuf context for extractor |
| 83 | * @param[in] minsize - The minimum required length of buffer `buf` |
| 84 | * @param[in] buf - buffer to be extracted |
| 85 | * @param[in] len - size of buffer |
| 86 | * |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 87 | * @return 0 on success, otherwise an error code appropriate for the current |
| 88 | * personality. |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 89 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 90 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 91 | LIBPLDM_CC_ALWAYS_INLINE |
| 92 | LIBPLDM_CC_WARN_UNUSED_RESULT |
| 93 | int |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 94 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 95 | pldm_msgbuf_init_errno(struct pldm_msgbuf *ctx, size_t minsize, const void *buf, |
| 96 | size_t len) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 97 | { |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 98 | if ((minsize > len)) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 99 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 100 | } |
| 101 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 102 | #if INTMAX_MAX < SIZE_MAX |
| 103 | if (len > INTMAX_MAX) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 104 | return -EOVERFLOW; |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 105 | } |
| 106 | #endif |
| 107 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 108 | if (UINTPTR_MAX - (uintptr_t)buf < len) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 109 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | ctx->cursor = (uint8_t *)buf; |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 113 | ctx->remaining = (intmax_t)len; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 114 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | /** |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 119 | * @brief Validate buffer overflow state |
| 120 | * |
| 121 | * @param[in] ctx - pldm_msgbuf context for extractor |
| 122 | * |
| 123 | * @return PLDM_SUCCESS if there are zero or more bytes of data that remain |
| 124 | * unread from the buffer. Otherwise, PLDM_ERROR_INVALID_LENGTH indicates that a |
| 125 | * prior accesses would have occurred beyond the bounds of the buffer, and |
| 126 | * PLDM_ERROR_INVALID_DATA indicates that the provided context was not a valid |
| 127 | * pointer. |
| 128 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 129 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 130 | LIBPLDM_CC_ALWAYS_INLINE |
| 131 | LIBPLDM_CC_WARN_UNUSED_RESULT |
| 132 | int pldm_msgbuf_validate(struct pldm_msgbuf *ctx) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 133 | { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 134 | if (ctx->remaining < 0) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 135 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 136 | } |
| 137 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 138 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 142 | * @brief Test whether a message buffer has been exactly consumed |
| 143 | * |
| 144 | * @param[in] ctx - pldm_msgbuf context for extractor |
| 145 | * |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 146 | * @return 0 iff there are zero bytes of data that remain unread from the buffer |
| 147 | * and no overflow has occurred. Otherwise, -EBADMSG. |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 148 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 149 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 150 | LIBPLDM_CC_ALWAYS_INLINE |
| 151 | LIBPLDM_CC_WARN_UNUSED_RESULT |
| 152 | int pldm_msgbuf_consumed(struct pldm_msgbuf *ctx) |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 153 | { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 154 | if (ctx->remaining != 0) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 155 | return -EBADMSG; |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 156 | } |
| 157 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 158 | return 0; |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 162 | * @brief Complete the pldm_msgbuf instance |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 163 | * |
| 164 | * @param[in] ctx - pldm_msgbuf context for extractor |
| 165 | * |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 166 | * @return 0 if all buffer accesses were in-bounds, -EOVERFLOW otherwise. |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 167 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 168 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 169 | LIBPLDM_CC_ALWAYS_INLINE |
| 170 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 171 | int pldm_msgbuf_complete(struct pldm_msgbuf *ctx) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 172 | { |
| 173 | int valid; |
| 174 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 175 | valid = pldm_msgbuf_validate(ctx); |
| 176 | |
| 177 | ctx->cursor = NULL; |
| 178 | ctx->remaining = 0; |
| 179 | |
| 180 | return valid; |
| 181 | } |
| 182 | |
| 183 | /** |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 184 | * @brief Complete the pldm_msgbuf instance, and check that the underlying buffer |
| 185 | * has been entirely consumed without overflow |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 186 | * |
| 187 | * @param[in] ctx - pldm_msgbuf context |
| 188 | * |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 189 | * @return 0 if all buffer access were in-bounds and completely consume the |
| 190 | * underlying buffer. Otherwise, -EBADMSG. |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 191 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 192 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 193 | LIBPLDM_CC_ALWAYS_INLINE |
| 194 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 195 | int pldm_msgbuf_complete_consumed(struct pldm_msgbuf *ctx) |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 196 | { |
| 197 | int consumed; |
| 198 | |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 199 | consumed = pldm_msgbuf_consumed(ctx); |
| 200 | |
| 201 | ctx->cursor = NULL; |
| 202 | ctx->remaining = 0; |
| 203 | |
| 204 | return consumed; |
| 205 | } |
| 206 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 207 | /* |
| 208 | * Exploit the pre-processor to perform type checking by macro substitution. |
| 209 | * |
| 210 | * A C type is defined by its alignment as well as its object |
| 211 | * size, and compilers have a hammer to enforce it in the form of |
| 212 | * `-Waddress-of-packed-member`. Due to the unpacked/packed struct conflict in |
| 213 | * the libpldm public API this presents a problem: Naively attempting to use the |
| 214 | * msgbuf APIs on a member of a packed struct would yield an error. |
| 215 | * |
| 216 | * The msgbuf APIs are implemented such that data is moved through unaligned |
| 217 | * pointers in a safe way, but to mitigate `-Waddress-of-packed-member` we must |
| 218 | * make the object pointers take a trip through `void *` at its API boundary. |
| 219 | * That presents a bit too much of an opportunity to non-surgically remove your |
| 220 | * own foot, so here we set about doing something to mitigate that as well. |
| 221 | * |
| 222 | * pldm_msgbuf_extract_typecheck() exists to enforce pointer type correctness |
| 223 | * only for the purpose of object sizes, disregarding alignment. We have a few |
| 224 | * constraints that cause some headaches: |
| 225 | * |
| 226 | * 1. We have to perform the type-check before a call through a C function, |
| 227 | * as the function must take the object pointer argument as `void *`. |
| 228 | * Essentially, this constrains us to doing something with macros. |
| 229 | * |
| 230 | * 2. While libpldm is a C library, its test suite is written in C++ to take |
| 231 | * advantage of gtest. |
| 232 | * |
| 233 | * 3. Ideally we'd do something with C's `static_assert()`, however |
| 234 | * `static_assert()` is defined as void, and as we're constrained to macros, |
| 235 | * using `static_assert()` would require a statement-expression |
| 236 | * |
| 237 | * 4. Currently the project is built with `-std=c17`. CPP statement-expressions |
| 238 | * are a GNU extension. We prefer to avoid switching to `-std=gnu17` just for |
| 239 | * the purpose of enabling statement-expressions in this one instance. |
| 240 | * |
| 241 | * 5. We can achieve a conditional build error using `pldm_require_obj_type()`, |
| 242 | * however it's implemented in terms of `_Generic()`, which is not available |
| 243 | * in C++. |
| 244 | * |
| 245 | * Combined this means we need separate solutions for C and C++. |
| 246 | * |
| 247 | * For C, as we don't have statement-expressions, we need to exploit some other |
| 248 | * language feature to inject a `pldm_require_obj_type()` prior to the msgbuf |
| 249 | * API function call. We also have to take care of the fact that the call-sites |
| 250 | * may be in the context of a variable assignment for error-handling purposes. |
| 251 | * The key observation is that we can use the comma operator as a sequence point |
| 252 | * to order the type check before the API call, discarding the "result" value of |
| 253 | * the type check and yielding the return value of the API call. |
| 254 | * |
| 255 | * C++ could be less of a headache than the C as we can leverage template |
| 256 | * functions. An advantage of template functions is that while their definition |
| 257 | * is driven by instantion, the definition does not appear at the source |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 258 | * location of the instantiation, which gives it a great leg-up over the problems |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 259 | * we have in the C path. However, the use of the msgbuf APIs in the test suite |
| 260 | * still makes things somewhat tricky, as the call-sites in the test suite are |
| 261 | * wrapped up in EXPECT_*() gtest macros. Ideally we'd implement functions that |
| 262 | * takes both the object type and the required type as template arguments, and |
| 263 | * then define the object pointer parameter as `void *` for a call through to |
| 264 | * the appropriate msgbuf API. However, because the msgbuf API call-sites are |
| 265 | * encapsulated in gtest macros, use of commas in the template specification |
| 266 | * causes pre-processor confusion. In this way we're constrained to only one |
| 267 | * template argument per function. |
| 268 | * |
| 269 | * Implement the C++ path using template functions that take the destination |
| 270 | * object type as a template argument, while the name of the function symbols |
| 271 | * are derived from the required type. The manual implementations of these |
| 272 | * appear at the end of the header. The type safety is actually enforced |
| 273 | * by `static_assert()` this time, as we can use statements as we're not |
| 274 | * constrained to an expression in the templated function body. |
| 275 | * |
| 276 | * The invocations of pldm_msgbuf_extract_typecheck() typically result in |
| 277 | * double-evaluation of some arguments. We're not yet bothered by this for two |
| 278 | * reasons: |
| 279 | * |
| 280 | * 1. The nature of the current call-sites are such that there are no |
| 281 | * argument expressions that result in undesirable side-effects |
| 282 | * |
| 283 | * 2. It's an API internal to the libpldm implementation, and we can fix things |
| 284 | * whenever something crops up the violates the observation in 1. |
| 285 | */ |
| 286 | #ifdef __cplusplus |
| 287 | #define pldm_msgbuf_extract_typecheck(ty, fn, dst, ...) \ |
| 288 | pldm_msgbuf_typecheck_##ty<decltype(dst)>(__VA_ARGS__) |
| 289 | #else |
| 290 | #define pldm_msgbuf_extract_typecheck(ty, fn, dst, ...) \ |
| 291 | (pldm_require_obj_type(dst, ty), fn(__VA_ARGS__)) |
| 292 | #endif |
| 293 | |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 294 | /** |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 295 | * @brief pldm_msgbuf extractor for a uint8_t |
| 296 | * |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 297 | * @param[in,out] ctx - pldm_msgbuf context for extractor |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 298 | * @param[out] dst - destination of extracted value |
| 299 | * |
| 300 | * @return PLDM_SUCCESS if buffer accesses were in-bounds, |
| 301 | * PLDM_ERROR_INVALID_LENGTH otherwise. |
| 302 | * PLDM_ERROR_INVALID_DATA if input a invalid ctx |
| 303 | */ |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 304 | #define pldm_msgbuf_extract_uint8(ctx, dst) \ |
| 305 | pldm_msgbuf_extract_typecheck(uint8_t, pldm__msgbuf_extract_uint8, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 306 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 307 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 308 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 309 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 310 | pldm__msgbuf_extract_uint8(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 311 | { |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 312 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 313 | return -EINVAL; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 314 | } |
| 315 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 316 | if (ctx->remaining >= (intmax_t)sizeof(uint8_t)) { |
| 317 | memcpy(dst, ctx->cursor, sizeof(uint8_t)); |
| 318 | ctx->cursor++; |
| 319 | ctx->remaining -= sizeof(uint8_t); |
| 320 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 321 | } |
| 322 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 323 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(uint8_t)) { |
| 324 | ctx->remaining -= sizeof(uint8_t); |
| 325 | } |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 326 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 327 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 328 | } |
| 329 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 330 | #define pldm_msgbuf_extract_int8(ctx, dst) \ |
| 331 | pldm_msgbuf_extract_typecheck(int8_t, pldm__msgbuf_extract_int8, dst, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 332 | ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 333 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 334 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 335 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 336 | pldm__msgbuf_extract_int8(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 337 | { |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 338 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 339 | return -EINVAL; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 340 | } |
| 341 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 342 | if (ctx->remaining >= (intmax_t)sizeof(int8_t)) { |
| 343 | memcpy(dst, ctx->cursor, sizeof(int8_t)); |
| 344 | ctx->cursor++; |
| 345 | ctx->remaining -= sizeof(int8_t); |
| 346 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 347 | } |
| 348 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 349 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(int8_t)) { |
| 350 | ctx->remaining -= sizeof(int8_t); |
| 351 | } |
| 352 | |
| 353 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 354 | } |
| 355 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 356 | #define pldm_msgbuf_extract_uint16(ctx, dst) \ |
| 357 | pldm_msgbuf_extract_typecheck(uint16_t, pldm__msgbuf_extract_uint16, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 358 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 359 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 360 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 361 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 362 | pldm__msgbuf_extract_uint16(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 363 | { |
| 364 | uint16_t ldst; |
| 365 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 366 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 367 | return -EINVAL; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 368 | } |
| 369 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 370 | // Check for underflow while tracking the magnitude of the buffer overflow |
| 371 | static_assert( |
| 372 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 373 | sizeof(ldst) < INTMAX_MAX, |
| 374 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 375 | |
| 376 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
| 377 | // Use memcpy() to have the compiler deal with any alignment |
| 378 | // issues on the target architecture |
| 379 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 380 | |
| 381 | // Only assign the target value once it's correctly decoded |
| 382 | ldst = le16toh(ldst); |
| 383 | |
| 384 | // Allow storing to unaligned |
| 385 | memcpy(dst, &ldst, sizeof(ldst)); |
| 386 | |
| 387 | ctx->cursor += sizeof(ldst); |
| 388 | ctx->remaining -= sizeof(ldst); |
| 389 | return 0; |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 390 | } |
| 391 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 392 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
| 393 | ctx->remaining -= sizeof(ldst); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 394 | } |
| 395 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 396 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 397 | } |
| 398 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 399 | #define pldm_msgbuf_extract_int16(ctx, dst) \ |
| 400 | pldm_msgbuf_extract_typecheck(int16_t, pldm__msgbuf_extract_int16, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 401 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 402 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 403 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 404 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 405 | pldm__msgbuf_extract_int16(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 406 | { |
| 407 | int16_t ldst; |
| 408 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 409 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 410 | return -EINVAL; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 411 | } |
| 412 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 413 | static_assert( |
| 414 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 415 | sizeof(ldst) < INTMAX_MAX, |
| 416 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 417 | |
| 418 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
| 419 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 420 | ldst = le16toh(ldst); |
| 421 | memcpy(dst, &ldst, sizeof(ldst)); |
| 422 | ctx->cursor += sizeof(ldst); |
| 423 | ctx->remaining -= sizeof(ldst); |
| 424 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 425 | } |
| 426 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 427 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
| 428 | ctx->remaining -= sizeof(ldst); |
| 429 | } |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 430 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 431 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 432 | } |
| 433 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 434 | #define pldm_msgbuf_extract_uint32(ctx, dst) \ |
| 435 | pldm_msgbuf_extract_typecheck(uint32_t, pldm__msgbuf_extract_uint32, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 436 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 437 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 438 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 439 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 440 | pldm__msgbuf_extract_uint32(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 441 | { |
| 442 | uint32_t ldst; |
| 443 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 444 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 445 | return -EINVAL; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 446 | } |
| 447 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 448 | static_assert( |
| 449 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 450 | sizeof(ldst) < INTMAX_MAX, |
| 451 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 452 | |
| 453 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
| 454 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 455 | ldst = le32toh(ldst); |
| 456 | memcpy(dst, &ldst, sizeof(ldst)); |
| 457 | ctx->cursor += sizeof(ldst); |
| 458 | ctx->remaining -= sizeof(ldst); |
| 459 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 460 | } |
| 461 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 462 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
| 463 | ctx->remaining -= sizeof(ldst); |
| 464 | } |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 465 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 466 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 467 | } |
| 468 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 469 | #define pldm_msgbuf_extract_int32(ctx, dst) \ |
| 470 | pldm_msgbuf_extract_typecheck(int32_t, pldm__msgbuf_extract_int32, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 471 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 472 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 473 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 474 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 475 | pldm__msgbuf_extract_int32(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 476 | { |
| 477 | int32_t ldst; |
| 478 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 479 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 480 | return -EINVAL; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 481 | } |
| 482 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 483 | static_assert( |
| 484 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 485 | sizeof(ldst) < INTMAX_MAX, |
| 486 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 487 | |
| 488 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
| 489 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 490 | ldst = le32toh(ldst); |
| 491 | memcpy(dst, &ldst, sizeof(ldst)); |
| 492 | ctx->cursor += sizeof(ldst); |
| 493 | ctx->remaining -= sizeof(ldst); |
| 494 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 495 | } |
| 496 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 497 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
| 498 | ctx->remaining -= sizeof(ldst); |
| 499 | } |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 500 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 501 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 502 | } |
| 503 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 504 | #define pldm_msgbuf_extract_real32(ctx, dst) \ |
| 505 | pldm_msgbuf_extract_typecheck(real32_t, pldm__msgbuf_extract_real32, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 506 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 507 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 508 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 509 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 510 | pldm__msgbuf_extract_real32(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 511 | { |
| 512 | uint32_t ldst; |
| 513 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 514 | static_assert(sizeof(real32_t) == sizeof(ldst), |
| 515 | "Mismatched type sizes for dst and ldst"); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 516 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 517 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 518 | return -EINVAL; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 519 | } |
| 520 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 521 | static_assert( |
| 522 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 523 | sizeof(ldst) < INTMAX_MAX, |
| 524 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 525 | |
| 526 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
| 527 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 528 | ldst = le32toh(ldst); |
| 529 | memcpy(dst, &ldst, sizeof(ldst)); |
| 530 | ctx->cursor += sizeof(ldst); |
| 531 | ctx->remaining -= sizeof(ldst); |
| 532 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 533 | } |
| 534 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 535 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
| 536 | ctx->remaining -= sizeof(ldst); |
| 537 | } |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 538 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 539 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 540 | } |
| 541 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 542 | /** |
| 543 | * Extract the field at the msgbuf cursor into the lvalue named by dst. |
| 544 | * |
| 545 | * @param ctx The msgbuf context object |
| 546 | * @param dst The lvalue into which the field at the msgbuf cursor should be |
| 547 | * extracted |
| 548 | * |
| 549 | * @return PLDM_SUCCESS on success, otherwise another value on error |
| 550 | */ |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 551 | #define pldm_msgbuf_extract(ctx, dst) \ |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 552 | _Generic((dst), \ |
| 553 | uint8_t: pldm__msgbuf_extract_uint8, \ |
| 554 | int8_t: pldm__msgbuf_extract_int8, \ |
| 555 | uint16_t: pldm__msgbuf_extract_uint16, \ |
| 556 | int16_t: pldm__msgbuf_extract_int16, \ |
| 557 | uint32_t: pldm__msgbuf_extract_uint32, \ |
| 558 | int32_t: pldm__msgbuf_extract_int32, \ |
| 559 | real32_t: pldm__msgbuf_extract_real32)(ctx, (void *)&(dst)) |
| 560 | |
| 561 | /** |
| 562 | * Extract the field at the msgbuf cursor into the object pointed-to by dst. |
| 563 | * |
| 564 | * @param ctx The msgbuf context object |
| 565 | * @param dst The pointer to the object into which the field at the msgbuf |
| 566 | * cursor should be extracted |
| 567 | * |
| 568 | * @return PLDM_SUCCESS on success, otherwise another value on error |
| 569 | */ |
| 570 | #define pldm_msgbuf_extract_p(ctx, dst) \ |
| 571 | _Generic((dst), \ |
| 572 | uint8_t *: pldm__msgbuf_extract_uint8, \ |
| 573 | int8_t *: pldm__msgbuf_extract_int8, \ |
| 574 | uint16_t *: pldm__msgbuf_extract_uint16, \ |
| 575 | int16_t *: pldm__msgbuf_extract_int16, \ |
| 576 | uint32_t *: pldm__msgbuf_extract_uint32, \ |
| 577 | int32_t *: pldm__msgbuf_extract_int32, \ |
| 578 | real32_t *: pldm__msgbuf_extract_real32)(ctx, dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 579 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 580 | /** |
| 581 | * @ref pldm_msgbuf_extract_array |
| 582 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 583 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 584 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 585 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 586 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 587 | pldm__msgbuf_extract_array_void(struct pldm_msgbuf *ctx, size_t count, |
| 588 | void *dst, size_t dst_count) |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 589 | { |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 590 | if (!ctx->cursor || count > dst_count) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 591 | return -EINVAL; |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | if (!count) { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 595 | return 0; |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 596 | } |
| 597 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 598 | #if INTMAX_MAX < SIZE_MAX |
| 599 | if (count > INTMAX_MAX) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 600 | return -EOVERFLOW; |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 601 | } |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 602 | #endif |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 603 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 604 | if (ctx->remaining >= (intmax_t)count) { |
| 605 | memcpy(dst, ctx->cursor, count); |
| 606 | ctx->cursor += count; |
| 607 | ctx->remaining -= (intmax_t)count; |
| 608 | return 0; |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 609 | } |
| 610 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 611 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)count) { |
| 612 | ctx->remaining -= (intmax_t)count; |
| 613 | } |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 614 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 615 | return -EOVERFLOW; |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 616 | } |
| 617 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 618 | /** |
| 619 | * @ref pldm_msgbuf_extract_array |
| 620 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 621 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 622 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 623 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 624 | pldm_msgbuf_extract_array_char(struct pldm_msgbuf *ctx, size_t count, char *dst, |
| 625 | size_t dst_count) |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 626 | { |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 627 | return pldm__msgbuf_extract_array_void(ctx, count, dst, dst_count); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 628 | } |
| 629 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 630 | /** |
| 631 | * @ref pldm_msgbuf_extract_array |
| 632 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 633 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 634 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 635 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 636 | pldm_msgbuf_extract_array_uint8(struct pldm_msgbuf *ctx, size_t count, |
| 637 | uint8_t *dst, size_t dst_count) |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 638 | { |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 639 | return pldm__msgbuf_extract_array_void(ctx, count, dst, dst_count); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 640 | } |
| 641 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 642 | /** |
| 643 | * Extract an array of data from the msgbuf instance |
| 644 | * |
| 645 | * @param ctx - The msgbuf instance from which to extract an array of data |
| 646 | * @param count - The number of array elements to extract |
| 647 | * @param dst - The array object into which elements from @p ctx should be |
| 648 | extracted |
| 649 | * @param dst_count - The maximum number of elements to place into @p dst |
| 650 | * |
| 651 | * Note that both @p count and @p dst_count can only be counted by `sizeof` for |
| 652 | * arrays where `sizeof(*dst) == 1` holds. Specifically, they count the number |
| 653 | * of array elements and _not_ the object size of the array. |
| 654 | */ |
| 655 | #define pldm_msgbuf_extract_array(ctx, count, dst, dst_count) \ |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 656 | _Generic((*(dst)), \ |
| 657 | uint8_t: pldm_msgbuf_extract_array_uint8, \ |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 658 | char: pldm_msgbuf_extract_array_char)(ctx, count, dst, \ |
| 659 | dst_count) |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 660 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 661 | LIBPLDM_CC_NONNULL |
Matt Johnston | e8d8d33 | 2024-10-28 17:13:32 +0800 | [diff] [blame] | 662 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint64(struct pldm_msgbuf *ctx, |
| 663 | const uint64_t src) |
| 664 | { |
| 665 | uint64_t val = htole64(src); |
| 666 | |
| 667 | if (!ctx->cursor) { |
| 668 | return -EINVAL; |
| 669 | } |
| 670 | |
| 671 | static_assert( |
| 672 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 673 | sizeof(src) < INTMAX_MAX, |
| 674 | "The following addition may not uphold the runtime assertion"); |
| 675 | |
| 676 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
| 677 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 678 | ctx->cursor += sizeof(src); |
| 679 | ctx->remaining -= sizeof(src); |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) { |
| 684 | ctx->remaining -= sizeof(src); |
| 685 | } |
| 686 | |
| 687 | return -EOVERFLOW; |
| 688 | } |
| 689 | |
| 690 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 691 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint32(struct pldm_msgbuf *ctx, |
| 692 | const uint32_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 693 | { |
| 694 | uint32_t val = htole32(src); |
| 695 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 696 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 697 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 698 | } |
| 699 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 700 | static_assert( |
| 701 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 702 | sizeof(src) < INTMAX_MAX, |
| 703 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 704 | |
| 705 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
| 706 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 707 | ctx->cursor += sizeof(src); |
| 708 | ctx->remaining -= sizeof(src); |
| 709 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 710 | } |
| 711 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 712 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) { |
| 713 | ctx->remaining -= sizeof(src); |
| 714 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 715 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 716 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 717 | } |
| 718 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 719 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 720 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint16(struct pldm_msgbuf *ctx, |
| 721 | const uint16_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 722 | { |
| 723 | uint16_t val = htole16(src); |
| 724 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 725 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 726 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 727 | } |
| 728 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 729 | static_assert( |
| 730 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 731 | sizeof(src) < INTMAX_MAX, |
| 732 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 733 | |
| 734 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
| 735 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 736 | ctx->cursor += sizeof(src); |
| 737 | ctx->remaining -= sizeof(src); |
| 738 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 739 | } |
| 740 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 741 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) { |
| 742 | ctx->remaining -= sizeof(src); |
| 743 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 744 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 745 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 746 | } |
| 747 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 748 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 749 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint8(struct pldm_msgbuf *ctx, |
| 750 | const uint8_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 751 | { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 752 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 753 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 754 | } |
| 755 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 756 | static_assert( |
| 757 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 758 | sizeof(src) < INTMAX_MAX, |
| 759 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 760 | |
| 761 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
| 762 | memcpy(ctx->cursor, &src, sizeof(src)); |
| 763 | ctx->cursor += sizeof(src); |
| 764 | ctx->remaining -= sizeof(src); |
| 765 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 766 | } |
| 767 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 768 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) { |
| 769 | ctx->remaining -= sizeof(src); |
| 770 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 771 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 772 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 773 | } |
| 774 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 775 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 776 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_int32(struct pldm_msgbuf *ctx, |
| 777 | const int32_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 778 | { |
| 779 | int32_t val = htole32(src); |
| 780 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 781 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 782 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 783 | } |
| 784 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 785 | static_assert( |
| 786 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 787 | sizeof(src) < INTMAX_MAX, |
| 788 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 789 | |
| 790 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
| 791 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 792 | ctx->cursor += sizeof(src); |
| 793 | ctx->remaining -= sizeof(src); |
| 794 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 795 | } |
| 796 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 797 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) { |
| 798 | ctx->remaining -= sizeof(src); |
| 799 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 800 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 801 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 802 | } |
| 803 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 804 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 805 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_int16(struct pldm_msgbuf *ctx, |
| 806 | const int16_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 807 | { |
| 808 | int16_t val = htole16(src); |
| 809 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 810 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 811 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 812 | } |
| 813 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 814 | static_assert( |
| 815 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 816 | sizeof(src) < INTMAX_MAX, |
| 817 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 818 | |
| 819 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
| 820 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 821 | ctx->cursor += sizeof(src); |
| 822 | ctx->remaining -= sizeof(src); |
| 823 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 824 | } |
| 825 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 826 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) { |
| 827 | ctx->remaining -= sizeof(src); |
| 828 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 829 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 830 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 831 | } |
| 832 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 833 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 834 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_int8(struct pldm_msgbuf *ctx, |
| 835 | const int8_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 836 | { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 837 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 838 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 839 | } |
| 840 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 841 | static_assert( |
| 842 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 843 | sizeof(src) < INTMAX_MAX, |
| 844 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 845 | |
| 846 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
| 847 | memcpy(ctx->cursor, &src, sizeof(src)); |
| 848 | ctx->cursor += sizeof(src); |
| 849 | ctx->remaining -= sizeof(src); |
| 850 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 851 | } |
| 852 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 853 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) { |
| 854 | ctx->remaining -= sizeof(src); |
| 855 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 856 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 857 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | #define pldm_msgbuf_insert(dst, src) \ |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 861 | _Generic((src), \ |
| 862 | uint8_t: pldm_msgbuf_insert_uint8, \ |
| 863 | int8_t: pldm_msgbuf_insert_int8, \ |
| 864 | uint16_t: pldm_msgbuf_insert_uint16, \ |
| 865 | int16_t: pldm_msgbuf_insert_int16, \ |
| 866 | uint32_t: pldm_msgbuf_insert_uint32, \ |
Matt Johnston | e8d8d33 | 2024-10-28 17:13:32 +0800 | [diff] [blame] | 867 | int32_t: pldm_msgbuf_insert_int32, \ |
| 868 | uint64_t: pldm_msgbuf_insert_uint64)(dst, src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 869 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 870 | /** |
| 871 | * @ref pldm_msgbuf_insert_array |
| 872 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 873 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 874 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 875 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 876 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 877 | pldm__msgbuf_insert_array_void(struct pldm_msgbuf *ctx, size_t count, |
| 878 | const void *src, size_t src_count) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 879 | { |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 880 | if (!ctx->cursor || count > src_count) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 881 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | if (!count) { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 885 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 886 | } |
| 887 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 888 | #if INTMAX_MAX < SIZE_MAX |
| 889 | if (count > INTMAX_MAX) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 890 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 891 | } |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 892 | #endif |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 893 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 894 | if (ctx->remaining >= (intmax_t)count) { |
| 895 | memcpy(ctx->cursor, src, count); |
| 896 | ctx->cursor += count; |
| 897 | ctx->remaining -= (intmax_t)count; |
| 898 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 899 | } |
| 900 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 901 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)count) { |
| 902 | ctx->remaining -= (intmax_t)count; |
| 903 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 904 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 905 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 906 | } |
| 907 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 908 | /** |
| 909 | * @ref pldm_msgbuf_insert_array |
| 910 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 911 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 912 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 913 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 914 | pldm_msgbuf_insert_array_char(struct pldm_msgbuf *ctx, size_t count, |
| 915 | const char *src, size_t src_count) |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 916 | { |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 917 | return pldm__msgbuf_insert_array_void(ctx, count, src, src_count); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 918 | } |
| 919 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 920 | /** |
| 921 | * @ref pldm_msgbuf_insert_array |
| 922 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 923 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 924 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 925 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 926 | pldm_msgbuf_insert_array_uint8(struct pldm_msgbuf *ctx, size_t count, |
| 927 | const uint8_t *src, size_t src_count) |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 928 | { |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 929 | return pldm__msgbuf_insert_array_void(ctx, count, src, src_count); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 930 | } |
| 931 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 932 | /** |
| 933 | * Insert an array of data into the msgbuf instance |
| 934 | * |
| 935 | * @param ctx - The msgbuf instance into which the array of data should be |
| 936 | * inserted |
| 937 | * @param count - The number of array elements to insert |
| 938 | * @param src - The array object from which elements should be inserted into |
| 939 | @p ctx |
| 940 | * @param src_count - The maximum number of elements to insert from @p src |
| 941 | * |
| 942 | * Note that both @p count and @p src_count can only be counted by `sizeof` for |
| 943 | * arrays where `sizeof(*dst) == 1` holds. Specifically, they count the number |
| 944 | * of array elements and _not_ the object size of the array. |
| 945 | */ |
| 946 | #define pldm_msgbuf_insert_array(dst, count, src, src_count) \ |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 947 | _Generic((*(src)), \ |
| 948 | uint8_t: pldm_msgbuf_insert_array_uint8, \ |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 949 | char: pldm_msgbuf_insert_array_char)(dst, count, src, \ |
| 950 | src_count) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 951 | |
Varsha Kaverappa | 7939382 | 2024-08-07 00:40:13 -0500 | [diff] [blame] | 952 | LIBPLDM_CC_NONNULL_ARGS(1) |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 953 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_span_required(struct pldm_msgbuf *ctx, |
| 954 | size_t required, |
| 955 | void **cursor) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 956 | { |
Andrew Jeffery | 2b42f7f | 2025-03-03 21:23:13 +1030 | [diff] [blame] | 957 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 958 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 959 | } |
| 960 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 961 | #if INTMAX_MAX < SIZE_MAX |
| 962 | if (required > INTMAX_MAX) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 963 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 964 | } |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 965 | #endif |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 966 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 967 | if (ctx->remaining >= (intmax_t)required) { |
| 968 | if (cursor) { |
| 969 | *cursor = ctx->cursor; |
| 970 | } |
| 971 | ctx->cursor += required; |
| 972 | ctx->remaining -= (intmax_t)required; |
| 973 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 974 | } |
| 975 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 976 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)required) { |
| 977 | ctx->remaining -= (intmax_t)required; |
Varsha Kaverappa | 7939382 | 2024-08-07 00:40:13 -0500 | [diff] [blame] | 978 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 979 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 980 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 981 | } |
| 982 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 983 | LIBPLDM_CC_NONNULL_ARGS(1) |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 984 | LIBPLDM_CC_ALWAYS_INLINE int |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 985 | pldm_msgbuf_span_string_ascii(struct pldm_msgbuf *ctx, void **cursor, |
| 986 | size_t *length) |
| 987 | { |
| 988 | intmax_t measured; |
| 989 | |
Andrew Jeffery | 2b42f7f | 2025-03-03 21:23:13 +1030 | [diff] [blame] | 990 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 991 | return -EINVAL; |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | if (ctx->remaining < 0) { |
| 995 | /* Tracking the amount of overflow gets disturbed here */ |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 996 | return -EOVERFLOW; |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | measured = (intmax_t)strnlen((const char *)ctx->cursor, ctx->remaining); |
| 1000 | if (measured == ctx->remaining) { |
| 1001 | /* |
| 1002 | * We have hit the end of the buffer prior to the NUL terminator. |
| 1003 | * Optimistically, the NUL terminator was one-beyond-the-end. Setting |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 1004 | * ctx->remaining negative ensures the `pldm_msgbuf_complete*()` APIs also |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1005 | * return an error. |
| 1006 | */ |
| 1007 | ctx->remaining = -1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1008 | return -EOVERFLOW; |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /* Include the NUL terminator in the span length, as spans are opaque */ |
| 1012 | measured++; |
| 1013 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1014 | if (ctx->remaining >= measured) { |
| 1015 | if (cursor) { |
| 1016 | *cursor = ctx->cursor; |
| 1017 | } |
| 1018 | |
| 1019 | ctx->cursor += measured; |
| 1020 | |
| 1021 | if (length) { |
| 1022 | *length = measured; |
| 1023 | } |
| 1024 | |
| 1025 | ctx->remaining -= measured; |
| 1026 | return 0; |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1029 | if (ctx->remaining >= INTMAX_MIN + measured) { |
| 1030 | ctx->remaining -= measured; |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1033 | return -EOVERFLOW; |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1036 | LIBPLDM_CC_NONNULL_ARGS(1) |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1037 | LIBPLDM_CC_ALWAYS_INLINE int |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1038 | pldm_msgbuf_span_string_utf16(struct pldm_msgbuf *ctx, void **cursor, |
| 1039 | size_t *length) |
| 1040 | { |
| 1041 | static const char16_t term = 0; |
| 1042 | ptrdiff_t measured; |
| 1043 | void *end; |
| 1044 | |
Andrew Jeffery | 2b42f7f | 2025-03-03 21:23:13 +1030 | [diff] [blame] | 1045 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1046 | return -EINVAL; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | if (ctx->remaining < 0) { |
| 1050 | /* Tracking the amount of overflow gets disturbed here */ |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1051 | return -EOVERFLOW; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* |
| 1055 | * Avoid tripping up on UTF16-LE: We may have consecutive NUL _bytes_ that do |
| 1056 | * not form a UTF16 NUL _code-point_ due to alignment with respect to the |
| 1057 | * start of the string |
| 1058 | */ |
Andrew Jeffery | 2b440d4 | 2024-07-25 10:36:08 +0930 | [diff] [blame] | 1059 | end = ctx->cursor; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1060 | do { |
| 1061 | if (end != ctx->cursor) { |
| 1062 | /* |
| 1063 | * If we've looped we've found a relatively-unaligned NUL code-point. |
| 1064 | * Scan again from a relatively-aligned start point. |
| 1065 | */ |
| 1066 | end = (char *)end + 1; |
| 1067 | } |
| 1068 | measured = (char *)end - (char *)ctx->cursor; |
Andrew Jeffery | 2b440d4 | 2024-07-25 10:36:08 +0930 | [diff] [blame] | 1069 | end = memmem(end, ctx->remaining - measured, &term, |
| 1070 | sizeof(term)); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1071 | } while (end && ((uintptr_t)end & 1) != ((uintptr_t)ctx->cursor & 1)); |
| 1072 | |
| 1073 | if (!end) { |
| 1074 | /* |
| 1075 | * Optimistically, the last required pattern byte was one beyond the end of |
| 1076 | * the buffer. Setting ctx->remaining negative ensures the |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 1077 | * `pldm_msgbuf_complete*()` APIs also return an error. |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1078 | */ |
| 1079 | ctx->remaining = -1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1080 | return -EOVERFLOW; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | end = (char *)end + sizeof(char16_t); |
| 1084 | measured = (char *)end - (char *)ctx->cursor; |
| 1085 | |
| 1086 | #if INTMAX_MAX < PTRDIFF_MAX |
| 1087 | if (measured >= INTMAX_MAX) { |
| 1088 | return pldm_msgbuf_status(ctx, EOVERFLOW); |
| 1089 | } |
| 1090 | #endif |
| 1091 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1092 | if (ctx->remaining >= (intmax_t)measured) { |
| 1093 | if (cursor) { |
| 1094 | *cursor = ctx->cursor; |
| 1095 | } |
| 1096 | |
| 1097 | ctx->cursor += measured; |
| 1098 | |
| 1099 | if (length) { |
| 1100 | *length = (size_t)measured; |
| 1101 | } |
| 1102 | |
| 1103 | ctx->remaining -= (intmax_t)measured; |
| 1104 | return 0; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1107 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)measured) { |
| 1108 | ctx->remaining -= (intmax_t)measured; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1111 | return -EOVERFLOW; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1114 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1115 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 1116 | pldm_msgbuf_span_remaining(struct pldm_msgbuf *ctx, void **cursor, size_t *len) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1117 | { |
Andrew Jeffery | 2b42f7f | 2025-03-03 21:23:13 +1030 | [diff] [blame] | 1118 | if (!ctx->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1119 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1122 | if (ctx->remaining < 0) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1123 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | *cursor = ctx->cursor; |
| 1127 | ctx->cursor += ctx->remaining; |
| 1128 | *len = ctx->remaining; |
| 1129 | ctx->remaining = 0; |
| 1130 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1131 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1132 | } |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1133 | |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1134 | LIBPLDM_CC_NONNULL |
| 1135 | LIBPLDM_CC_ALWAYS_INLINE int |
| 1136 | pldm_msgbuf_peek_remaining(struct pldm_msgbuf *ctx, void **cursor, size_t *len) |
| 1137 | { |
Andrew Jeffery | 2b42f7f | 2025-03-03 21:23:13 +1030 | [diff] [blame] | 1138 | if (!ctx->cursor) { |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1139 | return -EINVAL; |
| 1140 | } |
| 1141 | |
| 1142 | if (ctx->remaining < 0) { |
| 1143 | return -EOVERFLOW; |
| 1144 | } |
| 1145 | |
| 1146 | *cursor = ctx->cursor; |
| 1147 | *len = ctx->remaining; |
| 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | LIBPLDM_CC_NONNULL |
| 1153 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_skip(struct pldm_msgbuf *ctx, |
| 1154 | size_t count) |
| 1155 | { |
| 1156 | if (!ctx->cursor) { |
| 1157 | return -EINVAL; |
| 1158 | } |
| 1159 | |
| 1160 | #if INTMAX_MAX < SIZE_MAX |
| 1161 | if (count > INTMAX_MAX) { |
| 1162 | return -EOVERFLOW; |
| 1163 | } |
| 1164 | #endif |
| 1165 | |
Andrew Jeffery | 7e68f34 | 2025-03-03 21:37:23 +1030 | [diff] [blame] | 1166 | if (ctx->remaining >= (intmax_t)count) { |
| 1167 | ctx->cursor += count; |
| 1168 | ctx->remaining -= (intmax_t)count; |
| 1169 | return 0; |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1170 | } |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1171 | |
Andrew Jeffery | 7e68f34 | 2025-03-03 21:37:23 +1030 | [diff] [blame] | 1172 | if (ctx->remaining >= INTMAX_MIN + (intmax_t)count) { |
| 1173 | ctx->remaining -= (intmax_t)count; |
| 1174 | } |
| 1175 | |
| 1176 | return -EOVERFLOW; |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1179 | /** |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 1180 | * @brief Complete the pldm_msgbuf instance and return the number of bytes |
| 1181 | * consumed. |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1182 | * |
| 1183 | * @param ctx - The msgbuf. |
| 1184 | * @param orig_len - The original size of the msgbuf, the `len` argument passed to |
| 1185 | * pldm_msgbuf_init_errno(). |
| 1186 | * @param ret_used_len - The number of bytes that have been used from the msgbuf instance. |
| 1187 | * |
| 1188 | * This can be called after a number of pldm_msgbuf_insert...() calls to |
| 1189 | * determine the total size that was written. |
| 1190 | * |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 1191 | * @return 0 on success, -EOVERFLOW if an implausible orig_len was provided or |
| 1192 | * an out-of-bounds access occurred. |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1193 | */ |
| 1194 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 1195 | LIBPLDM_CC_ALWAYS_INLINE |
| 1196 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame^] | 1197 | int pldm_msgbuf_complete_used(struct pldm_msgbuf *ctx, size_t orig_len, |
| 1198 | size_t *ret_used_len) |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1199 | { |
| 1200 | int rc; |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 1201 | |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1202 | rc = pldm_msgbuf_validate(ctx); |
| 1203 | if (rc) { |
| 1204 | return rc; |
| 1205 | } |
| 1206 | |
| 1207 | if ((size_t)ctx->remaining > orig_len) { |
| 1208 | /* Caller passed incorrect orig_len */ |
| 1209 | return -EOVERFLOW; |
| 1210 | } |
| 1211 | |
| 1212 | *ret_used_len = orig_len - ctx->remaining; |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | /** |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1217 | * @brief pldm_msgbuf copy data between two msg buffers |
| 1218 | * |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 1219 | * @param[in,out] src - pldm_msgbuf for source from where value should be copied |
| 1220 | * @param[in,out] dst - destination of copy from source |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1221 | * @param[in] size - size of data to be copied |
| 1222 | * @param[in] description - description of data copied |
| 1223 | * |
| 1224 | * @return PLDM_SUCCESS if buffer accesses were in-bounds, |
| 1225 | * PLDM_ERROR_INVALID_LENGTH otherwise. |
| 1226 | * PLDM_ERROR_INVALID_DATA if input is invalid |
| 1227 | */ |
| 1228 | #define pldm_msgbuf_copy(dst, src, type, name) \ |
| 1229 | pldm__msgbuf_copy(dst, src, sizeof(type), #name) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1230 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1231 | LIBPLDM_CC_ALWAYS_INLINE int |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1232 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 1233 | pldm__msgbuf_copy(struct pldm_msgbuf *dst, struct pldm_msgbuf *src, size_t size, |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1234 | const char *description LIBPLDM_CC_UNUSED) |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1235 | { |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1236 | if (!src->cursor || !dst->cursor) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1237 | return -EINVAL; |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | #if INTMAX_MAX < SIZE_MAX |
| 1241 | if (size > INTMAX_MAX) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1242 | return -EOVERFLOW; |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1243 | } |
| 1244 | #endif |
| 1245 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1246 | if (src->remaining >= (intmax_t)size && |
| 1247 | dst->remaining >= (intmax_t)size) { |
| 1248 | memcpy(dst->cursor, src->cursor, size); |
| 1249 | src->cursor += size; |
| 1250 | src->remaining -= (intmax_t)size; |
| 1251 | dst->cursor += size; |
| 1252 | dst->remaining -= (intmax_t)size; |
| 1253 | return 0; |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1254 | } |
| 1255 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1256 | if (src->remaining >= INTMAX_MIN + (intmax_t)size) { |
| 1257 | src->remaining -= (intmax_t)size; |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1258 | } |
| 1259 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1260 | if (dst->remaining >= INTMAX_MIN + (intmax_t)size) { |
| 1261 | dst->remaining -= (intmax_t)size; |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1262 | } |
| 1263 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1264 | return -EOVERFLOW; |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1265 | } |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1266 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1267 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1268 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1269 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1270 | pldm_msgbuf_copy_string_ascii(struct pldm_msgbuf *dst, struct pldm_msgbuf *src) |
| 1271 | { |
| 1272 | void *ascii = NULL; |
| 1273 | size_t len = 0; |
| 1274 | int rc; |
| 1275 | |
| 1276 | rc = pldm_msgbuf_span_string_ascii(src, &ascii, &len); |
| 1277 | if (rc < 0) { |
| 1278 | return rc; |
| 1279 | } |
| 1280 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1281 | return pldm__msgbuf_insert_array_void(dst, len, ascii, len); |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1282 | } |
| 1283 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1284 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1285 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1286 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 56f73f9 | 2024-07-08 12:50:28 +0930 | [diff] [blame] | 1287 | pldm_msgbuf_copy_string_utf16(struct pldm_msgbuf *dst, struct pldm_msgbuf *src) |
| 1288 | { |
| 1289 | void *utf16 = NULL; |
| 1290 | size_t len = 0; |
| 1291 | int rc; |
| 1292 | |
| 1293 | rc = pldm_msgbuf_span_string_utf16(src, &utf16, &len); |
| 1294 | if (rc < 0) { |
| 1295 | return rc; |
| 1296 | } |
| 1297 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1298 | return pldm__msgbuf_insert_array_void(dst, len, utf16, len); |
Andrew Jeffery | 56f73f9 | 2024-07-08 12:50:28 +0930 | [diff] [blame] | 1299 | } |
| 1300 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 1301 | #ifdef __cplusplus |
| 1302 | } |
| 1303 | #endif |
| 1304 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1305 | #ifdef __cplusplus |
| 1306 | #include <type_traits> |
| 1307 | |
| 1308 | template <typename T> |
| 1309 | static inline int pldm_msgbuf_typecheck_uint8_t(struct pldm_msgbuf *ctx, |
| 1310 | void *buf) |
| 1311 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1312 | static_assert(std::is_same<uint8_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1313 | return pldm__msgbuf_extract_uint8(ctx, buf); |
| 1314 | } |
| 1315 | |
| 1316 | template <typename T> |
| 1317 | static inline int pldm_msgbuf_typecheck_int8_t(struct pldm_msgbuf *ctx, |
| 1318 | void *buf) |
| 1319 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1320 | static_assert(std::is_same<int8_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1321 | return pldm__msgbuf_extract_int8(ctx, buf); |
| 1322 | } |
| 1323 | |
| 1324 | template <typename T> |
| 1325 | static inline int pldm_msgbuf_typecheck_uint16_t(struct pldm_msgbuf *ctx, |
| 1326 | void *buf) |
| 1327 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1328 | static_assert(std::is_same<uint16_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1329 | return pldm__msgbuf_extract_uint16(ctx, buf); |
| 1330 | } |
| 1331 | |
| 1332 | template <typename T> |
| 1333 | static inline int pldm_msgbuf_typecheck_int16_t(struct pldm_msgbuf *ctx, |
| 1334 | void *buf) |
| 1335 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1336 | static_assert(std::is_same<int16_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1337 | return pldm__msgbuf_extract_int16(ctx, buf); |
| 1338 | } |
| 1339 | |
| 1340 | template <typename T> |
| 1341 | static inline int pldm_msgbuf_typecheck_uint32_t(struct pldm_msgbuf *ctx, |
| 1342 | void *buf) |
| 1343 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1344 | static_assert(std::is_same<uint32_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1345 | return pldm__msgbuf_extract_uint32(ctx, buf); |
| 1346 | } |
| 1347 | |
| 1348 | template <typename T> |
| 1349 | static inline int pldm_msgbuf_typecheck_int32_t(struct pldm_msgbuf *ctx, |
| 1350 | void *buf) |
| 1351 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1352 | static_assert(std::is_same<int32_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1353 | return pldm__msgbuf_extract_int32(ctx, buf); |
| 1354 | } |
| 1355 | |
| 1356 | template <typename T> |
| 1357 | static inline int pldm_msgbuf_typecheck_real32_t(struct pldm_msgbuf *ctx, |
| 1358 | void *buf) |
| 1359 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1360 | static_assert(std::is_same<real32_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1361 | return pldm__msgbuf_extract_real32(ctx, buf); |
| 1362 | } |
| 1363 | #endif |
| 1364 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 1365 | #endif /* BUF_H */ |