1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
/*\ 
||| This file a part of Pike, and is copyright by Fredrik Hubinette 
||| Pike is distributed as GPL (General Public License) 
||| See the files COPYING and DISCLAIMER for more information. 
\*/ 
/**/ 
#include "global.h" 
RCSID("$Id: zlibmod.c,v 1.32 2000/12/01 08:09:57 hubbe Exp $"); 
 
#include "zlib_machine.h" 
 
#if !defined(HAVE_LIBZ) && !defined(HAVE_LIBGZ) 
#undef HAVE_ZLIB_H 
#endif 
 
#ifdef HAVE_ZLIB_H 
 
#include "interpret.h" 
#include "svalue.h" 
#include "stralloc.h" 
#include "array.h" 
#include "pike_macros.h" 
#include "program.h" 
#include "stralloc.h" 
#include "object.h" 
#include "pike_types.h" 
#include "threads.h" 
#include "dynamic_buffer.h" 
 
#include <zlib.h> 
 
/* This must be included last! */ 
#include "module_magic.h" 
 
struct zipper 
{ 
  struct z_stream_s gz; 
#ifdef _REENTRANT 
  DEFINE_MUTEX(lock); 
#endif /* _REENTRANT */ 
}; 
 
#define BUF 32768 
#define MAX_BUF     (64*BUF) 
 
#undef THIS 
#define THIS ((struct zipper *)(Pike_fp->current_storage)) 
 
static void gz_deflate_create(INT32 args) 
{ 
  int level=Z_DEFAULT_COMPRESSION; 
 
  if(THIS->gz.state) 
  { 
/*     mt_lock(& THIS->lock); */ 
    deflateEnd(&THIS->gz); 
/*     mt_unlock(& THIS->lock); */ 
  } 
 
  if(args) 
  { 
    if(sp[-args].type != T_INT) 
      Pike_error("Bad argument 1 to gz->create()\n"); 
    level=sp[-args].u.integer; 
    if(level < Z_NO_COMPRESSION || 
       level > Z_BEST_COMPRESSION) 
    { 
      Pike_error("Compression level out of range for gz_deflate->create()\n"); 
    } 
  } 
 
  THIS->gz.zalloc=Z_NULL; 
  THIS->gz.zfree=Z_NULL; 
  THIS->gz.opaque=(void *)THIS; 
 
  pop_n_elems(args); 
/*   mt_lock(& THIS->lock); */ 
  level=deflateInit(&THIS->gz, level); 
/*   mt_unlock(& THIS->lock); */ 
  switch(level) 
  { 
  case Z_OK: 
    return; 
 
  case Z_VERSION_ERROR: 
    Pike_error("libz not compatible with zlib.h!!!\n"); 
    break; 
 
  default: 
    if(THIS->gz.msg) 
      Pike_error("Failed to initialize gz_deflate: %s\n",THIS->gz.msg); 
    else 
      Pike_error("Failed to initialize gz_deflate\n"); 
  } 
} 
 
static int do_deflate(dynamic_buffer *buf, 
                      struct zipper *this, 
                      int flush) 
{ 
   int ret=0; 
 
   THREADS_ALLOW(); 
   mt_lock(& this->lock); 
   THREADS_DISALLOW(); 
   if(!this->gz.state) 
      ret=Z_STREAM_ERROR; 
   else 
      do 
      { 
         this->gz.next_out=low_make_buf_space( 
            /* recommended by the zlib people */ 
            (this->gz.avail_out = 
             this->gz.avail_in+this->gz.avail_in/1000+42), 
            buf); 
 
         THREADS_ALLOW(); 
         ret=deflate(& this->gz, flush); 
         THREADS_DISALLOW(); 
 
         /* Absorb any unused space /Hubbe */ 
         low_make_buf_space(-((ptrdiff_t)this->gz.avail_out), buf); 
 
         /* we don't care about Z_BUF_ERROR here; it won't happen. */ 
      } 
      while (ret==Z_OK && flush==Z_FINISH); 
 
   mt_unlock(& this->lock); 
   return ret; 
} 
 
