static void write40_skip(byte*& w, int count) { while (count) { if (count < 0x80) { write40_c5(w, count); count = 0; } else { int c_write = count < 0x8000 ? count : 0x7fff; write40_c2(w, c_write); count -= c_write; } } }
int encode40(const byte* last_s, const byte* x, byte* d, int cb_s) { // full compression byte* s = new byte[cb_s]; { byte* a = s; int size = cb_s; while (size--) *a++ = *last_s++ ^ *x++; } const byte* s_end = s + cb_s; const byte* r = s; byte* w = d; const byte* copy_from = NULL; while (r < s_end) { int v = *r; int t = get_run_length(r, s_end); if (!v) { flush_copy(w, r, copy_from); write40_skip(w, t); } else if (t > 2) { flush_copy(w, r, copy_from); write40_fill(w, t, v); } else { if (!copy_from) copy_from = r; } r += t; } flush_copy(w, r, copy_from); write40_c2(w, 0); delete[] s; return w - d; }