3
3
* Conversion to/from base64 encoding
4
4
*
5
5
* @authors
6
+ * Copyright (C) 1993,1995 Carl Harris
7
+ * Copyright (C) 1997 Eric S. Raymond
8
+ * Copyright (C) 1999 Brendan Cully <[email protected] >
9
+ *
6
10
* @copyright
7
11
* This program is free software: you can redistribute it and/or modify it under
8
12
* the terms of the GNU General Public License as published by the Free Software
18
22
* this program. If not, see <http://www.gnu.org/licenses/>.
19
23
*/
20
24
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.
31
29
*
32
- * Original copyright notice:
30
+ * @note RFC3548 obsoletes RFC2045.
31
+ * @note RFC4648 obsoletes RFC3548.
33
32
*
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
38
37
*/
39
38
40
39
#include "config.h"
41
- #include <stddef.h>
42
- #include "mime.h"
40
+ #include "lib_base64.h"
43
41
44
42
#define BAD -1
45
43
@@ -51,6 +49,24 @@ static const char B64Chars[64] = {
51
49
'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/' ,
52
50
};
53
51
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
+
54
70
/**
55
71
* mutt_to_base64 - convert raw bytes to null-terminated base64 string
56
72
* @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)
68
84
{
69
85
unsigned char * begin = (unsigned char * ) out ;
70
86
const unsigned char * in = (const unsigned char * ) cin ;
71
- while (len >= 3 && olen > 10 )
87
+
88
+ while ((len >= 3 ) && (olen > 10 ))
72
89
{
73
90
* out ++ = B64Chars [in [0 ] >> 2 ];
74
91
* 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)
80
97
}
81
98
82
99
/* clean up remainder */
83
- if (len > 0 && olen > 4 )
100
+ if (( len > 0 ) && ( olen > 4 ) )
84
101
{
85
102
unsigned char fragment ;
86
103
@@ -110,21 +127,21 @@ size_t mutt_to_base64(char *out, const char *cin, size_t len, size_t olen)
110
127
int mutt_from_base64 (char * out , const char * in )
111
128
{
112
129
int len = 0 ;
113
- register unsigned char digit1 , digit2 , digit3 , digit4 ;
130
+ unsigned char digit1 , digit2 , digit3 , digit4 ;
114
131
115
132
do
116
133
{
117
134
digit1 = in [0 ];
118
- if (digit1 > 127 || base64val (digit1 ) == BAD )
135
+ if (( digit1 > 127 ) || ( base64val (digit1 ) == BAD ) )
119
136
return -1 ;
120
137
digit2 = in [1 ];
121
- if (digit2 > 127 || base64val (digit2 ) == BAD )
138
+ if (( digit2 > 127 ) || ( base64val (digit2 ) == BAD ) )
122
139
return -1 ;
123
140
digit3 = in [2 ];
124
- if (digit3 > 127 || ((digit3 != '=' ) && (base64val (digit3 ) == BAD )))
141
+ if (( digit3 > 127 ) || ((digit3 != '=' ) && (base64val (digit3 ) == BAD )))
125
142
return -1 ;
126
143
digit4 = in [3 ];
127
- if (digit4 > 127 || ((digit4 != '=' ) && (base64val (digit4 ) == BAD )))
144
+ if (( digit4 > 127 ) || ((digit4 != '=' ) && (base64val (digit4 ) == BAD )))
128
145
return -1 ;
129
146
in += 4 ;
130
147
0 commit comments