int do_output(match_ctx ctx, search_nodep snp, encode_match_data emd, encode_match_f * f, struct membuf *outbuf, int *literal_sequences_used) { int pos; int pos_diff; int max_diff; int diff; int copy_used = 0; output_ctxp old; output_ctx out; output_ctx_init(out, outbuf); old = emd->out; emd->out = out; pos = output_get_pos(out); pos_diff = pos; max_diff = 0; LOG(LOG_DUMP, ("pos $%04X\n", out->pos)); output_gamma_code(out, 16); output_bits(out, 1, 0); /* 1 bit out */ diff = output_get_pos(out) - pos_diff; if(diff > max_diff) { max_diff = diff; } LOG(LOG_DUMP, ("pos $%04X\n", out->pos)); LOG(LOG_DUMP, ("------------\n")); while (snp != NULL) { const_matchp mp; mp = snp->match; if (mp != NULL && mp->len > 0) { if (mp->offset == 0) { if(mp->len == 1) { /* literal */ LOG(LOG_DUMP, ("literal byte: $%02X\n", ctx->buf[snp->index])); output_byte(out, ctx->buf[snp->index]); output_bits(out, 1, 1); } else { int i; for(i = 0; i < mp->len; ++i) { output_byte(out, ctx->buf[snp->index + i]); } output_bits(out, 16, mp->len); output_gamma_code(out, 17); output_bits(out, 1, 0); copy_used = 1; } } else { f(mp, emd); output_bits(out, 1, 0); } pos_diff += mp->len; diff = output_get_pos(out) - pos_diff; if(diff > max_diff) { max_diff = diff; } } LOG(LOG_DUMP, ("------------\n")); snp = snp->prev; } LOG(LOG_DUMP, ("pos $%04X\n", out->pos)); /* output header here */ optimal_out(out, emd); LOG(LOG_DUMP, ("pos $%04X\n", out->pos)); output_bits_flush(out); emd->out = old; if(literal_sequences_used != NULL) { *literal_sequences_used = copy_used; } return max_diff; }
static int generate_output(match_ctx ctx, search_nodep snp, struct sfx_decruncher *decr, encode_match_f * f, encode_match_data emd, int load, int len, int start, unsigned char *buf) { int pos; int pos_diff; int max_diff; int diff; static output_ctx out; output_ctxp old; output_ctx_init(out); old = emd->out; emd->out = out; pos = output_get_pos(out); pos_diff = pos; max_diff = 0; output_gamma_code(out, 16); output_bits(out, 1, 0); /* 1 bit out */ diff = output_get_pos(out) - pos_diff; if(diff > max_diff) { max_diff = diff; } while (snp != NULL) { const_matchp mp; mp = snp->match; if (mp != NULL && mp->len > 0) { if (mp->offset == 0) { /* literal */ output_byte(out, ctx->buf[snp->index]); output_bits(out, 1, 1); } else { f(mp, emd); output_bits(out, 1, 0); } pos_diff += mp->len; diff = output_get_pos(out) - pos_diff; if(diff > max_diff) { max_diff = diff; } } snp = snp->prev; } /* output header here */ optimal_out(out, emd); output_bits_flush(out); output_word(out, (unsigned short int) (load + len)); len = output_get_pos(out); decr->load(out, (unsigned short int) load - max_diff); output_copy_bytes(out, 0, len); /* second stage of decruncher */ decr->stages(out, (unsigned short int) start); /*len = output_ctx_close(out, of);*/ len = out->pos - out->start; memcpy(buf, out->buf + out->start, len); emd->out = old; return len; }