Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
#include "internal/to_hex.h"
18
19
101k
#define DEFAULT_SEPARATOR ':'
20
2.46G
#define CH_ZERO '\0'
21
22
char *CRYPTO_strdup(const char *str, const char *file, int line)
23
107M
{
24
107M
    char *ret;
25
26
107M
    if (str == NULL)
27
0
        return NULL;
28
107M
    ret = CRYPTO_malloc(strlen(str) + 1, file, line);
29
107M
    if (ret != NULL)
30
107M
        strcpy(ret, str);
31
107M
    return ret;
32
107M
}
33
34
char *CRYPTO_strndup(const char *str, size_t s, const char *file, int line)
35
239M
{
36
239M
    size_t maxlen;
37
239M
    char *ret;
38
39
239M
    if (str == NULL)
40
0
        return NULL;
41
42
239M
    maxlen = OPENSSL_strnlen(str, s);
43
44
239M
    ret = CRYPTO_malloc(maxlen + 1, file, line);
45
239M
    if (ret) {
46
239M
        memcpy(ret, str, maxlen);
47
239M
        ret[maxlen] = CH_ZERO;
48
239M
    }
49
239M
    return ret;
50
239M
}
51
52
void *CRYPTO_memdup(const void *data, size_t siz, const char *file, int line)
53
57.4M
{
54
57.4M
    void *ret;
55
56
57.4M
    if (data == NULL || siz >= INT_MAX)
57
0
        return NULL;
58
59
57.4M
    ret = CRYPTO_malloc(siz, file, line);
60
57.4M
    if (ret == NULL)
61
0
        return NULL;
62
57.4M
    return memcpy(ret, data, siz);
63
57.4M
}
64
65
size_t OPENSSL_strnlen(const char *str, size_t maxlen)
66
449M
{
67
449M
    const char *p;
68
69
2.39G
    for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p)
70
1.94G
        ;
71
72
449M
    return p - str;
73
449M
}
74
75
size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
76
75.0M
{
77
75.0M
    size_t l = 0;
78
828M
    for (; size > 1 && *src; size--) {
79
753M
        *dst++ = *src++;
80
753M
        l++;
81
753M
    }
82
75.0M
    if (size)
83
75.0M
        *dst = CH_ZERO;
84
75.0M
    return l + strlen(src);
85
75.0M
}
86
87
size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
88
57.3M
{
89
57.3M
    size_t l = 0;
90
692M
    for (; size > 0 && *dst; size--, dst++)
91
635M
        l++;
92
57.3M
    return l + OPENSSL_strlcpy(dst, src, size);
93
57.3M
}
94
95
/**
96
 * @brief Converts a string to an unsigned long integer.
97
 *
98
 * This function attempts to convert a string representation of a number
99
 * to an unsigned long integer, given a specified base. It also provides
100
 * error checking and reports whether the conversion was successful.
101
 * This function is just a wrapper around the POSIX strtoul function with
102
 * additional error checking.  This implies that errno for the caller is set
103
 * on calls to this function.
104
 *
105
 * @param str The string containing the representation of the number.
106
 * @param endptr A pointer to a pointer to character. If not NULL, it is set
107
 *               to the character immediately following the number in the
108
 *               string.
109
 * @param base The base to use for the conversion, which must be between 2,
110
 *             and 36 inclusive, or be the special value 0. If the base is 0,
111
 *             the actual base is determined by the format of the initial
112
 *             characters of the string.
113
 * @param num A pointer to an unsigned long where the result of the
114
 *            conversion is stored.
115
 *
116
 * @return 1 if the conversion was successful, 0 otherwise. Conversion is
117
 *         considered unsuccessful if no digits were consumed or if an error
118
 *         occurred during conversion.
119
 *
120
 * @note It is the caller's responsibility to check if the conversion is
121
 *       correct based on the expected consumption of the string as reported
122
 *       by endptr.
123
 */
124
int OPENSSL_strtoul(const char *str, char **endptr, int base,
125
    unsigned long *num)
