summary refs log tree commit diff stats
path: root/libs/cocos2d/Support/base64.c
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/Support/base64.c')
-rwxr-xr-xlibs/cocos2d/Support/base64.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/libs/cocos2d/Support/base64.c b/libs/cocos2d/Support/base64.c new file mode 100755 index 0000000..9aa52a6 --- /dev/null +++ b/libs/cocos2d/Support/base64.c
@@ -0,0 +1,93 @@
1/*
2 public domain BASE64 code
3
4 modified for cocos2d-iphone: http://www.cocos2d-iphone.org
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9
10#include "base64.h"
11
12unsigned char alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
13
14int _base64Decode( unsigned char *input, unsigned int input_len, unsigned char *output, unsigned int *output_len );
15
16int _base64Decode( unsigned char *input, unsigned int input_len, unsigned char *output, unsigned int *output_len )
17{
18 static char inalphabet[256], decoder[256];
19 int i, bits, c, char_count, errors = 0;
20 unsigned int input_idx = 0;
21 unsigned int output_idx = 0;
22
23 for (i = (sizeof alphabet) - 1; i >= 0 ; i--) {
24 inalphabet[alphabet[i]] = 1;
25 decoder[alphabet[i]] = i;
26 }
27
28 char_count = 0;
29 bits = 0;
30 for( input_idx=0; input_idx < input_len ; input_idx++ ) {
31 c = input[ input_idx ];
32 if (c == '=')
33 break;
34 if (c > 255 || ! inalphabet[c])
35 continue;
36 bits += decoder[c];
37 char_count++;
38 if (char_count == 4) {
39 output[ output_idx++ ] = (bits >> 16);
40 output[ output_idx++ ] = ((bits >> 8) & 0xff);
41 output[ output_idx++ ] = ( bits & 0xff);
42 bits = 0;
43 char_count = 0;
44 } else {
45 bits <<= 6;
46 }
47 }
48
49 if( c == '=' ) {
50 switch (char_count) {
51 case 1:
52 fprintf(stderr, "base64Decode: encoding incomplete: at least 2 bits missing");
53 errors++;
54 break;
55 case 2:
56 output[ output_idx++ ] = ( bits >> 10 );
57 break;
58 case 3:
59 output[ output_idx++ ] = ( bits >> 16 );
60 output[ output_idx++ ] = (( bits >> 8 ) & 0xff);
61 break;
62 }
63 } else if ( input_idx < input_len ) {
64 if (char_count) {
65 fprintf(stderr, "base64 encoding incomplete: at least %d bits truncated",
66 ((4 - char_count) * 6));
67 errors++;
68 }
69 }
70
71 *output_len = output_idx;
72 return errors;
73}
74
75int base64Decode(unsigned char *in, unsigned int inLength, unsigned char **out)
76{
77 unsigned int outLength = 0;
78
79 //should be enough to store 6-bit buffers in 8-bit buffers
80 *out = malloc( inLength * 3.0f / 4.0f + 1 );
81 if( *out ) {
82 int ret = _base64Decode(in, inLength, *out, &outLength);
83
84 if (ret > 0 )
85 {
86 printf("Base64Utils: error decoding");
87 free(*out);
88 *out = NULL;
89 outLength = 0;
90 }
91 }
92 return outLength;
93}