]> code.delx.au - pulseaudio/blob - src/modules/raop/base64.c
Fix a potentially non-returning function in base64 code.
[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 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 i = 0;
61 for (i = 0; i < size;) {
62 c = q[i++];
63 c *= 256;
64 if (i < size)
65 c += q[i];
66 i++;
67 c *= 256;
68 if (i < size)
69 c += q[i];
70 i++;
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];
75 if (i > size)
76 p[3] = '=';
77 if (i > size + 1)
78 p[2] = '=';
79 p += 4;
80 }
81 *p = 0;
82 *str = s;
83 return strlen(s);
84 }
85
86 #define DECODE_ERROR 0xffffffff
87
88 static unsigned int token_decode(const char *token)
89 {
90 int i;
91 unsigned int val = 0;
92 int marker = 0;
93 if (strlen(token) < 4)
94 return DECODE_ERROR;
95 for (i = 0; i < 4; i++) {
96 val *= 64;
97 if (token[i] == '=')
98 marker++;
99 else if (marker > 0)
100 return DECODE_ERROR;
101 else {
102 int lpos = pos(token[i]);
103 if (lpos < 0)
104 return DECODE_ERROR;
105 val += lpos;
106 }
107 }
108 if (marker > 2)
109 return DECODE_ERROR;
110 return (marker << 24) | val;
111 }
112
113 int pa_base64_decode(const char *str, void *data)
114 {
115 const char *p;
116 unsigned char *q;
117
118 q = 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)
123 return -1;
124 *q++ = (val >> 16) & 0xff;
125 if (marker < 2)
126 *q++ = (val >> 8) & 0xff;
127 if (marker < 1)
128 *q++ = val & 0xff;
129 }
130 return q - (unsigned char *) data;
131 }