Coverage Report

Created: 2025-07-18 07:02

/src/sudo/plugins/sudoers/b64_decode.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2013-2018 Todd C. Miller <Todd.Miller@sudo.ws>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
/*
20
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22
 */
23
24
#include <config.h>
25
26
#include <sudoers.h>
27
28
/*
29
 * Derived from code with the following declaration:
30
 *  PUBLIC DOMAIN - Jon Mayo - November 13, 2003 
31
 */
32
33
static const unsigned char base64dec_tab[256]= {
34
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
35
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
36
    255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
37
     52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,  0,255,255,
38
    255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
39
     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
40
    255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41
     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
42
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
43
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
44
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
45
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
46
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
47
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
48
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
49
    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
50
};
51
52
/*
53
 * Decode a NUL-terminated string in base64 format and store the
54
 * result in dst.
55
 */
56
size_t
57
base64_decode(const char * restrict in, unsigned char * restrict out, size_t out_size)
58
0
{
59
0
    unsigned char *out_end = out + out_size;
60
0
    const unsigned char *out0 = out;
61
0
    unsigned int rem, v;
62
0
    debug_decl(base64_decode, SUDOERS_DEBUG_MATCH);
63
64
0
    for (v = 0, rem = 0; *in != '\0' && *in != '='; in++) {
65
0
  unsigned char ch = base64dec_tab[(unsigned char)*in];
66
0
  if (ch == 255)
67
0
      debug_return_size_t((size_t)-1);
68
0
  v = (v << 6) | ch;
69
0
  rem += 6;
70
0
  if (rem >= 8) {
71
0
      rem -= 8;
72
0
      if (out >= out_end)
73
0
    debug_return_size_t((size_t)-1);
74
0
      *out++ = (v >> rem) & 0xff;
75
0
  }
76
0
    }
77
0
    if (rem >= 8) {
78
0
      if (out >= out_end)
79
0
    debug_return_size_t((size_t)-1);
80
0
      *out++ = (v >> rem) & 0xff;
81
0
    }
82
0
    debug_return_size_t((size_t)(out - out0));
83
0
}