1 /* base64.c base64 encoding and decoding functions
2 * Copyright (C) 2002 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi 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.
11 * Shishi 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.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * This code is heavily modified from fetchmail (also GPL'd, of
24 * course) by Brendan Cully <brendan@kublai.com>, via Mutt.
26 * Original copyright notice:
28 * The code in the fetchmail distribution is Copyright 1997 by Eric
29 * S. Raymond. Portions are also copyrighted by Carl Harris, 1993
30 * and 1995. Copyright retained for the purpose of protecting free
31 * redistribution of source.
37 static char B64Chars
[64] = {
38 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
39 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
40 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
41 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
47 static const char base64val
[] = {
48 BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
,
50 BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
,
52 BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
, 62, BAD
, BAD
, BAD
,
54 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, BAD
, BAD
, BAD
, BAD
, BAD
, BAD
,
55 BAD
, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
56 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, BAD
, BAD
, BAD
, BAD
, BAD
,
57 BAD
, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
58 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, BAD
, BAD
, BAD
, BAD
, BAD
61 #define base64val(c) B64Chars[(unsigned int)(c)]
63 #define DECODE64(c) (isascii(c) ? base64val[c] : BAD)
65 /* raw bytes to null-terminated base 64 string */
67 shishi_to_base64 (unsigned char *out
, const unsigned char *in
, int len
,
70 while (len
>= 3 && olen
> 10)
72 *out
++ = B64Chars
[in
[0] >> 2];
73 *out
++ = B64Chars
[((in
[0] << 4) & 0x30) | (in
[1] >> 4)];
74 *out
++ = B64Chars
[((in
[1] << 2) & 0x3c) | (in
[2] >> 6)];
75 *out
++ = B64Chars
[in
[2] & 0x3f];
81 /* clean up remainder */
82 if (len
> 0 && olen
> 4)
84 unsigned char fragment
;
86 *out
++ = B64Chars
[in
[0] >> 2];
87 fragment
= (in
[0] << 4) & 0x30;
89 fragment
|= in
[1] >> 4;
90 *out
++ = B64Chars
[fragment
];
91 *out
++ = (len
< 2) ? '=' : B64Chars
[(in
[1] << 2) & 0x3c];
97 /* Convert '\0'-terminated base 64 string to raw bytes.
98 * Returns length of returned buffer, or -1 on error */
100 shishi_from_base64 (unsigned char *out
, const unsigned char *in
)
103 register unsigned char digit1
, digit2
, digit3
, digit4
;
108 if (digit1
> 127 || DECODE64 (digit1
) == BAD
)
111 if (digit2
> 127 || DECODE64 (digit2
) == BAD
)
114 if (digit3
> 127 || ((digit3
!= '=') && (DECODE64 (digit3
) == BAD
)))
117 if (digit4
> 127 || ((digit4
!= '=') && (DECODE64 (digit4
) == BAD
)))
121 /* digits are already sanity-checked */
122 *out
++ = (DECODE64 (digit1
) << 2) | (DECODE64 (digit2
) >> 4);
127 ((DECODE64 (digit2
) << 4) & 0xf0) | (DECODE64 (digit3
) >> 2);
131 *out
++ = ((DECODE64 (digit3
) << 6) & 0xc0) | DECODE64 (digit4
);
136 while (*in
&& digit4
!= '=');