Merge flim-1_11_3.
[elisp/flim.git] / mel-b-dl.el
1 ;;; mel-b-dl.el: Base64 encoder/decoder using DL module
2
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: MIME, Base64
7
8 ;; This file is part of MEL (MIME Encoding Library).
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 GNU Emacs; 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 'poe)
28 (require 'mime-def)
29
30 (eval-and-compile
31   (defvar base64-dl-handle
32     (and (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 (defun base64-encode-region (start end)
39   "Encode current region by base64.
40 START and END are buffer positions."
41   (interactive "r")
42   (let ((str (buffer-substring start end)))
43     (delete-region start end)
44     (insert (encode-base64-string str))
45     )
46   (or (bolp)
47       (insert "\n"))
48   )
49
50 (defun decode-base64-region (start end)
51   "Decode current region by base64.
52 START and END are buffer positions."
53   (interactive "r")
54   (let ((str (buffer-substring start end)))
55     (delete-region start end)
56     (condition-case err
57         (insert (decode-base64-string str))
58       (error (message (nth 1 err)))
59       )))
60
61 (defalias 'base64-encode-string 'encode-base64-string)
62 (defalias 'base64-decode-string 'decode-base64-string)
63
64
65 (mel-define-method-function (mime-encode-string string (nil "base64"))
66                             'encode-base64-string)
67 (mel-define-method-function (mime-decode-string string (nil "base64"))
68                             'decode-base64-string)
69 (mel-define-method-function (mime-encode-region start end (nil "base64"))
70                             'base64-encode-region)
71 (mel-define-method-function (mime-decode-region start end (nil "base64"))
72                             'decode-base64-region)
73
74 (mel-define-method-function (encoded-text-encode-string string (nil "B"))
75                             'encode-base64-string)
76
77 (mel-define-method encoded-text-decode-string (string (nil "B"))
78   (if (and (string-match B-encoded-text-regexp string)
79            (string= string (match-string 0 string)))
80       (decode-base64-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 (list (read-file-name "Insert encoded file: ")))
93   (insert (encode-base64-string
94            (with-temp-buffer
95              (set-buffer-multibyte nil)
96              (insert-file-contents-as-binary filename)
97              (buffer-string))))
98   (or (bolp)
99       (insert "\n"))
100   )
101
102 ;; (mel-define-method mime-write-decoded-region (start end filename
103 ;;                                                     (nil "base64"))
104 ;;   "Decode and write current region encoded by base64 into FILENAME.
105 ;; START and END are buffer positions."
106 ;;   (interactive
107 ;;    (list (region-beginning) (region-end)
108 ;;          (read-file-name "Write decoded region to file: ")))
109 ;;   (let ((str (buffer-substring start end)))
110 ;;     (with-temp-buffer
111 ;;       (insert (decode-base64-string str))
112 ;;       (write-region-as-binary (point-min) (point-max) filename)
113 ;;       )))
114
115
116 ;;; @ end
117 ;;;
118
119 (provide 'mel-b-dl)
120
121 ;;; mel-b-dl.el ends here.