"Fossies" - the Fresh Open Source Software Archive 
Member "sudo-1.9.11p3/plugins/sudoers/b64_encode.c" (12 Jun 2022, 2091 Bytes) of package /linux/misc/sudo-1.9.11p3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "b64_encode.c" see the
Fossies "Dox" file reference documentation.
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 static const unsigned char base64enc_tab[64] =
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
30
31 size_t
32 base64_encode(const unsigned char *in, size_t in_len, char *out, size_t out_len)
33 {
34 size_t ii, io;
35 unsigned int rem, v;
36 debug_decl(base64_encode, SUDOERS_DEBUG_MATCH);
37
38 for (io = 0, ii = 0, v = 0, rem = 0; ii < in_len; ii++) {
39 unsigned char ch = in[ii];
40 v = (v << 8) | ch;
41 rem += 8;
42 while (rem >= 6) {
43 rem -= 6;
44 if (io >= out_len)
45 debug_return_size_t((size_t)-1); /* truncation is failure */
46 out[io++] = base64enc_tab[(v >> rem) & 63];
47 }
48 }
49 if (rem != 0) {
50 v <<= (6 - rem);
51 if (io >= out_len)
52 debug_return_size_t((size_t)-1); /* truncation is failure */
53 out[io++] = base64enc_tab[v&63];
54 }
55 while (io & 3) {
56 if (io >= out_len)
57 debug_return_size_t((size_t)-1); /* truncation is failure */
58 out[io++] = '=';
59 }
60 if (io >= out_len)
61 debug_return_size_t((size_t)-1); /* no room for NUL terminator */
62 out[io] = '\0';
63 debug_return_size_t(io);
64 }