bool InsnSemanticsExpr::LeafNode::equivalent_to(const TreeNodePtr &other_) const { bool retval = false; LeafNodePtr other = other_->isLeafNode(); if (this==other.get()) { retval = true; } else if (other && get_nbits()==other->get_nbits()) { if (is_known()) { retval = other->is_known() && ival==other->ival; } else { retval = !other->is_known() && name==other->name; } } return retval; }
/** * * Decode a single block that contains the DCT coefficients. * The table coefficients is already dezigzaged at the end of the operation. * */ static int process_Huffman_data_unit(struct huffman_context *hc, struct jdec_task *hdata, int component, short int *DCT_out) { unsigned char j; unsigned int huff_code; int retcode; unsigned char size_val, count_0; struct component *c = &hc->component_infos[component]; short int DCT[64]; /* Initialize the DCT coef table */ memset(DCT, 0, sizeof(DCT)); /* DC coefficient decoding */ retcode = get_next_huffman_code(hdata, c->DC_table); // End of stream if(retcode == -1) return -1; else huff_code = (unsigned int)retcode; if (huff_code) { get_nbits(hdata->reservoir, hdata->nbits_in_reservoir, hdata->stream, huff_code, DCT[0]); DCT[0] += c->previous_DC; c->previous_DC = DCT[0]; } else { DCT[0] = c->previous_DC; } /* AC coefficient decoding */ j = 1; while (j<64) { huff_code = get_next_huffman_code(hdata, c->AC_table); //trace("- %x\n", huff_code); size_val = huff_code & 0xF; count_0 = huff_code >> 4; if (size_val == 0) { /* RLE */ if (count_0 == 0) break; /* EOB found, go out */ else if (count_0 == 0xF) j += 16; /* skip 16 zeros */ } else { j += count_0; /* skip count_0 zeroes */ if (j >= 64) { snprintf(error_string, sizeof(error_string), "Bad huffman data (buffer overflow)"); break; } get_nbits(hdata->reservoir, hdata->nbits_in_reservoir, hdata->stream, size_val, DCT[j]); j++; } } for (j = 0; j < 64; j++) DCT_out[j] = DCT[zigzag[j]]; return 0; }
// Compress STDIN into stream of codes // First byte sent in the form [MAXBITS (6 bits)][E_FLAG][P_FLAG] // // The only special code sent is ESCAPE for -e; // everything else is derived in decode. // // Pruning performed as soon as the table is full int encode(int MAXBITS, int E_FLAG, int P_FLAG) { // Send option args encoded as: // MAXBITS: 6 bits (since max value is 20) // E_FLAG: 1 bit // P_FLAG: 1 bit putBits(6, MAXBITS); putBits(1, E_FLAG); putBits(1, P_FLAG); int next_code = 0; // == number of codes assigned == # elts in ARRAY int nBits = 1; // #bits required to send NEXT code if (E_FLAG) next_code = 2; // already assigned 0 to QUIT // 1 to ESCAPE // ============== INITIALIZE TRIE ================ Trie t = createT(); if (!E_FLAG) { // initialize all one-char strings for (int K = 0; K < 256; K++) insertT(t, K, next_code++, 0); nBits = 8; } // ================ ENCODE INPUT ================= Trie C = t; // last node visited int K; while ((K = getchar()) != EOF) { Trie child = getT(C, K); if (child != NULL) { // increment NAP and go down trie sawT(child); C = child; } else { // ============ PUTBITS ========================== if (C == t) { // new 1-char string if (!E_FLAG) DIE_FORMAT("E_FLAG false, yet (EMPTY, K=%d) not in table\n", K); putBits(nBits, ESCAPE); putBits(CHAR_BIT, K); } else { // Output code C putBits(nBits, getCodeT(C)); } // =========== INSERT ============================== // insert new code if table not full if (next_code < (1 << MAXBITS)) { insertT(C, K, next_code++, 1); } // =========== UPDATE NBITS ======================= // Prune as soon as last slot taken if (next_code == (1 << MAXBITS)) { if (P_FLAG) { next_code = prune(&t, E_FLAG); nBits = get_nbits(next_code); } else ; } // Increase NBITS only when #codes assigned // exceeds it else if (next_code > (1 << nBits)) nBits++; // ============ RESET C ===== if (C == t) // new single-char, so skip continue; else { C = getT(t, K); if (C == NULL) { // (EMPTY, K) not in table if (!E_FLAG) DIE_FORMAT("E_FLAG false, yet (EMPTY, K=%d) not in table\n", K); ungetc(K, stdin); // single-char on next insert C = t; } else sawT(C); // increment NAP } } } // Put leftover known prefix if (C != t) { putBits(nBits, getCodeT(C)); } flushBits(); destroyT(t); return 0; }
int decode() { // Decode first byte as options int MAXBITS = getBits(6); int E_FLAG = getBits(1); int P_FLAG = getBits(1); if (MAXBITS <= CHAR_BIT || MAXBITS > 20 || E_FLAG == EOF || P_FLAG == EOF) DIE("decode: bit stream not encoded by encode"); int next_code = 0; // == number of codes assigned == # elts in ARRAY int nBits = 1; // #bits required to send NEXT code if (E_FLAG) next_code = 2; // already assigned 0 to QUIT // 1 to ESCAPE // =============== INITIALIZE TRIE ===================== Trie t = createT(); if (!E_FLAG) { // initialize all one-char strings for (int K = 0; K < 256; K++) insertT(t, K, next_code++, 0); nBits = 8; } // =============== DECODE BIT STREAM ==================== int C; int last_insert = EMPTY; // code assigned to last inserted node while ((C = getBits(nBits)) != EOF) { // -e: Break on C = QUIT (flushBits() junk) if (E_FLAG && C == QUIT) break; // ========== PRINT STRING WITH NEW CODE ======= int finalK; // first char in C string // -e: check for ESCAPE if (E_FLAG && C == ESCAPE) { finalK = getBits(CHAR_BIT); if (finalK == EOF) DIE("decode: bit stream not encoded by encode"); putchar(finalK); } else { int KwK = 0; finalK = putstring(C, &KwK); // DIEs if C not in table // If C was just inserted w/ STANDBY (KwK), // print oldC==Kw then K if (KwK) putchar(finalK); } // =========== PATCH LAST-INSERTED STRING ========= // K now known for word inserted with prefix OLDC if (last_insert != EMPTY) updateK(last_insert, finalK); // =========== INSERT NEW CODE ==================== // insert new code if table not full if (next_code < (1 << MAXBITS)) { if (E_FLAG && (C == ESCAPE)) { insertT(t, finalK, next_code++, 1); last_insert = EMPTY; } else { // Insert node with C as prefix and K=STANDBY insertT( C_to_T(C), STANDBY, next_code, 1); last_insert = next_code++; } } else last_insert = EMPTY; // no insert to update next time // =========== UPDATE NBITS ======================= // Prune as soon as last slot taken if (next_code == (1 << MAXBITS)) { if (P_FLAG) { next_code = prune(&t, E_FLAG); nBits = get_nbits(next_code); // no need to update K in insertion // since it'll be pruned last_insert = EMPTY; } else ; } // Increase NBITS only when #codes assigned // exceeds it else if (next_code > (1 << nBits)) nBits++; } destroyT(t); return 0; }