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

version» Context lines:

pike.git/src/modules/Gz/zlibmod.c:38:   {    int level;    int state;    struct z_stream_s gz;    struct pike_string *epilogue, *dict;   #ifdef _REENTRANT    DEFINE_MUTEX(lock);   #endif /* _REENTRANT */   };    + struct memobj + { +  void *ptr; +  size_t len; +  int shift; + }; +    #define BUF 32768   #define MAX_BUF (64*BUF)      #undef THIS   #define THIS ((struct zipper *)(Pike_fp->current_storage))      /*! @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
pike.git/src/modules/Gz/zlibmod.c:328:    if(ret == Z_BUF_ERROR) ret=Z_OK;    }    while (ret==Z_OK && (this->gz.avail_in || !this->gz.avail_out));      #ifdef _REENTRANT    CALL_AND_UNSET_ONERROR (uwp);   #endif    return ret;   }    - void zlibmod_pack(struct pike_string *data, dynamic_buffer *buf, + void low_zlibmod_pack(struct memobj data, dynamic_buffer *buf,    int level, int strategy, int wbits)   {    struct zipper z;    int ret;       if(level < Z_NO_COMPRESSION ||    level > Z_BEST_COMPRESSION)    Pike_error("Compression level out of range for pack. %d %d %d\n",    Z_DEFAULT_COMPRESSION, Z_NO_COMPRESSION, Z_BEST_COMPRESSION);   
pike.git/src/modules/Gz/zlibmod.c:357:    strategy != Z_HUFFMAN_ONLY)    Pike_error("Invalid compression strategy %d for pack.\n", strategy);       if( wbits<0 ? (wbits<-15 || wbits>-8) : (wbits<8 || wbits>15 ) )    Pike_error("Invalid window size value %d for pack.\n", wbits);       memset(&z, 0, sizeof(z));    z.gz.zalloc = Z_NULL;    z.gz.zfree = Z_NULL;    -  z.gz.next_in = (Bytef *)data->str; -  z.gz.avail_in = (unsigned INT32)(data->len); +  z.gz.next_in = (Bytef *)data.ptr; +  z.gz.avail_in = (unsigned INT32)(data.len);       do {    ret = deflateInit2(&z.gz, level, Z_DEFLATED, wbits, 9, strategy);    if (ret == Z_STREAM_ERROR) {    /* zlib 1.1.4's deflateInit2() only supports wbits 9..15 (not 8). */    if (wbits == -8) wbits = -9;    else if (wbits == 8) wbits = 9;    else break;    continue;    }
pike.git/src/modules/Gz/zlibmod.c:404:       ret = do_deflate(buf, &z, Z_FINISH);       deflateEnd(&z.gz);    mt_destroy(&z.lock);       if(ret != Z_STREAM_END)    Pike_error("Error while deflating data (%d).\n",ret);   }    + void zlibmod_pack(struct pike_string *data, dynamic_buffer *buf, +  int level, int strategy, int wbits) + { +  struct memobj lowdata; +  lowdata.ptr = data->str; +  lowdata.len = data->len; +  lowdata.shift = data->size_shift; +  low_zlibmod_pack(lowdata, buf, level, strategy, wbits); + } +    /*! @endclass    */    - /*! @decl string(8bit) compress(string(8bit) data, void|int(0..1) raw, @ + /*! @decl string(8bit) compress(string(8bit)|String.Buffer|System.Memory|Stdio.Buffer data, @ +  *! void|int(0..1) raw, @    *! void|int(0..9) level, void|int strategy, @    *! void|int(8..15) window_size)    *!    *! Encodes and returns the input @[data] according to the deflate    *! format defined in RFC 1951.    *!    *! @param data    *! The data to be encoded.    *!    *! @param raw
pike.git/src/modules/Gz/zlibmod.c:463:    *!    *! @param window_size    *! Defines the size of the LZ77 window from 256 bytes to 32768    *! bytes, expressed as 2^x.    *!    *! @seealso    *! @[deflate], @[inflate], @[uncompress]    */   static void gz_compress(INT32 args)   { -  struct pike_string *data; +  struct svalue *data_arg; +  struct memobj data;    dynamic_buffer buf;    ONERROR err;       int wbits = 15;    int raw = 0;    int level = 8;    int strategy = Z_DEFAULT_STRATEGY;    -  get_all_args("compress", args, "%n.%d%d%d%d", &data, &raw, &level, &strategy, +  get_all_args("compress", args, "%*.%d%d%d%d", &data_arg, &raw, &level, &strategy,    &wbits);    -  +  switch (TYPEOF(*data_arg)) +  { +  case PIKE_T_STRING: +  { +  struct pike_string *s = data_arg->u.string; +  data.ptr = (unsigned char*)s->str; +  data.len = s->len; +  data.shift = s->size_shift; +  break; +  } +  case PIKE_T_OBJECT: +  { +  enum memobj_type t = get_memory_object_memory(data_arg->u.object, +  &data.ptr, &data.len, +  &data.shift); +  if (t != MEMOBJ_NONE) +  break; +  // fall through +  } +  default: +  SIMPLE_BAD_ARG_ERROR("compress", 1, "string|String.Buffer|System.Memory|Stdio.Buffer"); +  } +  if (data.shift) +  Pike_error("Cannot input wide string to compress\n"); +     if( !wbits )    wbits = 15;       if( raw )    wbits = -wbits;       initialize_buf(&buf);    SET_ONERROR(err, toss_buffer, &buf); -  zlibmod_pack(data, &buf, level, strategy, wbits); +  low_zlibmod_pack(data, &buf, level, strategy, wbits);    UNSET_ONERROR(err);       pop_n_elems(args);    push_string(low_free_buf(&buf));   }      /*! @class deflate    */    - /*! @decl string(8bit) deflate(string(8bit) data, int|void flush) + /*! @decl string(8bit) deflate(string(8bit)|String.Buffer|System.Memory|Stdio.Buffer 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
pike.git/src/modules/Gz/zlibmod.c:516:    *! 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()]    */   static void gz_deflate(INT32 args)   { -  struct pike_string *data; +  struct memobj 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(TYPEOF(sp[-args]) != T_STRING) +  switch (TYPEOF(Pike_sp[-args])) +  { +  case PIKE_T_STRING: +  { +  struct pike_string *s = Pike_sp[-args].u.string; +  data.ptr = (unsigned char*)s->str; +  data.len = s->len; +  data.shift = s->size_shift; +  break; +  } +  case PIKE_T_OBJECT: +  { +  enum memobj_type t = get_memory_object_memory(Pike_sp[-args].u.object, +  &data.ptr, &data.len, +  &data.shift); +  if (t != MEMOBJ_NONE) +  break; +  // fall through +  } +  default:    Pike_error("Bad argument 1 to gz_deflate->deflate()\n"); -  +  }    -  data=sp[-args].u.string; -  if (data->size_shift) +  if (data.shift)    Pike_error("Cannot input wide string to gz_deflate->deflate()\n");       if(args>1)    {    if(TYPEOF(sp[1-args]) != T_INT)    Pike_error("Bad argument 2 to gz_deflate->deflate()\n");       flush=sp[1-args].u.integer;       switch(flush)
pike.git/src/modules/Gz/zlibmod.c:564:    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((unsigned INT32)(data->len)); +  this->gz.next_in=(Bytef *)data.ptr; +  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);
pike.git/src/modules/Gz/zlibmod.c:818:    }    } while(!this->gz.avail_out || flush==Z_FINISH || this->gz.avail_in);    }      #ifdef _REENTRANT    CALL_AND_UNSET_ONERROR (uwp);   #endif    return fail;   }    - void zlibmod_unpack(struct pike_string *data, dynamic_buffer *buf, int raw) + void low_zlibmod_unpack(struct memobj data, dynamic_buffer *buf, int raw)   {    struct zipper z;    int ret;       memset(&z, 0, sizeof(z));    z.gz.zalloc = Z_NULL;    z.gz.zfree = Z_NULL;    -  z.gz.next_in=(Bytef *)data->str; -  z.gz.avail_in = DO_NOT_WARN((unsigned INT32)(data->len)); +  z.gz.next_in=(Bytef *)data.ptr; +  z.gz.avail_in = DO_NOT_WARN((unsigned INT32)(data.len));       if( raw )    ret = inflateInit2(&z.gz, -15);    else    ret = inflateInit( &z.gz );       switch(ret)    {    case Z_OK:    break;
pike.git/src/modules/Gz/zlibmod.c:868:    ret = do_inflate(buf, &z, Z_SYNC_FLUSH);    mt_destroy(&z.lock);    inflateEnd( &z.gz );       if(ret==Z_OK)    Pike_error("Compressed data is truncated.\n");    if(ret!=Z_STREAM_END)    Pike_error("Failed to inflate data (%d).\n", ret);   }    + void zlibmod_unpack(struct pike_string *data, dynamic_buffer *buf, int raw) + { +  struct memobj lowdata; +  lowdata.ptr = data->str; +  lowdata.len = data->len; +  lowdata.shift = data->size_shift; +  low_zlibmod_unpack(lowdata, buf, raw); + } +    /*! @endclass   */    - /*! @decl string(8bit) uncompress(string(8bit) data, void|int(0..1) raw) + /*! @decl string(8bit) uncompress(string(8bit)|String.Buffer|System.Memory|Stdio.Buffer data, void|int(0..1) raw)    *!    *! Uncompresses the @[data] and returns it. The @[raw] parameter    *! tells the decoder that the indata lacks the data header and footer    *! defined in RFC 1950.    */   static void gz_uncompress(INT32 args)   {    dynamic_buffer buf; -  +  struct memobj data;    ONERROR err;    int raw = 0;       if(args<1)    SIMPLE_TOO_FEW_ARGS_ERROR("uncompress", 1); -  if(TYPEOF(Pike_sp[-args]) != PIKE_T_STRING) -  SIMPLE_BAD_ARG_ERROR("uncompress", 1, "string"); -  if (Pike_sp[-args].u.string->size_shift) +  switch (TYPEOF(Pike_sp[-args])) +  { +  case PIKE_T_STRING: +  { +  struct pike_string *s = Pike_sp[-args].u.string; +  data.ptr = (unsigned char*)s->str; +  data.len = s->len; +  data.shift = s->size_shift; +  break; +  } +  case PIKE_T_OBJECT: +  { +  enum memobj_type t = get_memory_object_memory(Pike_sp[-args].u.object, +  &data.ptr, &data.len, +  &data.shift); +  if (t != MEMOBJ_NONE) +  break; +  // fall through +  } +  default: +  SIMPLE_BAD_ARG_ERROR("uncompress", 1, "string|String.Buffer|System.Memory|Stdio.Buffer"); +  } +  if (data.shift)    Pike_error("Cannot input wide string to uncompress\n");    if(args>1)    {    if(TYPEOF(Pike_sp[1-args]) == PIKE_T_INT)    raw = Pike_sp[1-args].u.integer;    else    SIMPLE_BAD_ARG_ERROR("uncompress", 2, "int");    }       initialize_buf(&buf);    SET_ONERROR(err, toss_buffer, &buf); -  zlibmod_unpack(Pike_sp[-args].u.string, &buf, raw); +  low_zlibmod_unpack(data, &buf, raw);    UNSET_ONERROR(err);       pop_n_elems(args);    push_string(low_free_buf(&buf));      }      /*! @class inflate    */    - /*! @decl string(8bit) inflate(string(8bit) data) + /*! @decl string(8bit) inflate(string(8bit)|String.Buffer|System.Memory|Stdio.Buffer data)    *!    *! This function performs gzip style decompression. It can inflate    *! a whole file at once or in blocks.    *!    *! @example    *! // whole file    *! @code    *! write(Gz.inflate()->inflate(stdin->read(0x7fffffff));    *!    *! // streaming (blocks)    *! function inflate=Gz.inflate()->inflate;    *! while(string s=stdin->read(8192))    *! write(inflate(s));    *! @endcode    *!    *! @seealso    *! @[Gz.deflate->deflate()], @[Gz.decompress]    */   static void gz_inflate(INT32 args)   { -  struct pike_string *data; +  struct memobj data;    int fail;    struct zipper *this=THIS;    dynamic_buffer buf;    ONERROR err;       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(TYPEOF(sp[-args]) != T_STRING) +  switch (TYPEOF(Pike_sp[-args])) +  { +  case PIKE_T_STRING: +  { +  struct pike_string *s = Pike_sp[-args].u.string; +  data.ptr = (unsigned char*)s->str; +  data.len = s->len; +  data.shift = s->size_shift; +  break; +  } +  case PIKE_T_OBJECT: +  { +  enum memobj_type t = get_memory_object_memory(Pike_sp[-args].u.object, +  &data.ptr, &data.len, +  &data.shift); +  if (t != MEMOBJ_NONE) +  break; +  // fall through +  } +  default:    Pike_error("Bad argument 1 to gz_inflate->inflate()\n"); -  +  }    -  data=sp[-args].u.string; -  if (data->size_shift) +  if (data.shift)    Pike_error("Cannot input wide string to gz_inflate->inflate()\n");    -  this->gz.next_in=(Bytef *)data->str; -  this->gz.avail_in = DO_NOT_WARN((unsigned INT32)(data->len)); +  this->gz.next_in=(Bytef *)data.ptr; +  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);
pike.git/src/modules/Gz/zlibmod.c:1116: Inside #if defined(HAVE_ZLIB_H)
  #ifdef HAVE_ZLIB_H    struct z_stream_s z; /* Used to detect support for extensions. */    int have_rle = 0;    int have_fixed = 0;       start_new_program();    ADD_STORAGE(struct zipper);       /* function(int|void,int|void:void) */    ADD_FUNCTION("create",gz_deflate_create,tFunc(tOr(tMapping, tOr(tInt,tVoid)) tOr(tInt,tVoid),tVoid),0); -  /* function(string(8bit),int|void:string(8bit)) */ -  ADD_FUNCTION("deflate",gz_deflate,tFunc(tStr8 tOr(tInt,tVoid),tStr8),0); +  /* function(string(8bit)|String.Buffer|System.Memory|Stdio.Buffer,int|void:string(8bit)) */ +  ADD_FUNCTION("deflate",gz_deflate,tFunc(tOr(tStr8,tObj) tOr(tInt,tVoid),tStr8),0);    ADD_FUNCTION("_size_object", gz_deflate_size, tFunc(tVoid,tInt), 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);    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);   
pike.git/src/modules/Gz/zlibmod.c:1154: Inside #if defined(HAVE_ZLIB_H)
   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(tMapping,tOr(tInt,tVoid)),tVoid),0); -  /* function(string(8bit):string(8bit)) */ -  ADD_FUNCTION("inflate",gz_inflate,tFunc(tStr8,tStr8),0); +  /* function(string(8bit)|String.Buffer|System.Memory|Stdio.Buffer:string(8bit)) */ +  ADD_FUNCTION("inflate",gz_inflate,tFunc(tOr(tStr8,tObj),tStr8),0);    /* function(:string(8bit)) */    ADD_FUNCTION("end_of_stream",gz_end_of_stream,tFunc(tNone,tStr8),0);    ADD_FUNCTION("_size_object", gz_inflate_size, tFunc(tVoid,tInt), 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);
pike.git/src/modules/Gz/zlibmod.c:1191: Inside #if defined(HAVE_ZLIB_H) and #if defined(Z_FIXED)
  #endif   #ifdef Z_FIXED    if (have_fixed) {    add_integer_constant("FIXED", Z_FIXED,0);    }   #endif       /* function(string(8bit),void|int:int) */    ADD_FUNCTION("crc32",gz_crc32,tFunc(tStr8 tOr(tVoid,tInt),tInt),0);    -  /* function(string(8bit),void|int(0..1),void|int,void|int:string(8bit)) */ -  ADD_FUNCTION("compress",gz_compress,tFunc(tStr8 tOr(tVoid,tInt01) tOr(tVoid,tInt09) tOr(tVoid,tInt) tOr(tVoid,tInt),tStr8),0); +  /* function(string(8bit)|String.Buffer|System.Memory|Stdio.Buffer,void|int(0..1),void|int,void|int:string(8bit)) */ +  ADD_FUNCTION("compress",gz_compress,tFunc(tOr(tStr8,tObj) tOr(tVoid,tInt01) tOr(tVoid,tInt09) tOr(tVoid,tInt) tOr(tVoid,tInt),tStr8),0);    -  /* function(string(8bit),void|int(0..1):string(8bit)) */ -  ADD_FUNCTION("uncompress",gz_uncompress,tFunc(tStr8 tOr(tVoid,tInt01),tStr8),0); +  /* function(string(8bit)|String.Buffer|System.Memory|Stdio.Buffer,void|int(0..1):string(8bit)) */ +  ADD_FUNCTION("uncompress",gz_uncompress,tFunc(tOr(tStr8,tObj) tOr(tVoid,tInt01),tStr8),0);       PIKE_MODULE_EXPORT(Gz, crc32);    PIKE_MODULE_EXPORT(Gz, zlibmod_pack);    PIKE_MODULE_EXPORT(Gz, zlibmod_unpack);   #else    if(!TEST_COMPAT(7,6))    HIDE_MODULE();   #endif   }