Coverage Report

Created: 2023-06-08 06:40

/src/openssl/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
43.0k
{
30
43.0k
    const unsigned char *p;
31
43.0k
    unsigned long value;
32
43.0k
    int ret;
33
43.0k
    if (len <= 0)
34
0
        return 0;
35
43.0k
    p = str;
36
37
    /* Check syntax and work out the encoded value (if correct) */
38
43.0k
    if ((*p & 0x80) == 0) {
39
37.7k
        value = *p++ & 0x7f;
40
37.7k
        ret = 1;
41
37.7k
    } else if ((*p & 0xe0) == 0xc0) {
42
1.39k
        if (len < 2)
43
393
            return -1;
44
1.00k
        if ((p[1] & 0xc0) != 0x80)
45
113
            return -3;
46
889
        value = (*p++ & 0x1f) << 6;
47
889
        value |= *p++ & 0x3f;
48
889
        if (value < 0x80)
49
24
            return -4;
50
865
        ret = 2;
51
3.92k
    } else if ((*p & 0xf0) == 0xe0) {
52
2.05k
        if (len < 3)
53
532
            return -1;
54
1.52k
        if (((p[1] & 0xc0) != 0x80)
55
1.52k
            || ((p[2] & 0xc0) != 0x80))
56
107
            return -3;
57
1.41k
        value = (*p++ & 0xf) << 12;
58
1.41k
        value |= (*p++ & 0x3f) << 6;
59
1.41k
        value |= *p++ & 0x3f;
60
1.41k
        if (value < 0x800)
61
76
            return -4;
62
1.33k
        if (is_unicode_surrogate(value))
63
27
            return -2;
64
1.31k
        ret = 3;
65
1.87k
    } else if ((*p & 0xf8) == 0xf0) {
66
1.27k
        if (len < 4)
67
67
            return -1;
68
1.21k
        if (((p[1] & 0xc0) != 0x80)
69
1.21k
            || ((p[2] & 0xc0) != 0x80)
70
1.21k
            || ((p[3] & 0xc0) != 0x80))
71
181
            return -3;
72
1.03k
        value = ((unsigned long)(*p++ & 0x7)) << 18;
73
1.03k
        value |= (*p++ & 0x3f) << 12;
74
1.03k
        value |= (*p++ & 0x3f) << 6;
75
1.03k
        value |= *p++ & 0x3f;
76
1.03k
        if (value < 0x10000)
77
23
            return -4;
78
1.00k
        ret = 4;
79
1.00k
    } else
80
594
        return -2;
81
40.8k
    *val = value;
82
40.8k
    return ret;
83
43.0k
}
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
24.0M
{
95
24.0M
    if (!str)
96
12.0M
        len = 4;                /* Maximum we will need */
97
12.0M
    else if (len <= 0)
98
0
        return -1;
99
24.0M
    if (value < 0x80) {
100
1.36M
        if (str)
101
693k
            *str = (unsigned char)value;
102
1.36M
        return 1;
103
1.36M
    }
104
22.7M
    if (value < 0x800) {
105
22.7M
        if (len < 2)
106
0
            return -1;
107
22.7M
        if (str) {
108
11.3M
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
109
11.3M
            *str = (unsigned char)((value & 0x3f) | 0x80);
110
11.3M
        }
111
22.7M
        return 2;
112
22.7M
    }
113
7.59k
    if (value < 0x10000) {
114
7.27k
        if (is_unicode_surrogate(value))
115
50
            return -2;
116
7.22k
        if (len < 3)
117
0
            return -1;
118
7.22k
        if (str) {
119
4.19k
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
120
4.19k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
121
4.19k
            *str = (unsigned char)((value & 0x3f) | 0x80);
122
4.19k
        }
123
7.22k
        return 3;
124
7.22k
    }
125
324
    if (value < UNICODE_LIMIT) {
126
188
        if (len < 4)
127
0
            return -1;
128
188
        if (str) {
129
104
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
130
104
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
131
104
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
132
104
            *str = (unsigned char)((value & 0x3f) | 0x80);
133
104
        }
134
188
        return 4;
135
188
    }
136
136
    return -2;
137
324
}