]> code.delx.au - pulseaudio/blob - src/modules/raop/base64.c
Fix up according to Coding Style
[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 if (c >= 'A' && c <= 'Z') return c - 'A' + 0;
43 if (c >= 'a' && c <= 'z') return c - 'a' + 26;
44 if (c >= '0' && c <= '9') return c - '0' + 52;
45 if (c == '+') return 62;
46 if (c == '/') return 63;
47 return -1;
48 }
49
50 int pa_base64_encode(const void *data, int size, char **str) {
51 char *s, *p;
52 int i;
53 int c;
54 const unsigned char *q;
55
56 p = s = pa_xnew(char, size * 4 / 3 + 4);
57 q = (const unsigned char *) data;
58 for (i = 0; i < size;) {
59 c = q[i++];
60 c *= 256;
61 if (i < size)
62 c += q[i];
63 i++;
64 c *= 256;
65 if (i < size)
66 c += q[i];
67 i++;
68 p[0] = base64_chars[(c & 0x00fc0000) >> 18];
69 p[1] = base64_chars[(c & 0x0003f000) >> 12];
70 p[2] = base64_chars[(c & 0x00000fc0) >> 6];
71 p[3] = base64_chars[(c & 0x0000003f) >> 0];
72 if (i > size)
73 p[3] = '=';
74 if (i > size + 1)
75 p[2] = '=';
76 p += 4;
77 }
78 *p = 0;
79 *str = s;
80 return strlen(s);
81 }
82
83 #define DECODE_ERROR 0xffffffff
84
85 static unsigned int token_decode(const char *token) {
86 int i;
87 unsigned int val = 0;
88 int marker = 0;
89 if (strlen(token) < 4)
90 return DECODE_ERROR;
91 for (i = 0; i < 4; i++) {
92 val *= 64;
93 if (token[i] == '=')
94 marker++;
95 else if (marker > 0)
96 return DECODE_ERROR;
97 else {
98 int lpos = pos(token[i]);
99 if (lpos < 0)
100 return DECODE_ERROR;
101 val += lpos;
102 }
103 }
104 if (marker > 2)
105 return DECODE_ERROR;
106 return (marker << 24) | val;
107 }
108
109 int pa_base64_decode(const char *str, void *data) {
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 }