Coverage Report

Created: 2025-08-11 07:04

/src/openssl32/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
18.2M
{
30
18.2M
    const unsigned char *p;
31
18.2M
    unsigned long value;
32
18.2M
    int ret;
33
18.2M
    if (len <= 0)
34
0
        return 0;
35
18.2M
    p = str;
36
37
    /* Check syntax and work out the encoded value (if correct) */
38
18.2M
    if ((*p & 0x80) == 0) {
39
17.8M
        value = *p++ & 0x7f;
40
17.8M
        ret = 1;
41
17.8M
    } else if ((*p & 0xe0) == 0xc0) {
42
150k
        if (len < 2)
43
3.16k
            return -1;
44
147k
        if ((p[1] & 0xc0) != 0x80)
45
7.67k
            return -3;
46
139k
        value = (*p++ & 0x1f) << 6;
47
139k
        value |= *p++ & 0x3f;
48
139k
        if (value < 0x80)
49
56.5k
            return -4;
50
83.1k
        ret = 2;
51
197k
    } else if ((*p & 0xf0) == 0xe0) {
52
144k
        if (len < 3)
53
1.67k
            return -1;
54
142k
        if (((p[1] & 0xc0) != 0x80)
55
142k
            || ((p[2] & 0xc0) != 0x80))
56
9.07k
            return -3;
57
133k
        value = (*p++ & 0xf) << 12;
58
133k
        value |= (*p++ & 0x3f) << 6;
59
133k
        value |= *p++ & 0x3f;
60
133k
        if (value < 0x800)
61
4.94k
            return -4;
62
128k
        if (is_unicode_surrogate(value))
63
1.48k
            return -2;
64
127k
        ret = 3;
65
127k
    } else if ((*p & 0xf8) == 0xf0) {
66
41.0k
        if (len < 4)
67
2.71k
            return -1;
68
38.3k
        if (((p[1] & 0xc0) != 0x80)
69
38.3k
            || ((p[2] & 0xc0) != 0x80)
70
38.3k
            || ((p[3] & 0xc0) != 0x80))
71
13.1k
            return -3;
72
25.2k
        value = ((unsigned long)(*p++ & 0x7)) << 18;
73
25.2k
        value |= (*p++ & 0x3f) << 12;
74
25.2k
        value |= (*p++ & 0x3f) << 6;
75
25.2k
        value |= *p++ & 0x3f;
76
25.2k
        if (value < 0x10000)
77
3.15k
            return -4;
78
22.0k
        ret = 4;
79
22.0k
    } else
80
11.8k
        return -2;
81
18.1M
    *val = value;
82
18.1M
    return ret;
83
18.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
846M
{
95
846M
    if (!str)
96
422M
        len = 4;                /* Maximum we will need */
97
423M
    else if (len <= 0)
98
0
        return -1;
99
846M
    if (value < 0x80) {
100
98.4M
        if (str)
101
49.4M
            *str = (unsigned char)value;
102
98.4M
        return 1;
103
98.4M
    }
104
748M
    if (value < 0x800) {
105
726M
        if (len < 2)
106
0
            return -1;
107
726M
        if (str) {
108
363M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
109
363M
            *str = (unsigned char)((value & 0x3f) | 0x80);
110
363M
        }
111
726M
        return 2;
112
726M
    }
113
21.3M
    if (value < 0x10000) {
114
21.3M
        if (is_unicode_surrogate(value))
115
1.80k
            return -2;
116
21.3M
        if (len < 3)
117
0
            return -1;
118
21.3M
        if (str) {
119
10.9M
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
120
10.9M
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
121
10.9M
            *str = (unsigned char)((value & 0x3f) | 0x80);
122
10.9M
        }
123
21.3M
        return 3;
124
21.3M
    }
125
29.6k
    if (value < UNICODE_LIMIT) {
126
23.7k
        if (len < 4)
127
0
            return -1;
128
23.7k
        if (str) {
129
12.8k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
130
12.8k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
131
12.8k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
132
12.8k
            *str = (unsigned char)((value & 0x3f) | 0x80);
133
12.8k
        }
134
23.7k
        return 4;
135
23.7k
    }
136
5.90k
    return -2;
137
29.6k
}