Debian Codegraph @

ext/safestringlib/safeclib/wmemmove_s.c

raw symbols SCIP
1 /*------------------------------------------------------------------
2 * wmemmove_s.c
3 *
4 * August 2014, D Wheeler
5 *
6 * Copyright (c) 2014 Intel Corp
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 #include <wchar.h>
37
38 /**
39 * NAME
40 * wmemmove_s
41 *
42 * SYNOPSIS
43 * #include "safe_mem_lib.h"
44 * errno_t
45 * wmemmove_s(wchar_t* dest, rsize_t dmax,
46 * const wchar_t* src, size_t smax)
47 *
48 * DESCRIPTION
49 * The wmemmove_s function copies smax wide characters from the region pointed
50 * to by src into the region pointed to by dest. This copying takes place
51 * as if the smax wide characters from the region pointed to by src are first copied
52 * into a temporary array of smax bytes that does not overlap the region
53 * pointed to by dest or src, and then the smax bytes from the temporary
54 * array are copied into the object region to by dest.
55 *
56 * SPECIFIED IN
57 * ISO/IEC TR 24731, Programming languages, environments
58 * and system software interfaces, Extensions to the C Library,
59 * Part I: Bounds-checking interfaces
60 *
61 * INPUT PARAMETERS
62 * dest pointer to the memory that will be replaced by src.
63 *
64 * dmax maximum number of resulting wide characters in dest
65 *
66 * src pointer to the memory that will be copied
67 * to dest
68 *
69 * smax maximum number wide characters of src that can be copied
70 *
71 * OUTPUT PARAMETERS
72 * dest is updated
73 *
74 * RUNTIME CONSTRAINTS
75 * Neither dest nor src shall be a null pointer.
76 * Neither dmax nor smax shall be 0.
77 * dmax shall not be greater than RSIZE_MAX_MEM/sizeof(wchar_t).
78 * smax shall not be greater than dmax.
79 * If there is a runtime-constraint violation, the wmemmove_s function
80 * stores zeros in the first dmax characters of the region pointed to
81 * by dest if dest is not a null pointer and dmax is not greater
82 * than RSIZE_MAX_MEM/sizeof(wchar_t).
83 *
84 * RETURN VALUE
85 * EOK successful operation
86 * ESNULLP NULL pointer
87 * ESZEROL zero length
88 * ESLEMAX length exceeds max limit
89 *
90 * ALSO SEE
91 * memmove16_s(), memmove32_s(), memcpy_s(), memcpy16_s() memcpy32_s()
92 * wmemcpy_s()
93 *
94 */
95 errno_t
96 wmemmove_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, size_t smax)
97 {
98 wchar_t *dp;
99 const wchar_t *sp;
100
101 dp= dest;
102 sp = src;
103
104 if (dp == NULL) {
105 invoke_safe_mem_constraint_handler("wmemmove_s: dest is null",
106 NULL, ESNULLP);
107 return (RCNEGATE(ESNULLP));
108 }
109
110 if (dmax == 0) {
111 invoke_safe_mem_constraint_handler("wmemmove_s: dmax is 0",
112 NULL, ESZEROL);
113 return (RCNEGATE(ESZEROL));
114 }
115
116 if (dmax*sizeof(wchar_t) > RSIZE_MAX_MEM) {
117 invoke_safe_mem_constraint_handler("wmemmove_s: dmax exceeds max",
118 NULL, ESLEMAX);
119 return (RCNEGATE(ESLEMAX));
120 }
121
122 if (smax == 0) {
123 mem_prim_set(dp, dmax*sizeof(wchar_t), 0);
124 invoke_safe_mem_constraint_handler("wmemmove_s: smax is 0",
125 NULL, ESZEROL);
126 return (RCNEGATE(ESZEROL));
127 }
128
129 if (smax > dmax) {
130 mem_prim_set(dp, dmax*sizeof(wchar_t), 0);
131 invoke_safe_mem_constraint_handler("wmemmove_s: smax exceeds max",
132 NULL, ESLEMAX);
133 return (RCNEGATE(ESLEMAX));
134 }
135
136 if (sp == NULL) {
137 mem_prim_set(dp, dmax*sizeof(wchar_t), 0);
138 invoke_safe_mem_constraint_handler("wmemmove_s: src is null",
139 NULL, ESNULLP);
140 return (RCNEGATE(ESNULLP));
141 }
142
143 /*
144 * now perform the copy
145 */
146 mem_prim_move(dp, sp, smax*sizeof(wchar_t));
147
148 return (RCNEGATE(EOK));
149 }
150 EXPORT_SYMBOL(wmemmove_s)