/src/sudo/lib/util/b64_decode.c
Line | Count | Source |
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 | | #include <config.h> |
20 | | |
21 | | #include <sudo_compat.h> |
22 | | #include <sudo_debug.h> |
23 | | #include <sudo_util.h> |
24 | | |
25 | | /* |
26 | | * Derived from code with the following declaration: |
27 | | * PUBLIC DOMAIN - Jon Mayo - November 13, 2003 |
28 | | */ |
29 | | |
30 | | static const unsigned char base64dec_tab[256]= { |
31 | | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
32 | | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
33 | | 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63, |
34 | | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255, |
35 | | 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
36 | | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, |
37 | | 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
38 | | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255, |
39 | | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
40 | | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, |
41 | | 255,255,255,255,255,255,255,255,255,255,255,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 | | }; |
48 | | |
49 | | /* |
50 | | * Decode a NUL-terminated string in base64 format and store the |
51 | | * result in dst. |
52 | | */ |
53 | | size_t |
54 | | sudo_base64_decode_v1(const char * restrict in, unsigned char * restrict out, |
55 | | size_t out_size) |
56 | 1.01k | { |
57 | 1.01k | unsigned char *out_end = out + out_size; |
58 | 1.01k | const unsigned char *out0 = out; |
59 | 1.01k | unsigned int rem, v; |
60 | 1.01k | debug_decl(sudo_base64_decode, SUDO_DEBUG_UTIL); |
61 | | |
62 | 417k | for (v = 0, rem = 0; *in != '\0' && *in != '='; in++) { |
63 | 416k | unsigned char ch = base64dec_tab[(unsigned char)*in]; |
64 | 416k | if (ch == 255) |
65 | 313 | debug_return_size_t((size_t)-1); |
66 | 416k | v = (v << 6) | ch; |
67 | 416k | rem += 6; |
68 | 416k | if (rem >= 8) { |
69 | 312k | rem -= 8; |
70 | 312k | if (out >= out_end) |
71 | 0 | debug_return_size_t((size_t)-1); |
72 | 312k | *out++ = (v >> rem) & 0xff; |
73 | 312k | } |
74 | 416k | } |
75 | 698 | if (rem >= 8) { |
76 | 0 | if (out >= out_end) |
77 | 0 | debug_return_size_t((size_t)-1); |
78 | 0 | *out++ = (v >> rem) & 0xff; |
79 | 0 | } |
80 | 698 | debug_return_size_t((size_t)(out - out0)); |
81 | 698 | } |