Example #1
0
int zopfli_deflate(
	unsigned char *dest,
	size_t *destLen,
	const unsigned char *source,
	size_t sourceLen,
	int level
){
#ifdef FEOS
	return -1;
#else
	ZopfliOptions options;
	ZopfliInitOptions(&options);
	options.numiterations=level;
	size_t __compsize=0;
	unsigned char bp = 0;
	unsigned char *COMPBUF=NULL;
	ZopfliDeflate(&options, 2 /* Dynamic block */, 1, source, sourceLen, &bp, &COMPBUF, &__compsize);
	if(__compsize<=*destLen){
		memcpy(dest,COMPBUF,__compsize);
		*destLen=__compsize;
	}
	free(COMPBUF);
	return __compsize>*destLen;
#endif
}
Example #2
0
/* You can use this function in Your own lib calls/applications.
   ZopfliFormat is required but can be passed as simple number.
   ZopfliOptions, ZopfliPredefinedSplits and ZopfliAdditionalData
   structures are optional and can be NULL.

   ZopfliPredefinedSplits structure holds 3 variables being
   an array of block split offsets, number of block split offsets
   and if to perform additional auto splitting between predefined
   split points (0 or 1). size_t array, size_t and int.

   ZopfliAdditonalData structure can hold 2 variables being
   file timestamp and name. If timestamp is omitted, lower possible
   value will be used for GZ and ZIP. If file name is omitted
   GZ will not store filename and ZIP will use input data CRC32 as
   file name.
*/
DLL_PUBLIC void ZopfliCompress(ZopfliOptions* options, const ZopfliFormat output_type,
                    const unsigned char* in, size_t insize,
                    unsigned char** out, size_t* outsize, ZopfliPredefinedSplits* sp,
                    const ZopfliAdditionalData* moredata) {
  if(in == NULL || out == NULL || outsize == NULL) {
    fprintf(stderr,"Critical Error: one or more required pointers are NULL\n");
    exit(EXIT_FAILURE);
  } else {
    ZopfliOptions* optionslib = Zmalloc(sizeof(*optionslib));
    if(options == NULL) {
      ZopfliInitOptions(optionslib);
      optionslib->verbose = 0;
    } else {
      optionslib = options;
      mui = options->maxfailiterations;
    }
    if (output_type == ZOPFLI_FORMAT_GZIP || output_type == ZOPFLI_FORMAT_GZIP_NAME) {
      ZopfliGzipCompress(optionslib, in, insize, out, outsize, sp, moredata);
    } else if (output_type == ZOPFLI_FORMAT_ZLIB) {
      ZopfliZlibCompress(optionslib, in, insize, out, outsize, sp);
    } else if (output_type == ZOPFLI_FORMAT_ZIP) {
      ZopfliZipCompress(optionslib, in, insize, out, outsize, sp, moredata);
    } else if (output_type == ZOPFLI_FORMAT_DEFLATE) {
      unsigned char bp = 0;
      ZopfliDeflate(optionslib, 2 /* Dynamic block */, 1,
                    in, insize, &bp, out, outsize, sp);
    } else {
      fprintf(stderr,"Error: No output format specified.\n");
      exit (EXIT_FAILURE);
    }
    free(optionslib);
  }
}
Example #3
0
/*
Compresses the data according to the gzip specification.
*/
void ZopfliGzipCompress(const ZopfliOptions* options,
                        const unsigned char* in, size_t insize,
                        unsigned char** out, size_t* outsize) {
  unsigned long crcvalue = lodepng_crc32(in, insize);
  unsigned char bp = 0;

  ZOPFLI_APPEND_DATA(31, out, outsize);  /* ID1 */
  ZOPFLI_APPEND_DATA(139, out, outsize);  /* ID2 */
  ZOPFLI_APPEND_DATA(8, out, outsize);  /* CM */
  ZOPFLI_APPEND_DATA(0, out, outsize);  /* FLG */
  /* MTIME */
  ZOPFLI_APPEND_DATA(0, out, outsize);
  ZOPFLI_APPEND_DATA(0, out, outsize);
  ZOPFLI_APPEND_DATA(0, out, outsize);
  ZOPFLI_APPEND_DATA(0, out, outsize);

  ZOPFLI_APPEND_DATA(2, out, outsize);  /* XFL, 2 indicates best compression. */
  ZOPFLI_APPEND_DATA(3, out, outsize);  /* OS follows Unix conventions. */

  ZopfliDeflate(options, 2 /* Dynamic block */, 1,
                in, insize, &bp, out, outsize);

  /* CRC */
  ZOPFLI_APPEND_DATA(crcvalue % 256, out, outsize);
  ZOPFLI_APPEND_DATA((crcvalue >> 8) % 256, out, outsize);
  ZOPFLI_APPEND_DATA((crcvalue >> 16) % 256, out, outsize);
  ZOPFLI_APPEND_DATA((crcvalue >> 24) % 256, out, outsize);

  /* ISIZE */
  ZOPFLI_APPEND_DATA(insize % 256, out, outsize);
  ZOPFLI_APPEND_DATA((insize >> 8) % 256, out, outsize);
  ZOPFLI_APPEND_DATA((insize >> 16) % 256, out, outsize);
  ZOPFLI_APPEND_DATA((insize >> 24) % 256, out, outsize);

  if (options->verbose) {
    ZopfliPrintSizeVerbose(insize, *outsize, "Gzip");
  }
}
Example #4
0
size_t Gz::Leanify(size_t size_leanified /*= 0*/)
{
    // written according to this specification
    // http://www.gzip.org/zlib/rfc-gzip.html

    if (size <= 18)
    {
        std::cerr << "Not a valid GZ file." << std::endl;
        return Format::Leanify(size_leanified);
    }

    depth++;
    char flags = *(fp + 3);
    // set the flags to 0, remove all unnecessary section
    *(fp + 3 - size_leanified) = 0;

    char *p_read = fp + 10;
    char *p_write = p_read - size_leanified;

    *(p_write - 2) = 2;     // XFL

    if (flags & (1 << 2))   // FEXTRA
    {
        p_read += *(uint16_t *)p_read + 2;
    }

    std::string filename;
    if (flags & (1 << 3))   // FNAME
    {
        for (int i = 1; i < depth; i++)
        {
            std::cout << "-> ";
        }
        std::cout << p_read << std::endl;
        filename = std::string(p_read);
        while (p_read < fp + size && *p_read++)
        {
            // skip string
        }
    }

    if (flags & (1 << 4))   // FCOMMENT
    {
        while (p_read < fp + size && *p_read++)
        {
            // skip string
        }
    }

    if (flags & (1 << 1))   // FHCRC
    {
        p_read += 2;
    }

    if (p_read >= fp + size)
    {
        return Format::Leanify(size_leanified);
    }

    if (size_leanified)
    {
        memmove(fp - size_leanified, fp, 10);
    }

    if (is_fast)
    {
        memmove(p_write, p_read, fp + size - p_read);
        return size - (p_read - p_write);
    }

    uint32_t uncompressed_size = *(uint32_t *)(fp + size - 4);
    uint32_t crc = *(uint32_t *)(fp + size - 8);
    size_t original_size = fp + size - 8 - p_read;

    size_t s = 0;
    unsigned char *buffer = (unsigned char *)tinfl_decompress_mem_to_heap(p_read, original_size, &s, 0);

    if (!buffer ||
        s != uncompressed_size ||
        crc != mz_crc32(0, buffer, uncompressed_size))
    {
        std::cerr << "GZ corrupted!" << std::endl;
        mz_free(buffer);
        memmove(p_write, p_read, original_size + 8);
        return size - (p_read - p_write);
    }

    uncompressed_size = LeanifyFile(buffer, uncompressed_size, 0, filename);

    ZopfliOptions options;
    ZopfliInitOptions(&options);
    options.numiterations = iterations;

    unsigned char bp = 0, *out = NULL;
    size_t outsize = 0;
    ZopfliDeflate(&options, 2, 1, buffer, uncompressed_size, &bp, &out, &outsize);


    if (outsize < original_size)
    {
        memcpy(p_write, out, outsize);
        p_write += outsize;
        *(uint32_t *)p_write = mz_crc32(0, buffer, uncompressed_size);
        *(uint32_t *)(p_write + 4) = uncompressed_size;
    }
    else
    {
        memmove(p_write, p_read, original_size + 8);
        p_write += original_size;
    }
    mz_free(buffer);
    delete[] out;
    depth--;
    fp -= size_leanified;
    return p_write + 8 - fp;
}