3b41eb9b6b3efcf3f57163811501007d674c6eb0
[uffi.git] / tests / compress.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          compress.cl
6 ;;;; Purpose:       UFFI Example file for zlib compression
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; Copyright (c) 2002 Kevin M. Rosenberg
11 ;;;;
12 ;;;; $Id: compress.cl,v 1.6 2002/03/10 20:01:55 kevin Exp $
13 ;;;;
14 ;;;; This file is part of UFFI. 
15 ;;;;
16 ;;;; UFFI is free software; you can redistribute it and/or modify
17 ;;;; it under the terms of the GNU General Public License (version 2) as
18 ;;;; published by the Free Software Foundation.
19 ;;;;
20 ;;;; UFFI is distributed in the hope that it will be useful,
21 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;;;; GNU General Public License for more details.
24 ;;;;
25 ;;;; You should have received a copy of the GNU General Public License
26 ;;;; along with UFFI; if not, write to the Free Software Foundation, Inc.,
27 ;;;; 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 ;;;; *************************************************************************
29
30 (in-package :cl-user)
31
32 (unless (uffi:load-foreign-library "/usr/lib/libz.so" 
33                                    :module "zlib" 
34                                    :supporting-libraries '("c"))
35   (warn "Unable to load zlib"))
36
37 (uffi:def-function ("compress" c-compress)
38     ((dest (* :unsigned-char))
39      (destlen (* :long))
40      (source :cstring)
41      (source-len :long))
42   :returning :int
43   :module "zlib")
44   
45 (defun compress (source)
46   "Returns two values: array of bytes containing the compressed data
47  and the numbe of compressed bytes"
48   (let* ((sourcelen (length source))
49          (destsize (+ 12 (ceiling (* sourcelen 1.01))))
50          (dest (uffi:allocate-foreign-string destsize :unsigned t))
51          (destlen (uffi:allocate-foreign-object :long)))
52     (setf (uffi:deref-pointer destlen :long) destsize)
53     (uffi:with-cstring (source-native source)
54       (let ((result (c-compress dest destlen source-native sourcelen))
55             (newdestlen (uffi:deref-pointer destlen :long)))
56         (unwind-protect
57             (if (zerop result)
58                 (values (uffi:convert-from-foreign-string 
59                          dest
60                          :length newdestlen
61                          :null-terminated-p nil)
62                         newdestlen)
63               (error "zlib error, code ~D" result))
64           (progn
65             (uffi:free-foreign-object destlen)
66             (uffi:free-foreign-object dest)))))))
67
68
69 #+test-uffi
70 (progn
71   (flet ((print-results (str)
72            (multiple-value-bind (compressed len) (compress str)
73                (format t "~&(compress ~S) => ~S,~D" str compressed len))))
74     (print-results "")
75     (print-results "test")
76     (print-results "test2")))