"Fossies" - the Fresh Open Source Software Archive

Member "sudo-1.9.11p3/plugins/sudoers/b64_decode.c" (12 Jun 2022, 3142 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_decode.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 /*
   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 *in, unsigned char *out, size_t out_size)
   58 {
   59     unsigned char *out_end = out + out_size;
   60     const unsigned char *out0 = out;
   61     unsigned int rem, v;
   62     debug_decl(base64_decode, SUDOERS_DEBUG_MATCH);
   63 
   64     for (v = 0, rem = 0; *in != '\0' && *in != '='; in++) {
   65     unsigned char ch = base64dec_tab[(unsigned char)*in];
   66     if (ch == 255)
   67         debug_return_size_t((size_t)-1);
   68     v = (v << 6) | ch;
   69     rem += 6;
   70     if (rem >= 8) {
   71         rem -= 8;
   72         if (out >= out_end)
   73         debug_return_size_t((size_t)-1);
   74         *out++ = (v >> rem) & 0xff;
   75     }
   76     }
   77     if (rem >= 8) {
   78         if (out >= out_end)
   79         debug_return_size_t((size_t)-1);
   80         *out++ = (v >> rem) & 0xff;
   81     }
   82     debug_return_size_t((size_t)(out - out0));
   83 }