root / libsylph / uuencode.c @ 8d7dcace
History | View | Annotate | Download (2.7 kB)
| 1 | /*
|
|---|---|
| 2 | * LibSylph -- E-Mail client library |
| 3 | * Copyright (C) 1999-2005 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library 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 GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 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 touufrombits(unsigned char *out, const unsigned char *in, int inlen) |
| 38 | {
|
| 39 | int len;
|
| 40 | |
| 41 | if (inlen > 45) return -1; |
| 42 | len = (inlen * 4 + 2) / 3 + 1; |
| 43 | *out++ = uudigit[inlen]; |
| 44 | |
| 45 | for (; inlen >= 3; inlen -= 3) { |
| 46 | *out++ = uudigit[in[0] >> 2]; |
| 47 | *out++ = uudigit[((in[0] << 4) & 0x30) | (in[1] >> 4)]; |
| 48 | *out++ = uudigit[((in[1] << 2) & 0x3c) | (in[2] >> 6)]; |
| 49 | *out++ = uudigit[in[2] & 0x3f]; |
| 50 | in += 3;
|
| 51 | } |
| 52 | |
| 53 | if (inlen > 0) { |
| 54 | *out++ = uudigit[(in[0] >> 2)]; |
| 55 | if (inlen == 1) { |
| 56 | *out++ = uudigit[((in[0] << 4) & 0x30)]; |
| 57 | } else {
|
| 58 | *out++ = uudigit[(((in[0] << 4) & 0x30) | (in[1] >> 4))] ; |
| 59 | *out++ = uudigit[((in[1] << 2) & 0x3c)]; |
| 60 | } |
| 61 | } |
| 62 | *out = '\0';
|
| 63 | |
| 64 | return len;
|
| 65 | } |
| 66 | |
| 67 | int fromuutobits(char *out, const char *in) |
| 68 | {
|
| 69 | int len, outlen, inlen;
|
| 70 | register unsigned char digit1, digit2; |
| 71 | |
| 72 | outlen = UUDECODE(in[0]);
|
| 73 | in += 1;
|
| 74 | if(outlen < 0 || outlen > 45) |
| 75 | return -2; |
| 76 | if(outlen == 0) |
| 77 | return 0; |
| 78 | inlen = (outlen * 4 + 2) / 3; |
| 79 | len = 0;
|
| 80 | |
| 81 | for( ; inlen>0; inlen-=4) { |
| 82 | digit1 = UUDECODE(in[0]);
|
| 83 | if (N64(digit1)) return -1; |
| 84 | digit2 = UUDECODE(in[1]);
|
| 85 | if (N64(digit2)) return -1; |
| 86 | out[len++] = (digit1 << 2) | (digit2 >> 4); |
| 87 | if (inlen > 2) { |
| 88 | digit1 = UUDECODE(in[2]);
|
| 89 | if (N64(digit1)) return -1; |
| 90 | out[len++] = (digit2 << 4) | (digit1 >> 2); |
| 91 | if (inlen > 3) { |
| 92 | digit2 = UUDECODE(in[3]);
|
| 93 | if (N64(digit2)) return -1; |
| 94 | out[len++] = (digit1 << 6) | digit2;
|
| 95 | } |
| 96 | } |
| 97 | in += 4;
|
| 98 | } |
| 99 | |
| 100 | return len == outlen ? len : -3; |
| 101 | } |