Path: blob/main/crypto/krb5/src/util/support/base64.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/base64.c - base64 encoder and decoder */2/*3* Copyright (c) 1995-2001 Kungliga Tekniska Högskolan4* (Royal Institute of Technology, Stockholm, Sweden).5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include <k5-platform.h>36#include <k5-base64.h>3738static const char base64_chars[] =39"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";4041char *42k5_base64_encode(const void *data, size_t len)43{44char *s, *p;45size_t i;46unsigned int c;47const unsigned char *q;4849if (len > SIZE_MAX / 4)50return NULL;5152p = s = malloc(len * 4 / 3 + 4);53if (p == NULL)54return NULL;55q = (const unsigned char *)data;5657for (i = 0; i < len;) {58c = q[i++];59c *= 256;60if (i < len)61c += q[i];62i++;63c *= 256;64if (i < len)65c += q[i];66i++;67p[0] = base64_chars[(c & 0x00fc0000) >> 18];68p[1] = base64_chars[(c & 0x0003f000) >> 12];69p[2] = base64_chars[(c & 0x00000fc0) >> 6];70p[3] = base64_chars[(c & 0x0000003f) >> 0];71if (i > len)72p[3] = '=';73if (i > len + 1)74p[2] = '=';75p += 4;76}77*p = '\0';78return s;79}8081#define DECODE_ERROR 0xffffffff8283/* Decode token, which must be four bytes long. */84static unsigned int85decode_token(const char *token)86{87int i, marker = 0;88unsigned int val = 0;89const char *p;9091for (i = 0; i < 4; i++) {92val *= 64;93if (token[i] == '=') {94marker++;95} else if (marker > 0) {96return DECODE_ERROR;97} else {98p = strchr(base64_chars, token[i]);99if (p == NULL)100return DECODE_ERROR;101val += p - base64_chars;102}103}104if (marker > 2)105return DECODE_ERROR;106return (marker << 24) | val;107}108109void *110k5_base64_decode(const char *str, size_t *len_out)111{112unsigned char *data, *q;113unsigned int val, marker;114size_t len;115116*len_out = SIZE_MAX;117118/* Allocate the output buffer. */119len = strlen(str);120if (len % 4)121return NULL;122q = data = malloc(len / 4 * 3);123if (data == NULL) {124*len_out = 0;125return NULL;126}127128/* Decode the string. */129for (; *str != '\0'; str += 4) {130val = decode_token(str);131if (val == DECODE_ERROR) {132free(data);133return NULL;134}135marker = (val >> 24) & 0xff;136*q++ = (val >> 16) & 0xff;137if (marker < 2)138*q++ = (val >> 8) & 0xff;139if (marker < 1)140*q++ = val & 0xff;141}142*len_out = q - data;143return data;144}145146147