Coverage Report

Created: 2026-09-12 06:55

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
8.57M
{
31
8.57M
    const unsigned char *p;
32
8.57M
    uint32_t value;
33
8.57M
    int ret;
34
8.57M
    if (len <= 0)
35
0
        return 0;
36
8.57M
    p = str;
37
38
    /* Check syntax and work out the encoded value (if correct) */
39
8.57M
    if ((*p & 0x80) == 0) {
40
6.07M
        value = *p++ & 0x7f;
41
6.07M
        ret = 1;
42
6.07M
    } else if ((*p & 0xe0) == 0xc0) {
43
2.36M
        if (len < 2)
44
1.49k
            return -1;
45
2.36M
        if ((p[1] & 0xc0) != 0x80)
46
1.21k
            return -3;
47
2.36M
        value = (*p++ & 0x1f) << 6;
48
2.36M
        value |= *p++ & 0x3f;
49
2.36M
        if (value < 0x80)
50
601
            return -4;
51
2.36M
        ret = 2;
52
2.36M
    } else if ((*p & 0xf0) == 0xe0) {
53
121k
        if (len < 3)
54
1.22k
            return -1;
55
120k
        if (((p[1] & 0xc0) != 0x80)
56
113k
            || ((p[2] & 0xc0) != 0x80))
57
8.55k
            return -3;
58
111k
        value = (*p++ & 0xf) << 12;
59
111k
        value |= (*p++ & 0x3f) << 6;
60
111k
        value |= *p++ & 0x3f;
61
111k
        if (value < 0x800)
62
1.73k
            return -4;
63
109k
        if (is_unicode_surrogate(value))
64
9.34k
            return -2;
65
100k
        ret = 3;
66
100k
    } else if ((*p & 0xf8) == 0xf0) {
67
17.2k
        if (len < 4)
68
6.02k
            return -1;
69
11.2k
        if (((p[1] & 0xc0) != 0x80)
70
8.29k
            || ((p[2] & 0xc0) != 0x80)
71
7.24k
            || ((p[3] & 0xc0) != 0x80))
72
4.85k
            return -3;
73
6.36k
        value = ((unsigned long)(*p++ & 0x7)) << 18;
74
6.36k
        value |= (*p++ & 0x3f) << 12;
75
6.36k
        value |= (*p++ & 0x3f) << 6;
76
6.36k
        value |= *p++ & 0x3f;
77
6.36k
        if (value < 0x10000 || value >= UNICODE_LIMIT)
78
1.29k
            return -4;
79
5.07k
        ret = 4;
80
5.07k
    } else
81
3.96k
        return -2;
82
8.53M
    *val = value;
83
8.53M
    return ret;
84
8.57M
}
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
231M
{
111
231M
    if (!str)
112
115M
        len = 4; /* Maximum we will need */
113
115M
    else if (len <= 0)
114
0
        return -1;
115
231M
    if (value < 0x80) {
116
39.3M
        if (str)
117
19.7M
            *str = (unsigned char)value;
118
39.3M
        return 1;
119
39.3M
    }
120
192M
    if (value < 0x800) {
121
184M
        if (len < 2)
122
0
            return -1;
123
184M
        if (str) {
124
92.2M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
125
92.2M
            *str = (unsigned char)((value & 0x3f) | 0x80);
126
92.2M
        }
127
184M
        return 2;
128
184M
    }
129
7.78M
    if (value < 0x10000) {
130
7.77M
        if (is_unicode_surrogate(value))
131
130
            return -2;
132
7.77M
        if (len < 3)
133
0
            return -1;
134
7.77M
        if (str) {
135
3.90M
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
136
3.90M
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
137
3.90M
            *str = (unsigned char)((value & 0x3f) | 0x80);
138
3.90M
        }
139
7.77M
        return 3;
140
7.77M
    }
141
6.26k
    if (value < UNICODE_LIMIT) {
142
6.01k
        if (len < 4)
143
0
            return -1;
144
6.01k
        if (str) {
145
3.16k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
146
3.16k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
147
3.16k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
148
3.16k
            *str = (unsigned char)((value & 0x3f) | 0x80);
149
3.16k
        }
150
6.01k
        return 4;
151
6.01k
    }
152
259
    return -2;
153
6.26k
}
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