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