Add ticketlife, renewlife.
[shishi.git] / lib / base64.c
blobcabeaac5c98c71cb126a2937ad5dcc4a86f1ae8c
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.
35 #include "internal.h"
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',
42 '8', '9', '+', '/'
45 #define BAD -1
47 static const char base64val[] = {
48 BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
49 BAD,
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, 62, BAD, BAD, BAD,
53 63,
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 */
66 void
67 shishi_to_base64 (unsigned char *out, const unsigned char *in, int len,
68 int olen)
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];
76 olen -= 4;
77 len -= 3;
78 in += 3;
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;
88 if (len > 1)
89 fragment |= in[1] >> 4;
90 *out++ = B64Chars[fragment];
91 *out++ = (len < 2) ? '=' : B64Chars[(in[1] << 2) & 0x3c];
92 *out++ = '=';
94 *out = '\0';
97 /* Convert '\0'-terminated base 64 string to raw bytes.
98 * Returns length of returned buffer, or -1 on error */
99 int
100 shishi_from_base64 (unsigned char *out, const unsigned char *in)
102 int len = 0;
103 register unsigned char digit1, digit2, digit3, digit4;
107 digit1 = in[0];
108 if (digit1 > 127 || DECODE64 (digit1) == BAD)
109 return -1;
110 digit2 = in[1];
111 if (digit2 > 127 || DECODE64 (digit2) == BAD)
112 return -1;
113 digit3 = in[2];
114 if (digit3 > 127 || ((digit3 != '=') && (DECODE64 (digit3) == BAD)))
115 return -1;
116 digit4 = in[3];
117 if (digit4 > 127 || ((digit4 != '=') && (DECODE64 (digit4) == BAD)))
118 return -1;
119 in += 4;
121 /* digits are already sanity-checked */
122 *out++ = (DECODE64 (digit1) << 2) | (DECODE64 (digit2) >> 4);
123 len++;
124 if (digit3 != '=')
126 *out++ =
127 ((DECODE64 (digit2) << 4) & 0xf0) | (DECODE64 (digit3) >> 2);
128 len++;
129 if (digit4 != '=')
131 *out++ = ((DECODE64 (digit3) << 6) & 0xc0) | DECODE64 (digit4);
132 len++;
136 while (*in && digit4 != '=');
138 return len;