Coverage Report

Created: 2026-06-08 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nettle/base64-decode.c
Line
Count
Source
1
/* base64-encode.c
2
3
   Copyright (C) 2002 Niels Möller
4
5
   This file is part of GNU Nettle.
6
7
   GNU Nettle is free software: you can redistribute it and/or
8
   modify it under the terms of either:
9
10
     * the GNU Lesser General Public License as published by the Free
11
       Software Foundation; either version 3 of the License, or (at your
12
       option) any later version.
13
14
   or
15
16
     * the GNU General Public License as published by the Free
17
       Software Foundation; either version 2 of the License, or (at your
18
       option) any later version.
19
20
   or both in parallel, as here.
21
22
   GNU Nettle is distributed in the hope that it will be useful,
23
   but WITHOUT ANY WARRANTY; without even the implied warranty of
24
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
   General Public License for more details.
26
27
   You should have received copies of the GNU General Public License and
28
   the GNU Lesser General Public License along with this program.  If
29
   not, see http://www.gnu.org/licenses/.
30
*/
31
32
#if HAVE_CONFIG_H
33
# include "config.h"
34
#endif
35
36
#include <assert.h>
37
#include <stdlib.h>
38
39
#include "base64.h"
40
41
107
#define TABLE_INVALID -1
42
1.06k
#define TABLE_SPACE -2
43
19.4k
#define TABLE_END -3
44
45
void
46
base64_decode_init(struct base64_decode_ctx *ctx)
47
29.2k
{
48
29.2k
  static const signed char base64_decode_table[0x100] =
49
29.2k
    {
50
      /* White space is HT, VT, FF, CR, LF and SPC */
51
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -1, -1, 
52
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53
29.2k
      -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
54
29.2k
      52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1,
55
29.2k
      -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
56
29.2k
      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
57
29.2k
      -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
58
29.2k
      41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
59
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
61
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66
29.2k
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67
29.2k
    };
68
69
29.2k
  ctx->word = ctx->bits = ctx->padding = 0;
70
29.2k
  ctx->table = base64_decode_table;
71
29.2k
}
72
73
int
74
base64_decode_single(struct base64_decode_ctx *ctx,
75
         uint8_t *dst,
76
         char src)
77
46.5M
{
78
46.5M
  int data = ctx->table[(uint8_t) src];
79
80
46.5M
  switch(data)
81
46.5M
    {
82
46.5M
    default:
83
46.5M
      assert(data >= 0 && data < 0x40);
84
85
46.5M
      if (ctx->padding)
86
9
  return -1;
87
      
88
46.5M
      ctx->word = ctx->word << 6 | data;
89
46.5M
      ctx->bits += 6;
90
91
46.5M
      if (ctx->bits >= 8)
92
34.8M
  {
93
34.8M
    ctx->bits -= 8;
94
34.8M
    dst[0] = ctx->word >> ctx->bits;
95
34.8M
    return 1;
96
34.8M
  }
97
11.6M
      else return 0;
98
99
107
    case TABLE_INVALID:
100
107
      return -1;
101
102
1.06k
    case TABLE_SPACE:
103
1.06k
      return 0;
104
      
105
19.4k
    case TABLE_END:
106
      /* There can be at most two padding characters. */
107
19.4k
      if (!ctx->bits || ctx->padding > 2)
108
11
  return -1;
109
      
110
19.4k
      if (ctx->word & ( (1<<ctx->bits) - 1))
111
  /* We shouldn't have any leftover bits */
112
12
  return -1;
113
114
19.4k
      ctx->padding++;
115
19.4k
      ctx->bits -= 2;
116
19.4k
      return 0;
117
46.5M
    }
118
46.5M
}
119
120
int
121
base64_decode_update(struct base64_decode_ctx *ctx,
122
         size_t *dst_length,
123
         uint8_t *dst,
124
         size_t src_length,
125
         const char *src)
126
29.2k
{
127
29.2k
  size_t done;
128
29.2k
  size_t i;
129
130
46.5M
  for (i = 0, done = 0; i<src_length; i++)
131
46.5M
    switch(base64_decode_single(ctx, dst + done, src[i]))
132
46.5M
      {
133
139
      case -1:
134
139
  return 0;
135
34.8M
      case 1:
136
34.8M
  done++;
137
  /* Fall through */
138
46.5M
      case 0:
139
46.5M
  break;
140
0
      default:
141
0
  abort();
142
46.5M
      }
143
  
144
29.2k
  assert(done <= BASE64_DECODE_LENGTH(src_length));
145
146
29.1k
  *dst_length = done;
147
29.1k
  return 1;
148
29.1k
}
149
150
int
151
base64_decode_final(struct base64_decode_ctx *ctx)
152
29.1k
{
153
29.1k
  return ctx->bits == 0;
154
29.1k
}