blob: 1d13f10f1e9555852553eaa651b60d8369300741 [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 -------- ---- ---------------------------------------------------
44 01/08/2020 llundblade Documentation corrections & improved code formatting.
45 11/08/2019 llundblade Re check pointer math and update comments
46 3/6/2019 llundblade Add UsefulBuf_IsValue()
47 09/07/17 llundbla Fix critical bug in UsefulBuf_Find() -- a read off
48 the end of memory when the bytes to find is longer
49 than the bytes to search.
50 06/27/17 llundbla Fix UsefulBuf_Compare() bug. Only affected comparison
51 for < or > for unequal length buffers. Added
52 UsefulBuf_Set() function.
53 05/30/17 llundbla Functions for NULL UsefulBufs and const / unconst
54 11/13/16 llundbla Initial Version.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080055
Laurence Lundbladeee851742020-01-08 08:37:05 -080056 ============================================================================*/
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070057
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070058#include "UsefulBuf.h"
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070059
Laurence Lundbladeee851742020-01-08 08:37:05 -080060// used to catch use of uninitialized or corrupted UsefulOutBuf
61#define USEFUL_OUT_BUF_MAGIC (0x0B0F)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070062
Laurence Lundblade041ffa52018-10-07 11:43:51 +070063
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053064/*
65 Public function -- see UsefulBuf.h
66 */
Laurence Lundblade041ffa52018-10-07 11:43:51 +070067UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src)
68{
Laurence Lundbladeee851742020-01-08 08:37:05 -080069 // Do this with subtraction so it doesn't give erroneous
70 // result if uOffset + Src.len overflows
Laurence Lundblade7566b9f2018-10-12 09:13:32 +080071 if(uOffset > Dest.len || Src.len > Dest.len - uOffset) { // uOffset + Src.len > Dest.len
72 return NULLUsefulBufC;
73 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080074
Laurence Lundblade570fab52018-10-13 18:28:27 +080075 memcpy((uint8_t *)Dest.ptr + uOffset, Src.ptr, Src.len);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080076
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -080077 return (UsefulBufC){Dest.ptr, Src.len + uOffset};
Laurence Lundblade041ffa52018-10-07 11:43:51 +070078}
79
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053080
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070081/*
82 Public function -- see UsefulBuf.h
83 */
84int UsefulBuf_Compare(const UsefulBufC UB1, const UsefulBufC UB2)
85{
86 // use the comparisons rather than subtracting lengths to
87 // return an int instead of a size_t
88 if(UB1.len < UB2.len) {
89 return -1;
90 } else if (UB1.len > UB2.len) {
91 return 1;
92 } // else UB1.len == UB2.len
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080093
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070094 return memcmp(UB1.ptr, UB2.ptr, UB1.len);
95}
96
97
Laurence Lundbladed5e101e2019-03-06 17:23:18 -080098/*
99 Public function -- see UsefulBuf.h
100 */
101size_t UsefulBuf_IsValue(const UsefulBufC UB, uint8_t uValue)
102{
103 if(UsefulBuf_IsNULLOrEmptyC(UB)) {
104 /* Not a match */
105 return 0;
106 }
107
108 const uint8_t * const pEnd = (uint8_t *)UB.ptr + UB.len;
109 for(const uint8_t *p = UB.ptr; p < pEnd; p++) {
110 if(*p != uValue) {
111 /* Byte didn't match */
112 return p - (uint8_t *)UB.ptr;
113 }
114 }
115
116 /* Success. All bytes matched */
117 return SIZE_MAX;
118}
119
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700120
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700121/*
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530122 Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700123 */
124size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind)
125{
126 if(BytesToSearch.len < BytesToFind.len) {
127 return SIZE_MAX;
128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800129
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700130 for(size_t uPos = 0; uPos <= BytesToSearch.len - BytesToFind.len; uPos++) {
131 if(!UsefulBuf_Compare((UsefulBufC){((uint8_t *)BytesToSearch.ptr) + uPos, BytesToFind.len}, BytesToFind)) {
132 return uPos;
133 }
134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800135
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700136 return SIZE_MAX;
137}
138
139
140/*
141 Public function -- see UsefulBuf.h
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800142
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530143 Code Reviewers: THIS FUNCTION DOES POINTER MATH
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700144 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100145void UsefulOutBuf_Init(UsefulOutBuf *pMe, UsefulBuf Storage)
Laurence Lundblade2296db52018-09-14 18:08:39 -0700146{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100147 pMe->magic = USEFUL_OUT_BUF_MAGIC;
148 UsefulOutBuf_Reset(pMe);
149 pMe->UB = Storage;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800150
Laurence Lundblade2296db52018-09-14 18:08:39 -0700151#if 0
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530152 // This check is off by default.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800153
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530154 // The following check fails on ThreadX
155
Laurence Lundblade2296db52018-09-14 18:08:39 -0700156 // Sanity check on the pointer and size to be sure we are not
157 // passed a buffer that goes off the end of the address space.
158 // Given this test, we know that all unsigned lengths less than
159 // me->size are valid and won't wrap in any pointer additions
160 // based off of pStorage in the rest of this code.
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530161 const uintptr_t ptrM = UINTPTR_MAX - Storage.len;
162 if(Storage.ptr && (uintptr_t)Storage.ptr > ptrM) // Check #0
Laurence Lundblade2296db52018-09-14 18:08:39 -0700163 me->err = 1;
164#endif
165}
166
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700167
168
169/*
170 Public function -- see UsefulBuf.h
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800171
Laurence Lundbladeee851742020-01-08 08:37:05 -0800172 The core of UsefulOutBuf -- put some bytes in the buffer without writing off
173 the end of it.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800174
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700175 Code Reviewers: THIS FUNCTION DOES POINTER MATH
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
Laurence Lundbladeee851742020-01-08 08:37:05 -0800177 This function inserts the source buffer, NewData, into the destination
178 buffer, me->UB.ptr.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800179
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700180 Destination is represented as:
181 me->UB.ptr -- start of the buffer
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700182 me->UB.len -- size of the buffer UB.ptr
183 me->data_len -- length of value data in UB
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800184
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700185 Source is data:
186 NewData.ptr -- start of source buffer
187 NewData.len -- length of source buffer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800188
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700189 Insertion point:
190 uInsertionPos.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800191
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700192 Steps:
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800193
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700194 0. Corruption checks on UsefulOutBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800195
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700196 1. Figure out if the new data will fit or not
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800197
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700198 2. Is insertion position in the range of valid data?
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800199
Laurence Lundbladeee851742020-01-08 08:37:05 -0800200 3. If insertion point is not at the end, slide data to the right of the
201 insertion point to the right
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800202
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700203 4. Put the new data in at the insertion position.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800204
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700205 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100206void UsefulOutBuf_InsertUsefulBuf(UsefulOutBuf *pMe, UsefulBufC NewData, size_t uInsertionPos)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700207{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100208 if(pMe->err) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700209 // Already in error state.
210 return;
211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800212
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700213 /* 0. Sanity check the UsefulOutBuf structure */
214 // A "counter measure". If magic number is not the right number it
215 // probably means me was not initialized or it was corrupted. Attackers
216 // can defeat this, but it is a hurdle and does good with very
217 // little code.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100218 if(pMe->magic != USEFUL_OUT_BUF_MAGIC) {
219 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700220 return; // Magic number is wrong due to uninitalization or corrption
221 }
222
223 // Make sure valid data is less than buffer size. This would only occur
224 // if there was corruption of me, but it is also part of the checks to
225 // be sure there is no pointer arithmatic under/overflow.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100226 if(pMe->data_len > pMe->UB.len) { // Check #1
227 pMe->err = 1;
Laurence Lundbladeee851742020-01-08 08:37:05 -0800228 // Offset of valid data is off the end of the UsefulOutBuf due to
229 // uninitialization or corruption
230 return;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800232
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700233 /* 1. Will it fit? */
Laurence Lundblade61209742019-11-08 13:16:43 -0800234 // WillItFit() is the same as: NewData.len <= (me->UB.len - me->data_len)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700235 // Check #1 makes sure subtraction in RoomLeft will not wrap around
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100236 if(! UsefulOutBuf_WillItFit(pMe, NewData.len)) { // Check #2
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700237 // The new data will not fit into the the buffer.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100238 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700239 return;
240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800241
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700242 /* 2. Check the Insertion Position */
Laurence Lundblade61209742019-11-08 13:16:43 -0800243 // This, with Check #1, also confirms that uInsertionPos <= me->data_len and
244 // that uInsertionPos + pMe->UB.ptr will not wrap around the end of the
245 // address space.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100246 if(uInsertionPos > pMe->data_len) { // Check #3
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700247 // Off the end of the valid data in the buffer.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100248 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700249 return;
250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800251
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700252 /* 3. Slide existing data to the right */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100253 uint8_t *pSourceOfMove = ((uint8_t *)pMe->UB.ptr) + uInsertionPos; // PtrMath #1
254 size_t uNumBytesToMove = pMe->data_len - uInsertionPos; // PtrMath #2
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700255 uint8_t *pDestinationOfMove = pSourceOfMove + NewData.len; // PtrMath #3
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800256
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100257 if(uNumBytesToMove && pMe->UB.ptr) {
Laurence Lundblade56a79322019-01-10 09:12:37 -0800258 // To know memmove won't go off end of destination, see PtrMath #4
Laurence Lundblade61209742019-11-08 13:16:43 -0800259 // Use memove because it handles overlapping buffers
Laurence Lundblade74f68412018-09-13 12:18:49 -0700260 memmove(pDestinationOfMove, pSourceOfMove, uNumBytesToMove);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700263 /* 4. Put the new data in */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100264 uint8_t *pInsertionPoint = ((uint8_t *)pMe->UB.ptr) + uInsertionPos; // PtrMath #5
265 if(pMe->UB.ptr) {
Laurence Lundblade56a79322019-01-10 09:12:37 -0800266 // To know memmove won't go off end of destination, see PtrMath #6
Laurence Lundblade74f68412018-09-13 12:18:49 -0700267 memmove(pInsertionPoint, NewData.ptr, NewData.len);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700268 }
Laurence Lundblade61209742019-11-08 13:16:43 -0800269 pMe->data_len += NewData.len;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700270}
271
272
273/*
274 Rationale that describes why the above pointer math is safe
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800275
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700276 PtrMath #1 will never wrap around over because
Laurence Lundblade56a79322019-01-10 09:12:37 -0800277 Check #0 in UsefulOutBuf_Init makes sure me->UB.ptr + me->UB.len doesn't wrap
278 Check #1 makes sure me->data_len is less than me->UB.len
279 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800280
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700281 PtrMath #2 will never wrap around under because
Laurence Lundblade56a79322019-01-10 09:12:37 -0800282 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800283
Laurence Lundblade61209742019-11-08 13:16:43 -0800284 PtrMath #3 will never wrap around over because
285 PtrMath #1 is checked resulting in pSourceOfMove being between me->UB.ptr and me->UB.ptr + me->data_len
286 Check #2 that NewData.len will fit in the unused space left in me->UB
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800287
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700288 PtrMath #4 will never wrap under because
Laurence Lundblade56a79322019-01-10 09:12:37 -0800289 Calculation for extent or memmove is uRoomInDestination = me->UB.len - (uInsertionPos + NewData.len)
Laurence Lundblade9e7f4742018-09-20 18:50:31 -0700290 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700291 Check #3 allows Check #2 to be refactored as NewData.Len > (me->size - uInsertionPos)
292 This algebraically rearranges to me->size > uInsertionPos + NewData.len
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800293
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700294 PtrMath #5 is exactly the same as PtrMath #1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800295
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700296 PtrMath #6 will never wrap under because
Laurence Lundblade56a79322019-01-10 09:12:37 -0800297 Calculation for extent of memove is uRoomInDestination = me->UB.len - uInsertionPos;
298 Check #1 makes sure me->data_len is less than me->size
299 Check #3 makes sure uInsertionPos is less than me->data_len
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700300 */
301
302
303/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800304 Public function -- see UsefulBuf.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700305 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100306UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *pMe)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700307{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100308 if(pMe->err) {
Laurence Lundblade2296db52018-09-14 18:08:39 -0700309 return NULLUsefulBufC;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800311
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100312 if(pMe->magic != USEFUL_OUT_BUF_MAGIC) {
313 pMe->err = 1;
Laurence Lundblade2296db52018-09-14 18:08:39 -0700314 return NULLUsefulBufC;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700315 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800316
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100317 return (UsefulBufC){pMe->UB.ptr, pMe->data_len};
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700318}
319
320
321/*
322 Public function -- see UsefulBuf.h
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800323
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530324 Copy out the data accumulated in to the output buffer.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700325 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100326UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *pMe, UsefulBuf pDest)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700327{
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100328 const UsefulBufC Tmp = UsefulOutBuf_OutUBuf(pMe);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530329 if(UsefulBuf_IsNULLC(Tmp)) {
330 return NULLUsefulBufC;
331 }
332 return UsefulBuf_Copy(pDest, Tmp);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700333}
334
335
336
337
338/*
339 Public function -- see UsefulBuf.h
340
Laurence Lundbladeee851742020-01-08 08:37:05 -0800341 The core of UsefulInputBuf -- consume bytes without going off end of buffer.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800342
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700343 Code Reviewers: THIS FUNCTION DOES POINTER MATH
344 */
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100345const void * UsefulInputBuf_GetBytes(UsefulInputBuf *pMe, size_t uAmount)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700346{
347 // Already in error state. Do nothing.
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100348 if(pMe->err) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700349 return NULL;
350 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800351
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100352 if(!UsefulInputBuf_BytesAvailable(pMe, uAmount)) {
Laurence Lundbladeee851742020-01-08 08:37:05 -0800353 // Number of bytes asked for at current position are more than available
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100354 pMe->err = 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700355 return NULL;
356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800357
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700358 // This is going to succeed
Laurence Lundblade5cc25682019-03-26 21:58:35 +0100359 const void * const result = ((uint8_t *)pMe->UB.ptr) + pMe->cursor;
Laurence Lundbladeee851742020-01-08 08:37:05 -0800360 // Will not overflow because of check using UsefulInputBuf_BytesAvailable()
361 pMe->cursor += uAmount;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700362 return result;
363}
364