Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bin 16 and bin 32 types and decode bin as str #7

Merged
merged 3 commits into from Aug 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion cl-messagepack.lisp
Expand Up @@ -107,6 +107,7 @@
(defvar *symbol->int* nil)
(defvar *decoder-prefers-lists* nil)
(defvar *decoder-prefers-alists* nil)
(defvar *decode-bin-as-string* nil)

(defvar *extended-types* nil)
(defvar *lookup-table* nil)
Expand Down Expand Up @@ -400,7 +401,11 @@
((= #xdf byte)
(decode-map (load-big-endian stream 4) stream))
((= #xc4 byte)
(decode-array (read-byte stream) stream))
(funcall (if *decode-bin-as-string* #'decode-string #'decode-array) (read-byte stream) stream))
((= #xc5 byte)
(funcall (if *decode-bin-as-string* #'decode-string #'decode-array) (load-big-endian stream 2) stream))
((= #xc6 byte)
(funcall (if *decode-bin-as-string* #'decode-string #'decode-array) (load-big-endian stream 4) stream))
(t (error
(format nil
"Cannot decode ~a (maybe you should bind *extended-types*?)" byte))))))
Expand Down
3 changes: 2 additions & 1 deletion package.lisp
Expand Up @@ -43,7 +43,8 @@
define-extension-types
symbol-to-extension-type
*decoder-prefers-lists*
*decoder-prefers-alists*))
*decoder-prefers-alists*
*decode-bin-as-string*))

(defpackage #:messagepack-tests
(:use #:cl #:fiveam)
Expand Down
29 changes: 29 additions & 0 deletions tests.lisp
Expand Up @@ -168,6 +168,35 @@ encode properly."
(is (string= medium-string (mpk:decode (mpk:encode medium-string))))
(is (string= long-string (mpk:decode (mpk:encode long-string)))))))

(test decoding-binary
"Test decoding of binary type."
(is (equalp #() (mpk:decode #(#xc4 #x00))))
(is (equalp #() (mpk:decode #(#xc5 #x00 #x00))))
(is (equalp #() (mpk:decode #(#xc6 #x00 #x00 #x00 #x00))))
(flet ((to-b8array (&rest contents)
(apply #'concatenate '(vector (unsigned-byte 8)) contents)))
(let ((short-array #(1 2 3))
(medium-array (make-array 1000 :initial-element 10))
(long-array (make-array 70000 :initial-element 10)))
(is (equalp short-array (mpk:decode (to-b8array #(#xc4 #x03) short-array))))
(is (equalp short-array (mpk:decode (to-b8array #(#xc5 #x00 #x03) short-array))))
(is (equalp medium-array (mpk:decode (to-b8array #(#xc5 #x03 #xe8) medium-array))))
(is (equalp short-array (mpk:decode (to-b8array #(#xc6 #x00 #x00 #x00 #x03) short-array))))
(is (equalp medium-array (mpk:decode (to-b8array #(#xc6 #x00 #x00 #x03 #xe8) medium-array))))
(is (equalp long-array (mpk:decode (to-b8array #(#xc6 #x00 #x01 #x11 #x70) long-array)))))
(let ((mpk:*decode-bin-as-string* T)
(short-string "tes")
(medium-string (with-output-to-string (str)
(loop repeat (/ 1000 5) do (princ "tests" str))))
(long-string (with-output-to-string (str)
(loop repeat (/ 70000 5) do (princ "tests" str)))))
(is (string= short-string (mpk:decode (to-b8array #(#xc4 #x03) (babel:string-to-octets short-string)))))
(is (string= short-string (mpk:decode (to-b8array #(#xc5 #x00 #x03) (babel:string-to-octets short-string)))))
(is (string= medium-string (mpk:decode (to-b8array #(#xc5 #x03 #xe8) (babel:string-to-octets medium-string)))))
(is (string= short-string (mpk:decode (to-b8array #(#xc6 #x00 #x00 #x00 #x03) (babel:string-to-octets short-string)))))
(is (string= medium-string (mpk:decode (to-b8array #(#xc6 #x00 #x00 #x03 #xe8) (babel:string-to-octets medium-string)))))
(is (string= long-string (mpk:decode (to-b8array #(#xc6 #x00 #x01 #x11 #x70) (babel:string-to-octets long-string))))))))

(test decoding-arrays
"Test that (equalp (decode (encode data)) data) holds for arrays."
(let ((short-array #(1 2 3))
Expand Down