root / libsylph / uuencode.c @ 528
History | View | Annotate | Download (2.7 kB)
| 1 | 528 | hiro | /*
|
|---|---|---|---|
| 2 | 528 | hiro | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | 528 | hiro | * Copyright (C) 1999,2000 Hiroyuki Yamamoto |
| 4 | 528 | hiro | * |
| 5 | 528 | hiro | * This program is free software; you can redistribute it and/or modify |
| 6 | 528 | hiro | * it under the terms of the GNU General Public License as published by |
| 7 | 528 | hiro | * the Free Software Foundation; either version 2 of the License, or |
| 8 | 528 | hiro | * (at your option) any later version. |
| 9 | 528 | hiro | * |
| 10 | 528 | hiro | * This program is distributed in the hope that it will be useful, |
| 11 | 528 | hiro | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | 528 | hiro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | 528 | hiro | * GNU General Public License for more details. |
| 14 | 528 | hiro | * |
| 15 | 528 | hiro | * You should have received a copy of the GNU General Public License |
| 16 | 528 | hiro | * along with this program; if not, write to the Free Software |
| 17 | 528 | hiro | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | 528 | hiro | */ |
| 19 | 528 | hiro | |
| 20 | 528 | hiro | #include <ctype.h> |
| 21 | 528 | hiro | |
| 22 | 528 | hiro | #define UUDECODE(c) (c=='`' ? 0 : c - ' ') |
| 23 | 528 | hiro | #define N64(i) (i & ~63) |
| 24 | 528 | hiro | |
| 25 | 528 | hiro | const char uudigit[64] = |
| 26 | 528 | hiro | {
|
| 27 | 528 | hiro | '`', '!', '"', '#', '$', '%', '&', '\'', |
| 28 | 528 | hiro | '(', ')', '*', '+', ',', '-', '.', '/', |
| 29 | 528 | hiro | '0', '1', '2', '3', '4', '5', '6', '7', |
| 30 | 528 | hiro | '8', '9', ':', ';', '<', '=', '>', '?', |
| 31 | 528 | hiro | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
| 32 | 528 | hiro | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
| 33 | 528 | hiro | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
| 34 | 528 | hiro | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_' |
| 35 | 528 | hiro | }; |
| 36 | 528 | hiro | |
| 37 | 528 | hiro | int touufrombits(unsigned char *out, const unsigned char *in, int inlen) |
| 38 | 528 | hiro | {
|
| 39 | 528 | hiro | int len;
|
| 40 | 528 | hiro | |
| 41 | 528 | hiro | if (inlen > 45) return -1; |
| 42 | 528 | hiro | len = (inlen * 4 + 2) / 3 + 1; |
| 43 | 528 | hiro | *out++ = uudigit[inlen]; |
| 44 | 528 | hiro | |
| 45 | 528 | hiro | for (; inlen >= 3; inlen -= 3) { |
| 46 | 528 | hiro | *out++ = uudigit[in[0] >> 2]; |
| 47 | 528 | hiro | *out++ = uudigit[((in[0] << 4) & 0x30) | (in[1] >> 4)]; |
| 48 | 528 | hiro | *out++ = uudigit[((in[1] << 2) & 0x3c) | (in[2] >> 6)]; |
| 49 | 528 | hiro | *out++ = uudigit[in[2] & 0x3f]; |
| 50 | 528 | hiro | in += 3;
|
| 51 | 528 | hiro | } |
| 52 | 528 | hiro | |
| 53 | 528 | hiro | if (inlen > 0) { |
| 54 | 528 | hiro | *out++ = uudigit[(in[0] >> 2)]; |
| 55 | 528 | hiro | if (inlen == 1) { |
| 56 | 528 | hiro | *out++ = uudigit[((in[0] << 4) & 0x30)]; |
| 57 | 528 | hiro | } else {
|
| 58 | 528 | hiro | *out++ = uudigit[(((in[0] << 4) & 0x30) | (in[1] >> 4))] ; |
| 59 | 528 | hiro | *out++ = uudigit[((in[1] << 2) & 0x3c)]; |
| 60 | 528 | hiro | } |
| 61 | 528 | hiro | } |
| 62 | 528 | hiro | *out = '\0';
|
| 63 | 528 | hiro | |
| 64 | 528 | hiro | return len;
|
| 65 | 528 | hiro | } |
| 66 | 528 | hiro | |
| 67 | 528 | hiro | int fromuutobits(char *out, const char *in) |
| 68 | 528 | hiro | {
|
| 69 | 528 | hiro | int len, outlen, inlen;
|
| 70 | 528 | hiro | register unsigned char digit1, digit2; |
| 71 | 528 | hiro | |
| 72 | 528 | hiro | outlen = UUDECODE(in[0]);
|
| 73 | 528 | hiro | in += 1;
|
| 74 | 528 | hiro | if(outlen < 0 || outlen > 45) |
| 75 | 528 | hiro | return -2; |
| 76 | 528 | hiro | if(outlen == 0) |
| 77 | 528 | hiro | return 0; |
| 78 | 528 | hiro | inlen = (outlen * 4 + 2) / 3; |
| 79 | 528 | hiro | len = 0;
|
| 80 | 528 | hiro | |
| 81 | 528 | hiro | for( ; inlen>0; inlen-=4) { |
| 82 | 528 | hiro | digit1 = UUDECODE(in[0]);
|
| 83 | 528 | hiro | if (N64(digit1)) return -1; |
| 84 | 528 | hiro | digit2 = UUDECODE(in[1]);
|
| 85 | 528 | hiro | if (N64(digit2)) return -1; |
| 86 | 528 | hiro | out[len++] = (digit1 << 2) | (digit2 >> 4); |
| 87 | 528 | hiro | if (inlen > 2) { |
| 88 | 528 | hiro | digit1 = UUDECODE(in[2]);
|
| 89 | 528 | hiro | if (N64(digit1)) return -1; |
| 90 | 528 | hiro | out[len++] = (digit2 << 4) | (digit1 >> 2); |
| 91 | 528 | hiro | if (inlen > 3) { |
| 92 | 528 | hiro | digit2 = UUDECODE(in[3]);
|
| 93 | 528 | hiro | if (N64(digit2)) return -1; |
| 94 | 528 | hiro | out[len++] = (digit1 << 6) | digit2;
|
| 95 | 528 | hiro | } |
| 96 | 528 | hiro | } |
| 97 | 528 | hiro | in += 4;
|
| 98 | 528 | hiro | } |
| 99 | 528 | hiro | |
| 100 | 528 | hiro | return len == outlen ? len : -3; |
| 101 | 528 | hiro | } |