Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 2 | * 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 Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 32 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 33 | |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 34 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 35 | /*============================================================================= |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 36 | FILE: UsefulBuf.c |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 37 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 38 | DESCRIPTION: General purpose input and output buffers |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 39 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 40 | EDIT HISTORY FOR FILE: |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 41 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 42 | This section contains comments describing changes made to the module. |
| 43 | Notice that changes are listed in reverse chronological order. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 44 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 45 | when who what, where, why |
| 46 | -------- ---- --------------------------------------------------- |
Laurence Lundblade | e222674 | 2024-08-16 10:50:23 -0700 | [diff] [blame] | 47 | 08/08/2024 llundblade Add UsefulOutBuf_SubString(). |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 48 | 21/05/2024 llundblade Comment formatting and some code tidiness. |
Laurence Lundblade | de2e150 | 2024-08-26 11:37:05 -0700 | [diff] [blame] | 49 | 1/7/2024 llundblade Add UsefulInputBuf_Compare(). |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame] | 50 | 28/02/2022 llundblade Rearrange UsefulOutBuf_Compare(). |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 51 | 19/11/2023 llundblade Add UsefulOutBuf_GetOutput(). |
| 52 | 19/11/2023 llundblade Add UsefulOutBuf_Swap(). |
| 53 | 19/11/2023 llundblade Add UsefulOutBuf_Compare(). |
Laurence Lundblade | 5a6fec5 | 2022-12-25 11:28:43 -0700 | [diff] [blame] | 54 | 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 Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 56 | 3/6/2021 mcr/llundblade Fix warnings related to --Wcast-qual |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 57 | 01/28/2020 llundblade Refine integer signedness to quiet static analysis. |
| 58 | 01/08/2020 llundblade Documentation corrections & improved code formatting. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 59 | 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 Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 69 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 70 | ============================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 71 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 72 | #include "UsefulBuf.h" |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 73 | |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 74 | /* used to catch use of uninitialized or corrupted UsefulOutBuf */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 75 | #define USEFUL_OUT_BUF_MAGIC (0x0B0F) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 76 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 77 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 78 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 79 | * Public function -- see UsefulBuf.h |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 80 | */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 81 | UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src) |
| 82 | { |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 83 | /* 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 Lundblade | 7566b9f | 2018-10-12 09:13:32 +0800 | [diff] [blame] | 88 | return NULLUsefulBufC; |
| 89 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 90 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 91 | memcpy((uint8_t *)Dest.ptr + uOffset, Src.ptr, Src.len); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 92 | |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 93 | return (UsefulBufC){Dest.ptr, Src.len + uOffset}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 94 | } |
| 95 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 96 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 97 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 98 | * Public function -- see UsefulBuf.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 99 | */ |
| 100 | int UsefulBuf_Compare(const UsefulBufC UB1, const UsefulBufC UB2) |
| 101 | { |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 102 | /* Use comparisons rather than subtracting lengths to |
| 103 | * return an int instead of a size_t |
| 104 | */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 105 | if(UB1.len < UB2.len) { |
| 106 | return -1; |
| 107 | } else if (UB1.len > UB2.len) { |
| 108 | return 1; |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 109 | } /* else UB1.len == UB2.len */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 110 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 111 | return memcmp(UB1.ptr, UB2.ptr, UB1.len); |
| 112 | } |
| 113 | |
| 114 | |
Laurence Lundblade | d5e101e | 2019-03-06 17:23:18 -0800 | [diff] [blame] | 115 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 116 | * Public function -- see UsefulBuf.h |
Laurence Lundblade | d5e101e | 2019-03-06 17:23:18 -0800 | [diff] [blame] | 117 | */ |
| 118 | size_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 Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 125 | const uint8_t * const pEnd = (const uint8_t *)UB.ptr + UB.len; |
Laurence Lundblade | d5e101e | 2019-03-06 17:23:18 -0800 | [diff] [blame] | 126 | for(const uint8_t *p = UB.ptr; p < pEnd; p++) { |
| 127 | if(*p != uValue) { |
| 128 | /* Byte didn't match */ |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 129 | /* Cast from signed to unsigned. Safe because the loop increments.*/ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 130 | return (size_t)(p - (const uint8_t *)UB.ptr); |
Laurence Lundblade | d5e101e | 2019-03-06 17:23:18 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | /* Success. All bytes matched */ |
| 135 | return SIZE_MAX; |
| 136 | } |
| 137 | |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 138 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 139 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 140 | * Public function -- see UsefulBuf.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 141 | */ |
| 142 | size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind) |
| 143 | { |
| 144 | if(BytesToSearch.len < BytesToFind.len) { |
| 145 | return SIZE_MAX; |
| 146 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 147 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 148 | for(size_t uPos = 0; uPos <= BytesToSearch.len - BytesToFind.len; uPos++) { |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 149 | UsefulBufC SearchNext; |
| 150 | |
| 151 | SearchNext.ptr = ((const uint8_t *)BytesToSearch.ptr) + uPos; |
| 152 | SearchNext.len = BytesToFind.len; |
| 153 | if(!UsefulBuf_Compare(SearchNext, BytesToFind)) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 154 | return uPos; |
| 155 | } |
| 156 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 157 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 158 | return SIZE_MAX; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 163 | * Public function -- see UsefulBuf.h |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 164 | */ |
| 165 | UsefulBufC |
| 166 | UsefulBuf_SkipLeading(UsefulBufC String, uint8_t uByte) |
| 167 | { |
| 168 | for(;String.len; String.len--) { |
| 169 | if(*(const uint8_t *)String.ptr != uByte) { |
| 170 | break; |
| 171 | } |
| 172 | String.ptr = (const uint8_t *)String.ptr + 1; |
| 173 | } |
| 174 | |
| 175 | return String; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | /* |
| 181 | * Public function -- see UsefulBuf.h |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 182 | * |
| 183 | * Code Reviewers: THIS FUNCTION DOES POINTER MATH |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 184 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 185 | void UsefulOutBuf_Init(UsefulOutBuf *pMe, UsefulBuf Storage) |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 186 | { |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 187 | pMe->magic = USEFUL_OUT_BUF_MAGIC; |
| 188 | UsefulOutBuf_Reset(pMe); |
| 189 | pMe->UB = Storage; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 190 | |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 191 | #if 0 |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 192 | /* This check is off by default. |
| 193 | * |
| 194 | * The following check fails on ThreadX |
| 195 | * |
| 196 | * Sanity check on the pointer and size to be sure we are not |
| 197 | * passed a buffer that goes off the end of the address space. |
| 198 | * Given this test, we know that all unsigned lengths less than |
| 199 | * me->size are valid and won't wrap in any pointer additions |
| 200 | * based off of pStorage in the rest of this code. |
| 201 | */ |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 202 | const uintptr_t ptrM = UINTPTR_MAX - Storage.len; |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 203 | if(Storage.ptr && (uintptr_t)Storage.ptr > ptrM) /* Check #0 */ |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 204 | me->err = 1; |
| 205 | #endif |
| 206 | } |
| 207 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 208 | |
| 209 | |
| 210 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 211 | * Public function -- see UsefulBuf.h |
| 212 | * |
| 213 | * The core of UsefulOutBuf -- put some bytes in the buffer without writing off |
| 214 | * the end of it. |
| 215 | * |
| 216 | * Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 217 | * |
| 218 | * This function inserts the source buffer, NewData, into the destination |
| 219 | * buffer, me->UB.ptr. |
| 220 | * |
| 221 | * Destination is represented as: |
| 222 | * me->UB.ptr -- start of the buffer |
| 223 | * me->UB.len -- size of the buffer UB.ptr |
| 224 | * me->data_len -- length of value data in UB |
| 225 | * |
| 226 | * Source is data: |
| 227 | * NewData.ptr -- start of source buffer |
| 228 | * NewData.len -- length of source buffer |
| 229 | * |
| 230 | * Insertion point: |
| 231 | * uInsertionPos. |
| 232 | * |
| 233 | * Steps: |
| 234 | * |
| 235 | * 0. Corruption checks on UsefulOutBuf |
| 236 | * |
| 237 | * 1. Figure out if the new data will fit or not |
| 238 | * |
| 239 | * 2. Is insertion position in the range of valid data? |
| 240 | * |
| 241 | * 3. If insertion point is not at the end, slide data to the right of the |
| 242 | * insertion point to the right |
| 243 | * |
| 244 | * 4. Put the new data in at the insertion position. |
| 245 | * |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 246 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 247 | void UsefulOutBuf_InsertUsefulBuf(UsefulOutBuf *pMe, UsefulBufC NewData, size_t uInsertionPos) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 248 | { |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 249 | if(pMe->err) { |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 250 | /* Already in error state. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 251 | return; |
| 252 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 253 | |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 254 | /* 0. Sanity check the UsefulOutBuf structure |
| 255 | * A "counter measure". If magic number is not the right number it |
| 256 | * probably means pMe was not initialized or it was corrupted. Attackers |
| 257 | * can defeat this, but it is a hurdle and does good with very |
| 258 | * little code. |
| 259 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 260 | if(pMe->magic != USEFUL_OUT_BUF_MAGIC) { |
| 261 | pMe->err = 1; |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 262 | return; /* Magic number is wrong due to uninitalization or corrption */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 265 | /* Make sure valid data is less than buffer size. This would only occur |
| 266 | * if there was corruption of me, but it is also part of the checks to |
| 267 | * be sure there is no pointer arithmatic under/overflow. |
| 268 | */ |
| 269 | if(pMe->data_len > pMe->UB.len) { /* Check #1 */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 270 | pMe->err = 1; |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 271 | /* Offset of valid data is off the end of the UsefulOutBuf due to |
| 272 | * uninitialization or corruption |
| 273 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 274 | return; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 275 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 276 | |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 277 | /* 1. Will it fit? |
| 278 | * WillItFit() is the same as: NewData.len <= (me->UB.len - me->data_len) |
| 279 | * Check #1 makes sure subtraction in RoomLeft will not wrap around |
| 280 | */ |
| 281 | if(! UsefulOutBuf_WillItFit(pMe, NewData.len)) { /* Check #2 */ |
| 282 | /* The new data will not fit into the the buffer. */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 283 | pMe->err = 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 284 | return; |
| 285 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 286 | |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 287 | /* 2. Check the Insertion Position |
| 288 | * This, with Check #1, also confirms that uInsertionPos <= me->data_len and |
| 289 | * that uInsertionPos + pMe->UB.ptr will not wrap around the end of the |
| 290 | * address space. |
| 291 | */ |
| 292 | if(uInsertionPos > pMe->data_len) { /* Check #3 */ |
| 293 | /* Off the end of the valid data in the buffer. */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 294 | pMe->err = 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 295 | return; |
| 296 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 297 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 298 | /* 3. Slide existing data to the right */ |
Maxim Zhukov | 9fab689 | 2022-12-20 20:16:07 +0300 | [diff] [blame] | 299 | if (!UsefulOutBuf_IsBufferNULL(pMe)) { |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 300 | uint8_t *pSourceOfMove = ((uint8_t *)pMe->UB.ptr) + uInsertionPos; /* PtrMath #1 */ |
| 301 | size_t uNumBytesToMove = pMe->data_len - uInsertionPos; /* PtrMath #2 */ |
| 302 | uint8_t *pDestinationOfMove = pSourceOfMove + NewData.len; /* PtrMath #3*/ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 303 | |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 304 | /* To know memmove won't go off end of destination, see PtrMath #4. |
| 305 | * Use memove because it handles overlapping buffers |
| 306 | */ |
Laurence Lundblade | 74f6841 | 2018-09-13 12:18:49 -0700 | [diff] [blame] | 307 | memmove(pDestinationOfMove, pSourceOfMove, uNumBytesToMove); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 308 | |
Maxim Zhukov | 9fab689 | 2022-12-20 20:16:07 +0300 | [diff] [blame] | 309 | /* 4. Put the new data in */ |
| 310 | uint8_t *pInsertionPoint = pSourceOfMove; |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 311 | /* To know memmove won't go off end of destination, see PtrMath #5 */ |
Laurence Lundblade | 5a6fec5 | 2022-12-25 11:28:43 -0700 | [diff] [blame] | 312 | if(NewData.ptr != NULL) { |
| 313 | memmove(pInsertionPoint, NewData.ptr, NewData.len); |
| 314 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 315 | } |
Maxim Zhukov | 9fab689 | 2022-12-20 20:16:07 +0300 | [diff] [blame] | 316 | |
Laurence Lundblade | 6120974 | 2019-11-08 13:16:43 -0800 | [diff] [blame] | 317 | pMe->data_len += NewData.len; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
| 321 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 322 | * Rationale that describes why the above pointer math is safe |
| 323 | * |
| 324 | * PtrMath #1 will never wrap around over because |
| 325 | * Check #0 in UsefulOutBuf_Init that me->UB.ptr + me->UB.len doesn't wrap |
| 326 | * Check #1 makes sure me->data_len is less than me->UB.len |
| 327 | * Check #3 makes sure uInsertionPos is less than me->data_len |
| 328 | * |
| 329 | * PtrMath #2 will never wrap around under because |
| 330 | * Check #3 makes sure uInsertionPos is less than me->data_len |
| 331 | * |
| 332 | * PtrMath #3 will never wrap around over because |
| 333 | * PtrMath #1 is checked resulting in pSourceOfMove being between me->UB.ptr and me->UB.ptr + me->data_len |
| 334 | * Check #2 that NewData.len will fit in the unused space left in me->UB |
| 335 | * |
| 336 | * PtrMath #4 will never wrap under because |
| 337 | * Calculation for extent or memmove is uRoomInDestination = me->UB.len - (uInsertionPos + NewData.len) |
| 338 | * Check #3 makes sure uInsertionPos is less than me->data_len |
| 339 | * Check #3 allows Check #2 to be refactored as NewData.Len > (me->size - uInsertionPos) |
| 340 | * This algebraically rearranges to me->size > uInsertionPos + NewData.len |
| 341 | * |
| 342 | * PtrMath #5 will never wrap under because |
| 343 | * Calculation for extent of memove is uRoomInDestination = me->UB.len - uInsertionPos; |
| 344 | * Check #1 makes sure me->data_len is less than me->size |
| 345 | * Check #3 makes sure uInsertionPos is less than me->data_len |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 346 | */ |
| 347 | |
| 348 | |
| 349 | /* |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 350 | * Public function for advancing data length. See qcbor/UsefulBuf.h |
| 351 | */ |
| 352 | void UsefulOutBuf_Advance(UsefulOutBuf *pMe, size_t uAmount) |
| 353 | { |
| 354 | /* This function is a trimmed down version of |
| 355 | * UsefulOutBuf_InsertUsefulBuf(). This could be combined with the |
| 356 | * code in UsefulOutBuf_InsertUsefulBuf(), but that would make |
| 357 | * UsefulOutBuf_InsertUsefulBuf() bigger and this will be very |
| 358 | * rarely used. |
| 359 | */ |
| 360 | |
| 361 | if(pMe->err) { |
| 362 | /* Already in error state. */ |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | /* 0. Sanity check the UsefulOutBuf structure |
| 367 | * |
| 368 | * A "counter measure". If magic number is not the right number it |
| 369 | * probably means me was not initialized or it was |
| 370 | * corrupted. Attackers can defeat this, but it is a hurdle and |
| 371 | * does good with very little code. |
| 372 | */ |
| 373 | if(pMe->magic != USEFUL_OUT_BUF_MAGIC) { |
| 374 | pMe->err = 1; |
| 375 | return; /* Magic number is wrong due to uninitalization or corrption */ |
| 376 | } |
| 377 | |
| 378 | /* Make sure valid data is less than buffer size. This would only |
| 379 | * occur if there was corruption of me, but it is also part of the |
| 380 | * checks to be sure there is no pointer arithmatic |
| 381 | * under/overflow. |
| 382 | */ |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 383 | if(pMe->data_len > pMe->UB.len) { /* Check #1 */ |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 384 | pMe->err = 1; |
| 385 | /* Offset of valid data is off the end of the UsefulOutBuf due |
| 386 | * to uninitialization or corruption. |
| 387 | */ |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | /* 1. Will it fit? |
| 392 | * |
| 393 | * WillItFit() is the same as: NewData.len <= (me->UB.len - |
| 394 | * me->data_len) Check #1 makes sure subtraction in RoomLeft will |
| 395 | * not wrap around |
| 396 | */ |
| 397 | if(! UsefulOutBuf_WillItFit(pMe, uAmount)) { /* Check #2 */ |
| 398 | /* The new data will not fit into the the buffer. */ |
| 399 | pMe->err = 1; |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | pMe->data_len += uAmount; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 408 | * Public function -- see UsefulBuf.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 409 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 410 | UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *pMe) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 411 | { |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 412 | if(pMe->err) { |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 413 | return NULLUsefulBufC; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 414 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 415 | |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 416 | if(pMe->magic != USEFUL_OUT_BUF_MAGIC) { |
| 417 | pMe->err = 1; |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 418 | return NULLUsefulBufC; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 419 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 420 | |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 421 | return (UsefulBufC){pMe->UB.ptr, pMe->data_len}; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
| 425 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 426 | * Public function -- see UsefulBuf.h |
| 427 | * |
| 428 | * Copy out the data accumulated in to the output buffer. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 429 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 430 | UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *pMe, UsefulBuf pDest) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 431 | { |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 432 | const UsefulBufC Tmp = UsefulOutBuf_OutUBuf(pMe); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 433 | if(UsefulBuf_IsNULLC(Tmp)) { |
| 434 | return NULLUsefulBufC; |
| 435 | } |
| 436 | return UsefulBuf_Copy(pDest, Tmp); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
Laurence Lundblade | e222674 | 2024-08-16 10:50:23 -0700 | [diff] [blame] | 440 | /* |
| 441 | * Public function -- see UsefulBuf.h |
| 442 | * |
| 443 | * Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 444 | */ |
| 445 | UsefulBufC UsefulOutBuf_SubString(UsefulOutBuf *pMe, |
| 446 | const size_t uStart, |
| 447 | const size_t uLen) |
| 448 | { |
| 449 | const UsefulBufC Tmp = UsefulOutBuf_OutUBuf(pMe); |
| 450 | |
| 451 | if(UsefulBuf_IsNULLC(Tmp)) { |
| 452 | return NULLUsefulBufC; |
| 453 | } |
| 454 | |
| 455 | if(uStart > Tmp.len) { |
| 456 | return NULLUsefulBufC; |
| 457 | } |
| 458 | |
| 459 | if(Tmp.len - uStart < uLen) { |
| 460 | return NULLUsefulBufC; |
| 461 | } |
| 462 | |
| 463 | UsefulBufC SubString; |
| 464 | SubString.ptr = (const uint8_t *)Tmp.ptr + uStart; |
| 465 | SubString.len = uLen; |
| 466 | |
| 467 | return SubString; |
| 468 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 469 | |
| 470 | |
| 471 | /* |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 472 | * Public function -- see UsefulBuf.h |
| 473 | * |
| 474 | * The core of UsefulInputBuf -- consume bytes without going off end of buffer. |
| 475 | * |
| 476 | * Code Reviewers: THIS FUNCTION DOES POINTER MATH |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 477 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 478 | const void * UsefulInputBuf_GetBytes(UsefulInputBuf *pMe, size_t uAmount) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 479 | { |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 480 | /* Already in error state. Do nothing. */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 481 | if(pMe->err) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 482 | return NULL; |
| 483 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 484 | |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 485 | if(!UsefulInputBuf_BytesAvailable(pMe, uAmount)) { |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 486 | /* Number of bytes asked for is more than available */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 487 | pMe->err = 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 488 | return NULL; |
| 489 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 490 | |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 491 | /* This is going to succeed */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 492 | const void * const result = ((const uint8_t *)pMe->UB.ptr) + pMe->cursor; |
Laurence Lundblade | b239c65 | 2024-05-26 08:07:05 -0700 | [diff] [blame] | 493 | /* Won't overflow because of check using UsefulInputBuf_BytesAvailable() */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 494 | pMe->cursor += uAmount; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 495 | return result; |
| 496 | } |
| 497 | |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * Public function -- see UsefulBuf.h |
| 501 | * |
| 502 | * Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 503 | */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 504 | int |
| 505 | UsefulInputBuf_Compare(UsefulInputBuf *pUInBuf, |
| 506 | const size_t uOffset1, |
| 507 | const size_t uLen1, |
| 508 | const size_t uOffset2, |
| 509 | const size_t uLen2) |
| 510 | { |
| 511 | UsefulBufC UB1; |
| 512 | UsefulBufC UB2; |
| 513 | |
| 514 | const size_t uInputSize = UsefulInputBuf_GetBufferLength(pUInBuf); |
| 515 | |
| 516 | /* Careful length check that works even if uLen1 + uOffset1 > SIZE_MAX */ |
| 517 | if(uOffset1 > uInputSize || uLen1 > uInputSize - uOffset1) { |
| 518 | return 1; |
| 519 | } |
| 520 | UB1.ptr = (const uint8_t *)pUInBuf->UB.ptr + uOffset1; |
| 521 | UB1.len = uLen1; |
| 522 | |
| 523 | /* Careful length check that works even if uLen2 + uOffset2 > SIZE_MAX */ |
| 524 | if(uOffset2 > uInputSize || uLen2 > uInputSize - uOffset2) { |
| 525 | return -1; |
| 526 | } |
| 527 | UB2.ptr = (const uint8_t *)pUInBuf->UB.ptr + uOffset2; |
| 528 | UB2.len = uLen2; |
| 529 | |
| 530 | return UsefulBuf_Compare(UB1, UB2); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | /* |
| 535 | * Public function -- see UsefulBuf.h |
| 536 | * |
| 537 | * Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 538 | */ |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame] | 539 | int UsefulOutBuf_Compare(UsefulOutBuf *pMe, |
| 540 | const size_t uStart1, const size_t uLen1, |
| 541 | const size_t uStart2, const size_t uLen2) |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 542 | { |
| 543 | const uint8_t *pBase; |
| 544 | const uint8_t *pEnd; |
| 545 | const uint8_t *p1; |
| 546 | const uint8_t *p2; |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame] | 547 | const uint8_t *p1End; |
| 548 | const uint8_t *p2End; |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 549 | int uComparison; |
| 550 | |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame] | 551 | pBase = pMe->UB.ptr; |
| 552 | pEnd = (const uint8_t *)pBase + pMe->data_len; |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 553 | p1 = pBase + uStart1; |
| 554 | p2 = pBase + uStart2; |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame] | 555 | p1End = p1 + uLen1; |
| 556 | p2End = p2 + uLen2; |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 557 | |
| 558 | uComparison = 0; |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame] | 559 | while(p1 < pEnd && p2 < pEnd && p1 < p1End && p2 < p2End) { |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 560 | uComparison = *p2 - *p1; |
| 561 | if(uComparison != 0) { |
| 562 | break;; |
| 563 | } |
| 564 | p1++; |
| 565 | p2++; |
| 566 | } |
| 567 | |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame] | 568 | if(uComparison == 0 && p1 != p1End && p2 != p2End) { |
| 569 | if(uLen1 > uLen2) { |
| 570 | uComparison = 1; |
| 571 | } else if(uLen2 < uLen1){ |
| 572 | uComparison = -1; |
| 573 | } else { |
| 574 | return 0; |
| 575 | } |
| 576 | } |
| 577 | |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 578 | return uComparison; |
| 579 | } |
| 580 | |
| 581 | |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame] | 582 | |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 583 | /** |
| 584 | * @brief Reverse order of bytes in a buffer. |
| 585 | * |
| 586 | * This reverses bytes starting at pStart, up to, but not including |
| 587 | * the byte at pEnd |
| 588 | */ |
| 589 | static void |
| 590 | UsefulOutBuf_Private_ReverseBytes(uint8_t *pStart, uint8_t *pEnd) |
| 591 | { |
| 592 | uint8_t uTmp; |
| 593 | |
| 594 | while(pStart < pEnd) { |
| 595 | pEnd--; |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame] | 596 | uTmp = *pStart; |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 597 | *pStart = *pEnd; |
| 598 | *pEnd = uTmp; |
| 599 | pStart++; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | |
| 604 | /* |
| 605 | * Public function -- see UsefulBuf.h |
| 606 | * |
| 607 | * Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 608 | */ |
| 609 | void UsefulOutBuf_Swap(UsefulOutBuf *pMe, size_t uStartOffset, size_t uPivotOffset, size_t uEndOffset) |
| 610 | { |
| 611 | uint8_t *pBase; |
| 612 | |
| 613 | if(uStartOffset > pMe->data_len || uPivotOffset > pMe->data_len || uEndOffset > pMe->data_len) { |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | if(uStartOffset > uPivotOffset || uStartOffset > uEndOffset || uPivotOffset > uEndOffset) { |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | /* This is the "reverse" algorithm to swap two memory regions */ |
| 622 | pBase = pMe->UB.ptr; |
| 623 | UsefulOutBuf_Private_ReverseBytes(pBase + uStartOffset, pBase + uPivotOffset); |
| 624 | UsefulOutBuf_Private_ReverseBytes(pBase + uPivotOffset, pBase + uEndOffset); |
| 625 | UsefulOutBuf_Private_ReverseBytes(pBase + uStartOffset, pBase + uEndOffset); |
| 626 | } |
| 627 | |
| 628 | |
| 629 | /* |
| 630 | * Public function -- see UsefulBuf.h |
| 631 | */ |
| 632 | UsefulBufC |
| 633 | UsefulOutBuf_OutUBufOffset(UsefulOutBuf *pMe, size_t uOffset) |
| 634 | { |
| 635 | UsefulBufC ReturnValue; |
| 636 | |
| 637 | ReturnValue = UsefulOutBuf_OutUBuf(pMe); |
| 638 | |
| 639 | if(UsefulBuf_IsNULLC(ReturnValue)) { |
| 640 | return NULLUsefulBufC; |
| 641 | } |
| 642 | |
| 643 | if(uOffset >= ReturnValue.len) { |
| 644 | return NULLUsefulBufC; |
| 645 | } |
| 646 | |
| 647 | ReturnValue.ptr = (const uint8_t *)ReturnValue.ptr + uOffset; |
| 648 | ReturnValue.len -= uOffset; |
| 649 | |
| 650 | return ReturnValue; |
| 651 | } |