static void push_segment(stream_t* stream, int enc, size_t n, const char* str) { if (!n) return; push_bits(stream, 4, enc); // mode if (enc) push_bits(stream, lenbits[enc][stream->vr], n); // length if (enc == 1) { for (; n>=3; n-=3, str+=3) { unsigned int c = (D(0)*10 + D(1))*10 + D(2); push_bits(stream, 10, c); } if (n == 2) { unsigned int c = D(0)*10 + D(1); push_bits(stream, 7, c); } else if (n == 1) { unsigned int c = D(0); push_bits(stream, 4, c); } } else if (enc == 2) { for (; n>=2; n-=2, str+=2) { unsigned int c = A(0)*45 + A(1); push_bits(stream, 11, c); } if (n == 1) { unsigned int c = A(0); push_bits(stream, 6, c); } } else if (enc == 4) { for (; n; n--, str++) push_bits(stream, 8, *str); } else { exit(1); } }
static void encode_in_range(stream_t* stream, const char* data) { stream->n = -1; stream->b = 0; size_t n_numer = 0; size_t n_alpha = 0; size_t n_byte = 0; for (const char* c = data; *c; c++) { if ('0' <= *c && *c <= '9') { n_numer++; n_alpha++; } else { if (n_numer >= 6) { if (n_alpha - n_numer >= 11) { PUSH(4, n_byte-n_alpha) PUSH(2, n_alpha-n_numer) } else { PUSH(4, n_byte-n_alpha) } PUSH(1, n_numer) n_alpha = 0; n_byte = 0; } if (strchr(charset_alpha+10, *c)) // skip digits { n_alpha++; } else { if (n_alpha >= 11) { PUSH(4, n_byte-n_alpha) PUSH(2, n_alpha) n_byte = 0; } n_alpha = 0; } n_numer = 0; } n_byte++; } // handle remaining data if (n_byte) { // there is something left if (n_alpha == n_byte) { // it's all alphanumeric if (n_numer == n_alpha) { // it's all numeric PUSH(1, n_numer); } else { // actually alphanumeric PUSH(2, n_alpha); } } else { // 8-bit data PUSH(4, n_byte); } } push_bits(stream, 4, 0); push_bits(stream, 0, stream->b); stream->n++; }
static void add_tag_bit(heatshrink_encoder *hse, output_info *oi, uint8_t tag) { LOG("-- adding tag bit: %d\n", tag); push_bits(hse, 1, tag, oi); }