blob: 6cab0d2bcd6afc3ea13e16f6e17802c0c92835f6 [file] [log] [blame]
Patrick Williams691668f2023-11-01 08:19:10 -05001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10302#ifndef PLDM_MSGBUF_H
3#define PLDM_MSGBUF_H
4
Andrew Jeffery860a43d2024-08-23 01:21:58 +00005#include "compiler.h"
6
Andrew Jeffery66c77232024-04-24 11:42:02 +09307/*
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 Jefferyc63f63a2023-02-24 22:29:33 +103033#ifdef __cplusplus
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093034/*
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 Jefferyc63f63a2023-02-24 22:29:33 +103041extern "C" {
42#endif
43
Andrew Jefferyb0c1d202023-11-07 22:08:44 +103044#include <libpldm/base.h>
45#include <libpldm/pldm_types.h>
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103046
Andrew Jeffery66c77232024-04-24 11:42:02 +093047#include "compiler.h"
48
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103049#include <assert.h>
50#include <endian.h>
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093051#include <errno.h>
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103052#include <limits.h>
53#include <stdbool.h>
Andrew Jeffery66c77232024-04-24 11:42:02 +093054#include <stdint.h>
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103055#include <string.h>
56#include <sys/types.h>
Thu Nguyen15237782024-07-02 09:30:41 +000057#include <uchar.h>
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103058
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +093059/*
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*/
65static 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 Jeffery860a43d2024-08-23 01:21:58 +000072} build_assertions LIBPLDM_CC_UNUSED;
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +093073
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103074struct pldm_msgbuf {
Thu Nguyen062c8762023-04-22 20:45:04 +070075 uint8_t *cursor;
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +093076 intmax_t remaining;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103077};
78
Andrew Jefferyd861a682024-06-03 21:43:09 +093079/**
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103080 * @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 Jefferyc8df31c2024-05-21 16:47:43 +093087 * @return 0 on success, otherwise an error code appropriate for the current
88 * personality.
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103089 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +030090LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +030091LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093092// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093093pldm_msgbuf_init_errno(struct pldm_msgbuf *ctx, size_t minsize, const void *buf,
94 size_t len)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103095{
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +093096 if ((minsize > len)) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093097 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103098 }
99
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930100#if INTMAX_MAX < SIZE_MAX
101 if (len > INTMAX_MAX) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930102 return -EOVERFLOW;
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930103 }
104#endif
105
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930106 if (UINTPTR_MAX - (uintptr_t)buf < len) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930107 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030108 }
109
110 ctx->cursor = (uint8_t *)buf;
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930111 ctx->remaining = (intmax_t)len;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030112
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930113 return 0;
114}
115
116/**
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030117 * @brief Validate buffer overflow state
118 *
119 * @param[in] ctx - pldm_msgbuf context for extractor
120 *
121 * @return PLDM_SUCCESS if there are zero or more bytes of data that remain
122 * unread from the buffer. Otherwise, PLDM_ERROR_INVALID_LENGTH indicates that a
123 * prior accesses would have occurred beyond the bounds of the buffer, and
124 * PLDM_ERROR_INVALID_DATA indicates that the provided context was not a valid
125 * pointer.
126 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300127LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300128LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_validate(struct pldm_msgbuf *ctx)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030129{
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930130 if (ctx->remaining < 0) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930131 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030132 }
133
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930134 return 0;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030135}
136
137/**
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930138 * @brief Test whether a message buffer has been exactly consumed
139 *
140 * @param[in] ctx - pldm_msgbuf context for extractor
141 *
142 * @return PLDM_SUCCESS iff there are zero bytes of data that remain unread from
143 * the buffer and no overflow has occurred. Otherwise, PLDM_ERROR_INVALID_LENGTH
144 * indicates that an incorrect sequence of accesses have occurred, and
145 * PLDM_ERROR_INVALID_DATA indicates that the provided context was not a valid
146 * pointer.
147 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300148LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300149LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_consumed(struct pldm_msgbuf *ctx)
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930150{
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930151 if (ctx->remaining != 0) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930152 return -EBADMSG;
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930153 }
154
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930155 return 0;
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930156}
157
158/**
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030159 * @brief Destroy the pldm buf
160 *
161 * @param[in] ctx - pldm_msgbuf context for extractor
162 *
163 * @return PLDM_SUCCESS if all buffer accesses were in-bounds,
164 * PLDM_ERROR_INVALID_DATA if the ctx parameter is invalid, or
165 * PLDM_ERROR_INVALID_LENGTH if prior accesses would have occurred beyond the
166 * bounds of the buffer.
167 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300168LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300169LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_destroy(struct pldm_msgbuf *ctx)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030170{
171 int valid;
172
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030173 valid = pldm_msgbuf_validate(ctx);
174
175 ctx->cursor = NULL;
176 ctx->remaining = 0;
177
178 return valid;
179}
180
181/**
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930182 * @brief Destroy the pldm_msgbuf instance, and check that the underlying buffer
183 * has been completely consumed without overflow
184 *
185 * @param[in] ctx - pldm_msgbuf context
186 *
187 * @return PLDM_SUCCESS if all buffer access were in-bounds and completely
188 * consume the underlying buffer. Otherwise, PLDM_ERROR_INVALID_DATA if the ctx
189 * parameter is invalid, or PLDM_ERROR_INVALID_LENGTH if prior accesses would
190 * have occurred byond the bounds of the buffer
191 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300192LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300193LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery76712f62024-05-22 15:19:00 +0930194pldm_msgbuf_destroy_consumed(struct pldm_msgbuf *ctx)
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930195{
196 int consumed;
197
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930198 consumed = pldm_msgbuf_consumed(ctx);
199
200 ctx->cursor = NULL;
201 ctx->remaining = 0;
202
203 return consumed;
204}
205
Andrew Jeffery66c77232024-04-24 11:42:02 +0930206/*
207 * Exploit the pre-processor to perform type checking by macro substitution.
208 *
209 * A C type is defined by its alignment as well as its object
210 * size, and compilers have a hammer to enforce it in the form of
211 * `-Waddress-of-packed-member`. Due to the unpacked/packed struct conflict in
212 * the libpldm public API this presents a problem: Naively attempting to use the
213 * msgbuf APIs on a member of a packed struct would yield an error.
214 *
215 * The msgbuf APIs are implemented such that data is moved through unaligned
216 * pointers in a safe way, but to mitigate `-Waddress-of-packed-member` we must
217 * make the object pointers take a trip through `void *` at its API boundary.
218 * That presents a bit too much of an opportunity to non-surgically remove your
219 * own foot, so here we set about doing something to mitigate that as well.
220 *
221 * pldm_msgbuf_extract_typecheck() exists to enforce pointer type correctness
222 * only for the purpose of object sizes, disregarding alignment. We have a few
223 * constraints that cause some headaches:
224 *
225 * 1. We have to perform the type-check before a call through a C function,
226 * as the function must take the object pointer argument as `void *`.
227 * Essentially, this constrains us to doing something with macros.
228 *
229 * 2. While libpldm is a C library, its test suite is written in C++ to take
230 * advantage of gtest.
231 *
232 * 3. Ideally we'd do something with C's `static_assert()`, however
233 * `static_assert()` is defined as void, and as we're constrained to macros,
234 * using `static_assert()` would require a statement-expression
235 *
236 * 4. Currently the project is built with `-std=c17`. CPP statement-expressions
237 * are a GNU extension. We prefer to avoid switching to `-std=gnu17` just for
238 * the purpose of enabling statement-expressions in this one instance.
239 *
240 * 5. We can achieve a conditional build error using `pldm_require_obj_type()`,
241 * however it's implemented in terms of `_Generic()`, which is not available
242 * in C++.
243 *
244 * Combined this means we need separate solutions for C and C++.
245 *
246 * For C, as we don't have statement-expressions, we need to exploit some other
247 * language feature to inject a `pldm_require_obj_type()` prior to the msgbuf
248 * API function call. We also have to take care of the fact that the call-sites
249 * may be in the context of a variable assignment for error-handling purposes.
250 * The key observation is that we can use the comma operator as a sequence point
251 * to order the type check before the API call, discarding the "result" value of
252 * the type check and yielding the return value of the API call.
253 *
254 * C++ could be less of a headache than the C as we can leverage template
255 * functions. An advantage of template functions is that while their definition
256 * is driven by instantion, the definition does not appear at the source
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +0530257 * location of the instantiation, which gives it a great leg-up over the problems
Andrew Jeffery66c77232024-04-24 11:42:02 +0930258 * we have in the C path. However, the use of the msgbuf APIs in the test suite
259 * still makes things somewhat tricky, as the call-sites in the test suite are
260 * wrapped up in EXPECT_*() gtest macros. Ideally we'd implement functions that
261 * takes both the object type and the required type as template arguments, and
262 * then define the object pointer parameter as `void *` for a call through to
263 * the appropriate msgbuf API. However, because the msgbuf API call-sites are
264 * encapsulated in gtest macros, use of commas in the template specification
265 * causes pre-processor confusion. In this way we're constrained to only one
266 * template argument per function.
267 *
268 * Implement the C++ path using template functions that take the destination
269 * object type as a template argument, while the name of the function symbols
270 * are derived from the required type. The manual implementations of these
271 * appear at the end of the header. The type safety is actually enforced
272 * by `static_assert()` this time, as we can use statements as we're not
273 * constrained to an expression in the templated function body.
274 *
275 * The invocations of pldm_msgbuf_extract_typecheck() typically result in
276 * double-evaluation of some arguments. We're not yet bothered by this for two
277 * reasons:
278 *
279 * 1. The nature of the current call-sites are such that there are no
280 * argument expressions that result in undesirable side-effects
281 *
282 * 2. It's an API internal to the libpldm implementation, and we can fix things
283 * whenever something crops up the violates the observation in 1.
284 */
285#ifdef __cplusplus
286#define pldm_msgbuf_extract_typecheck(ty, fn, dst, ...) \
287 pldm_msgbuf_typecheck_##ty<decltype(dst)>(__VA_ARGS__)
288#else
289#define pldm_msgbuf_extract_typecheck(ty, fn, dst, ...) \
290 (pldm_require_obj_type(dst, ty), fn(__VA_ARGS__))
291#endif
292
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930293/**
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030294 * @brief pldm_msgbuf extractor for a uint8_t
295 *
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +0530296 * @param[in,out] ctx - pldm_msgbuf context for extractor
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030297 * @param[out] dst - destination of extracted value
298 *
299 * @return PLDM_SUCCESS if buffer accesses were in-bounds,
300 * PLDM_ERROR_INVALID_LENGTH otherwise.
301 * PLDM_ERROR_INVALID_DATA if input a invalid ctx
302 */
Andrew Jeffery66c77232024-04-24 11:42:02 +0930303#define pldm_msgbuf_extract_uint8(ctx, dst) \
304 pldm_msgbuf_extract_typecheck(uint8_t, pldm__msgbuf_extract_uint8, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930305 dst, ctx, (void *)&(dst))
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300306LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300307LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery66c77232024-04-24 11:42:02 +0930308// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery76712f62024-05-22 15:19:00 +0930309pldm__msgbuf_extract_uint8(struct pldm_msgbuf *ctx, void *dst)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030310{
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300311 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930312 return -EINVAL;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030313 }
314
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930315 if (ctx->remaining >= (intmax_t)sizeof(uint8_t)) {
316 memcpy(dst, ctx->cursor, sizeof(uint8_t));
317 ctx->cursor++;
318 ctx->remaining -= sizeof(uint8_t);
319 return 0;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030320 }
321
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930322 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(uint8_t)) {
323 ctx->remaining -= sizeof(uint8_t);
324 }
Andrew Jeffery66c77232024-04-24 11:42:02 +0930325
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930326 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030327}
328
Andrew Jeffery66c77232024-04-24 11:42:02 +0930329#define pldm_msgbuf_extract_int8(ctx, dst) \
330 pldm_msgbuf_extract_typecheck(int8_t, pldm__msgbuf_extract_int8, dst, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930331 ctx, (void *)&(dst))
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300332LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300333LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery66c77232024-04-24 11:42:02 +0930334// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery76712f62024-05-22 15:19:00 +0930335pldm__msgbuf_extract_int8(struct pldm_msgbuf *ctx, void *dst)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030336{
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300337 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930338 return -EINVAL;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030339 }
340
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930341 if (ctx->remaining >= (intmax_t)sizeof(int8_t)) {
342 memcpy(dst, ctx->cursor, sizeof(int8_t));
343 ctx->cursor++;
344 ctx->remaining -= sizeof(int8_t);
345 return 0;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030346 }
347
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930348 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(int8_t)) {
349 ctx->remaining -= sizeof(int8_t);
350 }
351
352 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030353}
354
Andrew Jeffery66c77232024-04-24 11:42:02 +0930355#define pldm_msgbuf_extract_uint16(ctx, dst) \
356 pldm_msgbuf_extract_typecheck(uint16_t, pldm__msgbuf_extract_uint16, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930357 dst, ctx, (void *)&(dst))
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300358LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300359LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery66c77232024-04-24 11:42:02 +0930360// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery76712f62024-05-22 15:19:00 +0930361pldm__msgbuf_extract_uint16(struct pldm_msgbuf *ctx, void *dst)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030362{
363 uint16_t ldst;
364
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300365 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930366 return -EINVAL;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030367 }
368
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930369 // Check for underflow while tracking the magnitude of the buffer overflow
370 static_assert(
371 // NOLINTNEXTLINE(bugprone-sizeof-expression)
372 sizeof(ldst) < INTMAX_MAX,
373 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930374
375 if (ctx->remaining >= (intmax_t)sizeof(ldst)) {
376 // Use memcpy() to have the compiler deal with any alignment
377 // issues on the target architecture
378 memcpy(&ldst, ctx->cursor, sizeof(ldst));
379
380 // Only assign the target value once it's correctly decoded
381 ldst = le16toh(ldst);
382
383 // Allow storing to unaligned
384 memcpy(dst, &ldst, sizeof(ldst));
385
386 ctx->cursor += sizeof(ldst);
387 ctx->remaining -= sizeof(ldst);
388 return 0;
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930389 }
390
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930391 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) {
392 ctx->remaining -= sizeof(ldst);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030393 }
394
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930395 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030396}
397
Andrew Jeffery66c77232024-04-24 11:42:02 +0930398#define pldm_msgbuf_extract_int16(ctx, dst) \
399 pldm_msgbuf_extract_typecheck(int16_t, pldm__msgbuf_extract_int16, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930400 dst, ctx, (void *)&(dst))
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300401LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300402LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery66c77232024-04-24 11:42:02 +0930403// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery76712f62024-05-22 15:19:00 +0930404pldm__msgbuf_extract_int16(struct pldm_msgbuf *ctx, void *dst)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030405{
406 int16_t ldst;
407
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300408 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930409 return -EINVAL;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030410 }
411
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930412 static_assert(
413 // NOLINTNEXTLINE(bugprone-sizeof-expression)
414 sizeof(ldst) < INTMAX_MAX,
415 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930416
417 if (ctx->remaining >= (intmax_t)sizeof(ldst)) {
418 memcpy(&ldst, ctx->cursor, sizeof(ldst));
419 ldst = le16toh(ldst);
420 memcpy(dst, &ldst, sizeof(ldst));
421 ctx->cursor += sizeof(ldst);
422 ctx->remaining -= sizeof(ldst);
423 return 0;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030424 }
425
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930426 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) {
427 ctx->remaining -= sizeof(ldst);
428 }
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030429
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930430 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030431}
432
Andrew Jeffery66c77232024-04-24 11:42:02 +0930433#define pldm_msgbuf_extract_uint32(ctx, dst) \
434 pldm_msgbuf_extract_typecheck(uint32_t, pldm__msgbuf_extract_uint32, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930435 dst, ctx, (void *)&(dst))
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300436LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300437LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery66c77232024-04-24 11:42:02 +0930438// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery76712f62024-05-22 15:19:00 +0930439pldm__msgbuf_extract_uint32(struct pldm_msgbuf *ctx, void *dst)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030440{
441 uint32_t ldst;
442
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300443 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930444 return -EINVAL;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030445 }
446
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930447 static_assert(
448 // NOLINTNEXTLINE(bugprone-sizeof-expression)
449 sizeof(ldst) < INTMAX_MAX,
450 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930451
452 if (ctx->remaining >= (intmax_t)sizeof(ldst)) {
453 memcpy(&ldst, ctx->cursor, sizeof(ldst));
454 ldst = le32toh(ldst);
455 memcpy(dst, &ldst, sizeof(ldst));
456 ctx->cursor += sizeof(ldst);
457 ctx->remaining -= sizeof(ldst);
458 return 0;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030459 }
460
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930461 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) {
462 ctx->remaining -= sizeof(ldst);
463 }
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030464
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930465 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030466}
467
Andrew Jeffery66c77232024-04-24 11:42:02 +0930468#define pldm_msgbuf_extract_int32(ctx, dst) \
469 pldm_msgbuf_extract_typecheck(int32_t, pldm__msgbuf_extract_int32, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930470 dst, ctx, (void *)&(dst))
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300471LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300472LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery66c77232024-04-24 11:42:02 +0930473// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery76712f62024-05-22 15:19:00 +0930474pldm__msgbuf_extract_int32(struct pldm_msgbuf *ctx, void *dst)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030475{
476 int32_t ldst;
477
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300478 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930479 return -EINVAL;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030480 }
481
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930482 static_assert(
483 // NOLINTNEXTLINE(bugprone-sizeof-expression)
484 sizeof(ldst) < INTMAX_MAX,
485 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930486
487 if (ctx->remaining >= (intmax_t)sizeof(ldst)) {
488 memcpy(&ldst, ctx->cursor, sizeof(ldst));
489 ldst = le32toh(ldst);
490 memcpy(dst, &ldst, sizeof(ldst));
491 ctx->cursor += sizeof(ldst);
492 ctx->remaining -= sizeof(ldst);
493 return 0;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030494 }
495
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930496 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) {
497 ctx->remaining -= sizeof(ldst);
498 }
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030499
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930500 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030501}
502
Andrew Jeffery66c77232024-04-24 11:42:02 +0930503#define pldm_msgbuf_extract_real32(ctx, dst) \
504 pldm_msgbuf_extract_typecheck(real32_t, pldm__msgbuf_extract_real32, \
Andrew Jefferye5f12532024-10-01 12:18:49 +0930505 dst, ctx, (void *)&(dst))
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300506LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300507LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery66c77232024-04-24 11:42:02 +0930508// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery76712f62024-05-22 15:19:00 +0930509pldm__msgbuf_extract_real32(struct pldm_msgbuf *ctx, void *dst)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030510{
511 uint32_t ldst;
512
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930513 static_assert(sizeof(real32_t) == sizeof(ldst),
514 "Mismatched type sizes for dst and ldst");
Andrew Jeffery66c77232024-04-24 11:42:02 +0930515
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300516 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930517 return -EINVAL;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030518 }
519
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930520 static_assert(
521 // NOLINTNEXTLINE(bugprone-sizeof-expression)
522 sizeof(ldst) < INTMAX_MAX,
523 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930524
525 if (ctx->remaining >= (intmax_t)sizeof(ldst)) {
526 memcpy(&ldst, ctx->cursor, sizeof(ldst));
527 ldst = le32toh(ldst);
528 memcpy(dst, &ldst, sizeof(ldst));
529 ctx->cursor += sizeof(ldst);
530 ctx->remaining -= sizeof(ldst);
531 return 0;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030532 }
533
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930534 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(ldst)) {
535 ctx->remaining -= sizeof(ldst);
536 }
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030537
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930538 return -EOVERFLOW;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030539}
540
Andrew Jeffery66c77232024-04-24 11:42:02 +0930541/**
542 * Extract the field at the msgbuf cursor into the lvalue named by dst.
543 *
544 * @param ctx The msgbuf context object
545 * @param dst The lvalue into which the field at the msgbuf cursor should be
546 * extracted
547 *
548 * @return PLDM_SUCCESS on success, otherwise another value on error
549 */
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030550#define pldm_msgbuf_extract(ctx, dst) \
Andrew Jeffery66c77232024-04-24 11:42:02 +0930551 _Generic((dst), \
552 uint8_t: pldm__msgbuf_extract_uint8, \
553 int8_t: pldm__msgbuf_extract_int8, \
554 uint16_t: pldm__msgbuf_extract_uint16, \
555 int16_t: pldm__msgbuf_extract_int16, \
556 uint32_t: pldm__msgbuf_extract_uint32, \
557 int32_t: pldm__msgbuf_extract_int32, \
558 real32_t: pldm__msgbuf_extract_real32)(ctx, (void *)&(dst))
559
560/**
561 * Extract the field at the msgbuf cursor into the object pointed-to by dst.
562 *
563 * @param ctx The msgbuf context object
564 * @param dst The pointer to the object into which the field at the msgbuf
565 * cursor should be extracted
566 *
567 * @return PLDM_SUCCESS on success, otherwise another value on error
568 */
569#define pldm_msgbuf_extract_p(ctx, dst) \
570 _Generic((dst), \
571 uint8_t *: pldm__msgbuf_extract_uint8, \
572 int8_t *: pldm__msgbuf_extract_int8, \
573 uint16_t *: pldm__msgbuf_extract_uint16, \
574 int16_t *: pldm__msgbuf_extract_int16, \
575 uint32_t *: pldm__msgbuf_extract_uint32, \
576 int32_t *: pldm__msgbuf_extract_int32, \
577 real32_t *: pldm__msgbuf_extract_real32)(ctx, dst)
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030578
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000579/**
580 * @ref pldm_msgbuf_extract_array
581 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300582LIBPLDM_CC_NONNULL
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000583LIBPLDM_CC_WARN_UNUSED_RESULT
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300584LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery1c571442024-07-08 10:25:48 +0930585// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000586pldm__msgbuf_extract_array_void(struct pldm_msgbuf *ctx, size_t count,
587 void *dst, size_t dst_count)
Andrew Jeffery369b1212023-04-20 15:44:48 +0930588{
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300589 if (!ctx->cursor || count > dst_count) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930590 return -EINVAL;
Andrew Jeffery369b1212023-04-20 15:44:48 +0930591 }
592
593 if (!count) {
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930594 return 0;
Andrew Jeffery369b1212023-04-20 15:44:48 +0930595 }
596
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930597#if INTMAX_MAX < SIZE_MAX
598 if (count > INTMAX_MAX) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930599 return -EOVERFLOW;
Andrew Jeffery369b1212023-04-20 15:44:48 +0930600 }
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930601#endif
Andrew Jeffery369b1212023-04-20 15:44:48 +0930602
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930603 if (ctx->remaining >= (intmax_t)count) {
604 memcpy(dst, ctx->cursor, count);
605 ctx->cursor += count;
606 ctx->remaining -= (intmax_t)count;
607 return 0;
Andrew Jeffery369b1212023-04-20 15:44:48 +0930608 }
609
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930610 if (ctx->remaining >= INTMAX_MIN + (intmax_t)count) {
611 ctx->remaining -= (intmax_t)count;
612 }
Andrew Jeffery369b1212023-04-20 15:44:48 +0930613
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930614 return -EOVERFLOW;
Andrew Jeffery369b1212023-04-20 15:44:48 +0930615}
616
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000617/**
618 * @ref pldm_msgbuf_extract_array
619 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300620LIBPLDM_CC_NONNULL
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000621LIBPLDM_CC_WARN_UNUSED_RESULT
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300622LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000623pldm_msgbuf_extract_array_char(struct pldm_msgbuf *ctx, size_t count, char *dst,
624 size_t dst_count)
Andrew Jeffery1c571442024-07-08 10:25:48 +0930625{
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000626 return pldm__msgbuf_extract_array_void(ctx, count, dst, dst_count);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930627}
628
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000629/**
630 * @ref pldm_msgbuf_extract_array
631 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300632LIBPLDM_CC_NONNULL
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000633LIBPLDM_CC_WARN_UNUSED_RESULT
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300634LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000635pldm_msgbuf_extract_array_uint8(struct pldm_msgbuf *ctx, size_t count,
636 uint8_t *dst, size_t dst_count)
Andrew Jeffery1c571442024-07-08 10:25:48 +0930637{
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000638 return pldm__msgbuf_extract_array_void(ctx, count, dst, dst_count);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930639}
640
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000641/**
642 * Extract an array of data from the msgbuf instance
643 *
644 * @param ctx - The msgbuf instance from which to extract an array of data
645 * @param count - The number of array elements to extract
646 * @param dst - The array object into which elements from @p ctx should be
647 extracted
648 * @param dst_count - The maximum number of elements to place into @p dst
649 *
650 * Note that both @p count and @p dst_count can only be counted by `sizeof` for
651 * arrays where `sizeof(*dst) == 1` holds. Specifically, they count the number
652 * of array elements and _not_ the object size of the array.
653 */
654#define pldm_msgbuf_extract_array(ctx, count, dst, dst_count) \
Andrew Jeffery1c571442024-07-08 10:25:48 +0930655 _Generic((*(dst)), \
656 uint8_t: pldm_msgbuf_extract_array_uint8, \
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000657 char: pldm_msgbuf_extract_array_char)(ctx, count, dst, \
658 dst_count)
Andrew Jeffery369b1212023-04-20 15:44:48 +0930659
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300660LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300661LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint32(struct pldm_msgbuf *ctx,
662 const uint32_t src)
Thu Nguyen062c8762023-04-22 20:45:04 +0700663{
664 uint32_t val = htole32(src);
665
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930666 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930667 return -EINVAL;
Thu Nguyen062c8762023-04-22 20:45:04 +0700668 }
669
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930670 static_assert(
671 // NOLINTNEXTLINE(bugprone-sizeof-expression)
672 sizeof(src) < INTMAX_MAX,
673 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930674
675 if (ctx->remaining >= (intmax_t)sizeof(src)) {
676 memcpy(ctx->cursor, &val, sizeof(val));
677 ctx->cursor += sizeof(src);
678 ctx->remaining -= sizeof(src);
679 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +0700680 }
681
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930682 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) {
683 ctx->remaining -= sizeof(src);
684 }
Thu Nguyen062c8762023-04-22 20:45:04 +0700685
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930686 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700687}
688
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300689LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300690LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint16(struct pldm_msgbuf *ctx,
691 const uint16_t src)
Thu Nguyen062c8762023-04-22 20:45:04 +0700692{
693 uint16_t val = htole16(src);
694
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930695 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930696 return -EINVAL;
Thu Nguyen062c8762023-04-22 20:45:04 +0700697 }
698
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930699 static_assert(
700 // NOLINTNEXTLINE(bugprone-sizeof-expression)
701 sizeof(src) < INTMAX_MAX,
702 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930703
704 if (ctx->remaining >= (intmax_t)sizeof(src)) {
705 memcpy(ctx->cursor, &val, sizeof(val));
706 ctx->cursor += sizeof(src);
707 ctx->remaining -= sizeof(src);
708 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +0700709 }
710
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930711 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) {
712 ctx->remaining -= sizeof(src);
713 }
Thu Nguyen062c8762023-04-22 20:45:04 +0700714
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930715 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700716}
717
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300718LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300719LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint8(struct pldm_msgbuf *ctx,
720 const uint8_t src)
Thu Nguyen062c8762023-04-22 20:45:04 +0700721{
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930722 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930723 return -EINVAL;
Thu Nguyen062c8762023-04-22 20:45:04 +0700724 }
725
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930726 static_assert(
727 // NOLINTNEXTLINE(bugprone-sizeof-expression)
728 sizeof(src) < INTMAX_MAX,
729 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930730
731 if (ctx->remaining >= (intmax_t)sizeof(src)) {
732 memcpy(ctx->cursor, &src, sizeof(src));
733 ctx->cursor += sizeof(src);
734 ctx->remaining -= sizeof(src);
735 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +0700736 }
737
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930738 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) {
739 ctx->remaining -= sizeof(src);
740 }
Thu Nguyen062c8762023-04-22 20:45:04 +0700741
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930742 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700743}
744
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300745LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300746LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_int32(struct pldm_msgbuf *ctx,
747 const int32_t src)
Thu Nguyen062c8762023-04-22 20:45:04 +0700748{
749 int32_t val = htole32(src);
750
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930751 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930752 return -EINVAL;
Thu Nguyen062c8762023-04-22 20:45:04 +0700753 }
754
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930755 static_assert(
756 // NOLINTNEXTLINE(bugprone-sizeof-expression)
757 sizeof(src) < INTMAX_MAX,
758 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930759
760 if (ctx->remaining >= (intmax_t)sizeof(src)) {
761 memcpy(ctx->cursor, &val, sizeof(val));
762 ctx->cursor += sizeof(src);
763 ctx->remaining -= sizeof(src);
764 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +0700765 }
766
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930767 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) {
768 ctx->remaining -= sizeof(src);
769 }
Thu Nguyen062c8762023-04-22 20:45:04 +0700770
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930771 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700772}
773
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300774LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300775LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_int16(struct pldm_msgbuf *ctx,
776 const int16_t src)
Thu Nguyen062c8762023-04-22 20:45:04 +0700777{
778 int16_t val = htole16(src);
779
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930780 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930781 return -EINVAL;
Thu Nguyen062c8762023-04-22 20:45:04 +0700782 }
783
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930784 static_assert(
785 // NOLINTNEXTLINE(bugprone-sizeof-expression)
786 sizeof(src) < INTMAX_MAX,
787 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930788
789 if (ctx->remaining >= (intmax_t)sizeof(src)) {
790 memcpy(ctx->cursor, &val, sizeof(val));
791 ctx->cursor += sizeof(src);
792 ctx->remaining -= sizeof(src);
793 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +0700794 }
795
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930796 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) {
797 ctx->remaining -= sizeof(src);
798 }
Thu Nguyen062c8762023-04-22 20:45:04 +0700799
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930800 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700801}
802
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300803LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300804LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_int8(struct pldm_msgbuf *ctx,
805 const int8_t src)
Thu Nguyen062c8762023-04-22 20:45:04 +0700806{
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930807 if (!ctx->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930808 return -EINVAL;
Thu Nguyen062c8762023-04-22 20:45:04 +0700809 }
810
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930811 static_assert(
812 // NOLINTNEXTLINE(bugprone-sizeof-expression)
813 sizeof(src) < INTMAX_MAX,
814 "The following addition may not uphold the runtime assertion");
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930815
816 if (ctx->remaining >= (intmax_t)sizeof(src)) {
817 memcpy(ctx->cursor, &src, sizeof(src));
818 ctx->cursor += sizeof(src);
819 ctx->remaining -= sizeof(src);
820 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +0700821 }
822
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930823 if (ctx->remaining >= INTMAX_MIN + (intmax_t)sizeof(src)) {
824 ctx->remaining -= sizeof(src);
825 }
Thu Nguyen062c8762023-04-22 20:45:04 +0700826
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930827 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700828}
829
830#define pldm_msgbuf_insert(dst, src) \
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930831 _Generic((src), \
832 uint8_t: pldm_msgbuf_insert_uint8, \
833 int8_t: pldm_msgbuf_insert_int8, \
834 uint16_t: pldm_msgbuf_insert_uint16, \
835 int16_t: pldm_msgbuf_insert_int16, \
836 uint32_t: pldm_msgbuf_insert_uint32, \
837 int32_t: pldm_msgbuf_insert_int32)(dst, src)
Thu Nguyen062c8762023-04-22 20:45:04 +0700838
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000839/**
840 * @ref pldm_msgbuf_insert_array
841 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300842LIBPLDM_CC_NONNULL
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000843LIBPLDM_CC_WARN_UNUSED_RESULT
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300844LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery1c571442024-07-08 10:25:48 +0930845// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000846pldm__msgbuf_insert_array_void(struct pldm_msgbuf *ctx, size_t count,
847 const void *src, size_t src_count)
Thu Nguyen062c8762023-04-22 20:45:04 +0700848{
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300849 if (!ctx->cursor || count > src_count) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930850 return -EINVAL;
Thu Nguyen062c8762023-04-22 20:45:04 +0700851 }
852
853 if (!count) {
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930854 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +0700855 }
856
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930857#if INTMAX_MAX < SIZE_MAX
858 if (count > INTMAX_MAX) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930859 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700860 }
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930861#endif
Thu Nguyen062c8762023-04-22 20:45:04 +0700862
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930863 if (ctx->remaining >= (intmax_t)count) {
864 memcpy(ctx->cursor, src, count);
865 ctx->cursor += count;
866 ctx->remaining -= (intmax_t)count;
867 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +0700868 }
869
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930870 if (ctx->remaining >= INTMAX_MIN + (intmax_t)count) {
871 ctx->remaining -= (intmax_t)count;
872 }
Thu Nguyen062c8762023-04-22 20:45:04 +0700873
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930874 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700875}
876
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000877/**
878 * @ref pldm_msgbuf_insert_array
879 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300880LIBPLDM_CC_NONNULL
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000881LIBPLDM_CC_WARN_UNUSED_RESULT
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300882LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000883pldm_msgbuf_insert_array_char(struct pldm_msgbuf *ctx, size_t count,
884 const char *src, size_t src_count)
Andrew Jeffery1c571442024-07-08 10:25:48 +0930885{
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000886 return pldm__msgbuf_insert_array_void(ctx, count, src, src_count);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930887}
888
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000889/**
890 * @ref pldm_msgbuf_insert_array
891 */
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300892LIBPLDM_CC_NONNULL
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000893LIBPLDM_CC_WARN_UNUSED_RESULT
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300894LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000895pldm_msgbuf_insert_array_uint8(struct pldm_msgbuf *ctx, size_t count,
896 const uint8_t *src, size_t src_count)
Andrew Jeffery1c571442024-07-08 10:25:48 +0930897{
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000898 return pldm__msgbuf_insert_array_void(ctx, count, src, src_count);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930899}
900
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000901/**
902 * Insert an array of data into the msgbuf instance
903 *
904 * @param ctx - The msgbuf instance into which the array of data should be
905 * inserted
906 * @param count - The number of array elements to insert
907 * @param src - The array object from which elements should be inserted into
908 @p ctx
909 * @param src_count - The maximum number of elements to insert from @p src
910 *
911 * Note that both @p count and @p src_count can only be counted by `sizeof` for
912 * arrays where `sizeof(*dst) == 1` holds. Specifically, they count the number
913 * of array elements and _not_ the object size of the array.
914 */
915#define pldm_msgbuf_insert_array(dst, count, src, src_count) \
Andrew Jeffery1c571442024-07-08 10:25:48 +0930916 _Generic((*(src)), \
917 uint8_t: pldm_msgbuf_insert_array_uint8, \
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000918 char: pldm_msgbuf_insert_array_char)(dst, count, src, \
919 src_count)
Thu Nguyen062c8762023-04-22 20:45:04 +0700920
Varsha Kaverappa79393822024-08-07 00:40:13 -0500921LIBPLDM_CC_NONNULL_ARGS(1)
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300922LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_span_required(struct pldm_msgbuf *ctx,
923 size_t required,
924 void **cursor)
Thu Nguyen062c8762023-04-22 20:45:04 +0700925{
Varsha Kaverappa79393822024-08-07 00:40:13 -0500926 if (!ctx->cursor || (cursor && *cursor)) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930927 return -EINVAL;
Thu Nguyen062c8762023-04-22 20:45:04 +0700928 }
929
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930930#if INTMAX_MAX < SIZE_MAX
931 if (required > INTMAX_MAX) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930932 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700933 }
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930934#endif
Thu Nguyen062c8762023-04-22 20:45:04 +0700935
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930936 if (ctx->remaining >= (intmax_t)required) {
937 if (cursor) {
938 *cursor = ctx->cursor;
939 }
940 ctx->cursor += required;
941 ctx->remaining -= (intmax_t)required;
942 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +0700943 }
944
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930945 if (ctx->remaining >= INTMAX_MIN + (intmax_t)required) {
946 ctx->remaining -= (intmax_t)required;
Varsha Kaverappa79393822024-08-07 00:40:13 -0500947 }
Thu Nguyen062c8762023-04-22 20:45:04 +0700948
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930949 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +0700950}
951
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +0300952LIBPLDM_CC_NONNULL_ARGS(1)
Andrew Jefferycb569bc2024-09-01 09:38:09 +0300953LIBPLDM_CC_ALWAYS_INLINE int
Thu Nguyen9c83d682024-07-02 08:43:09 +0000954pldm_msgbuf_span_string_ascii(struct pldm_msgbuf *ctx, void **cursor,
955 size_t *length)
956{
957 intmax_t measured;
958
Thu Nguyen9c83d682024-07-02 08:43:09 +0000959 if (!ctx->cursor || (cursor && *cursor)) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930960 return -EINVAL;
Thu Nguyen9c83d682024-07-02 08:43:09 +0000961 }
962
963 if (ctx->remaining < 0) {
964 /* Tracking the amount of overflow gets disturbed here */
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930965 return -EOVERFLOW;
Thu Nguyen9c83d682024-07-02 08:43:09 +0000966 }
967
968 measured = (intmax_t)strnlen((const char *)ctx->cursor, ctx->remaining);
969 if (measured == ctx->remaining) {
970 /*
971 * We have hit the end of the buffer prior to the NUL terminator.
972 * Optimistically, the NUL terminator was one-beyond-the-end. Setting
973 * ctx->remaining negative ensures the `pldm_msgbuf_destroy*()` APIs also
974 * return an error.
975 */
976 ctx->remaining = -1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930977 return -EOVERFLOW;
Thu Nguyen9c83d682024-07-02 08:43:09 +0000978 }
979
980 /* Include the NUL terminator in the span length, as spans are opaque */
981 measured++;
982
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930983 if (ctx->remaining >= measured) {
984 if (cursor) {
985 *cursor = ctx->cursor;
986 }
987
988 ctx->cursor += measured;
989
990 if (length) {
991 *length = measured;
992 }
993
994 ctx->remaining -= measured;
995 return 0;
Thu Nguyen9c83d682024-07-02 08:43:09 +0000996 }
997
Andrew Jeffery4f60fb72024-09-23 13:56:44 +0930998 if (ctx->remaining >= INTMAX_MIN + measured) {
999 ctx->remaining -= measured;
Thu Nguyen9c83d682024-07-02 08:43:09 +00001000 }
1001
Andrew Jeffery4f60fb72024-09-23 13:56:44 +09301002 return -EOVERFLOW;
Thu Nguyen9c83d682024-07-02 08:43:09 +00001003}
1004
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +03001005LIBPLDM_CC_NONNULL_ARGS(1)
Andrew Jefferycb569bc2024-09-01 09:38:09 +03001006LIBPLDM_CC_ALWAYS_INLINE int
Thu Nguyen15237782024-07-02 09:30:41 +00001007pldm_msgbuf_span_string_utf16(struct pldm_msgbuf *ctx, void **cursor,
1008 size_t *length)
1009{
1010 static const char16_t term = 0;
1011 ptrdiff_t measured;
1012 void *end;
1013
Thu Nguyen15237782024-07-02 09:30:41 +00001014 if (!ctx->cursor || (cursor && *cursor)) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301015 return -EINVAL;
Thu Nguyen15237782024-07-02 09:30:41 +00001016 }
1017
1018 if (ctx->remaining < 0) {
1019 /* Tracking the amount of overflow gets disturbed here */
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301020 return -EOVERFLOW;
Thu Nguyen15237782024-07-02 09:30:41 +00001021 }
1022
1023 /*
1024 * Avoid tripping up on UTF16-LE: We may have consecutive NUL _bytes_ that do
1025 * not form a UTF16 NUL _code-point_ due to alignment with respect to the
1026 * start of the string
1027 */
Andrew Jeffery2b440d42024-07-25 10:36:08 +09301028 end = ctx->cursor;
Thu Nguyen15237782024-07-02 09:30:41 +00001029 do {
1030 if (end != ctx->cursor) {
1031 /*
1032 * If we've looped we've found a relatively-unaligned NUL code-point.
1033 * Scan again from a relatively-aligned start point.
1034 */
1035 end = (char *)end + 1;
1036 }
1037 measured = (char *)end - (char *)ctx->cursor;
Andrew Jeffery2b440d42024-07-25 10:36:08 +09301038 end = memmem(end, ctx->remaining - measured, &term,
1039 sizeof(term));
Thu Nguyen15237782024-07-02 09:30:41 +00001040 } while (end && ((uintptr_t)end & 1) != ((uintptr_t)ctx->cursor & 1));
1041
1042 if (!end) {
1043 /*
1044 * Optimistically, the last required pattern byte was one beyond the end of
1045 * the buffer. Setting ctx->remaining negative ensures the
1046 * `pldm_msgbuf_destroy*()` APIs also return an error.
1047 */
1048 ctx->remaining = -1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301049 return -EOVERFLOW;
Thu Nguyen15237782024-07-02 09:30:41 +00001050 }
1051
1052 end = (char *)end + sizeof(char16_t);
1053 measured = (char *)end - (char *)ctx->cursor;
1054
1055#if INTMAX_MAX < PTRDIFF_MAX
1056 if (measured >= INTMAX_MAX) {
1057 return pldm_msgbuf_status(ctx, EOVERFLOW);
1058 }
1059#endif
1060
Andrew Jeffery4f60fb72024-09-23 13:56:44 +09301061 if (ctx->remaining >= (intmax_t)measured) {
1062 if (cursor) {
1063 *cursor = ctx->cursor;
1064 }
1065
1066 ctx->cursor += measured;
1067
1068 if (length) {
1069 *length = (size_t)measured;
1070 }
1071
1072 ctx->remaining -= (intmax_t)measured;
1073 return 0;
Thu Nguyen15237782024-07-02 09:30:41 +00001074 }
1075
Andrew Jeffery4f60fb72024-09-23 13:56:44 +09301076 if (ctx->remaining >= INTMAX_MIN + (intmax_t)measured) {
1077 ctx->remaining -= (intmax_t)measured;
Thu Nguyen15237782024-07-02 09:30:41 +00001078 }
1079
Andrew Jeffery4f60fb72024-09-23 13:56:44 +09301080 return -EOVERFLOW;
Thu Nguyen15237782024-07-02 09:30:41 +00001081}
1082
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +03001083LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +03001084LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery76712f62024-05-22 15:19:00 +09301085pldm_msgbuf_span_remaining(struct pldm_msgbuf *ctx, void **cursor, size_t *len)
Thu Nguyen062c8762023-04-22 20:45:04 +07001086{
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +03001087 if (!ctx->cursor || *cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301088 return -EINVAL;
Thu Nguyen062c8762023-04-22 20:45:04 +07001089 }
1090
Thu Nguyen062c8762023-04-22 20:45:04 +07001091 if (ctx->remaining < 0) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301092 return -EOVERFLOW;
Thu Nguyen062c8762023-04-22 20:45:04 +07001093 }
1094
1095 *cursor = ctx->cursor;
1096 ctx->cursor += ctx->remaining;
1097 *len = ctx->remaining;
1098 ctx->remaining = 0;
1099
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301100 return 0;
Thu Nguyen062c8762023-04-22 20:45:04 +07001101}
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001102
1103/**
1104 * @brief pldm_msgbuf copy data between two msg buffers
1105 *
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +05301106 * @param[in,out] src - pldm_msgbuf for source from where value should be copied
1107 * @param[in,out] dst - destination of copy from source
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001108 * @param[in] size - size of data to be copied
1109 * @param[in] description - description of data copied
1110 *
1111 * @return PLDM_SUCCESS if buffer accesses were in-bounds,
1112 * PLDM_ERROR_INVALID_LENGTH otherwise.
1113 * PLDM_ERROR_INVALID_DATA if input is invalid
1114 */
1115#define pldm_msgbuf_copy(dst, src, type, name) \
1116 pldm__msgbuf_copy(dst, src, sizeof(type), #name)
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +03001117LIBPLDM_CC_NONNULL
Andrew Jefferycb569bc2024-09-01 09:38:09 +03001118LIBPLDM_CC_ALWAYS_INLINE int
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001119// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
Andrew Jeffery76712f62024-05-22 15:19:00 +09301120pldm__msgbuf_copy(struct pldm_msgbuf *dst, struct pldm_msgbuf *src, size_t size,
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +03001121 const char *description LIBPLDM_CC_UNUSED)
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001122{
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +03001123 if (!src->cursor || !dst->cursor) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301124 return -EINVAL;
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001125 }
1126
1127#if INTMAX_MAX < SIZE_MAX
1128 if (size > INTMAX_MAX) {
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301129 return -EOVERFLOW;
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001130 }
1131#endif
1132
Andrew Jeffery4f60fb72024-09-23 13:56:44 +09301133 if (src->remaining >= (intmax_t)size &&
1134 dst->remaining >= (intmax_t)size) {
1135 memcpy(dst->cursor, src->cursor, size);
1136 src->cursor += size;
1137 src->remaining -= (intmax_t)size;
1138 dst->cursor += size;
1139 dst->remaining -= (intmax_t)size;
1140 return 0;
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001141 }
1142
Andrew Jeffery4f60fb72024-09-23 13:56:44 +09301143 if (src->remaining >= INTMAX_MIN + (intmax_t)size) {
1144 src->remaining -= (intmax_t)size;
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001145 }
1146
Andrew Jeffery4f60fb72024-09-23 13:56:44 +09301147 if (dst->remaining >= INTMAX_MIN + (intmax_t)size) {
1148 dst->remaining -= (intmax_t)size;
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001149 }
1150
Andrew Jeffery4f60fb72024-09-23 13:56:44 +09301151 return -EOVERFLOW;
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001152}
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301153
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +03001154LIBPLDM_CC_NONNULL
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001155LIBPLDM_CC_WARN_UNUSED_RESULT
Andrew Jefferycb569bc2024-09-01 09:38:09 +03001156LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery8b879602024-07-08 12:50:19 +09301157pldm_msgbuf_copy_string_ascii(struct pldm_msgbuf *dst, struct pldm_msgbuf *src)
1158{
1159 void *ascii = NULL;
1160 size_t len = 0;
1161 int rc;
1162
1163 rc = pldm_msgbuf_span_string_ascii(src, &ascii, &len);
1164 if (rc < 0) {
1165 return rc;
1166 }
1167
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001168 return pldm__msgbuf_insert_array_void(dst, len, ascii, len);
Andrew Jeffery8b879602024-07-08 12:50:19 +09301169}
1170
Andrew Jeffery90bbe6c2024-09-01 13:02:02 +03001171LIBPLDM_CC_NONNULL
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001172LIBPLDM_CC_WARN_UNUSED_RESULT
Andrew Jefferycb569bc2024-09-01 09:38:09 +03001173LIBPLDM_CC_ALWAYS_INLINE int
Andrew Jeffery56f73f92024-07-08 12:50:28 +09301174pldm_msgbuf_copy_string_utf16(struct pldm_msgbuf *dst, struct pldm_msgbuf *src)
1175{
1176 void *utf16 = NULL;
1177 size_t len = 0;
1178 int rc;
1179
1180 rc = pldm_msgbuf_span_string_utf16(src, &utf16, &len);
1181 if (rc < 0) {
1182 return rc;
1183 }
1184
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001185 return pldm__msgbuf_insert_array_void(dst, len, utf16, len);
Andrew Jeffery56f73f92024-07-08 12:50:28 +09301186}
1187
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10301188#ifdef __cplusplus
1189}
1190#endif
1191
Andrew Jeffery66c77232024-04-24 11:42:02 +09301192#ifdef __cplusplus
1193#include <type_traits>
1194
1195template <typename T>
1196static inline int pldm_msgbuf_typecheck_uint8_t(struct pldm_msgbuf *ctx,
1197 void *buf)
1198{
Andrew Jefferye5f12532024-10-01 12:18:49 +09301199 static_assert(std::is_same<uint8_t, T>::value);
Andrew Jeffery66c77232024-04-24 11:42:02 +09301200 return pldm__msgbuf_extract_uint8(ctx, buf);
1201}
1202
1203template <typename T>
1204static inline int pldm_msgbuf_typecheck_int8_t(struct pldm_msgbuf *ctx,
1205 void *buf)
1206{
Andrew Jefferye5f12532024-10-01 12:18:49 +09301207 static_assert(std::is_same<int8_t, T>::value);
Andrew Jeffery66c77232024-04-24 11:42:02 +09301208 return pldm__msgbuf_extract_int8(ctx, buf);
1209}
1210
1211template <typename T>
1212static inline int pldm_msgbuf_typecheck_uint16_t(struct pldm_msgbuf *ctx,
1213 void *buf)
1214{
Andrew Jefferye5f12532024-10-01 12:18:49 +09301215 static_assert(std::is_same<uint16_t, T>::value);
Andrew Jeffery66c77232024-04-24 11:42:02 +09301216 return pldm__msgbuf_extract_uint16(ctx, buf);
1217}
1218
1219template <typename T>
1220static inline int pldm_msgbuf_typecheck_int16_t(struct pldm_msgbuf *ctx,
1221 void *buf)
1222{
Andrew Jefferye5f12532024-10-01 12:18:49 +09301223 static_assert(std::is_same<int16_t, T>::value);
Andrew Jeffery66c77232024-04-24 11:42:02 +09301224 return pldm__msgbuf_extract_int16(ctx, buf);
1225}
1226
1227template <typename T>
1228static inline int pldm_msgbuf_typecheck_uint32_t(struct pldm_msgbuf *ctx,
1229 void *buf)
1230{
Andrew Jefferye5f12532024-10-01 12:18:49 +09301231 static_assert(std::is_same<uint32_t, T>::value);
Andrew Jeffery66c77232024-04-24 11:42:02 +09301232 return pldm__msgbuf_extract_uint32(ctx, buf);
1233}
1234
1235template <typename T>
1236static inline int pldm_msgbuf_typecheck_int32_t(struct pldm_msgbuf *ctx,
1237 void *buf)
1238{
Andrew Jefferye5f12532024-10-01 12:18:49 +09301239 static_assert(std::is_same<int32_t, T>::value);
Andrew Jeffery66c77232024-04-24 11:42:02 +09301240 return pldm__msgbuf_extract_int32(ctx, buf);
1241}
1242
1243template <typename T>
1244static inline int pldm_msgbuf_typecheck_real32_t(struct pldm_msgbuf *ctx,
1245 void *buf)
1246{
Andrew Jefferye5f12532024-10-01 12:18:49 +09301247 static_assert(std::is_same<real32_t, T>::value);
Andrew Jeffery66c77232024-04-24 11:42:02 +09301248 return pldm__msgbuf_extract_real32(ctx, buf);
1249}
1250#endif
1251
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10301252#endif /* BUF_H */