Coverage Report

Created: 2025-08-26 06:30

/src/open62541/deps/base64.c
Line
Count
Source (jump to first uncovered line)
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
12.2k
UA_base64(const unsigned char *src, size_t len, size_t *out_len) {
18
12.2k
    if(len == 0) {
19
0
        *out_len = 0;
20
0
        return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
21
0
    }
22
23
12.2k
  size_t olen = 4*((len + 2) / 3); /* 3-byte blocks to 4-byte */
24
12.2k
  if(olen < len)
25
0
    return NULL; /* integer overflow */
26
27
12.2k
  unsigned char *out = (unsigned char*)UA_malloc(olen);
28
12.2k
  if(!out)
29
0
    return NULL;
30
31
12.2k
    *out_len = UA_base64_buf(src, len, out);
32
12.2k
    return out;
33
12.2k
}
34
35
size_t
36
14.2k
UA_base64_buf(const unsigned char *src, size_t len, unsigned char *out) {
37
14.2k
  const unsigned char *end = src + len;
38
14.2k
  const unsigned char *in = src;
39
14.2k
  unsigned char *pos = out;
40
7.54M
  while(end - in >= 3) {
41
7.52M
    *pos++ = base64_table[in[0] >> 2];
42
7.52M
    *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
43
7.52M
    *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
44
7.52M
    *pos++ = base64_table[in[2] & 0x3f];
45
7.52M
    in += 3;
46
7.52M
  }
47
48
14.2k
  if(end - in) {
49
9.50k
    *pos++ = base64_table[in[0] >> 2];
50
9.50k
    if(end - in == 1) {
51
6.02k
      *pos++ = base64_table[(in[0] & 0x03) << 4];
52
6.02k
      *pos++ = '=';
53
6.02k
    } else {
54
3.48k
      *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
55
3.48k
      *pos++ = base64_table[(in[1] & 0x0f) << 2];
56
3.48k
    }
57
9.50k
    *pos++ = '=';
58
9.50k
  }
59
60
14.2k
    return (size_t)(pos - out);
61
14.2k
}
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
18.0k
UA_unbase64(const unsigned char *src, size_t len, size_t *out_len) {
76
    /* Empty base64 results in an empty byte-string */
77
18.0k
    if(len == 0) {
78
2.10k
        *out_len = 0;
79
2.10k
        return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
80
2.10k
    }
81
82
    /* Allocate the output string. Append four bytes to allow missing padding */
83
15.9k
  size_t olen = (len / 4 * 3) + 4;
84
15.9k
    unsigned char *out = (unsigned char*)UA_malloc(olen);
85
15.9k
  if(!out)
86
19
    return NULL;
87
88
    /* Iterate over the input */
89
15.9k
  size_t pad = 0;
90
15.9k
    unsigned char count = 0;
91
15.9k
    unsigned char block[4];
92
15.9k
    unsigned char *pos = out;
93
143M
  for(size_t i = 0; i < len; i++) {
94
        /* Process character */
95
143M
    unsigned char tmp = dtable[src[i] & 0x7f];
96
143M
        if(tmp == 0x80)
97
143
            goto error;
98
143M
        if(tmp == 0x7f)
99
20.0k
            continue; /* Whitespace is ignored to accomodate RFC 2045, used in
100
                       * XML for xs:base64Binary. */
101
143M
    block[count++] = tmp;
102
103
        /* Allow padding in the middle.
104
         * For example if base64 streams have been concatenated */
105
143M
        if(src[i] == '=')
106
16.1k
            pad++;
107
108
        /* Premature end of input. Fill up the block with padding. */
109
143M
        if(i + 1 == len) {
110
14.6k
            len = (len + 3) & ~0x03; /* Next multiple of four */
111
27.5k
            for(i++; i < len; i++)
112
12.8k
                pad++;
113
27.7k
            for(; count < 4; count++)
114
13.0k
                block[count] = 0;
115
14.6k
        }
116
117
        /* Write three output characters for four characters of input */
118
143M
    if(count == 4) {
119
35.9M
            if(pad > 2)
120
89
                goto error; /* Invalid padding */
121
35.9M
      *pos++ = (block[0] << 2) | (block[1] >> 4);
122
35.9M
      *pos++ = (block[1] << 4) | (block[2] >> 2);
123
35.9M
      *pos++ = (block[2] << 6) | block[3];
124
35.9M
      count = 0;
125
35.9M
            pos -= pad;
126
35.9M
            pad = 0;
127
35.9M
    }
128
143M
  }
129
130
15.6k
  *out_len = (size_t)(pos - out);
131
15.6k
    if(*out_len == 0) {
132
943
        UA_free(out);
133
943
        return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
134
943
    }
135
14.7k
    return out;
136
137
232
 error:
138
232
    UA_free(out);
139
232
    return NULL;
140
15.6k
}