source: roaraudio/libroar/base64.c @ 4587:92d0a6279c00

Last change on this file since 4587:92d0a6279c00 was 4587:92d0a6279c00, checked in by phi, 13 years ago

some basic stuff to implement base64 encoding

File size: 4.4 KB
Line 
1//base64.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38static char _base64_tab_fw[] =
39 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 "abcdefghijklmnopqrstuvwxyz"
41 "0123456789+/";
42
43static char _base64_tab_bw[] = {
44  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
46  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
48  0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff,
49  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
50  0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
51  0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
52  0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
53  0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
54  0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
55};
56
57
58int roar_base64_init(struct roar_base64 * state, int flags) {
59 if ( flags == -1 )
60  flags = 0;
61
62 if ( state == NULL )
63  return -1;
64
65 memset(state, 0, sizeof(struct roar_base64));
66
67 state->flags = flags;
68
69 return 0;
70}
71
72static inline void _libroar_base64_encode_block(unsigned char out[4], const unsigned char in[3]) {
73 out[0] = _base64_tab_fw[(in[0] >> 2) & 0x3F];
74 out[1] = _base64_tab_fw[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3F];
75 out[2] = _base64_tab_fw[(((in[1] & 0x0F) << 2) | ((in[2] >> 6) & 0x03)) & 0x3F];
76 out[3] = _base64_tab_fw[in[2] & 0x3F];
77}
78
79#define _setoff(x) do { if ( off != NULL ) { *off = (x) } } while (0)
80
81ssize_t roar_base64_encode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off, int eof) {
82 struct roar_base64 state_store;
83 size_t offset = 0;
84 size_t tlen;
85
86 if ( inlen && in == NULL )
87  return -1;
88
89 if ( outlen && out == NULL )
90  return -1;
91
92 // this test should be removed as soon as those modes are supported.
93 if ( in == NULL || out == NULL )
94  return -1;
95
96 if ( state == NULL ) {
97  state = &state_store;
98  roar_base64_init_encode(state);
99  eof = 1;
100 }
101
102 if ( state->buflen ) {
103  if ( (state->buflen + inlen) >= 3 ) {
104   tlen = 3 - state->buflen;
105   memcpy(state->buf + state->buflen, in, tlen);
106   offset += tlen;
107   in += tlen;
108   inlen -= tlen;
109  } else {
110   memcpy(state->buf + state->buflen, in, inlen);
111   _setoff(inlen);
112   return 0;
113  }
114 }
115}
116
117ssize_t roar_base64_decode(struct roar_base64 * state, void * out, size_t outlen, void * in, size_t inlen, size_t * off);
118
119int roar_base64_is_eof(struct roar_base64 * state) {
120 if ( state == NULL )
121  return -1;
122
123 return !!(state->flags & ROAR_BASE64_FLAG_EOF);
124}
125
126int roar_base64_uninit(struct roar_base64 * state) {
127 if ( state == NULL )
128  return -1;
129
130 if ( state->buflen ) {
131  ROAR_WARN("roar_base64_uninit(state=%p): State has bytes left in IO buffer. This is a application error.");
132 }
133
134 if ( state->reglen ) {
135  ROAR_WARN("roar_base64_uninit(state=%p): State has bits left in decode register. This is a application error.");
136 }
137
138 return 0;
139}
140
141//ll
Note: See TracBrowser for help on using the repository browser.