/*
 * miniz.h
 *
 *  Created on: 04/ago/2017
 *      Author: chessaa
 */

#ifndef MINIZ_H_
#define MINIZ_H_

/*
 * miniz.h: customized interface file to a mybe "reduced" and customized minz.c
 *
 * miniz.c s apublic domain, "unlicensed" deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing
 * from Rich Geldreich <richgel99@gmail.com>, last updated May 20, 2012
 * For more information about licence, please refer to <http://unlicense.org/>
 */

/* parasoft off
 *   Thist part file
 */

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

// Heap allocation callbacks.
// Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long.
typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size);
typedef void (*mz_free_func)(void *opaque, void *address);
typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size);

//typedef unsigned long mz_ulong;
#define mz_ulong unsigned long

struct mz_internal_state;  /*forward declaration*/

typedef struct mz_stream_s
{
  const unsigned char *next_in;     // pointer to next byte to read
  unsigned int avail_in;            // number of bytes available at next_in
  mz_ulong total_in;                // total number of bytes consumed so far

  unsigned char *next_out;          // pointer to next byte to write
  unsigned int avail_out;           // number of bytes that can be written to next_out
  mz_ulong total_out;               // total number of bytes produced so far

  char *msg;                        // error msg (unused)
  struct mz_internal_state *state;  // internal state, allocated by zalloc/zfree

  mz_alloc_func zalloc;             // optional heap allocation function (defaults to malloc)
  mz_free_func zfree;               // optional heap free function (defaults to free)
  void *opaque;                     // heap alloc function user pointer

  int data_type;                    // data_type (unused)
  mz_ulong adler;                   // adler32 of the source or uncompressed data
  mz_ulong reserved;                // not used
} mz_stream;

typedef mz_stream* mz_streamp;

// Initializes a decompressor.
int mz_inflateInit(mz_streamp pStream);

// mz_inflateInit2() is like mz_inflateInit() with an additional option that controls the window size and whether or not the stream has been wrapped with a zlib header/footer:
// window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate).
//int mz_inflateInit2(mz_streamp pStream, int window_bits);

// Decompresses the input stream to the output, consuming only as much of the input as needed, and writing as much to the output as possible.
// Parameters:
//   pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members.
//   flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH.
//   On the first call, if flush is MZ_FINISH it's assumed the input and output buffers are both sized large enough to decompress the entire stream in a single call (this is slightly faster).
//   MZ_FINISH implies that there are no more source bytes available beside what's already in the input buffer, and that the output buffer is large enough to hold the rest of the decompressed data.
// Return values:
//   MZ_OK on success. Either more input is needed but not available, and/or there's more output to be written but the output buffer is full.
//   MZ_STREAM_END if all needed input has been consumed and all output bytes have been written. For zlib streams, the adler-32 of the decompressed data has also been verified.
//   MZ_STREAM_ERROR if the stream is bogus.
//   MZ_DATA_ERROR if the deflate stream is invalid.
//   MZ_PARAM_ERROR if one of the parameters is invalid.
//   MZ_BUF_ERROR if no forward progress is possible because the input buffer is empty but the inflater needs more input to continue, or if the output buffer is not large enough. Call mz_inflate() again
//   with more input data, or with more room in the output buffer (except when using single call decompression, described above).
int mz_inflate(mz_streamp pStream, int flush);

// Deinitializes a decompressor.
int mz_inflateEnd(mz_streamp pStream);

// Single-call decompression.
// Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure.
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);

// Returns a string description of the specified error code, or NULL if the error code is invalid.
const char *mz_error(int err);

struct zip_file_header_t
{
  unsigned char header_signature[4];
  unsigned char ver_extractor[2];
  unsigned char flags[2];
  unsigned char comp_method[2];
  unsigned char mod_time[2];
  unsigned char mod_date[2];
  unsigned char crc32[4];
  unsigned char csize[4];
  unsigned char usize[4];
  unsigned char fname_len[2];
  unsigned char extra_len[2];
};

/*
struct mz_zip_info_t
{
  int valid;
  int compressed;
  unsigned int compressed_size;
  unsigned int uncompressed_size;
  const char* filename;
  unsigned int crc32;
  unsigned int data_offset;
  const void* data_start;
};
*/

typedef enum
{
  MZ_ZIP_FLAG_CASE_SENSITIVE                = 0x0100,
  MZ_ZIP_FLAG_IGNORE_PATH                   = 0x0200,
  MZ_ZIP_FLAG_COMPRESSED_DATA               = 0x0400,
  MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800
} mz_zip_flags;

int mz_fix_zip_header_uncompressed(const void* const data, unsigned int fixed_size);


mz_ulong mz_crc32_ext(mz_ulong crc, const void *ptr, size_t buf_len);
mz_ulong mz_crc32(const void *ptr, size_t buf_len);


unsigned long mz_b2l(const unsigned char* b);
unsigned short mz_b2s(const unsigned char* b);
void mz_s2b(unsigned char* b, unsigned short s);
void mz_l2b(unsigned char* b, unsigned long s);

#ifdef __cplusplus
}
#endif



#endif /* MINIZ_H_ */
