blob: 25a92f42d773f6bbe8e0c00202dfdcb8f2400060 [file] [log] [blame]
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001/*==============================================================================
Laurence Lundbladeb239c652024-05-26 08:07:05 -07002 * Copyright (c) 2016-2018, The Linux Foundation.
3 * Copyright (c) 2018-2024, Laurence Lundblade.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
15 * * Neither the name of The Linux Foundation nor the names of its
16 * contributors, nor the name "Laurence Lundblade" may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * ========================================================================= */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080032
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080033
Laurence Lundblade624405d2018-09-18 20:10:47 -070034
Laurence Lundbladeee851742020-01-08 08:37:05 -080035/*=============================================================================
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070036 FILE: UsefulBuf.c
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080037
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070038 DESCRIPTION: General purpose input and output buffers
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080039
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070040 EDIT HISTORY FOR FILE:
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080041
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070042 This section contains comments describing changes made to the module.
43 Notice that changes are listed in reverse chronological order.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080044
Laurence Lundbladeee851742020-01-08 08:37:05 -080045 when who what, where, why
46 -------- ---- ---------------------------------------------------
Laurence Lundbladee2226742024-08-16 10:50:23 -070047 08/08/2024 llundblade Add UsefulOutBuf_SubString().
Laurence Lundbladeb239c652024-05-26 08:07:05 -070048 21/05/2024 llundblade Comment formatting and some code tidiness.
Laurence Lundbladede2e1502024-08-26 11:37:05 -070049 1/7/2024 llundblade Add UsefulInputBuf_Compare().
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -070050 28/02/2022 llundblade Rearrange UsefulOutBuf_Compare().
Laurence Lundbladed6e13022023-11-26 10:14:02 -070051 19/11/2023 llundblade Add UsefulOutBuf_GetOutput().
52 19/11/2023 llundblade Add UsefulOutBuf_Swap().
53 19/11/2023 llundblade Add UsefulOutBuf_Compare().
Laurence Lundblade5a6fec52022-12-25 11:28:43 -070054 19/12/2022 llundblade Don't pass NULL to memmove when adding empty data.
55 4/11/2022 llundblade Add GetOutPlace and Advance to UsefulOutBuf
Laurence Lundbladed6e13022023-11-26 10:14:02 -070056 3/6/2021 mcr/llundblade Fix warnings related to --Wcast-qual
Laurence Lundblade06350ea2020-01-27 19:32:40 -080057 01/28/2020 llundblade Refine integer signedness to quiet static analysis.
58 01/08/2020 llundblade Documentation corrections & improved code formatting.
Laurence Lundbladeee851742020-01-08 08:37:05 -080059 11/08/2019 llundblade Re check pointer math and update comments
60 3/6/2019 llundblade Add UsefulBuf_IsValue()
61 09/07/17 llundbla Fix critical bug in UsefulBuf_Find() -- a read off
62 the end of memory when the bytes to find is longer
63 than the bytes to search.
64 06/27/17 llundbla Fix UsefulBuf_Compare() bug. Only affected comparison
65 for < or > for unequal length buffers. Added
66 UsefulBuf_Set() function.
67 05/30/17 llundbla Functions for NULL UsefulBufs and const / unconst
68 11/13/16 llundbla Initial Version.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080069
Laurence Lundbladeee851742020-01-08 08:37:05 -080070 ============================================================================*/
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070071
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070072#include "UsefulBuf.h"
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070073
Laurence Lundbladeb239c652024-05-26 08:07:05 -070074/* used to catch use of uninitialized or corrupted UsefulOutBuf */
Laurence Lundbladeee851742020-01-08 08:37:05 -080075#define USEFUL_OUT_BUF_MAGIC (0x0B0F)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070076
Laurence Lundblade041ffa52018-10-07 11:43:51 +070077
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053078/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -070079 * Public function -- see UsefulBuf.h
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053080 */
Laurence Lundblade041ffa52018-10-07 11:43:51 +070081UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src)
82{
Laurence Lundbladeb239c652024-05-26 08:07:05 -070083 /* Do this with subtraction so it doesn't give an erroneous
84 * result if uOffset + Src.len overflows. Right side is equivalent to
85 * uOffset + Src.len > Dest.len
86 */
87 if(uOffset > Dest.len || Src.len > Dest.len - uOffset) {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +080088 return NULLUsefulBufC;
89 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080090
Laurence Lundblade570fab52018-10-13 18:28:27 +080091 memcpy((uint8_t *)Dest.ptr + uOffset, Src.ptr, Src.len);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080092
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -080093 return (UsefulBufC){Dest.ptr, Src.len + uOffset};
Laurence Lundblade041ffa52018-10-07 11:43:51 +070094}
95
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053096
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070097/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -070098 * Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070099 */
100int UsefulBuf_Compare(const UsefulBufC UB1, const UsefulBufC UB2)
101{
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700102 /* Use comparisons rather than subtracting lengths to
103 * return an int instead of a size_t
104 */
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700105 if(UB1.len < UB2.len) {
106 return -1;
107 } else if (UB1.len > UB2.len) {
108 return 1;
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700109 } /* else UB1.len == UB2.len */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800110
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700111 return memcmp(UB1.ptr, UB2.ptr, UB1.len);
112}
113
114
Laurence Lundbladed5e101e2019-03-06 17:23:18 -0800115/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700116 * Public function -- see UsefulBuf.h
Laurence Lundbladed5e101e2019-03-06 17:23:18 -0800117 */
118size_t UsefulBuf_IsValue(const UsefulBufC UB, uint8_t uValue)
119{
120 if(UsefulBuf_IsNULLOrEmptyC(UB)) {
121 /* Not a match */
122 return 0;
123 }
124
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800125 const uint8_t * const pEnd = (const uint8_t *)UB.ptr + UB.len;
Laurence Lundbladed5e101e2019-03-06 17:23:18 -0800126 for(const uint8_t *p = UB.ptr; p < pEnd; p++) {
127 if(*p != uValue) {
128 /* Byte didn't match */
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700129 /* Cast from signed to unsigned. Safe because the loop increments.*/
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800130 return (size_t)(p - (const uint8_t *)UB.ptr);
Laurence Lundbladed5e101e2019-03-06 17:23:18 -0800131 }
132 }
133
134 /* Success. All bytes matched */
135 return SIZE_MAX;
136}
137
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700138
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700139/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700140 * Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700141 */
142size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind)
143{
144 if(BytesToSearch.len < BytesToFind.len) {
145 return SIZE_MAX;
146 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800147
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700148 for(size_t uPos = 0; uPos <= BytesToSearch.len - BytesToFind.len; uPos++) {
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700149 UsefulBufC SearchNext;
150
151 SearchNext.ptr = ((const uint8_t *)BytesToSearch.ptr) + uPos;
152 SearchNext.len = BytesToFind.len;
153 if(!UsefulBuf_Compare(SearchNext, BytesToFind)) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700154 return uPos;
155 }
156 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800157
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700158 return SIZE_MAX;
159}
160
161
162/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700163 * Public function -- see UsefulBuf.h
164 *
165 * Code Reviewers: THIS FUNCTION DOES POINTER MATH
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700166 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100167void UsefulOutBuf_Init(UsefulOutBuf *pMe, UsefulBuf Storage)
Laurence Lundblade2296db52018-09-14 18:08:39 -0700168{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100169 pMe->magic = USEFUL_OUT_BUF_MAGIC;
170 UsefulOutBuf_Reset(pMe);
171 pMe->UB = Storage;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade2296db52018-09-14 18:08:39 -0700173#if 0
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700174 /* This check is off by default.
175 *
176 * The following check fails on ThreadX
177 *
178 * Sanity check on the pointer and size to be sure we are not
179 * passed a buffer that goes off the end of the address space.
180 * Given this test, we know that all unsigned lengths less than
181 * me->size are valid and won't wrap in any pointer additions
182 * based off of pStorage in the rest of this code.
183 */
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530184 const uintptr_t ptrM = UINTPTR_MAX - Storage.len;
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700185 if(Storage.ptr && (uintptr_t)Storage.ptr > ptrM) /* Check #0 */
Laurence Lundblade2296db52018-09-14 18:08:39 -0700186 me->err = 1;
187#endif
188}
189
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700190
191
192/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700193 * Public function -- see UsefulBuf.h
194 *
195 * The core of UsefulOutBuf -- put some bytes in the buffer without writing off
196 * the end of it.
197 *
198 * Code Reviewers: THIS FUNCTION DOES POINTER MATH
199 *
200 * This function inserts the source buffer, NewData, into the destination
201 * buffer, me->UB.ptr.
202 *
203 * Destination is represented as:
204 * me->UB.ptr -- start of the buffer
205 * me->UB.len -- size of the buffer UB.ptr
206 * me->data_len -- length of value data in UB
207 *
208 * Source is data:
209 * NewData.ptr -- start of source buffer
210 * NewData.len -- length of source buffer
211 *
212 * Insertion point:
213 * uInsertionPos.
214 *
215 * Steps:
216 *
217 * 0. Corruption checks on UsefulOutBuf
218 *
219 * 1. Figure out if the new data will fit or not
220 *
221 * 2. Is insertion position in the range of valid data?
222 *
223 * 3. If insertion point is not at the end, slide data to the right of the
224 * insertion point to the right
225 *
226 * 4. Put the new data in at the insertion position.
227 *
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700228 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100229void UsefulOutBuf_InsertUsefulBuf(UsefulOutBuf *pMe, UsefulBufC NewData, size_t uInsertionPos)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700230{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100231 if(pMe->err) {
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700232 /* Already in error state. */
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700233 return;
234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700236 /* 0. Sanity check the UsefulOutBuf structure
237 * A "counter measure". If magic number is not the right number it
238 * probably means pMe was not initialized or it was corrupted. Attackers
239 * can defeat this, but it is a hurdle and does good with very
240 * little code.
241 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100242 if(pMe->magic != USEFUL_OUT_BUF_MAGIC) {
243 pMe->err = 1;
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700244 return; /* Magic number is wrong due to uninitalization or corrption */
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700245 }
246
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700247 /* Make sure valid data is less than buffer size. This would only occur
248 * if there was corruption of me, but it is also part of the checks to
249 * be sure there is no pointer arithmatic under/overflow.
250 */
251 if(pMe->data_len > pMe->UB.len) { /* Check #1 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100252 pMe->err = 1;
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700253 /* Offset of valid data is off the end of the UsefulOutBuf due to
254 * uninitialization or corruption
255 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800256 return;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800258
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700259 /* 1. Will it fit?
260 * WillItFit() is the same as: NewData.len <= (me->UB.len - me->data_len)
261 * Check #1 makes sure subtraction in RoomLeft will not wrap around
262 */
263 if(! UsefulOutBuf_WillItFit(pMe, NewData.len)) { /* Check #2 */
264 /* The new data will not fit into the the buffer. */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100265 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700266 return;
267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800268
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700269 /* 2. Check the Insertion Position
270 * This, with Check #1, also confirms that uInsertionPos <= me->data_len and
271 * that uInsertionPos + pMe->UB.ptr will not wrap around the end of the
272 * address space.
273 */
274 if(uInsertionPos > pMe->data_len) { /* Check #3 */
275 /* Off the end of the valid data in the buffer. */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100276 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700277 return;
278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800279
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700280 /* 3. Slide existing data to the right */
Maxim Zhukov9fab6892022-12-20 20:16:07 +0300281 if (!UsefulOutBuf_IsBufferNULL(pMe)) {
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700282 uint8_t *pSourceOfMove = ((uint8_t *)pMe->UB.ptr) + uInsertionPos; /* PtrMath #1 */
283 size_t uNumBytesToMove = pMe->data_len - uInsertionPos; /* PtrMath #2 */
284 uint8_t *pDestinationOfMove = pSourceOfMove + NewData.len; /* PtrMath #3*/
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800285
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700286 /* To know memmove won't go off end of destination, see PtrMath #4.
287 * Use memove because it handles overlapping buffers
288 */
Laurence Lundblade74f68412018-09-13 12:18:49 -0700289 memmove(pDestinationOfMove, pSourceOfMove, uNumBytesToMove);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800290
Maxim Zhukov9fab6892022-12-20 20:16:07 +0300291 /* 4. Put the new data in */
292 uint8_t *pInsertionPoint = pSourceOfMove;
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700293 /* To know memmove won't go off end of destination, see PtrMath #5 */
Laurence Lundblade5a6fec52022-12-25 11:28:43 -0700294 if(NewData.ptr != NULL) {
295 memmove(pInsertionPoint, NewData.ptr, NewData.len);
296 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700297 }
Maxim Zhukov9fab6892022-12-20 20:16:07 +0300298
Laurence Lundblade61209742019-11-08 13:16:43 -0800299 pMe->data_len += NewData.len;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700300}
301
302
303/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700304 * Rationale that describes why the above pointer math is safe
305 *
306 * PtrMath #1 will never wrap around over because
307 * Check #0 in UsefulOutBuf_Init that me->UB.ptr + me->UB.len doesn't wrap
308 * Check #1 makes sure me->data_len is less than me->UB.len
309 * Check #3 makes sure uInsertionPos is less than me->data_len
310 *
311 * PtrMath #2 will never wrap around under because
312 * Check #3 makes sure uInsertionPos is less than me->data_len
313 *
314 * PtrMath #3 will never wrap around over because
315 * PtrMath #1 is checked resulting in pSourceOfMove being between me->UB.ptr and me->UB.ptr + me->data_len
316 * Check #2 that NewData.len will fit in the unused space left in me->UB
317 *
318 * PtrMath #4 will never wrap under because
319 * Calculation for extent or memmove is uRoomInDestination = me->UB.len - (uInsertionPos + NewData.len)
320 * Check #3 makes sure uInsertionPos is less than me->data_len
321 * Check #3 allows Check #2 to be refactored as NewData.Len > (me->size - uInsertionPos)
322 * This algebraically rearranges to me->size > uInsertionPos + NewData.len
323 *
324 * PtrMath #5 will never wrap under because
325 * Calculation for extent of memove is uRoomInDestination = me->UB.len - uInsertionPos;
326 * Check #1 makes sure me->data_len is less than me->size
327 * Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700328 */
329
330
331/*
Laurence Lundbladeb24faef2022-04-26 11:03:08 -0600332 * Public function for advancing data length. See qcbor/UsefulBuf.h
333 */
334void UsefulOutBuf_Advance(UsefulOutBuf *pMe, size_t uAmount)
335{
336 /* This function is a trimmed down version of
337 * UsefulOutBuf_InsertUsefulBuf(). This could be combined with the
338 * code in UsefulOutBuf_InsertUsefulBuf(), but that would make
339 * UsefulOutBuf_InsertUsefulBuf() bigger and this will be very
340 * rarely used.
341 */
342
343 if(pMe->err) {
344 /* Already in error state. */
345 return;
346 }
347
348 /* 0. Sanity check the UsefulOutBuf structure
349 *
350 * A "counter measure". If magic number is not the right number it
351 * probably means me was not initialized or it was
352 * corrupted. Attackers can defeat this, but it is a hurdle and
353 * does good with very little code.
354 */
355 if(pMe->magic != USEFUL_OUT_BUF_MAGIC) {
356 pMe->err = 1;
357 return; /* Magic number is wrong due to uninitalization or corrption */
358 }
359
360 /* Make sure valid data is less than buffer size. This would only
361 * occur if there was corruption of me, but it is also part of the
362 * checks to be sure there is no pointer arithmatic
363 * under/overflow.
364 */
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700365 if(pMe->data_len > pMe->UB.len) { /* Check #1 */
Laurence Lundbladeb24faef2022-04-26 11:03:08 -0600366 pMe->err = 1;
367 /* Offset of valid data is off the end of the UsefulOutBuf due
368 * to uninitialization or corruption.
369 */
370 return;
371 }
372
373 /* 1. Will it fit?
374 *
375 * WillItFit() is the same as: NewData.len <= (me->UB.len -
376 * me->data_len) Check #1 makes sure subtraction in RoomLeft will
377 * not wrap around
378 */
379 if(! UsefulOutBuf_WillItFit(pMe, uAmount)) { /* Check #2 */
380 /* The new data will not fit into the the buffer. */
381 pMe->err = 1;
382 return;
383 }
384
385 pMe->data_len += uAmount;
386}
387
388
389/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700390 * Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700391 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100392UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *pMe)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700393{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100394 if(pMe->err) {
Laurence Lundblade2296db52018-09-14 18:08:39 -0700395 return NULLUsefulBufC;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700396 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800397
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100398 if(pMe->magic != USEFUL_OUT_BUF_MAGIC) {
399 pMe->err = 1;
Laurence Lundblade2296db52018-09-14 18:08:39 -0700400 return NULLUsefulBufC;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700401 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800402
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100403 return (UsefulBufC){pMe->UB.ptr, pMe->data_len};
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700404}
405
406
407/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700408 * Public function -- see UsefulBuf.h
409 *
410 * Copy out the data accumulated in to the output buffer.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700411 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100412UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *pMe, UsefulBuf pDest)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700413{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100414 const UsefulBufC Tmp = UsefulOutBuf_OutUBuf(pMe);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530415 if(UsefulBuf_IsNULLC(Tmp)) {
416 return NULLUsefulBufC;
417 }
418 return UsefulBuf_Copy(pDest, Tmp);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700419}
420
421
Laurence Lundbladee2226742024-08-16 10:50:23 -0700422/*
423 * Public function -- see UsefulBuf.h
424 *
425 * Code Reviewers: THIS FUNCTION DOES POINTER MATH
426 */
427UsefulBufC UsefulOutBuf_SubString(UsefulOutBuf *pMe,
428 const size_t uStart,
429 const size_t uLen)
430{
431 const UsefulBufC Tmp = UsefulOutBuf_OutUBuf(pMe);
432
433 if(UsefulBuf_IsNULLC(Tmp)) {
434 return NULLUsefulBufC;
435 }
436
437 if(uStart > Tmp.len) {
438 return NULLUsefulBufC;
439 }
440
441 if(Tmp.len - uStart < uLen) {
442 return NULLUsefulBufC;
443 }
444
445 UsefulBufC SubString;
446 SubString.ptr = (const uint8_t *)Tmp.ptr + uStart;
447 SubString.len = uLen;
448
449 return SubString;
450}
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700451
452
453/*
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700454 * Public function -- see UsefulBuf.h
455 *
456 * The core of UsefulInputBuf -- consume bytes without going off end of buffer.
457 *
458 * Code Reviewers: THIS FUNCTION DOES POINTER MATH
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700459 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100460const void * UsefulInputBuf_GetBytes(UsefulInputBuf *pMe, size_t uAmount)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700461{
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700462 /* Already in error state. Do nothing. */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100463 if(pMe->err) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700464 return NULL;
465 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800466
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100467 if(!UsefulInputBuf_BytesAvailable(pMe, uAmount)) {
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700468 /* Number of bytes asked for is more than available */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100469 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700470 return NULL;
471 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800472
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700473 /* This is going to succeed */
Laurence Lundbladeb9702452021-03-08 21:02:57 -0800474 const void * const result = ((const uint8_t *)pMe->UB.ptr) + pMe->cursor;
Laurence Lundbladeb239c652024-05-26 08:07:05 -0700475 /* Won't overflow because of check using UsefulInputBuf_BytesAvailable() */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800476 pMe->cursor += uAmount;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700477 return result;
478}
479
Laurence Lundbladed6e13022023-11-26 10:14:02 -0700480
481/*
482 * Public function -- see UsefulBuf.h
483 *
484 * Code Reviewers: THIS FUNCTION DOES POINTER MATH
485 */
Laurence Lundblade9b2ae8a2024-07-12 11:00:20 -0700486int
487UsefulInputBuf_Compare(UsefulInputBuf *pUInBuf,
488 const size_t uOffset1,
489 const size_t uLen1,
490 const size_t uOffset2,
491 const size_t uLen2)
492{
493 UsefulBufC UB1;
494 UsefulBufC UB2;
495
496 const size_t uInputSize = UsefulInputBuf_GetBufferLength(pUInBuf);
497
498 /* Careful length check that works even if uLen1 + uOffset1 > SIZE_MAX */
499 if(uOffset1 > uInputSize || uLen1 > uInputSize - uOffset1) {
500 return 1;
501 }
502 UB1.ptr = (const uint8_t *)pUInBuf->UB.ptr + uOffset1;
503 UB1.len = uLen1;
504
505 /* Careful length check that works even if uLen2 + uOffset2 > SIZE_MAX */
506 if(uOffset2 > uInputSize || uLen2 > uInputSize - uOffset2) {
507 return -1;
508 }
509 UB2.ptr = (const uint8_t *)pUInBuf->UB.ptr + uOffset2;
510 UB2.len = uLen2;
511
512 return UsefulBuf_Compare(UB1, UB2);
513}
514
515
516/*
517 * Public function -- see UsefulBuf.h
518 *
519 * Code Reviewers: THIS FUNCTION DOES POINTER MATH
520 */
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -0700521int UsefulOutBuf_Compare(UsefulOutBuf *pMe,
522 const size_t uStart1, const size_t uLen1,
523 const size_t uStart2, const size_t uLen2)
Laurence Lundbladed6e13022023-11-26 10:14:02 -0700524{
525 const uint8_t *pBase;
526 const uint8_t *pEnd;
527 const uint8_t *p1;
528 const uint8_t *p2;
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -0700529 const uint8_t *p1End;
530 const uint8_t *p2End;
Laurence Lundbladed6e13022023-11-26 10:14:02 -0700531 int uComparison;
532
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -0700533 pBase = pMe->UB.ptr;
534 pEnd = (const uint8_t *)pBase + pMe->data_len;
Laurence Lundbladed6e13022023-11-26 10:14:02 -0700535 p1 = pBase + uStart1;
536 p2 = pBase + uStart2;
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -0700537 p1End = p1 + uLen1;
538 p2End = p2 + uLen2;
Laurence Lundbladed6e13022023-11-26 10:14:02 -0700539
540 uComparison = 0;
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -0700541 while(p1 < pEnd && p2 < pEnd && p1 < p1End && p2 < p2End) {
Laurence Lundbladed6e13022023-11-26 10:14:02 -0700542 uComparison = *p2 - *p1;
543 if(uComparison != 0) {
544 break;;
545 }
546 p1++;
547 p2++;
548 }
549
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -0700550 if(uComparison == 0 && p1 != p1End && p2 != p2End) {
551 if(uLen1 > uLen2) {
552 uComparison = 1;
553 } else if(uLen2 < uLen1){
554 uComparison = -1;
555 } else {
556 return 0;
557 }
558 }
559
Laurence Lundbladed6e13022023-11-26 10:14:02 -0700560 return uComparison;
561}
562
563
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -0700564
Laurence Lundbladed6e13022023-11-26 10:14:02 -0700565/**
566 * @brief Reverse order of bytes in a buffer.
567 *
568 * This reverses bytes starting at pStart, up to, but not including
569 * the byte at pEnd
570 */
571static void
572UsefulOutBuf_Private_ReverseBytes(uint8_t *pStart, uint8_t *pEnd)
573{
574 uint8_t uTmp;
575
576 while(pStart < pEnd) {
577 pEnd--;
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -0700578 uTmp = *pStart;
Laurence Lundbladed6e13022023-11-26 10:14:02 -0700579 *pStart = *pEnd;
580 *pEnd = uTmp;
581 pStart++;
582 }
583}
584
585
586/*
587 * Public function -- see UsefulBuf.h
588 *
589 * Code Reviewers: THIS FUNCTION DOES POINTER MATH
590 */
591void UsefulOutBuf_Swap(UsefulOutBuf *pMe, size_t uStartOffset, size_t uPivotOffset, size_t uEndOffset)
592{
593 uint8_t *pBase;
594
595 if(uStartOffset > pMe->data_len || uPivotOffset > pMe->data_len || uEndOffset > pMe->data_len) {
596 return;
597 }
598
599 if(uStartOffset > uPivotOffset || uStartOffset > uEndOffset || uPivotOffset > uEndOffset) {
600 return;
601 }
602
603 /* This is the "reverse" algorithm to swap two memory regions */
604 pBase = pMe->UB.ptr;
605 UsefulOutBuf_Private_ReverseBytes(pBase + uStartOffset, pBase + uPivotOffset);
606 UsefulOutBuf_Private_ReverseBytes(pBase + uPivotOffset, pBase + uEndOffset);
607 UsefulOutBuf_Private_ReverseBytes(pBase + uStartOffset, pBase + uEndOffset);
608}
609
610
611/*
612 * Public function -- see UsefulBuf.h
613 */
614UsefulBufC
615UsefulOutBuf_OutUBufOffset(UsefulOutBuf *pMe, size_t uOffset)
616{
617 UsefulBufC ReturnValue;
618
619 ReturnValue = UsefulOutBuf_OutUBuf(pMe);
620
621 if(UsefulBuf_IsNULLC(ReturnValue)) {
622 return NULLUsefulBufC;
623 }
624
625 if(uOffset >= ReturnValue.len) {
626 return NULLUsefulBufC;
627 }
628
629 ReturnValue.ptr = (const uint8_t *)ReturnValue.ptr + uOffset;
630 ReturnValue.len -= uOffset;
631
632 return ReturnValue;
633}