X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=imap%2Futf7.c;h=b6a838dbe5b79915d7eb6d0d37656d30b0487f71;hp=b6781a8248d86a3797cda65fe4626f60a0bcc9b0;hb=39d11f5b3feb125654d5c2a4e8f93637a12706c3;hpb=6833ce8bdca2d64e14485118f2a4417b7e1cb1b1 diff --git a/imap/utf7.c b/imap/utf7.c index b6781a8..b6a838d 100644 --- a/imap/utf7.c +++ b/imap/utf7.c @@ -1,33 +1,29 @@ /* + * Copyright notice from original mutt: * Copyright (C) 2000 Edmund Grimley Evans - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. - */ + * + * This file is part of mutt-ng, see http://www.muttng.org/. + * It's licensed under the GNU General Public License, + * please see the file GPL in the top level source directory. + */ + +#if HAVE_CONFIG_H +# include "config.h" +#endif #include "mutt.h" #include "charset.h" +#include "imap_private.h" static int Index_64[128] = { - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, - -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, 63,-1,-1,-1, - 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, - 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, - -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, - 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, 63, -1, -1, -1, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 }; static char B64Chars[64] = { @@ -49,70 +45,61 @@ static char B64Chars[64] = { * of &AMAAwA-). */ static char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8, - size_t *u8len) + size_t * u8len) { char *buf, *p; int b, ch, k; p = buf = safe_malloc (u7len + u7len / 8 + 1); - for (; u7len; u7++, u7len--) - { - if (*u7 == '&') - { + for (; u7len; u7++, u7len--) { + if (*u7 == '&') { u7++, u7len--; - if (u7len && *u7 == '-') - { - *p++ = '&'; - continue; + if (u7len && *u7 == '-') { + *p++ = '&'; + continue; } ch = 0; k = 10; - for (; u7len; u7++, u7len--) - { - if ((*u7 & 0x80) || (b = Index_64[(int)*u7]) == -1) - break; - if (k > 0) - { - ch |= b << k; - k -= 6; - } - else - { - ch |= b >> (-k); - if (ch < 0x80) - { - if (0x20 <= ch && ch < 0x7f) - /* Printable US-ASCII */ - goto bail; - *p++ = ch; - } - else if (ch < 0x800) - { - *p++ = 0xc0 | (ch >> 6); - *p++ = 0x80 | (ch & 0x3f); - } - else - { - *p++ = 0xe0 | (ch >> 12); - *p++ = 0x80 | ((ch >> 6) & 0x3f); - *p++ = 0x80 | (ch & 0x3f); - } - ch = (b << (16 + k)) & 0xffff; - k += 10; - } + for (; u7len; u7++, u7len--) { + if ((*u7 & 0x80) || (b = Index_64[(int) *u7]) == -1) + break; + if (k > 0) { + ch |= b << k; + k -= 6; + } + else { + ch |= b >> (-k); + if (ch < 0x80) { + if (0x20 <= ch && ch < 0x7f) + /* Printable US-ASCII */ + goto bail; + *p++ = ch; + } + else if (ch < 0x800) { + *p++ = 0xc0 | (ch >> 6); + *p++ = 0x80 | (ch & 0x3f); + } + else { + *p++ = 0xe0 | (ch >> 12); + *p++ = 0x80 | ((ch >> 6) & 0x3f); + *p++ = 0x80 | (ch & 0x3f); + } + ch = (b << (16 + k)) & 0xffff; + k += 10; + } } if (ch || k < 6) - /* Non-zero or too many extra bits */ - goto bail; + /* Non-zero or too many extra bits */ + goto bail; if (!u7len || *u7 != '-') - /* BASE64 not properly terminated */ - goto bail; + /* BASE64 not properly terminated */ + goto bail; if (u7len > 2 && u7[1] == '&' && u7[2] != '-') - /* Adjacent BASE64 sections */ - goto bail; + /* Adjacent BASE64 sections */ + goto bail; } else if (*u7 < 0x20 || *u7 >= 0x7f) /* Not printable US-ASCII */ @@ -129,7 +116,7 @@ static char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8, *u8 = buf; return buf; - bail: +bail: FREE (&buf); return 0; } @@ -142,7 +129,7 @@ static char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8, * If input data is invalid, return 0 and don't store anything. */ static char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7, - size_t *u7len) + size_t * u7len) { char *buf, *p; int ch; @@ -155,8 +142,7 @@ static char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7, */ p = buf = safe_malloc ((u8len / 2) * 7 + 6); - while (u8len) - { + while (u8len) { unsigned char c = *u8; if (c < 0x80) @@ -179,57 +165,50 @@ static char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7, u8++, u8len--; if (n > u8len) goto bail; - for (i = 0; i < n; i++) - { + for (i = 0; i < n; i++) { if ((u8[i] & 0xc0) != 0x80) - goto bail; + goto bail; ch = (ch << 6) | (u8[i] & 0x3f); } if (n > 1 && !(ch >> (n * 5 + 1))) goto bail; u8 += n, u8len -= n; - if (ch < 0x20 || ch >= 0x7f) - { - if (!base64) - { - *p++ = '&'; - base64 = 1; - b = 0; - k = 10; + if (ch < 0x20 || ch >= 0x7f) { + if (!base64) { + *p++ = '&'; + base64 = 1; + b = 0; + k = 10; } if (ch & ~0xffff) - ch = 0xfffe; + ch = 0xfffe; *p++ = B64Chars[b | ch >> k]; k -= 6; for (; k >= 0; k -= 6) - *p++ = B64Chars[(ch >> k) & 0x3f]; + *p++ = B64Chars[(ch >> k) & 0x3f]; b = (ch << (-k)) & 0x3f; k += 16; } - else - { - if (base64) - { - if (k > 10) - *p++ = B64Chars[b]; - *p++ = '-'; - base64 = 0; + else { + if (base64) { + if (k > 10) + *p++ = B64Chars[b]; + *p++ = '-'; + base64 = 0; } *p++ = ch; if (ch == '&') - *p++ = '-'; + *p++ = '-'; } } - if (u8len) - { + if (u8len) { FREE (&buf); return 0; } - if (base64) - { + if (base64) { if (k > 10) *p++ = B64Chars[b]; *p++ = '-'; @@ -239,32 +218,32 @@ static char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7, if (u7len) *u7len = p - buf; safe_realloc (&buf, p - buf); - if (u7) *u7 = buf; + if (u7) + *u7 = buf; return buf; - bail: +bail: FREE (&buf); return 0; } void imap_utf7_encode (char **s) { - if (Charset) - { + if (Charset) { char *t = safe_strdup (*s); + if (!mutt_convert_string (&t, Charset, "UTF-8", 0)) - utf8_to_utf7 (t, strlen (t), s, 0); + utf8_to_utf7 (t, mutt_strlen (t), s, 0); FREE (&t); } } void imap_utf7_decode (char **s) { - if (Charset) - { - char *t = utf7_to_utf8 (*s, strlen (*s), 0, 0); - if (t && !mutt_convert_string (&t, "UTF-8", Charset, 0)) - { + if (Charset) { + char *t = utf7_to_utf8 (*s, mutt_strlen (*s), 0, 0); + + if (t && !mutt_convert_string (&t, "UTF-8", Charset, 0)) { FREE (s); *s = t; }