/* Generated from "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod" by precompile.pike running Pike v8.0 release 1737
 *
 * Do NOT edit this file.
 */

#undef PRECOMPILE_API_VERSION
#define PRECOMPILE_API_VERSION 6

#undef PRECOMPILE_SUB_PIKEVARS
#define PRECOMPILE_SUB_PIKEVARS 1

#undef PRECOMPILE_DYNAMIC_TYPES
#define PRECOMPILE_DYNAMIC_TYPES 1



#undef cmod___CMOD__
#define cmod___CMOD__ 1
#line 1 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#line 1 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#line 1 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#line 3003 "/home/hww3/pike-git/lib/modules/Tools.pmod/Standalone.pmod/precompile.pike"







#line 3010 "/home/hww3/pike-git/lib/modules/Tools.pmod/Standalone.pmod/precompile.pike"
#line 1 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#line 1 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
/* -*- c -*-
|| 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.
*/

#include "global.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 "module_support.h"
#include "builtin_functions.h"
#include "libbzip2mod_config.h"

#include <stdio.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

#ifdef HAVE_BZLIB_H
#include <bzlib.h>
#endif



#ifndef PIKE_UNUSED_ATTRIBUTE
#define PIKE_UNUSED_ATTRIBUTE
#endif
#define CMOD_MAP_PROGRAM_IDS_DEFINED 1
static int ___cmod_map_program_ids(int id) PIKE_UNUSED_ATTRIBUTE;
#ifndef TYPEOF
/* Compat with older Pikes. */
#define TYPEOF(SVAL)	((SVAL).type)
#define SUBTYPEOF(SVAL)	((SVAL).subtype)
#define SET_SVAL_TYPE(SVAL, TYPE)	(TYPEOF(SVAL) = TYPE)
#define SET_SVAL_SUBTYPE(SVAL, TYPE)	(SUBTYPEOF(SVAL) = TYPE)
#define SET_SVAL(SVAL, TYPE, SUBTYPE, FIELD, EXPR) do {	\
    /* Set the type afterwards to avoid a clobbered	\
     * svalue in case EXPR throws. */			\
    (SVAL).u.FIELD = (EXPR);				\
    SET_SVAL_TYPE((SVAL), (TYPE));			\
    SET_SVAL_SUBTYPE((SVAL), (SUBTYPE));		\
  } while(0)
#endif /* !TYPEOF */
#ifndef PIKE_UNUSED
/* Compat with Pike 7.8 and earlier. */
/* NB: Not strictly correct; PIKE_UNUSED was added the
 *     day after set_program_id_to_id(), but good enough.
 */
#ifndef UNUSED
#define UNUSED(X)	X
#endif
#ifndef set_program_id_to_id
/* NB: Recent Pike 7.8 has a #define that conflicts with
 *     the following declaration.
 */
static void set_program_id_to_id(void*UNUSED(id)){}
#endif
#else /* */
PMOD_EXPORT void set_program_id_to_id( int (*to)(int) );
#endif /* !PIKE_UNUSED */


#ifndef DEFAULT_CMOD_STORAGE
#define CMOD_COND_USED
#define DEFAULT_CMOD_STORAGE static
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_Deflate_feed_fun_num_used 1
#else
#undef f_Bz2_Deflate_feed_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_Deflate_create_fun_num_used 1
#else
#undef f_Bz2_Deflate_create_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_Deflate_read_fun_num_used 1
#else
#undef f_Bz2_Deflate_read_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_Deflate_finish_fun_num_used 1
#else
#undef f_Bz2_Deflate_finish_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_Deflate_deflate_fun_num_used 1
#else
#undef f_Bz2_Deflate_deflate_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define Bz2_Deflate_program_fun_num_used 1
#else
#undef Bz2_Deflate_program_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_Inflate_create_fun_num_used 1
#else
#undef f_Bz2_Inflate_create_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_Inflate_inflate_fun_num_used 1
#else
#undef f_Bz2_Inflate_inflate_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define Bz2_Inflate_program_fun_num_used 1
#else
#undef Bz2_Inflate_program_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_File_close_fun_num_used 1
#else
#undef f_Bz2_File_close_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_File_read_open_fun_num_used 1
#else
#undef f_Bz2_File_read_open_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_File_write_open_fun_num_used 1
#else
#undef f_Bz2_File_write_open_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_File_open_fun_num_used 1
#else
#undef f_Bz2_File_open_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_File_create_fun_num_used 1
#else
#undef f_Bz2_File_create_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_File_write_fun_num_used 1
#else
#undef f_Bz2_File_write_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_File_read_fun_num_used 1
#else
#undef f_Bz2_File_read_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Bz2_File_eof_fun_num_used 1
#else
#undef f_Bz2_File_eof_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define Bz2_File_program_fun_num_used 1
#else
#undef Bz2_File_program_fun_num_used
#endif
#line 37 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
/* These constants are the default values that
   are sent to a Bz2 stream when it is initialized */
#define DEFAULT_COMPRESSION_RATE 9
#define DEFAULT_WORK_FACTOR 30

/* Constants used when keeping track of
   the open mode of a file. They are added
   only to make the code more easy to read */
#define NO_FILE_MODE 0
#define READ_MODE 1
#define WRITE_MODE 2


/* The initial amount of bytes that are allocated
   as for deflate/inflate when memory is needed */
#define DEFL_BUF_SIZE 500000
#define INFL_BUF_SIZE 500000


/* Why use double prefixes? */
#ifdef HAVE_BZ2_BZCOMPRESSINIT
#define bzCompressInit		BZ2_bzCompressInit
#define bzCompress		BZ2_bzCompress
#define bzCompressEnd		BZ2_bzCompressEnd
#define bzDecompressInit	BZ2_bzDecompressInit
#define bzDecompress		BZ2_bzDecompress
#define bzDecompressEnd		BZ2_bzDecompressEnd
#define bzReadOpen		BZ2_bzReadOpen
#define bzReadClose		BZ2_bzReadClose
#define bzRead			BZ2_bzRead
#define bzWriteOpen		BZ2_bzWriteOpen
#define bzWrite			BZ2_bzWrite
#define bzWriteClose		BZ2_bzWriteClose

#endif

#ifdef HAVE_BZ_STREAM_TOTAL_OUT
#define TOTAL_OUT(S) ((S)->total_out)
#else
/* use 64 bit counting struct */
#define TOTAL_OUT(S) \
    ((S)->total_out_lo32 + (((INT64)(S)->total_out_hi32) << 32))
#endif

/*! @module Bz2
 *!
 *! The Bz2 module contains functions to compress and uncompress
 *! strings using the same algorithm as the program @tt{bzip2@}.
 *! Compressing and decompressing can be done in streaming mode
 *! feeding the compress and decompress objects with arbitrarily large
 *! pieces of data.
 *!
 *! The Bz2 module consists of three classes; @[Bz2.Deflate],
 *! @[Bz2.Inflate] and @[Bz2.File]. @[Bz2.Deflate] is used to compress
 *! data and @[Bz2.Inflate] is used to uncompress data. @[Bz2.File] is
 *! used to handle Bzip2 files.
 *!
 *! @note
 *!   Note that this module is only available if libbzip2 was
 *!   available when Pike was compiled.
 *!
 *!   Note that although the functions in @[Inflate] and @[Deflate]
 *!   use the same @i{algorithm@} as @tt{bzip2@}, they do not use the
 *!   exact same format, so you can not directly zip files or unzip
 *!   zip-files using those functions. That is why there exists a
 *!   third class for files.
 */

/*! @class Deflate
 *!
 *! Bz2.Deflate is a builtin program written in C. It interfaces the
 *! packing routines in the bzlib library.
 *!
 *! @note
 *! This program is only available if libz was available and found
 *! when Pike was compiled.
 *!
 *! @seealso
 *! @[Bz2.Inflate()]
 */

/*! @decl void create(int(1..9)|void block_size)
 *!
 *! If given, @[block_size] should be a number from 1 to 9 indicating
 *! the block size used when doing compression. The actual block size
 *! will be a 100000 times this number. Low numbers are considered
 *! 'fast', higher numbers are considered 'slow' but give better
 *! packing. The parameter is set to 9 if it is omitted.
 *!
 *! This function can also be used to re-initialize a Bz2.Deflate
 *! object so it can be re-used.
 */

#ifdef HAVE_BZ2LIB
#ifdef HAVE_BZLIB_H

#undef class_Bz2_Deflate_defined
#define class_Bz2_Deflate_defined
DEFAULT_CMOD_STORAGE struct program *Bz2_Deflate_program=NULL;

