From ed93571d3fdb248bad114e88e4552f68551438af Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Wed, 2 Aug 2017 14:43:57 +0100 Subject: [PATCH] move the base64 functions to the library --- Makefile.am | 2 +- handler.c | 11 ------ imap/utf7.c | 9 +++-- lib/Makefile.am | 4 +-- lib/lib.h | 2 ++ base64.c => lib/lib_base64.c | 65 +++++++++++++++++++++++------------- lib/lib_base64.h | 33 ++++++++++++++++++ mime.h | 8 +---- po/POTFILES.in | 2 +- protos.h | 4 --- sendlib.c | 2 -- 11 files changed, 88 insertions(+), 54 deletions(-) rename base64.c => lib/lib_base64.c (68%) create mode 100644 lib/lib_base64.h diff --git a/Makefile.am b/Makefile.am index ab4a4d6fb..d9d1e9f67 100644 --- a/Makefile.am +++ b/Makefile.am @@ -45,7 +45,7 @@ BUILT_SOURCES = conststrings.c git_ver.h keymap_alldefs.h keymap_defs.h bin_PROGRAMS = mutt $(PGPAUX_TARGET) mutt_SOURCES = account.c addrbook.c address.h alias.c alias.h attach.c \ - base64.c bcache.c body.h browser.c buffer.c buffy.c charset.c color.c \ + bcache.c body.h browser.c buffer.c buffy.c charset.c color.c \ commands.c complete.c compose.c compress.c content.h context.h copy.c \ curs_lib.c curs_main.c date.c edit.c editmsg.c enter.c enter_state.h \ envelope.h filter.c flags.c format_flags.h from.c getdomain.c group.c \ diff --git a/handler.c b/handler.c index f2b5a26f7..83f662e72 100644 --- a/handler.c +++ b/handler.c @@ -73,17 +73,6 @@ const int Index_hex[128] = { -1,10,11,12, 13,14,15,-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 }; - -const 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, -1,-1,-1,63, - 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 -}; // clang-format on static void print_part_line(struct State *s, struct Body *b, int n) diff --git a/imap/utf7.c b/imap/utf7.c index 1f1d823e4..4be19ce24 100644 --- a/imap/utf7.c +++ b/imap/utf7.c @@ -29,7 +29,12 @@ #include "lib/lib.h" // clang-format off -static const int Index_64[128] = { +/* This is very similar to the table in lib/lib_base64.c + * Encoding chars: + * utf7 A-Za-z0-9+, + * mime A-Za-z0-9+/ + */ +const int Index_64u[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, @@ -81,7 +86,7 @@ static char *utf7_to_utf8(const char *u7, size_t u7len, char **u8, size_t *u8len k = 10; for (; u7len; u7++, u7len--) { - if ((*u7 & 0x80) || (b = Index_64[(int) *u7]) == -1) + if ((*u7 & 0x80) || (b = Index_64u[(int) *u7]) == -1) break; if (k > 0) { diff --git a/lib/Makefile.am b/lib/Makefile.am index cd7eae0b0..890a63c98 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -3,12 +3,12 @@ include $(top_srcdir)/flymake.am AUTOMAKE_OPTIONS = 1.6 foreign -EXTRA_DIST = lib.h lib_ascii.h +EXTRA_DIST = lib.h lib_ascii.h lib_base64.h AM_CPPFLAGS = -I$(top_srcdir) noinst_LIBRARIES = liblib.a noinst_HEADERS = -liblib_a_SOURCES = lib_ascii.c +liblib_a_SOURCES = lib_ascii.c lib_base64.c diff --git a/lib/lib.h b/lib/lib.h index 500bbabea..bfb98ff86 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -29,11 +29,13 @@ * library, but none depends on source from outside. * * -# @subpage ascii + * -# @subpage base64 */ #ifndef _LIB_LIB_H #define _LIB_LIB_H #include "lib_ascii.h" +#include "lib_base64.h" #endif /* _LIB_LIB_H */ diff --git a/base64.c b/lib/lib_base64.c similarity index 68% rename from base64.c rename to lib/lib_base64.c index da8c495af..efd5ba3bb 100644 --- a/base64.c +++ b/lib/lib_base64.c @@ -3,6 +3,10 @@ * Conversion to/from base64 encoding * * @authors + * Copyright (C) 1993,1995 Carl Harris + * Copyright (C) 1997 Eric S. Raymond + * Copyright (C) 1999 Brendan Cully + * * @copyright * 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 @@ -18,28 +22,22 @@ * this program. If not, see . */ -/* - * Base64 handling elsewhere in mutt should be modified to call - * these routines. These routines were written because IMAP's - * AUTHENTICATE protocol required them, and base64 handling - * elsewhere wasn't sufficiently generic. - */ - -/* - * This code is heavily modified from fetchmail (also GPL'd, of - * course) by Brendan Cully . +/** + * @page base64 Conversion to/from base64 encoding + * + * Convert between binary data and base64 text, according to RFC2045. * - * Original copyright notice: + * @note RFC3548 obsoletes RFC2045. + * @note RFC4648 obsoletes RFC3548. * - * The code in the fetchmail distribution is Copyright 1997 by Eric - * S. Raymond. Portions are also copyrighted by Carl Harris, 1993 - * and 1995. Copyright retained for the purpose of protecting free - * redistribution of source. + * | Function | Description + * | :----------------- | :------------------------------------------------- + * | mutt_from_base64() | convert null-terminated base64 string to raw bytes + * | mutt_to_base64() | convert raw bytes to null-terminated base64 string */ #include "config.h" -#include -#include "mime.h" +#include "lib_base64.h" #define BAD -1 @@ -51,6 +49,24 @@ static const char B64Chars[64] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', }; +// clang-format off +/* This is very similar to the table in imap/utf7.c + * Encoding chars: + * utf7 A-Za-z0-9+, + * mime A-Za-z0-9+/ + */ +const 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, -1,-1,-1,63, + 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 +}; +// clang-format on + /** * mutt_to_base64 - convert raw bytes to null-terminated base64 string * @param out Output buffer for the base64 encoded string @@ -68,7 +84,8 @@ size_t mutt_to_base64(char *out, const char *cin, size_t len, size_t olen) { unsigned char *begin = (unsigned char *) out; const unsigned char *in = (const unsigned char *) cin; - while (len >= 3 && olen > 10) + + while ((len >= 3) && (olen > 10)) { *out++ = B64Chars[in[0] >> 2]; *out++ = B64Chars[((in[0] << 4) & 0x30) | (in[1] >> 4)]; @@ -80,7 +97,7 @@ size_t mutt_to_base64(char *out, const char *cin, size_t len, size_t olen) } /* clean up remainder */ - if (len > 0 && olen > 4) + if ((len > 0) && (olen > 4)) { unsigned char fragment; @@ -110,21 +127,21 @@ size_t mutt_to_base64(char *out, const char *cin, size_t len, size_t olen) int mutt_from_base64(char *out, const char *in) { int len = 0; - register unsigned char digit1, digit2, digit3, digit4; + unsigned char digit1, digit2, digit3, digit4; do { digit1 = in[0]; - if (digit1 > 127 || base64val(digit1) == BAD) + if ((digit1 > 127) || (base64val(digit1) == BAD)) return -1; digit2 = in[1]; - if (digit2 > 127 || base64val(digit2) == BAD) + if ((digit2 > 127) || (base64val(digit2) == BAD)) return -1; digit3 = in[2]; - if (digit3 > 127 || ((digit3 != '=') && (base64val(digit3) == BAD))) + if ((digit3 > 127) || ((digit3 != '=') && (base64val(digit3) == BAD))) return -1; digit4 = in[3]; - if (digit4 > 127 || ((digit4 != '=') && (base64val(digit4) == BAD))) + if ((digit4 > 127) || ((digit4 != '=') && (base64val(digit4) == BAD))) return -1; in += 4; diff --git a/lib/lib_base64.h b/lib/lib_base64.h new file mode 100644 index 000000000..116befedf --- /dev/null +++ b/lib/lib_base64.h @@ -0,0 +1,33 @@ +/** + * @file + * Conversion to/from base64 encoding + * + * @authors + * @copyright + * 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, see . + */ + +#ifndef _LIB_BASE64_H +#define _LIB_BASE64_H + +#include + +extern const int Index_64[]; + +#define base64val(c) Index_64[(unsigned int) (c)] + +size_t mutt_to_base64(char *out, const char *cin, size_t len, size_t olen); +int mutt_from_base64(char *out, const char *in); + +#endif /* _LIB_BASE64_H */ diff --git a/mime.h b/mime.h index 0d059f6b2..257757dd2 100644 --- a/mime.h +++ b/mime.h @@ -67,13 +67,9 @@ enum ContentDisposition /* MIME encoding/decoding global vars */ -#ifndef _SENDLIB_C extern const int Index_hex[]; -extern const int Index_64[]; -#endif #define hexval(c) Index_hex[(unsigned int) (c)] -#define base64val(c) Index_64[(unsigned int) (c)] #define is_multipart(x) \ ((x)->type == TYPEMULTIPART || \ @@ -88,8 +84,6 @@ extern const char *BodyEncodings[]; #define ENCODING(X) BodyEncodings[(X)] /* other MIME-related global variables */ -#ifndef _SENDLIB_C -extern char MimeSpecials[]; -#endif +extern const char MimeSpecials[]; #endif /* _MUTT_MIME_H */ diff --git a/po/POTFILES.in b/po/POTFILES.in index bc3056596..928936557 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -2,7 +2,6 @@ account.c addrbook.c alias.c attach.c -base64.c bcache.c browser.c buffer.c @@ -62,6 +61,7 @@ keymap.c keymap_alldefs.h lib.c lib/lib_ascii.c +lib/lib_base64.c main.c mbox.c mbyte.c diff --git a/protos.h b/protos.h index 475b0f7fd..a1056ce7c 100644 --- a/protos.h +++ b/protos.h @@ -390,10 +390,6 @@ uint64_t mutt_rand64(void); struct Address *alias_reverse_lookup(struct Address *a); -/* base64.c */ -size_t mutt_to_base64(char *out, const char *cin, size_t len, size_t olen); -int mutt_from_base64(char *out, const char *in); - /* utf8.c */ int mutt_wctoutf8(char *s, unsigned int c, size_t buflen); diff --git a/sendlib.c b/sendlib.c index 094927966..b96427a44 100644 --- a/sendlib.c +++ b/sendlib.c @@ -20,8 +20,6 @@ * this program. If not, see . */ -#define _SENDLIB_C 1 - #include "config.h" #include #include -- 2.40.0