Coverage Report

Created: 2023-09-25 06:45

/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
770k
{
29
770k
    const unsigned char *p;
30
770k
    unsigned long value;
31
770k
    int ret;
32
770k
    if (len <= 0)
33
0
        return 0;
34
770k
    p = str;
35
36
    /* Check syntax and work out the encoded value (if correct) */
37
770k
    if ((*p & 0x80) == 0) {
38
732k
        value = *p++ & 0x7f;
39
732k
        ret = 1;
40
732k
    } else if ((*p & 0xe0) == 0xc0) {
41
3.63k
        if (len < 2)
42
608
            return -1;
43
3.03k
        if ((p[1] & 0xc0) != 0x80)
44
677
            return -3;
45
2.35k
        value = (*p++ & 0x1f) << 6;
46
2.35k
        value |= *p++ & 0x3f;
47
2.35k
        if (value < 0x80)
48
367
            return -4;
49
1.98k
        ret = 2;
50
33.8k
    } else if ((*p & 0xf0) == 0xe0) {
51
4.49k
        if (len < 3)
52
627
            return -1;
53
3.86k
        if (((p[1] & 0xc0) != 0x80)
54
3.86k
            || ((p[2] & 0xc0) != 0x80))
55
938
            return -3;
56
2.92k
        value = (*p++ & 0xf) << 12;
57
2.92k
        value |= (*p++ & 0x3f) << 6;
58
2.92k
        value |= *p++ & 0x3f;
59
2.92k
        if (value < 0x800)
60
511
            return -4;
61
2.41k
        ret = 3;
62
29.3k
    } else if ((*p & 0xf8) == 0xf0) {
63
8.82k
        if (len < 4)
64
427
            return -1;
65
8.39k
        if (((p[1] & 0xc0) != 0x80)
66
8.39k
            || ((p[2] & 0xc0) != 0x80)
67
8.39k
            || ((p[3] & 0xc0) != 0x80))
68
1.79k
            return -3;
69
6.59k
        value = ((unsigned long)(*p++ & 0x7)) << 18;
70
6.59k
        value |= (*p++ & 0x3f) << 12;
71
6.59k
        value |= (*p++ & 0x3f) << 6;
72
6.59k
        value |= *p++ & 0x3f;
73
6.59k
        if (value < 0x10000)
74
571
            return -4;
75
6.02k
        ret = 4;
76
20.5k
    } else if ((*p & 0xfc) == 0xf8) {
77
7.13k
        if (len < 5)
78
569
            return -1;
79
6.56k
        if (((p[1] & 0xc0) != 0x80)
80
6.56k
            || ((p[2] & 0xc0) != 0x80)
81
6.56k
            || ((p[3] & 0xc0) != 0x80)
82
6.56k
            || ((p[4] & 0xc0) != 0x80))
83
1.73k
            return -3;
84
4.82k
        value = ((unsigned long)(*p++ & 0x3)) << 24;
85
4.82k
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
86
4.82k
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
87
4.82k
        value |= (*p++ & 0x3f) << 6;
88
4.82k
        value |= *p++ & 0x3f;
89
4.82k
        if (value < 0x200000)
90
767
            return -4;
91
4.05k
        ret = 5;
92
13.3k
    } else if ((*p & 0xfe) == 0xfc) {
93
12.0k
        if (len < 6)
94
524
            return -1;
95
11.4k
        if (((p[1] & 0xc0) != 0x80)
96
11.4k
            || ((p[2] & 0xc0) != 0x80)
97
11.4k
            || ((p[3] & 0xc0) != 0x80)
98
11.4k
            || ((p[4] & 0xc0) != 0x80)
99
11.4k
            || ((p[5] & 0xc0) != 0x80))
100
5.33k
            return -3;
101
6.14k
        value = ((unsigned long)(*p++ & 0x1)) << 30;
102
6.14k
        value |= ((unsigned long)(*p++ & 0x3f)) << 24;
103
6.14k
        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
104
6.14k
        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
105
6.14k
        value |= (*p++ & 0x3f) << 6;
106
6.14k
        value |= *p++ & 0x3f;
107
6.14k
        if (value < 0x4000000)
108
907
            return -4;
109
5.23k
        ret = 6;
110
5.23k
    } else
111
1.39k
        return -2;
112
752k
    *val = value;
113
752k
    return ret;
114
770k
}
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
47.4M
{
126
47.4M
    if (!str)
127
23.6M
        len = 6;                /* Maximum we will need */
128
23.7M
    else if (len <= 0)
129
0
        return -1;
130
47.4M
    if (value < 0x80) {
131
5.24M
        if (str)
132
2.62M
            *str = (unsigned char)value;
133
5.24M
        return 1;
134
5.24M
    }
135
42.1M
    if (value < 0x800) {
136
31.6M
        if (len < 2)
137
0
            return -1;
138
31.6M
        if (str) {
139
15.8M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
140
15.8M
            *str = (unsigned char)((value & 0x3f) | 0x80);
141
15.8M
        }
142
31.6M
        return 2;
143
31.6M
    }
144
10.5M
    if (value < 0x10000) {
145
95.6k
        if (len < 3)
146
0
            return -1;
147
95.6k
        if (str) {
148
71.1k
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
149
71.1k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
150
71.1k
            *str = (unsigned char)((value & 0x3f) | 0x80);
151
71.1k
        }
152
95.6k
        return 3;
153
95.6k
    }
154
10.4M
    if (value < 0x200000) {
155
35.0k
        if (len < 4)
156
0
            return -1;
157
35.0k
        if (str) {
158
17.5k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
159
17.5k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
160
17.5k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
161
17.5k
            *str = (unsigned char)((value & 0x3f) | 0x80);
162
17.5k
        }
163
35.0k
        return 4;
164
35.0k
    }
165
10.3M
    if (value < 0x4000000) {
166
724k
        if (len < 5)
167
0
            return -1;
168
724k
        if (str) {
169
362k
            *str++ = (unsigned char)(((value >> 24) & 0x3) | 0xf8);
170
362k
            *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
171
362k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
172
362k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
173
362k
            *str = (unsigned char)((value & 0x3f) | 0x80);
174
362k
        }
175
724k
        return 5;
176
724k
    }
177
9.65M
    if (len < 6)
178
0
        return -1;
179
9.65M
    if (str) {
180
4.82M
        *str++ = (unsigned char)(((value >> 30) & 0x1) | 0xfc);
181
4.82M
        *str++ = (unsigned char)(((value >> 24) & 0x3f) | 0x80);
182
4.82M
        *str++ = (unsigned char)(((value >> 18) & 0x3f) | 0x80);
183
4.82M
        *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
184
4.82M
        *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
185
4.82M
        *str = (unsigned char)((value & 0x3f) | 0x80);
186
4.82M
    }
187
9.65M
    return 6;
188
9.65M
}