Coverage Report

Created: 2023-09-25 06:43

/src/openssl30/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
28.9k
{
30
28.9k
    const unsigned char *p;
31
28.9k
    unsigned long value;
32
28.9k
    int ret;
33
28.9k
    if (len <= 0)
34
0
        return 0;
35
28.9k
    p = str;
36
37
    /* Check syntax and work out the encoded value (if correct) */
38
28.9k
    if ((*p & 0x80) == 0) {
39
21.0k
        value = *p++ & 0x7f;
40
21.0k
        ret = 1;
41
21.0k
    } else if ((*p & 0xe0) == 0xc0) {
42
1.33k
        if (len < 2)
43
392
            return -1;
44
946
        if ((p[1] & 0xc0) != 0x80)
45
281
            return -3;
46
665
        value = (*p++ & 0x1f) << 6;
47
665
        value |= *p++ & 0x3f;
48
665
        if (value < 0x80)
49
216
            return -4;
50
449
        ret = 2;
51
6.55k
    } else if ((*p & 0xf0) == 0xe0) {
52
3.36k
        if (len < 3)
53
206
            return -1;
54
3.16k
        if (((p[1] & 0xc0) != 0x80)
55
3.16k
            || ((p[2] & 0xc0) != 0x80))
56
452
            return -3;
57
2.70k
        value = (*p++ & 0xf) << 12;
58
2.70k
        value |= (*p++ & 0x3f) << 6;
59
2.70k
        value |= *p++ & 0x3f;
60
2.70k
        if (value < 0x800)
61
199
            return -4;
62
2.51k
        if (is_unicode_surrogate(value))
63
7
            return -2;
64
2.50k
        ret = 3;
65
3.18k
    } else if ((*p & 0xf8) == 0xf0) {
66
2.47k
        if (len < 4)
67
198
            return -1;
68
2.27k
        if (((p[1] & 0xc0) != 0x80)
69
2.27k
            || ((p[2] & 0xc0) != 0x80)
70
2.27k
            || ((p[3] & 0xc0) != 0x80))
71
616
            return -3;
72
1.65k
        value = ((unsigned long)(*p++ & 0x7)) << 18;
73
1.65k
        value |= (*p++ & 0x3f) << 12;
74
1.65k
        value |= (*p++ & 0x3f) << 6;
75
1.65k
        value |= *p++ & 0x3f;
76
1.65k
        if (value < 0x10000)
77
213
            return -4;
78
1.44k
        ret = 4;
79
1.44k
    } else
80
714
        return -2;
81
25.4k
    *val = value;
82
25.4k
    return ret;
83
28.9k
}
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
42.0M
{
95
42.0M
    if (!str)
96
21.0M
        len = 4;                /* Maximum we will need */
97
21.0M
    else if (len <= 0)
98
0
        return -1;
99
42.0M
    if (value < 0x80) {
100
1.02M
        if (str)
101
513k
            *str = (unsigned char)value;
102
1.02M
        return 1;
103
1.02M
    }
104
41.0M
    if (value < 0x800) {
105
40.9M
        if (len < 2)
106
0
            return -1;
107
40.9M
        if (str) {
108
20.4M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
109
20.4M
            *str = (unsigned char)((value & 0x3f) | 0x80);
110
20.4M
        }
111
40.9M
        return 2;
112
40.9M
    }
113
65.2k
    if (value < 0x10000) {
114
64.6k
        if (is_unicode_surrogate(value))
115
18
            return -2;
116
64.6k
        if (len < 3)
117
0
            return -1;
118
64.6k
        if (str) {
119
51.5k
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
120
51.5k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
121
51.5k
            *str = (unsigned char)((value & 0x3f) | 0x80);
122
51.5k
        }
123
64.6k
        return 3;
124
64.6k
    }
125
620
    if (value < UNICODE_LIMIT) {
126
216
        if (len < 4)
127
0
            return -1;
128
216
        if (str) {
129
110
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
130
110
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
131
110
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
132
110
            *str = (unsigned char)((value & 0x3f) | 0x80);
133
110
        }
134
216
        return 4;
135
216
    }
136
404
    return -2;
137
620
}