aboutsummaryrefslogtreecommitdiff
path: root/src/nc-util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/nc-util.h')
-rw-r--r--src/nc-util.h78
1 files changed, 74 insertions, 4 deletions
diff --git a/src/nc-util.h b/src/nc-util.h
index d0afe28..2ddfd3f 100644
--- a/src/nc-util.h
+++ b/src/nc-util.h
@@ -102,6 +102,26 @@ typedef struct read_only_memory_span_struct
uint32_t size;
} cspan_t;
+static _nc_fn_inline int ncSpanIsValid(span_t span)
+{
+ return span.data != NULL;
+}
+
+static _nc_fn_inline int ncSpanIsValidC(cspan_t span)
+{
+ return span.data != NULL;
+}
+
+static _nc_fn_inline int ncSpanIsValidRange(span_t span, uint32_t offset, uint32_t size)
+{
+ return ncSpanIsValid(span) && offset + size <= span.size;
+}
+
+static _nc_fn_inline int ncSpanIsValidRangeC(cspan_t span, uint32_t offset, uint32_t size)
+{
+ return ncSpanIsValidC(span) && offset + size <= span.size;
+}
+
static _nc_fn_inline void ncSpanInitC(cspan_t* span, const uint8_t* data, uint32_t size)
{
span->data = data;
@@ -114,9 +134,25 @@ static _nc_fn_inline void ncSpanInit(span_t* span, uint8_t* data, uint32_t size)
span->size = size;
}
+static _nc_fn_inline const uint8_t* ncSpanGetOffsetC(cspan_t span, uint32_t offset)
+{
+ DEBUG_ASSERT2(ncSpanIsValidC(span), "Expected span to be non-null");
+ DEBUG_ASSERT2(offset < span.size, "Expected offset to be less than span size");
+
+ return span.data + offset;
+}
+
+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");
+
+ return span.data + offset;
+}
+
static _nc_fn_inline void ncSpanWrite(span_t span, uint32_t offset, const uint8_t* data, uint32_t size)
{
- DEBUG_ASSERT2(span.data != NULL, "Expected span to be non-null")
+ DEBUG_ASSERT2(ncSpanIsValid(span), "Expected span to be non-null")
DEBUG_ASSERT2(data != NULL, "Expected data to be non-null")
DEBUG_ASSERT2(offset + size <= span.size, "Expected offset + size to be less than span size")
@@ -126,7 +162,7 @@ static _nc_fn_inline void ncSpanWrite(span_t span, uint32_t offset, const uint8_
static _nc_fn_inline void ncSpanAppend(span_t span, uint32_t* offset, const uint8_t* data, uint32_t size)
{
- DEBUG_ASSERT2(span.data != NULL, "Expected span to be non-null")
+ DEBUG_ASSERT2(ncSpanIsValid(span), "Expected span to be non-null")
DEBUG_ASSERT2(offset != NULL, "Expected offset to be non-null")
DEBUG_ASSERT2(data != NULL, "Expected data to be non-null")
DEBUG_ASSERT2(*offset + size <= span.size, "Expected offset + size to be less than span size")
@@ -142,7 +178,7 @@ static _nc_fn_inline span_t ncSpanSlice(span_t span, uint32_t offset, uint32_t s
{
span_t slice;
- DEBUG_ASSERT2(span.data != NULL, "Expected span to be non-null");
+ DEBUG_ASSERT2(ncSpanIsValid(span), "Expected span to be non-null");
DEBUG_ASSERT2(offset + size <= span.size, "Expected offset + size to be less than span size")
/* Initialize slice, offset input data by the specified offset */
@@ -155,7 +191,7 @@ static _nc_fn_inline cspan_t ncSpanSliceC(cspan_t span, uint32_t offset, uint32_
{
cspan_t slice;
- DEBUG_ASSERT2(span.data != NULL, "Expected span to be non-null");
+ DEBUG_ASSERT2(ncSpanIsValidC(span), "Expected span to be non-null");
DEBUG_ASSERT2(offset + size <= span.size, "Expected offset + size to be less than span size")
/* Initialize slice, offset input data by the specified offset */
@@ -164,5 +200,39 @@ static _nc_fn_inline cspan_t ncSpanSliceC(cspan_t span, uint32_t offset, uint32_
return slice;
}
+static _nc_fn_inline void ncSpanCopyC(cspan_t src, span_t dest)
+{
+ DEBUG_ASSERT2(ncSpanIsValidC(src), "Expected span to be non-null");
+ DEBUG_ASSERT2(ncSpanIsValid(dest), "Expected offset + size to be less than span size");
+ DEBUG_ASSERT2(dest.size >= src.size, "Output buffer too small. Overrun detected");
+
+ /* Copy data to span */
+ MEMMOV(dest.data, src.data, src.size);
+}
+
+static _nc_fn_inline void ncSpanCopy(span_t src, span_t dest)
+{
+ cspan_t csrc;
+
+ ncSpanInitC(&csrc, src.data, src.size);
+ ncSpanCopyC(csrc, dest);
+}
+
+static _nc_fn_inline void ncSpanReadC(cspan_t src, uint8_t* dest, uint32_t size)
+{
+ span_t dsts;
+
+ ncSpanInit(&dsts, dest, size);
+ ncSpanCopyC(src, dsts);
+}
+
+static _nc_fn_inline void ncSpanRead(span_t src, uint8_t* dest, uint32_t size)
+{
+ cspan_t srcs;
+
+ ncSpanInitC(&srcs, src.data, src.size);
+ ncSpanReadC(srcs, dest, size);
+}
+
#endif /* !_NC_UTIL_H */ \ No newline at end of file