(eword-decode-string, eword-decode-region): Mention language info in doc string.
[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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Code:
26
27 (require 'mime-def)
28
29 (defvar base64-dl-handle
30   (and (stringp base64-dl-module)
31        (file-exists-p base64-dl-module)
32        (dynamic-link base64-dl-module)))
33
34 (dynamic-call "emacs_base64_init" base64-dl-handle)
35
36 ;; base64-dl-module provides `encode-base64-string' and `decode-base64-string'.
37 (defalias 'base64-encode-string 'encode-base64-string)
38 (defalias 'base64-decode-string 'decode-base64-string)
39
40 (defun base64-encode-region (start end)
41   "Encode current region by base64.
42 START and END are buffer positions."
43   (interactive "*r")
44   (insert
45     (prog1
46         (base64-encode-string
47          (buffer-substring start end))
48       (delete-region start end)))
49   (or (bolp) (insert ?\n)))
50
51 (defun base64-decode-region (start end)
52   "Decode current region by base64.
53 START and END are buffer positions."
54   (interactive "*r")
55   (insert
56    (prog1
57        (base64-decode-string
58         (buffer-substring start end))
59      (delete-region start end))))
60
61
62 (mel-define-method-function (mime-encode-string string (nil "base64"))
63                             'base64-encode-string)
64 (mel-define-method-function (mime-decode-string string (nil "base64"))
65                             'base64-decode-string)
66 (mel-define-method-function (mime-encode-region start end (nil "base64"))
67                             'base64-encode-region)
68 (mel-define-method-function (mime-decode-region start end (nil "base64"))
69                             'base64-decode-region)
70
71 (mel-define-method-function (encoded-text-encode-string string (nil "B"))
72                             'base64-encode-string)
73
74 (mel-define-method encoded-text-decode-string (string (nil "B"))
75   (if (string-match (eval-when-compile
76                       (concat "\\`" B-encoded-text-regexp "\\'"))
77                     string)
78       (base64-decode-string string)
79     (error "Invalid encoded-text %s" string)))
80
81
82 ;;; @ base64 encoder/decoder for file
83 ;;;
84
85 (mel-define-method mime-insert-encoded-file (filename (nil "base64"))
86   "Encode contents of file FILENAME to base64, and insert the result.
87 It calls external base64 encoder specified by
88 `base64-external-encoder'.  So you must install the program (maybe
89 mmencode included in metamail or XEmacs package)."
90   (interactive "*fInsert encoded file: ")
91   (insert (base64-encode-string
92            (with-temp-buffer
93              (set-buffer-multibyte nil)
94              (insert-file-contents-as-binary filename)
95              (buffer-string))))
96   (or (bolp) (insert ?\n)))
97
98 ;; (mel-define-method mime-write-decoded-region (start end filename
99 ;;                                                     (nil "base64"))
100 ;;   "Decode and write current region encoded by base64 into FILENAME.
101 ;; START and END are buffer positions."
102 ;;   (interactive "*r\nFWrite decoded region to file: ")
103 ;;   (let ((str (buffer-substring start end)))
104 ;;     (with-temp-buffer
105 ;;       (insert (base64-decode-string str))
106 ;;       (write-region-as-binary (point-min)(point-max) filename))))
107
108
109 ;;; @ end
110 ;;;
111
112 (provide 'mel-b-dl)
113
114 ;;; mel-b-dl.el ends here.