Coverage Report

Created: 2023-09-25 06:41

/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
159k
{
30
159k
    const unsigned char *p;
31
159k
    unsigned long value;
32
159k
    int ret;
33
159k
    if (len <= 0)
34
0
        return 0;
35
159k
    p = str;
36
37
    /* Check syntax and work out the encoded value (if correct) */
38
159k
    if ((*p & 0x80) == 0) {
39
150k
        value = *p++ & 0x7f;
40
150k
        ret = 1;
41
150k
    } else if ((*p & 0xe0) == 0xc0) {
42
3.50k
        if (len < 2)
43
40
            return -1;
44
3.46k
        if ((p[1] & 0xc0) != 0x80)
45
1.20k
            return -3;
46
2.26k
        value = (*p++ & 0x1f) << 6;
47
2.26k
        value |= *p++ & 0x3f;
48
2.26k
        if (value < 0x80)
49
24
            return -4;
50
2.23k
        ret = 2;
51
4.86k
    } else if ((*p & 0xf0) == 0xe0) {
52
1.66k
        if (len < 3)
53
20
            return -1;
54
1.64k
        if (((p[1] & 0xc0) != 0x80)
55
1.64k
            || ((p[2] & 0xc0) != 0x80))
56
683
            return -3;
57
958
        value = (*p++ & 0xf) << 12;
58
958
        value |= (*p++ & 0x3f) << 6;
59
958
        value |= *p++ & 0x3f;
60
958
        if (value < 0x800)
61
20
            return -4;
62
938
        if (is_unicode_surrogate(value))
63
28
            return -2;
64
910
        ret = 3;
65
3.19k
    } else if ((*p & 0xf8) == 0xf0) {
66
2.96k
        if (len < 4)
67
35
            return -1;
68
2.92k
        if (((p[1] & 0xc0) != 0x80)
69
2.92k
            || ((p[2] & 0xc0) != 0x80)
70
2.92k
            || ((p[3] & 0xc0) != 0x80))
71
2.25k
            return -3;
72
672
        value = ((unsigned long)(*p++ & 0x7)) << 18;
73
672
        value |= (*p++ & 0x3f) << 12;
74
672
        value |= (*p++ & 0x3f) << 6;
75
672
        value |= *p++ & 0x3f;
76
672
        if (value < 0x10000)
77
15
            return -4;
78
657
        ret = 4;
79
657
    } else
80
238
        return -2;
81
154k
    *val = value;
82
154k
    return ret;
83
159k
}
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
22.5M
{
95
22.5M
    if (!str)
96
11.2M
        len = 4;                /* Maximum we will need */
97
11.2M
    else if (len <= 0)
98
0
        return -1;
99
22.5M
    if (value < 0x80) {
100
1.22M
        if (str)
101
614k
            *str = (unsigned char)value;
102
1.22M
        return 1;
103
1.22M
    }
104
21.2M
    if (value < 0x800) {
105
21.2M
        if (len < 2)
106
0
            return -1;
107
21.2M
        if (str) {
108
10.6M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
109
10.6M
            *str = (unsigned char)((value & 0x3f) | 0x80);
110
10.6M
        }
111
21.2M
        return 2;
112
21.2M
    }
113
4.70k
    if (value < 0x10000) {
114
3.86k
        if (is_unicode_surrogate(value))
115
78
            return -2;
116
3.78k
        if (len < 3)
117
0
            return -1;
118
3.78k
        if (str) {
119
2.07k
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
120
2.07k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
121
2.07k
            *str = (unsigned char)((value & 0x3f) | 0x80);
122
2.07k
        }
123
3.78k
        return 3;
124
3.78k
    }
125
846
    if (value < UNICODE_LIMIT) {
126
592
        if (len < 4)
127
0
            return -1;
128
592
        if (str) {
129
320
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
130
320
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
131
320
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
132
320
            *str = (unsigned char)((value & 0x3f) | 0x80);
133
320
        }
134
592
        return 4;
135
592
    }
136
254
    return -2;
137
846
}