blob: a96f74eb26c062e3494db8d826371d9fb5246ea1 [file] [log] [blame]
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07005Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above
11 copyright notice, this list of conditions and the following
12 disclaimer in the documentation and/or other materials provided
13 with the distribution.
14 * Neither the name of The Linux Foundation nor the names of its
15 contributors, nor the name "Laurence Lundblade" may be used to
16 endorse or promote products derived from this software without
17 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080018
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070019THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
20WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
23BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080030 =============================================================================*/
Laurence Lundblade624405d2018-09-18 20:10:47 -070031
Laurence Lundbladeee851742020-01-08 08:37:05 -080032/*=============================================================================
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070033 FILE: UsefulBuf.c
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080034
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070035 DESCRIPTION: General purpose input and output buffers
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080036
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070037 EDIT HISTORY FOR FILE:
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080038
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070039 This section contains comments describing changes made to the module.
40 Notice that changes are listed in reverse chronological order.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080041
Laurence Lundbladeee851742020-01-08 08:37:05 -080042 when who what, where, why
43 -------- ---- ---------------------------------------------------
Laurence Lundblade06350ea2020-01-27 19:32:40 -080044 01/28/2020 llundblade Refine integer signedness to quiet static analysis.
45 01/08/2020 llundblade Documentation corrections & improved code formatting.
Laurence Lundbladeee851742020-01-08 08:37:05 -080046 11/08/2019 llundblade Re check pointer math and update comments
47 3/6/2019 llundblade Add UsefulBuf_IsValue()
48 09/07/17 llundbla Fix critical bug in UsefulBuf_Find() -- a read off
49 the end of memory when the bytes to find is longer
50 than the bytes to search.
51 06/27/17 llundbla Fix UsefulBuf_Compare() bug. Only affected comparison
52 for < or > for unequal length buffers. Added
53 UsefulBuf_Set() function.
54 05/30/17 llundbla Functions for NULL UsefulBufs and const / unconst
55 11/13/16 llundbla Initial Version.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080056
Laurence Lundbladeee851742020-01-08 08:37:05 -080057 ============================================================================*/
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070058
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070059#include "UsefulBuf.h"
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070060
Laurence Lundbladeee851742020-01-08 08:37:05 -080061// used to catch use of uninitialized or corrupted UsefulOutBuf
62#define USEFUL_OUT_BUF_MAGIC (0x0B0F)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070063
Laurence Lundblade041ffa52018-10-07 11:43:51 +070064
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053065/*
66 Public function -- see UsefulBuf.h
67 */
Laurence Lundblade041ffa52018-10-07 11:43:51 +070068UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src)
69{
Laurence Lundbladeee851742020-01-08 08:37:05 -080070 // Do this with subtraction so it doesn't give erroneous
71 // result if uOffset + Src.len overflows
Laurence Lundblade7566b9f2018-10-12 09:13:32 +080072 if(uOffset > Dest.len || Src.len > Dest.len - uOffset) { // uOffset + Src.len > Dest.len
73 return NULLUsefulBufC;
74 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080075
Laurence Lundblade570fab52018-10-13 18:28:27 +080076 memcpy((uint8_t *)Dest.ptr + uOffset, Src.ptr, Src.len);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080077
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -080078 return (UsefulBufC){Dest.ptr, Src.len + uOffset};
Laurence Lundblade041ffa52018-10-07 11:43:51 +070079}
80
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053081
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070082/*
83 Public function -- see UsefulBuf.h
84 */
85int UsefulBuf_Compare(const UsefulBufC UB1, const UsefulBufC UB2)
86{
87 // use the comparisons rather than subtracting lengths to
88 // return an int instead of a size_t
89 if(UB1.len < UB2.len) {
90 return -1;
91 } else if (UB1.len > UB2.len) {
92 return 1;
93 } // else UB1.len == UB2.len
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080094
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070095 return memcmp(UB1.ptr, UB2.ptr, UB1.len);
96}
97
98
Laurence Lundbladed5e101e2019-03-06 17:23:18 -080099/*
100 Public function -- see UsefulBuf.h
101 */
102size_t UsefulBuf_IsValue(const UsefulBufC UB, uint8_t uValue)
103{
104 if(UsefulBuf_IsNULLOrEmptyC(UB)) {
105 /* Not a match */
106 return 0;
107 }
108
109 const uint8_t * const pEnd = (uint8_t *)UB.ptr + UB.len;
110 for(const uint8_t *p = UB.ptr; p < pEnd; p++) {
111 if(*p != uValue) {
112 /* Byte didn't match */
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800113 /* Cast from signed to unsigned . Safe because the loop increments.*/
114 return (size_t)(p - (uint8_t *)UB.ptr);
Laurence Lundbladed5e101e2019-03-06 17:23:18 -0800115 }
116 }
117
118 /* Success. All bytes matched */
119 return SIZE_MAX;
120}
121
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700122
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700123/*
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530124 Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700125 */
126size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind)
127{
128 if(BytesToSearch.len < BytesToFind.len) {
129 return SIZE_MAX;
130 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800131
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700132 for(size_t uPos = 0; uPos <= BytesToSearch.len - BytesToFind.len; uPos++) {
133 if(!UsefulBuf_Compare((UsefulBufC){((uint8_t *)BytesToSearch.ptr) + uPos, BytesToFind.len}, BytesToFind)) {
134 return uPos;
135 }
136 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800137
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700138 return SIZE_MAX;
139}
140
141
142/*
143 Public function -- see UsefulBuf.h
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800144
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530145 Code Reviewers: THIS FUNCTION DOES POINTER MATH
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700146 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100147void UsefulOutBuf_Init(UsefulOutBuf *pMe, UsefulBuf Storage)
Laurence Lundblade2296db52018-09-14 18:08:39 -0700148{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100149 pMe->magic = USEFUL_OUT_BUF_MAGIC;
150 UsefulOutBuf_Reset(pMe);
151 pMe->UB = Storage;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800152
Laurence Lundblade2296db52018-09-14 18:08:39 -0700153#if 0
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530154 // This check is off by default.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800155
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530156 // The following check fails on ThreadX
157
Laurence Lundblade2296db52018-09-14 18:08:39 -0700158 // Sanity check on the pointer and size to be sure we are not
159 // passed a buffer that goes off the end of the address space.
160 // Given this test, we know that all unsigned lengths less than
161 // me->size are valid and won't wrap in any pointer additions
162 // based off of pStorage in the rest of this code.
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530163 const uintptr_t ptrM = UINTPTR_MAX - Storage.len;
164 if(Storage.ptr && (uintptr_t)Storage.ptr > ptrM) // Check #0
Laurence Lundblade2296db52018-09-14 18:08:39 -0700165 me->err = 1;
166#endif
167}
168
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700169
170
171/*
172 Public function -- see UsefulBuf.h
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800173
Laurence Lundbladeee851742020-01-08 08:37:05 -0800174 The core of UsefulOutBuf -- put some bytes in the buffer without writing off
175 the end of it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700177 Code Reviewers: THIS FUNCTION DOES POINTER MATH
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800178
Laurence Lundbladeee851742020-01-08 08:37:05 -0800179 This function inserts the source buffer, NewData, into the destination
180 buffer, me->UB.ptr.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800181
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700182 Destination is represented as:
183 me->UB.ptr -- start of the buffer
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700184 me->UB.len -- size of the buffer UB.ptr
185 me->data_len -- length of value data in UB
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800186
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700187 Source is data:
188 NewData.ptr -- start of source buffer
189 NewData.len -- length of source buffer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800190
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700191 Insertion point:
192 uInsertionPos.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800193
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700194 Steps:
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800195
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700196 0. Corruption checks on UsefulOutBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800197
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700198 1. Figure out if the new data will fit or not
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800199
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700200 2. Is insertion position in the range of valid data?
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800201
Laurence Lundbladeee851742020-01-08 08:37:05 -0800202 3. If insertion point is not at the end, slide data to the right of the
203 insertion point to the right
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800204
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700205 4. Put the new data in at the insertion position.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700207 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100208void UsefulOutBuf_InsertUsefulBuf(UsefulOutBuf *pMe, UsefulBufC NewData, size_t uInsertionPos)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700209{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100210 if(pMe->err) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700211 // Already in error state.
212 return;
213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800214
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700215 /* 0. Sanity check the UsefulOutBuf structure */
216 // A "counter measure". If magic number is not the right number it
217 // probably means me was not initialized or it was corrupted. Attackers
218 // can defeat this, but it is a hurdle and does good with very
219 // little code.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100220 if(pMe->magic != USEFUL_OUT_BUF_MAGIC) {
221 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700222 return; // Magic number is wrong due to uninitalization or corrption
223 }
224
225 // Make sure valid data is less than buffer size. This would only occur
226 // if there was corruption of me, but it is also part of the checks to
227 // be sure there is no pointer arithmatic under/overflow.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100228 if(pMe->data_len > pMe->UB.len) { // Check #1
229 pMe->err = 1;
Laurence Lundbladeee851742020-01-08 08:37:05 -0800230 // Offset of valid data is off the end of the UsefulOutBuf due to
231 // uninitialization or corruption
232 return;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800234
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700235 /* 1. Will it fit? */
Laurence Lundblade61209742019-11-08 13:16:43 -0800236 // WillItFit() is the same as: NewData.len <= (me->UB.len - me->data_len)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700237 // Check #1 makes sure subtraction in RoomLeft will not wrap around
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100238 if(! UsefulOutBuf_WillItFit(pMe, NewData.len)) { // Check #2
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700239 // The new data will not fit into the the buffer.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100240 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700241 return;
242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800243
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700244 /* 2. Check the Insertion Position */
Laurence Lundblade61209742019-11-08 13:16:43 -0800245 // This, with Check #1, also confirms that uInsertionPos <= me->data_len and
246 // that uInsertionPos + pMe->UB.ptr will not wrap around the end of the
247 // address space.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100248 if(uInsertionPos > pMe->data_len) { // Check #3
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700249 // Off the end of the valid data in the buffer.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100250 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700251 return;
252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700254 /* 3. Slide existing data to the right */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100255 uint8_t *pSourceOfMove = ((uint8_t *)pMe->UB.ptr) + uInsertionPos; // PtrMath #1
256 size_t uNumBytesToMove = pMe->data_len - uInsertionPos; // PtrMath #2
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700257 uint8_t *pDestinationOfMove = pSourceOfMove + NewData.len; // PtrMath #3
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800258
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100259 if(uNumBytesToMove && pMe->UB.ptr) {
Laurence Lundblade56a79322019-01-10 09:12:37 -0800260 // To know memmove won't go off end of destination, see PtrMath #4
Laurence Lundblade61209742019-11-08 13:16:43 -0800261 // Use memove because it handles overlapping buffers
Laurence Lundblade74f68412018-09-13 12:18:49 -0700262 memmove(pDestinationOfMove, pSourceOfMove, uNumBytesToMove);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700263 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800264
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700265 /* 4. Put the new data in */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100266 uint8_t *pInsertionPoint = ((uint8_t *)pMe->UB.ptr) + uInsertionPos; // PtrMath #5
267 if(pMe->UB.ptr) {
Laurence Lundblade56a79322019-01-10 09:12:37 -0800268 // To know memmove won't go off end of destination, see PtrMath #6
Laurence Lundblade74f68412018-09-13 12:18:49 -0700269 memmove(pInsertionPoint, NewData.ptr, NewData.len);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700270 }
Laurence Lundblade61209742019-11-08 13:16:43 -0800271 pMe->data_len += NewData.len;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700272}
273
274
275/*
276 Rationale that describes why the above pointer math is safe
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800277
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700278 PtrMath #1 will never wrap around over because
Laurence Lundblade56a79322019-01-10 09:12:37 -0800279 Check #0 in UsefulOutBuf_Init makes sure me->UB.ptr + me->UB.len doesn't wrap
280 Check #1 makes sure me->data_len is less than me->UB.len
281 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800282
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700283 PtrMath #2 will never wrap around under because
Laurence Lundblade56a79322019-01-10 09:12:37 -0800284 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800285
Laurence Lundblade61209742019-11-08 13:16:43 -0800286 PtrMath #3 will never wrap around over because
287 PtrMath #1 is checked resulting in pSourceOfMove being between me->UB.ptr and me->UB.ptr + me->data_len
288 Check #2 that NewData.len will fit in the unused space left in me->UB
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800289
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700290 PtrMath #4 will never wrap under because
Laurence Lundblade56a79322019-01-10 09:12:37 -0800291 Calculation for extent or memmove is uRoomInDestination = me->UB.len - (uInsertionPos + NewData.len)
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700292 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700293 Check #3 allows Check #2 to be refactored as NewData.Len > (me->size - uInsertionPos)
294 This algebraically rearranges to me->size > uInsertionPos + NewData.len
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800295
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700296 PtrMath #5 is exactly the same as PtrMath #1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800297
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700298 PtrMath #6 will never wrap under because
Laurence Lundblade56a79322019-01-10 09:12:37 -0800299 Calculation for extent of memove is uRoomInDestination = me->UB.len - uInsertionPos;
300 Check #1 makes sure me->data_len is less than me->size
301 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700302 */
303
304
305/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800306 Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700307 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100308UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *pMe)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700309{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100310 if(pMe->err) {
Laurence Lundblade2296db52018-09-14 18:08:39 -0700311 return NULLUsefulBufC;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800313
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100314 if(pMe->magic != USEFUL_OUT_BUF_MAGIC) {
315 pMe->err = 1;
Laurence Lundblade2296db52018-09-14 18:08:39 -0700316 return NULLUsefulBufC;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700317 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800318
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100319 return (UsefulBufC){pMe->UB.ptr, pMe->data_len};
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700320}
321
322
323/*
324 Public function -- see UsefulBuf.h
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800325
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530326 Copy out the data accumulated in to the output buffer.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700327 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100328UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *pMe, UsefulBuf pDest)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700329{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100330 const UsefulBufC Tmp = UsefulOutBuf_OutUBuf(pMe);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530331 if(UsefulBuf_IsNULLC(Tmp)) {
332 return NULLUsefulBufC;
333 }
334 return UsefulBuf_Copy(pDest, Tmp);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700335}
336
337
338
339
340/*
341 Public function -- see UsefulBuf.h
342
Laurence Lundbladeee851742020-01-08 08:37:05 -0800343 The core of UsefulInputBuf -- consume bytes without going off end of buffer.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800344
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700345 Code Reviewers: THIS FUNCTION DOES POINTER MATH
346 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100347const void * UsefulInputBuf_GetBytes(UsefulInputBuf *pMe, size_t uAmount)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700348{
349 // Already in error state. Do nothing.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100350 if(pMe->err) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700351 return NULL;
352 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800353
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100354 if(!UsefulInputBuf_BytesAvailable(pMe, uAmount)) {
Laurence Lundbladeee851742020-01-08 08:37:05 -0800355 // Number of bytes asked for at current position are more than available
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100356 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700357 return NULL;
358 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800359
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700360 // This is going to succeed
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100361 const void * const result = ((uint8_t *)pMe->UB.ptr) + pMe->cursor;
Laurence Lundbladeee851742020-01-08 08:37:05 -0800362 // Will not overflow because of check using UsefulInputBuf_BytesAvailable()
363 pMe->cursor += uAmount;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700364 return result;
365}
366