]> code.delx.au - pulseaudio/blob - src/modules/raop/base64.c
5b061034174af5053a23547a8db3f5b0d468695d
[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.1 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 return -1;
49 }
50
51 int pa_base64_encode(const void *data, int size, char **str)
52 {
53 char *s, *p;
54 int i;
55 int c;
56 const unsigned char *q;
57
58 p = s = pa_xnew(char, size * 4 / 3 + 4);
59 q = (const unsigned char *) data;
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 int lpos = pos(token[i]);
102 if (lpos < 0)
103 return DECODE_ERROR;
104 val += lpos;
105 }
106 }
107 if (marker > 2)
108 return DECODE_ERROR;
109 return (marker << 24) | val;
110 }
111
112 int pa_base64_decode(const char *str, void *data)
113 {
114 const char *p;
115 unsigned char *q;
116
117 q = data;
118 for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
119 unsigned int val = token_decode(p);
120 unsigned int marker = (val >> 24) & 0xff;
121 if (val == DECODE_ERROR)
122 return -1;
123 *q++ = (val >> 16) & 0xff;
124 if (marker < 2)
125 *q++ = (val >> 8) & 0xff;
126 if (marker < 1)
127 *q++ = val & 0xff;
128 }
129 return q - (unsigned char *) data;
130 }