#ifdef Bz2_Deflate_program_fun_num_used
DEFAULT_CMOD_STORAGE int Bz2_Deflate_program_fun_num=-1;
#endif /* Bz2_Deflate_program_fun_num_used */

#undef var_intern_buffer_Bz2_Deflate_defined
#define var_intern_buffer_Bz2_Deflate_defined

#undef var_internbuf_Bz2_Deflate_defined
#define var_internbuf_Bz2_Deflate_defined

#undef var_strm_Bz2_Deflate_defined
#define var_strm_Bz2_Deflate_defined

#undef var_total_out_previous_flush_Bz2_Deflate_defined
#define var_total_out_previous_flush_Bz2_Deflate_defined

#undef var_total_out_previous_buf_Bz2_Deflate_defined
#define var_total_out_previous_buf_Bz2_Deflate_defined

#undef var_compression_rate_Bz2_Deflate_defined
#define var_compression_rate_Bz2_Deflate_defined

#undef var_work_factor_Bz2_Deflate_defined
#define var_work_factor_Bz2_Deflate_defined

#undef THIS
#define THIS ((struct Bz2_Deflate_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef THIS_BZ2_DEFLATE
#define THIS_BZ2_DEFLATE ((struct Bz2_Deflate_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef OBJ2_BZ2_DEFLATE
#define OBJ2_BZ2_DEFLATE(o) ((struct Bz2_Deflate_struct *)(o->storage+Bz2_Deflate_storage_offset))

#undef GET_BZ2_DEFLATE_STORAGE
#define GET_BZ2_DEFLATE_STORAGE(o) ((struct Bz2_Deflate_struct *)(o->storage+Bz2_Deflate_storage_offset)
static ptrdiff_t Bz2_Deflate_storage_offset;
struct Bz2_Deflate_struct {

#ifdef var_intern_buffer_Bz2_Deflate_defined
#line 134 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
dynamic_buffer intern_buffer;
#endif /* var_intern_buffer_Bz2_Deflate_defined */

#ifdef var_internbuf_Bz2_Deflate_defined
#line 135 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
dynamic_buffer *internbuf;
#endif /* var_internbuf_Bz2_Deflate_defined */

#ifdef var_strm_Bz2_Deflate_defined
#line 136 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
bz_stream strm;
#endif /* var_strm_Bz2_Deflate_defined */

#ifdef var_total_out_previous_flush_Bz2_Deflate_defined
#line 137 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
int total_out_previous_flush;
#endif /* var_total_out_previous_flush_Bz2_Deflate_defined */

#ifdef var_total_out_previous_buf_Bz2_Deflate_defined
#line 138 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
int total_out_previous_buf;
#endif /* var_total_out_previous_buf_Bz2_Deflate_defined */

#ifdef var_compression_rate_Bz2_Deflate_defined
#line 139 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
int compression_rate;
#endif /* var_compression_rate_Bz2_Deflate_defined */

#ifdef var_work_factor_Bz2_Deflate_defined
#line 140 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
int work_factor;
#endif /* var_work_factor_Bz2_Deflate_defined */
};
#ifdef PIKE_DEBUG
/* Ensure the struct is used in a variable declaration, or else gdb might not see it. */
static struct Bz2_Deflate_struct *Bz2_Deflate_gdb_dummy_ptr;
#endif
#define f_Bz2_Deflate_create_defined

#ifdef f_Bz2_Deflate_create_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_Deflate_create_fun_num = 0;

#endif /* f_Bz2_Deflate_create_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_Deflate_create(INT32 args) {
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct svalue * compression;
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct svalue * work;
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args > 2) wrong_number_of_args_error("create",args,2);
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if ((args > 0) &&
    ((TYPEOF(Pike_sp[0-args]) != PIKE_T_INT) ||
     (SUBTYPEOF(Pike_sp[0-args]) != NUMBER_UNDEFINED))) {
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-args]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("create",1,"int|void");
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
compression=Pike_sp+0-args; dmalloc_touch_svalue(Pike_sp+0-args);
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
} else compression = NULL;
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if ((args > 1) &&
    ((TYPEOF(Pike_sp[1-args]) != PIKE_T_INT) ||
     (SUBTYPEOF(Pike_sp[1-args]) != NUMBER_UNDEFINED))) {
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[1-args]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("create",2,"int|void");
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
work=Pike_sp+1-args; dmalloc_touch_svalue(Pike_sp+1-args);
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
} else work = NULL;
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
int compression_rate = DEFAULT_COMPRESSION_RATE;
int work_factor = DEFAULT_WORK_FACTOR;
int retval;
bz_stream *s;

if (work) {
work_factor = work->u.integer;
if(work_factor < 1 || work_factor > 250)
Pike_error("Work factor out of range for Bz2.Deflate().\n");
}

if (compression) {
compression_rate = compression->u.integer;
if(compression_rate < 1 || compression_rate > 9)
Pike_error("Compression rate out of range for Bz2.Deflate().\n");
}

s = &(THIS->strm);

if(THIS->internbuf != NULL){
toss_buffer(&(THIS->intern_buffer));
THIS->internbuf = NULL;
bzCompressEnd(s);
}

s->bzalloc = NULL;
s->bzfree = NULL;
s->opaque = NULL;
s->next_in = NULL;
s->next_out = NULL;
s->avail_in = 0;
s->avail_out = 0;

THIS->total_out_previous_flush = 0;
THIS->total_out_previous_buf = 0;
THIS->compression_rate = compression_rate;
THIS->work_factor = work_factor;

retval = bzCompressInit(s,compression_rate,0,work_factor); 

switch(retval){
case BZ_PARAM_ERROR:
Pike_error("Parameter error when initializing Bz2.Deflate object.\n");
break;
case BZ_MEM_ERROR:
Pike_error("Memory error when initialing Bz2.Deflate object.\n");
break;
case BZ_OK:
break;
default:
Pike_error("Failed to initialize Bz2.Deflate object.\n");
}
pop_n_elems(args);
}

}
/*! @decl void feed(string data)
   *!	
   *! This function feeds the data to the internal buffers of the
   *! Deflate object. All data is buffered until a read or a finish is
   *! done.
   *!
   *! @seealso
   *! @[Bz2.Deflate->read()]
   *! @[Bz2.Deflate->finish()]
   */

#define f_Bz2_Deflate_feed_defined

#ifdef f_Bz2_Deflate_feed_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_Deflate_feed_fun_num = 0;

#endif /* f_Bz2_Deflate_feed_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_Deflate_feed(INT32 args) {
#line 209 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * data;
#line 209 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args != 1) wrong_number_of_args_error("feed",args,1);
#line 209 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-1]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("feed",1,"string");
#line 209 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(data=Pike_sp[0-1].u.string);
#line 209 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{

int retval = 0;
int i = 1; 
bz_stream *s;
char* tmp = NULL;

if (THIS->internbuf==NULL){
/*initialize the internal buffer needed, because libbzip2 is weird*/
initialize_buf(&(THIS->intern_buffer));
THIS->internbuf = &(THIS->intern_buffer);
}

s = &(THIS->strm);
s->next_in=(char *)data->str;
s->avail_in = data->len;

while(1){
/* need to have a loop here in 
	 case the output-buffer is filled*/
/* allocate out buffer */
tmp = xalloc(i * DEFL_BUF_SIZE);
s->next_out = tmp;
s->avail_out = i * DEFL_BUF_SIZE;

retval = bzCompress(s, BZ_RUN);
if (retval != BZ_RUN_OK){
bzCompressEnd(s);
free(tmp);
Pike_error("Error when compressing in Bz2.feed().\n");
}

if(TOTAL_OUT(s) - THIS->total_out_previous_buf > 0){
/*if something was written to the outbuffer, then buffer it internally*/
low_my_binary_strcat(tmp, TOTAL_OUT(s) - THIS->total_out_previous_buf,&(THIS->intern_buffer));
THIS->internbuf = &(THIS->intern_buffer);
THIS->total_out_previous_buf = TOTAL_OUT(s);
}
/*free the outbuffer*/
free(tmp);
if (s->avail_out > 0 || s->avail_in == 0){
break;
}
i = 2 * i;
}
pop_n_elems(args);

}

}
#line 258 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
void do_deflate(struct pike_string *data, dynamic_buffer *retbuf,
int mode, INT32 UNUSED(args)){
char *tmp = NULL;
int retval;
int total_out_old = 0;
int i = 1;

bz_stream *s = &(THIS->strm);

s->next_in = data->str;
s->avail_in = data->len;
s->next_out = retbuf->s.str;
s->avail_out = DEFL_BUF_SIZE;

while(1){ 
retval = bzCompress(s, mode);

if(tmp != NULL){
low_my_binary_strcat(tmp, TOTAL_OUT(s)-total_out_old, retbuf);
free(tmp);
}
if(retval < 0){
#if 0
 fprintf(stderr,"\nRetval is %d", retval); 
#endif /* 0 */
 bzCompressEnd(s);
Pike_error("Error when compressing data.\n");
}
/* If all data has been finished or flushed then end this loop*/
if(retval == BZ_STREAM_END || ((retval == BZ_RUN_OK)&&(s->avail_in == 0))){
break;
}
/* If the output buffer is full, then allocate more space
	 and go on compressing*/
if (s->avail_out == 0){
i = 2 * i;
tmp = xcalloc(i, DEFL_BUF_SIZE);
s->next_out = tmp;
s->avail_out = i * DEFL_BUF_SIZE;
total_out_old = TOTAL_OUT(s);
}
}
}

