37 #include "io_lib_config.h"
49 case E_NULL:
return "NULL";
55 case E_BETA:
return "BETA";
72 static signed int get_bit_MSB(
cram_block *block) {
79 if (--block->
bit == -1) {
94 static int get_one_bits_MSB(
cram_block *block) {
98 if (--block->
bit == -1) {
108 static int get_zero_bits_MSB(
cram_block *block) {
112 if (--block->
bit == -1) {
124 static void store_bit_MSB(
cram_block *block,
unsigned int bit) {
133 if (--block->
bit == -1) {
143 static void store_bytes_MSB(
cram_block *block,
char *bytes,
int len) {
144 if (block->
bit != 7) {
149 while (block->
byte + len >= block->
alloc) {
154 memcpy(&block->
data[block->
byte], bytes, len);
160 static inline unsigned int get_bits_MSB(
cram_block *block,
int nbits) {
161 unsigned int val = 0;
166 if (nbits <= block->bit+1) {
167 val = (block->
data[block->
byte]>>(block->
bit-(nbits-1))) & ((1<<nbits)-1);
168 if ((block->
bit -= nbits) == -1) {
176 val = block->
data[block->
byte] & ((1<<(block->
bit+1))-1);
177 nbits -= block->
bit+1;
183 val = (val << 8) | block->
data[block->
byte++];
188 val |= (block->
data[block->
byte]>>(block->
bit-(nbits-1))) & ((1<<nbits)-1);
196 for (i = 0; i < nbits; i++)
203 if (nbits <= block->bit+1) {
204 val = (block->
data[block->
byte]>>(block->
bit-(nbits-1))) & ((1<<nbits)-1);
205 if ((block->
bit -= nbits) == -1) {
231 for (i = 0; i < nbits; i++)
250 static int store_bits_MSB(
cram_block *block,
unsigned int val,
int nbits) {
276 if (nbits <= block->bit+1) {
277 block->
data[block->
byte] |= (val << (block->
bit+1-nbits));
278 if ((block->
bit-=nbits) == -1) {
286 block->
data[block->
byte] |= (val >> (nbits -= block->
bit+1));
295 if (--block->
bit == -1) {
311 static char *cram_extract_block(
cram_block *b,
int size) {
312 char *cp = (
char *)b->
data + b->
idx;
377 cp = cram_extract_block(b, *out_size);
381 memcpy(out, cp, *out_size);
409 cp = cram_extract_block(b, *out_size);
428 if (!(c = malloc(
sizeof(*c))))
442 if (cp - data != size) {
443 fprintf(stderr,
"Malformed external header stream\n");
469 char tmp[99], *tp = tmp;
473 size_t l = strlen(prefix);
493 c = malloc(
sizeof(*c));
515 for (i = 0, n = *out_size; i < n; i++)
518 for (i = 0, n = *out_size; i < n; i++)
529 for (i = 0, n = *out_size; i < n; i++)
532 for (i = 0, n = *out_size; i < n; i++)
550 if (!(c = malloc(
sizeof(*c))))
565 if (cp - data != size) {
566 fprintf(stderr,
"Malformed beta header stream\n");
575 char *prefix,
int version) {
579 size_t l = strlen(prefix);
595 int *syms = (
int *)in;
598 for (i = 0; i < in_size; i++)
606 unsigned char *syms = (
unsigned char *)in;
609 for (i = 0; i < in_size; i++)
624 int min_val, max_val, len = 0;
626 c = malloc(
sizeof(*c));
638 min_val = ((
int *)dat)[0];
639 max_val = ((
int *)dat)[1];
667 assert(max_val >= min_val);
688 for (count = 0, n = *out_size; count < n; count++) {
694 i = get_one_bits_MSB(in);
709 val += 1 << (i + k-1);
737 if (!(c = malloc(
sizeof(*c))))
747 if (cp - data != size) {
748 fprintf(stderr,
"Malformed subexp header stream\n");
764 for (i = 0, n = *out_size; i < n; i++) {
768 nz = get_zero_bits_MSB(in);
793 if (!(c = malloc(
sizeof(*c))))
802 if (cp - data != size) {
803 fprintf(stderr,
"Malformed gamma header stream\n");
816 static int code_sort(
const void *vp1,
const void *vp2) {
840 for (i = 0, n = *out_size; i < n; i++) {
851 for (i = 0, n = *out_size; i < n; i++) {
853 int val = 0, len = 0, last_len = 0;
856 int dlen = codes[idx].
len - last_len;
857 if (dlen <= 0 || (in->
alloc - in->
byte)*8 + in->
bit + 7 < dlen)
864 last_len = (len += dlen);
867 idx = val - codes[idx].
p;
868 if (idx >= ncodes || idx < 0)
871 if (codes[idx].
code == val && codes[idx].len == len) {
872 out[i] = codes[idx].
symbol;
888 for (i = 0, n = *out_size; i < n; i++) {
889 out_i[i] = codes[0].
symbol;
900 for (i = 0, n = *out_size; i < n; i++) {
902 int val = 0, len = 0, last_len = 0;
906 int dlen = codes[idx].
len - last_len;
907 if (dlen <= 0 || (in->
alloc - in->
byte)*8 + in->
bit + 7 < dlen)
914 last_len = (len += dlen);
917 idx = val - codes[idx].
p;
918 if (idx >= ncodes || idx < 0)
921 if (codes[idx].
code == val && codes[idx].len == len) {
922 out_i[i] = codes[idx].
symbol;
938 char *cp = data, *data_end = &data[size];
941 int32_t val, last_len, max_len = 0;
944 h = calloc(1,
sizeof(*h));
951 codes = h->
huffman.
codes = malloc(ncodes *
sizeof(*codes));
958 for (i = 0; i < ncodes && cp < data_end; i++) {
959 cp +=
itf8_get(cp, &codes[i].symbol);
962 if (cp >= data_end) {
963 fprintf(stderr,
"Malformed huffman header stream\n");
969 fprintf(stderr,
"Malformed huffman header stream\n");
979 for (i = 0; i < ncodes && cp < data_end; i++) {
981 if (max_len < codes[i].len)
982 max_len = codes[i].
len;
984 if (cp - data != size || max_len >= ncodes) {
985 fprintf(stderr,
"Malformed huffman header stream\n");
991 qsort(codes, ncodes,
sizeof(*codes), code_sort);
994 val = -1, last_len = 0;
995 for (i = 0; i < ncodes; i++) {
997 if (codes[i].len > last_len) {
998 while (codes[i].len > last_len) {
1003 codes[i].
code = val;
1012 for (i = j = 0; i < ncodes; i++) {
1013 if (codes[i].len > last_len) {
1014 j = codes[i].
code - i;
1015 last_len = codes[i].
len;
1060 int i,
code, len, r = 0;
1061 unsigned char *syms = (
unsigned char *)in;
1083 r |= store_bits_MSB(out, code, len);
1084 }
while (--in_size);
1096 int i,
code, len, r = 0;
1097 int *syms = (
int *)in;
1120 r |= store_bits_MSB(out, code, len);
1121 }
while (--in_size);
1159 size_t l = strlen(prefix);
1166 tp +=
itf8_put(tp, codes[i].symbol);
1188 int *vals = NULL, *freqs = NULL, vals_alloc = 0, *lens,
code, len;
1189 int nvals, i, ntot = 0, max_val = 0, min_val = INT_MAX, k;
1193 c = malloc(
sizeof(*c));
1202 if (nvals >= vals_alloc) {
1203 vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
1204 vals = realloc(vals, vals_alloc *
sizeof(
int));
1205 freqs = realloc(freqs, vals_alloc *
sizeof(
int));
1206 if (!vals || !freqs) {
1207 if (vals) free(vals);
1208 if (freqs) free(freqs);
1214 freqs[nvals] = st->
freqs[i];
1215 assert(st->
freqs[i] > 0);
1216 ntot += freqs[nvals];
1217 if (max_val < i) max_val = i;
1218 if (min_val > i) min_val = i;
1227 if (nvals >= vals_alloc) {
1228 vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
1229 vals = realloc(vals, vals_alloc *
sizeof(
int));
1230 freqs = realloc(freqs, vals_alloc *
sizeof(
int));
1231 if (!vals || !freqs)
1234 vals[nvals]=
kh_key(st->h, k);
1235 freqs[nvals] =
kh_val(st->h, k);
1236 assert(freqs[nvals] > 0);
1237 ntot += freqs[nvals];
1238 if (max_val < i) max_val = i;
1239 if (min_val > i) min_val = i;
1246 freqs = realloc(freqs, 2*nvals*
sizeof(*freqs));
1247 lens = calloc(2*nvals,
sizeof(*lens));
1248 if (!lens || !freqs)
1255 int low1 = INT_MAX, low2 = INT_MAX;
1256 int ind1 = 0, ind2 = 0;
1257 for (i = 0; i < nvals; i++) {
1260 if (low1 > freqs[i])
1261 low2 = low1, ind2 = ind1, low1 = freqs[i], ind1 = i;
1262 else if (low2 > freqs[i])
1263 low2 = freqs[i], ind2 = i;
1265 if (low2 == INT_MAX)
1268 freqs[nvals] = low1 + low2;
1278 for (i = 0; i < nvals; i++) {
1280 for (k = lens[i]; k; k = lens[k])
1289 if (!(codes = malloc(nvals *
sizeof(*codes))))
1291 for (i = 0; i < nvals; i++) {
1292 codes[i].
symbol = vals[i];
1293 codes[i].
len = lens[i];
1295 qsort(codes, nvals,
sizeof(*codes), code_sort);
1317 code = 0; len = codes[0].
len;
1318 for (i = 0; i < nvals; i++) {
1319 while (len != codes[i].len) {
1323 codes[i].
code = code++;
1325 if (codes[i].symbol >= -1 && codes[i].symbol <
MAX_HUFF)
1402 if (!(c = malloc(
sizeof(*c))))
1421 if (cp - data != size) {
1422 fprintf(stderr,
"Malformed byte_array_len header stream\n");
1442 char *prefix,
int version) {
1446 size_t l = strlen(prefix);
1470 c = malloc(
sizeof(*c));
1502 b = slice->
block[i];
1515 cp = (
char *)b->
data + b->
idx;
1523 *out_size = cp - (
char *)(b->
data + b->
idx);
1524 b->
idx = cp - (
char *)b->
data + 1;
1535 char *cp, ch, *out_cp, *cp_end, *out_end;
1544 b = slice->
block[i];
1556 cp = (
char *)b->
data + b->
idx;
1560 out_end = out_cp + space;
1563 while ((ch = *cp) != stop) {
1568 if (out_cp == out_end) {
1573 out_end = out_cp + space;
1578 *out_size = cp - (
char *)(b->
data + b->
idx);
1579 b->
idx = cp - (
char *)b->
data + 1;
1594 unsigned char *cp = (
unsigned char *)data;
1596 if (!(c = malloc(
sizeof(*c))))
1614 if ((
char *)cp - data != size) {
1615 fprintf(stderr,
"Malformed byte_array_stop header stream\n");
1635 char *prefix,
int version) {
1637 char buf[20], *cp = buf;
1640 size_t l = strlen(prefix);
1672 c = malloc(
sizeof(*c));
1692 case E_NULL:
return "NULL";
1698 case E_BETA:
return "BETA";
1706 static cram_codec *(*decode_init[])(
char *data,
1723 char *data,
int size,
1726 if (decode_init[codec]) {
1727 return decode_init[codec](data, size, option, version);
1729 fprintf(stderr,
"Unimplemented codec of type %s\n", codec2str(codec));
1755 if (st && !st->
nvals)
1758 if (encode_init[codec]) {
1759 return encode_init[codec](st, option, dat, version);
1761 fprintf(stderr,
"Unimplemented codec of type %s\n", codec2str(codec));