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