X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=examples%2Fcompress.lisp;fp=examples%2Fcompress.lisp;h=1229be56e75ac1962099665ffb398786821cce6e;hb=276c8f34b3070db313908a6d49a1ada16923fc21;hp=e5140aea57b6d4b60988d5dd2cb670b7175142cc;hpb=673f79a7d41d393c776f864a3c8a58640500b774;p=uffi.git diff --git a/examples/compress.lisp b/examples/compress.lisp index e5140ae..1229be5 100644 --- a/examples/compress.lisp +++ b/examples/compress.lisp @@ -7,7 +7,7 @@ ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Feb 2002 ;;;; -;;;; $Id: compress.lisp,v 1.2 2002/12/03 06:58:39 kevin Exp $ +;;;; $Id: compress.lisp,v 1.3 2002/12/09 16:30:20 kevin Exp $ ;;;; ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; @@ -63,6 +63,33 @@ (uffi:free-foreign-object destlen) (uffi:free-foreign-object dest))))))) +(uffi:def-function ("uncompress" c-uncompress) + ((dest (* :unsigned-char)) + (destlen (* :long)) + (source :cstring) + (source-len :long)) + :returning :int + :module "zlib") + +(defun uncompress (source) + (let* ((sourcelen (length source)) + (destsize 200000) ;adjust as needed + (dest (uffi:allocate-foreign-string destsize :unsigned t)) + (destlen (uffi:allocate-foreign-object :long))) + (setf (uffi:deref-pointer destlen :long) destsize) + (uffi:with-cstring (source-native source) + (let ((result (c-uncompress dest destlen source-native sourcelen)) + (newdestlen (uffi:deref-pointer destlen :long))) + (unwind-protect + (if (zerop result) + (uffi:convert-from-foreign-string + dest + :length newdestlen + :null-terminated-p nil) + (error "zlib error, code ~D" result)) + (progn + (uffi:free-foreign-object destlen) + (uffi:free-foreign-object dest))))))) #+examples-uffi (progn @@ -76,6 +103,17 @@ (print-results "test") (print-results "test2"))) +#+test-uffi +(progn + (flet ((test-compress (str) + (multiple-value-bind (compressed len) (compress str) + (multiple-value-bind (uncompressed len2) (uncompress compressed) + (util.test:test str uncompressed :test #'string= + :fail-info "Error uncompressing a compressed string"))))) + (test-compress "") + (test-compress "test") + (test-compress "test2"))) + ;; Results of the above on my system: ;; (compress "") => 789c300001,8 ;; (compress "test") => 789c2b492d2e1045d1c1,12