1 /*
2  * $Id: base64.c,v 1.7 2003/11/25 09:28:27 alphix Exp $
3  *
4  * This file is part of Ample.
5  *
6  * Ample is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Ample is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Ample; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #include <config.h>
22 
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif
28 
29 #include "ample.h"
30 #include "base64.h"
31 
32 static int
valof(char c)33 valof(char c)
34 {
35         char *alphabet =
36 		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
37 	char *tmp = alphabet;
38 	int i = 0;
39 
40 	if (c == '\0')
41 		return -1;
42 
43 	while(TRUE) {
44 		if (*tmp == '\0')
45 			return -1;
46 		if (*tmp == c)
47 			return i;
48 		i++;
49 		tmp++;
50 	}
51 }
52 
53 bool
isbase64(char c)54 isbase64(char c)
55 {
56 	if(valof(c) >= 0)
57 		return(TRUE);
58 	else
59 		return(FALSE);
60 }
61 
62 static void
nextchunk(char ** c,char * chunk)63 nextchunk(char ** c, char * chunk)
64 {
65 	int i;
66 	int b64val[4];
67 
68 	for(i=0; i < 4; i++) {
69 		if(**c != '\0') {
70 			b64val[i] = valof(**c);
71 			(*c)++;
72 		} else {
73 			b64val[i] = '\0';
74 		}
75 	}
76 
77 	*(chunk + 0) = CHAR1(b64val[0],b64val[1]);
78 	*(chunk + 1) = CHAR2(b64val[1],b64val[2]);
79 	*(chunk + 2) = CHAR3(b64val[2],b64val[3]);
80 
81 }
82 
83 char *
b64dec(char * msg)84 b64dec(char * msg)
85 {
86 	char * tmp = msg;
87 	char * buffer;
88 	char chunk[4];
89 
90 	if(*tmp == '\0')
91 		return NULL;
92 
93 	memset(chunk,0,sizeof(chunk));
94 	buffer = (char *)malloc(strlen(msg) + 1);
95 	*buffer = '\0';
96 
97 	while(*tmp != '\0') {
98 		nextchunk(&tmp,chunk);
99 		strcat(buffer,chunk);
100 	}
101 
102 	return buffer;
103 }
104 
105 /* I stored this here for now, might be useful later - David */
106  /*
107  * Sends a sequence of digital silence that is at least as long as duration
108  * and probably shorter than duration + max length of a frame
109  * (which is something like 26.126 milliseconds)
110  */
111 /*
112 static bool
113 sendsilence(float duration, FILE *to, int *metaflag)
114 {
115 	int i = 0;
116 */
117 	/* FrameSize = 144 * BitRate / (SampleRate + Padding) */
118 	/*double framesize = (144 * 128*1000) / (44.1*1000 + 0);*/
119 	/* Frames = Time * SampleRate / (FrameSize * 8) */
120 /*	int max = (int)ceil(((double)duration * 128*1000) / (framesize * 8));*/
121 	/* How much have we drifted from FrameSize bytes / frame */
122 /*	double diff = 0;
123 
124 	char framebody[410];
125 	char firstheaders[2][8] = {{0xFF,0xFB,0x92,0x04,0x00,0x00,0x00,0x00},
126 				   {0xFF,0xFB,0x92,0x04,0xBF,0x00,0x07,0xE8}};
127 	char shortheader[8]     =  {0xFF,0xFB,0x90,0x04,0xFF,0x80,0x0B,0xE8};
128 	char longheader[8]      =  {0xFF,0xFB,0x92,0x04,0xFF,0x80,0x0B,0xF0};
129 	char framebegin[28]     =  {0x00,0x69,0x00,0x00,0x00,0x00,0x00,0x00,
130 				    0x0D,0x20,0x00,0x00,0x00,0x00,0x00,0x01,
131 				    0xA4,0x00,0x00,0x00,0x00,0x00,0x00,0x34,
132 				    0x80,0x00,0x00,0x00};
133 
134 	memset(framebody, 0xFF, 410);
135 	memcpy(framebody, framebegin, 28);
136 	debug(3, "sending %i frames of digital silence", max);
137 
138 	while(i < max) {
139 		diff += 418 - framesize;
140 
141 		if(i < 2) {
142 			senddata(to, firstheaders[i], 8, metaflag);
143 			senddata(to, framebody, 410, metaflag);
144 		} else if(diff < 1) {
145 			senddata(to, longheader, 8, metaflag);
146 			senddata(to, framebody, 410, metaflag);
147 		} else {
148 			diff--;
149 			senddata(to, shortheader, 8, metaflag);
150 			senddata(to, shortheader, 409, metaflag);
151 		}
152 
153 		i++;
154 	}
155 
156 	return(TRUE);
157 }
158 */
159