From 72faddcc83e13d36b58bdeb5b4fe8250ffe52447 Mon Sep 17 00:00:00 2001 From: morioka Date: Sat, 19 Sep 1998 18:02:28 +0000 Subject: [PATCH] (base64-encode-region): Define directly (abolish `base64-dl-encode-region'). (base64-decode-region): Define directly (abolish `base64-dl-decode-region'). Define method functions of mel. (mime-insert-encoded-file): Use `mel-define-method'; abolish `base64-insert-encoded-file'; don't use external encoder. (mime-write-decoded-region): Use `mel-define-method'; abolish `base64-write-decoded-region'; don't use external decoder. - Move `base64-encoded-length' to mel.el. --- mel-dl.el | 87 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/mel-dl.el b/mel-dl.el index e1df065..1995d5d 100644 --- a/mel-dl.el +++ b/mel-dl.el @@ -25,20 +25,20 @@ ;;; Code: (require 'emu) +(require 'mime-def) -(defvar base64-dl-module - (expand-file-name "base64.so" exec-directory)) +(eval-and-compile + (defvar base64-dl-module + (expand-file-name "base64.so" exec-directory)) -(defvar base64-dl-handle - (and (file-exists-p base64-dl-module) - (dynamic-link base64-dl-module))) + (defvar base64-dl-handle + (and (file-exists-p base64-dl-module) + (dynamic-link base64-dl-module))) -(dynamic-call "emacs_base64_init" base64-dl-handle) - -(defalias 'base64-dl-encode-string 'encode-base64-string) -(defalias 'base64-dl-decode-string 'decode-base64-string) + (dynamic-call "emacs_base64_init" base64-dl-handle) + ) -(defun base64-dl-encode-region (start end) +(defun base64-encode-region (start end) "Encode current region by base64. START and END are buffer positions." (interactive "r") @@ -50,7 +50,7 @@ START and END are buffer positions." (insert "\n")) ) -(defun base64-dl-decode-region (start end) +(defun base64-decode-region (start end) "Decode current region by base64. START and END are buffer positions." (interactive "r") @@ -63,57 +63,56 @@ START and END are buffer positions." (defalias 'base64-encode-string 'encode-base64-string) (defalias 'base64-decode-string 'decode-base64-string) -(defalias 'base64-encode-region 'base64-dl-encode-region) -(defalias 'base64-decode-region 'base64-dl-decode-region) -;;; @ base64 encoder/decoder for file -;;; +(mel-define-method-function (mime-encode-string string (nil "base64")) + 'encode-base64-string) +(mel-define-method-function (mime-decode-string string (nil "base64")) + 'decode-base64-string) +(mel-define-method-function (mime-encode-region start end (nil "base64")) + 'base64-encode-region) +(mel-define-method-function (mime-decode-region start end (nil "base64")) + 'base64-decode-region) -(defvar base64-external-encoder '("mmencode") - "*list of base64 encoder program name and its arguments.") +(mel-define-method-function (encoded-text-encode-string string (nil "B")) + 'encode-base64-string) -(defvar base64-external-decoder '("mmencode" "-u") - "*list of base64 decoder program name and its arguments.") +(mel-define-method encoded-text-decode-string (string (nil "B")) + (if (and (string-match B-encoded-text-regexp string) + (string= string (match-string 0 string))) + (decode-base64-string string) + (error "Invalid encoded-text %s" string))) -(defvar base64-external-decoder-option-to-specify-file '("-o") - "*list of options of base64 decoder program to specify file.") -(defun base64-insert-encoded-file (filename) +;;; @ base64 encoder/decoder for file +;;; + +(mel-define-method mime-insert-encoded-file (filename (nil "base64")) "Encode contents of file FILENAME to base64, and insert the result. It calls external base64 encoder specified by `base64-external-encoder'. So you must install the program (maybe mmencode included in metamail or XEmacs package)." (interactive (list (read-file-name "Insert encoded file: "))) - (apply (function call-process) (car base64-external-encoder) - filename t nil (cdr base64-external-encoder)) + (insert (encode-base64-string + (with-temp-buffer + (insert-file-contents-as-binary filename) + (buffer-string)))) + (or (bolp) + (insert "\n")) ) -(defun base64-write-decoded-region (start end filename) +(mel-define-method mime-write-decoded-region (start end filename + (nil "base64")) "Decode and write current region encoded by base64 into FILENAME. START and END are buffer positions." (interactive (list (region-beginning) (region-end) (read-file-name "Write decoded region to file: "))) - (as-binary-process - (apply (function call-process-region) - start end (car base64-external-decoder) - nil nil nil - (append (cdr base64-external-decoder) - base64-external-decoder-option-to-specify-file - (list filename)) - ))) - - -;;; @ etc -;;; - -(defun base64-encoded-length (string) - (let ((len (length string))) - (* (+ (/ len 3) - (if (= (mod len 3) 0) 0 1) - ) 4) - )) + (let ((str (buffer-substring start end))) + (with-temp-buffer + (insert (decode-base64-string str)) + (write-region-as-binary (point-min) (point-max) filename) + ))) ;;; @ end -- 1.7.10.4