msgbuf: pldm_msgbuf_peek_remaining and pldm_msgbuf_skip
These can be used to populate a msgbuf by passing a buffer/length
to an external function that doesn't use msgbuf.
pldm_msgbuf_peek_remaining() provides a buffer and length to pass,
and pldm_msgbuf_skip() is called after the data has been
written into that buffer, to update the used length.
Change-Id: I55012e2e724842665f5317e252c9e3ae81803936
Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
diff --git a/src/msgbuf.h b/src/msgbuf.h
index a176891..63cd827 100644
--- a/src/msgbuf.h
+++ b/src/msgbuf.h
@@ -1130,6 +1130,50 @@
return 0;
}
+LIBPLDM_CC_NONNULL
+LIBPLDM_CC_ALWAYS_INLINE int
+pldm_msgbuf_peek_remaining(struct pldm_msgbuf *ctx, void **cursor, size_t *len)
+{
+ if (!ctx->cursor || *cursor) {
+ return -EINVAL;
+ }
+
+ if (ctx->remaining < 0) {
+ return -EOVERFLOW;
+ }
+
+ *cursor = ctx->cursor;
+ *len = ctx->remaining;
+
+ return 0;
+}
+
+LIBPLDM_CC_NONNULL
+LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_skip(struct pldm_msgbuf *ctx,
+ size_t count)
+{
+ if (!ctx->cursor) {
+ return -EINVAL;
+ }
+
+#if INTMAX_MAX < SIZE_MAX
+ if (count > INTMAX_MAX) {
+ return -EOVERFLOW;
+ }
+#endif
+
+ if (ctx->remaining < INTMAX_MIN + (intmax_t)count) {
+ return -EOVERFLOW;
+ }
+ ctx->remaining -= (intmax_t)count;
+ if (ctx->remaining < 0) {
+ return -EOVERFLOW;
+ }
+ ctx->cursor += count;
+
+ return 0;
+}
+
/**
* Return the number of bytes used in a msgbuf instance.
*