/*1* Copyright (c) 1995-2001 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435#include <stdlib.h>36#include <string.h>37#include <limits.h>38#include "base64.h"3940static const char base64_chars[] =41"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";4243static int44pos(char c)45{46const char *p;47for (p = base64_chars; *p; p++)48if (*p == c)49return p - base64_chars;50return -1;51}5253ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL54base64_encode(const void *data, int size, char **str)55{56char *s, *p;57int i;58int c;59const unsigned char *q;6061if (size > INT_MAX/4 || size < 0) {62*str = NULL;63return -1;64}6566p = s = (char *) malloc(size * 4 / 3 + 4);67if (p == NULL) {68*str = NULL;69return -1;70}71q = (const unsigned char *) data;7273for (i = 0; i < size;) {74c = q[i++];75c *= 256;76if (i < size)77c += q[i];78i++;79c *= 256;80if (i < size)81c += q[i];82i++;83p[0] = base64_chars[(c & 0x00fc0000) >> 18];84p[1] = base64_chars[(c & 0x0003f000) >> 12];85p[2] = base64_chars[(c & 0x00000fc0) >> 6];86p[3] = base64_chars[(c & 0x0000003f) >> 0];87if (i > size)88p[3] = '=';89if (i > size + 1)90p[2] = '=';91p += 4;92}93*p = 0;94*str = s;95return (int) strlen(s);96}9798#define DECODE_ERROR 0xffffffff99100static unsigned int101token_decode(const char *token)102{103int i;104unsigned int val = 0;105int marker = 0;106if (strlen(token) < 4)107return DECODE_ERROR;108for (i = 0; i < 4; i++) {109val *= 64;110if (token[i] == '=')111marker++;112else if (marker > 0)113return DECODE_ERROR;114else115val += pos(token[i]);116}117if (marker > 2)118return DECODE_ERROR;119return (marker << 24) | val;120}121122ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL123base64_decode(const char *str, void *data)124{125const char *p;126unsigned char *q;127128q = data;129for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {130unsigned int val = token_decode(p);131unsigned int marker = (val >> 24) & 0xff;132if (val == DECODE_ERROR)133return -1;134*q++ = (val >> 16) & 0xff;135if (marker < 2)136*q++ = (val >> 8) & 0xff;137if (marker < 1)138*q++ = val & 0xff;139}140return q - (unsigned char *) data;141}142143144