| 1 |
;;; |
| 2 |
;;; Gauche-zlib - zlib module |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2006 Rui Ueyama, All rights reserved. |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
#!no-fold-case |
| 35 |
|
| 36 |
(define-module rfc.zlib |
| 37 |
(use gauche.uvector) |
| 38 |
(export zlib-version adler32 crc32 |
| 39 |
open-deflating-port open-inflating-port |
| 40 |
deflate-string inflate-string |
| 41 |
<zlib-error> <zlib-need-dict-error> |
| 42 |
<zlib-stream-error> <zlib-data-error> |
| 43 |
<zlib-memory-error> <zlib-version-error> |
| 44 |
<deflating-port> <inflating-port> |
| 45 |
deflating-port-full-flush |
| 46 |
zstream-total-in zstream-total-out |
| 47 |
zstream-params-set! |
| 48 |
zstream-adler32 |
| 49 |
zstream-data-type |
| 50 |
zstream-dictionary-adler32 |
| 51 |
gzip-encode-string gzip-decode-string |
| 52 |
inflate-sync |
| 53 |
Z_NO_COMPRESSION Z_BEST_SPEED |
| 54 |
Z_BEST_COMPRESSION Z_DEFAULT_COMPRESSION |
| 55 |
Z_FILTERED Z_HUFFMAN_ONLY |
| 56 |
Z_RLE Z_DEFAULT_STRATEGY |
| 57 |
Z_BINARY Z_ASCII Z_UNKNOWN |
| 58 |
)) |
| 59 |
(select-module rfc.zlib) |
| 60 |
|
| 61 |
(inline-stub |
| 62 |
(declcode |
| 63 |
(.include "gauche-zlib.h")) |
| 64 |
(initcode (Scm_Init_zlib)) |
| 65 |
|
| 66 |
(declare-stub-type <deflating-port> "ScmPort*" "deflating port" |
| 67 |
"SCM_DEFLATING_PORT_P" "SCM_PORT") |
| 68 |
(declare-stub-type <inflating-port> "ScmPort*" "inflating port" |
| 69 |
"SCM_INFLATING_PORT_P" "SCM_PORT") |
| 70 |
|
| 71 |
(.define SCM_XFLATING_PORT_P (x) (or (SCM_INFLATING_PORT_P x) |
| 72 |
(SCM_DEFLATING_PORT_P x))) |
| 73 |
|
| 74 |
;; proxy type for shorter code. <xflating-port> isn't really a Scheme class. |
| 75 |
(declare-stub-type <xflating-port> "ScmPort*" "inflating or deflating port" |
| 76 |
"SCM_XFLATING_PORT_P" "SCM_PORT") |
| 77 |
|
| 78 |
(define-cfn data_element (data::ScmObj |
| 79 |
start::(const unsigned char**) |
| 80 |
siz::int*) |
| 81 |
::void :static |
| 82 |
(cond [(SCM_U8VECTORP data) |
| 83 |
(set! (* start) (SCM_UVECTOR_ELEMENTS (SCM_U8VECTOR data)) |
| 84 |
(* siz) (SCM_U8VECTOR_SIZE (SCM_U8VECTOR data)))] |
| 85 |
[(SCM_STRINGP data) |
| 86 |
(let* ([b::(const ScmStringBody*) (SCM_STRING_BODY data)]) |
| 87 |
(set! (* start) (cast (unsigned char*) (SCM_STRING_BODY_START b)) |
| 88 |
(* siz) (SCM_STRING_BODY_SIZE b)))] |
| 89 |
[else |
| 90 |
(Scm_Error "u8vector or string required, but got: %S" data)])) |
| 91 |
|
| 92 |
(define-cproc zlib-version () |
| 93 |
(expr <top> (SCM_MAKE_STR (zlibVersion)))) |
| 94 |
|
| 95 |
(define-enum Z_NO_COMPRESSION) |
| 96 |
(define-enum Z_BEST_SPEED) |
| 97 |
(define-enum Z_BEST_COMPRESSION) |
| 98 |
(define-enum Z_DEFAULT_COMPRESSION) |
| 99 |
(define-enum Z_FILTERED) |
| 100 |
(define-enum Z_HUFFMAN_ONLY) |
| 101 |
(define-enum Z_RLE) |
| 102 |
(define-enum-conditionally Z_FIXED) |
| 103 |
(define-enum Z_DEFAULT_STRATEGY) |
| 104 |
(define-enum Z_BINARY) |
| 105 |
(define-enum-conditionally Z_TEXT) |
| 106 |
(define-enum Z_ASCII) |
| 107 |
(define-enum Z_UNKNOWN) |
| 108 |
|
| 109 |
(define-cproc adler32 (data :optional (adler::<ulong> 1)) ::<ulong> |
| 110 |
(let* ([start::(const unsigned char*)] |
| 111 |
[siz::int]) |
| 112 |
(data_element data (& start) (& siz)) |
| 113 |
(return (adler32 adler start siz)))) |
| 114 |
|
| 115 |
(define-cproc crc32 (data :optional (crc::<ulong> 0)) ::<ulong> |
| 116 |
(let* ([start::(const unsigned char*)] |
| 117 |
[siz::int]) |
| 118 |
(data_element data (& start) (& siz)) |
| 119 |
(return (crc32 crc start siz)))) |
| 120 |
|
| 121 |
(define-cproc %open-deflating-port (source::<output-port> |
| 122 |
compression-level::<fixnum> |
| 123 |
window-bits::<fixnum> |
| 124 |
memory-level::<fixnum> |
| 125 |
strategy::<fixnum> |
| 126 |
dictionary |
| 127 |
buffer-size::<fixnum> |
| 128 |
owner?) |
| 129 |
(return (Scm_MakeDeflatingPort source compression-level window-bits |
| 130 |
memory-level strategy dictionary |
| 131 |
buffer-size (not (SCM_FALSEP owner?))))) |
| 132 |
|
| 133 |
(define-cproc open-inflating-port (sink::<input-port> |
| 134 |
:key (buffer-size::<fixnum> 0) |
| 135 |
(window-bits::<fixnum> 15) |
| 136 |
(dictionary #f) |
| 137 |
(owner? #f)) |
| 138 |
(return (Scm_MakeInflatingPort sink buffer-size window-bits dictionary |
| 139 |
(not (SCM_FALSEP owner?))))) |
| 140 |
|
| 141 |
(define-cproc zstream-total-in (port::<xflating-port>) ::<ulong> |
| 142 |
(return (-> (SCM_PORT_ZSTREAM port) total-in))) |
| 143 |
|
| 144 |
(define-cproc zstream-total-out (port::<xflating-port>) ::<ulong> |
| 145 |
(return (-> (SCM_PORT_ZSTREAM port) total-out))) |
| 146 |
|
| 147 |
(define-cproc zstream-params-set! (port::<deflating-port> |
| 148 |
:key (compression-level #f) (strategy #f)) |
| 149 |
::<void> |
| 150 |
(let* ([info::ScmZlibInfo* (SCM_PORT_ZLIB_INFO port)] |
| 151 |
[strm::z_streamp (SCM_PORT_ZSTREAM port)] |
| 152 |
[lv::int 0] |
| 153 |
[st::int 0]) |
| 154 |
(cond |
| 155 |
[(SCM_FALSEP compression-level) (set! lv (-> info level))] |
| 156 |
[(SCM_INTP compression-level) (set! lv (SCM_INT_VALUE compression-level))] |
| 157 |
[else (SCM_TYPE_ERROR compression-level "fixnum or #f")]) |
| 158 |
(cond |
| 159 |
[(SCM_FALSEP strategy) (set! st (-> info strategy))] |
| 160 |
[(SCM_INTP strategy) (set! st (SCM_INT_VALUE strategy))] |
| 161 |
[else (SCM_TYPE_ERROR strategy "fixnum or #f")]) |
| 162 |
(let* ([r::int (deflateParams strm lv st)]) |
| 163 |
(unless (== r Z_OK) |
| 164 |
(Scm_ZlibError r "deflateParams failed: %s" (-> strm msg)))))) |
| 165 |
|
| 166 |
(define-cproc deflating-port-full-flush (port::<deflating-port>) ::<void> |
| 167 |
(set! (-> (SCM_PORT_ZLIB_INFO port) flush) Z_FULL_FLUSH) |
| 168 |
(Scm_Flush port)) |
| 169 |
|
| 170 |
(define-cproc zstream-adler32 (port::<deflating-port>) ::<ulong> |
| 171 |
(return (-> (SCM_PORT_ZSTREAM port) adler))) |
| 172 |
|
| 173 |
(define-cproc zstream-data-type (port::<deflating-port>) ::<int> |
| 174 |
(return (-> (SCM_PORT_ZSTREAM port) data_type))) |
| 175 |
|
| 176 |
(define-cproc zstream-dictionary-adler32 (port::<xflating-port>) |
| 177 |
(return (-> (SCM_PORT_ZLIB_INFO port) dict_adler))) |
| 178 |
|
| 179 |
(define-cproc inflate-sync (port::<inflating-port>) Scm_InflateSync) |
| 180 |
) |
| 181 |
|
| 182 |
|
| 183 |
(export-if-defined Z_TEXT Z_FIXED) |
| 184 |
|
| 185 |
;; body |
| 186 |
(define (open-deflating-port source |
| 187 |
:key (compression-level Z_DEFAULT_COMPRESSION) |
| 188 |
(window-bits 15) |
| 189 |
(memory-level 8) |
| 190 |
(strategy Z_DEFAULT_STRATEGY) |
| 191 |
(dictionary #f) |
| 192 |
(buffer-size 0) |
| 193 |
(owner? #f)) |
| 194 |
(%open-deflating-port source compression-level |
| 195 |
window-bits memory-level |
| 196 |
strategy dictionary |
| 197 |
buffer-size owner?)) |
| 198 |
|
| 199 |
;; utility procedures |
| 200 |
(define (deflate-string str . args) |
| 201 |
(call-with-output-string |
| 202 |
(^p (let1 p2 (apply open-deflating-port p args) |
| 203 |
(display str p2) |
| 204 |
(close-output-port p2))))) |
| 205 |
|
| 206 |
(define (inflate-string str . args) |
| 207 |
(port->string (apply open-inflating-port (open-input-string str) args))) |
| 208 |
|
| 209 |
(define (gzip-encode-string str . args) |
| 210 |
(call-with-output-string |
| 211 |
(^p (let1 p2 (apply open-deflating-port p :window-bits (+ 15 16) args) |
| 212 |
(display str p2) |
| 213 |
(close-output-port p2))))) |
| 214 |
|
| 215 |
(define (gzip-decode-string str . args) |
| 216 |
(port->string (apply open-inflating-port |
| 217 |
(open-input-string str) |
| 218 |
:window-bits (+ 15 16) |
| 219 |
args))) |