X-Git-Url: http://git.chise.org/gitweb/?a=blobdiff_plain;f=mel-b.el;h=e9a382a5dfa2de27f3e8473d25068677778c6fb4;hb=5dea9b12c7bea11a20ebfd744ba0784013d8c007;hp=4c16f5e7ba6b053f4ccafa56cf8b4f82a56a13b0;hpb=a0a62365fcf263796084ca44d8e61dba40bfd1b9;p=elisp%2Fflim.git diff --git a/mel-b.el b/mel-b.el index 4c16f5e..e9a382a 100644 --- a/mel-b.el +++ b/mel-b.el @@ -103,18 +103,19 @@ external decoder is called.") (base64-num-to-char (ash (logand a 3) 4))) "==") )))) -(defun base64-decode-1 (pack) - (let ((a (base64-char-to-num (car pack))) - (b (base64-char-to-num (nth 1 pack))) - (c (nth 2 pack)) - (d (nth 3 pack))) - (concat (char-to-string (logior (ash a 2) (ash b -4))) - (if (and c (setq c (base64-char-to-num c))) - (concat (char-to-string - (logior (ash (logand b 15) 4) (ash c -2))) - (if (and d (setq d (base64-char-to-num d))) - (char-to-string (logior (ash (logand c 3) 6) d)) - )))))) +(defun base64-decode-unit (a b &optional c d) + (condition-case err + (concat + (char-to-string (logior (ash (base64-char-to-num a) 2) + (ash (setq b (base64-char-to-num b)) -4))) + (if (and c (setq c (base64-char-to-num c))) + (concat (char-to-string + (logior (ash (logand b 15) 4) (ash c -2))) + (if (and d (setq d (base64-char-to-num d))) + (char-to-string (logior (ash (logand c 3) 6) d)) + )))) + (error (message (nth 1 err)) + ""))) ;;; @@ base64 encoder/decoder for string @@ -148,11 +149,43 @@ external decoder is called.") )) ))) -(defun base64-decode-string (string) - "Decode STRING which is encoded in base64, and return the result." - (mapconcat (function base64-decode-1) - (pack-sequence string 4) - "")) +(defun base64-internal-decode-string (string) + (let ((len (length string)) + (i 0) + dest) + (while (< i len) + (let ((a (aref string i))) + (setq i (1+ i)) + (unless (eq a ?\n) + (let ((b (aref string i))) + (setq i (1+ i)) + (cond + ((eq b ?\n) + ;; invalid + ) + ((>= i len) + (setq dest (concat dest (base64-decode-unit a b) )) + ) + (t + (let ((c (aref string i))) + (setq i (1+ i)) + (cond + ((eq c ?\n) + (setq dest (concat dest (base64-decode-unit a b))) + ) + ((>= i len) + (setq dest (concat dest (base64-decode-unit a b c))) + ) + (t + (let ((d (aref string i))) + (setq i (1+ i)) + (setq dest + (concat dest + (if (eq c ?\n) + (base64-decode-unit a b c) + (base64-decode-unit a b c d)))) + )))))))))) + dest)) ;;; @ base64 encoder/decoder for region @@ -173,31 +206,10 @@ external decoder is called.") (defun base64-internal-decode-region (beg end) (save-excursion - (save-restriction - (narrow-to-region beg end) - (goto-char (point-min)) - (while (looking-at ".*\n") - (condition-case err - (replace-match - (base64-decode-string - (buffer-substring (match-beginning 0) (1- (match-end 0)))) - t t) - (error - (prog1 - (message (nth 1 err)) - (replace-match ""))))) - (if (looking-at ".*$") - (condition-case err - (replace-match - (base64-decode-string - (buffer-substring (match-beginning 0) (match-end 0))) - t t) - (error - (prog1 - (message (nth 1 err)) - (replace-match ""))) - )) - ))) + (let ((str (buffer-substring beg end))) + (delete-region beg end) + (goto-char beg) + (insert (base64-internal-decode-string str))))) (defun base64-external-encode-region (beg end) (save-excursion @@ -222,6 +234,17 @@ external decoder is called.") t t nil (cdr base64-external-decoder)) ))) +(defun base64-external-decode-string (string) + (with-temp-buffer + (insert string) + (as-binary-process (apply (function call-process-region) + (point-min) (point-max) + (car base64-external-decoder) + t t nil (cdr base64-external-decoder)) + ) + (buffer-string))) + + (defun base64-encode-region (start end) "Encode current region by base64. START and END are buffer positions. @@ -252,6 +275,20 @@ metamail or XEmacs package)." (base64-internal-decode-region start end) )) +(defun base64-decode-string (string) + "Decode STRING which is encoded in base64, and return the result. +This function calls internal base64 decoder if size of STRING is +smaller than `base64-internal-decoding-limit', otherwise it calls +external base64 decoder specified by `base64-external-decoder'. In +this case, you must install the program (maybe mmencode included in +metamail or XEmacs package)." + (interactive "r") + (if (and base64-internal-decoding-limit + (> (length string) base64-internal-decoding-limit)) + (base64-external-decode-string string) + (base64-internal-decode-string string) + )) + ;;; @ base64 encoder/decoder for file ;;;