Coverage Report

Created: 2023-09-25 06:41

/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
15.5k
{
29
15.5k
    const unsigned char *p;
30
15.5k
    unsigned long value;
31
15.5k
    int ret;
32
15.5k
    if (len <= 0)
33
0
        return 0;
34
15.5k
    p = str;
35
36
    /* Check syntax and work out the encoded value (if correct) */
37
15.5k
    if ((*p & 0x80) == 0) {
38
13.6k
        value = *p++ & 0x7f;
39
13.6k
        ret = 1;
40
13.6k
    } else if ((*p & 0xe0) == 0xc0) {
41
462
        if (len < 2)
42
5
            return -1;
43
457
        if ((p[1] & 0xc0) != 0x80)
44
12
            return -3;
45
445
        value = (*p++ & 0x1f) << 6;
46
445
        value |= *p++ & 0x3f;
47
445
        if (value < 0x80)
48
2
            return -4;
49
443
        ret = 2;
50
1.41k
    } else if ((*p & 0xf0) == 0xe0) {
51
572
        if (len < 3)
52
7
            return -1;
53
565
        if (((p[1] & 0xc0) != 0x80)
54
565
            || ((p[2] & 0xc0) != 0x80))
55
23
            return -3;
56
542
        value = (*p++ & 0xf) << 12;
57
542
        value |= (*p++ & 0x3f) << 6;
58
542
        value |= *p++ & 0x3f;
59
542
        if (value < 0x800)
60
14
            return -4;
61
528
        ret = 3;
62
843
    } else if ((*p & 0xf8) == 0xf0) {
63
265
        if (len < 4)
64
3
            return -1;
65
262
        if (((p[1] & 0xc0) != 0x80)
66
262
            || ((p[2] & 0xc0) != 0x80)
67
262
            || ((p[3] & 0xc0) != 0x80))
68
23
            return -3;
69
239
        value = ((unsigned long)(*p++ & 0x7)) << 18;
70
239
        value |= (*p++ & 0x3f) << 12;
71
239
        value |= (*p++ & 0x3f) << 6;
72
239
        value |= *p++ & 0x3f;
73
239
        if (value < 0x10000)
74
19
            return -4;
75
220
        ret = 4;
76
578
    } else if ((*p & 0xfc) == 0xf8) {
77
288
        if (len < 5)
78
23
            return -1;
79
265
        if (((p[1] & 0xc0) != 0x80)
80
265
            || ((p[2] & 0xc0) != 0x80)
81
265
            || ((p[3] & 0xc0) != 0x80)
82
265
            || ((p[4] & 0xc0) != 0x80))
83
11
            return -3;
84
254
        value = ((unsigned long)(*p++ & 0x3)) << 24;
85
254
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
86
254
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
87
254
        value |= (*p++ & 0x3f) << 6;
88
254
        value |= *p++ & 0x3f;
89
254
        if (value < 0x200000)
90
25
            return -4;
91
229
        ret = 5;
92
290
    } else if ((*p & 0xfe) == 0xfc) {
93
260
        if (len < 6)
94
5
            return -1;
95
255
        if (((p[1] & 0xc0) != 0x80)
96
255
            || ((p[2] & 0xc0) != 0x80)
97
255
            || ((p[3] & 0xc0) != 0x80)
98
255
            || ((p[4] & 0xc0) != 0x80)
99
255
            || ((p[5] & 0xc0) != 0x80))
100
22
            return -3;
101
233
        value = ((unsigned long)(*p++ & 0x1)) << 30;
102
233
        value |= ((unsigned long)(*p++ & 0x3f)) << 24;
103
233
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
104
233
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
105
233
        value |= (*p++ & 0x3f) << 6;
106
233
        value |= *p++ & 0x3f;
107
233
        if (value < 0x4000000)
108
26
            return -4;
109
207
        ret = 6;
110
207
    } else
111
30
        return -2;
112
15.2k
    *val = value;
113
15.2k
    return ret;
114
15.5k
}
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
19.8M
{
126
19.8M
    if (!str)
127
9.92M
        len = 6;                /* Maximum we will need */
128
9.93M
    else if (len <= 0)
129
0
        return -1;
130
19.8M
    if (value < 0x80) {
131
1.20M
        if (str)
132
604k
            *str = (unsigned char)value;
133
1.20M
        return 1;
134
1.20M
    }
135
18.6M
    if (value < 0x800) {
136
18.6M
        if (len < 2)
137
0
            return -1;
138
18.6M
        if (str) {
139
9.32M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
140
9.32M
            *str = (unsigned char)((value & 0x3f) | 0x80);
141
9.32M
        }
142
18.6M
        return 2;
143
18.6M
    }
144
8.30k
    if (value < 0x10000) {
145
1.82k
        if (len < 3)
146
0
            return -1;
147
1.82k
        if (str) {
148
1.04k
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
149
1.04k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
150
1.04k
            *str = (unsigned char)((value & 0x3f) | 0x80);
151
1.04k
        }
152
1.82k
        return 3;
153
1.82k
    }
154
6.48k
    if (value < 0x200000) {
155
474
        if (len < 4)
156
0
            return -1;
157
474
        if (str) {
158
238
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
159
238
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
160
238
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
161
238
            *str = (unsigned char)((value & 0x3f) | 0x80);
162
238
        }
163
474
        return 4;
164
474
    }
165
6.00k
    if (value < 0x4000000) {
166
694
        if (len < 5)
167
0
            return -1;
168
694
        if (str) {
169
350
            *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
170
350
            *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
171
350
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
172
350
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
173
350
            *str = (unsigned char)((value & 0x3f) | 0x80);
174
350
        }
175
694
        return 5;
176
694
    }
177
5.31k
    if (len < 6)
178
0
        return -1;
179
5.31k
    if (str) {
180
2.69k
        *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
181
2.69k
        *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
182
2.69k
        *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
183
2.69k
        *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
184
2.69k
        *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
185
2.69k
        *str = (unsigned char)((value & 0x3f) | 0x80);
186
2.69k
    }
187
5.31k
    return 6;
188
5.31k
}