126
0
{
127
0
    char *tmp_endptr;
128
0
    char **internal_endptr = endptr == NULL ? &tmp_endptr : endptr;
129
130
0
    errno = 0;
131
132
0
    *internal_endptr = (char *)str;
133
134
0
    if (num == NULL)
135
0
        return 0;
136
137
0
    if (str == NULL)
138
0
        return 0;
139
140
    /* Fail on negative input */
141
0
    if (*str == '-')
142
0
        return 0;
143
144
0
    *num = strtoul(str, internal_endptr, base);
145
    /*
146
     * We return error from this function under the following conditions
147
     * 1) If strtoul itself returned an error in translation
148
     * 2) If the caller didn't pass in an endptr value, and **internal_endptr
149
     *    doesn't point to '\0'.  The implication here is that if the caller
150
     *    doesn't care how much of a string is consumed, they expect the entire
151
     *    string to be consumed.  As such, no pointing to the NULL terminator
152
     *    means there was some part of the string left over after translation
153
     * 3) If no bytes of the string were consumed
154
     */
155
0
    if (errno != 0 || (endptr == NULL && **internal_endptr != '\0') || (str == *internal_endptr))
156
0
        return 0;
157
158
0
    return 1;
159
0
}
160
161
int OPENSSL_hexchar2int(unsigned char c)
162
146
{
163
#ifdef CHARSET_EBCDIC
164
    c = os_toebcdic[c];
165
#endif
166
167
146
    switch (c) {
168
2
    case '0':
169
2
        return 0;
170
4
    case '1':
171
4
        return 1;
172
2
    case '2':
173
2
        return 2;
174
3
    case '3':
175
3
        return 3;
176
11
    case '4':
177
11
        return 4;
178
5
    case '5':
179
5
        return 5;
180
8
    case '6':
181
8
        return 6;
182
9
    case '7':
183
9
        return 7;
184
5
    case '8':
185
5
        return 8;
186
0
    case '9':
187
0
        return 9;
188
3
    case 'a':
189
9
    case 'A':
190
9
        return 0x0A;
191
4
    case 'b':
192
12
    case 'B':
193
12
        return 0x0B;
194
4
    case 'c':
195
11
    case 'C':
196
11
        return 0x0C;
197
6
    case 'd':
198
17
    case 'D':
199
17
        return 0x0D;
200
8
    case 'e':
201
8
    case 'E':
202
8
        return 0x0E;
203
14
    case 'f':
204
21
    case 'F':
205
21
        return 0x0F;
206
146
    }
207
19
    return -1;
208
146
}
209
210
static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
211
    const char *str, const char sep)
212
0
{
213
0
    unsigned char *q;
214
0
    unsigned char ch, cl;
215
0
    int chi, cli;
216
0
    const unsigned char *p;
217
0
    size_t cnt;
218
219
0
    for (p = (const unsigned char *)str, q = buf, cnt = 0; *p;) {
220
0
        ch = *p++;
221
        /* A separator of CH_ZERO means there is no separator */
222
0
        if (ch == sep && sep != CH_ZERO)
223
0
            continue;
224
0
        cl = *p++;
225
0
        if (!cl) {
226
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
227
0
            return 0;
228
0
        }
229
0
        cli = OPENSSL_hexchar2int(cl);
230
0
        chi = OPENSSL_hexchar2int(ch);
231
0
        if (cli < 0 || chi < 0) {
232
0
            ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
233
0
            return 0;
234
0
        }
235
0
        cnt++;
236
0
        if (q != NULL) {
237
0
            if (cnt > buf_n) {
238
0
                ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
239
0
                return 0;
240
0
            }
241
0
            *q++ = (unsigned char)((chi << 4) | cli);
242
0
        }
243
0
    }
244
245
0
    if (buflen != NULL)
246
0
        *buflen = cnt;
247
0
    return 1;
248
0
}
249
250
/*
251
 * Given a string of hex digits convert to a buffer
252
 */
253
int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
254
    const char *str, const char sep)
255
0
{
256
0
    return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
257
0
}
258
259
unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
260
    const char sep)
261
0
{
262
0
    unsigned char *buf;
263
0
    size_t buf_n, tmp_buflen;
264
265
0
    buf_n = strlen(str);
266
0
    if (buf_n <= 1) {
267
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
268
0
        return NULL;
269
0
    }
270
0
    buf_n /= 2;
271
0
    if ((buf = OPENSSL_malloc(buf_n)) == NULL)
272
0
        return NULL;
273
274
0
    if (buflen != NULL)
275
0
        *buflen = 0;
276
0
    tmp_buflen = 0;
277
0
    if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
278
0
        if (buflen != NULL)
279
0
            *buflen = (long)tmp_buflen;
280
0
        return buf;
281
0
    }
282
0
    OPENSSL_free(buf);
283
0
    return NULL;
284
0
}
285
286
unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
287
0
{
288
0
    return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
289
0
}
290
291
static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
292
    const unsigned char *buf, size_t buflen,
293
    const char sep)
