unsigned char *UnPCKData ( unsigned char *data, size_t datasize, size_t *len ) { unsigned char magic = data[0] ^ 0xC8; for ( size_t i = 0; i < datasize; i++ ) data[i] ^= magic; ++data; --datasize; // create data buffer size_t *uncomprLenSize = (size_t*)(data + (datasize - 4)); unsigned long uncomprLen = *uncomprLenSize; unsigned char *uncompr = new unsigned char[uncomprLen]; memset ( uncompr, 0, sizeof(uncompr) ); // find header size unsigned char *buf = data + PCKHEADERSIZE; char flag = data[3]; if ( flag & GZ_FLAG_EXTRA ) { size_t xlen = *((short int*)(buf)); buf += xlen; } if ( flag & GZ_FLAG_FILENAME ) { char *origname = (char*)(buf); buf += strlen (origname) + 1; } if ( flag & GZ_FLAG_COMMENT ) { char *comment = (char*)(buf); buf += strlen(comment) + 1; } if ( flag & GZ_FLAG_HCRC ) buf += 2; long bufSize = datasize - (buf-data) - 8; int err = uncompress2 ( uncompr, &uncomprLen, buf, bufSize ); if ( err != Z_OK ) { delete uncompr; uncompr = NULL; uncomprLen = 0; } *len = uncomprLen; return uncompr; }
static void load_word_hash_from_buffer(WORD_HASH_TABLE *table_ptr, char *buffer) { int len; int num_words; int i; char *word = NULL; unsigned char *s = (unsigned char *)buffer; num_words = uncompress2(&s); for (i=0; i < num_words ; i++) { len = uncompress2(&s); word = emalloc(len+1); memcpy(word,s,len); s += len; word[len] = '\0'; add_word_to_hash_table( table_ptr, word , HASHSIZE); efree(word); } }
void parse_MetaNames_from_buffer(INDEXDATAHEADER *header, char *buffer) { int len; int num_metanames; int metaType, i, alias, sort_len, bias, metaID; char *word; unsigned char *s = (unsigned char *)buffer; struct metaEntry *m; /* First clear out the default metanames */ freeMetaEntries( header ); num_metanames = uncompress2(&s); for (i = 0; i < num_metanames; i++) { len = uncompress2(&s); word = emalloc(len +1); memcpy(word,s,len); s += len; word[len] = '\0'; /* Read metaID */ metaID = uncompress2(&s); /* metaType was saved as metaType+1 */ metaType = uncompress2(&s); alias = uncompress2(&s) - 1; sort_len = uncompress2(&s); bias = uncompress2(&s) - RANK_BIAS_RANGE - 1; /* add the meta tag */ if ( !(m = addNewMetaEntry(header, word, metaType, metaID))) progerr("failed to add new meta entry '%s:%d'", word, metaID ); m->alias = alias; m->rank_bias = bias; m->sort_len = sort_len; efree(word); } }
static int cdread_compressed(FILE *f, unsigned int base, void *dest, int sector) { unsigned long cdbuffer_size, cdbuffer_size_expect; unsigned int start_byte, size; int is_compressed; int ret, block; if (base) sector += base / 2352; block = sector >> compr_img->block_shift; compr_img->sector_in_blk = sector & ((1 << compr_img->block_shift) - 1); if (block == compr_img->current_block) { //printf("hit sect %d\n", sector); goto finish; } if (sector >= compr_img->index_len * 16) { SysPrintf("sector %d is past img end\n", sector); return -1; } start_byte = compr_img->index_table[block] & 0x7fffffff; if (fseek(cdHandle, start_byte, SEEK_SET) != 0) { SysPrintf("seek error for block %d at %x: ", block, start_byte); perror(NULL); return -1; } is_compressed = !(compr_img->index_table[block] & 0x80000000); size = (compr_img->index_table[block + 1] & 0x7fffffff) - start_byte; if (size > sizeof(compr_img->buff_compressed)) { SysPrintf("block %d is too large: %u\n", block, size); return -1; } if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0], 1, size, cdHandle) != size) { SysPrintf("read error for block %d at %x: ", block, start_byte); perror(NULL); return -1; } if (is_compressed) { cdbuffer_size_expect = sizeof(compr_img->buff_raw[0]) << compr_img->block_shift; cdbuffer_size = cdbuffer_size_expect; ret = uncompress2(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size); if (ret != 0) { SysPrintf("uncompress failed with %d for block %d, sector %d\n", ret, block, sector); return -1; } if (cdbuffer_size != cdbuffer_size_expect) SysPrintf("cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size, cdbuffer_size_expect, sector); } // done at last! compr_img->current_block = block; finish: if (dest != cdbuffer) // copy avoid HACK memcpy(dest, compr_img->buff_raw[compr_img->sector_in_blk], CD_FRAMESIZE_RAW); return CD_FRAMESIZE_RAW; }
// read track // time: byte 0 - minute; byte 1 - second; byte 2 - frame // uses bcd format static long CDRreadTrack(unsigned char *time) { unsigned int start_byte, size; unsigned long cdbuffer_size; int ret, sector, block; if (cd_file == NULL) return -1; sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])); // avoid division if possible switch (cd_sectors_per_blk) { case 1: block = sector; current_sect_in_blk = 0; break; case 10: block = sector / 10; current_sect_in_blk = sector % 10; break; case 16: block = sector >> 4; current_sect_in_blk = sector & 15; break; default: err("unhandled cd_sectors_per_blk: %d\n", cd_sectors_per_blk); return -1; } if (block == current_block) { // it's already there, nothing to do //printf("hit sect %d\n", sector); return 0; } if (sector >= cd_index_len * cd_sectors_per_blk) { err("sector %d is past track end\n", sector); return -1; } start_byte = cd_index_table[block]; if (fseek(cd_file, start_byte, SEEK_SET) != 0) { err("seek error for block %d at %x: ", block, start_byte); perror(NULL); return -1; } size = cd_index_table[block + 1] - start_byte; if (size > sizeof(cdbuffer->compressed)) { err("block %d is too large: %u\n", block, size); return -1; } if (fread(cdbuffer->compressed, 1, size, cd_file) != size) { err("read error for block %d at %x: ", block, start_byte); perror(NULL); return -1; } cdbuffer_size = sizeof(cdbuffer->raw[0]) * cd_sectors_per_blk; switch (cd_compression) { case CDRC_ZLIB: ret = uncompress(cdbuffer->raw[0], &cdbuffer_size, cdbuffer->compressed, size); break; case CDRC_ZLIB2: ret = uncompress2(cdbuffer->raw[0], &cdbuffer_size, cdbuffer->compressed, size); break; case CDRC_BZ: ret = BZ2_bzBuffToBuffDecompress((char *)cdbuffer->raw, (unsigned int *)&cdbuffer_size, (char *)cdbuffer->compressed, size, 0, 0); break; default: err("bad cd_compression: %d\n", cd_compression); return -1; } if (ret != 0) { err("uncompress failed with %d for block %d, sector %d\n", ret, block, sector); return -1; } if (cdbuffer_size != sizeof(cdbuffer->raw[0]) * cd_sectors_per_blk) err("cdbuffer_size: %lu != %d, sector %d\n", cdbuffer_size, sizeof(cdbuffer->raw[0]) * cd_sectors_per_blk, sector); // done at last! current_block = block; return 0; }