fix indent (broken in last commit)
[claws.git] / src / common / uuencode.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #include <ctype.h>
21
22 #define UUDECODE(c) (c=='`' ? 0 : c - ' ')
23 #define N64(i) (i & ~63)
24
25 const char uudigit[64] =
26 {
27   '`', '!', '"', '#', '$', '%', '&', '\'',
28   '(', ')', '*', '+', ',', '-', '.', '/',
29   '0', '1', '2', '3', '4', '5', '6', '7',
30   '8', '9', ':', ';', '<', '=', '>', '?',
31   '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
32   'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
33   'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
34   'X', 'Y', 'Z', '[', '\\', ']', '^', '_'
35 };
36
37 int fromuutobits(char *out, const char *in)
38 {
39     int len, outlen, inlen;
40     register unsigned char digit1, digit2;
41
42     outlen = UUDECODE(in[0]);
43     in += 1;
44     if(outlen < 0 || outlen > 45)
45         return -2;
46     if(outlen == 0)
47         return 0;
48     inlen = (outlen * 4 + 2) / 3;
49     len = 0;
50
51     for( ; inlen>0; inlen-=4) {
52         digit1 = UUDECODE(in[0]);
53         if (N64(digit1)) return -1;
54         digit2 = UUDECODE(in[1]);
55         if (N64(digit2)) return -1;
56         out[len++] = (digit1 << 2) | (digit2 >> 4);
57         if (inlen > 2) {
58             digit1 = UUDECODE(in[2]);
59             if (N64(digit1)) return -1;
60             out[len++] = (digit2 << 4) | (digit1 >> 2);
61             if (inlen > 3) {
62                 digit2 = UUDECODE(in[3]);
63                 if (N64(digit2)) return -1;
64                 out[len++] = (digit1 << 6) | digit2;
65             }
66         }
67         in += 4;
68     }
69
70     return len == outlen ? len : -3;
71 }