Commits

Anonymous committed 98dc539

2001-02-16 Simon Josefsson <simon@josefsson.org>

* base64.el: Synch with Gnus: Require cl. Use defalias instead of
fset. Add variable base64-binary-coding-system. Use it. Change
base64-*insert-char. Add optional argument no-line-break for
compatibility to base64-encode*.

  • Participants
  • Parent commits 8242287

Comments (0)

Files changed (2)

+2001-02-16  Simon Josefsson  <simon@josefsson.org>
+
+	* base64.el: Synch with Gnus: Require cl.  Use defalias instead of
+	fset. Add variable base64-binary-coding-system. Use it.  Change
+	base64-*insert-char.  Add optional argument no-line-break for
+	compatibility to base64-encode*.
+
 2000-12-31  Simon Josefsson  <simon@josefsson.org>
 
 	* reporter.el: Incorporate doc fixes from Emacs 21, update
 ;;; base64.el,v --- Base64 encoding functions
 ;; Author: Kyle E. Jones
-;; Created: 1999/03/25 05:30:03
-;; Version: 1.3
+;; Created: 1997/03/12 14:37:09
+;; Version: 1.6
 ;; Keywords: extensions
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;; Boston, MA 02111-1307, USA.
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
+(eval-when-compile (require 'cl))
+
 ;; For non-MULE
 (if (not (fboundp 'char-int))
-    (fset 'char-int 'identity))
+    (defalias 'char-int 'identity))
 
 (defvar base64-alphabet
   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
     ( ?w . 48) ( ?x . 49) ( ?y . 50) ( ?z . 51) ( ?0 . 52) ( ?1 . 53)
     ( ?2 . 54) ( ?3 . 55) ( ?4 . 56) ( ?5 . 57) ( ?6 . 58) ( ?7 . 59)
     ( ?8 . 60) ( ?9 . 61) ( ?+ . 62) ( ?/ . 63)
-   ))
+    ))
 
 (defvar base64-alphabet-decoding-vector
   (let ((v (make-vector 123 nil))
       (setq p (cdr p)))
     v))
 
+(defvar base64-binary-coding-system 'binary)
+
 (defun base64-run-command-on-region (start end output-buffer command
 					   &rest arg-list)
-  (let ((tempfile nil) status errstring)
+  (let ((tempfile nil) status errstring default-process-coding-system 
+	(coding-system-for-write base64-binary-coding-system)
+	(coding-system-for-read base64-binary-coding-system))
     (unwind-protect
 	(progn
 	  (setq tempfile (make-temp-name "base64"))
 		     (setq errstring (buffer-string))
 		     (kill-buffer nil)
 		     (cons status errstring)))))
-      (condition-case ()
-	  (delete-file tempfile)
-	(error nil)))))
+      (ignore-errors
+	(delete-file tempfile)))))
 