static void gz_deflate(INT32 args) 
{ 
  struct pike_string *data; 
  int flush, fail; 
  struct zipper *this=THIS; 
  dynamic_buffer buf; 
 
  if(!THIS->gz.state) 
    Pike_error("gz_deflate not initialized or destructed\n"); 
 
  if(args<1) 
    Pike_error("Too few arguments to gz_deflate->deflate()\n"); 
 
  if(sp[-args].type != T_STRING) 
    Pike_error("Bad argument 1 to gz_deflate->deflate()\n"); 
 
  data=sp[-args].u.string; 
 
  if(args>1) 
  { 
    if(sp[1-args].type != T_INT) 
      Pike_error("Bad argument 2 to gz_deflate->deflate()\n"); 
     
    flush=sp[1-args].u.integer; 
 
    switch(flush) 
    { 
    case Z_PARTIAL_FLUSH: 
    case Z_FINISH: 
    case Z_SYNC_FLUSH: 
    case Z_NO_FLUSH: 
      break; 
 
    default: 
      Pike_error("Argument 2 to gz_deflate->deflate() out of range.\n"); 
    } 
  }else{ 
    flush=Z_FINISH; 
  } 
 
  this->gz.next_in=(Bytef *)data->str; 
  this->gz.avail_in = DO_NOT_WARN(data->len); 
 
  initialize_buf(&buf); 
 
  fail=do_deflate(&buf,this,flush); 
  pop_n_elems(args); 
 
  if(fail != Z_OK && fail != Z_STREAM_END) 
  { 
    free(buf.s.str); 
    if(THIS->gz.msg) 
      Pike_error("Error in gz_deflate->deflate(): %s\n",THIS->gz.msg); 
    else 
      Pike_error("Error in gz_deflate->deflate(): %d\n",fail); 
  } 
 
  push_string(low_free_buf(&buf)); 
} 
 
 
static void init_gz_deflate(struct object *o) 
{ 
  mt_init(& THIS->lock); 
  MEMSET(& THIS->gz, 0, sizeof(THIS->gz)); 
  THIS->gz.zalloc=Z_NULL; 
  THIS->gz.zfree=Z_NULL; 
  THIS->gz.opaque=(void *)THIS; 
  deflateInit(& THIS->gz, Z_DEFAULT_COMPRESSION); 
} 
 
static void exit_gz_deflate(struct object *o) 
{ 
/*   mt_lock(& THIS->lock); */ 
  deflateEnd(&THIS->gz); 
/*   mt_unlock(& THIS->lock); */ 
  mt_destroy( & THIS->lock ); 
} 
 
/*******************************************************************/ 
 
 
static void gz_inflate_create(INT32 args) 
{ 
  int tmp; 
  if(THIS->gz.state) 
  { 
/*     mt_lock(& THIS->lock); */ 
    inflateEnd(&THIS->gz); 
/*     mt_unlock(& THIS->lock); */ 
  } 
 
 
  THIS->gz.zalloc=Z_NULL; 
  THIS->gz.zfree=Z_NULL; 
  THIS->gz.opaque=(void *)THIS; 
 
  pop_n_elems(args); 
/*    mt_lock(& THIS->lock);  */ 
  tmp=inflateInit(& THIS->gz); 
/*    mt_unlock(& THIS->lock); */ 
  switch(tmp) 
  { 
  case Z_OK: 
    return; 
 
  case Z_VERSION_ERROR: 
    Pike_error("libz not compatible with zlib.h!!!\n"); 
    break; 
 
  default: 
    if(THIS->gz.msg) 
      Pike_error("Failed to initialize gz_inflate: %s\n",THIS->gz.msg); 
    else 
      Pike_error("Failed to initialize gz_inflate\n"); 
  } 
} 
 
