1 /*************************************************************************************************
2 * System-dependent configurations of Tokyo Cabinet
3 * Copyright (C) 2006-2008 Mikio Hirabayashi
4 * This file is part of Tokyo Cabinet.
5 * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
6 * the GNU Lesser General Public License as published by the Free Software Foundation; either
7 * version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
8 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10 * License for more details.
11 * You should have received a copy of the GNU Lesser General Public License along with Tokyo
12 * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
13 * Boston, MA 02111-1307 USA.
14 *************************************************************************************************/
21 /*************************************************************************************************
23 *************************************************************************************************/
26 int _tc_dummyfunc(void){
31 int _tc_dummyfuncv(int a, ...){
37 /*************************************************************************************************
39 *************************************************************************************************/
47 #define ZLIBBUFSIZ 8192
50 static char *_tc_deflate_impl(const char *ptr, int size, int *sp, int mode);
51 static char *_tc_inflate_impl(const char *ptr, int size, int *sp, int mode);
52 static unsigned int _tc_getcrc_impl(const char *ptr, int size);
55 char *(*_tc_deflate)(const char *, int, int *, int) = _tc_deflate_impl;
56 char *(*_tc_inflate)(const char *, int, int *, int) = _tc_inflate_impl;
57 unsigned int (*_tc_getcrc)(const char *, int) = _tc_getcrc_impl;
60 static char *_tc_deflate_impl(const char *ptr, int size, int *sp, int mode){
61 assert(ptr && size >= 0 && sp);
64 unsigned char obuf[ZLIBBUFSIZ];
65 int rv, asiz, bsiz, osiz;
71 if(deflateInit2(&zs, 5, Z_DEFLATED, -15, 7, Z_DEFAULT_STRATEGY) != Z_OK)
75 if(deflateInit2(&zs, 6, Z_DEFLATED, 15 + 16, 9, Z_DEFAULT_STRATEGY) != Z_OK)
79 if(deflateInit2(&zs, 6, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY) != Z_OK)
84 if(asiz < ZLIBBUFSIZ) asiz = ZLIBBUFSIZ;
85 if(!(buf = malloc(asiz))){
90 zs.next_in = (unsigned char *)ptr;
93 zs.avail_out = ZLIBBUFSIZ;
94 while((rv = deflate(&zs, Z_FINISH)) == Z_OK){
95 osiz = ZLIBBUFSIZ - zs.avail_out;
96 if(bsiz + osiz > asiz){
97 asiz = asiz * 2 + osiz;
98 if(!(swap = realloc(buf, asiz))){
105 memcpy(buf + bsiz, obuf, osiz);
108 zs.avail_out = ZLIBBUFSIZ;
110 if(rv != Z_STREAM_END){
115 osiz = ZLIBBUFSIZ - zs.avail_out;
116 if(bsiz + osiz + 1 > asiz){
117 asiz = asiz * 2 + osiz;
118 if(!(swap = realloc(buf, asiz))){
125 memcpy(buf + bsiz, obuf, osiz);
128 if(mode == _TCZMRAW) bsiz++;
135 static char *_tc_inflate_impl(const char *ptr, int size, int *sp, int mode){
136 assert(ptr && size >= 0 && sp);
139 unsigned char obuf[ZLIBBUFSIZ];
140 int rv, asiz, bsiz, osiz;
146 if(inflateInit2(&zs, -15) != Z_OK) return NULL;
149 if(inflateInit2(&zs, 15 + 16) != Z_OK) return NULL;
152 if(inflateInit2(&zs, 15) != Z_OK) return NULL;
155 asiz = size * 2 + 16;
156 if(asiz < ZLIBBUFSIZ) asiz = ZLIBBUFSIZ;
157 if(!(buf = malloc(asiz))){
162 zs.next_in = (unsigned char *)ptr;
165 zs.avail_out = ZLIBBUFSIZ;
166 while((rv = inflate(&zs, Z_NO_FLUSH)) == Z_OK){
167 osiz = ZLIBBUFSIZ - zs.avail_out;
168 if(bsiz + osiz >= asiz){
169 asiz = asiz * 2 + osiz;
170 if(!(swap = realloc(buf, asiz))){
177 memcpy(buf + bsiz, obuf, osiz);
180 zs.avail_out = ZLIBBUFSIZ;
182 if(rv != Z_STREAM_END){
187 osiz = ZLIBBUFSIZ - zs.avail_out;
188 if(bsiz + osiz >= asiz){
189 asiz = asiz * 2 + osiz;
190 if(!(swap = realloc(buf, asiz))){
197 memcpy(buf + bsiz, obuf, osiz);
206 static unsigned int _tc_getcrc_impl(const char *ptr, int size){
207 assert(ptr && size >= 0);
208 int crc = crc32(0, Z_NULL, 0);
209 return crc32(crc, (unsigned char *)ptr, size);
216 char *(*_tc_deflate)(const char *, int, int *, int) = NULL;
217 char *(*_tc_inflate)(const char *, int, int *, int) = NULL;
218 unsigned int (*_tc_getcrc)(const char *, int) = NULL;