]> code.delx.au - pulseaudio/blob - src/modules/raop/base64.c
Merge commit 'flameeyes/libtool-2.2'
[pulseaudio] / src / modules / raop / base64.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Colin Guthrie
5
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 of the License,
9 or (at your option) any later version.
10
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.
15
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
19 USA.
20 ***/
21
22 /*
23 This file was originally inspired by a file developed by
24 Kungliga Tekniska H�gskolan
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <pulse/xmalloc.h>
35
36 #include "base64.h"
37
38 static const char base64_chars[] =
39 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40
41 static int pos(char c)
42 {
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;
48 }
49
50 int pa_base64_encode(const void *data, int size, char **str)
51 {
52 char *s, *p;
53 int i;
54 int c;
55 const unsigned char *q;
56
57 p = s = pa_xnew(char, size * 4 / 3 + 4);
58 q = (const unsigned char *) data;
59 i = 0;
60 for (i = 0; i < size;) {
61 c = q[i++];
62 c *= 256;
63 if (i < size)
64 c += q[i];
65 i++;
66 c *= 256;
67 if (i < size)
68 c += q[i];
69 i++;
70 p[0] = base64_chars[(c & 0x00fc0000) >> 18];
71 p[1] = base64_chars[(c & 0x0003f000) >> 12];
72 p[2] = base64_chars[(c & 0x00000fc0) >> 6];
73 p[3] = base64_chars[(c & 0x0000003f) >> 0];
74 if (i > size)
75 p[3] = '=';
76 if (i > size + 1)
77 p[2] = '=';
78 p += 4;
79 }
80 *p = 0;
81 *str = s;
82 return strlen(s);
83 }
84
85 #define DECODE_ERROR 0xffffffff
86
87 static unsigned int token_decode(const char *token)
88 {
89 int i;
90 unsigned int val = 0;
91 int marker = 0;
92 if (strlen(token) < 4)
93 return DECODE_ERROR;
94 for (i = 0; i < 4; i++) {
95 val *= 64;
96 if (token[i] == '=')
97 marker++;
98 else if (marker > 0)
99 return DECODE_ERROR;
100 else
101 val += pos(token[i]);
102 }
103 if (marker > 2)
104 return DECODE_ERROR;
105 return (marker << 24) | val;
106 }
107
108 int pa_base64_decode(const char *str, void *data)
109 {
110 const char *p;
111 unsigned char *q;
112
113 q = data;
114 for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
115 unsigned int val = token_decode(p);
116 unsigned int marker = (val >> 24) & 0xff;
117 if (val == DECODE_ERROR)
118 return -1;
119 *q++ = (val >> 16) & 0xff;
120 if (marker < 2)
121 *q++ = (val >> 8) & 0xff;
122 if (marker < 1)
123 *q++ = val & 0xff;
124 }
125 return q - (unsigned char *) data;
126 }