Skip to content

Commit ed93571

Browse files
committed
move the base64 functions to the library
1 parent d85c3bb commit ed93571

11 files changed

+88
-54
lines changed

Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ BUILT_SOURCES = conststrings.c git_ver.h keymap_alldefs.h keymap_defs.h
4545
bin_PROGRAMS = mutt $(PGPAUX_TARGET)
4646

4747
mutt_SOURCES = account.c addrbook.c address.h alias.c alias.h attach.c \
48-
base64.c bcache.c body.h browser.c buffer.c buffy.c charset.c color.c \
48+
bcache.c body.h browser.c buffer.c buffy.c charset.c color.c \
4949
commands.c complete.c compose.c compress.c content.h context.h copy.c \
5050
curs_lib.c curs_main.c date.c edit.c editmsg.c enter.c enter_state.h \
5151
envelope.h filter.c flags.c format_flags.h from.c getdomain.c group.c \

handler.c

-11
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,6 @@ const int Index_hex[128] = {
7373
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
7474
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
7575
};
76-
77-
const int Index_64[128] = {
78-
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
79-
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
80-
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
81-
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
82-
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
83-
15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
84-
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
85-
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
86-
};
8776
// clang-format on
8877

8978
static void print_part_line(struct State *s, struct Body *b, int n)

imap/utf7.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
#include "lib/lib.h"
3030

