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