-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.lisp
30 lines (22 loc) · 860 Bytes
/
decode.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(in-package :cl-simplebase64)
(defun encoded-char2int (char)
(position char *base64-alphabet*))
(defun octets2string (str)
#+sbcl (sb-ext:octets-to-string str)
#-sbcl (error "run this on sbcl"))
(defun make-octets (lst)
(coerce
(map 'vector #'bits2integer
(remove-if #'(lambda (x) (< (length x) 8)) lst)) 'octets))
(defun encoded-string2int (str)
(map 'vector #'(lambda (x) (padding (make-bits (encoded-char2int x)) 6 0 nil 'bit))
(remove #\= str)))
(defgeneric decode (input))
(defmethod decode ((str string))
(octets2string
(make-octets
(divide-bits (conc-seq-list vector (encoded-string2int str)) 8))))
(defmethod decode ((stm stream))
(cond ((not (open-stream-p stm)) (error "stream is not opened"))
((not (input-stream-p stm)) (error "stream cannot provide input"))
(t (mapcar #'decode (base64::stream2list stm)))))