/*! @decl string read(string data)
   *!
   *! This function feeds the data to the internal buffers of the
   *! Deflate object. Then it compresses all buffered data and returns
   *! the compressed data as a string
   *!
   *! @seealso
   *! @[Bz2.Deflate->feed()]
   *! @[Bz2.Deflate->finish()]
   */

#define f_Bz2_Deflate_read_defined

#ifdef f_Bz2_Deflate_read_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_Deflate_read_fun_num = 0;

#endif /* f_Bz2_Deflate_read_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_Deflate_read(INT32 args) {
#line 313 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * data;
#line 313 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args != 1) wrong_number_of_args_error("read",args,1);
#line 313 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-1]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("read",1,"string");
#line 313 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(data=Pike_sp[0-1].u.string);
#line 313 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
struct pike_string *retstr;
/* FIXME: Ought to use string_builder instead. */
dynamic_buffer retbuf;
bz_stream *s = &(THIS->strm);
ONERROR err;

initialize_buf(&retbuf);
SET_ONERROR(err, toss_buffer, &retbuf);

low_make_buf_space(DEFL_BUF_SIZE, &retbuf);
do_deflate(data, &retbuf, BZ_FLUSH, args);

if(TOTAL_OUT(s) - THIS->total_out_previous_flush > 0){
if(THIS->total_out_previous_buf > THIS->total_out_previous_flush){
low_my_binary_strcat(retbuf.s.str,
TOTAL_OUT(s) - THIS->total_out_previous_buf,
&(THIS->intern_buffer));
retstr =
make_shared_binary_string(THIS->intern_buffer.s.str,
TOTAL_OUT(s) - THIS->total_out_previous_flush);
} 
else{
retstr =
make_shared_binary_string(retbuf.s.str,
TOTAL_OUT(s)-THIS->total_out_previous_flush);
}
if(THIS->internbuf != NULL){
toss_buffer(&THIS->intern_buffer);
THIS->internbuf = NULL;
} 
THIS->total_out_previous_flush = TOTAL_OUT(s);
THIS->total_out_previous_buf = TOTAL_OUT(s);
}
else{
retstr = make_shared_binary_string("",0);
}

CALL_AND_UNSET_ONERROR(err);

do { struct pike_string * ret_=((retstr)); pop_stack(); push_string(ret_); return; }while(0);
#line 354 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}


}
/*! @decl string finish(string data)
   *!
   *! This method feeds the data to the internal buffers of the
   *! Deflate object. Then it compresses all buffered data adds a end
   *! of data marker ot it, returns the compressed data as a string,
   *! and reinitializes the deflate object.
   *!
   *! @seealso
   *! @[Bz2.Deflate->feed()]
   *! @[Bz2.Deflate->read()]
   */

#define f_Bz2_Deflate_finish_defined

#ifdef f_Bz2_Deflate_finish_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_Deflate_finish_fun_num = 0;

#endif /* f_Bz2_Deflate_finish_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_Deflate_finish(INT32 args) {
#line 369 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * data;
#line 369 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args != 1) wrong_number_of_args_error("finish",args,1);
#line 369 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-1]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("finish",1,"string");
#line 369 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(data=Pike_sp[0-1].u.string);
#line 369 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
struct pike_string *retstr;
int retval = 0;
dynamic_buffer retbuf;
ONERROR err;
bz_stream *s = &(THIS->strm);

initialize_buf(&retbuf);
SET_ONERROR(err, toss_buffer, &retbuf);

low_make_buf_space(DEFL_BUF_SIZE, &retbuf);

do_deflate(data, &retbuf, BZ_FINISH, args);

/*use perhaps a better check?*/
if(TOTAL_OUT(s) - THIS->total_out_previous_flush > 0){
if(THIS->total_out_previous_buf > THIS->total_out_previous_flush){
low_my_binary_strcat(retbuf.s.str,
TOTAL_OUT(s) - THIS->total_out_previous_buf,
&(THIS->intern_buffer));
retstr =
make_shared_binary_string(THIS->intern_buffer.s.str,
TOTAL_OUT(s) - THIS->total_out_previous_flush);
} 
else{
retstr =
make_shared_binary_string(retbuf.s.str,
TOTAL_OUT(s) - THIS->total_out_previous_flush);
}

THIS->total_out_previous_flush = TOTAL_OUT(s);
THIS->total_out_previous_buf = TOTAL_OUT(s);
}
else{
/* something went wrong*/
retstr = NULL;
}

CALL_AND_UNSET_ONERROR(err);

bzCompressEnd(s);

if(THIS->internbuf != NULL){
toss_buffer(THIS->internbuf);
THIS->internbuf = NULL;
}

s->bzalloc = NULL;
s->bzfree = NULL;
s->opaque = NULL;
s->next_in = NULL;
s->next_out = NULL;
s->avail_in = 0;
s->avail_out = 0;

THIS->total_out_previous_flush = 0;
THIS->total_out_previous_buf = 0;

retval = bzCompressInit(s,THIS->compression_rate,0,THIS->work_factor); 
if(retval < 0){
Pike_error("Failed to reinitialize stream.\n");
}

if(retstr != NULL){
do { struct pike_string * ret_=((retstr)); pop_stack(); push_string(ret_); return; }while(0);
#line 434 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}
else{
do { struct pike_string * ret_=((0)); pop_stack(); push_string(ret_); return; }while(0);
#line 437 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}
}

}
/*! @decl string deflate(string data, int(0..2)|void flush_mode)
   *!
   *! This function performs bzip2 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_mode] should be one of the
   *! following:
   *! @int
   *!   @value Bz2.BZ_RUN
   *!     Runs Bz2.Deflate->feed()
   *!   @value Bz2.BZ_FLUSH
   *!     Runs Bz2.Deflate->read()
   *!   @value Bz2.BZ_FINISH
   *!     Runs Bz2.Deflate->finish()
   *! @endint
   *!
   *! @seealso
   *! @[Bz2.Inflate->inflate()]
   */

#define f_Bz2_Deflate_deflate_defined

#ifdef f_Bz2_Deflate_deflate_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_Deflate_deflate_fun_num = 0;

#endif /* f_Bz2_Deflate_deflate_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_Deflate_deflate(INT32 args) {
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * data;
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct svalue * mode;
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args < 1) wrong_number_of_args_error("deflate",args,1);
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args > 2) wrong_number_of_args_error("deflate",args,2);
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-args]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("deflate",1,"string");
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(data=Pike_sp[0-args].u.string);
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if ((args > 1) &&
    ((TYPEOF(Pike_sp[1-args]) != PIKE_T_INT) ||
     (SUBTYPEOF(Pike_sp[1-args]) != NUMBER_UNDEFINED))) {
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[1-args]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("deflate",2,"int|void");
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
mode=Pike_sp+1-args; dmalloc_touch_svalue(Pike_sp+1-args);
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
} else mode = NULL;
{
int flush_mode = BZ_FLUSH;

if(mode)
{
flush_mode = mode->u.integer;
pop_stack();
}

switch (flush_mode){
case BZ_FLUSH:
f_Bz2_Deflate_read(1);
break;
case BZ_FINISH:
f_Bz2_Deflate_finish(1);
break;
case BZ_RUN:
f_Bz2_Deflate_feed(1);
push_empty_string();
break;
}
}

}

#undef internal_init_Bz2_Deflate_defined
#define internal_init_Bz2_Deflate_defined

