/*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 <stdlib.h>34#include <string.h>3536#include "dma.h"3738static char base64_chars[] =39"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";4041static int42pos(char c)43{44char *p;45for (p = base64_chars; *p; p++)46if (*p == c)47return p - base64_chars;48return -1;49}505152int53base64_encode(const void *data, int size, char **str)54{55char *s, *p;56int i;57int c;58const unsigned char *q;5960p = s = (char *) malloc(size * 4 / 3 + 4);61if (p == NULL)62return -1;63q = (const unsigned char *) data;64i = 0;65for (i = 0; i < size;) {66c = q[i++];67c *= 256;68if (i < size)69c += q[i];70i++;71c *= 256;72if (i < size)73c += q[i];74i++;75p[0] = base64_chars[(c & 0x00fc0000) >> 18];76p[1] = base64_chars[(c & 0x0003f000) >> 12];77p[2] = base64_chars[(c & 0x00000fc0) >> 6];78p[3] = base64_chars[(c & 0x0000003f) >> 0];79if (i > size)80p[3] = '=';81if (i > size + 1)82p[2] = '=';83p += 4;84}85*p = 0;86*str = s;87return strlen(s);88}8990#define DECODE_ERROR 0xffffffff9192static unsigned int93token_decode(const char *token)94{95int i;96unsigned int val = 0;97int marker = 0;98if (strlen(token) < 4)99return DECODE_ERROR;100for (i = 0; i < 4; i++) {101val *= 64;102if (token[i] == '=')103marker++;104else if (marker > 0)105return DECODE_ERROR;106else107val += pos(token[i]);108}109if (marker > 2)110return DECODE_ERROR;111return (marker << 24) | val;112}113114int115base64_decode(const char *str, void *data)116{117const char *p;118unsigned char *q;119120q = data;121for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {122unsigned int val = token_decode(p);123unsigned int marker = (val >> 24) & 0xff;124if (val == DECODE_ERROR)125return -1;126*q++ = (val >> 16) & 0xff;127if (marker < 2)128*q++ = (val >> 8) & 0xff;129if (marker < 1)130*q++ = val & 0xff;131}132return q - (unsigned char *) data;133}134135136137