]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/tokyocabinet/myconf.c
ebl Add tokyocabinet source to bacula
[bacula/bacula] / bacula / src / lib / tokyocabinet / myconf.c
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  *************************************************************************************************/
15
16
17 #include "myconf.h"
18
19
20
21 /*************************************************************************************************
22  * common settings
23  *************************************************************************************************/
24
25
26 int _tc_dummyfunc(void){
27   return 0;
28 }
29
30
31 int _tc_dummyfuncv(int a, ...){
32   return 0;
33 }
34
35
36
37 /*************************************************************************************************
38  * for ZLIB
39  *************************************************************************************************/
40
41
42 #if TCUSEZLIB
43
44
45 #include <zlib.h>
46
47 #define ZLIBBUFSIZ     8192
48
49
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);
53
54
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;
58
59
60 static char *_tc_deflate_impl(const char *ptr, int size, int *sp, int mode){
61   assert(ptr && size >= 0 && sp);
62   z_stream zs;
63   char *buf, *swap;
64   unsigned char obuf[ZLIBBUFSIZ];
65   int rv, asiz, bsiz, osiz;
66   zs.zalloc = Z_NULL;
67   zs.zfree = Z_NULL;
68   zs.opaque = Z_NULL;
69   switch(mode){
70   case _TCZMRAW:
71     if(deflateInit2(&zs, 5, Z_DEFLATED, -15, 7, Z_DEFAULT_STRATEGY) != Z_OK)
72       return NULL;
73     break;
74   case _TCZMGZIP:
75     if(deflateInit2(&zs, 6, Z_DEFLATED, 15 + 16, 9, Z_DEFAULT_STRATEGY) != Z_OK)
76       return NULL;
77     break;
78   default:
79     if(deflateInit2(&zs, 6, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY) != Z_OK)
80       return NULL;
81     break;
82   }
83   asiz = size + 16;
84   if(asiz < ZLIBBUFSIZ) asiz = ZLIBBUFSIZ;
85   if(!(buf = malloc(asiz))){
86     deflateEnd(&zs);
87     return NULL;
88   }
89   bsiz = 0;
90   zs.next_in = (unsigned char *)ptr;
91   zs.avail_in = size;
92   zs.next_out = obuf;
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))){
99         free(buf);
100         deflateEnd(&zs);
101         return NULL;
102       }
103       buf = swap;
104     }
105     memcpy(buf + bsiz, obuf, osiz);
106     bsiz += osiz;
107     zs.next_out = obuf;
108     zs.avail_out = ZLIBBUFSIZ;
109   }
110   if(rv != Z_STREAM_END){
111     free(buf);
112     deflateEnd(&zs);
113     return NULL;
114   }
115   osiz = ZLIBBUFSIZ - zs.avail_out;
116   if(bsiz + osiz + 1 > asiz){
117     asiz = asiz * 2 + osiz;
118     if(!(swap = realloc(buf, asiz))){
119       free(buf);
120       deflateEnd(&zs);
121       return NULL;
122     }
123     buf = swap;
124   }
125   memcpy(buf + bsiz, obuf, osiz);
126   bsiz += osiz;
127   buf[bsiz] = '\0';
128   if(mode == _TCZMRAW) bsiz++;
129   *sp = bsiz;
130   deflateEnd(&zs);
131   return buf;
132 }
133
134
135 static char *_tc_inflate_impl(const char *ptr, int size, int *sp, int mode){
136   assert(ptr && size >= 0 && sp);
137   z_stream zs;
138   char *buf, *swap;
139   unsigned char obuf[ZLIBBUFSIZ];
140   int rv, asiz, bsiz, osiz;
141   zs.zalloc = Z_NULL;
142   zs.zfree = Z_NULL;
143   zs.opaque = Z_NULL;
144   switch(mode){
145   case _TCZMRAW:
146     if(inflateInit2(&zs, -15) != Z_OK) return NULL;
147     break;
148   case _TCZMGZIP:
149     if(inflateInit2(&zs, 15 + 16) != Z_OK) return NULL;
150     break;
151   default:
152     if(inflateInit2(&zs, 15) != Z_OK) return NULL;
153     break;
154   }
155   asiz = size * 2 + 16;
156   if(asiz < ZLIBBUFSIZ) asiz = ZLIBBUFSIZ;
157   if(!(buf = malloc(asiz))){
158     inflateEnd(&zs);
159     return NULL;
160   }
161   bsiz = 0;
162   zs.next_in = (unsigned char *)ptr;
163   zs.avail_in = size;
164   zs.next_out = obuf;
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))){
171         free(buf);
172         inflateEnd(&zs);
173         return NULL;
174       }
175       buf = swap;
176     }
177     memcpy(buf + bsiz, obuf, osiz);
178     bsiz += osiz;
179     zs.next_out = obuf;
180     zs.avail_out = ZLIBBUFSIZ;
181   }
182   if(rv != Z_STREAM_END){
183     free(buf);
184     inflateEnd(&zs);
185     return NULL;
186   }
187   osiz = ZLIBBUFSIZ - zs.avail_out;
188   if(bsiz + osiz >= asiz){
189     asiz = asiz * 2 + osiz;
190     if(!(swap = realloc(buf, asiz))){
191       free(buf);
192       inflateEnd(&zs);
193       return NULL;
194     }
195     buf = swap;
196   }
197   memcpy(buf + bsiz, obuf, osiz);
198   bsiz += osiz;
199   buf[bsiz] = '\0';
200   *sp = bsiz;
201   inflateEnd(&zs);
202   return buf;
203 }
204
205
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);
210 }
211
212
213 #else
214
215
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;
219
220
221 #endif
222
223
224
225 // END OF FILE