void build_subsumed(GHashTable *tbl, pred_t *p) { int i; for (i = 0; i < p->hist->pred_n; i++) { if (HAS_FLAG(p->hist->pred[i].flags, CONTEXT) && HAS_FLAG(p->hist->pred[i].flags, HIST_C) && p->hist->pred[i].cond == p->cond) { pair_t *pr = MYmalloc(sizeof(pair_t)); pr->origin = p; pr->produced = p->hist->pred + i; pred_t *pred = g_hash_table_lookup(tbl, p); if (!pred) { g_hash_table_insert(tbl, p, pr); pred = p; } if (!HAS_FLAG(pred->flags, BLACK)) SET_FLAG(pred->flags, RED); } else if (HAS_FLAG(p->hist->pred[i].flags, PRESET)) { pair_t *pr = g_hash_table_lookup(tbl, p->hist->pred + i); if (pr) { CLEAN_FLAG(pr->produced->flags, RED); SET_FLAG(pr->produced->flags, BLACK); } } build_subsumed(tbl, p->hist->pred + i); } }
void dot_history_cleanup(hist_t *h) { if (HAS_FLAG(h->flags, BLACK)) { CLEAN_FLAG(h->flags, BLACK); pred_t *pred = h->pred, *last = h->pred + h->pred_n; while (pred < last) { dot_history_cleanup(pred->hist); ++pred; } } }
void clean_pred(void *v) { CLEAN_FLAG(((pred_t*)v)->flags, RED); CLEAN_FLAG(((pred_t*)v)->flags, BLACK); }
void LZ78::decompress(const std::string* outfile) { unsigned long last_index = 0x00000000; decode_dict = new std::map<unsigned long, std::vector<unsigned char> > (); //fprintf(stderr, "Max map size is %lu\n", this->decode_dict->max_size()); unsigned long maskIndex, eofile; eofile = EOFILE; unsigned char newEntry; debug("Decompressing...", INFO); if (this->filename != NULL) { debug("Opening from file : " + *(this->filename), DEBUG); if (!freopen(this->filename->c_str(), "rb", stdin)) { debug("Error opening file !", CRITICAL); } } if (outfile != NULL) { debug("Opening outfile : " + *outfile, DEBUG); if (!freopen(outfile->c_str(), "wb", stdout)) { debug("Error opening file for output !", CRITICAL); } } bool terminated = false; int byteIndex; unsigned char currByte, lookahead1, lookahead2; long lastPos; std::vector<unsigned char> part; do { maskIndex = 0; for (byteIndex = 0; byteIndex < DICT_NUMBYTE - 1; byteIndex++) { currByte = fgetc(stdin); maskIndex = maskIndex + currByte; maskIndex = maskIndex << 8; } currByte = fgetc(stdin); maskIndex = maskIndex + currByte; //fprintf(stderr, "Maskindex = %lu\n ******************************\n", maskIndex); //cerr << "\t Index is:" << std::bitset < 24 > (maskIndex) << std::endl; if (IS_MATCH(maskIndex)) { debug("MATCH !", DEBUG); } else { debug("MISS !", DEBUG); } if (maskIndex == eofile) { debug("end of file", DEBUG); terminated = true; } else { //fprintf(stderr,"index : %lu\n",CLEAN_FLAG(maskIndex)); newEntry = fgetc(stdin); //fprintf(stderr,"Entry = %x\n",newEntry); lastPos = ftell(stdin); bool eofMark = false; if (DICT_NUMBYTE>1) { lookahead1 = fgetc(stdin); if (newEntry == 0x7F && lookahead1 == 0xFF) eofMark = true; } if (DICT_NUMBYTE>2) { lookahead2 = fgetc(stdin); if (eofMark && lookahead2 == 0xFF) eofMark = true; else eofMark = false; } if (!eofMark) { fseek(stdin, 1-DICT_NUMBYTE, SEEK_CUR); //fprintf(stderr, "last index is %lu \n", last_index); last_index += 0x00000001; if (IS_MATCH(maskIndex)) { debug("Decoding a match in dict", DDEBUG); // match -> 1??????? ........ where ?? is dict index, ... is symbol coding unsigned long index = CLEAN_FLAG(maskIndex); //cerr << "\t Real Index is:" << std::bitset < 24 > (index) << std::endl; part.clear(); part = getEntry(index); if (part.size() ==0) { //fprintf(stderr,"\nsize0!\n"); debug("Entry not found in dictionary, internal error !",CRITICAL); return; // break; } debug("Entry has been found", DEBUG); part.push_back(newEntry); //fprintf(stderr,"Pushing new entry in dictionary : %x\n",newEntry); debug("String appended", DDEBUG); this->decode_dict->insert(std::pair<unsigned long, std::vector<unsigned char> > (last_index, std::vector<unsigned char>(part))); if (this->decode_dict->size() >=MAX_DICSIZE) { this->decode_dict->clear(); last_index = 0x00000000; } for (std::vector<unsigned char>::iterator it = part.begin(); it!=part.end(); ++it) { fputc(*it, stdout); } part.clear(); } else { debug("No match for current DWORD Adding to dictionary", DDEBUG); std::vector<unsigned char> vect; vect.push_back(newEntry); this->decode_dict->insert(std::pair<unsigned long, std::vector<unsigned char> > (last_index, vect)); if (this->decode_dict->size() >=MAX_DICSIZE) { this->decode_dict->clear(); last_index = 0x00000000; } fputc(newEntry, stdout); } } else { debug("Special EOF situation !", DEBUG); //fprintf(stderr,"lookahead1 = %x \tlookahead2 = %x \t newEntry = %x\n",lookahead1,lookahead2,newEntry); if (IS_MATCH(maskIndex)) { debug("Decoding a match in dict", DDEBUG); unsigned long index = CLEAN_FLAG(maskIndex); part.clear(); part = getEntry(index); if (part.size() ==0) { //fprintf(stderr,"\nsize0!\n"); //fprintf(stderr,"Cannot find entry %lu\n",index); debug("Entry not found in dictionary, internal error !",CRITICAL); return; } else { debug("Entry has been found", DEBUG); //fprintf(stderr,"Found entry %lu\n",index); for (std::vector<unsigned char>::iterator it = part.begin(); it!=part.end(); ++it) { //fprintf(stderr,"->0x%x",*it); fputc(*it, stdout); } } } part.clear(); terminated = true; } } } while (maskIndex != eofile && !terminated); if (part.size()!=0) { debug("hm, Part is not empty !",CRITICAL); } debug("Closing stdin FILE descriptor", DDEBUG); fclose(stdin); debug("Closing stdout FILE descriptor", DDEBUG); fclose(stdout); debug("Done !", INFO); }