Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
   uint step = 0;
   int cmp_status;
   uLong src_len = (uLong)strlen(s_pStr);
   uLong cmp_len = compressBound(src_len);
   uLong uncomp_len = src_len;
   uint8 *pCmp, *pUncomp;
   uint total_succeeded = 0;
   argc, argv;

   printf("LZHAM example2 (single function call compression/decompression)\nUsing library version: %s\n", ZLIB_VERSION);

   do
   {
      // Allocate buffers to hold compressed and uncompressed data.
      pCmp = (uint8 *)malloc((size_t)cmp_len);
      pUncomp = (uint8 *)malloc((size_t)src_len);
      if ((!pCmp) || (!pUncomp))
      {
         printf("Out of memory!\n");
         return EXIT_FAILURE;
      }

      // Compress the string.
      cmp_status = compress(pCmp, &cmp_len, (const unsigned char *)s_pStr, src_len);
      if (cmp_status != Z_OK)
      {
         printf("compress() failed!\n");
         free(pCmp);
         free(pUncomp);
         return EXIT_FAILURE;
      }

      printf("Compressed from %u to %u bytes\n", src_len, cmp_len);

      if (step)
      {
         // Purposely corrupt the compressed data if fuzzy testing (this is a very crude fuzzy test).
         uint n = 1 + (rand() % 3);
         while (n--)
         {
            uint i = rand() % cmp_len;
            pCmp[i] ^= (rand() & 0xFF);
         }
      }

      // Decompress.
      cmp_status = uncompress(pUncomp, &uncomp_len, pCmp, cmp_len);
      total_succeeded += (cmp_status == Z_OK);

      if (step)
      {
         printf("Simple fuzzy test: step %u total_succeeded: %u\n", step, total_succeeded);
      }
      else
      {
         if (cmp_status != Z_OK)
         {
            printf("uncompress failed!\n");
            free(pCmp);
            free(pUncomp);
            return EXIT_FAILURE;
         }

         printf("Decompressed from %u to %u bytes\n", cmp_len, uncomp_len);

         // Ensure uncompress() returned the expected data.
         if ((uncomp_len != src_len) || (memcmp(pUncomp, s_pStr, (size_t)src_len)))
         {
            printf("Decompression failed!\n");
            free(pCmp);
            free(pUncomp);
            return EXIT_FAILURE;
         }
      }

      free(pCmp);
      free(pUncomp);

      step++;

      // Keep on fuzzy testing if there's a non-empty command line.
   } while (argc >= 2);  

   printf("Success.\n");
   return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
	void Run()
	{
		{ // ver 0

		SharedBuffer<u8> fromdata(4);
		fromdata[0]=1;
		fromdata[1]=5;
		fromdata[2]=5;
		fromdata[3]=1;
		
		std::ostringstream os(std::ios_base::binary);
		compress(fromdata, os, 0);

		std::string str_out = os.str();
		
		infostream<<"str_out.size()="<<str_out.size()<<std::endl;
		infostream<<"TestCompress: 1,5,5,1 -> ";
		for(u32 i=0; i<str_out.size(); i++)
		{
			infostream<<(u32)str_out[i]<<",";
		}
		infostream<<std::endl;

		assert(str_out.size() == 10);

		assert(str_out[0] == 0);
		assert(str_out[1] == 0);
		assert(str_out[2] == 0);
		assert(str_out[3] == 4);
		assert(str_out[4] == 0);
		assert(str_out[5] == 1);
		assert(str_out[6] == 1);
		assert(str_out[7] == 5);
		assert(str_out[8] == 0);
		assert(str_out[9] == 1);

		std::istringstream is(str_out, std::ios_base::binary);
		std::ostringstream os2(std::ios_base::binary);

		decompress(is, os2, 0);
		std::string str_out2 = os2.str();

		infostream<<"decompress: ";
		for(u32 i=0; i<str_out2.size(); i++)
		{
			infostream<<(u32)str_out2[i]<<",";
		}
		infostream<<std::endl;

		assert(str_out2.size() == fromdata.getSize());

		for(u32 i=0; i<str_out2.size(); i++)
		{
			assert(str_out2[i] == fromdata[i]);
		}

		}

		{ // ver HIGHEST

		SharedBuffer<u8> fromdata(4);
		fromdata[0]=1;
		fromdata[1]=5;
		fromdata[2]=5;
		fromdata[3]=1;
		
		std::ostringstream os(std::ios_base::binary);
		compress(fromdata, os, SER_FMT_VER_HIGHEST);

		std::string str_out = os.str();
		
		infostream<<"str_out.size()="<<str_out.size()<<std::endl;
		infostream<<"TestCompress: 1,5,5,1 -> ";
		for(u32 i=0; i<str_out.size(); i++)
		{
			infostream<<(u32)str_out[i]<<",";
		}
		infostream<<std::endl;

		/*assert(str_out.size() == 10);

		assert(str_out[0] == 0);
		assert(str_out[1] == 0);
		assert(str_out[2] == 0);
		assert(str_out[3] == 4);
		assert(str_out[4] == 0);
		assert(str_out[5] == 1);
		assert(str_out[6] == 1);
		assert(str_out[7] == 5);
		assert(str_out[8] == 0);
		assert(str_out[9] == 1);*/

		std::istringstream is(str_out, std::ios_base::binary);
		std::ostringstream os2(std::ios_base::binary);

		decompress(is, os2, SER_FMT_VER_HIGHEST);
		std::string str_out2 = os2.str();

		infostream<<"decompress: ";
		for(u32 i=0; i<str_out2.size(); i++)
		{
			infostream<<(u32)str_out2[i]<<",";
		}
		infostream<<std::endl;

		assert(str_out2.size() == fromdata.getSize());

		for(u32 i=0; i<str_out2.size(); i++)
		{
			assert(str_out2[i] == fromdata[i]);
		}

		}
	}
