Sync with apel-10_3-1.
[elisp/lemi.git] / mime / mel-b.el
1 ;;; mel-b.el --- Base64 encoder/decoder using builtin base64 handlers
2
3 ;; Copyright (C) 1998,1999,2000,2001 Free Software Foundation, Inc.
4
5 ;; Author: Tanaka Akira <akr@m17n.org>
6 ;; Created by modifying mel-b-ccl.el: 2001/2/6
7 ;; Keywords: MIME, Base64
8
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (require 'mime-def)
29
30 (defun base64-insert-encoded-file (filename)
31   "Encode contents of file FILENAME to base64, and insert the result."
32   (interactive "*fInsert encoded file: ")
33   (save-excursion
34     (save-restriction
35       (narrow-to-region (point) (point))
36       (let ((coding-system-for-read 'binary))
37         (insert-file-contents filename))
38       (base64-encode-region (point-min) (point-max)))))
39
40 (mel-define-method-function (mime-encode-string string (nil "base64"))
41                             'base64-encode-string)
42 (mel-define-method-function (mime-encode-region start end (nil "base64"))
43                             'base64-encode-region)
44 (mel-define-method-function (mime-insert-encoded-file filename (nil "base64"))
45                             'base64-insert-encoded-file)
46 (mel-define-method-function (encoded-text-encode-string string (nil "B"))
47                             'base64-encode-string)
48
49 (defun base64-write-decoded-region (start end filename)
50   "Decode the region from START to END and write out to FILENAME."
51   (interactive "*r\nFWrite decoded region to file: ")
52   (let ((current (current-buffer))
53         (multibyte enable-multibyte-characters))
54     (with-temp-buffer
55       (set-buffer-multibyte multibyte)
56       (insert-buffer-substring current start end)
57       (base64-decode-region (point-min) (point-max))
58       (let ((coding-system-for-write 'binary))
59         (write-region (point-min) (point-max) filename)))))
60
61 (mel-define-method-function (mime-decode-string string (nil "base64"))
62                             'base64-ccl-decode-string)
63 (mel-define-method-function (mime-decode-region start end (nil "base64"))
64                             'base64-ccl-decode-region)
65 (mel-define-method-function
66  (mime-write-decoded-region start end filename (nil "base64"))
67  'base64-write-decoded-region)
68
69 (mel-define-method encoded-text-decode-string (string (nil "B"))
70   (if (string-match (eval-when-compile
71                       (concat "\\`" B-encoded-text-regexp "\\'"))
72                     string)
73       (base64-decode-string string)
74     (error "Invalid encoded-text %s" string)))
75
76
77 ;;; @ end
78 ;;;
79
80 (provide 'mel-b)
81
82 ;;; mel-b.el ends here.