Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 2 | Copyright (c) 2016-2018, The Linux Foundation. |
Laurence Lundblade | f0ea5f3 | 2019-01-11 20:10:26 -0800 | [diff] [blame] | 3 | Copyright (c) 2018-2019, Laurence Lundblade. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 4 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are |
| 7 | met: |
| 8 | * Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | * Redistributions in binary form must reproduce the above |
| 11 | copyright notice, this list of conditions and the following |
| 12 | disclaimer in the documentation and/or other materials provided |
| 13 | with the distribution. |
| 14 | * Neither the name of The Linux Foundation nor the names of its |
| 15 | contributors, nor the name "Laurence Lundblade" may be used to |
| 16 | endorse or promote products derived from this software without |
| 17 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 18 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 19 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 20 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 23 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 26 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 28 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 29 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 30 | ==============================================================================*/ |
| 31 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 32 | /*=================================================================================== |
| 33 | FILE: UsefulBuf.c |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 34 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 35 | DESCRIPTION: General purpose input and output buffers |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 36 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 37 | EDIT HISTORY FOR FILE: |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 38 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 39 | This section contains comments describing changes made to the module. |
| 40 | Notice that changes are listed in reverse chronological order. |
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 | when who what, where, why |
| 43 | -------- ---- --------------------------------------------------- |
Laurence Lundblade | 6120974 | 2019-11-08 13:16:43 -0800 | [diff] [blame] | 44 | 11/08/2019 llundblade Re check pointer math and update comments |
Laurence Lundblade | d5e101e | 2019-03-06 17:23:18 -0800 | [diff] [blame] | 45 | 3/6/2019 llundblade Add UsefulBuf_IsValue() |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 46 | 09/07/17 llundbla Fix critical bug in UsefulBuf_Find() -- a read off |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 47 | the end of memory when the bytes to find is longer |
| 48 | than the bytes to search. |
| 49 | 06/27/17 llundbla Fix UsefulBuf_Compare() bug. Only affected comparison |
| 50 | for < or > for unequal length buffers. Added |
| 51 | UsefulBuf_Set() function. |
| 52 | 05/30/17 llundbla Functions for NULL UsefulBufs and const / unconst |
| 53 | 11/13/16 llundbla Initial Version. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 54 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 55 | =====================================================================================*/ |
| 56 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 57 | #include "UsefulBuf.h" |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 58 | |
| 59 | #define USEFUL_OUT_BUF_MAGIC (0x0B0F) // used to catch use of uninitialized or corrupted UOBs |
| 60 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 61 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 62 | /* |
| 63 | Public function -- see UsefulBuf.h |
| 64 | */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 65 | UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src) |
| 66 | { |
Laurence Lundblade | 7566b9f | 2018-10-12 09:13:32 +0800 | [diff] [blame] | 67 | // Do this with subtraction so it doesn't give erroneous result if uOffset + Src.len overflows |
| 68 | if(uOffset > Dest.len || Src.len > Dest.len - uOffset) { // uOffset + Src.len > Dest.len |
| 69 | return NULLUsefulBufC; |
| 70 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 71 | |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 72 | memcpy((uint8_t *)Dest.ptr + uOffset, Src.ptr, Src.len); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 73 | |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 74 | return (UsefulBufC){Dest.ptr, Src.len + uOffset}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 75 | } |
| 76 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 77 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 78 | /* |
| 79 | Public function -- see UsefulBuf.h |
| 80 | */ |
| 81 | int UsefulBuf_Compare(const UsefulBufC UB1, const UsefulBufC UB2) |
| 82 | { |
| 83 | // use the comparisons rather than subtracting lengths to |
| 84 | // return an int instead of a size_t |
| 85 | if(UB1.len < UB2.len) { |
| 86 | return -1; |
| 87 | } else if (UB1.len > UB2.len) { |
| 88 | return 1; |
| 89 | } // else UB1.len == UB2.len |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 90 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 91 | return memcmp(UB1.ptr, UB2.ptr, UB1.len); |
| 92 | } |
| 93 | |
| 94 | |
Laurence Lundblade | d5e101e | 2019-03-06 17:23:18 -0800 | [diff] [blame] | 95 | /* |
| 96 | Public function -- see UsefulBuf.h |
| 97 | */ |
| 98 | size_t UsefulBuf_IsValue(const UsefulBufC UB, uint8_t uValue) |
| 99 | { |
| 100 | if(UsefulBuf_IsNULLOrEmptyC(UB)) { |
| 101 | /* Not a match */ |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | const uint8_t * const pEnd = (uint8_t *)UB.ptr + UB.len; |
| 106 | for(const uint8_t *p = UB.ptr; p < pEnd; p++) { |
| 107 | if(*p != uValue) { |
| 108 | /* Byte didn't match */ |
| 109 | return p - (uint8_t *)UB.ptr; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Success. All bytes matched */ |
| 114 | return SIZE_MAX; |
| 115 | } |
| 116 | |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 117 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 118 | /* |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 119 | Public function -- see UsefulBuf.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 120 | */ |
| 121 | size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind) |
| 122 | { |
| 123 | if(BytesToSearch.len < BytesToFind.len) { |
| 124 | return SIZE_MAX; |
| 125 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 126 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 127 | for(size_t uPos = 0; uPos <= BytesToSearch.len - BytesToFind.len; uPos++) { |
| 128 | if(!UsefulBuf_Compare((UsefulBufC){((uint8_t *)BytesToSearch.ptr) + uPos, BytesToFind.len}, BytesToFind)) { |
| 129 | return uPos; |
| 130 | } |
| 131 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 132 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 133 | return SIZE_MAX; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /* |
| 138 | Public function -- see UsefulBuf.h |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 139 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 140 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 141 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 142 | void UsefulOutBuf_Init(UsefulOutBuf *pMe, UsefulBuf Storage) |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 143 | { |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 144 | pMe->magic = USEFUL_OUT_BUF_MAGIC; |
| 145 | UsefulOutBuf_Reset(pMe); |
| 146 | pMe->UB = Storage; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 147 | |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 148 | #if 0 |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 149 | // This check is off by default. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 150 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 151 | // The following check fails on ThreadX |
| 152 | |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 153 | // Sanity check on the pointer and size to be sure we are not |
| 154 | // passed a buffer that goes off the end of the address space. |
| 155 | // Given this test, we know that all unsigned lengths less than |
| 156 | // me->size are valid and won't wrap in any pointer additions |
| 157 | // based off of pStorage in the rest of this code. |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 158 | const uintptr_t ptrM = UINTPTR_MAX - Storage.len; |
| 159 | if(Storage.ptr && (uintptr_t)Storage.ptr > ptrM) // Check #0 |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 160 | me->err = 1; |
| 161 | #endif |
| 162 | } |
| 163 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 164 | |
| 165 | |
| 166 | /* |
| 167 | Public function -- see UsefulBuf.h |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 168 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 169 | The core of UsefulOutBuf -- put some bytes in the buffer without writing off the end of it. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 170 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 171 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 172 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 173 | This function inserts the source buffer, NewData, into the destination buffer, me->UB.ptr. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 174 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 175 | Destination is represented as: |
| 176 | me->UB.ptr -- start of the buffer |
Laurence Lundblade | 9e7f474 | 2018-09-20 18:50:31 -0700 | [diff] [blame] | 177 | me->UB.len -- size of the buffer UB.ptr |
| 178 | me->data_len -- length of value data in UB |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 179 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 180 | Source is data: |
| 181 | NewData.ptr -- start of source buffer |
| 182 | NewData.len -- length of source buffer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 183 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 184 | Insertion point: |
| 185 | uInsertionPos. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 186 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 187 | Steps: |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 188 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 189 | 0. Corruption checks on UsefulOutBuf |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 190 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 191 | 1. Figure out if the new data will fit or not |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 192 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 193 | 2. Is insertion position in the range of valid data? |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 194 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 195 | 3. If insertion point is not at the end, slide data to the right of the insertion point to the right |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 196 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 197 | 4. Put the new data in at the insertion position. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 198 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 199 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 200 | void UsefulOutBuf_InsertUsefulBuf(UsefulOutBuf *pMe, UsefulBufC NewData, size_t uInsertionPos) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 201 | { |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 202 | if(pMe->err) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 203 | // Already in error state. |
| 204 | return; |
| 205 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 206 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 207 | /* 0. Sanity check the UsefulOutBuf structure */ |
| 208 | // A "counter measure". If magic number is not the right number it |
| 209 | // probably means me was not initialized or it was corrupted. Attackers |
| 210 | // can defeat this, but it is a hurdle and does good with very |
| 211 | // little code. |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 212 | if(pMe->magic != USEFUL_OUT_BUF_MAGIC) { |
| 213 | pMe->err = 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 214 | return; // Magic number is wrong due to uninitalization or corrption |
| 215 | } |
| 216 | |
| 217 | // Make sure valid data is less than buffer size. This would only occur |
| 218 | // if there was corruption of me, but it is also part of the checks to |
| 219 | // be sure there is no pointer arithmatic under/overflow. |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 220 | if(pMe->data_len > pMe->UB.len) { // Check #1 |
| 221 | pMe->err = 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 222 | return; // Offset of valid data is off the end of the UsefulOutBuf due to uninitialization or corruption |
| 223 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 224 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 225 | /* 1. Will it fit? */ |
Laurence Lundblade | 6120974 | 2019-11-08 13:16:43 -0800 | [diff] [blame] | 226 | // WillItFit() is the same as: NewData.len <= (me->UB.len - me->data_len) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 227 | // Check #1 makes sure subtraction in RoomLeft will not wrap around |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 228 | if(! UsefulOutBuf_WillItFit(pMe, NewData.len)) { // Check #2 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 229 | // The new data will not fit into the the buffer. |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 230 | pMe->err = 1; |
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 | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 234 | /* 2. Check the Insertion Position */ |
Laurence Lundblade | 6120974 | 2019-11-08 13:16:43 -0800 | [diff] [blame] | 235 | // This, with Check #1, also confirms that uInsertionPos <= me->data_len and |
| 236 | // that uInsertionPos + pMe->UB.ptr will not wrap around the end of the |
| 237 | // address space. |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 238 | if(uInsertionPos > pMe->data_len) { // Check #3 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 239 | // Off the end of the valid data in the buffer. |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 240 | pMe->err = 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 241 | return; |
| 242 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 243 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 244 | /* 3. Slide existing data to the right */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 245 | uint8_t *pSourceOfMove = ((uint8_t *)pMe->UB.ptr) + uInsertionPos; // PtrMath #1 |
| 246 | size_t uNumBytesToMove = pMe->data_len - uInsertionPos; // PtrMath #2 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 247 | uint8_t *pDestinationOfMove = pSourceOfMove + NewData.len; // PtrMath #3 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 248 | |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 249 | if(uNumBytesToMove && pMe->UB.ptr) { |
Laurence Lundblade | 56a7932 | 2019-01-10 09:12:37 -0800 | [diff] [blame] | 250 | // To know memmove won't go off end of destination, see PtrMath #4 |
Laurence Lundblade | 6120974 | 2019-11-08 13:16:43 -0800 | [diff] [blame] | 251 | // Use memove because it handles overlapping buffers |
Laurence Lundblade | 74f6841 | 2018-09-13 12:18:49 -0700 | [diff] [blame] | 252 | memmove(pDestinationOfMove, pSourceOfMove, uNumBytesToMove); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 253 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 254 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 255 | /* 4. Put the new data in */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 256 | uint8_t *pInsertionPoint = ((uint8_t *)pMe->UB.ptr) + uInsertionPos; // PtrMath #5 |
| 257 | if(pMe->UB.ptr) { |
Laurence Lundblade | 56a7932 | 2019-01-10 09:12:37 -0800 | [diff] [blame] | 258 | // To know memmove won't go off end of destination, see PtrMath #6 |
Laurence Lundblade | 74f6841 | 2018-09-13 12:18:49 -0700 | [diff] [blame] | 259 | memmove(pInsertionPoint, NewData.ptr, NewData.len); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 260 | } |
Laurence Lundblade | 6120974 | 2019-11-08 13:16:43 -0800 | [diff] [blame] | 261 | pMe->data_len += NewData.len; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | |
| 265 | /* |
| 266 | Rationale that describes why the above pointer math is safe |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 267 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 268 | PtrMath #1 will never wrap around over because |
Laurence Lundblade | 56a7932 | 2019-01-10 09:12:37 -0800 | [diff] [blame] | 269 | Check #0 in UsefulOutBuf_Init makes sure me->UB.ptr + me->UB.len doesn't wrap |
| 270 | Check #1 makes sure me->data_len is less than me->UB.len |
| 271 | Check #3 makes sure uInsertionPos is less than me->data_len |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 272 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 273 | PtrMath #2 will never wrap around under because |
Laurence Lundblade | 56a7932 | 2019-01-10 09:12:37 -0800 | [diff] [blame] | 274 | Check #3 makes sure uInsertionPos is less than me->data_len |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 275 | |
Laurence Lundblade | 6120974 | 2019-11-08 13:16:43 -0800 | [diff] [blame] | 276 | PtrMath #3 will never wrap around over because |
| 277 | PtrMath #1 is checked resulting in pSourceOfMove being between me->UB.ptr and me->UB.ptr + me->data_len |
| 278 | Check #2 that NewData.len will fit in the unused space left in me->UB |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 279 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 280 | PtrMath #4 will never wrap under because |
Laurence Lundblade | 56a7932 | 2019-01-10 09:12:37 -0800 | [diff] [blame] | 281 | Calculation for extent or memmove is uRoomInDestination = me->UB.len - (uInsertionPos + NewData.len) |
Laurence Lundblade | 9e7f474 | 2018-09-20 18:50:31 -0700 | [diff] [blame] | 282 | Check #3 makes sure uInsertionPos is less than me->data_len |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 283 | Check #3 allows Check #2 to be refactored as NewData.Len > (me->size - uInsertionPos) |
| 284 | This algebraically rearranges to me->size > uInsertionPos + NewData.len |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 285 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 286 | PtrMath #5 is exactly the same as PtrMath #1 |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 287 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 288 | PtrMath #6 will never wrap under because |
Laurence Lundblade | 56a7932 | 2019-01-10 09:12:37 -0800 | [diff] [blame] | 289 | Calculation for extent of memove is uRoomInDestination = me->UB.len - uInsertionPos; |
| 290 | Check #1 makes sure me->data_len is less than me->size |
| 291 | Check #3 makes sure uInsertionPos is less than me->data_len |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 292 | */ |
| 293 | |
| 294 | |
| 295 | /* |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 296 | Public function -- see UsefulBuf.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 297 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 298 | UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *pMe) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 299 | { |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 300 | if(pMe->err) { |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 301 | return NULLUsefulBufC; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 302 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 303 | |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 304 | if(pMe->magic != USEFUL_OUT_BUF_MAGIC) { |
| 305 | pMe->err = 1; |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 306 | return NULLUsefulBufC; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 307 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 308 | |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 309 | return (UsefulBufC){pMe->UB.ptr, pMe->data_len}; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
| 313 | /* |
| 314 | Public function -- see UsefulBuf.h |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 315 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 316 | Copy out the data accumulated in to the output buffer. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 317 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 318 | UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *pMe, UsefulBuf pDest) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 319 | { |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 320 | const UsefulBufC Tmp = UsefulOutBuf_OutUBuf(pMe); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 321 | if(UsefulBuf_IsNULLC(Tmp)) { |
| 322 | return NULLUsefulBufC; |
| 323 | } |
| 324 | return UsefulBuf_Copy(pDest, Tmp); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | /* |
| 331 | Public function -- see UsefulBuf.h |
| 332 | |
| 333 | The core of UsefulInputBuf -- consume some bytes without going off the end of the buffer. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 334 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 335 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 336 | */ |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 337 | const void * UsefulInputBuf_GetBytes(UsefulInputBuf *pMe, size_t uAmount) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 338 | { |
| 339 | // Already in error state. Do nothing. |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 340 | if(pMe->err) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 341 | return NULL; |
| 342 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 343 | |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 344 | if(!UsefulInputBuf_BytesAvailable(pMe, uAmount)) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 345 | // The number of bytes asked for at current position are more than available |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 346 | pMe->err = 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 347 | return NULL; |
| 348 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 349 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 350 | // This is going to succeed |
Laurence Lundblade | 5cc2568 | 2019-03-26 21:58:35 +0100 | [diff] [blame] | 351 | const void * const result = ((uint8_t *)pMe->UB.ptr) + pMe->cursor; |
| 352 | pMe->cursor += uAmount; // this will not overflow because of check using UsefulInputBuf_BytesAvailable() |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 353 | return result; |
| 354 | } |
| 355 | |