4 This file is part of PulseAudio.
6 Copyright 2008 Colin Guthrie
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 This file was originally inspired by a file developed by
26 Kungliga Tekniska H�gskolan
36 #include <pulse/xmalloc.h>
40 static char base64_chars[] =
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
43 static int pos(char c)
46 for (p = base64_chars; *p; p++)
48 return p - base64_chars;
52 int pa_base64_encode(const void *data, int size, char **str)
57 const unsigned char *q;
59 p = s = pa_xnew(char, size * 4 / 3 + 4);
60 q = (const unsigned char *) data;
62 for (i = 0; i < size;) {
72 p[0] = base64_chars[(c & 0x00fc0000) >> 18];
73 p[1] = base64_chars[(c & 0x0003f000) >> 12];
74 p[2] = base64_chars[(c & 0x00000fc0) >> 6];
75 p[3] = base64_chars[(c & 0x0000003f) >> 0];
87 #define DECODE_ERROR 0xffffffff
89 static unsigned int token_decode(const char *token)
94 if (strlen(token) < 4)
96 for (i = 0; i < 4; i++) {
103 val += pos(token[i]);
107 return (marker << 24) | val;
110 int pa_base64_decode(const char *str, void *data)
116 for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
117 unsigned int val = token_decode(p);
118 unsigned int marker = (val >> 24) & 0xff;
119 if (val == DECODE_ERROR)
121 *q++ = (val >> 16) & 0xff;
123 *q++ = (val >> 8) & 0xff;
127 return q - (unsigned char *) data;