Coverage Report

Created: 2025-07-17 06:56

/src/rauc/subprojects/openssl-3.0.8/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
85.8k
{
30
85.8k
    const unsigned char *p;
31
85.8k
    unsigned long value;
32
85.8k
    int ret;
33
85.8k
    if (len <= 0)
34
0
        return 0;
35
85.8k
    p = str;
36
37
    /* Check syntax and work out the encoded value (if correct) */
38
85.8k
    if ((*p & 0x80) == 0) {
39
82.9k
        value = *p++ & 0x7f;
40
82.9k
        ret = 1;
41
82.9k
    } else if ((*p & 0xe0) == 0xc0) {
42
802
        if (len < 2)
43
1
            return -1;
44
801
        if ((p[1] & 0xc0) != 0x80)
45
3
            return -3;
46
798
        value = (*p++ & 0x1f) << 6;
47
798
        value |= *p++ & 0x3f;
48
798
        if (value < 0x80)
49
7
            return -4;
50
791
        ret = 2;
51
2.10k
    } else if ((*p & 0xf0) == 0xe0) {
52
1.87k
        if (len < 3)
53
2
            return -1;
54
1.87k
        if (((p[1] & 0xc0) != 0x80)
55
1.87k
            || ((p[2] & 0xc0) != 0x80))
56
8
            return -3;
57
1.86k
        value = (*p++ & 0xf) << 12;
58
1.86k
        value |= (*p++ & 0x3f) << 6;
59
1.86k
        value |= *p++ & 0x3f;
60
1.86k
        if (value < 0x800)
61
11
            return -4;
62
1.85k
        if (is_unicode_surrogate(value))
63
11
            return -2;
64
1.84k
        ret = 3;
65
1.84k
    } else if ((*p & 0xf8) == 0xf0) {
66
210
        if (len < 4)
67
1
            return -1;
68
209
        if (((p[1] & 0xc0) != 0x80)
69
209
            || ((p[2] & 0xc0) != 0x80)
70
209
            || ((p[3] & 0xc0) != 0x80))
71
10
            return -3;
72
199
        value = ((unsigned long)(*p++ & 0x7)) << 18;
73
199
        value |= (*p++ & 0x3f) << 12;
74
199
        value |= (*p++ & 0x3f) << 6;
75
199
        value |= *p++ & 0x3f;
76
199
        if (value < 0x10000)
77
9
            return -4;
78
190
        ret = 4;
79
190
    } else
80
20
        return -2;
81
85.7k
    *val = value;
82
85.7k
    return ret;
83
85.8k
}
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
116k
{
95
116k
    if (!str)
96
58.2k
        len = 4;                /* Maximum we will need */
97
58.2k
    else if (len <= 0)
98
0
        return -1;
99
116k
    if (value < 0x80) {
100
59.9k
        if (str)
101
29.9k
            *str = (unsigned char)value;
102
59.9k
        return 1;
103
59.9k
    }
104
56.5k
    if (value < 0x800) {
105
28.5k
        if (len < 2)
106
0
            return -1;
107
28.5k
        if (str) {
108
14.2k
            *str++ = (unsigned char)(((value >> 6) & 0x1f) | 0xc0);
109
14.2k
            *str = (unsigned char)((value & 0x3f) | 0x80);
110
14.2k
        }
111
28.5k
        return 2;
112
28.5k
    }
113
28.0k
    if (value < 0x10000) {
114
26.0k
        if (is_unicode_surrogate(value))
115
0
            return -2;
116
26.0k
        if (len < 3)
117
0
            return -1;
118
26.0k
        if (str) {
119
13.0k
            *str++ = (unsigned char)(((value >> 12) & 0xf) | 0xe0);
120
13.0k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
121
13.0k
            *str = (unsigned char)((value & 0x3f) | 0x80);
122
13.0k
        }
123
26.0k
        return 3;
124
26.0k
    }
125
2.01k
    if (value < UNICODE_LIMIT) {
126
2.01k
        if (len < 4)
127
0
            return -1;
128
2.01k
        if (str) {
129
1.00k
            *str++ = (unsigned char)(((value >> 18) & 0x7) | 0xf0);
130
1.00k
            *str++ = (unsigned char)(((value >> 12) & 0x3f) | 0x80);
131
1.00k
            *str++ = (unsigned char)(((value >> 6) & 0x3f) | 0x80);
132
1.00k
            *str = (unsigned char)((value & 0x3f) | 0x80);
133
1.00k
        }
134
2.01k
        return 4;
135
2.01k
    }
136
0
    return -2;
137
2.01k
}