-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode.c
More file actions
1257 lines (1103 loc) · 31.5 KB
/
encode.c
File metadata and controls
1257 lines (1103 loc) · 31.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Golomb encoding and decoding. The implementation is based on
* pseudocode taken from 'Compression and Coding Algorithms' by Alistair
* Moffat & Andrew Turping, Kluwer Academic Publishers, 2002.
* Copyright Anirudh Ramachandran <anirudhvr@gmail.com>, 2008
* Released under GPLv2
*/
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
#include <math.h>
#include "encode.h"
#define CHUNK 8192
#define CHUNK_2 16384
#define BUF_REALLOC_PENALTY 3
#define MAX_INPUT_SIZE 131072 /* 128 KB */
/*
* XXX
* these two are static buffers used to write encoded output, both
* during run-length encoding/decoding and golomb encoding/decoding.
* We need them to be greater than the max. input size we expect as some
* run-length encodings will actually be longer than the input. 10 times
* the max.input length seems reasonable (about 4 MB each, i think..)
*
*/
unsigned int rle_buf[10*MAX_INPUT_SIZE];
unsigned int golomb_buf[10*MAX_INPUT_SIZE];
/*
*
* Encoding functions: run-length encoding. Run-length encoding returns
* the number of trials required before a 1 is found. For example, the
* RLE according to the following impl. of this sequence of bits:
*
* 001 1 00001 1 001 0
* is
* 3 1 5 1 3 1 0 <--- the last '0' is used as a marker indicating that a
* string of 1 '0' terminated the input as opposed to a single '1'.
*
*
*/
/*
* The main RLE function. Takes as input an unsigned char buffer, and
* outputs the RLE as an unsigned int buf. This function automatically
* adds a last unsigned char of all 1s (ie., value 255) so that we don't
* come to the problem of having a hanging ends-in-zero marker, if the
* last char of the input ends in a 0. This char is automatically
* removed after you do run-length decode
*/
int
get_run_length_encoding (unsigned char *in,
unsigned long size,
unsigned int **out,
unsigned long *outsize)
{
unsigned int *rle;
int i, j;
int rle_index = 0;
int need_to_splice = 0;
unsigned char allones = 255;
rle = rle_buf;
for (i = 0; i < size; ++i) {
//printf ("got char %d\n", in[i]);
for (j = 0; j < rle_lookup_sizes[in[i]]; ++j) {
if (!j && need_to_splice) {
//printf ("splicing..");
if (rle_lookup[in[i]][0] > 1) {
/* this means that the first bit is a run of one or more
* zeros. so we can successfully splice with the previous set
* */
//assert (rle_index > 7);
//printf ("increased %d with %d\n",
//rle[rle_index - 2], rle_lookup[in[i]][0]);
rle[rle_index - 2] += rle_lookup[in[i]][0];
rle_index--;
} else {
/* darn, the first entry is a 1 (ie, the first bit is a 1), so
* we cant splice, so move increase the value of the last
* non-zero int in the rle, and move index back one so as to
* overwrite the last 0 */
rle[rle_index - 2]++;
--rle_index;
}
need_to_splice = 0;
} else {
rle[rle_index++] = rle_lookup[in[i]][j];
//printf ("writing %d\n", rle_lookup[in[i]][j]);
}
}
if (rle[rle_index - 1] == 0)
need_to_splice = 1;
}
//printf ("got char %d\n", allones);
for (j = 0; j < rle_lookup_sizes[allones]; ++j) {
if (!j && need_to_splice) {
//printf ("splicing..");
if (rle_lookup[allones][0] > 1) {
/* this means that the first bit is a run of one or more
* zeros. so we can successfully splice with the previous set
* */
//assert (rle_index > 7);
//printf ("increased %d with %d\n",
//rle[rle_index - 2], rle_lookup[allones][0]);
rle[rle_index - 2] += rle_lookup[allones][0];
rle_index--;
} else {
/* darn, the first entry is a 1 (ie, the first bit is a 1), so
* we cant splice, so move index back one so as to overwrite
* the last 0 */
rle[rle_index - 2]++;
--rle_index;
}
need_to_splice = 0;
} else {
rle[rle_index++] = rle_lookup[allones][j];
//printf ("writing %d\n", rle_lookup[allones][j]);
}
}
if ( !(*out = malloc (sizeof (unsigned int) * (rle_index))) ) {
perror ("cant malloc: ");
return 1;
}
if ( !(memcpy (*out, rle, sizeof (unsigned int) * (rle_index))) ) {
perror ("cant memcpy: ");
free (*out);
return 1;
}
*outsize = rle_index;
return 0;
}
/*
* Decode the unsigned integer buffer obtained above. Output is returned
* as an unsigned char buffer
*/
int
get_run_length_decoding (unsigned int *in,
unsigned long size,
unsigned char **out,
unsigned long *outsize)
{
int i;
int currindex, bytecounter;
unsigned char *currbyte;
currindex = 0;
bytecounter = 1;
memset (rle_buf, 0, sizeof(unsigned int) * MAX_INPUT_SIZE);
currbyte = (unsigned char*) rle_buf;
//printf ("last at %d: %d\n", size, in[size-1]);
for (i = 0; i < size; ++i) {
currindex += in[i];
while (currindex > 8) {
currbyte++;
currindex -= 8;
bytecounter++;
}
*currbyte |= 1 << (8 - currindex);
//printf ("setting bit %d of byte %d\n",
//currindex, bytecounter);
}
*outsize = bytecounter - 1;
if ( !(*out= (unsigned char *) malloc (*outsize)) ) {
perror ("RLD: cannot malloc: ");
return 1;
}
if ( !(memcpy (*out, rle_buf, *outsize)) ) {
perror ("RLD: cannot memcpy: ");
free (*out);
return 1;
}
return 0;
}
/*
* Golomb encoding and decoding. The implementation is based on
* pseudocode taken from 'Compression and Coding Algorithms' by Alistair
* Moffat & Andrew Turping, Kluwer Academic Publishers, 2002.
*/
/*
* Macros for certain functions used in the referecne above
*/
#define PUT_ONE_INTEGER(x, nbits) do { \
int i; \
/* printf ("x, nbits: %d %d\n", (x), (nbits)); */ \
for (i = (nbits) - 1; i > -1; --i) { \
/* printf ("byte %d, bit %d, putting ", (bytecounter), (currindex)); */ \
if ( ((x) >> i) & 1 ) { \
*(currbyte) |= 1 << (7 - currindex); \
/* printf ("..1"); */ \
} else { \
/* printf ("..0"); */ \
} \
/* printf ("\n"); */ \
currindex++; \
if (currindex >= 8) { \
(currbyte)++; \
bytecounter++; \
currindex -= 8; \
} \
} \
} while (0);
#define GET_ONE_INTEGER(nbits) do { \
int i; \
x = 0; \
/* printf ("nbits: %d\n", (nbits)); */ \
for (i = (nbits) - 1; i > -1; --i) { \
x = (x << 1) | \
((*(currbyte) & (1 << (7 - currindex))) > 0); \
/* printf ("i: %d, currbyte: %d, currindex: %d, x: %d\n", \
i, bytecounter, currindex, x);*/ \
currindex++; \
if (currindex >= 8) { \
(currbyte)++; \
bytecounter++; \
currindex -= 8; \
} \
} \
} while (0);
/*
* To make printing a number in binary easier
*/
char *h2b[] = {
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"
};
/*
* This function finds ceil (log (base 2, n))
*/
inline int
ceil_log2 (int b) {
int last = -1, secondlast = -1, count = 0;
int log2_b, tmp = b;
while (tmp) {
if (tmp & 1) {
secondlast = last;
last = count;
}
tmp >>= 1;
++count;
}
if (secondlast == -1)
log2_b = last;
else
log2_b = last+1;
return log2_b;
}
/* an array to help with the num_set_bits function that follows.
* Records the number of set bits for each value of an unsigned char */
const unsigned char set_bits_lookup_table[256] =
{
0, 1, 1, 2, 1, 2, 2, 3, /* 0 - 7 */
1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4,
2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4,
2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4,
2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, /* 120 - 127 */
4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4,
2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6,
4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5,
3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6,
4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6,
4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7,
5, 6, 6, 7, 6, 7, 7, 8 /* 248 - 255 */
};
/*
This function uses a lookup table to quickly find the number of bits
set in a char array of size 'size'
*/
static unsigned long
num_set_bits (unsigned char *input, unsigned long size)
{
unsigned long count = 0, i = 0;
while (i < size)
count += set_bits_lookup_table[input[i++]];
return count;
}
/*
*
* Encoding function: golomb encoding
*
* Golomb encoding is done on a sequence of positive integers.
* Typically, if you want to encode an arbitrary char buffer, you should
* first run-length encode it (which will give you an
* all-positive-integer buffer, which you can provide to golomb
* encoding.
*
* The input is the positive integer buffer, and the output is the
* encoded buffer (say a char *), and a parameter for the encoding (see
* the reference above for details), which is typically calculated as a
* function of the number of set bits in the (original) input. The more
* sparse the input, the better the compression (duh!).
*/
int
golomb_encode (void *input,
unsigned long input_len,
void **out,
unsigned long *outsize,
unsigned int *golomb_param)
{
unsigned long setbits, currindex, size, rle_size;
int b, i;
int q, r;
int d, bytecounter;
unsigned int *rle;
unsigned int log2_b;
unsigned char *currbyte, *in;
float prob;
in = (unsigned char*) input;
size = input_len;
if (!in) return -1;
setbits = size * 8 - num_set_bits (in, size); /* from bf.h */
prob = (float)setbits / (float)(size * 8);
//printf ("set bits in input: %lu\n", setbits);
//b = (int) ceilf (LN2 * (float) (size * 8.0) / (float) setbits);
b = (int) ceilf (-(LN2 / (float) (logf (prob))));
//b = 3;
/* b+=2; */
//printf ("p = %f, b = %d\n",(float) setbits / (float) (size * 8.0), b);
if (get_run_length_encoding (in, size, &rle, &rle_size) ) {
fprintf (stderr, "golomb encode: error with RLE\n");
return 1;
}
memset (golomb_buf, 0, sizeof(unsigned int) * MAX_INPUT_SIZE);
currbyte = (unsigned char*) golomb_buf;
currindex = 0; bytecounter = 0;
/* this is for the ceil (log_{2} (b) )
* needed in minimal binary decode
* need to compute only once
*/
log2_b = ceil_log2 (b);
//printf ("log2_b = %d\n", log2_b);
/* main loop reading RLE input */
for (i = 0; i < rle_size; ++i) {
q = (rle[i] - 1) / b;
r = rle[i] - q * b;
//printf ("q: %d, r: %d\n", q, r);
/* unary encode (q + 1) */
++q;
while (--q) {
*currbyte |= 1 << (7 - currindex);
++currindex;
if (currindex >= 8) {
currbyte++;
bytecounter++;
currindex -= 8;
}
}
//printf ("after encoding quotient byte %d = %s %s\n",
//bytecounter, h2b[*currbyte >> 4], h2b[*currbyte & 0x0f]);
/* equivalent to putting the last 0 */
currindex++;
if (currindex >= 8) {
currbyte++;
bytecounter++;
currindex -= 8;
}
/* minimal binary encode (r, b) */
d = (1 << log2_b) - b;
//printf ("d = %d\n", d);
if (r > d) {
/* put_one_integer (r - 1 + d, log2_b) */
PUT_ONE_INTEGER (r - 1 + d, log2_b);
} else {
PUT_ONE_INTEGER (r - 1, log2_b - 1);
}
//printf ("after encoding remainder byte %d (%d)= %s %s\n",
//bytecounter, currindex, h2b[*currbyte >> 4], h2b[*currbyte & 0x0f]);
}
*outsize = bytecounter;
if ( !(*out = malloc (*outsize) ) ) {
perror ("golombencode: cannot malloc output buf: ");
return 1;
}
if ( !(memcpy (*out, golomb_buf, *outsize) ) ) {
perror ("golombencode: cannot memcpy to output: ");
free (*out);
return 1;
}
//*golomb_param = log2_b;
*golomb_param = b;
return 0;
}
/*
* Decode a golomb-encoded input. This function also requires the
* parameter returned by the golomb encode funciton above
*/
int
golomb_decode (void *input, unsigned long input_len,
unsigned int golomb_param, void **out,
unsigned long *outsize)
{
unsigned long setbits, currindex, size, rle_size;
int b, i;
int x, q, r;
int d, bytecounter;
unsigned int *gd;
unsigned int log2_b;
unsigned char *currbyte, *in;
unsigned long decoded_rle_size;
in = (unsigned char*) input;
size = input_len;
b = golomb_param;
if (!in) return -1;
memset (golomb_buf, 0, sizeof (unsigned int) * MAX_INPUT_SIZE);
currbyte = (unsigned char*) in;
gd = (unsigned int*) golomb_buf;
currindex = 0; bytecounter = 0;
q = 0;
x = 0;
/* log2 hack */
log2_b = ceil_log2 (golomb_param);
//printf ("decode: log2_b = %d\n", log2_b);
while (bytecounter < size) {
//printf ("byte: %d: %s %s\n", bytecounter,
//h2b[*currbyte >> 4], h2b[*currbyte & 0x0f]);
/* q := unary_decode() - 1 */
q = 0;
while (*currbyte & (1 << (7 - currindex++))) {
++q;
if (currindex >= 8) {
currbyte++;
bytecounter++;
currindex -= 8;
}
}
//--q;
/* sanity check */
if (currindex >= 8) {
currbyte++;
bytecounter++;
currindex -= 8;
}
//printf ("got q: %d, currbyte: %d, currindex: %d\n",
//q, bytecounter, currindex);
/* r = minimal_binary_decode (b) */
d = (1 << log2_b) - b;
//printf ("d: %d, log2_b-1: %d\n", d, log2_b-1);
GET_ONE_INTEGER (log2_b - 1);
//printf ("got integer: %d\n", x);
//printf ("x+1 (%d) ~ d (%d)\n", x+1, d);
if (x + 1 > d) {
x = (x << 1) | ((*currbyte & (1 << (7 - currindex))) > 0);
x -= d;
currindex++;
if (currindex >= 8) {
currbyte++;
bytecounter++;
currindex -= 8;
}
}
r = x + 1;
//printf ("got remainder: %d, currbyte: %d, currindex: %d\n",
//r, bytecounter, currindex);
/* decode integer */
*gd++ = r + q * b;
//printf ("got answer: %d\n", r + q * b);
}
decoded_rle_size = gd - golomb_buf + 1;
if (get_run_length_decoding (golomb_buf, decoded_rle_size - 1,
(unsigned char**)out,
outsize) ) {
printf ("Golomb decoding worked; RLE decoding failed\n");
return 1;
}
/*
if ( !(*out = malloc (*outsize * sizeof (unsigned int)) ) ) {
perror ("golombdecode: cannot malloc output buf: ");
return 1;
}
if ( !(memcpy (*out, golomb_buf, *outsize * sizeof (unsigned int)) ) ) {
perror ("golombdecode: cannot memcpy to output: ");
free (*out);
return 1;
}
*/
return 0;
}
/*
* This is a hack for calculating run-length encoding. This lookup table
* tells me, for each possible unsigned char, the lengths of the runs
* (as described above). So suppose you have two chars, you can look up
* this table, get the run lengths, and figure out how to splice them
* (taking care of the '0' symbol at the end of the first char's RLE if
* the second char starts with one or more '0's.
*/
unsigned char rle_lookup[256][9] = {
{8,0,0,0,0,0,0,0,0},
{8,0,0,0,0,0,0,0,0},
{7,1,0,0,0,0,0,0,0},
{7,1,0,0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,2,4,0,0,0,0,0},
{1,1,2,4,0,0,0,0,0},
{1,1,2,3,1,0,0,0,0},
{1,1,2,3,1,0,0,0,0},
{1,1,2,2,2,0,0,0,0},
{1,1,2,2,2,0,0,0,0},
{1,1,2,2,1,1,0,0,0},
{1,1,2,2,1,1,0,0,0},
{1,1,2,1,3,0,0,0,0},
{1,1,2,1,3,0,0,0,0},
{1,1,2,1,2,1,0,0,0},
{1,1,2,1,2,1,0,0,0},
{1,1,2,1,1,2,0,0,0},
{1,1,2,1,1,2,0,0,0},
{1,1,2,1,1,1,1,0,0},
{1,1,2,1,1,1,1,0,0},
{1,1,1,5,0,0,0,0,0},
{1,1,1,5,0,0,0,0,0},
{1,1,1,4,1,0,0,0,0},
{1,1,1,4,1,0,0,0,0},
{1,1,1,3,2,0,0,0,0},
{1,1,1,3,2,0,0,0,0},
{1,1,1,3,1,1,0,0,0},
{1,1,1,3,1,1,0,0,0},
{1,1,1,2,3,0,0,0,0},
{1,1,1,2,3,0,0,0,0},
{1,1,1,2,2,1,0,0,0},
{1,1,1,2,2,1,0,0,0},
{1,1,1,2,1,2,0,0,0},
{1,1,1,2,1,2,0,0,0},
{1,1,1,2,1,1,1,0,0},
{1,1,1,2,1,1,1,0,0},
{1,1,1,1,4,0,0,0,0},
{1,1,1,1,4,0,0,0,0},
{1,1,1,1,3,1,0,0,0},
{1,1,1,1,3,1,0,0,0},
{1,1,1,1,2,2,0,0,0},
{1,1,1,1,2,2,0,0,0},
{1,1,1,1,2,1,1,0,0},
{1,1,1,1,2,1,1,0,0},
{1,1,1,1,1,3,0,0,0},
{1,1,1,1,1,3,0,0,0},
{1,1,1,1,1,2,1,0,0},
{1,1,1,1,1,2,1,0,0},
{1,1,1,1,1,1,2,0,0},
{1,1,1,1,1,1,2,0,0},
{1,1,1,1,1,1,1,1,0},
{1,1,1,1,1,1,1,1,0},
};
/*
* This lookup table is to figure out how many of the columns of the
* last lookup table are relevant.. C does not let me have variable
* column lengths for arrays of arrays, remember?
*/
int rle_lookup_sizes[256] = {
2, 1, 3, 2, 3, 2, 4, 3,
3, 2, 4, 3, 4, 3, 5, 4,
3, 2, 4, 3, 4, 3, 5, 4,
4, 3, 5, 4, 5, 4, 6, 5,
3, 2, 4, 3, 4, 3, 5, 4,
4, 3, 5, 4, 5, 4, 6, 5,
4, 3, 5, 4, 5, 4, 6, 5,
5, 4, 6, 5, 6, 5, 7, 6,
3, 2, 4, 3, 4, 3, 5, 4,
4, 3, 5, 4, 5, 4, 6, 5,
4, 3, 5, 4, 5, 4, 6, 5,
5, 4, 6, 5, 6, 5, 7, 6,
4, 3, 5, 4, 5, 4, 6, 5,
5, 4, 6, 5, 6, 5, 7, 6,
5, 4, 6, 5, 6, 5, 7, 6,
6, 5, 7, 6, 7, 6, 8, 7,
3, 2, 4, 3, 4, 3, 5, 4,
4, 3, 5, 4, 5, 4, 6, 5,
4, 3, 5, 4, 5, 4, 6, 5,
5, 4, 6, 5, 6, 5, 7, 6,
4, 3, 5, 4, 5, 4, 6, 5,
5, 4, 6, 5, 6, 5, 7, 6,
5, 4, 6, 5, 6, 5, 7, 6,
6, 5, 7, 6, 7, 6, 8, 7,
4, 3, 5, 4, 5, 4, 6, 5,
5, 4, 6, 5, 6, 5, 7, 6,
5, 4, 6, 5, 6, 5, 7, 6,
6, 5, 7, 6, 7, 6, 8, 7,
5, 4, 6, 5, 6, 5, 7, 6,
6, 5, 7, 6, 7, 6, 8, 7,
6, 5, 7, 6, 7, 6, 8, 7,
7, 6, 8, 7, 8, 7, 9, 8
};
/*
*
* Encoding functions: zlib, lifted from example.c on the zlib
* page, ported to encode and decode arbitrary-length char buffers into
* dynamically allocated (and reallocated) output buffers. Fucking slow.
*
*/
/* Compress from file source to file dest until EOF on source.
def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
allocated for processing, Z_STREAM_ERROR if an invalid compression
level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
version of the library linked do not match, or Z_ERRNO if there is
an error reading or writing the files. */
int
zlib_encode (void *input, unsigned long input_len,
void **output, unsigned long *output_len, int level)
{
int ret, flush;
unsigned have;
z_stream strm;
//unsigned char in[CHUNK];
unsigned char *in;
//unsigned char out[CHUNK];
unsigned char *out_head, *out;
unsigned long bytes_left, bytes_written;
unsigned long output_bytes_left;
int buf_realloc_penalty = BUF_REALLOC_PENALTY;
bytes_left = input_len;
bytes_written = 0;
in = (unsigned char*) input;
/* allocate exactly enough space to out as in */
/* XXX this is probably too much space, since we're encoding
* what are the performance hits of large malloc, i wonder... */
if ( !(out_head = (unsigned char*) malloc (input_len)) ) {
printf ("malloc failed\n");
goto encode_error_save;
}
out = out_head;
output_bytes_left = input_len;
/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, level);
if (ret != Z_OK) {
//return ret;
return 1;
}
/* compress until end of file */
do {
//strm.avail_in = fread(in, 1, CHUNK, source);
if (bytes_left > CHUNK) {
strm.avail_in = CHUNK;
strm.next_in = in;
in += CHUNK;
//bytes_left -= CHUNK;
flush = Z_NO_FLUSH;
} else if (bytes_left == 0) {
printf ("hmm, shouldnt have come here?\n");
break;
} else {
strm.avail_in = bytes_left;
strm.next_in = in;
in += bytes_left;
//bytes_left = 0;
flush = Z_FINISH;
}
//printf ("going ahead with %d bytes\n", strm.avail_in);
//if (ferror(source)) {
//(void)deflateEnd(&strm);
//return Z_ERRNO;
//}
//flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
//strm.next_in = in;
/* run deflate() on input until output buffer not full, finish
compression if all of source has been read in */
do {
/* to take care of the case when output is not big enough */
if (bytes_left && output_bytes_left <= bytes_left) {
unsigned extra = buf_realloc_penalty*CHUNK;
unsigned offset = out - out_head;