static int do_inflate(dynamic_buffer *buf, 
                      struct zipper *this, 
                      int flush) 
{ 
  int fail=0; 
  THREADS_ALLOW(); 
  mt_lock(& this->lock); 
  THREADS_DISALLOW(); 
  if(!this->gz.state) 
  { 
    fail=Z_STREAM_ERROR; 
  }else{ 
    do 
    { 
      char *loc; 
      int ret; 
      loc=low_make_buf_space(BUF,buf); 
      THREADS_ALLOW(); 
      this->gz.next_out=(Bytef *)loc; 
      this->gz.avail_out=BUF; 
      ret=inflate(& this->gz, flush); 
      THREADS_DISALLOW(); 
      low_make_buf_space(-((ptrdiff_t)this->gz.avail_out), buf); 
      if(ret != Z_OK) 
      { 
        fail=ret; 
        break; 
      } 
    } while(!this->gz.avail_out || flush==Z_FINISH || this->gz.avail_in); 
  } 
  mt_unlock(& this->lock); 
  return fail; 
} 
 
static void gz_inflate(INT32 args) 
{ 
  struct pike_string *data; 
  int fail; 
  struct zipper *this=THIS; 
  dynamic_buffer buf; 
 
  if(!THIS->gz.state) 
    Pike_error("gz_inflate not initialized or destructed\n"); 
 
  if(args<1) 
    Pike_error("Too few arguments to gz_inflate->inflate()\n"); 
 
  if(sp[-args].type != T_STRING) 
    Pike_error("Bad argument 1 to gz_inflate->inflate()\n"); 
 
  data=sp[-args].u.string; 
 
  this->gz.next_in=(Bytef *)data->str; 
  this->gz.avail_in = DO_NOT_WARN(data->len); 
 
  initialize_buf(&buf); 
 
  fail=do_inflate(&buf,this,Z_PARTIAL_FLUSH); 
  pop_n_elems(args); 
 
  if(fail != Z_OK && fail != Z_STREAM_END) 
  { 
    free(buf.s.str); 
    if(THIS->gz.msg) 
      Pike_error("Error in gz_inflate->inflate(): %s\n",THIS->gz.msg); 
    else 
      Pike_error("Error in gz_inflate->inflate(): %d\n",fail); 
  } 
  push_string(low_free_buf(&buf)); 
  if(fail != Z_STREAM_END && fail!=Z_OK && !sp[-1].u.string->len) 
  { 
    pop_stack(); 
    push_int(0); 
  } 
} 
 
static void init_gz_inflate(struct object *o) 
{ 
  mt_init(& THIS->lock); 
  MEMSET(& THIS->gz, 0, sizeof(THIS->gz)); 
  THIS->gz.zalloc=Z_NULL; 
  THIS->gz.zfree=Z_NULL; 
  THIS->gz.opaque=0; 
  inflateInit(&THIS->gz); 
  inflateEnd(&THIS->gz); 
} 
 
static void exit_gz_inflate(struct object *o) 
{ 
/*   mt_lock(& THIS->lock); */ 
  inflateEnd(& THIS->gz); 
/*   mt_unlock(& THIS->lock); */ 
  mt_destroy( & THIS->lock ); 
} 
 
 
static void gz_crc32(INT32 args) 
{ 
   unsigned INT32 crc; 
   if (!args || 
       sp[-args].type!=T_STRING) 
      Pike_error("Gz.crc32: illegal or missing argument 1 (expected string)\n"); 
 
   if (args>1) { 
      if (sp[1-args].type!=T_INT) 
         Pike_error("Gz.crc32: illegal argument 2 (expected integer)\n"); 
      else 
         crc=(unsigned INT32)sp[1-args].u.integer; 
   } else 
      crc=0; 
          
   crc=crc32(crc, 
             (unsigned char*)sp[-args].u.string->str, 
             DO_NOT_WARN(sp[-args].u.string->len)); 
 
   pop_n_elems(args); 
   push_int((INT32)crc); 
} 
 
 
#endif 
 
