Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/asn1/a_utf8.c
Line
Count
Source
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
20.2M
{
30
20.2M
    const unsigned char *p;
31
20.2M
    unsigned long value;
32
20.2M
    int ret;
33
20.2M
    if (len <= 0)
34
0
        return 0;
35
20.2M
    p = str;
36
37
    /* Check syntax and work out the encoded value (if correct) */
38
20.2M
    if ((*p & 0x80) == 0) {
39
19.6M
        value = *p++ & 0x7f;
40
19.6M
        ret = 1;
41
19.6M
    } else if ((*p & 0xe0) == 0xc0) {
42
198k
        if (len < 2)
43
3.86k
            return -1;
44
194k
        if ((p[1] & 0xc0) != 0x80)
45
6.49k
            return -3;
46
188k
        value = (*p++ & 0x1f) << 6;
47
188k
        value |= *p++ & 0x3f;
48
188k
        if (value < 0x80)
49
11.8k
            return -4;
50
176k
        ret = 2;
51
368k
    } else if ((*p & 0xf0) == 0xe0) {
52
301k
        if (len < 3)
53
2.16k
            return -1;
54
299k
        if (((p[1] & 0xc0) != 0x80)
55
293k
            || ((p[2] & 0xc0) != 0x80))
56
10.2k
            return -3;
57
289k
        value = (*p++ & 0xf) << 12;
58
289k
        value |= (*p++ & 0x3f) << 6;
59
289k
        value |= *p++ & 0x3f;
60
289k
        if (value < 0x800)
61
3.86k
            return -4;
62
285k
        if (is_unicode_surrogate(value))
63
1.81k
            return -2;
64
283k
        ret = 3;
65
283k
    } else if ((*p & 0xf8) == 0xf0) {
66
52.4k
        if (len < 4)
67
4.74k
            return -1;
68
47.6k
        if (((p[1] & 0xc0) != 0x80)
69
40.9k
            || ((p[2] & 0xc0) != 0x80)
70
37.7k
            || ((p[3] & 0xc0) != 0x80))
71
12.6k
            return -3;
72
34.9k
        value = ((unsigned long)(*p++ & 0x7)) << 18;
73
34.9k
        value |= (*p++ & 0x3f) << 12;
74
34.9k
        value |= (*p++ & 0x3f) << 6;
75
34.9k
        value |= *p++ & 0x3f;
76
34.9k
        if (value < 0x10000 || value >= UNICODE_LIMIT)
77
5.37k
            return -4;
78
29.5k
        ret = 4;
79
29.5k
    } else
80
14.9k
        return -2;
81
20.1M
    *val = value;
82
20.1M
    return ret;
83
20.2M
}
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
1.07G
{
95
1.07G
    if (!str)
96
538M
        len = 4; /* Maximum we will need */
97
539M
    else if (len <= 0)
98
0
        return -1;
99
1.07G
    if (value < 0x80) {
100
228M
        if (str)
101
114M
            *str = (unsigned char)value;
102
228M
        return 1;
103
228M
    }
104
849M
    if (value < 0x800) {
105
825M
        if (len < 2)
106
0
            return -1;
107
825M
        if (str) {
108
412M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
109
412M
            *str = (unsigned char)((value & 0x3f) | 0x80);
110
412M
        }
111
825M
        return 2;
112
825M
    }
113
24.0M
    if (value < 0x10000) {
114
24.0M
        if (is_unicode_surrogate(value))
115
1.10k
            return -2;
116
24.0M
        if (len < 3)
117
0
            return -1;
118
24.0M
        if (str) {
119
12.2M
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
120
12.2M
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
121
12.2M
            *str = (unsigned char)((value & 0x3f) | 0x80);
122
12.2M
        }
123
24.0M
        return 3;
124
24.0M
    }
125
32.3k
    if (value < UNICODE_LIMIT) {
126
31.4k
        if (len < 4)
127
0
            return -1;
128
31.4k
        if (str) {
129
16.0k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
130
16.0k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
131
16.0k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
132
16.0k
            *str = (unsigned char)((value & 0x3f) | 0x80);
133
16.0k
        }
134
31.4k
        return 4;
135
31.4k
    }
136
917
    return -2;
137
32.3k
}