#undef Bz2_Deflate_event_handler_defined
#define Bz2_Deflate_event_handler_defined
static void init_Bz2_Deflate_struct(void)
#line 487 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
bz_stream *s;
THIS->internbuf = NULL;
s =&(THIS->strm);
s->bzalloc = NULL;
s->bzfree = NULL;
s->opaque = NULL;
s->next_in = NULL;
s->next_out = NULL;
s->avail_in = 0;
s->avail_out = 0;
}

#undef internal_exit_Bz2_Deflate_defined
#define internal_exit_Bz2_Deflate_defined

#undef Bz2_Deflate_event_handler_defined
#define Bz2_Deflate_event_handler_defined
static void exit_Bz2_Deflate_struct(void)
#line 501 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
bzCompressEnd(&(THIS->strm));
if(THIS->internbuf != NULL){
toss_buffer(&(THIS->intern_buffer));
THIS->internbuf = NULL;
}
} 

#ifdef Bz2_Deflate_event_handler_defined
static void Bz2_Deflate_event_handler(int ev) {
  switch(ev) {

#ifdef internal_init_Bz2_Deflate_defined
  case PROG_EVENT_INIT: init_Bz2_Deflate_struct(); break;

#endif /* internal_init_Bz2_Deflate_defined */

#ifdef internal_exit_Bz2_Deflate_defined
  case PROG_EVENT_EXIT: exit_Bz2_Deflate_struct(); break;

#endif /* internal_exit_Bz2_Deflate_defined */
  default: break; 
  }
}

#endif /* Bz2_Deflate_event_handler_defined */
/*! @endclass
 */


/*! @class Inflate
 *!
 *! Bz2.Inflate is a builtin program written in C. It interfaces the
 *! unpacking routines in the libz library.
 *!
 *! @note
 *! This program is only available if bzlib was available and found
 *! when Pike was compiled.
 *!
 *! @seealso
 *!   @[Deflate]
 */




#undef class_Bz2_Inflate_defined
#define class_Bz2_Inflate_defined
DEFAULT_CMOD_STORAGE struct program *Bz2_Inflate_program=NULL;

#ifdef Bz2_Inflate_program_fun_num_used
DEFAULT_CMOD_STORAGE int Bz2_Inflate_program_fun_num=-1;
#endif /* Bz2_Inflate_program_fun_num_used */

#undef var_intern_buffer_Bz2_Inflate_defined
#define var_intern_buffer_Bz2_Inflate_defined

#undef var_internbuf_Bz2_Inflate_defined
#define var_internbuf_Bz2_Inflate_defined

#undef var_strm_Bz2_Inflate_defined
#define var_strm_Bz2_Inflate_defined

#undef var_total_out_previous_flush_Bz2_Inflate_defined
#define var_total_out_previous_flush_Bz2_Inflate_defined

#undef var_total_out_previous_buf_Bz2_Inflate_defined
#define var_total_out_previous_buf_Bz2_Inflate_defined

#undef THIS
#define THIS ((struct Bz2_Inflate_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef THIS_BZ2_INFLATE
#define THIS_BZ2_INFLATE ((struct Bz2_Inflate_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef OBJ2_BZ2_INFLATE
#define OBJ2_BZ2_INFLATE(o) ((struct Bz2_Inflate_struct *)(o->storage+Bz2_Inflate_storage_offset))

#undef GET_BZ2_INFLATE_STORAGE
#define GET_BZ2_INFLATE_STORAGE(o) ((struct Bz2_Inflate_struct *)(o->storage+Bz2_Inflate_storage_offset)
static ptrdiff_t Bz2_Inflate_storage_offset;
struct Bz2_Inflate_struct {

#ifdef var_intern_buffer_Bz2_Inflate_defined
#line 532 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
dynamic_buffer intern_buffer;
#endif /* var_intern_buffer_Bz2_Inflate_defined */

#ifdef var_internbuf_Bz2_Inflate_defined
#line 533 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
dynamic_buffer *internbuf;
#endif /* var_internbuf_Bz2_Inflate_defined */

#ifdef var_strm_Bz2_Inflate_defined
#line 534 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
bz_stream strm;
#endif /* var_strm_Bz2_Inflate_defined */

#ifdef var_total_out_previous_flush_Bz2_Inflate_defined
#line 535 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
int total_out_previous_flush;
#endif /* var_total_out_previous_flush_Bz2_Inflate_defined */

#ifdef var_total_out_previous_buf_Bz2_Inflate_defined
#line 536 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
int total_out_previous_buf;
#endif /* var_total_out_previous_buf_Bz2_Inflate_defined */
};
#ifdef PIKE_DEBUG
/* Ensure the struct is used in a variable declaration, or else gdb might not see it. */
static struct Bz2_Inflate_struct *Bz2_Inflate_gdb_dummy_ptr;
#endif
/*! @decl void create()
   */
#define f_Bz2_Inflate_create_defined

#ifdef f_Bz2_Inflate_create_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_Inflate_create_fun_num = 0;

#endif /* f_Bz2_Inflate_create_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_Inflate_create(INT32 args) {
#line 540 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args != 0) wrong_number_of_args_error("create",args,0);
#line 540 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{

int retval;
bz_stream *s = &(THIS->strm);

if(THIS->internbuf != NULL){
toss_buffer(&(THIS->intern_buffer));
THIS->internbuf = NULL;
bzDecompressEnd(s);
}

s->bzalloc = NULL;
s->bzfree = NULL;
s->opaque = NULL;

retval = bzDecompressInit(s, 0, 0);

/* make more detailed*/
if(retval != BZ_OK){
Pike_error("Unexpected error in Bz2.Inflate().\n");
}

s->next_in = NULL;
s->next_out = NULL;
s->avail_in = 0;
s->avail_out = 0;

/*reset the counted bytes*/
THIS->total_out_previous_flush = 0;
pop_n_elems(args);
}

}
/*! @decl string inflate(string data)
   *!
   *! This function performs bzip2 style decompression. It can do
   *! decompression with arbitrarily large pieces of data. When fed
   *! with data, it decompresses as much as it can and buffers the
   *! rest.
   *!
   *! @example
   *! while(..){
   *!   foo = compressed_data[i..i+9]; 
   *!   uncompressed_concatenated_data += inflate_object->inflate(foo);
   *!   i = i+10;
   *! }
   *!
   *! @seealso
   *! @[Bz2.Deflate->deflate()]
   */

#define f_Bz2_Inflate_inflate_defined

#ifdef f_Bz2_Inflate_inflate_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_Inflate_inflate_fun_num = 0;

#endif /* f_Bz2_Inflate_inflate_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_Inflate_inflate(INT32 args) {
#line 590 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * data;
#line 590 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args != 1) wrong_number_of_args_error("inflate",args,1);
#line 590 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-1]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("inflate",1,"string");
#line 590 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(data=Pike_sp[0-1].u.string);
#line 590 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
struct pike_string *retstr;
int i = 1;
int retval=0;
int total_out_old = 0;
char *tmp = NULL;
char *tmp_internbuf = NULL;
dynamic_buffer ret_buffer;
dynamic_buffer *retbuf = &ret_buffer;
bz_stream *s = &(THIS->strm);

/*the incoming string has to be appended to the input buffer*/

if (s->avail_in > 0){
tmp_internbuf = xalloc(s->avail_in);
memcpy (tmp_internbuf, s->next_in, s->avail_in);
}
/* free buf space*/
if(THIS->internbuf != NULL){
toss_buffer(&(THIS->intern_buffer));
}
initialize_buf(&(THIS->intern_buffer));

if(tmp_internbuf != NULL){
/*copying data that could not be unpacked in previous call*/
low_my_binary_strcat(tmp_internbuf,s->avail_in,&(THIS->intern_buffer));
free(tmp_internbuf);
}
/*appending the new data*/
low_my_binary_strcat(data->str,data->len,&(THIS->intern_buffer));

THIS->internbuf = &(THIS->intern_buffer);
s->next_in = THIS->internbuf->s.str;
s->avail_in += data->len;

/* the input buffer is now provided, provide an output buffer*/

initialize_buf(retbuf);

low_make_buf_space(INFL_BUF_SIZE, retbuf);
s->next_out = retbuf->s.str;
s->avail_out = INFL_BUF_SIZE;

