e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information.
644dc82009-08-28Martin Stjernholm || $Id: zlibmod.c,v 1.67 2009/08/28 13:30:53 mast Exp $
e576bb2002-10-11Martin Nilsson */
aedfb12002-10-09Martin Nilsson 
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #include "global.h"
644dc82009-08-28Martin Stjernholm RCSID("$Id: zlibmod.c,v 1.67 2009/08/28 13:30:53 mast Exp $");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  #include "zlib_machine.h"
51ef5c2002-10-21Marcus Comstedt #include "module.h"
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  #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"
bb55f81997-03-16Fredrik Hübinette (Hubbe) #include "pike_macros.h"
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #include "program.h" #include "stralloc.h" #include "object.h" #include "pike_types.h" #include "threads.h" #include "dynamic_buffer.h"
27ec272003-09-10Martin Stjernholm #include "operators.h"
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  #include <zlib.h>
2505872002-09-25Marcus Comstedt 
65bd332002-05-11Martin Stjernholm #define sp Pike_sp
ab6aec1997-02-11Fredrik Hübinette (Hubbe) struct zipper {
98a7e12002-04-07Leif Stensson  int level; int state;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  struct z_stream_s gz;
0a41162003-04-15Marcus Comstedt  struct pike_string *epilogue;
e0c81f1997-03-22Henrik Grubbström (Grubba) #ifdef _REENTRANT
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  DEFINE_MUTEX(lock);
e0c81f1997-03-22Henrik Grubbström (Grubba) #endif /* _REENTRANT */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) };
1d90b91997-11-11Henrik Grubbström (Grubba) #define BUF 32768
d7288e1997-11-11Henrik Grubbström (Grubba) #define MAX_BUF (64*BUF)
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
3568261998-05-07Fredrik Hübinette (Hubbe) #undef THIS
39221e2000-07-07Henrik Grubbström (Grubba) #define THIS ((struct zipper *)(Pike_fp->current_storage))
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6461ef2001-01-05Henrik Grubbström (Grubba) /*! @module Gz *! *! The Gz module contains functions to compress and uncompress strings using *! the same algorithm as the program @tt{gzip@}. Compressing can be done in *! streaming mode or all at once. *! *! The Gz module consists of two classes; Gz.deflate and Gz.inflate. *! Gz.deflate is used to pack data *! and Gz.inflate is used to unpack data. (Think "inflatable boat") *! *! @note *! Note that this module is only available if the gzip library was *! available when Pike was compiled. *! *! Note that although these functions use the same @i{algorithm@} as *! @tt{gzip@}, they do not use the exact same format, so you cannot directly *! unzip gzipped files with these routines. Support for this will be *! added in the future. */ /*! @class deflate *!
050d9c2001-11-18Martin Nilsson  *! Gz_deflate is a builtin program written in C. It interfaces the *! packing routines in the libz library. *! *! @note *! This program is only available if libz was available and found when *! Pike was compiled. *!
6461ef2001-01-05Henrik Grubbström (Grubba)  *! @seealso *! @[Gz.inflate()] */
b8f7652003-04-14Marcus Comstedt /*! @decl void create(int(0..9)|void level, int|void strategy)
6461ef2001-01-05Henrik Grubbström (Grubba)  *!
b8f7652003-04-14Marcus Comstedt  *! If given, @[level] should be a number from 0 to 9 indicating the
6461ef2001-01-05Henrik Grubbström (Grubba)  *! packing / CPU ratio. Zero means no packing, 2-3 is considered 'fast', *! 6 is default and higher is considered 'slow' but gives better packing. *! *! This function can also be used to re-initialize a Gz.deflate object *! so it can be re-used.
90c27b2003-03-31Per Hedbor  *! *! If the argument is negative, no headers will be emitted. This is *! needed to produce ZIP-files, as an example. The negative value is *! then negated, and handled as a positive value.
b8f7652003-04-14Marcus Comstedt  *! *! @[strategy], if given, should be one of DEFAULT_STRATEGY, FILTERED or *! HUFFMAN_ONLY.
6461ef2001-01-05Henrik Grubbström (Grubba)  */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) static void gz_deflate_create(INT32 args) {
90c27b2003-03-31Per Hedbor  int tmp, wbits = 15;
b8f7652003-04-14Marcus Comstedt  int strategy = Z_DEFAULT_STRATEGY;
e85aa92001-07-04Fredrik Hübinette (Hubbe)  THIS->level=Z_DEFAULT_COMPRESSION;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(THIS->gz.state) {
7260951997-09-01Per Hedbor /* mt_lock(& THIS->lock); */
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  deflateEnd(&THIS->gz);
7260951997-09-01Per Hedbor /* mt_unlock(& THIS->lock); */
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } if(args) { if(sp[-args].type != T_INT)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument 1 to gz->create()\n");
e85aa92001-07-04Fredrik Hübinette (Hubbe)  THIS->level=sp[-args].u.integer;
90c27b2003-03-31Per Hedbor  if( THIS->level < 0 ) { wbits = -wbits; THIS->level = -THIS->level; }
e85aa92001-07-04Fredrik Hübinette (Hubbe)  if(THIS->level < Z_NO_COMPRESSION || THIS->level > Z_BEST_COMPRESSION)
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Compression level out of range for gz_deflate->create()\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } }
b8f7652003-04-14Marcus Comstedt  if(args>1) { if(sp[1-args].type != T_INT) Pike_error("Bad argument 2 to gz->create()\n"); strategy=sp[1-args].u.integer; if(strategy != Z_DEFAULT_STRATEGY && strategy != Z_FILTERED && strategy != Z_HUFFMAN_ONLY) { Pike_error("Invalid compression strategy for gz_deflate->create()\n"); } }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  THIS->gz.zalloc=Z_NULL; THIS->gz.zfree=Z_NULL;
3164751998-04-30Henrik Grubbström (Grubba)  THIS->gz.opaque=(void *)THIS;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args);
7260951997-09-01Per Hedbor /* mt_lock(& THIS->lock); */
b8f7652003-04-14Marcus Comstedt  tmp=deflateInit2(&THIS->gz, THIS->level, Z_DEFLATED, wbits, 9, strategy );
7260951997-09-01Per Hedbor /* mt_unlock(& THIS->lock); */
e85aa92001-07-04Fredrik Hübinette (Hubbe)  switch(tmp)
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  { case Z_OK: return; case Z_VERSION_ERROR:
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("libz not compatible with zlib.h!!!\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  break; default: if(THIS->gz.msg)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Failed to initialize gz_deflate: %s\n",THIS->gz.msg);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  else
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Failed to initialize gz_deflate\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } }
644dc82009-08-28Martin Stjernholm static void do_mt_unlock (PIKE_MUTEX_T *lock) { mt_unlock (lock); }
ab6aec1997-02-11Fredrik Hübinette (Hubbe) static int do_deflate(dynamic_buffer *buf, struct zipper *this, int flush) {
b280771999-06-08Mirar (Pontus Hagland)  int ret=0;
644dc82009-08-28Martin Stjernholm  ONERROR uwp;
b280771999-06-08Mirar (Pontus Hagland)  THREADS_ALLOW(); mt_lock(& this->lock); THREADS_DISALLOW();
644dc82009-08-28Martin Stjernholm  SET_ONERROR (uwp, do_mt_unlock, &this->lock);
b280771999-06-08Mirar (Pontus Hagland)  if(!this->gz.state) ret=Z_STREAM_ERROR; else do
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  {
34f2262004-03-19Martin Nilsson  this->gz.next_out=(Bytef *)low_make_buf_space(
b280771999-06-08Mirar (Pontus Hagland)  /* recommended by the zlib people */ (this->gz.avail_out =
9189f32000-12-13Fredrik Hübinette (Hubbe)  this->gz.avail_in ? this->gz.avail_in+this->gz.avail_in/1000+42 : 4096),
b280771999-06-08Mirar (Pontus Hagland)  buf); THREADS_ALLOW(); ret=deflate(& this->gz, flush); THREADS_DISALLOW();
ffc0991999-07-15Fredrik Hübinette (Hubbe)  /* Absorb any unused space /Hubbe */
3919712000-08-17Henrik Grubbström (Grubba)  low_make_buf_space(-((ptrdiff_t)this->gz.avail_out), buf);
ffc0991999-07-15Fredrik Hübinette (Hubbe) 
3b13762001-02-15Fredrik Hübinette (Hubbe)  if(ret == Z_BUF_ERROR) ret=Z_OK;
7260951997-09-01Per Hedbor  }
9189f32000-12-13Fredrik Hübinette (Hubbe)  while (ret==Z_OK && (this->gz.avail_in || !this->gz.avail_out));
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
644dc82009-08-28Martin Stjernholm  CALL_AND_UNSET_ONERROR (uwp);
b280771999-06-08Mirar (Pontus Hagland)  return ret;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
6461ef2001-01-05Henrik Grubbström (Grubba) /*! @decl string deflate(string data, int|void flush) *! *! This function performs gzip style compression on a string @[data] and *! returns the packed data. Streaming can be done by calling this *! function several times and concatenating the returned data. *! *! The optional argument @[flush] should be one of the following: *! @int *! @value Gz.NO_FLUSH *! Only data that doesn't fit in the internal buffers is returned. *! @value Gz.PARTIAL_FLUSH *! All input is packed and returned. *! @value Gz.SYNC_FLUSH *! All input is packed and returned. *! @value Gz.FINISH *! All input is packed and an 'end of data' marker is appended. *! @endint *! *! @seealso *! @[Gz.inflate->inflate()] */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) static void gz_deflate(INT32 args) { struct pike_string *data; int flush, fail; struct zipper *this=THIS; dynamic_buffer buf;
a598cd2001-01-23Fredrik Hübinette (Hubbe)  ONERROR err;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
e85aa92001-07-04Fredrik Hübinette (Hubbe)  if(THIS->state == 1) { deflateEnd(& THIS->gz); deflateInit(& THIS->gz, THIS->level); THIS->state=0; }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(!THIS->gz.state)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("gz_deflate not initialized or destructed\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(args<1)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Too few arguments to gz_deflate->deflate()\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(sp[-args].type != T_STRING)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument 1 to gz_deflate->deflate()\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  data=sp[-args].u.string; if(args>1) { if(sp[1-args].type != T_INT)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument 2 to gz_deflate->deflate()\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  flush=sp[1-args].u.integer; switch(flush) { case Z_PARTIAL_FLUSH: case Z_FINISH: case Z_SYNC_FLUSH: case Z_NO_FLUSH: break;
8d36d51998-02-27Mirar (Pontus Hagland)  default:
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Argument 2 to gz_deflate->deflate() out of range.\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } }else{ flush=Z_FINISH; } this->gz.next_in=(Bytef *)data->str;
340c562001-06-13Henrik Grubbström (Grubba)  this->gz.avail_in = DO_NOT_WARN((unsigned INT32)(data->len));
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
2bb2221999-08-20Henrik Grubbström (Grubba)  initialize_buf(&buf);
a598cd2001-01-23Fredrik Hübinette (Hubbe)  SET_ONERROR(err,toss_buffer,&buf);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  fail=do_deflate(&buf,this,flush);
a598cd2001-01-23Fredrik Hübinette (Hubbe)  UNSET_ONERROR(err);
e85aa92001-07-04Fredrik Hübinette (Hubbe) 
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(fail != Z_OK && fail != Z_STREAM_END) {
a5b2c72001-01-24Henrik Grubbström (Grubba)  toss_buffer(&buf);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(THIS->gz.msg)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Error in gz_deflate->deflate(): %s\n",THIS->gz.msg);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  else
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Error in gz_deflate->deflate(): %d\n",fail);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
e85aa92001-07-04Fredrik Hübinette (Hubbe)  if(fail == Z_STREAM_END) THIS->state=1;
a5b2c72001-01-24Henrik Grubbström (Grubba)  pop_n_elems(args);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  push_string(low_free_buf(&buf)); } static void init_gz_deflate(struct object *o) {
2383021997-02-27Fredrik Hübinette (Hubbe)  mt_init(& THIS->lock);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  MEMSET(& THIS->gz, 0, sizeof(THIS->gz)); THIS->gz.zalloc=Z_NULL; THIS->gz.zfree=Z_NULL;
3164751998-04-30Henrik Grubbström (Grubba)  THIS->gz.opaque=(void *)THIS;
e85aa92001-07-04Fredrik Hübinette (Hubbe)  THIS->state=0; deflateInit(& THIS->gz, THIS->level = Z_DEFAULT_COMPRESSION);
0a41162003-04-15Marcus Comstedt  THIS->epilogue = NULL;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) } static void exit_gz_deflate(struct object *o) {
7260951997-09-01Per Hedbor /* mt_lock(& THIS->lock); */
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  deflateEnd(&THIS->gz);
0a41162003-04-15Marcus Comstedt  do_free_string(THIS->epilogue);
7260951997-09-01Per Hedbor /* mt_unlock(& THIS->lock); */
4e77fb1999-06-10Fredrik Hübinette (Hubbe)  mt_destroy( & THIS->lock );
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
6461ef2001-01-05Henrik Grubbström (Grubba) /*! @endclass */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) /*******************************************************************/
6461ef2001-01-05Henrik Grubbström (Grubba) /*! @class inflate
050d9c2001-11-18Martin Nilsson  *! *! Gz_deflate is a builtin program written in C. It interfaces the *! unpacking routines in the libz library. *! *! @note *! This program is only available if libz was available and found when *! Pike was compiled. *! *! @seealso *! @[deflate]
6461ef2001-01-05Henrik Grubbström (Grubba)  */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
90c27b2003-03-31Per Hedbor /*! @decl void create(int|void window_size)
04e3032003-03-30Martin Nilsson  *! @param magic
90c27b2003-03-31Per Hedbor  *! The window_size value is passed down to inflateInit2 in zlib. *! *! If the argument is negative, no header checks are done, and no *! verification of the data will be done either. This is needed for *! uncompressing ZIP-files, as an example. The negative value is then *! negated, and handled as a positive value. *! *! Positive arguments set the maximum dictionary size to an exponent *! of 2, such that 8 (the minimum) will cause the window size to be *! 256, and 15 (the maximum, and default value) will cause it to be *! 32Kb. Setting this to anything except 15 is rather pointless in *! Pike. *! *! It can be used to limit the amount of memory that is used to *! uncompress files, but 32Kb is not all that much in the great *! scheme of things. *! *! To decompress files compressed with level 9 compression, a 32Kb *! window size is needed. level 1 compression only requires a 256 *! byte window.
6461ef2001-01-05Henrik Grubbström (Grubba)  */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) static void gz_inflate_create(INT32 args) { int tmp; if(THIS->gz.state) {
7260951997-09-01Per Hedbor /* mt_lock(& THIS->lock); */
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  inflateEnd(&THIS->gz);
7260951997-09-01Per Hedbor /* mt_unlock(& THIS->lock); */
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } THIS->gz.zalloc=Z_NULL; THIS->gz.zfree=Z_NULL;
3164751998-04-30Henrik Grubbström (Grubba)  THIS->gz.opaque=(void *)THIS;
5ab86f2003-03-30Per Hedbor  if( args && Pike_sp[-1].type == PIKE_T_INT ) { tmp=inflateInit2(& THIS->gz, Pike_sp[-1].u.integer); } else { tmp=inflateInit( &THIS->gz ); }
04e3032003-03-30Martin Nilsson  pop_n_elems(args);
7260951997-09-01Per Hedbor /* mt_lock(& THIS->lock); */ /* mt_unlock(& THIS->lock); */
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  switch(tmp) { case Z_OK: return; case Z_VERSION_ERROR:
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("libz not compatible with zlib.h!!!\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  break; default: if(THIS->gz.msg)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Failed to initialize gz_inflate: %s\n",THIS->gz.msg);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  else
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Failed to initialize gz_inflate\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } } static int do_inflate(dynamic_buffer *buf, struct zipper *this, int flush) { int fail=0;
644dc82009-08-28Martin Stjernholm  ONERROR uwp;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  THREADS_ALLOW();
7260951997-09-01Per Hedbor  mt_lock(& this->lock);
cadacf1997-10-21Henrik Grubbström (Grubba)  THREADS_DISALLOW();
644dc82009-08-28Martin Stjernholm  SET_ONERROR (uwp, do_mt_unlock, &this->lock);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(!this->gz.state) { fail=Z_STREAM_ERROR; }else{
4951fc2001-02-08Fredrik Hübinette (Hubbe) #if 0 static int fnord=0; fnord++; #endif
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  do { char *loc; int ret; loc=low_make_buf_space(BUF,buf);
cadacf1997-10-21Henrik Grubbström (Grubba)  THREADS_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  this->gz.next_out=(Bytef *)loc; this->gz.avail_out=BUF;
4951fc2001-02-08Fredrik Hübinette (Hubbe) #if 0 fprintf(stderr,"INFLATE[%d]: avail_out=%7d avail_in=%7d flush=%d\n", fnord, this->gz.avail_out, this->gz.avail_in, flush); fprintf(stderr,"INFLATE[%d]: mode=%d\n",fnord, this->gz.state ? *(int *)(this->gz.state) : -1); #endif
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  ret=inflate(& this->gz, flush);
4951fc2001-02-08Fredrik Hübinette (Hubbe) #if 0 fprintf(stderr,"Result [%d]: avail_out=%7d avail_in=%7d ret=%d\n", fnord, this->gz.avail_out, this->gz.avail_in, ret); #endif
cadacf1997-10-21Henrik Grubbström (Grubba)  THREADS_DISALLOW();
3919712000-08-17Henrik Grubbström (Grubba)  low_make_buf_space(-((ptrdiff_t)this->gz.avail_out), buf);
4951fc2001-02-08Fredrik Hübinette (Hubbe)  if(ret == Z_BUF_ERROR) ret=Z_OK;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(ret != Z_OK)
7260951997-09-01Per Hedbor  { fail=ret; break; }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } while(!this->gz.avail_out || flush==Z_FINISH || this->gz.avail_in); }
644dc82009-08-28Martin Stjernholm  CALL_AND_UNSET_ONERROR (uwp);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  return fail; }
6461ef2001-01-05Henrik Grubbström (Grubba) /*! @decl string inflate(string data) *! *! This function performs gzip style decompression. It can inflate *! a whole file at once or in blocks. *!
050d9c2001-11-18Martin Nilsson  *! @example *! // whole file *! write(Gz_inflate()->inflate(stdin->read(0x7fffffff)); *! *! // streaming (blocks) *! function inflate=Gz_inflate()->inflate; *! while(string s=stdin->read(8192)) *! write(inflate(s)); *!
6461ef2001-01-05Henrik Grubbström (Grubba)  *! @seealso *! @[Gz.deflate->deflate()] */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) static void gz_inflate(INT32 args) { struct pike_string *data; int fail; struct zipper *this=THIS; dynamic_buffer buf;
a598cd2001-01-23Fredrik Hübinette (Hubbe)  ONERROR err;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(!THIS->gz.state)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("gz_inflate not initialized or destructed\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(args<1)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Too few arguments to gz_inflate->inflate()\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(sp[-args].type != T_STRING)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument 1 to gz_inflate->inflate()\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  data=sp[-args].u.string; this->gz.next_in=(Bytef *)data->str;
340c562001-06-13Henrik Grubbström (Grubba)  this->gz.avail_in = DO_NOT_WARN((unsigned INT32)(data->len));
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
2bb2221999-08-20Henrik Grubbström (Grubba)  initialize_buf(&buf);
a5b2c72001-01-24Henrik Grubbström (Grubba) 
a598cd2001-01-23Fredrik Hübinette (Hubbe)  SET_ONERROR(err,toss_buffer,&buf);
3b13762001-02-15Fredrik Hübinette (Hubbe)  fail=do_inflate(&buf,this,Z_SYNC_FLUSH);
a598cd2001-01-23Fredrik Hübinette (Hubbe)  UNSET_ONERROR(err);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(fail != Z_OK && fail != Z_STREAM_END) {
a5b2c72001-01-24Henrik Grubbström (Grubba)  toss_buffer(&buf);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(THIS->gz.msg)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Error in gz_inflate->inflate(): %s\n",THIS->gz.msg);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  else
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Error in gz_inflate->inflate(): %d\n",fail);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
e85aa92001-07-04Fredrik Hübinette (Hubbe) 
a5b2c72001-01-24Henrik Grubbström (Grubba)  pop_n_elems(args);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  push_string(low_free_buf(&buf));
0a41162003-04-15Marcus Comstedt  if(fail == Z_STREAM_END) { struct pike_string *old_epilogue = this->epilogue; if(old_epilogue) { push_string(old_epilogue); this->epilogue = NULL; }
34f2262004-03-19Martin Nilsson  push_string(make_shared_binary_string((const char *)this->gz.next_in,
0a41162003-04-15Marcus Comstedt  this->gz.avail_in)); if(old_epilogue) f_add(2); if(sp[-1].type == PIKE_T_STRING) this->epilogue = (--sp)->u.string; else pop_stack(); }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if(fail != Z_STREAM_END && fail!=Z_OK && !sp[-1].u.string->len) { pop_stack(); push_int(0); } }
0a41162003-04-15Marcus Comstedt /*! @decl string end_of_stream() *! *! This function returns 0 if the end of stream marker has not yet *! been encountered, or a string (possibly empty) containg any extra data *! received following the end of stream marker if the marker has been *! encountered. If the extra data is not needed, the result of this *! function can be treated as a logical value. */ static void gz_end_of_stream(INT32 args) { struct zipper *this=THIS; pop_n_elems(args); if(this->epilogue) ref_push_string(this->epilogue); else push_int(0); }
ab6aec1997-02-11Fredrik Hübinette (Hubbe) static void init_gz_inflate(struct object *o) {
77fe171997-02-27Fredrik Hübinette (Hubbe)  mt_init(& THIS->lock);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  MEMSET(& THIS->gz, 0, sizeof(THIS->gz)); THIS->gz.zalloc=Z_NULL; THIS->gz.zfree=Z_NULL;
e85aa92001-07-04Fredrik Hübinette (Hubbe)  THIS->gz.opaque=(void *)THIS;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  inflateInit(&THIS->gz); inflateEnd(&THIS->gz);
0a41162003-04-15Marcus Comstedt  THIS->epilogue = NULL;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) } static void exit_gz_inflate(struct object *o) {
7260951997-09-01Per Hedbor /* mt_lock(& THIS->lock); */
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  inflateEnd(& THIS->gz);
0a41162003-04-15Marcus Comstedt  do_free_string(THIS->epilogue);
7260951997-09-01Per Hedbor /* mt_unlock(& THIS->lock); */
4e77fb1999-06-10Fredrik Hübinette (Hubbe)  mt_destroy( & THIS->lock );
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
6461ef2001-01-05Henrik Grubbström (Grubba) /*! @endclass */
8d36d51998-02-27Mirar (Pontus Hagland) 
6461ef2001-01-05Henrik Grubbström (Grubba) /*! @decl int crc32(string data, void|int start_value) *!
90fc482002-11-25Henrik Grubbström (Grubba)  *! This function calculates the standard ISO3309 Cyclic Redundancy Check.
6461ef2001-01-05Henrik Grubbström (Grubba)  */
8d36d51998-02-27Mirar (Pontus Hagland) static void gz_crc32(INT32 args) { unsigned INT32 crc; if (!args || sp[-args].type!=T_STRING)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Gz.crc32: illegal or missing argument 1 (expected string)\n");
8d36d51998-02-27Mirar (Pontus Hagland) 
b804b21998-03-24Henrik Grubbström (Grubba)  if (args>1) {
8d36d51998-02-27Mirar (Pontus Hagland)  if (sp[1-args].type!=T_INT)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Gz.crc32: illegal argument 2 (expected integer)\n");
8d36d51998-02-27Mirar (Pontus Hagland)  else crc=(unsigned INT32)sp[1-args].u.integer;
b804b21998-03-24Henrik Grubbström (Grubba)  } else
8d36d51998-02-27Mirar (Pontus Hagland)  crc=0; crc=crc32(crc, (unsigned char*)sp[-args].u.string->str,
340c562001-06-13Henrik Grubbström (Grubba)  DO_NOT_WARN((unsigned INT32)(sp[-args].u.string->len)));
8d36d51998-02-27Mirar (Pontus Hagland)  pop_n_elems(args); push_int((INT32)crc); }
6461ef2001-01-05Henrik Grubbström (Grubba) /*! @endmodule */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #endif
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_EXIT {}
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_INIT
ab6aec1997-02-11Fredrik Hübinette (Hubbe) { #ifdef HAVE_ZLIB_H start_new_program();
90e9781999-01-31Fredrik Hübinette (Hubbe)  ADD_STORAGE(struct zipper);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
b8f7652003-04-14Marcus Comstedt  /* function(int|void,int|void:void) */ ADD_FUNCTION("create",gz_deflate_create,tFunc(tOr(tInt,tVoid) tOr(tInt,tVoid),tVoid),0);
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  /* function(string,int|void:string) */ ADD_FUNCTION("deflate",gz_deflate,tFunc(tStr tOr(tInt,tVoid),tStr),0);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  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);
b8f7652003-04-14Marcus Comstedt  add_integer_constant("DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY,0); add_integer_constant("FILTERED", Z_FILTERED,0); add_integer_constant("HUFFMAN_ONLY", Z_HUFFMAN_ONLY,0);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  set_init_callback(init_gz_deflate); set_exit_callback(exit_gz_deflate); end_class("deflate",0); start_new_program();
90e9781999-01-31Fredrik Hübinette (Hubbe)  ADD_STORAGE(struct zipper);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  /* 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);
0a41162003-04-15Marcus Comstedt  /* function(:string) */ ADD_FUNCTION("end_of_stream",gz_end_of_stream,tFunc(tNone,tStr),0);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  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);
2e87841997-04-06Fredrik Hübinette (Hubbe)  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);
b8f7652003-04-14Marcus Comstedt  add_integer_constant("DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY,0); add_integer_constant("FILTERED", Z_FILTERED,0); add_integer_constant("HUFFMAN_ONLY", Z_HUFFMAN_ONLY,0);
2e87841997-04-06Fredrik Hübinette (Hubbe) 
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  /* function(string,void|int:int) */ ADD_FUNCTION("crc32",gz_crc32,tFunc(tStr tOr(tVoid,tInt),tInt),
8d36d51998-02-27Mirar (Pontus Hagland)  OPT_TRY_OPTIMIZE);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #endif }
9eed301997-10-26Henrik Grubbström (Grubba) #if defined(HAVE___VTBL__9TYPE_INFO) || defined(HAVE___T_9__NOTHROW)
35d10a1997-10-12Henrik Grubbström (Grubba) /* Super-special kluge for IRIX 6.3 */
9eed301997-10-26Henrik Grubbström (Grubba) #ifdef HAVE___VTBL__9TYPE_INFO
297fff1997-10-14Henrik Grubbström (Grubba) extern void __vtbl__9type_info(void);
9eed301997-10-26Henrik Grubbström (Grubba) #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. */
297fff1997-10-14Henrik Grubbström (Grubba) void zlibmod_strap_kluge(void)
35d10a1997-10-12Henrik Grubbström (Grubba) {
9eed301997-10-26Henrik Grubbström (Grubba) #ifdef HAVE___VTBL__9TYPE_INFO
35d10a1997-10-12Henrik Grubbström (Grubba)  __vtbl__9type_info(); #endif /* HAVE___VTBL__9TYPE_INFO */
9eed301997-10-26Henrik Grubbström (Grubba) #ifdef HAVE___T_9__NOTHROW __T_9__nothrow(); #endif /* HAVE___T_9__NOTHROW */ } #endif /* HAVE___VTBL__9TYPE_INFO || HAVE___T_9__NOTHROW */