]> code.delx.au - pulseaudio/blob - src/modules/rtp/base64.c
043ef5a8a19806774e017fb3d51d2c94656e0c16
[pulseaudio] / src / modules / rtp / base64.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2008 Colin Guthrie
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 /*
25 This file was originally inspired by a file developed by
26 Kungliga Tekniska H�gskolan
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <pulse/xmalloc.h>
37
38 #include "base64.h"
39
40 static char base64_chars[] =
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
42
43 static int pos(char c)
44 {
45 char *p;
46 for (p = base64_chars; *p; p++)
47 if (*p == c)
48 return p - base64_chars;
49 return -1;
50 }
51
52 int pa_base64_encode(const void *data, int size, char **str)
53 {
54 char *s, *p;
55 int i;
56 int c;
57 const unsigned char *q;
58
59 p = s = pa_xnew(char, size * 4 / 3 + 4);
60 q = (const unsigned char *) data;
61 i = 0;
62 for (i = 0; i < size;) {
63 c = q[i++];
64 c *= 256;
65 if (i < size)
66 c += q[i];
67 i++;
68 c *= 256;
69 if (i < size)
70 c += q[i];
71 i++;
72 p[0] = base64_chars[(c & 0x00fc0000) >> 18];
73 p[1] = base64_chars[(c & 0x0003f000) >> 12];
74 p[2] = base64_chars[(c & 0x00000fc0) >> 6];
75 p[3] = base64_chars[(c & 0x0000003f) >> 0];
76 if (i > size)
77 p[3] = '=';
78 if (i > size + 1)
79 p[2] = '=';
80 p += 4;
81 }
82 *p = 0;
83 *str = s;
84 return strlen(s);
85 }
86
87 #define DECODE_ERROR 0xffffffff
88
89 static unsigned int token_decode(const char *token)
90 {
91 int i;
92 unsigned int val = 0;
93 int marker = 0;
94 if (strlen(token) < 4)
95 return DECODE_ERROR;
96 for (i = 0; i < 4; i++) {
97 val *= 64;
98 if (token[i] == '=')
99 marker++;
100 else if (marker > 0)
101 return DECODE_ERROR;
102 else
103 val += pos(token[i]);
104 }
105 if (marker > 2)
106 return DECODE_ERROR;
107 return (marker << 24) | val;
108 }
109
110 int pa_base64_decode(const char *str, void *data)
111 {
112 const char *p;
113 unsigned char *q;
114
115 q = data;
116 for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
117 unsigned int val = token_decode(p);
118 unsigned int marker = (val >> 24) & 0xff;
119 if (val == DECODE_ERROR)
120 return -1;
121 *q++ = (val >> 16) & 0xff;
122 if (marker < 2)
123 *q++ = (val >> 8) & 0xff;
124 if (marker < 1)
125 *q++ = val & 0xff;
126 }
127 return q - (unsigned char *) data;
128 }