while(1){
/* This Loop runs bzDecompress and stores the result in retbuf,
	 if retbuf is filled it makes retbuf bigger and calls
	 decompress again until the whole input data string has been
	 unpacked*/
retval = bzDecompress(s); /* updates next_in, avail_in, total_in, next_out, avail_out, total_out*/

if(tmp != NULL){
low_my_binary_strcat(tmp,TOTAL_OUT(s)-total_out_old,&ret_buffer);
free(tmp);
}
/* errorcheck*/
if(retval == BZ_STREAM_END && s->avail_in > 0){
bzDecompressEnd(s);
toss_buffer(&ret_buffer);
Pike_error("No data may follow after end of stream.\n"); 
}
if((retval != BZ_STREAM_END) && (retval != BZ_OK)){
bzDecompressEnd(s);
toss_buffer(&ret_buffer);
Pike_error("Error when decompressing, probably because inflate "
"is fed with invalid data.\n");
} 

/* If outputbuffer is not full, or input buffer is empty, or end
	 of stream has been detected then compression has been done as
	 far as possible*/
if((s->avail_out > 0) || (s->avail_in == 0) || (retval == BZ_STREAM_END)){
break;
}
/* If output buffer is full then allocate more output space and call
	 bzDecompress again*/
if (s->avail_out == 0){
i = i*2;
tmp = calloc(i, INFL_BUF_SIZE);
if(tmp == NULL){
toss_buffer(&ret_buffer);
SIMPLE_OUT_OF_MEMORY_ERROR("inflate", i*INFL_BUF_SIZE);
}
s->next_out = tmp;
s->avail_out = i * INFL_BUF_SIZE;
total_out_old = TOTAL_OUT(s);
}
}

/*return the bytes decompressed since the last time bytes were
      returned from this stream, in other words, return a block*/
if((retval == BZ_OK) || (retval==BZ_STREAM_END)){
if(TOTAL_OUT(s) - THIS->total_out_previous_flush > 0){
retstr = make_shared_binary_string(ret_buffer.s.str,TOTAL_OUT(s)-THIS->total_out_previous_flush);
}
else{
retstr = make_shared_binary_string("",0);
}
THIS->total_out_previous_flush = TOTAL_OUT(s);
if(retval == BZ_STREAM_END){
bzDecompressEnd(s);
toss_buffer(&(THIS->intern_buffer));/*free the final buffer*/

if(THIS->internbuf != NULL){
toss_buffer(&(THIS->intern_buffer));
THIS->internbuf = NULL;
}

s->bzalloc = NULL;
s->bzfree = NULL;
s->opaque = NULL;

retval = bzDecompressInit(s, 0, 0);

/* make more detailed*/
if(retval != BZ_OK){
Pike_error("Unexpected error in Bz2.Inflate().\n");
}

s->next_in = NULL;
s->next_out = NULL;
s->avail_in = 0;
s->avail_out = 0;


/*reset the counted bytes*/
THIS->total_out_previous_flush = 0;
}
toss_buffer(&ret_buffer);
do { struct pike_string * ret_=((retstr)); pop_stack(); push_string(ret_); return; }while(0);
#line 719 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}

else{
/* something went really wrong*/
toss_buffer(&ret_buffer);
do { struct pike_string * ret_=((0)); pop_stack(); push_string(ret_); return; }while(0);
#line 725 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}
}

}

#undef internal_init_Bz2_Inflate_defined
#define internal_init_Bz2_Inflate_defined

#undef Bz2_Inflate_event_handler_defined
#define Bz2_Inflate_event_handler_defined
static void init_Bz2_Inflate_struct(void)
#line 729 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
bz_stream *s;
THIS->internbuf = NULL;
s = &(THIS->strm);
s->bzalloc = NULL;
s->bzfree = NULL;
s->opaque = NULL;
s->next_in = NULL;
s->next_out = NULL;
s->avail_in = 0;
s->avail_out = 0;
}


#undef internal_exit_Bz2_Inflate_defined
#define internal_exit_Bz2_Inflate_defined

#undef Bz2_Inflate_event_handler_defined
#define Bz2_Inflate_event_handler_defined
static void exit_Bz2_Inflate_struct(void)
#line 744 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
bzDecompressEnd(&(THIS->strm));
if(THIS->internbuf != NULL){
toss_buffer(&(THIS->intern_buffer));
THIS->internbuf = NULL;
}
}

#ifdef Bz2_Inflate_event_handler_defined
static void Bz2_Inflate_event_handler(int ev) {
  switch(ev) {

#ifdef internal_init_Bz2_Inflate_defined
  case PROG_EVENT_INIT: init_Bz2_Inflate_struct(); break;

#endif /* internal_init_Bz2_Inflate_defined */

#ifdef internal_exit_Bz2_Inflate_defined
  case PROG_EVENT_EXIT: exit_Bz2_Inflate_struct(); break;

#endif /* internal_exit_Bz2_Inflate_defined */
  default: break; 
  }
}

#endif /* Bz2_Inflate_event_handler_defined */
/*! @endclass
*/

#line 755 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#ifndef __NT__
/* We currently have to disable this on Windows since we typically
 * have a different CRT in the bz2 lib than in pike. That makes it
 * impossible to pass fd's between them. */

/*! @class File
 *! Low-level implementation of read/write support for Bzip2 files
 *!
 *! @note
 *! This class is currently not available on Windows.
 */



#undef class_Bz2_File_defined
#define class_Bz2_File_defined
DEFAULT_CMOD_STORAGE struct program *Bz2_File_program=NULL;

#ifdef Bz2_File_program_fun_num_used
DEFAULT_CMOD_STORAGE int Bz2_File_program_fun_num=-1;
#endif /* Bz2_File_program_fun_num_used */

#undef var_file_Bz2_File_defined
#define var_file_Bz2_File_defined

#undef var_fp_Bz2_File_defined
#define var_fp_Bz2_File_defined

#undef var_mode_Bz2_File_defined
#define var_mode_Bz2_File_defined

#undef var_small_Bz2_File_defined
#define var_small_Bz2_File_defined

#undef var_bzerror_Bz2_File_defined
#define var_bzerror_Bz2_File_defined

#undef THIS
#define THIS ((struct Bz2_File_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef THIS_BZ2_FILE
#define THIS_BZ2_FILE ((struct Bz2_File_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef OBJ2_BZ2_FILE
#define OBJ2_BZ2_FILE(o) ((struct Bz2_File_struct *)(o->storage+Bz2_File_storage_offset))

#undef GET_BZ2_FILE_STORAGE
#define GET_BZ2_FILE_STORAGE(o) ((struct Bz2_File_struct *)(o->storage+Bz2_File_storage_offset)
static ptrdiff_t Bz2_File_storage_offset;
struct Bz2_File_struct {

#ifdef var_file_Bz2_File_defined
#line 770 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
BZFILE *file;
#endif /* var_file_Bz2_File_defined */

#ifdef var_fp_Bz2_File_defined
#line 771 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
FILE *fp;
#endif /* var_fp_Bz2_File_defined */

#ifdef var_mode_Bz2_File_defined
#line 772 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
int mode;
#endif /* var_mode_Bz2_File_defined */

#ifdef var_small_Bz2_File_defined
#line 773 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
int small;
#endif /* var_small_Bz2_File_defined */

#ifdef var_bzerror_Bz2_File_defined
#line 774 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
int bzerror;
#endif /* var_bzerror_Bz2_File_defined */
};
#ifdef PIKE_DEBUG
/* Ensure the struct is used in a variable declaration, or else gdb might not see it. */
static struct Bz2_File_struct *Bz2_File_gdb_dummy_ptr;
#endif
/*! @decl int(0..1) close()
   *! closes the file
   */

#define f_Bz2_File_close_defined

#ifdef f_Bz2_File_close_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_File_close_fun_num = 0;

#endif /* f_Bz2_File_close_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_File_close(INT32 args) {
#line 780 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args != 0) wrong_number_of_args_error("close",args,0);
{
if (!THIS->fp) do { INT_TYPE ret_=((1));  push_int(ret_); return; }while(0);
#line 783 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
switch(THIS->mode){
case READ_MODE:
bzReadClose(&(THIS->bzerror),THIS->file);
break;
case WRITE_MODE:
bzWriteClose ( &(THIS->bzerror), THIS->file,0,NULL,NULL);
break;
default:
Pike_error("This error can never occur.\n");
}

fclose(THIS->fp);

THIS->fp = NULL;
THIS->mode = NO_FILE_MODE;
THIS->small = 0;

if((THIS->bzerror)==BZ_OK)
do { INT_TYPE ret_=((1));  push_int(ret_); return; }while(0);do { INT_TYPE ret_=(
#line 803 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
(0));  push_int(ret_); return; }while(0);
#line 804 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}

}
/*! @decl int(0..1) read_open(string file)
   *! Opens a file for reading.
   *! @param file
   *! The name of the file to be opened
   */

