Coverage Report

Created: 2026-08-13 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541/deps/base64.c
Line
Count
Source
1
/*
2
 * Base64 encoding/decoding (RFC1341)
3
 * Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
4
 * Copyright (c) 2025, Julius Pfrommer (Fraunhofer IOSB)
5
 *
6
 * This software may be distributed under the terms of the BSD license.
7
 * See README for more details.
8
 */
9
10
#include "base64.h"
11
#include <open62541/types.h>
12
13
static const unsigned char base64_table[65] =
14
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
15
16
unsigned char *
17
231
UA_base64(const unsigned char *src, size_t len, size_t *out_len) {
18
231
    if(len == 0) {
19
0
        *out_len = 0;
20
0
        return NULL;
21
0
    }
22
23
231
    size_t olen = 4*((len + 2) / 3); /* 3-byte blocks to 4-byte */
24
231
    if(olen < len)
25
0
        return NULL; /* integer overflow */
26
27
231
    unsigned char *out = (unsigned char*)UA_malloc(olen);
28
231
    if(!out)
29
0
        return NULL;
30
31
231
    *out_len = UA_base64_buf(src, len, out);
32
231
    return out;
33
231
}
34
35
size_t
36
35.9k
UA_base64_buf(const unsigned char *src, size_t len, unsigned char *out) {
37
35.9k
    const unsigned char *end = src + len;
38
35.9k
    const unsigned char *in = src;
39
35.9k
    unsigned char *pos = out;
40
1.20M
    while(end - in >= 3) {
41
1.16M
        *pos++ = base64_table[in[0] >> 2];
42
1.16M
        *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
43
1.16M
        *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
44
1.16M
        *pos++ = base64_table[in[2] & 0x3f];
45
1.16M
        in += 3;
46
1.16M
    }
47
48
35.9k
    if(end - in) {
49
4.07k
        *pos++ = base64_table[in[0] >> 2];
50
4.07k
        if(end - in == 1) {
51
1.34k
            *pos++ = base64_table[(in[0] & 0x03) << 4];
52
1.34k
            *pos++ = '=';
53
2.73k
        } else {
54
2.73k
            *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
55
2.73k
            *pos++ = base64_table[(in[1] & 0x0f) << 2];
56
2.73k
        }
57
4.07k
        *pos++ = '=';
58
4.07k
    }
59
60
35.9k
    return (size_t)(pos - out);
61
35.9k
}
62
63
static unsigned char dtable[128] = {
64
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f, 0x7f, 0x80, 0x80, 0x7f, 0x80, 0x80,
65
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
66
    0x7f, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 62  , 0x80, 62  , 0x80, 63  ,
67
    52  , 53  , 54  , 55  , 56  , 57  , 58  , 59  , 60  , 61  , 0x80, 0x80, 0x80, 0   , 0x80, 0x80,
68
    0x80, 0   , 1   , 2   , 3   , 4   , 5   , 6   , 7   , 8   , 9   , 10  , 11  , 12  , 13  , 14  ,
69
    15  , 16  , 17  , 18  , 19  , 20  , 21  , 22  , 23  , 24  , 25  , 0x80, 0x80, 0x80, 0x80, 63  ,
70
    0x80, 26  , 27  , 28  , 29  , 30  , 31  , 32  , 33  , 34  , 35  , 36  , 37  , 38  , 39  , 40  ,
71
    41  , 42  , 43  , 44  , 45  , 46  , 47  , 48  , 49  , 50  , 51  , 0x80, 0x80, 0x80, 0x80, 0x80
72
};
73
74
unsigned char *
75
24.5k
UA_unbase64(const unsigned char *src, size_t len, size_t *out_len) {
76
    /* Empty base64 results in an empty byte-string */
77
24.5k
    if(len == 0) {
78
21.5k
        *out_len = 0;
79
21.5k
        return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
80
21.5k
    }
81
82
    /* Allocate the output string. Append four bytes to allow missing padding */
83
2.95k
    size_t olen = (len / 4 * 3) + 4;
84
2.95k
    unsigned char *out = (unsigned char*)UA_malloc(olen);
85
2.95k
    if(!out)
86
0
        return NULL;
87
88
    /* Iterate over the input */
89
2.95k
    size_t pad = 0;
90
2.95k
    unsigned char count = 0;
91
2.95k
    unsigned char block[4];
92
2.95k
    unsigned char *pos = out;
93
3.22M
    for(size_t i = 0; i < len; i++) {
94
3.22M
        if(src[i] & 0x80)
95
12
            goto error; /* Non-ASCII input */
96
3.22M
        unsigned char tmp = dtable[src[i]];
97
3.22M
        if(tmp == 0x80)
98
8
            goto error; /* Not an allowed character */
99
3.22M
        if(tmp == 0x7f)
100
1.40k
            continue; /* Whitespace is ignored to accomodate RFC 2045, used in
101
                       * XML for xs:base64Binary. */
102
3.22M
        block[count++] = tmp;
103
104
        /* Padding */
105
3.22M
        if(src[i] == '=') {
106
3.62k
            if(count < 3) /* Padding can only be the last two bytes of a block */
107
5
                goto error;
108
3.62k
            block[count-1] = 0;
109
3.62k
            pad++;
110
3.21M
        } else if(pad > 0) {
111
2
            goto error; /* Padding not terminated correctly */
112
2
        }
113
114
        /* Write three output characters for four characters of input */
115
3.22M
        if(count == 4) {
116
805k
            if(pad > 2)
117
0
                goto error; /* Invalid padding */
118
805k
            *pos++ = (block[0] << 2) | (block[1] >> 4);
119
805k
            *pos++ = (block[1] << 4) | (block[2] >> 2);
120
805k
            *pos++ = (block[2] << 6) | block[3];
121
805k
            count = 0;
122
805k
            pos -= pad;
123
805k
            pad = 0;
124
805k
        }
125
3.22M
    }
126
127
    /* Input length not a multiple of four */
128
2.93k
    if(count > 0)
129
5
        goto error;
130
131
2.92k
    *out_len = (size_t)(pos - out);
132
2.92k
    if(*out_len == 0) {
133
88
        UA_free(out);
134
88
        return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
135
88
    }
136
2.83k
    return out;
137
138
32
 error:
139
32
    UA_free(out);
140
    return NULL;
141
2.92k
}