1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/*
* Copyright (c) 2024 Vaughn Nugent
*
* Package: noscrypt
* File: nc-util.h
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with noscrypt. If not, see http://www.gnu.org/licenses/.
*/
#pragma once
#ifndef _NC_UTIL_H
#define _NC_UTIL_H
#include <platform.h>
/* NULL */
#ifndef NULL
#define NULL ((void*)0)
#endif /* !NULL */
#ifdef DEBUG
/* Must include assert.h for assertions */
#include <assert.h>
#define DEBUG_ASSERT(x) assert(x);
#define DEBUG_ASSERT2(x, message) assert(x && message);
/*
* Compiler enabled static assertion keywords are
* only available in C11 and later. Later versions
* have macros built-in from assert.h so we can use
* the static_assert macro directly.
*
* Static assertions are only used for testing such as
* sanity checks and this library targets the c89 standard
* so static_assret very likely will not be available.
*/
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define STATIC_ASSERT(x, m) static_assert(x, m);
#elif !defined(STATIC_ASSERT)
#define STATIC_ASSERT(x, m)
#pragma message("Static assertions are not supported by this language version")
#endif
#else
#define DEBUG_ASSERT(x)
#define DEBUG_ASSERT2(x, message)
#define STATIC_ASSERT(x, m)
#endif
#include <stdint.h>
#if SIZE_MAX < UINT32_MAX
#define _overflow_check(x) if(x > SIZE_MAX) return CSTATUS_FAIL;
#else
#define _overflow_check(x)
#endif
#ifdef NC_EXTREME_COMPAT
void _nc_memmove(void* dst, const void* src, uint32_t size)
{
uint32_t i;
for (i = 0; i < size; i++)
{
((uint8_t*)dst)[i] = ((uint8_t*)src)[i];
}
}
#define MEMMOV _nc_memmove
#else
/* Include string for memmove */
#include <string.h>
#define MEMMOV(dst, src, size) memmove(dst, src, size)
#endif /* NC_EXTREME_COMPAT */
typedef struct memory_span_struct
{
uint8_t* data;
uint32_t size;
} span_t;
typedef struct read_only_memory_span_struct
{
const uint8_t* data;
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;
span->size = size;
}
static _nc_fn_inline void ncSpanInit(span_t* span, uint8_t* data, uint32_t size)
{
span->data = data;
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(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")
/* Copy data to span */
MEMMOV(span.data + offset, data, size);
}
static _nc_fn_inline void ncSpanAppend(span_t span, uint32_t* offset, const uint8_t* data, uint32_t size)
{
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")
/* Copy data to span */
MEMMOV(span.data + *offset, data, size);
/* Increment offset */
*offset += size;
}
static _nc_fn_inline span_t ncSpanSlice(span_t span, uint32_t offset, uint32_t size)
{
span_t slice;
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 */
ncSpanInit(&slice, span.data + offset, size);
return slice;
}
static _nc_fn_inline cspan_t ncSpanSliceC(cspan_t span, uint32_t offset, uint32_t size)
{
cspan_t slice;
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 */
ncSpanInitC(&slice, span.data + offset, size);
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 */
|