(base64-dl-encode-string): New alias.
[elisp/flim.git] / mel-dl.el
1 ;;; mel-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 'emu)
28
29 (defvar base64-dl-module
30   (expand-file-name "base64.so" exec-directory))
31
32 (defvar base64-dl-handle
33   (and (file-exists-p base64-dl-module)
34        (dynamic-link base64-dl-module)))
35
36 (dynamic-call "emacs_base64_init" base64-dl-handle)
37
38 (defalias 'base64-dl-encode-string 'encode-base64-string)
39 (defalias 'base64-dl-decode-string 'decode-base64-string)
40
41 (defun base64-dl-encode-region (start end)
42   "Encode current region by base64.
43 START and END are buffer positions."
44   (interactive "r")
45   (let ((str (buffer-substring start end)))
46     (delete-region start end)
47     (insert (encode-base64-string str))
48     )
49   (or (bolp)
50       (insert "\n"))
51   )
52
53 (defun base64-dl-decode-region (start end)
54   "Decode current region by base64.
55 START and END are buffer positions."
56   (interactive "r")
57   (let ((str (buffer-substring start end)))
58     (delete-region start end)
59     (condition-case err
60         (insert (decode-base64-string str))
61       (error (message (nth 1 err)))
62       )))
63
64 (defalias 'base64-encode-string 'encode-base64-string)
65 (defalias 'base64-decode-string 'decode-base64-string)
66 (defalias 'base64-encode-region 'base64-dl-encode-region)
67 (defalias 'base64-decode-region 'base64-dl-decode-region)
68
69
70 ;;; @ base64 encoder/decoder for file
71 ;;;
72
73 (defvar base64-external-encoder '("mmencode")
74   "*list of base64 encoder program name and its arguments.")
75
76 (defvar base64-external-decoder '("mmencode" "-u")
77   "*list of base64 decoder program name and its arguments.")
78
79 (defvar base64-external-decoder-option-to-specify-file '("-o")
80   "*list of options of base64 decoder program to specify file.")
81
82 (defun base64-insert-encoded-file (filename)
83   "Encode contents of file FILENAME to base64, and insert the result.
84 It calls external base64 encoder specified by
85 `base64-external-encoder'.  So you must install the program (maybe
86 mmencode included in metamail or XEmacs package)."
87   (interactive (list (read-file-name "Insert encoded file: ")))
88   (apply (function call-process) (car base64-external-encoder)
89          filename t nil (cdr base64-external-encoder))
90   )
91
92 (defun base64-write-decoded-region (start end filename)
93   "Decode and write current region encoded by base64 into FILENAME.
94 START and END are buffer positions."
95   (interactive
96    (list (region-beginning) (region-end)
97          (read-file-name "Write decoded region to file: ")))
98   (as-binary-process
99    (apply (function call-process-region)
100           start end (car base64-external-decoder)
101           nil nil nil
102           (append (cdr base64-external-decoder)
103                   base64-external-decoder-option-to-specify-file
104                   (list filename))
105           )))
106
107
108 ;;; @ etc
109 ;;;
110
111 (defun base64-encoded-length (string)
112   (let ((len (length string)))
113     (* (+ (/ len 3)
114           (if (= (mod len 3) 0) 0 1)
115           ) 4)
116     ))
117
118
119 ;;; @ end
120 ;;;
121
122 (provide 'mel-dl)
123
124 ;;; mel-dl.el ends here.