#define f_Bz2_File_read_open_defined

#ifdef f_Bz2_File_read_open_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_File_read_open_fun_num = 0;

#endif /* f_Bz2_File_read_open_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_File_read_open(INT32 args) {
#line 812 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * file_name;
#line 812 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args != 1) wrong_number_of_args_error("read_open",args,1);
#line 812 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-1]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("read_open",1,"string");
#line 812 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(file_name=Pike_sp[0-1].u.string);
#line 812 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
FILE *tmp = NULL;

if( THIS->mode == NO_FILE_MODE){
tmp = fopen(file_name->str,"rb");
}
if(tmp != NULL){
THIS->fp = tmp;
THIS->file = bzReadOpen(&(THIS->bzerror),tmp,0,0,NULL,0);
THIS->mode = READ_MODE;

switch(THIS->bzerror){
case BZ_OK:
break;
case BZ_MEM_ERROR:
if(THIS->small == 0){
bzReadClose(&(THIS->bzerror),THIS->file);
THIS->small = 1;
bzReadOpen(&(THIS->bzerror),tmp,1,0,NULL,0);
if(THIS->bzerror != BZ_OK){
Pike_error("Bz2.File->read_open() failed.\n");
} 
}
else{
Pike_error("Bz2.File->read_open() out of memory.\n");
}
break;
default:
Pike_error("Error in Bz2.File()->read_open.\n");
}
}

if(tmp==NULL)
do { INT_TYPE ret_=((0)); pop_stack(); push_int(ret_); return; }while(0);do { INT_TYPE ret_=(
#line 847 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
(1)); pop_stack(); push_int(ret_); return; }while(0);
#line 848 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}

}
/*! @decl int(0..1) write_open(string file)
   *! Opens a file for writing.
   *! @param file
   *! The name of the file to be opened
   */

#define f_Bz2_File_write_open_defined

#ifdef f_Bz2_File_write_open_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_File_write_open_fun_num = 0;

#endif /* f_Bz2_File_write_open_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_File_write_open(INT32 args) {
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * file_name;
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct svalue * compression;
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct svalue * work;
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args < 1) wrong_number_of_args_error("write_open",args,1);
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args > 3) wrong_number_of_args_error("write_open",args,3);
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-args]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("write_open",1,"string");
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(file_name=Pike_sp[0-args].u.string);
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if ((args > 1) &&
    ((TYPEOF(Pike_sp[1-args]) != PIKE_T_INT) ||
     (SUBTYPEOF(Pike_sp[1-args]) != NUMBER_UNDEFINED))) {
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[1-args]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("write_open",2,"int|void");
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
compression=Pike_sp+1-args; dmalloc_touch_svalue(Pike_sp+1-args);
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
} else compression = NULL;
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if ((args > 2) &&
    ((TYPEOF(Pike_sp[2-args]) != PIKE_T_INT) ||
     (SUBTYPEOF(Pike_sp[2-args]) != NUMBER_UNDEFINED))) {
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[2-args]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("write_open",3,"int|void");
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
work=Pike_sp+2-args; dmalloc_touch_svalue(Pike_sp+2-args);
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
} else work = NULL;
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
int work_factor = DEFAULT_WORK_FACTOR;
int compression_rate = DEFAULT_COMPRESSION_RATE;
FILE *tmp = NULL;

if(compression)
{
compression_rate = compression->u.integer;
if(compression_rate < 1 || compression_rate > 9){
Pike_error("Compression rate %d is out of range for "
"Bz2.File()->write_open().\n", compression_rate);
}
}

if(work)
{
work_factor = work->u.integer;
if(work_factor < 1 || work_factor > 250){
Pike_error("Work factor %d is out of range for "
"Bz2.File()->write_open().\n", work_factor);
}
}

if (THIS->mode == NO_FILE_MODE)
tmp = fopen(file_name->str,"wb");

if(tmp != NULL){
THIS->file = bzWriteOpen(&(THIS->bzerror),tmp,compression_rate,0,work_factor);

switch(THIS->bzerror){
case BZ_OK:
break;
default:
fclose(tmp);
Pike_error("Error in Bz2.File()->write_open(): %d\n", THIS->bzerror);
}
THIS->fp = tmp;
THIS->mode = WRITE_MODE;
}

if(tmp == NULL)
do { INT_TYPE ret_=((0)); pop_n_elems(args); push_int(ret_); return; }while(0);do { INT_TYPE ret_=(
#line 899 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
(1)); pop_n_elems(args); push_int(ret_); return; }while(0);
#line 900 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}

}
/*! @decl int(0..1) open(string file, void|string mode)
   *! Opens a file for I/O.
   *! @param file
   *! The name of the file to be opened
   *! @param mode
   *! Mode for the file operations. Can be either "r" (read)
   *! or "w". Read is default.
   */

#define f_Bz2_File_open_defined

#ifdef f_Bz2_File_open_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_File_open_fun_num = 0;

#endif /* f_Bz2_File_open_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_File_open(INT32 args) {
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * filename;
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * mode;
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args < 1) wrong_number_of_args_error("open",args,1);
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args > 2) wrong_number_of_args_error("open",args,2);
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-args]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("open",1,"string");
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(filename=Pike_sp[0-args].u.string);
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if (args > 1 &&
    (TYPEOF(Pike_sp[1-args]) != PIKE_T_INT ||
     Pike_sp[1-args].u.integer)) {
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[1-args]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("open",2,"string|void");
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(mode=Pike_sp[1-args].u.string);
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
} else mode = NULL;
{
if (!mode)
{
f_Bz2_File_read_open(1);
return;
}

if (mode->len==1)
{
if (mode->str[0]=='w')
{
pop_stack();
f_Bz2_File_write_open(1);
return;
}
if (mode->str[0]=='r')
{
pop_stack();
f_Bz2_File_read_open(1);
return;
}
}

Pike_error("Unknown open mode for file, "
"should be either 'w' or 'r'.\n");
}


}
/*! @decl void create()
   *! @decl void create(string filename, void|string mode)
   *! Creates a Bz2.File object
   */

#define f_Bz2_File_create_defined

#ifdef f_Bz2_File_create_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_File_create_fun_num = 0;

#endif /* f_Bz2_File_create_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_File_create(INT32 args) {
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * file;
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * mode;
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args > 2) wrong_number_of_args_error("create",args,2);
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if (args > 0 &&
    (TYPEOF(Pike_sp[0-args]) != PIKE_T_INT ||
     Pike_sp[0-args].u.integer)) {
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-args]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("create",1,"void|string");
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(file=Pike_sp[0-args].u.string);
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
} else file = NULL;
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if (args > 1 &&
    (TYPEOF(Pike_sp[1-args]) != PIKE_T_INT ||
     Pike_sp[1-args].u.integer)) {
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[1-args]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("create",2,"void|string");
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(mode=Pike_sp[1-args].u.string);
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
} else mode = NULL;
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
if(THIS->file != NULL){
f_Bz2_File_close(0);
}
THIS->small = 0;
THIS->bzerror = 0;
THIS->mode = NO_FILE_MODE;
THIS->file = NULL;
THIS->fp = NULL;

if(args != 0){
f_Bz2_File_open(args);
}
}

}
/*! @decl int write(string data)
   *! Writes the data to the file.
   *! @returns 
   *!  the number of bytes written to the file.
   */

#define f_Bz2_File_write_defined

#ifdef f_Bz2_File_write_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_File_write_fun_num = 0;

#endif /* f_Bz2_File_write_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_File_write(INT32 args) {
#line 966 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct pike_string * data;
#line 966 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args != 1) wrong_number_of_args_error("write",args,1);
#line 966 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-1]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("write",1,"string");
#line 966 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
debug_malloc_pass(data=Pike_sp[0-1].u.string);
#line 966 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
int len = data->len;

bzWrite(&(THIS->bzerror), THIS->file, data->str,len);
switch(THIS->bzerror){
case BZ_OK:
break;
default:
Pike_error("Error in Bz2.File()->write().\n");
}

do { INT_TYPE ret_=((len)); pop_stack(); push_int(ret_); return; }while(0);
#line 978 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}

}
/*! @decl string read(int len)
   *! Reads len (uncompressed) bytes from the file.
   *! If len is omitted the whole file is read.
   *! If read is unsuccessful, 0 is returned.
   */

#define f_Bz2_File_read_defined

#ifdef f_Bz2_File_read_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_File_read_fun_num = 0;

