Commit 134c0cc6 134c0cc6398077a17ff692475fbd6aebfd4e6509 by Wojciech Polak

Move base64 routines to libmailutils.

* mailbox/base64.c: New file.
* imap4d/auth_gss.c: Use mu_base64_encode() and mu_base64_decode().
* imap4d/util.c (util_base64_encode, util_base64_decode): Remove.
1 parent a1b914b0
...@@ -171,7 +171,7 @@ auth_gssapi (struct imap4d_command *command, ...@@ -171,7 +171,7 @@ auth_gssapi (struct imap4d_command *command,
171 for (;;) 171 for (;;)
172 { 172 {
173 imap4d_getline (&token_str, &token_size, &token_len); 173 imap4d_getline (&token_str, &token_size, &token_len);
174 util_base64_decode (token_str, token_len, &tmp, &size); 174 mu_base64_decode (token_str, token_len, &tmp, &size);
175 tokbuf.value = tmp; 175 tokbuf.value = tmp;
176 tokbuf.length = size; 176 tokbuf.length = size;
177 177
...@@ -189,7 +189,7 @@ auth_gssapi (struct imap4d_command *command, ...@@ -189,7 +189,7 @@ auth_gssapi (struct imap4d_command *command,
189 { 189 {
190 if (outbuf.length) 190 if (outbuf.length)
191 { 191 {
192 util_base64_encode (outbuf.value, outbuf.length, &tmp, &size); 192 mu_base64_encode (outbuf.value, outbuf.length, &tmp, &size);
193 util_send ("+ %*.*s\r\n", size, size, tmp); 193 util_send ("+ %*.*s\r\n", size, size, tmp);
194 free (tmp); 194 free (tmp);
195 gss_release_buffer (&min_stat, &outbuf); 195 gss_release_buffer (&min_stat, &outbuf);
...@@ -209,7 +209,7 @@ auth_gssapi (struct imap4d_command *command, ...@@ -209,7 +209,7 @@ auth_gssapi (struct imap4d_command *command,
209 209
210 if (outbuf.length) 210 if (outbuf.length)
211 { 211 {
212 util_base64_encode (outbuf.value, outbuf.length, &tmp, &size); 212 mu_base64_encode (outbuf.value, outbuf.length, &tmp, &size);
213 util_send ("+ %*.*s\r\n", size, size, tmp); 213 util_send ("+ %*.*s\r\n", size, size, tmp);
214 free (tmp); 214 free (tmp);
215 gss_release_buffer (&min_stat, &outbuf); 215 gss_release_buffer (&min_stat, &outbuf);
...@@ -229,12 +229,12 @@ auth_gssapi (struct imap4d_command *command, ...@@ -229,12 +229,12 @@ auth_gssapi (struct imap4d_command *command,
229 return RESP_NO; 229 return RESP_NO;
230 } 230 }
231 231
232 util_base64_encode (outbuf.value, outbuf.length, &tmp, &size); 232 mu_base64_encode (outbuf.value, outbuf.length, &tmp, &size);
233 util_send ("+ %*.*s\r\n", size, size, tmp); 233 util_send ("+ %*.*s\r\n", size, size, tmp);
234 free (tmp); 234 free (tmp);
235 235
236 imap4d_getline (&token_str, &token_size, &token_len); 236 imap4d_getline (&token_str, &token_size, &token_len);
237 util_base64_decode (token_str, token_len, 237 mu_base64_decode (token_str, token_len,
238 (unsigned char **) &tokbuf.value, &tokbuf.length); 238 (unsigned char **) &tokbuf.value, &tokbuf.length);
239 free (token_str); 239 free (token_str);
240 240
......
...@@ -87,6 +87,7 @@ ...@@ -87,6 +87,7 @@
87 #include <mailutils/list.h> 87 #include <mailutils/list.h>
88 #include <mailutils/mailbox.h> 88 #include <mailutils/mailbox.h>
89 #include <mailutils/message.h> 89 #include <mailutils/message.h>
90 #include <mailutils/mime.h>
90 #include <mailutils/mutil.h> 91 #include <mailutils/mutil.h>
91 #include <mailutils/mu_auth.h> 92 #include <mailutils/mu_auth.h>
92 #include <mailutils/nls.h> 93 #include <mailutils/nls.h>
...@@ -347,15 +348,6 @@ extern int util_parse_internal_date (char *date, time_t *timep); ...@@ -347,15 +348,6 @@ extern int util_parse_internal_date (char *date, time_t *timep);
347 extern int util_parse_822_date (const char *date, time_t *timep); 348 extern int util_parse_822_date (const char *date, time_t *timep);
348 extern int util_parse_ctime_date (const char *date, time_t *timep); 349 extern int util_parse_ctime_date (const char *date, time_t *timep);
349 extern char *util_strcasestr (const char *haystack, const char *needle); 350 extern char *util_strcasestr (const char *haystack, const char *needle);
350
351 extern int util_base64_encode (const unsigned char *input,
352 size_t input_len,
353 unsigned char **output,
354 size_t *output_len);
355 extern int util_base64_decode (const unsigned char *input,
356 size_t input_len,
357 unsigned char **output,
358 size_t *output_len);
359 extern char *util_localname (void); 351 extern char *util_localname (void);
360 352
361 extern int util_wcard_match (const char *string, const char *pattern, 353 extern int util_wcard_match (const char *string, const char *pattern,
......
...@@ -673,86 +673,6 @@ util_attribute_matches_flag (mu_attribute_t attr, const char *item) ...@@ -673,86 +673,6 @@ util_attribute_matches_flag (mu_attribute_t attr, const char *item)
673 return flags & mask; 673 return flags & mask;
674 } 674 }
675 675
676 int
677 util_base64_encode (const unsigned char *input, size_t input_len,
678 unsigned char **output, size_t * output_len)
679 {
680 static char b64tab[] =
681 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
682 size_t olen = 4 * (input_len + 2) / 3;
683 unsigned char *out = malloc (olen);
684
685 if (!out)
686 return 1;
687 *output = out;
688 while (input_len >= 3)
689 {
690 *out++ = b64tab[input[0] >> 2];
691 *out++ = b64tab[((input[0] << 4) & 0x30) | (input[1] >> 4)];
692 *out++ = b64tab[((input[1] << 2) & 0x3c) | (input[2] >> 6)];
693 *out++ = b64tab[input[2] & 0x3f];
694 olen -= 4;
695 input_len -= 3;
696 input += 3;
697 }
698
699 if (input_len > 0)
700 {
701 unsigned char c = (input[0] << 4) & 0x30;
702 *out++ = b64tab[input[0] >> 2];
703 if (input_len > 1)
704 c |= input[1] >> 4;
705 *out++ = b64tab[c];
706 *out++ = (input_len < 2) ? '=' : b64tab[(input[1] << 2) & 0x3c];
707 *out++ = '=';
708 }
709 *output_len = out - *output;
710 return 0;
711 }
712
713 int
714 util_base64_decode (const unsigned char *input, size_t input_len,
715 unsigned char **output, size_t * output_len)
716 {
717 static int b64val[128] = {
718 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
719 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
720 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
721 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
722 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
723 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
724 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
725 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
726 };
727 int olen = input_len;
728 unsigned char *out = malloc (olen);
729
730 if (!out)
731 return 1;
732 *output = out;
733 do
734 {
735 if (input[0] > 127 || b64val[input[0]] == -1
736 || input[1] > 127 || b64val[input[1]] == -1
737 || input[2] > 127 || ((input[2] != '=') && (b64val[input[2]] == -1))
738 || input[3] > 127 || ((input[3] != '=')
739 && (b64val[input[3]] == -1)))
740 return -1;
741 *out++ = (b64val[input[0]] << 2) | (b64val[input[1]] >> 4);
742 if (input[2] != '=')
743 {
744 *out++ = ((b64val[input[1]] << 4) & 0xf0) | (b64val[input[2]] >> 2);
745 if (input[3] != '=')
746 *out++ = ((b64val[input[2]] << 6) & 0xc0) | b64val[input[3]];
747 }
748 input += 4;
749 input_len -= 4;
750 }
751 while (input_len > 0);
752 *output_len = out - *output;
753 return 0;
754 }
755
756 char * 676 char *
757 util_localname () 677 util_localname ()
758 { 678 {
......
...@@ -47,6 +47,12 @@ int mu_rfc2047_decode (const char *tocode, const char *fromstr, ...@@ -47,6 +47,12 @@ int mu_rfc2047_decode (const char *tocode, const char *fromstr,
47 int mu_rfc2047_encode (const char *charset, const char *encoding, 47 int mu_rfc2047_encode (const char *charset, const char *encoding,
48 const char *text, char **result); 48 const char *text, char **result);
49 49
50 int mu_base64_encode (const unsigned char *input, size_t input_len,
51 unsigned char **output, size_t * output_len);
52
53 int mu_base64_decode (const unsigned char *input, size_t input_len,
54 unsigned char **output, size_t * output_len);
55
50 #ifdef __cplusplus 56 #ifdef __cplusplus
51 } 57 }
52 #endif 58 #endif
......
...@@ -55,6 +55,7 @@ libmailutils_la_SOURCES = \ ...@@ -55,6 +55,7 @@ libmailutils_la_SOURCES = \
55 attachment.c\ 55 attachment.c\
56 attribute.c\ 56 attribute.c\
57 auth.c\ 57 auth.c\
58 base64.c\
58 body.c\ 59 body.c\
59 cstrcasecmp.c\ 60 cstrcasecmp.c\
60 cfg_driver.c\ 61 cfg_driver.c\
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2002, 2009 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library; if not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301 USA */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <mailutils/errno.h>
26
27 int
28 mu_base64_encode (const unsigned char *input, size_t input_len,
29 unsigned char **output, size_t * output_len)
30 {
31 static char b64tab[] =
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 size_t olen = 4 * (input_len + 2) / 3;
34 unsigned char *out = malloc (olen);
35
36 if (!out)
37 return ENOMEM;
38 *output = out;
39 while (input_len >= 3)
40 {
41 *out++ = b64tab[input[0] >> 2];
42 *out++ = b64tab[((input[0] << 4) & 0x30) | (input[1] >> 4)];
43 *out++ = b64tab[((input[1] << 2) & 0x3c) | (input[2] >> 6)];
44 *out++ = b64tab[input[2] & 0x3f];
45 olen -= 4;
46 input_len -= 3;
47 input += 3;
48 }
49
50 if (input_len > 0)
51 {
52 unsigned char c = (input[0] << 4) & 0x30;
53 *out++ = b64tab[input[0] >> 2];
54 if (input_len > 1)
55 c |= input[1] >> 4;
56 *out++ = b64tab[c];
57 *out++ = (input_len < 2) ? '=' : b64tab[(input[1] << 2) & 0x3c];
58 *out++ = '=';
59 }
60 *output_len = out - *output;
61 return 0;
62 }
63
64 int
65 mu_base64_decode (const unsigned char *input, size_t input_len,
66 unsigned char **output, size_t * output_len)
67 {
68 static int b64val[128] = {
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
72 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
73 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
74 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
75 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
76 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
77 };
78 int olen = input_len;
79 unsigned char *out = malloc (olen);
80
81 if (!out)
82 return ENOMEM;
83 *output = out;
84 do
85 {
86 if (input[0] > 127 || b64val[input[0]] == -1
87 || input[1] > 127 || b64val[input[1]] == -1
88 || input[2] > 127 || ((input[2] != '=') && (b64val[input[2]] == -1))
89 || input[3] > 127 || ((input[3] != '=')
90 && (b64val[input[3]] == -1)))
91 return -1;
92 *out++ = (b64val[input[0]] << 2) | (b64val[input[1]] >> 4);
93 if (input[2] != '=')
94 {
95 *out++ = ((b64val[input[1]] << 4) & 0xf0) | (b64val[input[2]] >> 2);
96 if (input[3] != '=')
97 *out++ = ((b64val[input[2]] << 6) & 0xc0) | b64val[input[3]];
98 }
99 input += 4;
100 input_len -= 4;
101 }
102 while (input_len > 0);
103 *output_len = out - *output;
104 return 0;
105 }
106