Coverage Report

Created: 2023-09-25 06:42

/src/openssl111/crypto/asn1/a_utf8.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 <openssl/asn1.h>
13
14
/* UTF8 utilities */
15
16
/*-
17
 * This parses a UTF8 string one character at a time. It is passed a pointer
18
 * to the string and the length of the string. It sets 'value' to the value of
19
 * the current character. It returns the number of characters read or a
20
 * negative error code:
21
 * -1 = string too short
22
 * -2 = illegal character
23
 * -3 = subsequent characters not of the form 10xxxxxx
24
 * -4 = character encoded incorrectly (not minimal length).
25
 */
26
27
int UTF8_getc(const unsigned char *str, int len, unsigned long *val)
28
197k
{
29
197k
    const unsigned char *p;
30
197k
    unsigned long value;
31
197k
    int ret;
32
197k
    if (len <= 0)
33
0
        return 0;
34
197k
    p = str;
35
36
    /* Check syntax and work out the encoded value (if correct) */
37
197k
    if ((*p & 0x80) == 0) {
38
188k
        value = *p++ & 0x7f;
39
188k
        ret = 1;
40
188k
    } else if ((*p & 0xe0) == 0xc0) {
41
1.29k
        if (len < 2)
42
333
            return -1;
43
959
        if ((p[1] & 0xc0) != 0x80)
44
309
            return -3;
45
650
        value = (*p++ & 0x1f) << 6;
46
650
        value |= *p++ & 0x3f;
47
650
        if (value < 0x80)
48
201
            return -4;
49
449
        ret = 2;
50
7.90k
    } else if ((*p & 0xf0) == 0xe0) {
51
2.16k
        if (len < 3)
52
379
            return -1;
53
1.78k
        if (((p[1] & 0xc0) != 0x80)
54
1.78k
            || ((p[2] & 0xc0) != 0x80))
55
431
            return -3;
56
1.35k
        value = (*p++ & 0xf) << 12;
57
1.35k
        value |= (*p++ & 0x3f) << 6;
58
1.35k
        value |= *p++ & 0x3f;
59
1.35k
        if (value < 0x800)
60
202
            return -4;
61
1.15k
        ret = 3;
62
5.73k
    } else if ((*p & 0xf8) == 0xf0) {
63
1.27k
        if (len < 4)
64
195
            return -1;
65
1.08k
        if (((p[1] & 0xc0) != 0x80)
66
1.08k
            || ((p[2] & 0xc0) != 0x80)
67
1.08k
            || ((p[3] & 0xc0) != 0x80))
68
630
            return -3;
69
452
        value = ((unsigned long)(*p++ & 0x7)) << 18;
70
452
        value |= (*p++ & 0x3f) << 12;
71
452
        value |= (*p++ & 0x3f) << 6;
72
452
        value |= *p++ & 0x3f;
73
452
        if (value < 0x10000)
74
202
            return -4;
75
250
        ret = 4;
76
4.46k
    } else if ((*p & 0xfc) == 0xf8) {
77
1.90k
        if (len < 5)
78
213
            return -1;
79
1.69k
        if (((p[1] & 0xc0) != 0x80)
80
1.69k
            || ((p[2] & 0xc0) != 0x80)
81
1.69k
            || ((p[3] & 0xc0) != 0x80)
82
1.69k
            || ((p[4] & 0xc0) != 0x80))
83
912
            return -3;
84
783
        value = ((unsigned long)(*p++ & 0x3)) << 24;
85
783
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
86
783
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
87
783
        value |= (*p++ & 0x3f) << 6;
88
783
        value |= *p++ & 0x3f;
89
783
        if (value < 0x200000)
90
217
            return -4;
91
566
        ret = 5;
92
2.55k
    } else if ((*p & 0xfe) == 0xfc) {
93
1.88k
        if (len < 6)
94
238
            return -1;
95
1.64k
        if (((p[1] & 0xc0) != 0x80)
96
1.64k
            || ((p[2] & 0xc0) != 0x80)
97
1.64k
            || ((p[3] & 0xc0) != 0x80)
98
1.64k
            || ((p[4] & 0xc0) != 0x80)
99
1.64k
            || ((p[5] & 0xc0) != 0x80))
100
1.14k
            return -3;
101
504
        value = ((unsigned long)(*p++ & 0x1)) << 30;
102
504
        value |= ((unsigned long)(*p++ & 0x3f)) << 24;
103
504
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
104
504
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
105
504
        value |= (*p++ & 0x3f) << 6;
106
504
        value |= *p++ & 0x3f;
107
504
        if (value < 0x4000000)
108
256
            return -4;
109
248
        ret = 6;
110
248
    } else
111
667
        return -2;
112
191k
    *val = value;
113
191k
    return ret;
114
197k
}
115
116
/*
117
 * This takes a character 'value' and writes the UTF8 encoded value in 'str'
118
 * where 'str' is a buffer containing 'len' characters. Returns the number of
119
 * characters written or -1 if 'len' is too small. 'str' can be set to NULL
120
 * in which case it just returns the number of characters. It will need at
121
 * most 6 characters.
122
 */
123
124
int UTF8_putc(unsigned char *str, int len, unsigned long value)
125
13.8M
{
126
13.8M
    if (!str)
127
6.89M
        len = 6;                /* Maximum we will need */
128
6.94M
    else if (len <= 0)
129
0
        return -1;
130
13.8M
    if (value < 0x80) {
131
1.26M
        if (str)
132
635k
            *str = (unsigned char)value;
133
1.26M
        return 1;
134
1.26M
    }
135
12.5M
    if (value < 0x800) {
136
12.3M
        if (len < 2)
137
0
            return -1;
138
12.3M
        if (str) {
139
6.16M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
140
6.16M
            *str = (unsigned char)((value & 0x3f) | 0x80);
141
6.16M
        }
142
12.3M
        return 2;
143
12.3M
    }
144
239k
    if (value < 0x10000) {
145
73.5k
        if (len < 3)
146
0
            return -1;
147
73.5k
        if (str) {
148
59.8k
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
149
59.8k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
150
59.8k
            *str = (unsigned char)((value & 0x3f) | 0x80);
151
59.8k
        }
152
73.5k
        return 3;
153
73.5k
    }
154
165k
    if (value < 0x200000) {
155
14.1k
        if (len < 4)
156
0
            return -1;
157
14.1k
        if (str) {
158
7.07k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
159
7.07k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
160
7.07k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
161
7.07k
            *str = (unsigned char)((value & 0x3f) | 0x80);
162
7.07k
        }
163
14.1k
        return 4;
164
14.1k
    }
165
151k
    if (value < 0x4000000) {
166
27.6k
        if (len < 5)
167
0
            return -1;
168
27.6k
        if (str) {
169
13.8k
            *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
170
13.8k
            *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
171
13.8k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
172
13.8k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
173
13.8k
            *str = (unsigned char)((value & 0x3f) | 0x80);
174
13.8k
        }
175
27.6k
        return 5;
176
27.6k
    }
177
123k
    if (len < 6)
178
0
        return -1;
179
123k
    if (str) {
180
61.9k
        *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
181
61.9k
        *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
182
61.9k
        *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
183
61.9k
        *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
184
61.9k
        *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
185
61.9k
        *str = (unsigned char)((value & 0x3f) | 0x80);
186
61.9k
    }
187
123k
    return 6;
188
123k
}