-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtd64.c
2172 lines (2118 loc) · 92.2 KB
/
td64.c
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
//
// td64.c
// high-speed lossless tiny data compression of 1 to 64 bytes based on td64
//
// Created by L. Stevan Leonard on 3/21/20.
// Copyright © 2020-2022 L. Stevan Leonard. All rights reserved.
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "td64.h"
#include "td64_internal.h"
#include "tdString.h"
#ifdef TD64_TEST_MODE
// these globals are used to collect info
uint32_t g_td64FailedTextMode=0;
uint32_t g_td64FailedStringMode=0;
uint32_t g_td64MaxStringModeUniquesExceeded=0;
uint32_t g_td64Text8bitCount=0;
uint32_t g_td64AdaptiveText8bitCount=0;
uint32_t g_td64StringBlocks=0;
double g_td64CompressNonSingleValues=0;
double g_td64CompressNSVcnt=0;
uint32_t g_td64CompressNSVblocks=0;
uint32_t g_td64CompressNSVfailures=0;
uint32_t g_td64nNSVcnt=0;
#endif
// fixed bit compression (fbc): for the number of uniques in input, the minimum number of input values for 25% compression
// uniques 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// nvalues 2, 4, 7, 9, 15, 17, 19, 23, 40, 44, 48, 52, 56, 60, 62, 64};
static const uint32_t uniqueLimits25[MAX_TD64_BYTES+1]=
// 2 4 7 9 15 17 19 23
{ 0, 0, 1,1, 2,2,2, 3,3, 4,4,4,4,4,4, 5,5, 6,6, 7,7,7,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
// 40 44 48 52 56 60 62 64
9,9,9,9, 10,10,10,10, 11,11,11,11, 12,12,12,12, 13,13,13,13, 14,14, 15,15, 16
};
// text mode with 16 fixed 4-bit characters
#define MAX_PREDEFINED_CHAR_COUNT 16 // text mode: based on character frequencies from Morse code
static const uint32_t textChars[MAX_PREDEFINED_CHAR_COUNT]={
' ', 'e', 't', 'a', 'i', 'n', 'o', 's', 'h', 'r', 'd', 'l', 'u', 'c', 'm', 'f'
};
// text mode: a one indicates a character from textChars
static const uint32_t predefinedTextChars[256]={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
// text mode: index to predefined char or 16 if another value
static const uint32_t textEncoding[256]={
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 3, 16, 13, 10, 1, 15, 16, 8, 4, 16, 16, 11, 14, 5, 6,
16, 16, 9, 7, 2, 12, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16
};
// adaptive text mode where characters have a variable number of bits
#define MAX_PREDEFINED_FREQUENCY_CHAR_COUNT 23 // adaptive text mode: most frequent characters from Morse code plus CR and comma
#define MAX_PREDEFINED_ADAPTIVE_CHAR_COUNT 23
static const uint32_t extendedTextChars[MAX_PREDEFINED_FREQUENCY_CHAR_COUNT]={
' ', 'e', 't', 'a', 'i', 'n', 'o', 's', 'h', 'r', 'd', 'l', 'u', 'c', 0xA, 'm', 'g', 'f', ',', 'y', 'w', 'p', 'b'
};
static const uint32_t XMLTextChars[MAX_PREDEFINED_ADAPTIVE_CHAR_COUNT]={
' ', '/', '<', '>', 'e', 't', 'a', 'i', 'n', 'o', 's', 'h', 'r', 'd', 'l', 0xA, '.', 'u', 'w', 'c', 0x27, '"', ':'
};
static const uint32_t CTextChars[MAX_PREDEFINED_ADAPTIVE_CHAR_COUNT]={
' ', 'e', 't', 'a', 'i', 'n', 'o', 's', 'h', 'r', 'd', 'l', '*', '=', '\t', 0xA, 'c', ';', 'f', '(', ')', 0x27, '/'
};
// adaptive text mode: a one indicates a character from the most frequent characters based on Morse code set plus a few non-alpha chars
const uint32_t predefinedBitTextChars[256]={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, // 0xA
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // space, comma
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, // a,b,c,d,e,f,g,h,i,l,m,n,o
1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, // p,r,s,t,u,w,y
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
// adaptive text mode: index to predefined char or 99 if another value
static const uint32_t extendedTextEncoding[256]={
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 14, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
0, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 18, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 3, 22, 13, 10, 1, 17, 16, 8, 4, 99, 99, 11, 15, 5, 6,
21, 99, 9, 7, 2, 12, 99, 20, 99, 19, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99
};
// adaptive text mode: values set to frequently occurring characters in XML or HTML data, see initAdaptiveTextMode
static uint32_t adaptiveXMLEncoding[256]={
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99
};
// adaptive text mode: values set to frequently occurring characters in C or other programming language data, see initAdaptiveTextMode
static uint32_t adaptiveCEncoding[256]={
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99
};
int32_t td5(const unsigned char *inVals, unsigned char *outVals, const uint32_t nValues)
// Compress 1 to 5 values
// Management of whether compressible and number of input values must be maintained
// by caller. Decdode requires number of input values and only accepts compressed data.
// Decode first byte:
// 01 = single unique, followed by low 6 bits of input then high two bits in second byte
// 11 = text mode characters, followed by a 4-bit character reference for each input value; all values must be one of the 16 textChars
// 0 = encoding by number of values:
// 1 byte: text mode
// 2 bytes: two unique nibbles
// 3 bytes: two unique nibbles
// 4 or 5 bytes: two uniques
// Arguments:
// inVals input data
// outVals compressed data
// nValues number of values to compress
// returns number of bits compressed, 0 if not compressible, or negative value if error
{
outVals[0] = 1; // default uncompressed reason is general failure
switch (nValues)
{
case 1:
{
// text mode encoding for 1 byte
const uint32_t ival1=(unsigned char)inVals[0];
if (predefinedTextChars[ival1])
{
// output 0 in first bit followed by encoding in next four
outVals[0] = (unsigned char)textEncoding[ival1] << 1;
return 5;
}
return 0;
}
case 2:
{
const uint32_t ival1=(unsigned char)inVals[0];
// encode for 1 unique or 2 nibbles
if (ival1 == (unsigned char)inVals[1])
{
outVals[0] = (unsigned char)(ival1 << 2) | 1;
outVals[1] = (unsigned char)ival1 >> 6;
return 10;
}
// check for two text chars
if (predefinedTextChars[ival1] && predefinedTextChars[inVals[1]])
{
// encode two text chars
outVals[0] = (unsigned char)(3 | (int32_t)textEncoding[ival1] << 2) | ((int32_t)textEncoding[inVals[1]] << 6);
outVals[1] = (unsigned char)textEncoding[inVals[1]] >> 2;
return 10;
}
// check for two unique nibbles
uint32_t outBits=0; // keep track of which nibbles match nibble1 (0) or otherVal (1)
uint32_t otherVal=256;
const uint32_t nibble1=ival1 >> 4;
const uint32_t nibble2=ival1 & 0xf;
const uint32_t nibble3=(unsigned char)inVals[1] >> 4;
if (nibble2 != nibble1)
{
otherVal = nibble2;
outBits = 2;
}
if (nibble3 != nibble1)
{
if ((nibble3 != otherVal) && (otherVal != 256))
return 0;
if (otherVal == 256)
otherVal = nibble3;
outBits |= 4; // set this bit to other value
}
const uint32_t nibble4=(unsigned char)inVals[1] & 0xf;
if (nibble4 != nibble1)
{
if ((nibble4 != otherVal) && (otherVal != 256))
return 0;
if (otherVal == 256)
otherVal = nibble4;
outBits |= 8; // set this bit to other value
}
outVals[0] = (unsigned char)((nibble1 << 4) | outBits);
outVals[1] = (unsigned char)otherVal;
return 12; // save 4 bits
}
case 3:
{
// encode for 1 unique
const uint32_t ival1=(unsigned char)inVals[0];
if ((ival1 == (unsigned char)inVals[1]) && (ival1 == (unsigned char)inVals[2]))
{
outVals[0] = (unsigned char)(ival1 << 2) | 1;
outVals[1] = (unsigned char)ival1 >> 6;
return 10;
}
// check for three text chars
if (predefinedTextChars[ival1] && predefinedTextChars[inVals[1]] && predefinedTextChars[inVals[2]])
{
// encode three text chars
outVals[0] = (unsigned char)(3 | textEncoding[ival1] << 2) | (textEncoding[inVals[1]] << 6);
outVals[1] = (unsigned char)(textEncoding[inVals[1]] >> 2) | (textEncoding[inVals[2]] << 2);
return 14;
}
// check for two unique nibbles
uint32_t outBits=0; // keep track of which nibbles match nibble1 or otherVal
uint32_t otherVal=256;
const uint32_t nibble1=(unsigned char)ival1 >> 4;
const uint32_t nibble2=(unsigned char)ival1 & 0xf;
const uint32_t nibble3=(unsigned char)inVals[1] >> 4;
if (nibble2 != nibble1)
{
otherVal = nibble2;
outBits = 2;
}
if (nibble3 != nibble1)
{
if ((nibble3 != otherVal) && (otherVal != 256))
return 0;
if (otherVal == 256)
otherVal = nibble3;
outBits |= 4; // set this bit to other value
}
const uint32_t nibble4=(unsigned char)inVals[1] & 0xf;
if (nibble4 != nibble1)
{
if ((nibble4 != otherVal) && (otherVal != 256))
return 0;
if (otherVal == 256)
otherVal = nibble4;
outBits |= 8; // set this bit to other value
}
const uint32_t nibble5=(unsigned char)inVals[2] >> 4;
if (nibble5 != nibble1)
{
if ((nibble5 != otherVal) && (otherVal != 256))
return 0;
if (otherVal == 256)
otherVal = nibble5;
outBits |= 16; // set this bit to other value
}
const uint32_t nibble6=(unsigned char)inVals[2] & 0xf;
if (nibble6 != nibble1)
{
if ((nibble6 != otherVal) && (otherVal != 256))
return 0;
if (otherVal == 256)
otherVal = nibble6;
outBits |= 32; // set this bit to other value
}
outVals[0] = (unsigned char)(nibble1 << 6) | (unsigned char)outBits;
outVals[1] = (unsigned char)(otherVal << 2) | (unsigned char)(nibble1 >> 2);
return 14; // save 10 bits
}
case 4:
case 5:
{
const int32_t ival1=(unsigned char)inVals[0];
const int32_t ival2=(unsigned char)inVals[1];
const int32_t ival3=(unsigned char)inVals[2];
const int32_t ival4=(unsigned char)inVals[3];
if (ival1 == ival2 && ival1 == ival3 && ival1 == ival4)
{
if (nValues == 4)
{
// all 4 values equal
outVals[0] = (unsigned char)(ival1 << 2) | 1;
outVals[1] = (unsigned char)ival1 >> 6;
return 10;
}
else if (ival1 == inVals[4])
{
// all 5 values equal
outVals[0] = (unsigned char)(ival1 << 2) | 1;
outVals[1] = (unsigned char)ival1 >> 6;
return 10;
}
}
// check for four and five text chars,
uint32_t nTextChars=predefinedTextChars[ival1]+predefinedTextChars[ival2]+predefinedTextChars[ival3]+predefinedTextChars[ival4];
if (nTextChars == 4)
{
// encode four text chars
outVals[0] = (unsigned char)(3 | textEncoding[ival1] << 2) | (textEncoding[ival2] << 6);
outVals[1] = (unsigned char)(textEncoding[ival2] >> 2) | (textEncoding[ival3] << 2) | (textEncoding[ival4] << 6);
outVals[2] = (unsigned char)textEncoding[ival4] >> 2;
if (nValues == 4)
return 18;
// encode fifth text char
if (predefinedTextChars[inVals[4]])
{
// fifth value is a text char, signal with a 1 in third bit
outVals[2] |= 4 | (unsigned char)textEncoding[inVals[4]] << 3;
return 23;
}
// fifth value is not a text char, output a 0 folllwed by itself
outVals[2] |= inVals[4] << 3;
outVals[3] = inVals[4] >> 5;
return 27;
}
// encode for 2 uniques
int32_t outBits=0; // first bit is 1 for one unique
// encode 0 for first val, 1 for other val, starting at second bit
int32_t otherVal=-1;
if (ival2 != ival1)
{
otherVal = ival2;
outBits |= 2;
}
if (ival3 != ival1)
{
if ((ival3 != otherVal) && (otherVal != -1))
return 0;
if (otherVal == -1)
otherVal = ival3;
outBits |= 4;
}
if (ival4 != ival1)
{
if ((ival4 != otherVal) && (otherVal != -1))
return 0;
if (otherVal == -1)
otherVal = ival4;
outBits |= 8;
}
if (nValues == 4)
{
// or in low 4 bits second unique in top bits
outVals[0] = (unsigned char)(outBits | (ival1 << 4));
outVals[1] = (unsigned char)((ival1 >> 4) | (otherVal << 4));
outVals[2] = (unsigned char)(otherVal >> 4);
return 20;
}
// continue to check fifth value
const int32_t ival5=(unsigned char)inVals[4];
if (ival5 != ival1)
{
if ((ival5 != otherVal) && (otherVal != -1))
return 0;
if (otherVal == -1)
otherVal = ival5;
outBits |= 0x10;
}
// clear low bit byte 0 (not single unique) and or in low 3 bits ival1
outVals[0] = (unsigned char)(outBits | (ival1 << 5));
outVals[1] = (unsigned char)(((ival1 >> 3)) | (otherVal << 5));
outVals[2] = (unsigned char)(otherVal >> 3);
return 21;
}
default:
return -9; // number of values specified not supported (1 to 5 handled here)
}
} // end td5
int32_t td5d(const unsigned char *inVals, unsigned char *outVals, const uint32_t nOriginalValues, uint32_t *bytesProcessed)
// Decode 1 to 5 values encoded by td5.
// Decode first byte:
// 01 = single unique, followed by low 6 bits of input; otherwise, high two bits in second byte
// 11 = text encoding: replace 4-bit text index by corresponding char
// 1 to 4 values requires all values text, 5 chars can have final char non-text, indicated by 0 or 1 bit following 4 text indexes
// 0 = encoding by number of values:
// 1 byte: text mode
// 2 bytes: two nibbles the same
// 3 bytes: single unique
// 4 or 5 bytes: two uniques
// Arguments:
// inVals compressed data with fewer bits than in original values
// outVals decompressed data
// nOriginalValues number of values in the original input to td5: required input
// bytesProcessed number of input bytes processed, which can be used by caller to position past these bytes
// returns number of bytes output or -1 if error
{
const uint32_t firstByte=(unsigned char)inVals[0];
if ((firstByte & 3) == 1)
{
// process single unique
uint32_t unique = (firstByte >> 2) | (inVals[1] << 6);
memset(outVals, (unsigned char)unique, nOriginalValues);
*bytesProcessed = 2;
return (int)nOriginalValues;
}
uint32_t val1;
uint32_t val2;
const uint32_t secondByte = (unsigned char)inVals[1];
switch (nOriginalValues)
{
case 1:
{
// text mode encoding for 1 byte
outVals[0] = (unsigned char)textChars[(firstByte>>1) & 0xf];
*bytesProcessed = 1;
return 1;
}
case 2:
{
*bytesProcessed = 2;
if ((firstByte & 3) == 3)
{
// text mode encoding: next two four-bit values are text offsets
outVals[0] = (unsigned char)textChars[(firstByte>>2) & 0xf];
outVals[1] = (unsigned char)textChars[(firstByte>>6) | ((secondByte<<2) & 0xf)];
return 2;
}
// decode nibble compression for 2 bytes
// nibble1 nibble 2 nibble3 nibble4
// high low high low
// first byte second byte
const uint32_t cbits = (unsigned char)(firstByte >> 1) & 0x7; // 3 control bits
const uint32_t nibble1 = (unsigned char)firstByte >> 4;
const uint32_t nibble2 = (unsigned char)secondByte & 0xf;
outVals[0] = (unsigned char)(nibble1 << 4) | (unsigned char)((cbits & 1) ? nibble2 : nibble1);
outVals[1] = (unsigned char)((((cbits & 2) ? nibble2 : nibble1) << 4) |
(unsigned char)((cbits & 4) ? nibble2 : nibble1));
return 2;
}
case 3:
{
*bytesProcessed = 2;
if ((firstByte & 3) == 3)
{
// text mode encoding: next three four-bit values are text offsets
outVals[0] = (unsigned char)textChars[(firstByte>>2) & 0xf];
outVals[1] = (unsigned char)textChars[(firstByte>>6) | ((secondByte<<2) & 0xf)];
outVals[2] = (unsigned char)textChars[(secondByte>>2) & 0xf];
return 3;
}
// decode nibble compression for 3 bytes
// nibble1 nibble 2 nibble3 nibble4 nibble5 nibble6
// high low high low high low
// first byte second byte third byte
const uint32_t cbits = ((unsigned char)firstByte >> 1) & 0x1f; // 5 control bits
const uint32_t nibble1 = ((unsigned char)firstByte >> 6 | ((unsigned char)secondByte << 2)) & 0xf;
const uint32_t nibble2 = ((unsigned char)secondByte >> 2) & 0xf;
outVals[0] = (unsigned char)(nibble1 << 4) | (unsigned char)((cbits & 1) ? nibble2 : nibble1);
outVals[1] = (unsigned char)((((cbits & 2) ? nibble2 : nibble1) << 4) |
(unsigned char)((cbits & 4) ? nibble2 : nibble1));
outVals[2] = (unsigned char)((((cbits & 8) ? nibble2 : nibble1) << 4) |
(unsigned char)((cbits & 16) ? nibble2 : nibble1));
return 3;
}
case 4:
{
*bytesProcessed = 3;
if ((firstByte & 3) == 3)
{
// text mode encoding: next four four-bit values are text offsets
outVals[0] = (unsigned char)textChars[(firstByte>>2) & 0xf];
outVals[1] = (unsigned char)textChars[(firstByte>>6) | ((secondByte<<2) & 0xf)];
outVals[2] = (unsigned char)textChars[(secondByte>>2) & 0xf];
outVals[3] = (unsigned char)textChars[(secondByte>>6) | ((inVals[2]<<2) & 0xf)];
return 4;
}
const int32_t thirdByte = (unsigned char)inVals[2];
val1 = (unsigned char)(firstByte >> 4) | (unsigned char)(secondByte << 4);
val2 = (unsigned char)(secondByte >> 4) | (unsigned char)(thirdByte << 4);
outVals[0] = (unsigned char)val1;
outVals[1] = (firstByte & 2) ? (unsigned char)val2 : (unsigned char)val1;
outVals[2] = (firstByte & 4) ? (unsigned char)val2 : (unsigned char)val1;
outVals[3] = (firstByte & 8) ? (unsigned char)val2 : (unsigned char)val1;
return 4;
}
case 5:
{
*bytesProcessed = 3;
if ((firstByte & 3) == 3)
{
// text mode encoding: next four four-bit values are text offsets
outVals[0] = (unsigned char)textChars[(firstByte>>2) & 0xf];
outVals[1] = (unsigned char)textChars[(firstByte>>6) | ((secondByte<<2) & 0xf)];
outVals[2] = (unsigned char)textChars[(secondByte>>2) & 0xf];
uint32_t thirdByte = inVals[2];
outVals[3] = (unsigned char)textChars[(secondByte>>6) | ((thirdByte<<2) & 0xf)];
if (thirdByte & 0x4)
{
// fifth byte is text char
outVals[4] = (unsigned char)textChars[(thirdByte >> 3) & 0xf];
}
else
{
// fifth byte is 8-bit value
outVals[4] = (unsigned char)(thirdByte >> 3) | ((inVals[3]<<5) & 0xff);
*bytesProcessed = 4;
}
return 5;
}
const int32_t thirdByte = (unsigned char)inVals[2];
val1 = (unsigned char)(firstByte >> 5) | (unsigned char)(secondByte << 3);
val2 = (unsigned char)(secondByte >> 5) | (unsigned char)(thirdByte << 3);
outVals[0] = (unsigned char)val1;
outVals[1] = (firstByte & 2) ? (unsigned char)val2 : (unsigned char)val1;
outVals[2] = (firstByte & 4) ? (unsigned char)val2 : (unsigned char)val1;
outVals[3] = (firstByte & 8) ? (unsigned char)val2 : (unsigned char)val1;
outVals[4] = (firstByte & 0x10) ? (unsigned char)val2 : (unsigned char)val1;
return 5;
}
default:
return -3; // unexpected program error
}
} // end td5d
static uint32_t textNBitsTable[MAX_PREDEFINED_FREQUENCY_CHAR_COUNT]={
3, 3, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
7, 7, 7, 7
};
static uint32_t textBitValTable[MAX_PREDEFINED_FREQUENCY_CHAR_COUNT]={
1, 3, 7, 15,
0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28,
0x1e, 0x3e, 0x5e, 0x7e
};
void initAdaptiveTextMode(void)
{
adaptiveXMLEncoding[' '] = 0;
adaptiveXMLEncoding['/'] = 1;
adaptiveXMLEncoding['<'] = 2;
adaptiveXMLEncoding['>'] = 3;
adaptiveXMLEncoding['e'] = 4;
adaptiveXMLEncoding['t'] = 5;
adaptiveXMLEncoding['a'] = 6;
adaptiveXMLEncoding['i'] = 7;
adaptiveXMLEncoding['n'] = 8;
adaptiveXMLEncoding['o'] = 9;
adaptiveXMLEncoding['s'] = 10;
adaptiveXMLEncoding['h'] = 11;
adaptiveXMLEncoding['r'] = 12;
adaptiveXMLEncoding['d'] = 13;
adaptiveXMLEncoding['l'] = 14;
adaptiveXMLEncoding[0xA] = 15;
adaptiveXMLEncoding['.'] = 16;
adaptiveXMLEncoding['u'] = 17;
adaptiveXMLEncoding['w'] = 18;
adaptiveXMLEncoding['c'] = 19;
adaptiveXMLEncoding[0x27] = 20;
adaptiveXMLEncoding['"'] = 21;
adaptiveXMLEncoding[':'] = 22;
adaptiveCEncoding[' '] = 0;
adaptiveCEncoding['e'] = 1;
adaptiveCEncoding['t'] = 2;
adaptiveCEncoding['a'] = 3;
adaptiveCEncoding['i'] = 4;
adaptiveCEncoding['n'] = 5;
adaptiveCEncoding['o'] = 6;
adaptiveCEncoding['s'] = 7;
adaptiveCEncoding['h'] = 8;
adaptiveCEncoding['r'] = 9;
adaptiveCEncoding['d'] = 10;
adaptiveCEncoding['l'] = 11;
adaptiveCEncoding['*'] = 12;
adaptiveCEncoding['='] = 13;
adaptiveCEncoding['\t'] = 14;
adaptiveCEncoding[0xA] = 15;
adaptiveCEncoding['c'] = 16;
adaptiveCEncoding[';'] = 17;
adaptiveCEncoding['f'] = 18;
adaptiveCEncoding['('] = 19;
adaptiveCEncoding[')'] = 20;
adaptiveCEncoding[0x27] = 21;
adaptiveCEncoding['/'] = 22;
}
static uint32_t initAdaptiveChars; // called once to init
static inline uint32_t setAdaptiveChars(const unsigned char *val256, unsigned char *outVals, const uint32_t nValues, const uint32_t **textEncodingArray)
{
// if count of characters particular to XML or HTML or C code, set the 8 lowest frequency characers to common characters for that data type
const uint32_t minCharCount=nValues < 24 ? 2 : 3; // minimum chars to choose an adaptive text set
if (initAdaptiveChars == 0)
{
initAdaptiveTextMode();
initAdaptiveChars = 1;
}
if (val256['<'] + val256['>'] + val256['/'] + val256['"'] +val256[':'] >= minCharCount)
{
// XML or HTML
*textEncodingArray = adaptiveXMLEncoding;
outVals[0] = 0x17; // indicate text mode with html/xml chars
return 1;
}
else if (val256[','] + val256['='] + val256[';'] + val256['('] + val256[')'] + val256['/'] + val256['*'] >= minCharCount)
{
// C or similar
*textEncodingArray = adaptiveCEncoding;
outVals[0] = 0x27; // indicate text mode with C or similar chars
return 2;
}
else
{
// standard text
*textEncodingArray = extendedTextEncoding;
outVals[0] = 0x7; // indicate standard text mode
return 0;
}
} // end setAdaptiveChars
int32_t encodeAdaptiveTextMode(const unsigned char *inVals, unsigned char *outVals, const uint32_t nValues, const unsigned char *val256, const uint32_t predefinedTextCharCnt, const uint32_t highBitclear, const uint32_t maxBytes)
{
// Use these frequency-related bit encodings:
// 101 value not in 23 text values, followed by 8-bit value
// 011, 001 2 values of highest frequency
// x111 2 values of next highest frequency
// xxxx0 15 values of medium frequency, except 11110
// xx11110 4 values of lowest frequency
const unsigned char *pInVal=inVals;
const unsigned char *pLastInValPlusOne=inVals+nValues;
uint32_t inVal;
uint32_t nextOutIx=1;
uint32_t nextOutBit=0;
uint64_t outBits=0; // store 64 bits before writing
uint32_t eVal;
const uint32_t *textEncodingArray=extendedTextEncoding;
const uint32_t output7or8=highBitclear ? 7 : 8;
if (predefinedTextCharCnt)
outVals[0] = 0x7; // default to standard text mode if predefined text char count is high enough
else
setAdaptiveChars(val256, outVals, nValues, &textEncodingArray);
if (highBitclear)
outVals[0] |= 128; // set high bit of info byte to indicate 7-bit values
while (pInVal < pLastInValPlusOne)
{
eVal=textEncodingArray[(inVal=(unsigned char)*(pInVal++))];
if (eVal < MAX_PREDEFINED_FREQUENCY_CHAR_COUNT)
{
// encode predefined chars and adaptive chars
thisOutIx2(outVals, textNBitsTable[eVal], textBitValTable[eVal], &nextOutIx, &nextOutBit, &outBits);
}
else
{
// output char not predefined or adaptive
if (nextOutIx > maxBytes)
return 0; // requested compression not met
thisOutIx2(outVals, 3, 0x5, &nextOutIx, &nextOutBit, &outBits);
thisOutIx2(outVals, output7or8, inVal, &nextOutIx, &nextOutBit, &outBits); // output 7 bits if high bit clear, else 8
#ifdef TD64_TEST_MODE
if (textEncodingArray == extendedTextEncoding)
g_td64Text8bitCount++;
else
g_td64AdaptiveText8bitCount++;
#endif
}
}
esmOutputRemainder(outVals, &nextOutIx, &nextOutBit, &outBits);
return nextOutIx * 8;
} // end encodeAdaptiveTextMode
int32_t encodeSingleValueMode(const unsigned char *inVals, unsigned char *outVals, const uint32_t nValues, int32_t singleValue, const uint32_t compressNSV)
{
// generate control bit 1 if single value, otherwise 0 plus 8-bit value
const unsigned char *pInVal=inVals;
const unsigned char *pLastInValPlusOne=inVals+nValues;
uint32_t inVal;
uint32_t nextOutVal=(nValues-1)/8+2; // allocate space for control bits in bytes following first
uint64_t controlByte=0;
uint64_t controlBit=1;
// output indicator byte, number uniques is 0, followed by next bit set
outVals[0] = 0x05; // indicate single value mode
outVals[nextOutVal++] = (unsigned char)singleValue;
while (pInVal < pLastInValPlusOne)
{
if ((inVal=*(pInVal++)) != (uint32_t)singleValue)
{
// encode 4-bit predefined index textIndex
controlBit <<= 1;
outVals[nextOutVal++] = (unsigned char)inVal;
}
else
{
// controlByte gets a 1 at this bit position to indicate single value
controlByte |= controlBit;
controlBit <<= 1;
}
}
// output control bytes for nValues
switch ((nValues-1)/8)
{
case 0:
outVals[1] = (unsigned char)controlByte;
break;
case 1:
outVals[1] = (unsigned char)controlByte;
outVals[2] = (unsigned char)(controlByte>>8);
break;
case 2:
outVals[1] = (unsigned char)controlByte;
outVals[2] = (unsigned char)(controlByte>>8);
outVals[3] = (unsigned char)(controlByte>>16);
break;
case 3:
outVals[1] = (unsigned char)controlByte;
outVals[2] = (unsigned char)(controlByte>>8);
outVals[3] = (unsigned char)(controlByte>>16);
outVals[4] = (unsigned char)(controlByte>>24);
break;
case 4:
outVals[1] = (unsigned char)controlByte;
outVals[2] = (unsigned char)(controlByte>>8);
outVals[3] = (unsigned char)(controlByte>>16);
outVals[4] = (unsigned char)(controlByte>>24);
outVals[5] = (unsigned char)(controlByte>>32);
break;
case 5:
outVals[1] = (unsigned char)controlByte;
outVals[2] = (unsigned char)(controlByte>>8);
outVals[3] = (unsigned char)(controlByte>>16);
outVals[4] = (unsigned char)(controlByte>>24);
outVals[5] = (unsigned char)(controlByte>>32);
outVals[6] = (unsigned char)(controlByte>>40);
break;
case 6:
outVals[1] = (unsigned char)controlByte;
outVals[2] = (unsigned char)(controlByte>>8);
outVals[3] = (unsigned char)(controlByte>>16);
outVals[4] = (unsigned char)(controlByte>>24);
outVals[5] = (unsigned char)(controlByte>>32);
outVals[6] = (unsigned char)(controlByte>>40);
outVals[7] = (unsigned char)(controlByte>>48);
break;
case 7:
outVals[1] = (unsigned char)controlByte;
outVals[2] = (unsigned char)(controlByte>>8);
outVals[3] = (unsigned char)(controlByte>>16);
outVals[4] = (unsigned char)(controlByte>>24);
outVals[5] = (unsigned char)(controlByte>>32);
outVals[6] = (unsigned char)(controlByte>>40);
outVals[7] = (unsigned char)(controlByte>>48);
outVals[8] = (unsigned char)(controlByte>>56);
break;
}
if (compressNSV)
{
uint32_t firstNonSingle=(nValues-1)/8+3; // skip single value itself
uint32_t nNSV=nextOutVal-firstNonSingle+1;
if (nNSV >= MIN_STRING_MODE_EXTENDED_VALUES)
{
unsigned char outTemp[64];
int32_t retBits;
uint32_t nValuesOut;
#ifdef TD64_TEST_MODE
g_td64CompressNSVcnt += nNSV;
g_td64CompressNSVblocks++;
g_td64nNSVcnt += nNSV;
#endif
retBits = encodeExtendedStringMode(outVals+firstNonSingle, outTemp, nNSV, &nValuesOut);
if (retBits < 0)
return -28;
if (retBits > (nNSV-2)*8 || retBits == 0)
{
#ifdef TD64_TEST_MODE
g_td64CompressNonSingleValues += nNSV*8; // not enough compression
g_td64CompressNSVfailures++;
#endif
}
else
{
// non-string mode values compressed: set bit 4
outVals[0] |= 8;
outVals[firstNonSingle] = nNSV; // store number non-single values
// copy compressed uniques in temp outvals to outvals
uint32_t nBytes=retBits/8;
if (retBits & 7)
nBytes++;
// don't keep first byte that is 0x7f for extended string mode
memcpy(outVals+firstNonSingle+1, outTemp+1, nBytes-1);
#ifdef TD64_TEST_MODE
g_td64CompressNonSingleValues += retBits-8;
#endif
return (firstNonSingle+1)*8 + retBits-8;
}
}
}
return (int32_t)nextOutVal * 8; // round up to full byte
} // end encodeSingleValueMode
int32_t encode7bits(const unsigned char *inVals, unsigned char *outVals, const uint32_t nValues)
{
uint32_t nextOutVal=1;
uint32_t nextInVal=0;
uint32_t val1;
uint32_t val2;
if (nValues < MIN_VALUES_7_BIT_MODE)
return 0;
outVals[0] = 0x03; // indicate 7-bit mode
// process groups of 8 bytes to output 7 bytes
while (nextInVal + 7 < nValues)
{
// copy groups of 8 7-bit vals into 7 bytes
val1 = inVals[nextInVal++];
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)(val1 | (val2 << 7));
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val2 >> 1) | (val1 << 6));
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val1 >> 2) | (val2 << 5));
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val2 >> 3) | (val1 << 4));
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val1 >> 4) | (val2 << 3));
val1 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val2 >> 5) | (val1 << 2));
val2 = inVals[nextInVal++];
outVals[nextOutVal++] = (unsigned char)((val1 >> 6) | (val2 << 1));
}
while (nextInVal < nValues)
{
// output final values as full bytes because no bytes saved, only bits
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
if (nextInVal == nValues)
break;
outVals[nextOutVal++] = inVals[nextInVal++];
break;
}
return (int32_t)nextOutVal*8;
} // end encode7bits
#define STRING_LIMIT 9
int32_t encodeStringMode(const unsigned char *inVals, unsigned char *outVals, const uint32_t nValues, const uint32_t nUniquesIn, const uint32_t *uniqueOccurrence, const uint32_t highBitClear, const uint32_t maxBits)
{
uint32_t twoValsPos[256]; // set to position+1 of first occurrence of value
uint32_t nUniques; // first value is always a unique
// if all inputs have high bit 0, compress the input values
// set first bit of second value to 0 if not compressed, or 1 if compressed and followed by the compressed values
// if not compressed, first bit of first output past uniques is high bit of first unique
//uint32_t nextOutIx=nUniquesIn + 1; // start of encoding past uniques written from outer loop;
uint32_t nextOutIx; // round for now
uint32_t nextOutBit=1; // first bit indicates 1 or 2 uniques in first two input values
uint64_t outBits; // store 64 bits before writing
const uint32_t maxBytes=maxBits/8; // compare against bytes out during loop
if (nUniquesIn > 32 || nUniquesIn < MIN_STRING_MODE_UNIQUES)
return -19;
// use 7-bit encoding for uniques if all high bits set
if (highBitClear)
{
uint32_t retBytes=encode7bitsInternal(outVals+1, outVals+1, nUniquesIn);
nextOutIx = retBytes + 1;
outVals[0] = 9 | (unsigned char)((nUniquesIn-17)<<4); // indicate string mode in first 3 bits, 1 for uniques compressed, then number original uniques - 17 (excess 16 as always 17+ values) in next 4 bits
}
else
{
nextOutIx=nUniquesIn+1;
outVals[0] = 1 | (unsigned char)((nUniquesIn-17)<<4); // indicate string mode in first 3 bits, 0 for uniques uncompressed, then number uniques - 17 (excess 16 as always 17+ values) in next 4 bits
}
// output two initial values
// first unique assumed
const unsigned char inVal0=inVals[0];
if (inVal0 == inVals[1])
{
// first two values are the same
nUniques = 1;
// output 1 to indicate first unique value repeated
outBits = 1; // 1=repeat for second value
twoValsPos[inVal0] = 1;
}
else
{
// second val is a new unique
nUniques = 2;
// set up position of 2nd unique
outBits = 0; // 0=uniques in first two values
twoValsPos[inVal0] = 1;
twoValsPos[inVals[1]] = 2;
}
uint32_t inPos=2; // start loop after init of first two values
const uint32_t lastPos=nValues-1;
uint32_t nextInVal = inVals[2];
while (inPos < lastPos)
{
if (nextOutIx > maxBytes)
return 0; // already failed compression minimum
const uint32_t inVal=nextInVal;
const uint32_t uoInVal=uniqueOccurrence[inVal];
nextInVal = inVals[++inPos]; // set up for next input as well as comparison later
if (uoInVal > nUniques-1)
{
// first occurrence of this unique
// output a 0 to indicate new unique
if (++nextOutBit == 64)
{
// output outBits and init for next output
esmOutputOutBits(outVals, &nextOutIx, &outBits);
outBits = 0;
nextOutBit = 0;
}
twoValsPos[inVal] = inPos;
nUniques++;
continue;
}
uint32_t tvPos = twoValsPos[inVal];
if (nextInVal == inVals[tvPos])
{
if (tvPos == inPos-1)
{
// pos of unique plus one and next input value match
// output repeated value: 01 plus unique