Coverage Report

Created: 2025-07-11 06:14

/src/hostap/src/utils/base64.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Base64 encoding/decoding (RFC1341)
3
 * Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi>
4
 *
5
 * This software may be distributed under the terms of the BSD license.
6
 * See README for more details.
7
 */
8
9
#include "includes.h"
10
#include <stdint.h>
11
12
#include "utils/common.h"
13
#include "os.h"
14
#include "base64.h"
15
16
static const char base64_table[65] =
17
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
18
static const char base64_url_table[65] =
19
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
20
21
22
0
#define BASE64_PAD BIT(0)
23
0
#define BASE64_LF BIT(1)
24
25
26
static char * base64_gen_encode(const unsigned char *src, size_t len,
27
        size_t *out_len, const char *table, int add_pad)
28
0
{
29
0
  char *out, *pos;
30
0
  const unsigned char *end, *in;
31
0
  size_t olen;
32
0
  int line_len;
33
34
0
  if (len >= SIZE_MAX / 4)
35
0
    return NULL;
36
0
  olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
37
0
  if (add_pad & BASE64_LF)
38
0
    olen += olen / 72; /* line feeds */
39
0
  olen++; /* nul termination */
40
0
  if (olen < len)
41
0
    return NULL; /* integer overflow */
42
0
  out = os_malloc(olen);
43
0
  if (out == NULL)
44
0
    return NULL;
45
46
0
  end = src + len;
47
0
  in = src;
48
0
  pos = out;
49
0
  line_len = 0;
50
0
  while (end - in >= 3) {
51
0
    *pos++ = table[(in[0] >> 2) & 0x3f];
52
0
    *pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f];
53
0
    *pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f];
54
0
    *pos++ = table[in[2] & 0x3f];
55
0
    in += 3;
56
0
    line_len += 4;
57
0
    if ((add_pad & BASE64_LF) && line_len >= 72) {
58
0
      *pos++ = '\n';
59
0
      line_len = 0;
60
0
    }
61
0
  }
62
63
0
  if (end - in) {
64
0
    *pos++ = table[(in[0] >> 2) & 0x3f];
65
0
    if (end - in == 1) {
66
0
      *pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
67
0
      if (add_pad & BASE64_PAD)
68
0
        *pos++ = '=';
69
0
    } else {
70
0
      *pos++ = table[(((in[0] & 0x03) << 4) |
71
0
          (in[1] >> 4)) & 0x3f];
72
0
      *pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
73
0
    }
74
0
    if (add_pad & BASE64_PAD)
75
0
      *pos++ = '=';
76
0
    line_len += 4;
77
0
  }
78
79
0
  if ((add_pad & BASE64_LF) && line_len)
80
0
    *pos++ = '\n';
81
82
0
  *pos = '\0';
83
0
  if (out_len)
84
0
    *out_len = pos - out;
85
0
  return out;
86
0
}
87
88
89
static unsigned char * base64_gen_decode(const char *src, size_t len,
90
           size_t *out_len, const char *table)
91
0
{
92
0
  unsigned char dtable[256], *out, *pos, block[4], tmp;
93
0
  size_t i, count, olen;
94
0
  int pad = 0;
95
0
  size_t extra_pad;
96
97
0
  os_memset(dtable, 0x80, 256);
98
0
  for (i = 0; i < sizeof(base64_table) - 1; i++)
99
0
    dtable[(unsigned char) table[i]] = (unsigned char) i;
100
0
  dtable['='] = 0;
101
102
0
  count = 0;
103
0
  for (i = 0; i < len; i++) {
104
0
    if (dtable[(unsigned char) src[i]] != 0x80)
105
0
      count++;
106
0
  }
107
108
0
  if (count == 0)
109
0
    return NULL;
110
0
  extra_pad = (4 - count % 4) % 4;
111
112
0
  olen = (count + extra_pad) / 4 * 3;
113
0
  pos = out = os_malloc(olen);
114
0
  if (out == NULL)
115
0
    return NULL;
116
117
0
  count = 0;
118
0
  for (i = 0; i < len + extra_pad; i++) {
119
0
    unsigned char val;
120
121
0
    if (i >= len)
122
0
      val = '=';
123
0
    else
124
0
      val = src[i];
125
0
    tmp = dtable[val];
126
0
    if (tmp == 0x80)
127
0
      continue;
128
129
0
    if (val == '=')
130
0
      pad++;
131
0
    block[count] = tmp;
132
0
    count++;
133
0
    if (count == 4) {
134
0
      *pos++ = (block[0] << 2) | (block[1] >> 4);
135
0
      *pos++ = (block[1] << 4) | (block[2] >> 2);
136
0
      *pos++ = (block[2] << 6) | block[3];
137
0
      count = 0;
138
0
      if (pad) {
139
0
        if (pad == 1)
140
0
          pos--;
141
0
        else if (pad == 2)
142
0
          pos -= 2;
143
0
        else {
144
          /* Invalid padding */
145
0
          os_free(out);
146
0
          return NULL;
147
0
        }
148
0
        break;
149
0
      }
150
0
    }
151
0
  }
152
153
0
  *out_len = pos - out;
154
0
  return out;
155
0
}
156
157
158
/**
159
 * base64_encode - Base64 encode
160
 * @src: Data to be encoded
161
 * @len: Length of the data to be encoded
162
 * @out_len: Pointer to output length variable, or %NULL if not used
163
 * Returns: Allocated buffer of out_len bytes of encoded data,
164
 * or %NULL on failure
165
 *
166
 * Caller is responsible for freeing the returned buffer. Returned buffer is
167
 * nul terminated to make it easier to use as a C string. The nul terminator is
168
 * not included in out_len.
169
 */
170
char * base64_encode(const void *src, size_t len, size_t *out_len)
171
0
{
172
0
  return base64_gen_encode(src, len, out_len, base64_table,
173
0
         BASE64_PAD | BASE64_LF);
174
0
}
175
176
177
char * base64_encode_no_lf(const void *src, size_t len, size_t *out_len)
178
0
{
179
0
  return base64_gen_encode(src, len, out_len, base64_table, BASE64_PAD);
180
0
}
181
182
183
char * base64_url_encode(const void *src, size_t len, size_t *out_len)
184
0
{
185
0
  return base64_gen_encode(src, len, out_len, base64_url_table, 0);
186
0
}
187
188
189
/**
190
 * base64_decode - Base64 decode
191
 * @src: Data to be decoded
192
 * @len: Length of the data to be decoded
193
 * @out_len: Pointer to output length variable
194
 * Returns: Allocated buffer of out_len bytes of decoded data,
195
 * or %NULL on failure
196
 *
197
 * Caller is responsible for freeing the returned buffer.
198
 */
199
unsigned char * base64_decode(const char *src, size_t len, size_t *out_len)
200
0
{
201
0
  return base64_gen_decode(src, len, out_len, base64_table);
202
0
}
203
204
205
unsigned char * base64_url_decode(const char *src, size_t len, size_t *out_len)
206
0
{
207
0
  return base64_gen_decode(src, len, out_len, base64_url_table);
208
0
}