Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/asn1/a_utf8.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 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
15
/* UTF8 utilities */
16
17
/*-
18
 * This parses a UTF8 string one character at a time. It is passed a pointer
19
 * to the string and the length of the string. It sets 'value' to the value of
20
 * the current character. It returns the number of characters read or a
21
 * negative error code:
22
 * -1 = string too short
23
 * -2 = illegal character
24
 * -3 = subsequent characters not of the form 10xxxxxx
25
 * -4 = character encoded incorrectly (not minimal length).
26
 */
27
28
int UTF8_getc(const unsigned char *str, int len, unsigned long *val)
29
6.21M
{
30
6.21M
    const unsigned char *p;
31
6.21M
    unsigned long value;
32
6.21M
    int ret;
33
6.21M
    if (len <= 0)
34
0
        return 0;
35
6.21M
    p = str;
36
37
    /* Check syntax and work out the encoded value (if correct) */
38
6.21M
    if ((*p & 0x80) == 0) {
39
5.71M
        value = *p++ & 0x7f;
40
5.71M
        ret = 1;
41
5.71M
    } else if ((*p & 0xe0) == 0xc0) {
42
167k
        if (len < 2)
43
3.31k
            return -1;
44
163k
        if ((p[1] & 0xc0) != 0x80)
45
8.01k
            return -3;
46
155k
        value = (*p++ & 0x1f) << 6;
47
155k
        value |= *p++ & 0x3f;
48
155k
        if (value < 0x80)
49
3.26k
            return -4;
50
152k
        ret = 2;
51
333k
    } else if ((*p & 0xf0) == 0xe0) {
52
253k
        if (len < 3)
53
2.69k
            return -1;
54
250k
        if (((p[1] & 0xc0) != 0x80)
55
250k
            || ((p[2] & 0xc0) != 0x80))
56
9.19k
            return -3;
57
241k
        value = (*p++ & 0xf) << 12;
58
241k
        value |= (*p++ & 0x3f) << 6;
59
241k
        value |= *p++ & 0x3f;
60
241k
        if (value < 0x800)
61
7.66k
            return -4;
62
233k
        if (is_unicode_surrogate(value))
63
1.74k
            return -2;
64
232k
        ret = 3;
65
232k
    } else if ((*p & 0xf8) == 0xf0) {
66
40.2k
        if (len < 4)
67
2.83k
            return -1;
68
37.4k
        if (((p[1] & 0xc0) != 0x80)
69
37.4k
            || ((p[2] & 0xc0) != 0x80)
70
37.4k
            || ((p[3] & 0xc0) != 0x80))
71
16.4k
            return -3;
72
20.9k
        value = ((unsigned long)(*p++ & 0x7)) << 18;
73
20.9k
        value |= (*p++ & 0x3f) << 12;
74
20.9k
        value |= (*p++ & 0x3f) << 6;
75
20.9k
        value |= *p++ & 0x3f;
76
20.9k
        if (value < 0x10000)
77
3.77k
            return -4;
78
17.2k
        ret = 4;
79
17.2k
    } else
80
39.9k
        return -2;
81
6.11M
    *val = value;
82
6.11M
    return ret;
83
6.21M
}
84
85
/*
86
 * This takes a character 'value' and writes the UTF8 encoded value in 'str'
87
 * where 'str' is a buffer containing 'len' characters. Returns the number of
88
 * characters written, -1 if 'len' is too small or -2 if 'value' is out of
89
 * range. 'str' can be set to NULL in which case it just returns the number of
90
 * characters. It will need at most 4 characters.
91
 */
92
93
int UTF8_putc(unsigned char *str, int len, unsigned long value)
94
612M
{
95
612M
    if (!str)
96
306M
        len = 4;                /* Maximum we will need */
97
306M
    else if (len <= 0)
98
0
        return -1;
99
612M
    if (value < 0x80) {
100
39.5M
        if (str)
101
19.9M
            *str = (unsigned char)value;
102
39.5M
        return 1;
103
39.5M
    }
104
573M
    if (value < 0x800) {
105
556M
        if (len < 2)
106
0
            return -1;
107
556M
        if (str) {
108
278M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
109
278M
            *str = (unsigned char)((value & 0x3f) | 0x80);
110
278M
        }
111
556M
        return 2;
112
556M
    }
113
17.0M
    if (value < 0x10000) {
114
17.0M
        if (is_unicode_surrogate(value))
115
1.64k
            return -2;
116
17.0M
        if (len < 3)
117
0
            return -1;
118
17.0M
        if (str) {
119
8.60M
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
120
8.60M
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
121
8.60M
            *str = (unsigned char)((value & 0x3f) | 0x80);
122
8.60M
        }
123
17.0M
        return 3;
124
17.0M
    }
125
20.2k
    if (value < UNICODE_LIMIT) {
126
16.1k
        if (len < 4)
127
0
            return -1;
128
16.1k
        if (str) {
129
8.43k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
130
8.43k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
131
8.43k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
132
8.43k
            *str = (unsigned char)((value & 0x3f) | 0x80);
133
8.43k
        }
134
16.1k
        return 4;
135
16.1k
    }
136
4.06k
    return -2;
137
20.2k
}