294
121k
{
295
121k
    char *q;
296
121k
    int has_sep = (sep != CH_ZERO);
297
121k
    size_t i, len = has_sep ? buflen * 3 : 1 + buflen * 2;
298
299
121k
    if (len == 0)
300
0
        ++len;
301
121k
    if (strlength != NULL)
302
0
        *strlength = len;
303
121k
    if (str == NULL)
304
0
        return 1;
305
306
121k
    if (str_n < len) {
307
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
308
0
        return 0;
309
0
    }
310
311
121k
    q = str;
312
13.3M
    for (i = 0; i < buflen; i++) {
313
13.2M
        q += ossl_to_hex(q, buf[i]);
314
13.2M
        if (has_sep)
315
13.0M
            *q++ = sep;
316
13.2M
    }
317
121k
    if (has_sep && buflen > 0)
318
91.2k
        --q;
319
121k
    *q = CH_ZERO;
320
321
#ifdef CHARSET_EBCDIC
322
    ebcdic2ascii(str, str, q - str);
323
#endif
324
121k
    return 1;
325
121k
}
326
327
int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
328
    const unsigned char *buf, size_t buflen,
329
    const char sep)
330
0
{
331
0
    return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
332
0
}
333
334
char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
335
94.1k
{
336
94.1k
    char *tmp;
337
94.1k
    size_t tmp_n;
338
339
94.1k
    if (buflen == 0)
340
7.33k
        return OPENSSL_zalloc(1);
341
342
86.8k
    tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
343
86.8k
    if ((tmp = OPENSSL_malloc(tmp_n)) == NULL)
344
0
        return NULL;
345
346
86.8k
    if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
347
86.8k
        return tmp;
348
0
    OPENSSL_free(tmp);
349
0
    return NULL;
350
86.8k
}
351
352
/*
353
 * Given a buffer of length 'buflen' return a OPENSSL_malloc'ed string with
354
 * its hex representation @@@ (Contents of buffer are always kept in ASCII,
355
 * also on EBCDIC machines)
356
 */
357
char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
358
101k
{
359
101k
    return ossl_buf2hexstr_sep(buf, buflen, DEFAULT_SEPARATOR);
360
101k
}
361
362
int openssl_strerror_r(int errnum, char *buf, size_t buflen)
363
0
{
364
#if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(_WIN32_WCE)
365
    return !strerror_s(buf, buflen, errnum);
366
#elif defined(_GNU_SOURCE)
367
    char *err;
368
369
    /*
370
     * GNU strerror_r may not actually set buf.
371
     * It can return a pointer to some (immutable) static string in which case
372
     * buf is left unused.
373
     */
374
    err = strerror_r(errnum, buf, buflen);
375
    if (err == NULL || buflen == 0)
376
        return 0;
377
    /*
378
     * If err is statically allocated, err != buf and we need to copy the data.
379
     * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
380
     * since src and dest are not annotated with __restrict and the function
381
     * reads src byte for byte and writes to dest.
382
     * If err == buf we do not have to copy anything.
383
     */
384
    if (err != buf)
385
        OPENSSL_strlcpy(buf, err, buflen);
386
    return 1;
387
#elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
388
    /*
389
     * We can use "real" strerror_r. The OpenSSL version differs in that it
390
     * gives 1 on success and 0 on failure for consistency with other OpenSSL
391
     * functions. Real strerror_r does it the other way around
392
     */
393
0
    return !strerror_r(errnum, buf, buflen);
394
#else
395
    char *err;
396
397
    /* Fall back to non-thread safe strerror()...its all we can do */
398
    if (buflen < 2)
399
        return 0;
400
    err = strerror(errnum);
401
    /* Can this ever happen? */
402
    if (err == NULL)
403
        return 0;
404
    OPENSSL_strlcpy(buf, err, buflen);
405
    return 1;
406
#endif
407
0
}
408
409
int OPENSSL_strcasecmp(const char *s1, const char *s2)
410
247M
{
411
247M
    int t;
412
413
1.45G
    while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
414
1.42G
        if (*s1++ == '\0')
415
216M
            return 0;
416
30.9M
    return t;
417
247M
}
418
419
int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
420
6.79M
{
421
6.79M
    int t;
422
6.79M
    size_t i;
423
424
13.1M
    for (i = 0; i < n; i++)
425
11.2M
        if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
426
4.96M
            return t;
427
6.31M
        else if (*s1++ == '\0')
428
0
            return 0;
429
1.83M
    return 0;
430
6.79M
}
431
432
size_t ossl_to_hex(char *buf, uint8_t n)
433
229M
{
434
229M
    static const char hexdig[] = "0123456789ABCDEF";
435
436
229M
    return to_hex(buf, n, hexdig);
437
229M
}