马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
近期用到BASE64编码,用XMLDOM加ADODB.Stream的方法没试成功。百度了一些LISP方法,觉得效率太低,所以自己写了一个文本BASE64编码的函数。数字lsh和logand确实比十进制转二进制再查表快不知多少倍,顺便用了一下gcd函数。
因为LISP不能直接操作二进制数据,所以只好先拿文本来试试。函数如下:
- (defun Base64Encode (str / strlst n i map ad y base64 a b c s)
- (setq map "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
- (setq strlst (vl-string->list str))
- (setq n (/ (length strlst) 3))
- (setq ad "")
- (if (= (gcd (length strlst) 3) 1)(setq n (1+ n)))
- (setq i 0)
- (repeat n
- (if (< i (length strlst))(setq a (append a (list (nth i strlst)))))
- (if (< (1+ i) (length strlst))(setq b (append b (list (nth (1+ i) strlst)))))
- (if (< (+ i 2) (length strlst))(setq c (append c (list (nth (+ i 2) strlst)))))
- (setq i (+ i 3))
- )
- (if (< (length b)(length a))(setq ad (strcat ad "=") b (append b (list 0))))
- (if (< (length c)(length a))(setq ad (strcat ad "=") c (append c (list 0))))
- (setq s (apply 'strcat (mapcar '(lambda(x)(substr map (1+ x) 1)) (foreach x (mapcar '(lambda(x)(alter34 x)) (mapcar '(lambda(x y z)(list x y z)) a b c)) (setq y (append y x))))))
- (setq s (strcat (substr s 1 (- (strlen s)(strlen ad))) ad))
- (setq base64 "" i 1)
- (if (zerop (rem (strlen s) 76))(/ (strlen s) 76)(1+ (/ (strlen s) 76)))
- (repeat (if (zerop (rem (strlen s) 76))(/ (strlen s) 76)(1+ (/ (strlen s) 76)))
- (setq base64 (strcat base64 "\n" (substr s i 76)) i (+ i 76))
- )
- (substr base64 2)
- )
- (defun alter34 (lst3 / n1 n2 n3 m1 m2 m3 m4)
- (setq n1 (car lst3) n2 (cadr lst3) n3 (last lst3))
- (setq m1 (lsh n1 -2) m2 (+ (lsh (logand n1 3) 4) (lsh (logand n2 240) -4)) m3 (+ (lsh (logand n2 15) 2) (lsh (logand n3 192) -6)) m4 (logand n3 63))
- (list m1 m2 m3 m4)
- )
测试:
- (setq txt "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.")
- (base64encode txt)
返回
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
解码函数
- (defun Base64Decode (str / map ad s1 i lst a b c d ss)
- (setq map "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
- (setq lst (vl-remove-if '(lambda(x)(= x 10)) (vl-string->list str)))
- (if (member 61 lst)
- (setq ad (length (member 61 lst)))
- (setq ad 0)
- )
- (setq lst (mapcar '(lambda(x)(vl-string-position (ascii x) map)) (mapcar 'chr (subst 65 61 lst))))
- (setq i 0)
- (repeat (/ (length lst) 4)
- (setq a (append a (list (nth i lst))))
- (setq b (append b (list (nth (1+ i) lst))))
- (setq c (append c (list (nth (+ i 2) lst))))
- (setq d (append d (list (nth (+ i 3) lst))))
- (setq i (+ i 4))
- )
- (setq ss (apply 'strcat (mapcar 'vl-list->string (mapcar '(lambda(u)(alter43 u)) (mapcar '(lambda(w x y z)(list w x y z)) a b c d)))))
- (substr ss 1 (- (strlen ss) ad))
- )
- (defun alter43 (lst4 / n1 n2 n3 n4 m1 m2 m3)
- (setq n1 (car lst4))
- (setq n2 (cadr lst4))
- (setq n3 (caddr lst4))
- (setq n4 (last lst4))
- (setq m1 (+ (lsh n1 2)(lsh (logand n2 48) -4)))
- (setq m2 (+ (lsh (logand n2 15) 4) (lsh (logand n3 60) -2)))
- (setq m3 (+ (lsh (logand n3 3) 6) n4))
- (list m1 m2 m3)
- )
|