1 ;;; mel.el : a MIME encoding/decoding library
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; modified by Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
8 ;; Keywords: MIME, Base64, Quoted-Printable, uuencode, gzip64
10 ;; This file is part of MEL (MIME Encoding Library).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
35 (defvar base64-dl-module
36 (and (fboundp 'dynamic-link)
37 (let ((path (expand-file-name "base64.so" exec-directory)))
38 (and (file-exists-p path)
45 (cond (base64-dl-module
46 (autoload 'base64-encode-string "mel-dl"
47 "Encode STRING to base64, and return the result.")
48 (autoload 'base64-decode-string "mel-dl"
49 "Decode STRING which is encoded in base64, and return the result.")
50 (autoload 'base64-encode-region "mel-dl"
51 "Encode current region by base64." t)
52 (autoload 'base64-decode-region "mel-dl"
53 "Decode current region by base64." t)
54 (autoload 'base64-insert-encoded-file "mel-dl"
55 "Encode contents of file to base64, and insert the result." t)
56 (autoload 'base64-write-decoded-region "mel-dl"
57 "Decode and write current region encoded by base64 into FILENAME." t)
59 (autoload 'base64-encoded-length "mel-dl")
62 (autoload 'base64-encode-string "mel-b"
63 "Encode STRING to base64, and return the result.")
64 (autoload 'base64-decode-string "mel-b"
65 "Decode STRING which is encoded in base64, and return the result.")
66 (autoload 'base64-encode-region "mel-b"
67 "Encode current region by base64." t)
68 (autoload 'base64-decode-region "mel-b"
69 "Decode current region by base64." t)
70 (autoload 'base64-insert-encoded-file "mel-b"
71 "Encode contents of file to base64, and insert the result." t)
72 (autoload 'base64-write-decoded-region "mel-b"
73 "Decode and write current region encoded by base64 into FILENAME." t)
75 (autoload 'base64-encoded-length "mel-b")
78 (autoload 'quoted-printable-encode-string "mel-q"
79 "Encode STRING to quoted-printable, and return the result.")
80 (autoload 'quoted-printable-decode-string "mel-q"
81 "Decode STRING which is encoded in quoted-printable, and return the result.")
82 (autoload 'quoted-printable-encode-region "mel-q"
83 "Encode current region by Quoted-Printable." t)
84 (autoload 'quoted-printable-decode-region "mel-q"
85 "Decode current region by Quoted-Printable." t)
86 (autoload 'quoted-printable-insert-encoded-file "mel-q"
87 "Encode contents of file to quoted-printable, and insert the result." t)
88 (autoload 'quoted-printable-write-decoded-region "mel-q"
89 "Decode and write current region encoded by quoted-printable into FILENAME."
92 (autoload 'q-encoding-encode-string "mel-q"
93 "Encode STRING to Q-encoding of encoded-word, and return the result.")
94 (autoload 'q-encoding-decode-string "mel-q"
95 "Decode STRING which is encoded in Q-encoding and return the result.")
96 (autoload 'q-encoding-encoded-length "mel-q")
98 (autoload 'uuencode-encode-region "mel-u"
99 "Encode current region by unofficial uuencode format." t)
100 (autoload 'uuencode-decode-region "mel-u"
101 "Decode current region by unofficial uuencode format." t)
102 (autoload 'uuencode-insert-encoded-file "mel-u"
103 "Insert file encoded by unofficial uuencode format." t)
104 (autoload 'uuencode-write-decoded-region "mel-u"
105 "Decode and write current region encoded by uuencode into FILENAME." t)
107 (autoload 'gzip64-encode-region "mel-g"
108 "Encode current region by unofficial x-gzip64 format." t)
109 (autoload 'gzip64-decode-region "mel-g"
110 "Decode current region by unofficial x-gzip64 format." t)
111 (autoload 'gzip64-insert-encoded-file "mel-g"
112 "Insert file encoded by unofficial gzip64 format." t)
113 (autoload 'gzip64-write-decoded-region "mel-g"
114 "Decode and write current region encoded by gzip64 into FILENAME." t)
121 (defvar mime-encoding-method-alist
122 '(("base64" . base64-encode-region)
123 ("quoted-printable" . quoted-printable-encode-region)
124 ;; Not standard, their use is DISCOURAGED.
125 ;; ("x-uue" . uuencode-encode-region)
126 ;; ("x-gzip64" . gzip64-encode-region)
131 "Alist of encoding vs. corresponding method to encode region.
132 Each element looks like (STRING . FUNCTION) or (STRING . nil).
133 STRING is content-transfer-encoding.
134 FUNCTION is region encoder and nil means not to encode.")
137 (defvar mime-decoding-method-alist
138 '(("base64" . base64-decode-region)
139 ("quoted-printable" . quoted-printable-decode-region)
140 ("x-uue" . uuencode-decode-region)
141 ("x-uuencode" . uuencode-decode-region)
142 ("x-gzip64" . gzip64-decode-region)
144 "Alist of encoding vs. corresponding method to decode region.
145 Each element looks like (STRING . FUNCTION).
146 STRING is content-transfer-encoding.
147 FUNCTION is region decoder.")
150 (defun mime-encode-region (start end encoding)
151 "Encode region START to END of current buffer using ENCODING.
152 ENCODING must be string. If ENCODING is found in
153 `mime-encoding-method-alist' as its key, this function encodes the
154 region by its value."
156 (list (region-beginning) (region-end)
157 (completing-read "encoding: "
158 mime-encoding-method-alist
161 (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
163 (funcall f start end)
167 (defun mime-decode-region (start end encoding)
168 "Decode region START to END of current buffer using ENCODING.
169 ENCODING must be string. If ENCODING is found in
170 `mime-decoding-method-alist' as its key, this function decodes the
171 region by its value."
173 (list (region-beginning) (region-end)
174 (completing-read "encoding: "
175 mime-decoding-method-alist
178 (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
180 (funcall f start end)
188 (defvar mime-string-decoding-method-alist
189 '(("base64" . base64-decode-string)
190 ("quoted-printable" . quoted-printable-decode-string)
193 ("binary" . identity)
195 "Alist of encoding vs. corresponding method to decode string.
196 Each element looks like (STRING . FUNCTION).
197 STRING is content-transfer-encoding.
198 FUNCTION is string decoder.")
201 (defun mime-decode-string (string encoding)
202 "Decode STRING using ENCODING.
203 ENCODING must be string. If ENCODING is found in
204 `mime-string-decoding-method-alist' as its key, this function decodes
205 the STRING by its value."
206 (let ((f (cdr (assoc encoding mime-string-decoding-method-alist))))
211 (mime-decode-region (point-min)(point-max) encoding)
220 (defvar mime-file-encoding-method-alist
221 '(("base64" . base64-insert-encoded-file)
222 ("quoted-printable" . quoted-printable-insert-encoded-file)
223 ;; Not standard, their use is DISCOURAGED.
224 ;; ("x-uue" . uuencode-insert-encoded-file)
225 ;; ("x-gzip64" . gzip64-insert-encoded-file)
226 ("7bit" . insert-file-contents-as-binary)
227 ("8bit" . insert-file-contents-as-binary)
228 ("binary" . insert-file-contents-as-binary)
230 "Alist of encoding vs. corresponding method to insert encoded file.
231 Each element looks like (STRING . FUNCTION).
232 STRING is content-transfer-encoding.
233 FUNCTION is function to insert encoded file.")
236 (defvar mime-file-decoding-method-alist
237 '(("base64" . base64-write-decoded-region)
238 ("quoted-printable" . quoted-printable-write-decoded-region)
239 ("x-uue" . uuencode-write-decoded-region)
240 ("x-gzip64" . gzip64-write-decoded-region)
241 ("7bit" . write-region-as-binary)
242 ("8bit" . write-region-as-binary)
243 ("binary" . write-region-as-binary)
245 "Alist of encoding vs. corresponding method to write decoded region to file.
246 Each element looks like (STRING . FUNCTION).
247 STRING is content-transfer-encoding.
248 FUNCTION is function to write decoded region to file.")
251 (defun mime-insert-encoded-file (filename encoding)
252 "Insert file FILENAME encoded by ENCODING format."
254 (list (read-file-name "Insert encoded file: ")
255 (completing-read "encoding: "
256 mime-encoding-method-alist
259 (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
265 (defun mime-write-decoded-region (start end filename encoding)
266 "Decode and write current region encoded by ENCODING into FILENAME.
267 START and END are buffer positions."
269 (list (region-beginning) (region-end)
270 (read-file-name "Write decoded region to file: ")
271 (completing-read "encoding: "
272 mime-file-decoding-method-alist
274 (let ((f (cdr (assoc encoding mime-file-decoding-method-alist))))
276 (funcall f start end filename)
285 ;;; mel.el ends here.