Add nettle.
[shishi.git] / gl / base64.c
blob841c3850101c53e40608e0de711fcadbc6982857
1 /* base64.c base64 encoding and decoding functions
2 * Copyright (C) 2002, 2003 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.
35 #include <string.h>
36 #include <ctype.h>
37 #include "base64.h"
39 static char B64Chars[64] = {
40 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
41 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
42 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
43 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
44 '8', '9', '+', '/'
47 #define BAD -1
49 static const char base64val[] = {
50 BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
51 BAD,
52 BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
53 BAD,
54 BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, 62, BAD, BAD, BAD,
55 63,
56 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, BAD, BAD, BAD, BAD, BAD, BAD,
57 BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
58 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, BAD, BAD, BAD, BAD, BAD,
59 BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
60 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, BAD, BAD, BAD, BAD, BAD
63 #define base64val(c) B64Chars[(unsigned int)(c)]
65 #define DECODE64(c) (isascii(c) ? base64val[c] : BAD)
67 /* raw bytes to null-terminated base 64 string */
68 void
69 base64_to (unsigned char *out, const unsigned char *in, int len, int olen)
71 while (len >= 3 && olen > 10)
73 *out++ = B64Chars[in[0] >> 2];
74 *out++ = B64Chars[((in[0] << 4) & 0x30) | (in[1] >> 4)];
75 *out++ = B64Chars[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
76 *out++ = B64Chars[in[2] & 0x3f];
77 olen -= 4;
78 len -= 3;
79 in += 3;
82 /* clean up remainder */
83 if (len > 0 && olen > 4)
85 unsigned char fragment;
87 *out++ = B64Chars[in[0] >> 2];
88 fragment = (in[0] << 4) & 0x30;
89 if (len > 1)
90 fragment |= in[1] >> 4;
91 *out++ = B64Chars[fragment];
92 *out++ = (len < 2) ? '=' : B64Chars[(in[1] << 2) & 0x3c];
93 *out++ = '=';
95 *out = '\0';
98 /* Convert '\0'-terminated base 64 string to raw bytes.
99 * Returns length of returned buffer, or -1 on error */
101 base64_from (unsigned char *out, const unsigned char *in)
103 int len = 0;
104 register unsigned char digit1, digit2, digit3, digit4;
108 digit1 = in[0];
109 if (digit1 > 127 || DECODE64 (digit1) == BAD)
110 return -1;
111 digit2 = in[1];
112 if (digit2 > 127 || DECODE64 (digit2) == BAD)
113 return -1;
114 digit3 = in[2];
115 if (digit3 > 127 || ((digit3 != '=') && (DECODE64 (digit3) == BAD)))
116 return -1;
117 digit4 = in[3];
118 if (digit4 > 127 || ((digit4 != '=') && (DECODE64 (digit4) == BAD)))
119 return -1;
120 in += 4;
122 /* digits are already sanity-checked */
123 *out++ = (DECODE64 (digit1) << 2) | (DECODE64 (digit2) >> 4);
124 len++;
125 if (digit3 != '=')
127 *out++ =
128 ((DECODE64 (digit2) << 4) & 0xf0) | (DECODE64 (digit3) >> 2);
129 len++;
130 if (digit4 != '=')
132 *out++ = ((DECODE64 (digit3) << 6) & 0xc0) | DECODE64 (digit4);
133 len++;
137 while (*in && digit4 != '=');
139 return len;