(Bug reports): Modify description of tm mailing list.
[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 ;; Version: $Id: mel-dl.el,v 1.1 1998-01-11 16:21:43 morioka Exp $
7 ;; Keywords: MIME, Base64
8
9 ;; This file is part of MEL (MIME Encoding Library).
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 GNU Emacs; 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 'emu)
29
30 (defvar base64-dl-module
31   (expand-file-name "base64.so" exec-directory))
32
33 (defvar base64-dl-handle
34   (and (file-exists-p base64-dl-module)
35        (dynamic-link base64-dl-module)))
36
37 (dynamic-call "emacs_base64_init" base64-dl-handle)
38
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   (save-excursion
47     (save-restriction
48       (narrow-to-region start end)
49       (let ((str (buffer-substring start end)))
50         (delete-region start end)
51         (insert (encode-base64-string str))
52         )
53       (or (bolp)
54           (insert "\n")
55           )
56       )))
57
58 (defun base64-decode-region (start end)
59   "Decode current region by base64.
60 START and END are buffer positions."
61   (interactive "r")
62   (save-excursion
63     (save-restriction
64       (narrow-to-region start end)
65       (goto-char (point-min))
66       (while (looking-at ".*\n")
67         (condition-case err
68             (replace-match
69              (decode-base64-string
70               (buffer-substring (match-beginning 0) (1- (match-end 0))))
71              t t)
72           (error
73            (prog1
74                (message (nth 1 err))
75              (replace-match "")))))
76       (if (looking-at ".*$")
77           (condition-case err
78               (replace-match
79                (decode-base64-string
80                 (buffer-substring (match-beginning 0) (match-end 0)))
81                t t)
82             (error
83              (prog1
84                  (message (nth 1 err))
85                (replace-match "")))
86             ))
87       )))
88
89
90 ;;; @ base64 encoder/decoder for file
91 ;;;
92
93 (defvar base64-external-encoder '("mmencode")
94   "*list of base64 encoder program name and its arguments.")
95
96 (defun base64-insert-encoded-file (filename)
97   "Encode contents of file FILENAME to base64, and insert the result.
98 It calls external base64 encoder specified by
99 `base64-external-encoder'.  So you must install the program (maybe
100 mmencode included in metamail or XEmacs package)."
101   (interactive (list (read-file-name "Insert encoded file: ")))
102   (apply (function call-process) (car base64-external-encoder)
103          filename t nil (cdr base64-external-encoder))
104   )
105
106
107 ;;; @ etc
108 ;;;
109
110 (defun base64-encoded-length (string)
111   (let ((len (length string)))
112     (* (+ (/ len 3)
113           (if (= (mod len 3) 0) 0 1)
114           ) 4)
115     ))
116
117
118 ;;; @ end
119 ;;;
120
121 (provide 'mel-dl)
122
123 ;;; mel-dl.el ends here.