-(defun base64-insert-char (char &optional count ignored buffer)
-  (condition-case nil
-      (progn
-	(insert-char char count ignored buffer)
-	(fset 'base64-insert-char 'insert-char))
-    (wrong-number-of-arguments
-     (fset 'base64-insert-char 'base64-xemacs-insert-char)
-     (base64-insert-char char count ignored buffer))))
-
-(defun base64-xemacs-insert-char (char &optional count ignored buffer)
-  (if (and buffer (eq buffer (current-buffer)))
-      (insert-char char count)
-    (save-excursion
-      (set-buffer buffer)
-      (insert-char char count))))
+(if (featurep 'xemacs)
+    (defalias 'base64-insert-char 'insert-char)
+  (defun base64-insert-char (char &optional count ignored buffer)
+    (if (or (null buffer) (eq buffer (current-buffer)))
+	(insert-char char count)
+      (with-current-buffer buffer
+	(insert-char char count))))
+  (setq base64-binary-coding-system 'no-conversion))
 
 (defun base64-decode-region (start end)
   (interactive "r")
-  (message "Decoding base64...")
+  ;;(message "Decoding base64...")
   (let ((work-buffer nil)
 	(done nil)
 	(counter 0)
 	  (if base64-decoder-program
 	      (let* ((binary-process-output t) ; any text already has CRLFs
 		     (status (apply 'base64-run-command-on-region
-				   start end work-buffer
-				   base64-decoder-program
-				   base64-decoder-switches)))
+				    start end work-buffer
+				    base64-decoder-program
+				    base64-decoder-switches)))
 		(if (not (eq status t))
 		    (error "%s" (cdr status))))
 	    (goto-char start)
 	       ((> (skip-chars-forward base64-alphabet end) 0)
 		(setq lim (point))
 		(while (< inputpos lim)
-		  (setq bits (+ bits 
+		  (setq bits (+ bits
 				(aref base64-alphabet-decoding-vector
 				      (char-int (char-after inputpos)))))
 		  (setq counter (1+ counter)
 		  (cond ((= counter 4)
 			 (base64-insert-char (lsh bits -16) 1 nil work-buffer)
 			 (base64-insert-char (logand (lsh bits -8) 255) 1 nil
-					 work-buffer)
+					     work-buffer)
 			 (base64-insert-char (logand bits 255) 1 nil
 					     work-buffer)
 			 (setq bits 0 counter 0))
 			(t (setq bits (lsh bits 6)))))))
 	      (cond
-	       ((= (point) end)
-		(if (not (zerop counter))
-		    (error "at least %d bits missing at end of base64 encoding"
-			   (* (- 4 counter) 6)))
-		(setq done t))
-	       ((= (char-after (point)) ?=)
+	       ((or (= (point) end)
+		    (eq (char-after (point)) ?=))
+		(if (and (= (point) end) (> counter 1))
+		    (message 
+		     "at least %d bits missing at end of base64 encoding"
+		     (* (- 4 counter) 6)))
 		(setq done t)
 		(cond ((= counter 1)
 		       (error "at least 2 bits missing at end of base64 encoding"))
 	  (insert-buffer-substring work-buffer)
 	  (delete-region (point) end))
       (and work-buffer (kill-buffer work-buffer))))
-  (message "Decoding base64... done"))
+  ;;(message "Decoding base64... done")
+  )
 
-(defun base64-encode-region (start end)
+(defun base64-encode-region (start end &optional no-line-break)
   (interactive "r")
   (message "Encoding base64...")
   (let ((work-buffer nil)
 		      (aref alphabet (logand bits 63))
 		      1 nil work-buffer)
 		     (setq cols (+ cols 4))
-		     (cond ((= cols 72)
+		     (cond ((and (= cols 72)
+				 (not no-line-break))
 			    (base64-insert-char ?\n 1 nil work-buffer)
 			    (setq cols 0)))
 		     (setq bits 0 counter 0))
 		(base64-insert-char (aref alphabet (logand (lsh bits -6) 63))
 				    1 nil work-buffer)
 		(base64-insert-char ?= 1 nil work-buffer)))
-	    (if (> cols 0)
-		(base64-insert-char ?\n 1 nil work-buffer)))
+	    (if (and (> cols 0)
+		     (not no-line-break))
+	    	(base64-insert-char ?\n 1 nil work-buffer)))
 	  (or (markerp end) (setq end (set-marker (make-marker) end)))
 	  (goto-char start)
 	  (insert-buffer-substring work-buffer)
       (and work-buffer (kill-buffer work-buffer))))
   (message "Encoding base64... done"))
 
-(defun base64-encode (string)
+(defun base64-encode (string &optional no-line-break)
   (save-excursion
     (set-buffer (get-buffer-create " *base64-encode*"))
     (erase-buffer)
     (insert string)
-    (base64-encode-region (point-min) (point-max))
+    (base64-encode-region (point-min) (point-max) no-line-break)
     (skip-chars-backward " \t\r\n")
     (delete-region (point-max) (point))
     (prog1
     (delete-region (point-max) (point))
     (prog1
 	(buffer-string)
-      (kill-buffer (current-buffer)))))  
+      (kill-buffer (current-buffer)))))
 
+(defalias 'base64-decode-string 'base64-decode)
 (defalias 'base64-encode-string 'base64-encode)
-(defalias 'base64-decode-string 'base64-decode)
 
 (provide 'base64)