Coverage Report

Created: 2026-02-14 06:29

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
0
UA_base64(const unsigned char *src, size_t len, size_t *out_len) {
18
0
    if(len == 0) {
19
0
        *out_len = 0;
20
0
        return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
21
0
    }
22
23
0
    size_t olen = 4*((len + 2) / 3); /* 3-byte blocks to 4-byte */
24
0
    if(olen < len)
25
0
        return NULL; /* integer overflow */
26
27
0
    unsigned char *out = (unsigned char*)UA_malloc(olen);
28
0
    if(!out)
29
0
        return NULL;
30
31
0
    *out_len = UA_base64_buf(src, len, out);
32
0
    return out;
33
0
}
34
35
size_t
36
0
UA_base64_buf(const unsigned char *src, size_t len, unsigned char *out) {
37
0
    const unsigned char *end = src + len;
38
0
    const unsigned char *in = src;
39
0
    unsigned char *pos = out;
40
0
    while(end - in >= 3) {
41
0
        *pos++ = base64_table[in[0] >> 2];
42
0
        *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
43
0
        *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
44
0
        *pos++ = base64_table[in[2] & 0x3f];
45
0
        in += 3;
46
0
    }
47
48
0
    if(end - in) {
49
0
        *pos++ = base64_table[in[0] >> 2];
50
0
        if(end - in == 1) {
51
0
            *pos++ = base64_table[(in[0] & 0x03) << 4];
52
0
            *pos++ = '=';
53
0
        } else {
54
0
            *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
55
0
            *pos++ = base64_table[(in[1] & 0x0f) << 2];
56
0
        }
57
0
        *pos++ = '=';
58
0
    }
59
60
0
    return (size_t)(pos - out);
61
0
}
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
0
UA_unbase64(const unsigned char *src, size_t len, size_t *out_len) {
76
    /* Empty base64 results in an empty byte-string */
77
0
    if(len == 0) {
78
0
        *out_len = 0;
79
0
        return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
80
0
    }
81
82
    /* Allocate the output string. Append four bytes to allow missing padding */
83
0
    size_t olen = (len / 4 * 3) + 4;
84
0
    unsigned char *out = (unsigned char*)UA_malloc(olen);
85
0
    if(!out)
86
0
        return NULL;
87
88
    /* Iterate over the input */
89
0
    size_t pad = 0;
90
0
    unsigned char count = 0;
91
0
    unsigned char block[4];
92
0
    unsigned char *pos = out;
93
0
    for(size_t i = 0; i < len; i++) {
94
0
        if(src[i] & 0x80)
95
0
            goto error; /* Non-ASCII input */
96
0
        unsigned char tmp = dtable[src[i]];
97
0
        if(tmp == 0x80)
98
0
            goto error; /* Not an allowed character */
99
0
        if(tmp == 0x7f)
100
0
            continue; /* Whitespace is ignored to accomodate RFC 2045, used in
101
                       * XML for xs:base64Binary. */
102
0
        block[count++] = tmp;
103
104
        /* Padding */
105
0
        if(src[i] == '=') {
106
0
            if(count < 3) /* Padding can only be the last two bytes of a block */
107
0
                goto error;
108
0
            block[count-1] = 0;
109
0
            pad++;
110
0
        } else if(pad > 0) {
111
0
            goto error; /* Padding not terminated correctly */
112
0
        }
113
114
        /* Write three output characters for four characters of input */
115
0
        if(count == 4) {
116
0
            if(pad > 2)
117
0
                goto error; /* Invalid padding */
118
0
            *pos++ = (block[0] << 2) | (block[1] >> 4);
119
0
            *pos++ = (block[1] << 4) | (block[2] >> 2);
120
0
            *pos++ = (block[2] << 6) | block[3];
121
0
            count = 0;
122
0
            pos -= pad;
123
0
            pad = 0;
124
0
        }
125
0
    }
126
127
    /* Input length not a multiple of four */
128
0
    if(count > 0)
129
0
        goto error;
130
131
0
    *out_len = (size_t)(pos - out);
132
0
    if(*out_len == 0) {
133
0
        UA_free(out);
134
0
        return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
135
0
    }
136
0
    return out;
137
138
0
 error:
139
0
    UA_free(out);
140
    return NULL;
141
0
}