forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmz_secure_api.c
248 lines (217 loc) · 5.6 KB
/
mz_secure_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* mz_secure_api.h -- Secure string and memory API
part of the minizip-ng project
https://github.com/Maxar-Corp/minizip-ng
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include "mz.h"
#include "mz_secure_api.h"
/**
* NAME
* strncat_s
*
* DESCRIPTION
* This function concatenates at most count bytes
* from src to destination string
*
* INPUT PARAMETERS
* dest pointer to string that will be concatenated to.
*
* destMax maximum length of the dest buffer
*
* src pointer to the memory that will be concatenated from.
*
* count maximum number bytes of src to concatenate
*
* RETURN VALUE
* MZ_OK successful operation
* MZ_PARAM_ERROR parameter error
* MZ_BUF_ERROR buffer error
*
* COMMENTS
* dest is null terminated on error if space is available
*/
int32_t strncat_s(char* dest, size_t destMax, const char* src, size_t count)
{
char* header = dest;
size_t availableSize = destMax;
const char* overlapGuard = NULL;
if (destMax == 0 || destMax > STRING_MAX_LEN) {
return MZ_BUF_ERROR;
}
if (dest == NULL || src == NULL) {
if (dest != NULL) {
header[0] = '\0';
}
return MZ_PARAM_ERROR;
}
if (dest < src) {
overlapGuard = src;
while (availableSize > 0 && *dest != 0) {
if (dest == overlapGuard) {
header[0] = '\0';
return MZ_BUF_ERROR;
}
dest++;
availableSize--;
}
/* dest unterminated, return error. */
if (availableSize == 0) {
header[0] = '\0';
return MZ_BUF_ERROR;
}
/* if available > 0, then excute the strcat operation */
while ((*dest++ = *src++) != 0 && --availableSize > 0) {
if (dest == overlapGuard) {
header[0] = '\0';
return MZ_BUF_ERROR;
}
}
} else {
while (availableSize > 0 && *dest != '\0') {
dest++;
availableSize--;
}
/* dest unterminated, return error. */
if (availableSize == 0) {
header[0] = '\0';
return MZ_BUF_ERROR;
}
while (count != 0 && availableSize != 0) {
if (src == overlapGuard) {
header[0] = '\0';
return MZ_BUF_ERROR;
}
*dest++ = *src++;
--count;
--availableSize;
}
}
if (availableSize != 0) {
*dest = '\0';
}
return MZ_OK;
}
/**
* NAME
* strnlen_s
*
* DESCRIPTION
* This function returns the length of the input string or
* 0 if error.
*
* INPUT PARAMETERS
* s pointer to the input string
* maxLen maximum length of the string
*
* RETURN VALUE
* length of the string or 0 if error
*
*/
size_t strnlen_s(const char* s, size_t maxlen)
{
size_t len;
if (s == NULL || maxlen > STRING_MAX_LEN) {
return 0;
}
for (len = 0; len < maxlen; len++, s++) {
if (!*s) {
break;
}
}
return len;
}
/**
* NAME
* strncpy_s
*
* DESCRIPTION
* This function copies at most count bytes from src to dest, up to
* destMax including terminating null if space is available.
*
* INPUT PARAMETERS
* dest pointer to memory that will be copied from src.
*
* destMax maximum length of the dest buffer
*
* src pointer to the memory that will be copied to dest
*
* count maximum number bytes of src to copy
*
* RETURN VALUE
* MZ_OK successful operation
* MZ_PARAM_ERROR parameter error
* MZ_BUF_ERROR buffer overlap error
*
* COMMENTS
* dest is null terminated on error if space is available
*/
int32_t strncpy_s(char* dest, size_t destMax, const char* src, size_t count)
{
const char* overlapGuard = NULL;
size_t availableSize = destMax;
char* header = dest;
if (destMax == 0 || destMax > STRING_MAX_LEN) {
return MZ_PARAM_ERROR;
}
if (dest == NULL || src == NULL) {
if (dest != NULL) {
dest[0] = '\0';
}
return MZ_PARAM_ERROR;
}
if (count > STRING_MAX_LEN || count == 0) {
dest[0] = '\0';
return MZ_PARAM_ERROR;
}
if (dest < src) {
overlapGuard = src;
}
while ((*dest++ = *src++) != '\0' && --availableSize > 0) {
if (src == overlapGuard) {
header[0] = '\0';
return MZ_BUF_ERROR;
}
}
return MZ_OK;
}
/**
* NAME
* memcpy_s
*
* DESCRIPTION
* This function copies at most count bytes from src to dest, up to
* destMax.
*
* INPUT PARAMETERS
* dest pointer to memory that will be copied from src.
*
* destMax maximum length of the dest buffer
*
* src pointer to the memory that will be copied to dest
*
* count maximum number bytes of src to copy
*
* RETURN VALUE
* MZ_OK successful operation
* MZ_PARAM_ERROR parameter error
*/
int32_t memcpy_s(void* dest, size_t destMax, const void* src, size_t count)
{
if (count == 0) {
return MZ_OK;
}
if (dest == NULL || src == NULL) {
if (dest) {
memset(dest, 0, destMax);
}
return MZ_PARAM_ERROR;
}
if (destMax < count) {
memset(dest, 0, destMax);
return MZ_PARAM_ERROR;
}
memcpy(dest, src, count);
return MZ_OK;
}
/***************************************************************************/