Annotation of embedaddon/sudo/plugins/sudoers/base64.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
        !             3:  *
        !             4:  * Permission to use, copy, modify, and distribute this software for any
        !             5:  * purpose with or without fee is hereby granted, provided that the above
        !             6:  * copyright notice and this permission notice appear in all copies.
        !             7:  *
        !             8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !             9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            15:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
        !            16:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        !            17:  */
        !            18: 
        !            19: #include <config.h>
        !            20: 
        !            21: #include <sys/types.h>
        !            22: #include <stdio.h>
        !            23: #ifdef STDC_HEADERS
        !            24: # include <stdlib.h>
        !            25: # include <stddef.h>
        !            26: #else
        !            27: # ifdef HAVE_STDLIB_H
        !            28: #  include <stdlib.h>
        !            29: # endif
        !            30: #endif /* STDC_HEADERS */
        !            31: #ifdef HAVE_STRING_H
        !            32: # include <string.h>
        !            33: #endif /* HAVE_STRING_H */
        !            34: #ifdef HAVE_STRINGS_H
        !            35: # include <strings.h>
        !            36: #endif /* HAVE_STRINGS_H */
        !            37: 
        !            38: #include "missing.h"
        !            39: #include "sudo_debug.h"
        !            40: 
        !            41: /*
        !            42:  * Decode a NUL-terminated string in base64 format and store the
        !            43:  * result in dst.
        !            44:  */
        !            45: size_t
        !            46: base64_decode(const char *str, unsigned char *dst, size_t dsize)
        !            47: {
        !            48:     static const char b64[] =
        !            49:        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        !            50:     const unsigned char *dst0 = dst;
        !            51:     const unsigned char *dend = dst + dsize;
        !            52:     unsigned char ch[4];
        !            53:     char *pos;
        !            54:     int i;
        !            55:     debug_decl(base64_decode, SUDO_DEBUG_MATCH)
        !            56: 
        !            57:     /*
        !            58:      * Convert from base64 to binary.  Each base64 char holds 6 bits of data
        !            59:      * so 4 base64 chars equals 3 chars of data.
        !            60:      * Padding (with the '=' char) may or may not be present.
        !            61:      */
        !            62:     while (*str != '\0') {
        !            63:        for (i = 0; i < 4; i++) {
        !            64:            switch (*str) {
        !            65:            case '=':
        !            66:                str++;
        !            67:                /* FALLTHROUGH */
        !            68:            case '\0':
        !            69:                ch[i] = '=';
        !            70:                break;
        !            71:            default:
        !            72:                if ((pos = strchr(b64, *str++)) == NULL)
        !            73:                    debug_return_size_t((size_t)-1);
        !            74:                ch[i] = (unsigned char)(pos - b64);
        !            75:                break;
        !            76:            }
        !            77:        }
        !            78:        if (ch[0] == '=' || ch[1] == '=' || dst == dend)
        !            79:            break;
        !            80:        *dst++ = (ch[0] << 2) | ((ch[1] & 0x30) >> 4);
        !            81:        if (ch[2] == '=' || dst == dend)
        !            82:            break;
        !            83:        *dst++ = ((ch[1] & 0x0f) << 4) | ((ch[2] & 0x3c) >> 2);
        !            84:        if (ch[3] == '=' || dst == dend)
        !            85:            break;
        !            86:        *dst++ = ((ch[2] & 0x03) << 6) | ch[3];
        !            87:     }
        !            88:     debug_return_size_t((size_t)(dst - dst0));
        !            89: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>