7 ;;; @ internal base64 decoder/encoder
8 ;;; based on base64 decoder by Enami Tsugutomo
10 ;;; @@ convert from/to base64 char
13 (defun base64-num-to-char (n)
16 ((< n 52) (+ ?a (- n 26)))
17 ((< n 62) (+ ?0 (- n 52)))
20 (t (error "not a base64 integer %d" n))))
22 (defun base64-char-to-num (c)
23 (cond ((and (<= ?A c) (<= c ?Z)) (- c ?A))
24 ((and (<= ?a c) (<= c ?z)) (+ (- c ?a) 26))
25 ((and (<= ?0 c) (<= c ?9)) (+ (- c ?0) 52))
29 (t (error "not a base64 character %c" c))))
32 ;;; @@ encode/decode one base64 unit
35 (defun base64-mask (i n) (logand i (1- (ash 1 n))))
37 (defun base64-encode-1 (a &optional b &optional c)
39 (cons (logior (ash (base64-mask a 2) (- 6 2))
42 (cons (logior (ash (base64-mask b 4) (- 6 4))
45 (cons (base64-mask c (- 6 0))
48 (defun base64-decode-1 (a b &optional c &optional d)
49 (cons (logior (ash a 2) (ash b (- 2 6)))
50 (if c (cons (logior (ash (base64-mask b 4) 4)
51 (base64-mask (ash c (- 4 6)) 4))
52 (if d (cons (logior (ash (base64-mask c 2) 6) d)
55 (defun base64-encode-chars (a &optional b &optional c)
56 (mapcar (function base64-num-to-char) (base64-encode-1 a b c)))
58 (defun base64-decode-chars (&rest args)
59 (apply (function base64-decode-1)
60 (mapcar (function base64-char-to-num) args)
64 ;;; @@ encode/decode base64 string
67 (defun base64-encode-string (string)
71 (mapconcat (function char-to-string)
72 (apply (function base64-encode-chars) pack)
75 (pack-sequence string 3)
77 (m (mod (length es) 4))
79 (concat es (cond ((= m 3) "=")
84 (defun base64-decode-string (string)
87 (mapconcat (function char-to-string)
88 (apply (function base64-decode-chars) pack)
91 (pack-sequence string 4)
98 (defun base64-encoded-length (string)
99 (let ((len (length string)))
101 (if (= (mod len 3) 0) 0 1)