| 1 | /*------------------------------------------------------------------ |
| 2 | * memmove32_s.c |
| 3 | * |
| 4 | * October 2008, Bo Berry |
| 5 | * |
| 6 | * Copyright (c) 2008-2011 Cisco Systems |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person |
| 10 | * obtaining a copy of this software and associated documentation |
| 11 | * files (the "Software"), to deal in the Software without |
| 12 | * restriction, including without limitation the rights to use, |
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or |
| 14 | * sell copies of the Software, and to permit persons to whom the |
| 15 | * Software is furnished to do so, subject to the following |
| 16 | * conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be |
| 19 | * included in all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 28 | * OTHER DEALINGS IN THE SOFTWARE. |
| 29 | *------------------------------------------------------------------ |
| 30 | */ |
| 31 | |
| 32 | #include "safeclib_private.h" |
| 33 | #include "safe_mem_constraint.h" |
| 34 | #include "mem_primitives_lib.h" |
| 35 | #include "safe_mem_lib.h" |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * NAME |
| 40 | * memmove32_s |
| 41 | * |
| 42 | * SYNOPSIS |
| 43 | * #include "safe_mem_lib.h" |
| 44 | * errno_t |
| 45 | * memmove32_s(uint32_t *dest, rsize_t dmax, |
| 46 | * const uint32_t *src, rsize_t smax) |
| 47 | * |
| 48 | * DESCRIPTION |
| 49 | * The memmove32_s function copies smax uint32_ts from the region |
| 50 | * pointed to by src into the region pointed to by dest. This |
| 51 | * copying takes place as if the smax uint32_ts from the region |
| 52 | * pointed to by src are first copied into a temporary array of |
| 53 | * smax uint32_ts that does not overlap the regions pointed to |
| 54 | * by dest or src, and then the smax uint32_ts from the temporary |
| 55 | * array are copied into the region pointed to by dest. |
| 56 | * |
| 57 | * EXTENSION TO |
| 58 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments |
| 59 | * and system software interfaces, Extensions to the C Library, |
| 60 | * Part I: Bounds-checking interfaces |
| 61 | * |
| 62 | * INPUT PARAMETERS |
| 63 | * dest pointer to the memory that will be replaced by src. |
| 64 | * |
| 65 | * dmax maximum length of the resulting dest, in uint32_t |
| 66 | * |
| 67 | * src pointer to the memory that will be copied |
| 68 | * to dest |
| 69 | * |
| 70 | * smax maximum number uint32_t of src that can be copied |
| 71 | * |
| 72 | * OUTPUT PARAMETERS |
| 73 | * dest is updated |
| 74 | * |
| 75 | * RUNTIME CONSTRAINTS |
| 76 | * Neither dest nor src shall be a null pointer. |
| 77 | * Neither dmax nor smax shall be 0. |
| 78 | * dmax shall not be greater than RSIZE_MAX_MEM. |
| 79 | * smax shall not be greater than dmax. |
| 80 | * If there is a runtime-constraint violation, the memmove_s function |
| 81 | * stores zeros in the first dmax characters of the regionpointed to |
| 82 | * by dest if dest is not a null pointer and dmax is not greater |
| 83 | * than RSIZE_MAX_MEM. |
| 84 | * |
| 85 | * RETURN VALUE |
| 86 | * EOK successful operation |
| 87 | * ESNULLP NULL pointer |
| 88 | * ESZEROL zero length |
| 89 | * ESLEMAX length exceeds max limit |
| 90 | * |
| 91 | * ALSO SEE |
| 92 | * memmove_s(), memmove16_s(), memcpy_s(), memcpy16_s() memcpy32_s() |
| 93 | * |
| 94 | */ |
| 95 | errno_t |
| 96 | memmove32_s (uint32_t *dest, rsize_t dmax, const uint32_t *src, rsize_t smax) |
| 97 | { |
| 98 | uint32_t *dp; |
| 99 | const uint32_t *sp; |
| 100 | |
| 101 | dp= dest; |
| 102 | sp = src; |
| 103 | |
| 104 | if (dp == NULL) { |
| 105 | invoke_safe_mem_constraint_handler("memove32_s: dest is null", |
| 106 | NULL, ESNULLP); |
| 107 | return (RCNEGATE(ESNULLP)); |
| 108 | } |
| 109 | |
| 110 | if (dmax == 0) { |
| 111 | invoke_safe_mem_constraint_handler("memove32_s: dest is zero", |
| 112 | NULL, ESZEROL); |
| 113 | return (RCNEGATE(ESZEROL)); |
| 114 | } |
| 115 | |
| 116 | if (dmax > RSIZE_MAX_MEM32) { |
| 117 | invoke_safe_mem_constraint_handler("memove32_s: dmax exceeds max", |
| 118 | NULL, ESLEMAX); |
| 119 | return (RCNEGATE(ESLEMAX)); |
| 120 | } |
| 121 | |
| 122 | if (smax == 0) { |
| 123 | mem_prim_set32(dp, dmax, 0); |
| 124 | invoke_safe_mem_constraint_handler("memove32_s: smax is 0", |
| 125 | NULL, ESZEROL); |
| 126 | return (RCNEGATE(ESZEROL)); |
| 127 | } |
| 128 | |
| 129 | if (smax > dmax) { |
| 130 | mem_prim_set32(dp, dmax, 0); |
| 131 | invoke_safe_mem_constraint_handler("memove32_s: smax exceeds dmax", |
| 132 | NULL, ESLEMAX); |
| 133 | return (RCNEGATE(ESLEMAX)); |
| 134 | } |
| 135 | |
| 136 | if (sp == NULL) { |
| 137 | mem_prim_set32(dp, dmax, 0); |
| 138 | invoke_safe_mem_constraint_handler("memove32_s: src is null", |
| 139 | NULL, ESNULLP); |
| 140 | return (RCNEGATE(ESNULLP)); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * now perform the copy |
| 145 | */ |
| 146 | mem_prim_move32(dp, sp, smax); |
| 147 | |
| 148 | return (RCNEGATE(EOK)); |
| 149 | } |
| 150 | EXPORT_SYMBOL(memmove32_s) |