-
Notifications
You must be signed in to change notification settings - Fork 0
/
b64.c
160 lines (138 loc) · 3.45 KB
/
b64.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* $Id: b64.c,v 1.1 2003/12/11 11:04:15 luis Exp $
* Author: Luis Colorado <Luis.Colorado@HispaLinux.ES>
* Date: Sun Oct 12 17:37:07 MEST 2003
*
* Disclaimer:
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define IN_B64_C
/* Standard include files */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "b64.h"
/* constants */
/* variables */
static char B64_C_RCSId[]="\n$Id: b64.c,v 1.1 2003/12/11 11:04:15 luis Exp $\n";
static char ct[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
/* functions */
void b64_init(b64_st *s)
{
s->n = s->bits = s->i = 0;
} /* b64_init */
size_t b64_code(
b64_st *s,
char *b,
size_t n,
char *ob)
{
size_t res = 0;
for(;n;b++,n--) {
switch(s->n) {
case 0: /* primer byte */
*ob++ = ct[ (b[0]>>2)]; res++;
s->bits = (b[0]&0x03) << 4;
s->i++;
s->n = 2; break;
case 2: /* segundo byte */
*ob++ = ct[s->bits | (b[0]>>4)]; res++;
s->bits = (b[0]&0x0f) << 2;
s->i++;
s->n = 4; break;
case 4: /* tercer byte */
*ob++ = ct[s->bits | (b[0]>>6)]; res++; s->i++;
*ob++ = ct[b[0]&0x3f]; res++;
if (++s->i >= 64) { s->i = 0; *ob++ = '\n'; res++; }
s->bits = s->n = 0; break;
} /* switch */
} /* for */
return res;
} /* b64_code */
size_t b64_decode(
b64_st *s,
char *b,
size_t n,
char *ob)
{
size_t res = 0;
if (s->n == 7) return res;
for (;n;b++,n--) {
char *p;
unsigned char c;
if (isspace(b[0]))
continue;
p = strchr(ct, b[0]);
if (!p) {
s->n = 7;
break;
}
c = p - ct;
switch(s->n) {
case 0:
s->bits = c << 2; s->n = 6; break;
case 2:
*ob++ = s->bits | (c ); res++;
s->bits = 0; s->n = 0; break;
case 4:
*ob++ = s->bits | (c >> 2); res++;
s->bits = (c & 0x03) << 6; s->n = 2; break;
case 6:
*ob++ = s->bits | (c >> 4); res++;
s->bits = (c & 0x0f) << 4; s->n = 4; break;
} /* switch */
} /* for */
return res;
} /* b64_decode */
size_t b64_code_end(
b64_st *s,
char *ob)
{
size_t res = 0;
switch(s->n) {
case 0: /* bytes múltiplo de tres, la salida tiene múltiplo de 4 bytes */
break;
case 2: /* solo hay un byte, ponemos el segundo y los dos terminadores */
*ob++ = ct[s->bits]; res++;
*ob++ = '='; res++;
*ob++ = '='; res++;
break;
case 4: /* hay dos bytes, ponemos el tercero y el terminador */
*ob++ = ct[s->bits]; res++;
*ob++ = '='; res++;
break;
} /* switch */
*ob++ = '\n'; res++;
s->bits = s->n = s->i = 0;
return res;
} /* b64_code_end */
size_t b64_decode_end(b64_st *s, char *ob)
{
size_t res = 0;
char *p;
unsigned char c;
switch(s->n) {
case 0: break;
case 2: case 4: case 6:
*ob++ = s->bits; res++;
s->bits = s->n = 0; break;
} /* switch */
return res;
} /* b64_decode_end */
/* $Id: b64.c,v 1.1 2003/12/11 11:04:15 luis Exp $ */