Skip to content

Commit

Permalink
Merge pull request #7 from adolenc/master
Browse files Browse the repository at this point in the history
Support bin 16 and bin 32 types and decode bin as str
  • Loading branch information
phmarek committed Aug 13, 2015
2 parents 038e3bc + 0d939f3 commit e0c9d1b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
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

0 comments on commit e0c9d1b

Please sign in to comment.