#endif /* f_Bz2_File_read_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_File_read(INT32 args) {
#line 986 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
struct svalue * bytes;
#line 986 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args > 1) wrong_number_of_args_error("read",args,1);
#line 986 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if ((args > 0) &&
    ((TYPEOF(Pike_sp[0-args]) != PIKE_T_INT) ||
     (SUBTYPEOF(Pike_sp[0-args]) != NUMBER_UNDEFINED))) {
#line 986 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(TYPEOF(Pike_sp[0-args]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("read",1,"int|void");
#line 986 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
bytes=Pike_sp+0-args; dmalloc_touch_svalue(Pike_sp+0-args);
#line 986 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
} else bytes = NULL;
#line 986 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
void *buf = NULL;
struct string_builder retstr;
int to_be_read = INFL_BUF_SIZE;
int was_read;

if( THIS->bzerror == BZ_STREAM_END )
{
pop_n_elems(args);
push_empty_string();
return;
}

if (bytes)
to_be_read = bytes->u.integer;

init_string_builder(&retstr, 0);
buf = xalloc(to_be_read);

while( to_be_read>0 && THIS->bzerror == BZ_OK )
{
was_read = bzRead( &(THIS->bzerror), THIS->file, buf, to_be_read );
string_builder_binary_strcat0( &retstr, buf, was_read );
if( args != 0 )
to_be_read -= was_read;

switch(THIS->bzerror){
case BZ_OK:
case BZ_STREAM_END:
break;
default:
free(buf);
Pike_error("Error in Bz2.File()->read().\n");
}
}

free(buf);
do { struct pike_string * ret_=(finish_string_builder(&retstr)); pop_n_elems(args); push_string(ret_); return; }while(0);
#line 1024 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}


}
/*! @decl int(0..1) eof()
   *! @returns 
   *!  1 if EOF has been reached,
   *!  0 otherwise
   */

#define f_Bz2_File_eof_defined

#ifdef f_Bz2_File_eof_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Bz2_File_eof_fun_num = 0;

#endif /* f_Bz2_File_eof_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Bz2_File_eof(INT32 args) {
#line 1033 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
if(args != 0) wrong_number_of_args_error("eof",args,0);
{
if(THIS->bzerror == BZ_STREAM_END)
do { INT_TYPE ret_=((1));  push_int(ret_); return; }while(0);do { INT_TYPE ret_=(
#line 1037 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
(0));  push_int(ret_); return; }while(0);
#line 1038 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
}

}

#undef internal_init_Bz2_File_defined
#define internal_init_Bz2_File_defined

#undef Bz2_File_event_handler_defined
#define Bz2_File_event_handler_defined
static void init_Bz2_File_struct(void)
#line 1041 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
THIS->small = 0;
THIS->file = NULL;
THIS->bzerror = 0;
THIS->mode = NO_FILE_MODE;
THIS->file = NULL;
}


#undef internal_exit_Bz2_File_defined
#define internal_exit_Bz2_File_defined

#undef Bz2_File_event_handler_defined
#define Bz2_File_event_handler_defined
static void exit_Bz2_File_struct(void)
#line 1051 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
{
if(THIS->fp != NULL){
switch(THIS->mode){
case READ_MODE:
bzReadClose(&(THIS->bzerror),THIS->file);
break;
case WRITE_MODE:
bzWriteClose ( &(THIS->bzerror), THIS->file,0,NULL,NULL);
break;
case NO_FILE_MODE:
break;
#ifdef PIKE_DEBUG
 default:
Pike_error("This error can never occur.\n");
#endif
 }
fclose(THIS->fp);
THIS->fp = NULL;
THIS->mode = NO_FILE_MODE;
}
}

#ifdef Bz2_File_event_handler_defined
static void Bz2_File_event_handler(int ev) {
  switch(ev) {

#ifdef internal_init_Bz2_File_defined
  case PROG_EVENT_INIT: init_Bz2_File_struct(); break;

#endif /* internal_init_Bz2_File_defined */

#ifdef internal_exit_Bz2_File_defined
  case PROG_EVENT_EXIT: exit_Bz2_File_struct(); break;

#endif /* internal_exit_Bz2_File_defined */
  default: break; 
  }
}

#endif /* Bz2_File_event_handler_defined */
#line 1074 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#endif	/* !__NT__ */

#endif
#endif  /* end HAVE_LIBLIBZIP2 */

/*! @endclass
 */

/*! @endmodule
 */