void pike_module_exit(void) {} 
 
void pike_module_init(void) 
{ 
#ifdef HAVE_ZLIB_H 
  start_new_program(); 
  ADD_STORAGE(struct zipper); 
   
  /* function(int|void:void) */ 
  ADD_FUNCTION("create",gz_deflate_create,tFunc(tOr(tInt,tVoid),tVoid),0); 
  /* function(string,int|void:string) */ 
  ADD_FUNCTION("deflate",gz_deflate,tFunc(tStr tOr(tInt,tVoid),tStr),0); 
 
  add_integer_constant("NO_FLUSH",Z_NO_FLUSH,0); 
  add_integer_constant("PARTIAL_FLUSH",Z_PARTIAL_FLUSH,0); 
  add_integer_constant("SYNC_FLUSH",Z_SYNC_FLUSH,0); 
  add_integer_constant("FINISH",Z_FINISH,0); 
 
  set_init_callback(init_gz_deflate); 
  set_exit_callback(exit_gz_deflate); 
 
  end_class("deflate",0); 
 
  start_new_program(); 
  ADD_STORAGE(struct zipper); 
   
  /* function(int|void:void) */ 
  ADD_FUNCTION("create",gz_inflate_create,tFunc(tOr(tInt,tVoid),tVoid),0); 
  /* function(string:string) */ 
  ADD_FUNCTION("inflate",gz_inflate,tFunc(tStr,tStr),0); 
 
  add_integer_constant("NO_FLUSH",Z_NO_FLUSH,0); 
  add_integer_constant("PARTIAL_FLUSH",Z_PARTIAL_FLUSH,0); 
  add_integer_constant("SYNC_FLUSH",Z_SYNC_FLUSH,0); 
  add_integer_constant("FINISH",Z_FINISH,0); 
 
  set_init_callback(init_gz_inflate); 
  set_exit_callback(exit_gz_inflate); 
 
  end_class("inflate",0); 
 
  add_integer_constant("NO_FLUSH",Z_NO_FLUSH,0); 
  add_integer_constant("PARTIAL_FLUSH",Z_PARTIAL_FLUSH,0); 
  add_integer_constant("SYNC_FLUSH",Z_SYNC_FLUSH,0); 
  add_integer_constant("FINISH",Z_FINISH,0); 
 
  /* function(string,void|int:int) */ 
  ADD_FUNCTION("crc32",gz_crc32,tFunc(tStr tOr(tVoid,tInt),tInt), 
               OPT_TRY_OPTIMIZE); 
 
#endif 
} 
 
#if defined(HAVE___VTBL__9TYPE_INFO) || defined(HAVE___T_9__NOTHROW) 
/* Super-special kluge for IRIX 6.3 */ 
#ifdef HAVE___VTBL__9TYPE_INFO 
extern void __vtbl__9type_info(void); 
#endif /* HAVE___VTBL__9TYPE_INFO */ 
#ifdef HAVE___T_9__NOTHROW 
extern void __T_9__nothrow(void); 
#endif /* HAVE___T_9__NOTHROW */ 
/* Don't even think of calling this one 
 * Not static, so the compiler can't optimize it away totally. 
 */ 
void zlibmod_strap_kluge(void) 
{ 
#ifdef HAVE___VTBL__9TYPE_INFO 
  __vtbl__9type_info(); 
#endif /* HAVE___VTBL__9TYPE_INFO */ 
#ifdef HAVE___T_9__NOTHROW 
  __T_9__nothrow(); 
#endif /* HAVE___T_9__NOTHROW */ 
} 
#endif /* HAVE___VTBL__9TYPE_INFO || HAVE___T_9__NOTHROW */