2 This file is part of PulseAudio.
4 Copyright 2008 Colin Guthrie
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 This file was originally inspired by a file developed by
24 Kungliga Tekniska H�gskolan
34 #include <pulse/xmalloc.h>
38 static const char base64_chars
[] =
39 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
41 static int pos(char c
)
43 if (c
>= 'A' && c
<= 'Z') return c
- 'A' + 0;
44 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
45 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
46 if (c
== '+') return 62;
47 if (c
== '/') return 63;
51 int pa_base64_encode(const void *data
, int size
, char **str
)
56 const unsigned char *q
;
58 p
= s
= pa_xnew(char, size
* 4 / 3 + 4);
59 q
= (const unsigned char *) data
;
61 for (i
= 0; i
< size
;) {
71 p
[0] = base64_chars
[(c
& 0x00fc0000) >> 18];
72 p
[1] = base64_chars
[(c
& 0x0003f000) >> 12];
73 p
[2] = base64_chars
[(c
& 0x00000fc0) >> 6];
74 p
[3] = base64_chars
[(c
& 0x0000003f) >> 0];
86 #define DECODE_ERROR 0xffffffff
88 static unsigned int token_decode(const char *token
)
93 if (strlen(token
) < 4)
95 for (i
= 0; i
< 4; i
++) {
102 int lpos
= pos(token
[i
]);
110 return (marker
<< 24) | val
;
113 int pa_base64_decode(const char *str
, void *data
)
119 for (p
= str
; *p
&& (*p
== '=' || strchr(base64_chars
, *p
)); p
+= 4) {
120 unsigned int val
= token_decode(p
);
121 unsigned int marker
= (val
>> 24) & 0xff;
122 if (val
== DECODE_ERROR
)
124 *q
++ = (val
>> 16) & 0xff;
126 *q
++ = (val
>> 8) & 0xff;
130 return q
- (unsigned char *) data
;