PIKE_MODULE_INIT
{
#ifdef HAVE_BZ2LIB
#ifdef HAVE_BZLIB_H
 add_integer_constant("BZ_RUN", BZ_RUN, 0);
add_integer_constant("BZ_FLUSH", BZ_FLUSH, 0);
add_integer_constant("BZ_FINISH", BZ_FINISH, 0);

#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
set_program_id_to_id( ___cmod_map_program_ids );

#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */

#ifdef class_Bz2_Deflate_defined

#ifdef PROG_BZ2_DEFLATE_ID
#line 132 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  START_NEW_PROGRAM_ID(BZ2_DEFLATE);
#else
#line 132 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  start_new_program();

#endif /* PROG_BZ2_DEFLATE_ID */
#line 132 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  Bz2_Deflate_program = Pike_compiler->new_program;

#ifndef tObjImpl_BZ2_DEFLATE

#undef tObjIs_BZ2_DEFLATE
#define tObjIs_BZ2_DEFLATE "\3\1\177\0\0\2"

#undef tObjImpl_BZ2_DEFLATE
#define tObjImpl_BZ2_DEFLATE "\3\0\177\0\0\2"

#endif /* tObjImpl_BZ2_DEFLATE */

#ifdef THIS_BZ2_DEFLATE
  Bz2_Deflate_storage_offset = ADD_STORAGE(struct Bz2_Deflate_struct);
#endif /* THIS_BZ2_DEFLATE */

#ifdef Bz2_Deflate_event_handler_defined
  pike_set_prog_event_callback(Bz2_Deflate_event_handler);

#ifndef Bz2_Deflate_gc_live_obj
  Pike_compiler->new_program->flags &= ~PROGRAM_LIVE_OBJ;

#endif /* Bz2_Deflate_gc_live_obj */

#endif /* Bz2_Deflate_event_handler_defined */

#ifdef f_Bz2_Deflate_create_defined

#ifdef f_Bz2_Deflate_create_fun_num_used
  f_Bz2_Deflate_create_fun_num =
#endif /* f_Bz2_Deflate_create_fun_num_used */
#line 142 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("create", f_Bz2_Deflate_create, tFunc(tOr("\10\200\0\0\0\177\377\377\377",tVoid) tOr("\10\200\0\0\0\177\377\377\377",tVoid),tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_Deflate_create_defined */

#ifdef f_Bz2_Deflate_feed_defined

#ifdef f_Bz2_Deflate_feed_fun_num_used
  f_Bz2_Deflate_feed_fun_num =
#endif /* f_Bz2_Deflate_feed_fun_num_used */
#line 209 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("feed", f_Bz2_Deflate_feed, tFunc(tStr,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_Deflate_feed_defined */

#ifdef f_Bz2_Deflate_read_defined

#ifdef f_Bz2_Deflate_read_fun_num_used
  f_Bz2_Deflate_read_fun_num =
#endif /* f_Bz2_Deflate_read_fun_num_used */
#line 313 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("read", f_Bz2_Deflate_read, tFunc(tStr,tStr), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_Deflate_read_defined */

#ifdef f_Bz2_Deflate_finish_defined

#ifdef f_Bz2_Deflate_finish_fun_num_used
  f_Bz2_Deflate_finish_fun_num =
#endif /* f_Bz2_Deflate_finish_fun_num_used */
#line 369 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("finish", f_Bz2_Deflate_finish, tFunc(tStr,tStr), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_Deflate_finish_defined */

#ifdef f_Bz2_Deflate_deflate_defined

#ifdef f_Bz2_Deflate_deflate_fun_num_used
  f_Bz2_Deflate_deflate_fun_num =
#endif /* f_Bz2_Deflate_deflate_fun_num_used */
#line 462 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("deflate", f_Bz2_Deflate_deflate, tFunc(tStr tOr("\10\200\0\0\0\177\377\377\377",tVoid),tStr), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_Deflate_deflate_defined */
#line 132 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  Bz2_Deflate_program=end_program();
#line 132 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#ifdef Bz2_Deflate_program_fun_num_used
Bz2_Deflate_program_fun_num=
#endif
 add_program_constant("Deflate",Bz2_Deflate_program,0);

#endif /* class_Bz2_Deflate_defined */

#ifdef class_Bz2_Inflate_defined

#ifdef PROG_BZ2_INFLATE_ID
#line 530 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  START_NEW_PROGRAM_ID(BZ2_INFLATE);
#else
#line 530 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  start_new_program();

#endif /* PROG_BZ2_INFLATE_ID */
#line 530 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  Bz2_Inflate_program = Pike_compiler->new_program;

#ifndef tObjImpl_BZ2_INFLATE

#undef tObjIs_BZ2_INFLATE
#define tObjIs_BZ2_INFLATE "\3\1\177\0\0\3"

#undef tObjImpl_BZ2_INFLATE
#define tObjImpl_BZ2_INFLATE "\3\0\177\0\0\3"

#endif /* tObjImpl_BZ2_INFLATE */

#ifdef THIS_BZ2_INFLATE
  Bz2_Inflate_storage_offset = ADD_STORAGE(struct Bz2_Inflate_struct);
#endif /* THIS_BZ2_INFLATE */

#ifdef Bz2_Inflate_event_handler_defined
  pike_set_prog_event_callback(Bz2_Inflate_event_handler);

#ifndef Bz2_Inflate_gc_live_obj
  Pike_compiler->new_program->flags &= ~PROGRAM_LIVE_OBJ;

#endif /* Bz2_Inflate_gc_live_obj */

#endif /* Bz2_Inflate_event_handler_defined */

#ifdef f_Bz2_Inflate_create_defined

#ifdef f_Bz2_Inflate_create_fun_num_used
  f_Bz2_Inflate_create_fun_num =
#endif /* f_Bz2_Inflate_create_fun_num_used */
#line 540 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("create", f_Bz2_Inflate_create, tFunc(tNone,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_Inflate_create_defined */

#ifdef f_Bz2_Inflate_inflate_defined

#ifdef f_Bz2_Inflate_inflate_fun_num_used
  f_Bz2_Inflate_inflate_fun_num =
#endif /* f_Bz2_Inflate_inflate_fun_num_used */
#line 590 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("inflate", f_Bz2_Inflate_inflate, tFunc(tStr,tStr), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_Inflate_inflate_defined */
#line 530 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  Bz2_Inflate_program=end_program();
#line 530 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#ifdef Bz2_Inflate_program_fun_num_used
Bz2_Inflate_program_fun_num=
#endif
 add_program_constant("Inflate",Bz2_Inflate_program,0);

#endif /* class_Bz2_Inflate_defined */

#ifdef class_Bz2_File_defined

#ifdef PROG_BZ2_FILE_ID
#line 768 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  START_NEW_PROGRAM_ID(BZ2_FILE);
#else
#line 768 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  start_new_program();

#endif /* PROG_BZ2_FILE_ID */
#line 768 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  Bz2_File_program = Pike_compiler->new_program;

#ifndef tObjImpl_BZ2_FILE

#undef tObjIs_BZ2_FILE
#define tObjIs_BZ2_FILE "\3\1\177\0\0\4"

#undef tObjImpl_BZ2_FILE
#define tObjImpl_BZ2_FILE "\3\0\177\0\0\4"

#endif /* tObjImpl_BZ2_FILE */

#ifdef THIS_BZ2_FILE
  Bz2_File_storage_offset = ADD_STORAGE(struct Bz2_File_struct);
#endif /* THIS_BZ2_FILE */

#ifdef Bz2_File_event_handler_defined
  pike_set_prog_event_callback(Bz2_File_event_handler);

#ifndef Bz2_File_gc_live_obj
  Pike_compiler->new_program->flags &= ~PROGRAM_LIVE_OBJ;

#endif /* Bz2_File_gc_live_obj */

#endif /* Bz2_File_event_handler_defined */

#ifdef f_Bz2_File_close_defined

#ifdef f_Bz2_File_close_fun_num_used
  f_Bz2_File_close_fun_num =
#endif /* f_Bz2_File_close_fun_num_used */
#line 780 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("close", f_Bz2_File_close, tFunc(tNone,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_File_close_defined */

#ifdef f_Bz2_File_read_open_defined

#ifdef f_Bz2_File_read_open_fun_num_used
  f_Bz2_File_read_open_fun_num =
#endif /* f_Bz2_File_read_open_fun_num_used */
#line 812 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("read_open", f_Bz2_File_read_open, tFunc(tStr,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_File_read_open_defined */

#ifdef f_Bz2_File_write_open_defined

#ifdef f_Bz2_File_write_open_fun_num_used
  f_Bz2_File_write_open_fun_num =
#endif /* f_Bz2_File_write_open_fun_num_used */
#line 856 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("write_open", f_Bz2_File_write_open, tFunc(tStr tOr("\10\200\0\0\0\177\377\377\377",tVoid) tOr("\10\200\0\0\0\177\377\377\377",tVoid),"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_File_write_open_defined */

#ifdef f_Bz2_File_open_defined

#ifdef f_Bz2_File_open_fun_num_used
  f_Bz2_File_open_fun_num =
#endif /* f_Bz2_File_open_fun_num_used */
#line 911 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("open", f_Bz2_File_open, tFunc(tStr tOr(tStr,tVoid),"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_File_open_defined */

#ifdef f_Bz2_File_create_defined

#ifdef f_Bz2_File_create_fun_num_used
  f_Bz2_File_create_fun_num =
#endif /* f_Bz2_File_create_fun_num_used */
#line 945 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("create", f_Bz2_File_create, tFunc(tOr(tVoid,tStr) tOr(tVoid,tStr),tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_File_create_defined */

#ifdef f_Bz2_File_write_defined

#ifdef f_Bz2_File_write_fun_num_used
  f_Bz2_File_write_fun_num =
#endif /* f_Bz2_File_write_fun_num_used */
#line 966 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("write", f_Bz2_File_write, tFunc(tStr,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_File_write_defined */

#ifdef f_Bz2_File_read_defined

#ifdef f_Bz2_File_read_fun_num_used
  f_Bz2_File_read_fun_num =
#endif /* f_Bz2_File_read_fun_num_used */
#line 986 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("read", f_Bz2_File_read, tFunc(tOr("\10\200\0\0\0\177\377\377\377",tVoid),tStr), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_File_read_defined */

#ifdef f_Bz2_File_eof_defined

#ifdef f_Bz2_File_eof_fun_num_used
  f_Bz2_File_eof_fun_num =
#endif /* f_Bz2_File_eof_fun_num_used */
#line 1033 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    ADD_FUNCTION2("eof", f_Bz2_File_eof, tFunc(tNone,"\10\0\0\0\0\0\0\0\1"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Bz2_File_eof_defined */
#line 768 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
  Bz2_File_program=end_program();
#line 768 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#ifdef Bz2_File_program_fun_num_used
Bz2_File_program_fun_num=
#endif
 add_program_constant("File",Bz2_File_program,0);

#endif /* class_Bz2_File_defined */

#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
set_program_id_to_id( 0 );
#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */
#line 1093 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#endif
#endif
}

PIKE_MODULE_EXIT
{
#ifdef HAVE_BZ2LIB
#ifdef HAVE_BZLIB_H	
 
#ifdef class_Bz2_Deflate_defined
  if(Bz2_Deflate_program) {
#line 132 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    free_program(Bz2_Deflate_program);
    Bz2_Deflate_program=0;
  }

#endif /* class_Bz2_Deflate_defined */

#ifdef class_Bz2_Inflate_defined
  if(Bz2_Inflate_program) {
#line 530 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    free_program(Bz2_Inflate_program);
    Bz2_Inflate_program=0;
  }

#endif /* class_Bz2_Inflate_defined */

#ifdef class_Bz2_File_defined
  if(Bz2_File_program) {
#line 768 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
    free_program(Bz2_File_program);
    Bz2_File_program=0;
  }

#endif /* class_Bz2_File_defined */
#line 1102 "/home/hww3/pike-git/src/post_modules/Bz2/libbzip2mod.cmod"
#endif
#endif
}




#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
static int ___cmod_map_program_ids(int id)
{
  int i = 0;
  if( (id&0x7f000000) != 0x7f000000 ) return id;
  id = id&0x00ffffff;
#ifdef class_Bz2_File_defined
if(id==4) return Bz2_File_program->id;
#endif
#ifdef class_Bz2_Deflate_defined
if(id==2) return Bz2_Deflate_program->id;
#endif
#ifdef class_Bz2_Inflate_defined
if(id==3) return Bz2_Inflate_program->id;
#endif
  return 0;
}
#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */
