Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/asn1/a_utf8.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include "internal/unicode.h"
13
#include <openssl/asn1.h>
14
#include <crypto/asn1.h>
15
16
/* UTF8 utilities */
17
18
/*-
19
 * This parses a UTF8 string one character at a time. It is passed a pointer
20
 * to the string and the length of the string. It sets 'value' to the value of
21
 * the current character. It returns the number of characters read or a
22
 * negative error code:
23
 * -1 = string too short
24
 * -2 = illegal character
25
 * -3 = subsequent characters not of the form 10xxxxxx
26
 * -4 = character encoded incorrectly (not minimal length).
27
 */
28
29
int ossl_utf8_getc_internal(const unsigned char *str, int len, uint32_t *val)
30
90.2M
{
31
90.2M
    const unsigned char *p;
32
90.2M
    uint32_t value;
33
90.2M
    int ret;
34
90.2M
    if (len <= 0)
35
0
        return 0;
36
90.2M
    p = str;
37
38
    /* Check syntax and work out the encoded value (if correct) */
39
90.2M
    if ((*p & 0x80) == 0) {
40
77.7M
        value = *p++ & 0x7f;
41
77.7M
        ret = 1;
42
77.7M
    } else if ((*p & 0xe0) == 0xc0) {
43
12.3M
        if (len < 2)
44
1.82k
            return -1;
45
12.3M
        if ((p[1] & 0xc0) != 0x80)
46
1.31k
            return -3;
47
12.3M
        value = (*p++ & 0x1f) << 6;
48
12.3M
        value |= *p++ & 0x3f;
49
12.3M
        if (value < 0x80)
50
1.17k
            return -4;
51
12.3M
        ret = 2;
52
12.3M
    } else if ((*p & 0xf0) == 0xe0) {
53
122k
        if (len < 3)
54
1.22k
            return -1;
55
121k
        if (((p[1] & 0xc0) != 0x80)
56
115k
            || ((p[2] & 0xc0) != 0x80))
57
8.21k
            return -3;
58
113k
        value = (*p++ & 0xf) << 12;
59
113k
        value |= (*p++ & 0x3f) << 6;
60
113k
        value |= *p++ & 0x3f;
61
113k
        if (value < 0x800)
62
1.56k
            return -4;
63
111k
        if (is_unicode_surrogate(value))
64
9.35k
            return -2;
65
102k
        ret = 3;
66
102k
    } else if ((*p & 0xf8) == 0xf0) {
67
21.3k
        if (len < 4)
68
6.11k
            return -1;
69
15.1k
        if (((p[1] & 0xc0) != 0x80)
70
12.4k
            || ((p[2] & 0xc0) != 0x80)
71
11.4k
            || ((p[3] & 0xc0) != 0x80))
72
4.95k
            return -3;
73
10.2k
        value = ((unsigned long)(*p++ & 0x7)) << 18;
74
10.2k
        value |= (*p++ & 0x3f) << 12;
75
10.2k
        value |= (*p++ & 0x3f) << 6;
76
10.2k
        value |= *p++ & 0x3f;
77
10.2k
        if (value < 0x10000 || value >= UNICODE_LIMIT)
78
1.33k
            return -4;
79
8.90k
        ret = 4;
80
8.90k
    } else
81
4.79k
        return -2;
82
90.1M
    *val = value;
83
90.1M
    return ret;
84
90.2M
}
85
86
#if !defined(OPENSSL_NO_DEPRECATED_4_1)
87
int UTF8_getc(const unsigned char *str, int len, unsigned long *val)
88
0
{
89
0
    uint32_t value = 0;
90
0
    int ret;
91
92
0
    ret = ossl_utf8_getc_internal(str, len, &value);
93
94
0
    if (ret)
95
0
        *val = (unsigned long)value;
96
97
0
    return ret;
98
0
}
99
#endif /* !defined(OPENSSL_NO_DEPRECATED_4_1) */
100
101
/*
102
 * This takes a character 'value' and writes the UTF8 encoded value in 'str'
103
 * where 'str' is a buffer containing 'len' characters. Returns the number of
104
 * characters written, -1 if 'len' is too small or -2 if 'value' is out of
105
 * range. 'str' can be set to NULL in which case it just returns the number of
106
 * characters. It will need at most 4 characters.
107
 */
108
109
int ossl_utf8_putc_internal(unsigned char *str, int len, uint32_t value)
110
453M
{
111
453M
    if (!str)
112
226M
        len = 4; /* Maximum we will need */
113
226M
    else if (len <= 0)
114
0
        return -1;
115
453M
    if (value < 0x80) {
116
147M
        if (str)
117
73.6M
            *str = (unsigned char)value;
118
147M
        return 1;
119
147M
    }
120
306M
    if (value < 0x800) {
121
293M
        if (len < 2)
122
0
            return -1;
123
293M
        if (str) {
124
147M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
125
147M
            *str = (unsigned char)((value & 0x3f) | 0x80);
126
147M
        }
127
293M
        return 2;
128
293M
    }
129
12.3M
    if (value < 0x10000) {
130
12.3M
        if (is_unicode_surrogate(value))
131
183
            return -2;
132
12.3M
        if (len < 3)
133
0
            return -1;
134
12.3M
        if (str) {
135
6.26M
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
136
6.26M
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
137
6.26M
            *str = (unsigned char)((value & 0x3f) | 0x80);
138
6.26M
        }
139
12.3M
        return 3;
140
12.3M
    }
141
13.4k
    if (value < UNICODE_LIMIT) {
142
12.9k
        if (len < 4)
143
0
            return -1;
144
12.9k
        if (str) {
145
6.66k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
146
6.66k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
147
6.66k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
148
6.66k
            *str = (unsigned char)((value & 0x3f) | 0x80);
149
6.66k
        }
150
12.9k
        return 4;
151
12.9k
    }
152
473
    return -2;
153
13.4k
}
154
155
#if !defined(OPENSSL_NO_DEPRECATED_4_1)
156
int UTF8_putc(unsigned char *str, int len, unsigned long value)
157
0
{
158
0
    return ossl_utf8_putc_internal(str, len, (uint32_t)value);
159
0
}
160
#endif