/src/openssl32/crypto/o_str.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2003-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/e_os.h" |
11 | | #include <string.h> |
12 | | #include <limits.h> |
13 | | #include <openssl/crypto.h> |
14 | | #include "crypto/ctype.h" |
15 | | #include "internal/cryptlib.h" |
16 | | #include "internal/thread_once.h" |
17 | | |
18 | 58.5k | #define DEFAULT_SEPARATOR ':' |
19 | 2.18G | #define CH_ZERO '\0' |
20 | | |
21 | | char *CRYPTO_strdup(const char *str, const char* file, int line) |
22 | 112M | { |
23 | 112M | char *ret; |
24 | | |
25 | 112M | if (str == NULL) |
26 | 0 | return NULL; |
27 | 112M | ret = CRYPTO_malloc(strlen(str) + 1, file, line); |
28 | 112M | if (ret != NULL) |
29 | 112M | strcpy(ret, str); |
30 | 112M | return ret; |
31 | 112M | } |
32 | | |
33 | | char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line) |
34 | 232M | { |
35 | 232M | size_t maxlen; |
36 | 232M | char *ret; |
37 | | |
38 | 232M | if (str == NULL) |
39 | 0 | return NULL; |
40 | | |
41 | 232M | maxlen = OPENSSL_strnlen(str, s); |
42 | | |
43 | 232M | ret = CRYPTO_malloc(maxlen + 1, file, line); |
44 | 232M | if (ret) { |
45 | 232M | memcpy(ret, str, maxlen); |
46 | 232M | ret[maxlen] = CH_ZERO; |
47 | 232M | } |
48 | 232M | return ret; |
49 | 232M | } |
50 | | |
51 | | void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line) |
52 | 20.0M | { |
53 | 20.0M | void *ret; |
54 | | |
55 | 20.0M | if (data == NULL || siz >= INT_MAX) |
56 | 0 | return NULL; |
57 | | |
58 | 20.0M | ret = CRYPTO_malloc(siz, file, line); |
59 | 20.0M | if (ret == NULL) |
60 | 0 | return NULL; |
61 | 20.0M | return memcpy(ret, data, siz); |
62 | 20.0M | } |
63 | | |
64 | | size_t OPENSSL_strnlen(const char *str, size_t maxlen) |
65 | 407M | { |
66 | 407M | const char *p; |
67 | | |
68 | 2.13G | for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ; |
69 | | |
70 | 407M | return p - str; |
71 | 407M | } |
72 | | |
73 | | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size) |
74 | 51.2M | { |
75 | 51.2M | size_t l = 0; |
76 | 551M | for (; size > 1 && *src; size--) { |
77 | 500M | *dst++ = *src++; |
78 | 500M | l++; |
79 | 500M | } |
80 | 51.2M | if (size) |
81 | 51.1M | *dst = CH_ZERO; |
82 | 51.2M | return l + strlen(src); |
83 | 51.2M | } |
84 | | |
85 | | size_t OPENSSL_strlcat(char *dst, const char *src, size_t size) |
86 | 38.5M | { |
87 | 38.5M | size_t l = 0; |
88 | 490M | for (; size > 0 && *dst; size--, dst++) |
89 | 452M | l++; |
90 | 38.5M | return l + OPENSSL_strlcpy(dst, src, size); |
91 | 38.5M | } |
92 | | |
93 | | int OPENSSL_hexchar2int(unsigned char c) |
94 | 102 | { |
95 | | #ifdef CHARSET_EBCDIC |
96 | | c = os_toebcdic[c]; |
97 | | #endif |
98 | | |
99 | 102 | switch (c) { |
100 | 3 | case '0': |
101 | 3 | return 0; |
102 | 6 | case '1': |
103 | 6 | return 1; |
104 | 6 | case '2': |
105 | 6 | return 2; |
106 | 6 | case '3': |
107 | 6 | return 3; |
108 | 13 | case '4': |
109 | 13 | return 4; |
110 | 5 | case '5': |
111 | 5 | return 5; |
112 | 5 | case '6': |
113 | 5 | return 6; |
114 | 7 | case '7': |
115 | 7 | return 7; |
116 | 7 | case '8': |
117 | 7 | return 8; |
118 | 8 | case '9': |
119 | 8 | return 9; |
120 | 10 | case 'a': case 'A': |
121 | 10 | return 0x0A; |
122 | 2 | case 'b': case 'B': |
123 | 2 | return 0x0B; |
124 | 1 | case 'c': case 'C': |
125 | 1 | return 0x0C; |
126 | 6 | case 'd': case 'D': |
127 | 6 | return 0x0D; |
128 | 3 | case 'e': case 'E': |
129 | 3 | return 0x0E; |
130 | 4 | case 'f': case 'F': |
131 | 4 | return 0x0F; |
132 | 102 | } |
133 | 10 | return -1; |
134 | 102 | } |
135 | | |
136 | | static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen, |
137 | | const char *str, const char sep) |
138 | 0 | { |
139 | 0 | unsigned char *q; |
140 | 0 | unsigned char ch, cl; |
141 | 0 | int chi, cli; |
142 | 0 | const unsigned char *p; |
143 | 0 | size_t cnt; |
144 | |
|
145 | 0 | for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) { |
146 | 0 | ch = *p++; |
147 | | /* A separator of CH_ZERO means there is no separator */ |
148 | 0 | if (ch == sep && sep != CH_ZERO) |
149 | 0 | continue; |
150 | 0 | cl = *p++; |
151 | 0 | if (!cl) { |
152 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS); |
153 | 0 | return 0; |
154 | 0 | } |
155 | 0 | cli = OPENSSL_hexchar2int(cl); |
156 | 0 | chi = OPENSSL_hexchar2int(ch); |
157 | 0 | if (cli < 0 || chi < 0) { |
158 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT); |
159 | 0 | return 0; |
160 | 0 | } |
161 | 0 | cnt++; |
162 | 0 | if (q != NULL) { |
163 | 0 | if (cnt > buf_n) { |
164 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
165 | 0 | return 0; |
166 | 0 | } |
167 | 0 | *q++ = (unsigned char)((chi << 4) | cli); |
168 | 0 | } |
169 | 0 | } |
170 | | |
171 | 0 | if (buflen != NULL) |
172 | 0 | *buflen = cnt; |
173 | 0 | return 1; |
174 | 0 | } |
175 | | |
176 | | /* |
177 | | * Given a string of hex digits convert to a buffer |
178 | | */ |
179 | | int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen, |
180 | | const char *str, const char sep) |
181 | 0 | { |
182 | 0 | return hexstr2buf_sep(buf, buf_n, buflen, str, sep); |
183 | 0 | } |
184 | | |
185 | | unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen, |
186 | | const char sep) |
187 | 0 | { |
188 | 0 | unsigned char *buf; |
189 | 0 | size_t buf_n, tmp_buflen; |
190 | |
|
191 | 0 | buf_n = strlen(str); |
192 | 0 | if (buf_n <= 1) { |
193 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT); |
194 | 0 | return NULL; |
195 | 0 | } |
196 | 0 | buf_n /= 2; |
197 | 0 | if ((buf = OPENSSL_malloc(buf_n)) == NULL) |
198 | 0 | return NULL; |
199 | | |
200 | 0 | if (buflen != NULL) |
201 | 0 | *buflen = 0; |
202 | 0 | tmp_buflen = 0; |
203 | 0 | if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) { |
204 | 0 | if (buflen != NULL) |
205 | 0 | *buflen = (long)tmp_buflen; |
206 | 0 | return buf; |
207 | 0 | } |
208 | 0 | OPENSSL_free(buf); |
209 | 0 | return NULL; |
210 | 0 | } |
211 | | |
212 | | unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen) |
213 | 0 | { |
214 | 0 | return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR); |
215 | 0 | } |
216 | | |
217 | | static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength, |
218 | | const unsigned char *buf, size_t buflen, |
219 | | const char sep) |
220 | 74.6k | { |
221 | 74.6k | static const char hexdig[] = "0123456789ABCDEF"; |
222 | 74.6k | const unsigned char *p; |
223 | 74.6k | char *q; |
224 | 74.6k | size_t i; |
225 | 74.6k | int has_sep = (sep != CH_ZERO); |
226 | 74.6k | size_t len = has_sep ? buflen * 3 : 1 + buflen * 2; |
227 | | |
228 | 74.6k | if (len == 0) |
229 | 0 | ++len; |
230 | 74.6k | if (strlength != NULL) |
231 | 0 | *strlength = len; |
232 | 74.6k | if (str == NULL) |
233 | 0 | return 1; |
234 | | |
235 | 74.6k | if (str_n < len) { |
236 | 0 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER); |
237 | 0 | return 0; |
238 | 0 | } |
239 | | |
240 | 74.6k | q = str; |
241 | 19.0M | for (i = 0, p = buf; i < buflen; i++, p++) { |
242 | 19.0M | *q++ = hexdig[(*p >> 4) & 0xf]; |
243 | 19.0M | *q++ = hexdig[*p & 0xf]; |
244 | 19.0M | if (has_sep) |
245 | 18.8M | *q++ = sep; |
246 | 19.0M | } |
247 | 74.6k | if (has_sep && buflen > 0) |
248 | 51.4k | --q; |
249 | 74.6k | *q = CH_ZERO; |
250 | | |
251 | | #ifdef CHARSET_EBCDIC |
252 | | ebcdic2ascii(str, str, q - str); |
253 | | #endif |
254 | 74.6k | return 1; |
255 | 74.6k | } |
256 | | |
257 | | int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength, |
258 | | const unsigned char *buf, size_t buflen, |
259 | | const char sep) |
260 | 0 | { |
261 | 0 | return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep); |
262 | 0 | } |
263 | | |
264 | | char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep) |
265 | 81.8k | { |
266 | 81.8k | char *tmp; |
267 | 81.8k | size_t tmp_n; |
268 | | |
269 | 81.8k | if (buflen == 0) |
270 | 7.13k | return OPENSSL_zalloc(1); |
271 | | |
272 | 74.6k | tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2; |
273 | 74.6k | if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) |
274 | 0 | return NULL; |
275 | | |
276 | 74.6k | if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep)) |
277 | 74.6k | return tmp; |
278 | 0 | OPENSSL_free(tmp); |
279 | 0 | return NULL; |
280 | 74.6k | } |
281 | | |
282 | | |
283 | | /* |
284 | | * Given a buffer of length 'buflen' return a OPENSSL_malloc'ed string with |
285 | | * its hex representation @@@ (Contents of buffer are always kept in ASCII, |
286 | | * also on EBCDIC machines) |
287 | | */ |
288 | | char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen) |
289 | 58.5k | { |
290 | 58.5k | return ossl_buf2hexstr_sep(buf, buflen, DEFAULT_SEPARATOR); |
291 | 58.5k | } |
292 | | |
293 | | int openssl_strerror_r(int errnum, char *buf, size_t buflen) |
294 | 0 | { |
295 | | #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE) |
296 | | return !strerror_s(buf, buflen, errnum); |
297 | | #elif defined(_GNU_SOURCE) |
298 | | char *err; |
299 | | |
300 | | /* |
301 | | * GNU strerror_r may not actually set buf. |
302 | | * It can return a pointer to some (immutable) static string in which case |
303 | | * buf is left unused. |
304 | | */ |
305 | | err = strerror_r(errnum, buf, buflen); |
306 | | if (err == NULL || buflen == 0) |
307 | | return 0; |
308 | | /* |
309 | | * If err is statically allocated, err != buf and we need to copy the data. |
310 | | * If err points somewhere inside buf, OPENSSL_strlcpy can handle this, |
311 | | * since src and dest are not annotated with __restrict and the function |
312 | | * reads src byte for byte and writes to dest. |
313 | | * If err == buf we do not have to copy anything. |
314 | | */ |
315 | | if (err != buf) |
316 | | OPENSSL_strlcpy(buf, err, buflen); |
317 | | return 1; |
318 | | #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \ |
319 | | (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) |
320 | | /* |
321 | | * We can use "real" strerror_r. The OpenSSL version differs in that it |
322 | | * gives 1 on success and 0 on failure for consistency with other OpenSSL |
323 | | * functions. Real strerror_r does it the other way around |
324 | | */ |
325 | 0 | return !strerror_r(errnum, buf, buflen); |
326 | | #else |
327 | | char *err; |
328 | | |
329 | | /* Fall back to non-thread safe strerror()...its all we can do */ |
330 | | if (buflen < 2) |
331 | | return 0; |
332 | | err = strerror(errnum); |
333 | | /* Can this ever happen? */ |
334 | | if (err == NULL) |
335 | | return 0; |
336 | | OPENSSL_strlcpy(buf, err, buflen); |
337 | | return 1; |
338 | | #endif |
339 | 0 | } |
340 | | |
341 | | int OPENSSL_strcasecmp(const char *s1, const char *s2) |
342 | 241M | { |
343 | 241M | int t; |
344 | | |
345 | 1.40G | while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0) |
346 | 1.37G | if (*s1++ == '\0') |
347 | 210M | return 0; |
348 | 30.9M | return t; |
349 | 241M | } |
350 | | |
351 | | int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n) |
352 | 5.15M | { |
353 | 5.15M | int t; |
354 | 5.15M | size_t i; |
355 | | |
356 | 9.18M | for (i = 0; i < n; i++) |
357 | 8.03M | if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0) |
358 | 4.00M | return t; |
359 | 4.02M | else if (*s1++ == '\0') |
360 | 0 | return 0; |
361 | 1.14M | return 0; |
362 | 5.15M | } |