1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
\r
2 ;;;; *************************************************************************
\r
3 ;;;; FILE IDENTIFICATION
\r
5 ;;;; Name: compress.cl
\r
6 ;;;; Purpose: UFFI Example file for zlib compression
\r
7 ;;;; Programmer: Kevin M. Rosenberg
\r
8 ;;;; Date Started: Feb 2002
\r
10 ;;;; $Id: compress.lisp,v 1.6 2003/04/29 13:16:14 kevin Exp $
\r
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
\r
14 ;;;; UFFI users are granted the rights to distribute and use this software
\r
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
\r
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
\r
17 ;;;; *************************************************************************
\r
19 (in-package :uffi-tests)
\r
21 (eval-when (:load-toplevel :execute)
\r
22 (unless (uffi:load-foreign-library
\r
23 #-(or macosx darwin)
\r
24 (uffi:find-foreign-library
\r
26 '("/usr/local/lib/" "/usr/lib/" "/zlib/")
\r
28 #+(or macosx darwin)
\r
29 (uffi:find-foreign-library "z"
\r
30 `(,(pathname-directory *load-pathname*)))
\r
32 :supporting-libraries '("c"))
\r
33 (warn "Unable to load zlib")))
\r
35 (uffi:def-function ("compress" c-compress)
\r
36 ((dest (* :unsigned-char))
\r
43 (defun compress (source)
\r
44 "Returns two values: array of bytes containing the compressed data
\r
45 and the numbe of compressed bytes"
\r
46 (let* ((sourcelen (length source))
\r
47 (destsize (+ 12 (ceiling (* sourcelen 1.01))))
\r
48 (dest (uffi:allocate-foreign-string destsize :unsigned t))
\r
49 (destlen (uffi:allocate-foreign-object :long)))
\r
50 (setf (uffi:deref-pointer destlen :long) destsize)
\r
51 (uffi:with-cstring (source-native source)
\r
52 (let ((result (c-compress dest destlen source-native sourcelen))
\r
53 (newdestlen (uffi:deref-pointer destlen :long)))
\r
56 (values (uffi:convert-from-foreign-string
\r
59 :null-terminated-p nil)
\r
61 (error "zlib error, code ~D" result))
\r
63 (uffi:free-foreign-object destlen)
\r
64 (uffi:free-foreign-object dest)))))))
\r
66 (uffi:def-function ("uncompress" c-uncompress)
\r
67 ((dest (* :unsigned-char))
\r
74 (defun uncompress (source)
\r
75 (let* ((sourcelen (length source))
\r
76 (destsize 200000) ;adjust as needed
\r
77 (dest (uffi:allocate-foreign-string destsize :unsigned t))
\r
78 (destlen (uffi:allocate-foreign-object :long)))
\r
79 (setf (uffi:deref-pointer destlen :long) destsize)
\r
80 (uffi:with-cstring (source-native source)
\r
81 (let ((result (c-uncompress dest destlen source-native sourcelen))
\r
82 (newdestlen (uffi:deref-pointer destlen :long)))
\r
85 (uffi:convert-from-foreign-string
\r
88 :null-terminated-p nil)
\r
89 (error "zlib error, code ~D" result))
\r
91 (uffi:free-foreign-object destlen)
\r
92 (uffi:free-foreign-object dest)))))))
\r
94 (deftest compress.1 (map 'list #'char-code (compress ""))
\r
95 (120 156 3 0 0 0 0 1))
\r
96 (deftest compress.2 (map 'list #'char-code (compress "test"))
\r
97 (120 156 43 73 45 46 1 0 4 93 1 193))
\r
98 (deftest compress.3 (map 'list #'char-code (compress "test2"))
\r
99 (120 156 43 73 45 46 49 2 0 6 80 1 243))
\r
101 (defun compress-uncompress (str)
\r
102 (multiple-value-bind (compressed len) (compress str)
\r
103 (multiple-value-bind (uncompressed len2) (uncompress compressed)
\r
107 (deftest uncompress.1 "" "")
\r
108 (deftest uncompress.2 "test" "test")
\r
109 (deftest uncompress.3 "test2" "test2")
\r