Merge flim-1_13_2_1.
[elisp/flim.git] / mel-b-dl.el
1 ;;; mel-b-dl.el --- Base64 encoder/decoder using DL module.
2
3 ;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: MIME, Base64
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'mime-def)
28
29 (eval-and-compile
30   (defvar base64-dl-handle
31     (and (stringp base64-dl-module)
32          (file-exists-p base64-dl-module)
33          (dynamic-link base64-dl-module)))
34
35   (dynamic-call "emacs_base64_init" base64-dl-handle)
36   )
37
38 ;; base64-dl-module provides `encode-base64-string' and `decode-base64-string'.
39 (defalias 'base64-encode-string 'encode-base64-string)
40 (defalias 'base64-decode-string 'decode-base64-string)
41
42 (defun base64-encode-region (start end)
43   "Encode current region by base64.
44 START and END are buffer positions."
45   (interactive "*r")
46   (insert
47     (prog1
48         (base64-encode-string
49          (buffer-substring start end))
50       (delete-region start end)))
51   (or (bolp) (insert ?\n)))
52
53 (defun base64-decode-region (start end)
54   "Decode current region by base64.
55 START and END are buffer positions."
56   (interactive "*r")
57   (insert
58    (prog1
59        (base64-decode-string
60         (buffer-substring start end))
61      (delete-region start end))))
62
63
64 (mel-define-method-function (mime-encode-string string (nil "base64"))
65                             'base64-encode-string)
66 (mel-define-method-function (mime-decode-string string (nil "base64"))
67                             'base64-decode-string)
68 (mel-define-method-function (mime-encode-region start end (nil "base64"))
69                             'base64-encode-region)
70 (mel-define-method-function (mime-decode-region start end (nil "base64"))
71                             'base64-decode-region)
72
73 (mel-define-method-function (encoded-text-encode-string string (nil "B"))
74                             'base64-encode-string)
75
76 (mel-define-method encoded-text-decode-string (string (nil "B"))
77   (if (string-match (eval-when-compile
78                       (concat "\\`" B-encoded-text-regexp "\\'"))
79                     string)
80       (base64-decode-string string)
81     (error "Invalid encoded-text %s" string)))
82
83
84 ;;; @ base64 encoder/decoder for file
85 ;;;
86
87 (mel-define-method mime-insert-encoded-file (filename (nil "base64"))
88   "Encode contents of file FILENAME to base64, and insert the result.
89 It calls external base64 encoder specified by
90 `base64-external-encoder'.  So you must install the program (maybe
91 mmencode included in metamail or XEmacs package)."
92   (interactive "*fInsert encoded file: ")
93   (insert (base64-encode-string
94            (with-temp-buffer
95              (set-buffer-multibyte nil)
96              (insert-file-contents-as-binary filename)
97              (buffer-string))))
98   (or (bolp) (insert ?\n)))
99
100 ;; (mel-define-method mime-write-decoded-region (start end filename
101 ;;                                                     (nil "base64"))
102 ;;   "Decode and write current region encoded by base64 into FILENAME.
103 ;; START and END are buffer positions."
104 ;;   (interactive "*r\nFWrite decoded region to file: ")
105 ;;   (let ((str (buffer-substring start end)))
106 ;;     (with-temp-buffer
107 ;;       (insert (base64-decode-string str))
108 ;;       (write-region-as-binary (point-min)(point-max) filename))))
109
110
111 ;;; @ end
112 ;;;
113
114 (provide 'mel-b-dl)
115
116 ;;; mel-b-dl.el ends here.