//base64.c: /* * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 * * This file is part of libroar a part of RoarAudio, * a cross-platform sound system for both, home and professional use. * See README for details. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 * as published by the Free Software Foundation. * * libroar is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; see the file COPYING. If not, write to * the Free Software Foundation, 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * * NOTE for everyone want's to change something and send patches: * read README and HACKING! There a addition information on * the license of this document you need to read before you send * any patches. * * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc * or libpulse*: * The libs libroaresd, libroararts and libroarpulse link this lib * and are therefore GPL. Because of this it may be illigal to use * them with any software that uses libesd, libartsc or libpulse*. */ #include "libroar.h" static char _base64_tab_fw[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; static char _base64_tab_bw[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; int roar_base64_init(struct roar_base64 * state, int flags) { if ( flags == -1 ) flags = 0; if ( state == NULL ) return -1; memset(state, 0, sizeof(struct roar_base64)); state->flags = flags; return 0; } static inline void _libroar_base64_encode_block(unsigned char out[4], const unsigned char in[3]) { out[0] = _base64_tab_fw[(in[0] >> 2) & 0x3F]; out[1] = _base64_tab_fw[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3F]; out[2] = _base64_tab_fw[(((in[1] & 0x0F) << 2) | ((in[2] >> 6) & 0x03)) & 0x3F]; out[3] = _base64_tab_fw[in[2] & 0x3F]; } #define _setoff(x) do { if ( off != NULL ) { *off = (x) } } while (0) ssize_t roar_base64_encode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off, int eof) { struct roar_base64 state_store; size_t offset = 0; size_t tlen; if ( inlen && in == NULL ) return -1; if ( outlen && out == NULL ) return -1; // this test should be removed as soon as those modes are supported. if ( in == NULL || out == NULL ) return -1; if ( state == NULL ) { state = &state_store; roar_base64_init_encode(state); eof = 1; } if ( state->buflen ) { if ( (state->buflen + inlen) >= 3 ) { tlen = 3 - state->buflen; memcpy(state->buf + state->buflen, in, tlen); offset += tlen; in += tlen; inlen -= tlen; } else { memcpy(state->buf + state->buflen, in, inlen); _setoff(inlen); return 0; } } } ssize_t roar_base64_decode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off); int roar_base64_is_eof(struct roar_base64 * state) { if ( state == NULL ) return -1; return !!(state->flags & ROAR_BASE64_FLAG_EOF); } int roar_base64_uninit(struct roar_base64 * state) { if ( state == NULL ) return -1; if ( state->buflen ) { ROAR_WARN("roar_base64_uninit(state=%p): State has bytes left in IO buffer. This is a application error."); } if ( state->reglen ) { ROAR_WARN("roar_base64_uninit(state=%p): State has bits left in decode register. This is a application error."); } return 0; } //ll