Ejemplo n.º 3
0
 void FPDFAPI_FlateCompress(unsigned char* dest_buf, unsigned long* dest_size, const unsigned char* src_buf, unsigned long src_size)
 {
     compress(dest_buf, dest_size, src_buf, src_size);
 }
Ejemplo n.º 4
0
Archivo: map.c Proyecto: slaeshjag/Muon
void map_save(MAP *map, const char *filename) {
	int i, j;
	LDI_MAIN m;
	LDI_ENTRY *entry;
	DARNIT_FILE *f;
	struct LDMZ ldmz;
	MAP_PROPERTY *stringtable, **st_add, **st_temp;
	char *stringtable_data, *p;
	int32_t *ref_data;
	unsigned long zsize;
	uint32_t (*layer_header)[8];
	uint32_t header[12];
	unsigned int stringtable_layerref;
	
	header[0]=ldmz.header.magic=LDMZ_MAGIC;
	header[1]=ldmz.header.version=LDMZ_VERSION;
	header[6]=ldmz.header.layers=map->map->layers;
	ldmz.layer=malloc(ldmz.header.layers*sizeof(struct LDMZ_LAYER));
	header[8]=ldmz.header.objects=0; /*map->map->objects; nope.jpg*/
	header[9]=ldmz.header.objects_zsize=0;
	ldmz.object=NULL;
	ldmz.objectz=NULL;
	
	
	ldmz.header.stringtable_size=0;
	for(st_add=&(map->stringtable), i=0; *st_add; st_add=&((*st_add)->next), i++)
		ldmz.header.stringtable_size+=strlen((*st_add)->key)+strlen((*st_add)->value)+2;
	/*So we can later clean out the added elements*/
	st_temp=st_add;
	*st_add=malloc(sizeof(MAP_PROPERTY));
	(*st_add)->key=(*st_add)->value=(void *) ((*st_add)->next=NULL);
	for(st_add=&((*st_add)->next), j=0; j<ldmz.header.layers*2; st_add=&((*st_add)->next), j++ ) {
		*st_add=malloc(sizeof(MAP_PROPERTY));
		(*st_add)->next=NULL;
		if(j&1) {
			(*st_add)->key=(*st_add)->value=NULL;
		} else {
			(*st_add)->key="NAME";
			(*st_add)->value="layer";
			ldmz.header.stringtable_size+=strlen((*st_add)->key)+strlen((*st_add)->value)+2;
		}
	}
	
	stringtable_data=malloc(header[2]=ldmz.header.stringtable_size);
	stringtable_layerref=i+1;
	header[10]=ldmz.header.refs=i+1+ldmz.header.layers*2;
	header[4]=ldmz.header.refs_size=sizeof(int32_t)*ldmz.header.refs*2;
	header[11]=ldmz.header.main_ref_index=0;
	ref_data=malloc(ldmz.header.refs_size);
	for(stringtable=map->stringtable, i=0, p=stringtable_data; stringtable; stringtable=stringtable->next) {
		unsigned int size;
		if(!stringtable->key) {
			ref_data[i++]=-1;
			ref_data[i++]=-1;
			continue;
		}
		strcpy(p, stringtable->key);
		size=strlen(stringtable->key)+1;
		ref_data[i++]=d_util_htonl(p-stringtable_data);
		p+=size;
		strcpy(p, stringtable->value);
		size=strlen(stringtable->value)+1;
		ref_data[i++]=d_util_htonl(p-stringtable_data);
		p+=size;
	}
	
	ldmz.stringtablez=malloc(zsize=compressBound(ldmz.header.stringtable_size));
	compress(ldmz.stringtablez, &zsize, (void *) stringtable_data, ldmz.header.stringtable_size);
	header[3]=ldmz.header.stringtable_zsize=zsize;
	free(stringtable_data);
	ldmz.refsz=malloc(zsize=compressBound(ldmz.header.refs_size));
	compress(ldmz.refsz, &zsize, (void *) ref_data, ldmz.header.refs_size);
	header[5]=ldmz.header.refs_zsize=zsize;
	free(ref_data);
	
	layer_header=malloc(sizeof(uint32_t)*8*ldmz.header.layers);
	for(i=0; i<map->map->layers; i++) {
		unsigned int size;
		unsigned int *data;
		layer_header[i][0]=d_util_htonl(ldmz.layer[i].width=map->map->layer[i].tilemap->w);
		layer_header[i][1]=d_util_htonl(ldmz.layer[i].height=map->map->layer[i].tilemap->h);
		layer_header[i][2]=d_util_htonl(ldmz.layer[i].offset_x=map->map->layer[i].offset_x);
		layer_header[i][3]=d_util_htonl(ldmz.layer[i].offset_y=map->map->layer[i].offset_y);
		
		layer_header[i][5]=d_util_htonl(ldmz.layer[i].ref_index=stringtable_layerref+i*2);
		layer_header[i][6]=d_util_htonl(ldmz.layer[i].tile_w=map->map->layer[i].tile_w);
		layer_header[i][7]=d_util_htonl(ldmz.layer[i].tile_h=map->map->layer[i].tile_h);
		
		size=sizeof(uint32_t)*map->map->layer[i].tilemap->w*map->map->layer[i].tilemap->h;
		ldmz.layer[i].dataz=malloc(zsize=compressBound(size));
		data=malloc(size);
		memcpy(data, map->map->layer[i].tilemap->data, size);
		d_util_endian_convert(data, size/sizeof(uint32_t));
		compress((void *) ldmz.layer[i].dataz, &zsize, (void *) data, size);
		free(data);
		layer_header[i][4]=d_util_htonl(ldmz.layer[i].zsize=((unsigned int) zsize));
	}
	ldmz.layer_headerz=malloc(zsize=compressBound(sizeof(uint32_t)*8*ldmz.header.layers));
	compress((void *) ldmz.layer_headerz, &zsize, (void *) layer_header, sizeof(uint32_t)*8*ldmz.header.layers);
	header[7]=ldmz.header.layer_headers_zsize=zsize;
	free(layer_header);
	
	if(!(f=d_file_open(filename, "wb"))) {
		ui_messagebox(font_std, "Unable to save map");
		goto map_save_error;
	}
	
	
	m.magic=d_util_htonl(LDI_MAGIC);
	m.version=d_util_htonl(LDI_VERSION);
	m.files=d_util_htonl(2);
	d_file_write(&m.magic, sizeof(uint32_t), f);
	d_file_write(&m.version, sizeof(uint32_t), f);
	d_file_write(&m.files, sizeof(uint32_t), f);
	m.files=d_util_ntohl(m.files);
	
	entry=calloc(sizeof(LDI_ENTRY), m.files);
	strcpy(entry[0].name, "mapdata/map.ldmz");
	entry[0].len=sizeof(uint32_t)*12+ldmz.header.stringtable_zsize+ldmz.header.refs_zsize+ldmz.header.layer_headers_zsize;
	for(i=0; i<ldmz.header.layers; i++)
		entry[0].len+=ldmz.layer[i].zsize;
	entry[0].len=d_util_htonl(entry[0].len);
	strcpy(entry[1].name, "mapdata/default.png");
	entry[1].pos=entry[0].len;
	entry[1].len=d_util_htonl(map->tilesheet_size);
	
	for(i=0; i<m.files; i++) {
		d_file_write(entry[i].name, LDI_FILENAME_LEN, f);
		d_file_write(&entry[i].pos, sizeof(uint32_t), f);
		d_file_write(&entry[i].len, sizeof(uint32_t), f);
		d_file_write(&entry[i].pad, sizeof(uint32_t), f);
	}
	free(entry);
	
	d_util_endian_convert(header, 12);
	d_file_write(&header, sizeof(uint32_t)*12, f);
	d_file_write(ldmz.stringtablez, ldmz.header.stringtable_zsize, f);
	d_file_write(ldmz.refsz, ldmz.header.refs_zsize, f);
	d_file_write(ldmz.layer_headerz, ldmz.header.layer_headers_zsize, f);
	for(i=0; i<ldmz.header.layers; i++)
		d_file_write(ldmz.layer[i].dataz, ldmz.layer[i].zsize, f);
	
	d_file_write(map->tilesheet, map->tilesheet_size, f);
	
	map_save_error:
	d_file_close(f);
	free(ldmz.stringtablez);
	free(ldmz.refsz);
	free(ldmz.layer_headerz);
	for(i=0; i<ldmz.header.layers; i++)
		free(ldmz.layer[i].dataz);
	free(ldmz.layer);
	while(*st_temp) {
		stringtable=(*st_temp)->next;
		free(*st_temp);
		*st_temp=stringtable;
	}
	
}
Ejemplo n.º 5
0
	mv mv_compress(const float *c, float epsilon/*= 0.0*/, int gu /*= ...*/) {
		float cc[4];
		int cgu;
		compress(c, cc, cgu, epsilon, gu);
		return mv(cgu, cc);
	}
Ejemplo n.º 6
0
void BLOCK::compress(                  // squash it up
                     const ICOORD vec  // and move
                    ) {
  box.move (vec);
  compress();
}
Ejemplo n.º 7
0
Archivo: main.cpp Proyecto: CCJY/coliru
std::string compress(const std::string & input, int level)
{
    std::vector<uint8_t> vec(input.begin(), input.end());
    auto result = compress(vec, level);
    return std::string(result.begin(), result.end());
}
Ejemplo n.º 8
0
QString
JSResolverHelper::readCompressed( const QString& fileName )
{
    return compress( readRaw( fileName ) );
}
Ejemplo n.º 9
0
 // Calculate the buffer length required to compress an integer.
 static inline size_t compressed_size(uint64_t ui64) {
   //TODO: something faster than this!
   unsigned char buffer[MAX_COMPRESSED_INT_SIZE];
   return compress(buffer, ui64) - buffer;
 }