aboutsummaryrefslogtreecommitdiff
path: root/src/nc-util.h
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-07-26 23:37:15 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-07-26 23:37:15 -0400
commit07de078a3b5b7b0043d9f81bb5a9e750a3a0c7c1 (patch)
treebb67c67e4d98bda21c6b2613549a82ee13e59b53 /src/nc-util.h
parent54f520e4bfc0fe23e2719d44b09739aa8709451c (diff)
refactor: Span invasion, checks and fix some evp api
Diffstat (limited to 'src/nc-util.h')
-rw-r--r--src/nc-util.h37
1 files changed, 34 insertions, 3 deletions
diff --git a/src/nc-util.h b/src/nc-util.h
index 2ddfd3f..a248578 100644
--- a/src/nc-util.h
+++ b/src/nc-util.h
@@ -90,6 +90,10 @@
#endif /* NC_EXTREME_COMPAT */
+#ifndef EMPTY_SPANS
+ #define EMPTY_SPANS 1
+#endif
+
typedef struct memory_span_struct
{
uint8_t* data;
@@ -136,6 +140,20 @@ static _nc_fn_inline void ncSpanInit(span_t* span, uint8_t* data, uint32_t size)
static _nc_fn_inline const uint8_t* ncSpanGetOffsetC(cspan_t span, uint32_t offset)
{
+
+#if EMPTY_SPANS
+
+ /*
+ * Allow passing null pointers for empty spans, if enabled,
+ * otherwise debug guards will catch empty spans
+ */
+ if (span.size == 0 && offset == 0)
+ {
+ return NULL;
+ }
+
+#endif /* !EMPTY_SPANS */
+
DEBUG_ASSERT2(ncSpanIsValidC(span), "Expected span to be non-null");
DEBUG_ASSERT2(offset < span.size, "Expected offset to be less than span size");
@@ -144,10 +162,23 @@ static _nc_fn_inline const uint8_t* ncSpanGetOffsetC(cspan_t span, uint32_t offs
static _nc_fn_inline uint8_t* ncSpanGetOffset(span_t span, uint32_t offset)
{
- DEBUG_ASSERT2(ncSpanIsValid(span), "Expected span to be non-null");
- DEBUG_ASSERT2(offset < span.size, "Expected offset to be less than span size");
+ cspan_t cspan;
+ ncSpanInitC(&cspan, span.data, span.size);
+ return (uint8_t*)ncSpanGetOffsetC(cspan, offset);
+}
- return span.data + offset;
+static _nc_fn_inline uint32_t ncSpanGetSizeC(cspan_t span)
+{
+ return ncSpanIsValidC(span)
+ ? span.size
+ : 0;
+}
+
+static _nc_fn_inline uint32_t ncSpanGetSize(span_t span)
+{
+ return ncSpanIsValid(span)
+ ? span.size
+ : 0;
}
static _nc_fn_inline void ncSpanWrite(span_t span, uint32_t offset, const uint8_t* data, uint32_t size)