kdtree code cleanup
[swftools.git] / lib / base64.c
blob3c5d22fb0fb85b4133bbb5aa06e9ebc45e20e935
1 /* base64.c
3 Copyright (c) 2010 Matthias Kramm <kramm@quiss.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "base64.h"
25 static const char base64[65]=
26 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28 void write_base64(FILE*fi, const char* s)
30 const unsigned char*b = (const unsigned char*)s;
31 const unsigned char*end = b + strlen(b);
32 const unsigned char*end_minus_3 = end - 3;
34 while(b <= end_minus_3) {
35 char data[4];
36 data[0] = base64[(b[0]>>2)&63 ];
37 data[1] = base64[(b[0]<<4|b[1]>>4)&63];
38 data[2] = base64[(b[1]<<2|b[2]>>6)&63];
39 data[3] = base64[(b[2])&63 ];
40 fwrite(data, 4, 1, fi);
41 b += 3;
44 unsigned char data[4];
45 switch(end - b) {
46 case 0:
47 return; //done
48 case 1:
49 data[0] = base64[(b[0]>>2)&63];
50 data[1] = base64[(b[0]<<4)&63];
51 data[2] = '=';
52 data[3] = '=';
53 break;
54 case 2:
55 data[0] = base64[(b[0]>>2)&63];
56 data[1] = base64[(b[0]<<4|b[1]>>4)&63];
57 data[2] = base64[(b[1]<<2)&63];
58 data[3] = '=';
59 break;
60 default:
61 fprintf(stderr, "internal error in base64 implementation (%d)\n", end-b);
62 break;
64 fwrite(data, 4, 1, fi);