10634a3ea0ece15d6b9897d38390a176f67244f0
[uffi.git] / examples / compress.lisp
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:  Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package :cl-user)
20
21 (eval-when (:load-toplevel :execute)
22   (unless (uffi:load-foreign-library
23            #-(or macosx darwin)
24            (uffi:find-foreign-library
25             "libz"
26             '("/usr/local/lib/" "/usr/lib/" "/zlib/")
27             :types '("so" "a"))
28            #+(or macosx darwin)
29            (uffi:find-foreign-library "z"
30                                       `(,(pathname-directory *load-pathname*)))
31            :module "zlib" 
32            :supporting-libraries '("c"))
33     (warn "Unable to load zlib")))
34   
35 (uffi:def-function ("compress" c-compress)
36     ((dest (* :unsigned-char))
37      (destlen (* :long))
38      (source :cstring)
39      (source-len :long))
40   :returning :int
41   :module "zlib")
42   
43 (defun compress (source)
44   "Returns two values: array of bytes containing the compressed data
45  and the numbe of compressed bytes"
46   (let* ((sourcelen (length source))
47          (destsize (+ 12 (ceiling (* sourcelen 1.01))))
48          (dest (uffi:allocate-foreign-string destsize :unsigned t))
49          (destlen (uffi:allocate-foreign-object :long)))
50     (setf (uffi:deref-pointer destlen :long) destsize)
51     (uffi:with-cstring (source-native source)
52       (let ((result (c-compress dest destlen source-native sourcelen))
53             (newdestlen (uffi:deref-pointer destlen :long)))
54         (unwind-protect
55             (if (zerop result)
56                 (values (uffi:convert-from-foreign-string 
57                          dest
58                          :length newdestlen
59                          :null-terminated-p nil)
60                         newdestlen)
61               (error "zlib error, code ~D" result))
62           (progn
63             (uffi:free-foreign-object destlen)
64             (uffi:free-foreign-object dest)))))))
65
66 (uffi:def-function ("uncompress" c-uncompress)
67     ((dest (* :unsigned-char))
68      (destlen (* :long))
69      (source :cstring)
70      (source-len :long))
71   :returning :int
72   :module "zlib")
73
74 (defun uncompress (source)
75   (let* ((sourcelen (length source))
76          (destsize 200000)  ;adjust as needed
77          (dest (uffi:allocate-foreign-string destsize :unsigned t))
78          (destlen (uffi:allocate-foreign-object :long)))
79     (setf (uffi:deref-pointer destlen :long) destsize)
80     (uffi:with-cstring (source-native source)
81       (let ((result (c-uncompress dest destlen source-native sourcelen))
82             (newdestlen (uffi:deref-pointer destlen :long)))
83         (unwind-protect
84              (if (zerop result)
85                  (uffi:convert-from-foreign-string 
86                   dest
87                   :length newdestlen
88                   :null-terminated-p nil)
89                  (error "zlib error, code ~D" result))
90           (progn
91             (uffi:free-foreign-object destlen)
92             (uffi:free-foreign-object dest)))))))
93
94 #+examples-uffi
95 (progn
96   (flet ((print-results (str)
97            (multiple-value-bind (compressed len) (compress str)
98              (let ((*print-length* nil))
99                (format t "~&(compress ~S) => " str)
100                (format t "~S~%" (map 'list #'char-code compressed))))))
101     (print-results "")
102     (print-results "test")
103     (print-results "test2")))
104
105 #+test-uffi
106 (progn
107   (flet ((test-compress (str)
108            (multiple-value-bind (compressed len) (compress str)
109              (multiple-value-bind (uncompressed len2) (uncompress compressed)
110                (util.test:test str uncompressed :test #'string=
111                                :fail-info "Error uncompressing a compressed string")))))
112     (test-compress "")
113     (test-compress "test")
114     (test-compress "test2")))
115
116 ;; Results of the above on my system:
117 ;; (compress "") => 789c300001,8
118 ;; (compress "test") => 789c2b492d2e1045d1c1,12
119 ;; (compress "test2") => 789c2b492d2e31206501f3,13