File: base64.c

package info (click to toggle)
sudo 1.8.10p3-1%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 12,212 kB
  • sloc: ansic: 50,774; sh: 18,066; makefile: 2,527; yacc: 1,576; lex: 1,113; perl: 386
file content (89 lines) | stat: -rw-r--r-- 2,634 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <config.h>

#include <sys/types.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
#  include <stdlib.h>
# endif
#endif /* STDC_HEADERS */
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */

#include "missing.h"
#include "sudo_debug.h"

/*
 * Decode a NUL-terminated string in base64 format and store the
 * result in dst.
 */
size_t
base64_decode(const char *str, unsigned char *dst, size_t dsize)
{
    static const char b64[] =
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    const unsigned char *dst0 = dst;
    const unsigned char *dend = dst + dsize;
    unsigned char ch[4];
    char *pos;
    int i;
    debug_decl(base64_decode, SUDO_DEBUG_MATCH)

    /*
     * Convert from base64 to binary.  Each base64 char holds 6 bits of data
     * so 4 base64 chars equals 3 chars of data.
     * Padding (with the '=' char) may or may not be present.
     */
    while (*str != '\0') {
	for (i = 0; i < 4; i++) {
	    switch (*str) {
	    case '=':
		str++;
		/* FALLTHROUGH */
	    case '\0':
		ch[i] = '=';
		break;
	    default:
		if ((pos = strchr(b64, *str++)) == NULL)
		    debug_return_size_t((size_t)-1);
		ch[i] = (unsigned char)(pos - b64);
		break;
	    }
	}
	if (ch[0] == '=' || ch[1] == '=' || dst == dend)
	    break;
	*dst++ = (ch[0] << 2) | ((ch[1] & 0x30) >> 4);
	if (ch[2] == '=' || dst == dend)
	    break;
	*dst++ = ((ch[1] & 0x0f) << 4) | ((ch[2] & 0x3c) >> 2);
	if (ch[3] == '=' || dst == dend)
	    break;
	*dst++ = ((ch[2] & 0x03) << 6) | ch[3];
    }
    debug_return_size_t((size_t)(dst - dst0));
}