pike.git / src / modules / Gz / zlibmod.c

version» Context lines:

pike.git/src/modules/Gz/zlibmod.c:1:   /*\   ||| 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.39 2001/02/15 18:09:49 hubbe Exp $"); + RCSID("$Id: zlibmod.c,v 1.40 2001/07/05 01:50:12 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"
pike.git/src/modules/Gz/zlibmod.c:27:   #include "threads.h"   #include "dynamic_buffer.h"      #include <zlib.h>      /* This must be included last! */   #include "module_magic.h"      struct zipper   { +  char level; +  char state;    struct z_stream_s gz;   #ifdef _REENTRANT    DEFINE_MUTEX(lock);   #endif /* _REENTRANT */   };      #define BUF 32768   #define MAX_BUF (64*BUF)      #undef THIS
pike.git/src/modules/Gz/zlibmod.c:76:    *!    *! If given, @[X] should be a number from 0 to 9 indicating the    *! 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.    */   static void gz_deflate_create(INT32 args)   { -  int level=Z_DEFAULT_COMPRESSION; +  int tmp; +  THIS->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) +  THIS->level=sp[-args].u.integer; +  if(THIS->level < Z_NO_COMPRESSION || +  THIS->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); +  tmp=deflateInit(&THIS->gz, THIS->level);   /* mt_unlock(& THIS->lock); */ -  switch(level) +  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.git/src/modules/Gz/zlibmod.c:188:    *! @[Gz.inflate->inflate()]    */   static void gz_deflate(INT32 args)   {    struct pike_string *data;    int flush, fail;    struct zipper *this=THIS;    dynamic_buffer buf;    ONERROR err;    +  if(THIS->state == 1) +  { +  deflateEnd(& THIS->gz); +  deflateInit(& THIS->gz, THIS->level); +  THIS->state=0; +  } +     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;
pike.git/src/modules/Gz/zlibmod.c:222:    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); +  this->gz.avail_in = DO_NOT_WARN((unsigned INT32)(data->len));       initialize_buf(&buf);       SET_ONERROR(err,toss_buffer,&buf);    fail=do_deflate(&buf,this,flush);    UNSET_ONERROR(err);       if(fail != Z_OK && fail != Z_STREAM_END)    {    toss_buffer(&buf);    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);    } -  +  +  if(fail == Z_STREAM_END) +  THIS->state=1; +     pop_n_elems(args);       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); +  THIS->state=0; +  deflateInit(& THIS->gz, THIS->level = 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 );   }   
pike.git/src/modules/Gz/zlibmod.c:320:    int flush)   {    int fail=0;    THREADS_ALLOW();    mt_lock(& this->lock);    THREADS_DISALLOW();    if(!this->gz.state)    {    fail=Z_STREAM_ERROR;    }else{ + #if 0 +  static int fnord=0; +  fnord++; + #endif +     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; -  + #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 +     ret=inflate(& this->gz, flush); -  + #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 +     THREADS_DISALLOW();    low_make_buf_space(-((ptrdiff_t)this->gz.avail_out), buf);    -  /* BUG WORKAROUND -Hubbe */ +     if(ret == Z_BUF_ERROR) ret=Z_OK;       if(ret != Z_OK)    {    fail=ret;    break;    }    } while(!this->gz.avail_out || flush==Z_FINISH || this->gz.avail_in);    }    mt_unlock(& this->lock);
pike.git/src/modules/Gz/zlibmod.c:374:       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); +  this->gz.avail_in = DO_NOT_WARN((unsigned INT32)(data->len));       initialize_buf(&buf);       SET_ONERROR(err,toss_buffer,&buf);    fail=do_inflate(&buf,this,Z_SYNC_FLUSH);    UNSET_ONERROR(err);       if(fail != Z_OK && fail != Z_STREAM_END)    {    toss_buffer(&buf);    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);    } -  +     pop_n_elems(args);       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; +  THIS->gz.opaque=(void *)THIS;    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 );
pike.git/src/modules/Gz/zlibmod.c:443:    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)); +  DO_NOT_WARN((unsigned INT32)(sp[-args].u.string->len)));       pop_n_elems(args);    push_int((INT32)crc);   }         /*! @endmodule    */   #endif