comparison src/base64/base64.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children 589c365d90b1
comparison
equal deleted inserted replaced
9:31cc8a89cb74 10:26e34ae9a3e3
2 * 2 *
3 * This program is free software; you can redistribute it and/or modify 3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by 4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or 5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version. 6 * (at your option) any later version.
7 * 7 *
8 * This program is distributed in the hope that it will be useful, 8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details. 11 * GNU General Public License for more details.
12 * 12 *
23 23
24 #include <glib.h> 24 #include <glib.h>
25 #include <string.h> 25 #include <string.h>
26 #include "base64.h" 26 #include "base64.h"
27 27
28 gchar *base64_encode(guchar *buf, gint len) 28 gchar*
29 base64_encode(guchar * buf, gint len)
29 { 30 {
30 guchar *outbuf, *q; 31 guchar *outbuf, *q;
31 gchar enc[64]; 32 gchar enc[64];
32 gint i = 0, j = 0; 33 gint i = 0, j = 0;
33 guint in0, in1, in2; 34 guint in0, in1, in2;
34 35
35 for(; i < 26; i++) enc[i] = (gchar)('A' + j++); j = 0; 36 for (; i < 26; i++)
36 for(; i < 52; i++) enc[i] = (gchar)('a' + j++); j = 0; 37 enc[i] = (gchar) ('A' + j++);
37 for(; i < 62; i++) enc[i] = (gchar)('0' + j++); 38 j = 0;
38 enc[i++] = '+'; 39 for (; i < 52; i++)
39 enc[i++] = '/'; 40 enc[i] = (gchar) ('a' + j++);
41 j = 0;
42 for (; i < 62; i++)
43 enc[i] = (gchar) ('0' + j++);
44 enc[i++] = '+';
45 enc[i++] = '/';
40 46
41 outbuf = g_malloc(((len+3) * 8)/6); 47 outbuf = g_malloc(((len + 3) * 8) / 6);
42 q = outbuf; 48 q = outbuf;
43 49
44 i = 0; 50 i = 0;
45 while(i < len-2){ 51 while (i < len - 2) {
46 in0 = buf[i++]; 52 in0 = buf[i++];
47 in1 = buf[i++]; 53 in1 = buf[i++];
48 in2 = buf[i++]; 54 in2 = buf[i++];
49 55
50 *(q++) = enc[(in0 >> 2) & 0x3f]; 56 *(q++) = enc[(in0 >> 2) & 0x3f];
51 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f]; 57 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
52 *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f]; 58 *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
53 *(q++) = enc[in2 & 0x3f]; 59 *(q++) = enc[in2 & 0x3f];
54 } 60 }
55 if((len - i) == 1){ 61 if ((len - i) == 1) {
56 in0 = buf[i++]; 62 in0 = buf[i++];
57 *(q++) = enc[(in0 >> 2) & 0x3f]; 63 *(q++) = enc[(in0 >> 2) & 0x3f];
58 *(q++) = enc[(in0 << 4) & 0x3f]; 64 *(q++) = enc[(in0 << 4) & 0x3f];
59 *(q++) = '='; 65 *(q++) = '=';
60 *(q++) = '='; 66 *(q++) = '=';
61 }else if((len - i) == 2){ 67 } else if ((len - i) == 2) {
62 in0 = buf[i++]; 68 in0 = buf[i++];
63 in1 = buf[i++]; 69 in1 = buf[i++];
64 *(q++) = enc[(in0 >> 2) & 0x3f]; 70 *(q++) = enc[(in0 >> 2) & 0x3f];
65 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f]; 71 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
66 *(q++) = enc[(in1 << 2) & 0x3f]; 72 *(q++) = enc[(in1 << 2) & 0x3f];
67 *(q++) = '='; 73 *(q++) = '=';
68 } 74 }
69 *q = 0; 75 *q = 0;
70
71 return outbuf;
72 }
73
74 gchar *base64_decode(gchar *buf, gint *size)
75 {
76 guchar *p = buf, *q;
77 guint in[4];
78 // gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1);
79 gchar *out = g_malloc((strlen(buf)+3) + 1);
80 76
81 q = out; 77 return outbuf;
82 *size = 0;
83
84 *q = 0;
85
86 while(*p){
87 int i = 0;
88 while(i < 4){
89 if(!*p) break;
90 if((*p >= 'A') && (*p <= 'Z'))
91 in[i++] = *p - 'A';
92 else if((*p >= 'a') && (*p <= 'z'))
93 in[i++] = (*p - 'a') + 26;
94 else if((*p >= '0') && (*p <= '9'))
95 in[i++] = (*p - '0') + 52;
96 else if(*p == '+')
97 in[i++] = 62;
98 else if(*p == '/')
99 in[i++] = 63;
100 else if(*p == '='){
101 in[i++] = 0;
102 p++;
103 break;
104 }else if((*p != '\r') && (*p != '\n')){
105 p++;
106 break;
107 }
108 p++;
109 }
110 if((i == 4) || (p[-1] == '=')){
111 *(q++) = ((in[0] << 2) | (in[1] >> 4));
112 *(q++) = ((in[1] << 4) | (in[2] >> 2));
113 *(q++) = ((in[2] << 6) | in[3]);
114 if(p[-1] == '='){
115 if(i == 3){
116 (*size)++;
117 }
118 else if(i == 4){
119 (*size) += 2;
120 }
121 }else{
122 *size += 3;
123 }
124 }
125 }
126 return out;
127 } 78 }
128 79
80 gchar *base64_decode(gchar * buf, gint * size)
81 {
82 guchar *p = buf, *q;
83 guint in[4];
84 /* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */
85 gchar *out = g_malloc((strlen(buf) + 3) + 1);
86
87 q = out;
88 *size = 0;
89
90 *q = 0;
91
92 while (*p) {
93 int i = 0;
94 while (i < 4) {
95 if (!*p)
96 break;
97 if ((*p >= 'A') && (*p <= 'Z'))
98 in[i++] = *p - 'A';
99 else if ((*p >= 'a') && (*p <= 'z'))
100 in[i++] = (*p - 'a') + 26;
101 else if ((*p >= '0') && (*p <= '9'))
102 in[i++] = (*p - '0') + 52;
103 else if (*p == '+')
104 in[i++] = 62;
105 else if (*p == '/')
106 in[i++] = 63;
107 else if (*p == '=') {
108 in[i++] = 0;
109 p++;
110 break;
111 } else if ((*p != '\r') && (*p != '\n')) {
112 p++;
113 break;
114 }
115 p++;
116 }
117 if ((i == 4) || (p[-1] == '=')) {
118 *(q++) = ((in[0] << 2) | (in[1] >> 4));
119 *(q++) = ((in[1] << 4) | (in[2] >> 2));
120 *(q++) = ((in[2] << 6) | in[3]);
121 if (p[-1] == '=') {
122 if (i == 3) {
123 (*size)++;
124 } else if (i == 4) {
125 (*size) += 2;
126 }
127 } else {
128 *size += 3;
129 }
130 }
131 }
132 return out;
133 }