3131
// clang-format off
32-
static const int Index_64[128] = {
32+
/* This is very similar to the table in lib/lib_base64.c
33+
* Encoding chars:
34+
* utf7 A-Za-z0-9+,
35+
* mime A-Za-z0-9+/
36+
*/
37+
const int Index_64u[128] = {
3338
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
3439
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
3540
-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
8186
k = 10;
8287
for (; u7len; u7++, u7len--)
8388
{
84-
if ((*u7 & 0x80) || (b = Index_64[(int) *u7]) == -1)
89+
if ((*u7 & 0x80) || (b = Index_64u[(int) *u7]) == -1)
8590
break;
8691
if (k > 0)
8792
{

lib/Makefile.am

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ include $(top_srcdir)/flymake.am
33

44
AUTOMAKE_OPTIONS = 1.6 foreign
55

6-
EXTRA_DIST = lib.h lib_ascii.h
6+
EXTRA_DIST = lib.h lib_ascii.h lib_base64.h
77

88
AM_CPPFLAGS = -I$(top_srcdir)
99

1010
noinst_LIBRARIES = liblib.a
1111
noinst_HEADERS =
1212

13-
liblib_a_SOURCES = lib_ascii.c
13+
liblib_a_SOURCES = lib_ascii.c lib_base64.c
1414

lib/lib.h

+2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
* library, but none depends on source from outside.
3030
*
3131
* -# @subpage ascii
32+
* -# @subpage base64
3233
*/
3334

3435
#ifndef _LIB_LIB_H
3536
#define _LIB_LIB_H
3637

3738
#include "lib_ascii.h"
39+
#include "lib_base64.h"
3840

3941
#endif /* _LIB_LIB_H */

base64.c lib/lib_base64.c

+41-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
* Conversion to/from base64 encoding
44
*
55
* @authors
6+
* Copyright (C) 1993,1995 Carl Harris
7+
* Copyright (C) 1997 Eric S. Raymond
8+
* Copyright (C) 1999 Brendan Cully <[email protected]>
9+
*
610
* @copyright
711
* This program is free software: you can redistribute it and/or modify it under
812
* the terms of the GNU General Public License as published by the Free Software
@@ -18,28 +22,22 @@
1822
* this program. If not, see <http://www.gnu.org/licenses/>.
1923
*/
2024

21-
/*
22-
* Base64 handling elsewhere in mutt should be modified to call
23-
* these routines. These routines were written because IMAP's
24-
* AUTHENTICATE protocol required them, and base64 handling
25-
* elsewhere wasn't sufficiently generic.
26-
*/
27-
28-
/*
29-
* This code is heavily modified from fetchmail (also GPL'd, of
30-
* course) by Brendan Cully <[email protected]>.
25+
/**
26+
* @page base64 Conversion to/from base64 encoding
27+
*
28+
* Convert between binary data and base64 text, according to RFC2045.
3129
*
32-
* Original copyright notice:
30+
* @note RFC3548 obsoletes RFC2045.
31+
* @note RFC4648 obsoletes RFC3548.
3332
*
34-
* The code in the fetchmail distribution is Copyright 1997 by Eric
35-
* S. Raymond. Portions are also copyrighted by Carl Harris, 1993
36-
* and 1995. Copyright retained for the purpose of protecting free
37-
* redistribution of source.
33+
* | Function | Description
34+
* | :----------------- | :-------------------------------------------------
35+
* | mutt_from_base64() | convert null-terminated base64 string to raw bytes
36+
* | mutt_to_base64() | convert raw bytes to null-terminated base64 string
3837
*/
3938

4039
#include "config.h"
41-
#include <stddef.h>
42-
#include "mime.h"
40+
#include "lib_base64.h"
4341

4442
#define BAD -1
4543

@@ -51,6 +49,24 @@ static const char B64Chars[64] = {
5149
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
5250
};
5351

52+
// clang-format off
53+
/* This is very similar to the table in imap/utf7.c
54+
* Encoding chars:
55+
* utf7 A-Za-z0-9+,
56+
* mime A-Za-z0-9+/
57+
*/
58+
const int Index_64[128] = {
59+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
60+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
61+
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
62+
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
63+
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
64+
15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
65+
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
66+
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
67+
};
68+
// clang-format on
69+
5470
/**
5571
* mutt_to_base64 - convert raw bytes to null-terminated base64 string
5672
* @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)
6884
{
6985
unsigned char *begin = (unsigned char *) out;
7086
const unsigned char *in = (const unsigned char *) cin;
71-
while (len >= 3 && olen > 10)
87+
88+
while ((len >= 3) && (olen > 10))
7289
{
7390
*out++ = B64Chars[in[0] >> 2];
7491
*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)
8097
}
8198

8299
/* clean up remainder */
83-
if (len > 0 && olen > 4)
100+
if ((len > 0) && (olen > 4))
84101
{
85102
unsigned char fragment;
86103

@@ -110,21 +127,21 @@ size_t mutt_to_base64(char *out, const char *cin, size_t len, size_t olen)
110127
int mutt_from_base64(char *out, const char *in)
111128
{
112129
int len = 0;
113-
register unsigned char digit1, digit2, digit3, digit4;
130+
unsigned char digit1, digit2, digit3, digit4;
114131

115132
do
116133
{
117134
digit1 = in[0];
118-
if (digit1 > 127 || base64val(digit1) == BAD)
135+
if ((digit1 > 127) || (base64val(digit1) == BAD))
119136
return -1;
120137
digit2 = in[1];
121-
if (digit2 > 127 || base64val(digit2) == BAD)
138+
if ((digit2 > 127) || (base64val(digit2) == BAD))
122139
return -1;
123140
digit3 = in[2];
124-
if (digit3 > 127 || ((digit3 != '=') && (base64val(digit3) == BAD)))
141+
if ((digit3 > 127) || ((digit3 != '=') && (base64val(digit3) == BAD)))
125142
return -1;
126143
digit4 = in[3];
127-
if (digit4 > 127 || ((digit4 != '=') && (base64val(digit4) == BAD)))
144+
if ((digit4 > 127) || ((digit4 != '=') && (base64val(digit4) == BAD)))
128145
return -1;
129146
in += 4;
130147

lib/lib_base64.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @file
3+
* Conversion to/from base64 encoding
4+
*
5+
* @authors
6+
* @copyright
7+
* This program is free software: you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License as published by the Free Software
9+
* Foundation, either version 2 of the License, or (at your option) any later
10+
* version.
11+
*
12+
* This program is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15+
* details.
16+
*
17+
* You should have received a copy of the GNU General Public License along with
18+
* this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef _LIB_BASE64_H
22+
#define _LIB_BASE64_H
23+
24+
#include <stdio.h>
25+
26+
extern const int Index_64[];
27+
28+
#define base64val(c) Index_64[(unsigned int) (c)]
29+
30+
size_t mutt_to_base64(char *out, const char *cin, size_t len, size_t olen);
31+
int mutt_from_base64(char *out, const char *in);
32+
33+
#endif /* _LIB_BASE64_H */

mime.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,9 @@ enum ContentDisposition
6767

6868
/* MIME encoding/decoding global vars */
6969

70-
#ifndef _SENDLIB_C
7170
extern const int Index_hex[];
72-
extern const int Index_64[];
73-
#endif
7471

7572
#define hexval(c) Index_hex[(unsigned int) (c)]
76-
#define base64val(c) Index_64[(unsigned int) (c)]
7773

7874
#define is_multipart(x) \
7975
((x)->type == TYPEMULTIPART || \
@@ -88,8 +84,6 @@ extern const char *BodyEncodings[];
8884
#define ENCODING(X) BodyEncodings[(X)]
8985

9086
/* other MIME-related global variables */
91-
#ifndef _SENDLIB_C
92-
extern char MimeSpecials[];
93-
#endif
87+
extern const char MimeSpecials[];
9488

9589
#endif /* _MUTT_MIME_H */

po/POTFILES.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ account.c
22
addrbook.c
33
alias.c
44
attach.c
5-
base64.c
65
bcache.c
76
browser.c
87
buffer.c
@@ -62,6 +61,7 @@ keymap.c
6261
keymap_alldefs.h
6362
lib.c
6463
lib/lib_ascii.c
64+
lib/lib_base64.c
6565
main.c
6666
mbox.c
6767
mbyte.c

protos.h

-4
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,6 @@ uint64_t mutt_rand64(void);
390390

391391
struct Address *alias_reverse_lookup(struct Address *a);
392392

393-
/* base64.c */
394-
size_t mutt_to_base64(char *out, const char *cin, size_t len, size_t olen);
395-
int mutt_from_base64(char *out, const char *in);
396-
397393
/* utf8.c */
398394
int mutt_wctoutf8(char *s, unsigned int c, size_t buflen);
399395

sendlib.c

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
* this program. If not, see <http://www.gnu.org/licenses/>.
2121
*/
2222

23-
#define _SENDLIB_C 1
24-
2523
#include "config.h"
2624
#include <stddef.h>
2725
#include <ctype.h>

0 commit comments

Comments
 (0)