-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxnz.h
2126 lines (1721 loc) · 88.5 KB
/
xnz.h
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
#ifndef _XNZ_H_
#define _XNZ_H_
#ifdef __cplusplus
extern "C" {
#endif
//inline doesn't exist in C89, __inline is MSVC specific
#ifndef XNZ_INLINE
#ifdef _WIN32
#define XNZ_INLINE __inline
#else
#define XNZ_INLINE
#endif
#endif
//inline doesn't exist in C89, __inline is MSVC specific
#ifndef XNZ_DECLSPEC
#ifdef _WIN32
#define XNZ_DECLSPEC __declspec
#else
#define XNZ_DECLSPEC
#endif
#endif
//align functions are diffent on windows vs iOS, Linux, etc.
#ifndef XNZ_ALIGN//(X)
#ifdef _WIN32
#define XNZ_ALIGN(X) (align(X))
#else
#define XNZ_ALIGN(X) __attribute__ ((aligned(X)))
#endif
#endif
#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
// ********************************** Unsigned Byte Vector Types *************************************** //
//packed_float2, packed_float3, packed_float4 equivalents
XNZ_DECLSPEC XNZ_ALIGN(4) typedef union xnz_byte4_packed// 40 bytes
{
XNZ_DECLSPEC XNZ_ALIGN(4) struct { unsigned char x, y, z, w; };
XNZ_DECLSPEC XNZ_ALIGN(4) struct { unsigned char r, g, b, a; };
XNZ_DECLSPEC XNZ_ALIGN(4) unsigned char vector[4];
//XNZ_DECLSPEC XNZ_ALIGN(4) __m128 simd;
}xnz_byte4_packed;
typedef xnz_byte4_packed xnz_packed_byte4;
XNZ_DECLSPEC XNZ_ALIGN(4) typedef union xnz_byte3_packed
{
XNZ_DECLSPEC XNZ_ALIGN(4) struct { unsigned char x, y, z; };
XNZ_DECLSPEC XNZ_ALIGN(4) unsigned char vector[3];
}xnz_byte3_packed;
typedef xnz_byte3_packed xnz_byte3_packed;
XNZ_DECLSPEC XNZ_ALIGN(4) typedef union xnz_byte2_packed
{
XNZ_DECLSPEC XNZ_ALIGN(4) struct { unsigned char x, y; };
XNZ_DECLSPEC XNZ_ALIGN(4) unsigned char vector[2];
}xnz_byte2_packed;
typedef xnz_byte2_packed xnz_byte2_packed;
//float2, float3, float4 equivalents
XNZ_DECLSPEC XNZ_ALIGN(16) typedef struct xnz_byte4 // 48 bytes (multiple of 16 because of simd variable)
{
union{
XNZ_DECLSPEC XNZ_ALIGN(16) struct { unsigned char x, y, z, w; };
XNZ_DECLSPEC XNZ_ALIGN(16) struct { unsigned char r, g, b, a; };
XNZ_DECLSPEC XNZ_ALIGN(16) unsigned char vector[4];
//#if defined(XNZ_USE_SIMD)
// XNZ_DECLSPEC XNZ_ALIGN(16) __m128 simd;
//#endif
XNZ_DECLSPEC XNZ_ALIGN(16) xnz_byte4_packed packed; //will allow use of assignment (copy) operator for vbos
};
}xnz_byte4;
/*XNZ_DECLSPEC XNZ_ALIGN(16)*/ typedef union xnz_byte3
{
/*XNZ_DECLSPEC XNZ_ALIGN(16)*/ struct { unsigned char x, y, z; };
/*XNZ_DECLSPEC XNZ_ALIGN(16)*/ unsigned char vector[3];
}xnz_byte3;
XNZ_DECLSPEC XNZ_ALIGN(16) typedef union xnz_byte2
{
XNZ_DECLSPEC XNZ_ALIGN(16) struct { unsigned char x, y; };
XNZ_DECLSPEC XNZ_ALIGN(16) unsigned char vector[2];
XNZ_DECLSPEC XNZ_ALIGN(16) xnz_byte2_packed packed; //will allow use of assignment (copy) operator for vbos
}xnz_byte2;
//linked list node structs
XNZ_DECLSPEC XNZ_ALIGN(16) typedef struct xnz_byte4_node// 48 bytes (multiple of 16 because of simd variable)
{
XNZ_DECLSPEC XNZ_ALIGN(16) unsigned char x,y,z,w;
XNZ_DECLSPEC XNZ_ALIGN(16) struct xnz_byte4_node * next; //will allow use of assignment (copy) operator for vbos
}xnz_byte4_node;
// ********************************** Unsigned Integer Vector Types *************************************** //
//packed_float2, packed_float3, packed_float4 equivalents
XNZ_DECLSPEC XNZ_ALIGN(4) typedef union xnz_uint2_packed
{
XNZ_DECLSPEC XNZ_ALIGN(4) struct { unsigned int x, y; };
XNZ_DECLSPEC XNZ_ALIGN(4) unsigned int vector[2];
}xnz_uint2_packed;
//typedef xnz_uint2_packed xnz_uint2_packed;
XNZ_DECLSPEC XNZ_ALIGN(4) typedef union xnz_uint4_packed // 40 bytes
{
XNZ_DECLSPEC XNZ_ALIGN(4) struct {
unsigned int x, y;
union { unsigned int z; unsigned int width; };
union { unsigned int w; unsigned int height; };
};
//CR_MATH_DECLSPEC CR_MATH_ALIGN(4) struct { unsigned int x, y, width, height; };
XNZ_DECLSPEC XNZ_ALIGN(4) unsigned int vector[4];
//CR_MATH_DECLSPEC CR_MATH_ALIGN(4) __m128 simd;
}xnz_uint4_packed;
//float2, float3, float4 equivalents
XNZ_DECLSPEC XNZ_ALIGN(16) typedef union xnz_uint2
{
XNZ_DECLSPEC XNZ_ALIGN(16) struct { unsigned int x, y; };
XNZ_DECLSPEC XNZ_ALIGN(16) unsigned int vector[2];
XNZ_DECLSPEC XNZ_ALIGN(16) xnz_uint2_packed packed; //will allow use of assignment (copy) operator for vbos
}xnz_uint2;
XNZ_DECLSPEC XNZ_ALIGN(16) typedef struct xnz_uint4 // 48 bytes (multiple of 16 because of simd variable)
{
union
{
XNZ_DECLSPEC XNZ_ALIGN(4) struct { unsigned int x, y, z, w; };
XNZ_DECLSPEC XNZ_ALIGN(16) struct { xnz_uint2_packed origin, size; }; //will allow use of assignment (copy) operator for vbos
XNZ_DECLSPEC XNZ_ALIGN(16) unsigned int vector[4];
#if defined(XNZ_USE_SIMD)
XNZ_DECLSPEC CR_MATH_ALIGN(16) __m128 simd;
#endif
XNZ_DECLSPEC XNZ_ALIGN(16) xnz_uint4_packed packed; //will allow use of assignment (copy) operator for vbos
};
}xnz_uint4;
// ********************************** Float Vector Types *************************************** //
//packed_float2, packed_float3, packed_float4 equivalents
XNZ_DECLSPEC XNZ_ALIGN(4) typedef union xnz_float2_packed
{
XNZ_DECLSPEC XNZ_ALIGN(4) struct { float x, y; };
XNZ_DECLSPEC XNZ_ALIGN(4) float vector[2];
}xnz_float2_packed;
typedef xnz_float2_packed xnz_packed_float2;
XNZ_DECLSPEC XNZ_ALIGN(4) typedef union xnz_float3_packed
{
XNZ_DECLSPEC XNZ_ALIGN(4) struct { float x, y, z; };
XNZ_DECLSPEC XNZ_ALIGN(4) float vector[3];
}xnz_float3_packed;
XNZ_DECLSPEC XNZ_ALIGN(4) typedef union xnz_float4_packed // 40 bytes
{
XNZ_DECLSPEC XNZ_ALIGN(4) struct { float x, y, z, w; };
XNZ_DECLSPEC XNZ_ALIGN(4) struct { float r, g, b, a; };
XNZ_DECLSPEC XNZ_ALIGN(4) float vector[4];
XNZ_DECLSPEC XNZ_ALIGN(4) xnz_float3_packed float3; //will allow use of assignment (copy) operator for vbos
//CR_MATH_DECLSPEC CR_MATH_ALIGN(4) __m128 simd;
}xnz_float4_packed;
typedef xnz_float4_packed xnz_packed_float4;
//float2, float3, float4 equivalents
XNZ_DECLSPEC XNZ_ALIGN(16) typedef union xnz_float2
{
XNZ_DECLSPEC XNZ_ALIGN(16) struct { float x, y; };
XNZ_DECLSPEC XNZ_ALIGN(16) float vector[2];
XNZ_DECLSPEC XNZ_ALIGN(16) xnz_packed_float2 packed; //will allow use of assignment (copy) operator for vbos
}xnz_float2;
XNZ_DECLSPEC XNZ_ALIGN(16) typedef union xnz_float3
{
XNZ_DECLSPEC XNZ_ALIGN(16) struct { float x, y, z; };
XNZ_DECLSPEC XNZ_ALIGN(16) float vector[3];
XNZ_DECLSPEC XNZ_ALIGN(4) xnz_float3_packed packed; //will allow use of assignment (copy) operator for vbos
XNZ_DECLSPEC XNZ_ALIGN(16) xnz_float2 float2;
}xnz_float3;
XNZ_DECLSPEC XNZ_ALIGN(16) typedef union xnz_float_affine_transform
{
XNZ_DECLSPEC XNZ_ALIGN(16) struct { float a,b,c,d,e,f; };
XNZ_DECLSPEC XNZ_ALIGN(16) float vector[6];
#if defined(XNZ_USE_SIMD)
XNZ_DECLSPEC XNZ_ALIGN(16) __m128 simd;
#endif
XNZ_DECLSPEC XNZ_ALIGN(16) xnz_float4_packed packed; //will allow use of assignment (copy) operator for vbos
XNZ_DECLSPEC XNZ_ALIGN(16) xnz_float3 float3; //will allow use of assignment (copy) operator for vbos
}xnz_float_affine_transform;
//linked list node structs
/*
CR_MATH_DECLSPEC CR_MATH_ALIGN(16) typedef struct xnz_uint4_node// 48 bytes (multiple of 16 because of simd variable)
{
CR_MATH_DECLSPEC CR_MATH_ALIGN(16) uint32_t x, y, z, w;
CR_MATH_DECLSPEC CR_MATH_ALIGN(16) struct xnz_uint4_node* next; //will allow use of assignment (copy) operator for vbos
}xnz_uint4_node;
#define XNZ_RECT_ZERO { 0,0,0,1 }
static xnz_uint4 xnz_uint4_zero = XNZ_RECT_ZERO;
*/
/***
* xnz_byte_utils.h
* XNZ
*
* Created by Joe Moulton on 1/8/17
* Copyright � 2017 Abstract Embedded. All rights reserved.
***/
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
# define __WINDOWS__
#endif
/** compatibility header for endian.h
* This is a simple compatibility shim to convert
* BSD/Linux endian macros to the Mac OS X equivalents.
* It is public domain.
* */
//8 bit stubs are here for posterity, such as when using x-macro expansion for serializing with a function macro
#define xnz_htobe8(x) x
#define xnz_htole8(x) x
#define xnz_be8toh(x) x
#define xnz_le8toh(x) x
//Platform specific byte conversion functions
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define xnz_htobe16(x) OSSwapHostToBigInt16(x)
#define xnz_htole16(x) OSSwapHostToLittleInt16(x)
#define xnz_be16toh(x) OSSwapBigToHostInt16(x)
#define xnz_le16toh(x) OSSwapLittleToHostInt16(x)
#define xnz_htobe32(x) OSSwapHostToBigInt32(x)
#define xnz_htole32(x) OSSwapHostToLittleInt32(x)
#define xnz_be32toh(x) OSSwapBigToHostInt32(x)
#define xnz_le32toh(x) OSSwapLittleToHostInt32(x)
#define xnz_htobe64(x) OSSwapHostToBigInt64(x)
#define xnz_htole64(x) OSSwapHostToLittleInt64(x)
#define xnz_be64toh(x) OSSwapBigToHostInt64(x)
#define xnz_le64toh(x) OSSwapLittleToHostInt64(x)
#elif defined(__OpenBSD__)
# include <sys/endian.h>
# define xnz_be16toh(x) betoh16(x)
# define xnz_le16toh(x) letoh16(x)
# define xnz_be32toh(x) betoh32(x)
# define xnz_le32toh(x) letoh32(x)
# define xnz_be64toh(x) betoh64(x)
# define xnz_le64toh(x) letoh64(x)
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
# include <sys/endian.h>
# define xnz_be16toh(x) betoh16(x)
# define xnz_le16toh(x) letoh16(x)
# define xnz_be32toh(x) betoh32(x)
# define xnz_le32toh(x) letoh32(x)
# define xnz_be64toh(x) betoh64(x)
# define xnz_le64toh(x) letoh64(x)
#elif defined(__WINDOWS__)
#include <stdlib.h>
//# include <winsock2.h>
//# include <sys/param.h>
//#pragma comment(lib, "Ws2_32.lib")
# if BYTE_ORDER == LITTLE_ENDIAN
/*
# define xnz_htobe16(x) htons(x)
# define xnz_htole16(x) (x)
# define xnz_be16toh(x) ntohs(x)
# define xnz_le16toh(x) (x)
# define xnz_htobe32(x) htonl(x)
# define xnz_htole32(x) (x)
# define xnz_be32toh(x) ntohl(x)
# define xnz_le32toh(x) (x)
# define xnz_htobe64(x) htonll(x)
# define xnz_htole64(x) (x)
# define xnz_be64toh(x) ntohll(x)
# define xnz_le64toh(x) (x)
*/
# define xnz_htobe16(x) _byteswap_ushort(x)
# define xnz_htole16(x) (x)
# define xnz_be16toh(x) _byteswap_ushort(x)
# define xnz_le16toh(x) (x)
# define xnz_htobe32(x) _byteswap_ulong(x)
# define xnz_htole32(x) (x)
# define xnz_be32toh(x) _byteswap_ulong(x)
# define xnz_le32toh(x) (x)
# define xnz_htobe64(x) _byteswap_uint64(x)
# define xnz_htole64(x) (x)
# define xnz_be64toh(x) _byteswap_uint64(x)
# define xnz_le64toh(x) (x)
# elif BYTE_ORDER == BIG_ENDIAN
/* that would be xbox 360 */
# define xnz_htobe16(x) (x)
# define xnz_htole16(x) __builtin_bswap16(x)
# define xnz_be16toh(x) (x)
# define xnz_le16toh(x) __builtin_bswap16(x)
# define xnz_htobe32(x) (x)
# define xnz_htole32(x) __builtin_bswap32(x)
# define xnz_be32toh(x) (x)
# define xnz_le32toh(x) __builtin_bswap32(x)
# define xnz_htobe64(x) (x)
# define xnz_htole64(x) __builtin_bswap64(x)
# define xnz_be64toh(x) (x)
# define xnz_le64toh(x) __builtin_bswap64(x)
# else
# error byte order not supported
# endif
# define __BYTE_ORDER BYTE_ORDER
# define __BIG_ENDIAN BIG_ENDIAN
# define __LITTLE_ENDIAN LITTLE_ENDIAN
# define __PDP_ENDIAN PDP_ENDIAN
#else
//# error platform not supported
#endif
/***
* xnz_types.h
* XNZ
*
* Created by Joe Moulton on 4/1/24
* Copyright � 2024 Abstract Embedded. All rights reserved.
***/
typedef enum XNG_IMAGE_API_STATUS
{
XNG_IMAGE_API_SUCCESS = 0,
XNG_IMAGE_API_INVALID_FILE = 1,
XNG_IMAGE_API_NULL_CONTAINER = 2,
XNG_IMAGE_API_INCOMPATIBLE_FOURCC = 3,
XNG_IMAGE_API_INPUT_FILE_ERROR = 4,
XNG_IMAGE_API_INVALID_FILE_STRUCTURE = 5,
XNG_IMAGE_API_UNSUPPORTED_FORMAT = 6,
XNG_IMAGE_API_UNHANDLED_PIXEL_FORMAT = 7,
XNG_IMAGE_API_UNHANDLED_FOURCC_FORMAT = 8,
XNG_IMAGE_API_UNABLE_TO_READ_DATA = 9,
XNG_IMAGE_API_BUFFER_IS_NULL = 10
}XNG_IMAGE_API_STATUS;
/***
* xnz_bitstream.h
* XNZ
*
* Created by Joe Moulton on 4/1/24
* Copyright � 2024 Abstract Embedded. All rights reserved.
***/
#include <stdint.h>
#include <assert.h>
#ifdef _WIN32
#include <malloc.h>
#else
#include <stdlib.h> /* malloc, free, rand */
#include <alloca.h>
#endif
//BITSTREAM COMPILE OPTIONS
#define XNZ_BITSTREAM_QUEUE 0
//#define XNZ_BITSTREAM_DEBUG
typedef uint32_t xnz_bs_size;
typedef uint32_t xnz_bs_len;
typedef uint32_t xnz_bs_reg;
typedef uint32_t xnz_bs_mask;
//static const N_PREFETCH_BYTES = sizeof(xnz_bs_reg);
//static const N_PREFETCH_BITS = (sizeof(xnz_bs_reg) * 8);
typedef struct xnz_bitstream // 44 or 56 bytes
{
//pointers
uint8_t* buffer; // 8 bytes, stream chunk byte start
uint8_t* pointer; // 8 bytes, stream chunk byte position
#if XNZ_BITSTREAM_QUEUE
uint8_t* next_buffer; // 8 bytes
xnz_bs_size next_capacity; // 4 bytes
#endif
//sizes
xnz_bs_size capacity; // 4 bytes, stream chunk size in bytes
xnz_bs_size length; // 4 bytes, stream chunk size in bits + prev chunk overflow bits
//lengths
xnz_bs_len position; // 4 bytes, stream bit position
xnz_bs_len offset; // 4 bytes, accumulation register position bit position
//registers
xnz_bs_reg prefetch; // 4 bytes
xnz_bs_reg accumulator; // 4 bytes
//bitwise op helpers
xnz_bs_mask mask; // 4 bytes, can be eliminated
}xnz_bitstream;
//prefetch up to 4 bytes from the src buffer
/* V1
#define xnz_bitstream_prefetch(_bs) \
(_bs->prefetch) = 0; \
if (((uint32_t) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 0)) \
(_bs->prefetch) |= (*(_bs->pointer + 0)); \
if (((uint32_t) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 1)) \
(_bs->prefetch) |= (*(_bs->pointer + 1) << 8); \
if (((uint32_t) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 2)) \
(_bs->prefetch) |= (*(_bs->pointer + 2) << 16); \
if (((uint32_t) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 3)) \
(_bs->prefetch) |= (*(_bs->pointer + 3) << 24);
*/
#define xnz_bitstream_prefetch(_bs) \
uint32_t nbytes = (uint32_t)(_bs->pointer - _bs->buffer); \
if ( nbytes < _bs->capacity + 3) _bs->prefetch = (*(_bs->pointer + 0)) | (*(_bs->pointer + 1) << 8) | (*(_bs->pointer + 2) << 16) | (*(_bs->pointer + 3) << 24); \
else if ( nbytes < _bs->capacity + 2) _bs->prefetch = (*(_bs->pointer + 0)) | (*(_bs->pointer + 1) << 8) | (*(_bs->pointer + 2) << 16); \
else if ( nbytes < _bs->capacity + 1) _bs->prefetch = (*(_bs->pointer + 0)) | (*(_bs->pointer + 1) << 8); \
else if ( nbytes < _bs->capacity + 0) _bs->prefetch = (*(_bs->pointer + 0));
/* V3
#define xnz_bitstream_prefetch(_bs) \
uint32_t nbytes = (uint32_t)(_bs->pointer - _bs->buffer); _bs->prefetch = 0; \
if ( nbytes < _bs->capacity + 3) memcpy(&_bs->prefetch, _bs->pointer, 4); \
else if ( nbytes < _bs->capacity + 2) memcpy(&_bs->prefetch, _bs->pointer, 3); \
else if ( nbytes < _bs->capacity + 1) memcpy(&_bs->prefetch, _bs->pointer, 2); \
else if ( nbytes < _bs->capacity + 0) memcpy(&_bs->prefetch, _bs->pointer, 1);
*/
//#define xnz_bitstream_prefetch(_bs) _bs->prefetch = 0; memcpy(&_bs->prefetch, _bs->pointer, MIN(_bs->capacity + 3 - (uint32_t)(_bs->pointer - _bs->buffer), 4));
/*
#define xnz_bitstream_fetch(_bs) do { \
(_bs->accumulator) = 0; \
if (((uint32_t) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 0)) \
(_bs->accumulator) |= (*(_bs->pointer + 0) << 0); \
if (((uint32_t) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 1)) \
(_bs->accumulator) |= (*(_bs->pointer + 1) << 8); \
if (((uint32_t) (_bs->pointer - _bs->buffer)) < (_bs->capacity + 2)) \
(_bs->accumulator) |= (*(_bs->pointer + 2) << 16); \
if (((uint32_t) (_bs->pointer - _bs->buffer)) <(_bs->capacity + 3)) \
(_bs->accumulator) |= (*(_bs->pointer + 3) << 24); \
xnz_bitstream_prefetch(_bs); \
} while(0)
*/
#define xnz_bitstream_shift(_bs, _nbits) \
_bs->accumulator <<= _nbits; \
_bs->position += _nbits; \
_bs->offset += _nbits; \
if (_bs->offset < 32) { \
_bs->mask = ((1 << _nbits) - 1); \
_bs->accumulator |= ((_bs->prefetch) & _bs->mask); \
_bs->prefetch >>= _nbits; \
} else { \
_bs->mask = ((1 << (32 - (_bs->offset - _nbits))) - 1); \
_bs->accumulator |= ((_bs->prefetch) & _bs->mask); \
_bs->prefetch >>= (32 - (_bs->offset - _nbits)); \
_bs->offset -= 32; \
_bs->pointer += 4; \
xnz_bitstream_prefetch(_bs); \
if (_bs->offset) { \
_bs->mask = ((1 << _bs->offset) - 1); \
_bs->accumulator |= ((_bs->prefetch) & _bs->mask) << (32 - ((_bs->offset+32) - _nbits)); \
_bs->prefetch >>= _bs->offset; \
} \
}
#ifdef XNZ_BITSTREAM_DEBUG
void xnz_bitstream_print(xnz_bitstream* bs)
{
fprintf(stderr, "bs->position = %d\n", bs->position);
fprintf(stderr, "bs->offset = %d\n", bs->offset);
fprintf(stderr, "bs->capacity = %d\n", bs->capacity);
fprintf(stderr, "bs->length = %d\n", bs->length);
//fprintf(stderr, "bs->accumulator = %d\n", bs->accumulator);
}
#endif
static void xnz_bitstream_attach_prefetch(xnz_bitstream* bs, uint8_t* buffer, uint32_t capacity)
{
assert(bs->position == 0);
//assert(bs->length - bs->position <= 32);
bs->buffer = buffer;
bs->pointer = bs->buffer;
#if XNZ_BITSTREAM_QUEUE
bs->next_buffer = NULL;
bs->next_capacity = 0;
#endif
bs->position = 0;
bs->capacity = capacity;
bs->length = bs->capacity * 8;
bs->mask = 0;
bs->offset = 0;
bs->accumulator = 0;
bs->prefetch = 0;
//load first four bytes into prefetch register
xnz_bitstream_prefetch(bs);
/*
unsigned* codebits = xnz_convert_to_binary((unsigned*)&(bs->prefetch), 3 * 8);
uint32_t bit; for (bit = 3 * 8; bit--;)
{
if (bit % 8 == 7) printf(" ");
fprintf(stderr, "%u", +codebits[bit]);
}
free(codebits);
printf("\n");
*/
}
static void xnz_bitstream_attach(xnz_bitstream* bs, uint8_t* buffer, uint32_t capacity)
{
#if XNZ_BITSTREAM_QUEUE
bs->next_buffer = NULL;
bs->next_capacity = 0;
if ((int32_t)bs->length - (int32_t)bs->position > 32) //hate the cast here but it's necessary
{
//delay reattach to occur during read bit(s) by "queuing" the next stream buffer
#ifdef XNZ_BITSTREAM_DEBUG
fprintf(stderr, "xnz_bitstream_attach::queue\n");
#endif
bs->next_buffer = buffer;
bs->next_capacity = capacity;
return;
}
#endif
// attach/reattach immediately
{
#ifdef XNZ_BITSTREAM_DEBUG
fprintf(stderr, "xnz_bitstream_attach::immediate\n");
assert(bs->length - bs->position <= 32);
#endif
//assert if bitstream was init'd with bitstream_attach_prefetch then reattach should never be called w position == 0
//bc it indicates that we decoded exactly the amount of bytes we needed and there was no prefetch overlap
//assert(bs->position != 0);
bs->buffer = buffer;
bs->pointer = bs->buffer - 4; //subtract 4 bytes to force prefetch to occur at start of buffer on bitstream_shift
//bs->next_buffer = NULL;
//bs->next_capacity = 0;
bs->offset = 32 - (bs->length - bs->position);
bs->capacity = capacity;
bs->length = (bs->length - bs->position) + bs->capacity * 8;
bs->position = 0;
}
}
static uint8_t xnz_bitstream_read_bit(xnz_bitstream* bs)
{
#if XNZ_BITSTREAM_QUEUE
if (bs->position >= bs->length && bs->next_buffer)
{
//reattach
assert(bs->next_capacity > 0);
xnz_bitstream_attach(bs, bs->next_buffer, bs->capacity);
}
#endif
#ifdef XNZ_BITSTREAM_DEBUG
assert(bs->position < bs->length); //never read beyond max bits in current stream chunk of bits
//this conditional is necessary because of negative reattach offset for automatic prefetech (i suppose i could just add 4 to the assert statement)
if (bs->pointer > bs->buffer) assert(((uint32_t)(bs->pointer - bs->buffer)) <= bs->capacity + 4);
#endif
//read bit by shifiting from prefetch into accumulation register
xnz_bitstream_shift(bs, 1);
return (uint8_t)(bs->accumulator & (uint32_t)0x01);
}
static uint32_t xnz_bitstream_read_bits(xnz_bitstream* bs, uint32_t nbits)
{
#if XNZ_BITSTREAM_QUEUE
if (bs->position >= bs->length && bs->next_buffer)
{
//reattach
assert(bs->next_capacity > 0);
xnz_bitstream_attach(bs, bs->next_buffer, bs->next_capacity);
}
#endif
#ifdef XNZ_BITSTREAM_DEBUG
assert(nbits <= 32); //Ensure we aren't asking for more bits than accumulation register can handle
assert(bs->position < bs->length); //never read beyond max bits in current stream chunk of bits
//this conditional is necessary because of negative reattach offset for automatic prefetech (i suppose i could just add 4 to the assert statement)
if (bs->pointer > bs->buffer) assert(((uint32_t)(bs->pointer - bs->buffer)) <= bs->capacity + 4);
#endif
//read bits by shifiting from prefetch into accumulation register
xnz_bitstream_shift(bs, nbits);
return (bs->accumulator & ((1 << nbits) - 1));
}
/***
* xnz.h
* XNZ
*
* Created by Joe Moulton on 4/1/24
* Copyright � 2024 Abstract Embedded. All rights reserved.
***/
//# pragma mark -- Common Decompression Struct Defines
//INFLATE COMPILE OPTIONS
//#define XNZ_DEBUG
//#define XNZ_HUFF_DEBUG
//#define XNZ_LOG_BLOCKS
typedef enum xnz_status
{
XNZ_STATUS_ERROR = -1,
XNZ_STATUS_OK = 0,
XNZ_STATUS_END = 1
}xnz_status;
typedef enum xnz_stream_operation
{
/* Encode to a compressed stream */
XNZ_STREAM_ENCODE = 0,
/* Decode from a compressed stream */
XNZ_STREAM_DECODE = 1,
} xnz_stream_operation;
/* Bits for the flags in compression_stream_process. */
typedef enum xnz_stream_flags
{
XNZ_STREAM_FINALIZE = 0x0001,
} xnz_stream_flags;
typedef enum xnz_block_type
{
XNZ_BLOCK_RAW = 0x0000, // raw [bytes]
XNZ_BLOCK_FIXED = 0x0001, // fixed [huff codes]
XNZ_BLOCK_DYNAMIC = 0x0001, // dynamic [huff codes]
XNZ_BLOCK_CUSTOM = 0x0001, // custom [compression TBD]
XNZ_BLOCK_CONTINUE = 0x0004, // continue [block decode]
XNZ_BLOCK_MAX = 0x0005
}xnz_block_type;
/*XNZ_DECLSPEC XNZ_ALIGN(16)*/
typedef struct xnz_huff_node //24 bytes
{
uint32_t symbol; // 4 bytes, code sequence
uint32_t len; // 4 bytes, code sequence length in bits, unused
struct xnz_huff_node* left; // 8 bytes
struct xnz_huff_node* right; // 8 bytes
}xnz_huff_node;
//# Compact representation of the length code value(257 - 285), length rangeand number
//# of extra bits to use in LZ77 compression(See Section 3.2.5 of RFC 1951)
/*
length_code_ranges = [
[257, 0, 3, 3], [258, 0, 4, 4], [259, 0, 5, 5], [260, 0, 6, 6], [261, 0, 7, 7],
[262, 0, 8, 8], [263, 0, 9, 9], [264, 0, 10, 10], [265, 1, 11, 12], [266, 1, 13, 14],
[267, 1, 15, 16], [268, 1, 17, 18], [269, 2, 19, 22], [270, 2, 23, 26], [271, 2, 27, 30],
[272, 2, 31, 34], [273, 3, 35, 42], [274, 3, 43, 50], [275, 3, 51, 58], [276, 3, 59, 66],
[277, 4, 67, 82], [278, 4, 83, 98], [279, 4, 99, 114], [280, 4, 115, 130], [281, 5, 131, 162],
[282, 5, 163, 194], [283, 5, 195, 226], [284, 5, 227, 257], [285, 0, 258, 258]
]
//#Construct a lookup table mapping length codes to(num_bits, lower_bound) pairs
length_codes = {}
for code, num_bits, lower_bound, upper_bound in length_code_ranges :
for i in range(lower_bound, upper_bound + 1) :
length_codes[code] = (num_bits, lower_bound)
int code; for (code = 0; code < 286; code++)
{
}
//# Compact representation of the distance code value(0 - 31), distance range and number
//# of extra bits to use in LZ77 compression(See Section 3.2.5 of RFC 1951)
distance_code_ranges = [
[0, 0, 1, 1], [1, 0, 2, 2], [2, 0, 3, 3], [3, 0, 4, 4], [4, 1, 5, 6],
[5, 1, 7, 8], [6, 2, 9, 12], [7, 2, 13, 16], [8, 3, 17, 24], [9, 3, 25, 32],
[10, 4, 33, 48], [11, 4, 49, 64], [12, 5, 65, 96], [13, 5, 97, 128], [14, 6, 129, 192],
[15, 6, 193, 256], [16, 7, 257, 384], [17, 7, 385, 512], [18, 8, 513, 768], [19, 8, 769, 1024],
[20, 9, 1025, 1536], [21, 9, 1537, 2048], [22, 10, 2049, 3072], [23, 10, 3073, 4096], [24, 11, 4097, 6144],
[25, 11, 6145, 8192], [26, 12, 8193, 12288], [27, 12, 12289, 16384], [28, 13, 16385, 24576], [29, 13, 24577, 32768],
]
//#Construct a lookup table mapping distance codes to(num_bits, lower_bound) pairs
distance_codes = {}
for code, num_bits, lower_bound, upper_bound in distance_code_ranges :
for i in range(lower_bound, upper_bound + 1) :
distance_codes[code] = (num_bits, lower_bound)
*/
#define N_FIXED_LL_CODES 288
#define N_FIXED_DIST_CODES 32
#define N_DYNAMIC_CL_CODES 19
//binary [huffman code] trees required num_codes * 2 - 1 (because the root node is only needed once)
#define N_FIXED_LL_NODES ((N_FIXED_LL_CODES * 2) - 1)
#define N_FIXED_DIST_NODES ((N_FIXED_DIST_CODES * 2) - 1)
#define N_DYNAMIC_CL_NODES ((N_DYNAMIC_CL_CODES * 2) - 1)
#define XNZ_DEFLATE_BUFFER_SIZE 32768
//Fixed length code offsets and ranges as defined by Katz are used only with length symbols (ie values > 256) in the literal-length code table
static xnz_uint2 RFC_1951_FIXED_LENGTH_CODES[29] =
{
//{0, XXX} // 256 (terminating symbol)
{0, 3}, // 257
{0, 4}, // 258
{0, 5}, // 259
{0, 6}, // 260
{0, 7}, // 261
{0, 8}, // 262
{0, 9}, // 263
{0, 10}, // 264
{1, 11}, // 265
{1, 13}, // 266
{1, 15}, // 267
{1, 17}, // 268
{2, 19}, // 269
{2, 23}, // 270
{2, 27}, // 271
{2, 31}, // 272
{3, 35}, // 273
{3, 43}, // 274
{3, 51}, // 275
{3, 59}, // 276
{4, 67}, // 277
{4, 83}, // 278
{4, 99}, // 279
{4, 115}, // 280
{5, 131}, // 281
{5, 163}, // 282
{5, 195}, // 283
{5, 227}, // 284
{0, 258} // 285
//{0, XXX} // 286
//{0, XXX} // 287
};
//Fixed distance code offsets and ranges as defined by Katz are given for 30/32 possible dist symbols (2 unused/reserved beyond 32768 range)
static xnz_uint2 RFC_1951_FIXED_DIST_CODES[30] =
{
{0, 1}, // 0
{0, 2}, // 1
{0, 3}, // 2
{0, 4}, // 3
{1, 5}, // 4
{1, 7}, // 5
{2, 9}, // 6
{2, 13}, // 7
{3, 17}, // 8
{3, 25}, // 9
{4, 33}, // 10
{4, 49}, // 11
{5, 65}, // 12
{5, 97}, // 13
{6, 129}, // 14
{6, 193}, // 15
{7, 257}, // 16
{7, 385}, // 17
{8, 513}, // 18
{8, 769}, // 19
{9, 1025}, // 20
{9, 1537}, // 21
{10, 2049}, // 22
{10, 3073}, // 23
{11, 4097}, // 24
{11, 6145}, // 25
{12, 8193}, // 26
{12, 12289}, // 27
{13, 16385}, // 28
{13, 24577} // 29
//{14, XXX} // 39
//{14, XXX} // 31
};
/*
typedef struct
{
You are partially responsible for management of the dst_ptr,
dst_size, src_ptr, and src_size fields. You must initialize
them to describe valid memory buffers before making a call to
compression_stream_process. compression_stream_process will update
these fields before returning to account for the bytes of the src
and dst buffers that were successfully processed.
uint8_t * dst_ptr;
size_t dst_size;
const uint8_t * src_ptr;
size_t src_size;
//The stream state object is managed by the compression_stream functions.
//You should not ever directly access this field.
void * __nullable state;
} compression_stream;
*/
//This struct needs more abstraction to make it decompression algorithm agnostic
//(ie the fixed sizes of its tree structures and history buffer are for RFC 1951 only)
typedef struct xnz_block_stream //96 bytes + DEFLATE trees/history
{
//public
uint8_t* src_ptr; // 8 bytes
uint8_t* dst_ptr; // 8 bytes
union
{
uint8_t* history; // 8 bytes
uint8_t* scratch;
};
//Sizes
size_t src_size; // 8 bytes
size_t dst_size; // 8 bytes
//private state
size_t nHistoryBytes; // 8 bytes, total decompressed stream bytes
size_t nBlockBytesDecoded; // 8 bytes, total decompressed block bytes per decode_block call
//Lengths
uint32_t nBlocksProcessed; // 4 bytes, total compression blocks processed
//"base" class
xnz_bitstream bs; // 40 or 52 bytes
/********************************* RFC 1951 DEFLATE *********************************/
//allocating circular history buffer of decoded bytes is now client responsibility
//as it is too large for the stack and also algorithm dependent
//uint8_t history[HISTORY_BUFFER_SIZE]; // 32768 bytes
//RFC 1951 Algorithm Dependent Prefix Code Trees
//dist tree defined first bc it is a multiple of 2^5 to maintain struct property alignment
xnz_huff_node dist_tree[ N_FIXED_DIST_NODES ]; // 24 * 32 bytes
xnz_huff_node ll_tree[ N_FIXED_LL_NODES ]; // 24 * 288 bytes
//xnz_huff_node cl_tree[ N_DYNAMIC_CL_NODES ]; // 24 * 19 bytes
//track dynamic streaming cl tree state
uint32_t dist_code_lengths[N_FIXED_DIST_CODES]; //store dist lengths decoded from cl tree
uint32_t ll_code_lengths[N_FIXED_LL_CODES]; //store ll lengths decoded from cl treee
//track prefix code unpack state for fixed and dynamic block types
xnz_huff_node* ll_node;
xnz_huff_node* dist_node;
//track dynamic block cl_code lengths read + cl_codes read
uint32_t lengths_read; //# cl code lengths read at the beginning of block
uint32_t codes_read; //# ll/dist lengths decoded from the cl tree
uint32_t last_symbol; //the last cl symbol parsed when parsing the cl tree
//uint32_t clBitCount; //# of bits parsed for all symbols in the cl tree (debug only)
//These are no longer in use
uint32_t max_cl_code_length;
uint32_t max_ll_code_length; // 4 bytes, when decoding streamed incomplete buffers these properties ensure
uint32_t max_dist_code_length; // 4 bytes, the bitstream will be prevented from reading partial prefix codes
//track dynamic block first 14 bytes
uint32_t hlit;
uint32_t hdist;
uint32_t hclen;
//track current block type
uint8_t bfinal;
uint8_t btype;
}xnz_block_stream;
typedef xnz_block_stream xnz_compression_stream;
//#pragma mark -- CTConnection API Method Function Pointer Definitions
typedef xnz_status (*xnz_block_stream_func)(xnz_block_stream* stream, xnz_bitstream* bs, uint32_t flags);
//forward declare xnz inflate block functions
static xnz_status xnz_inflate_raw_block(xnz_block_stream* stream, xnz_bitstream* bs, uint32_t flags);
static xnz_status xnz_inflate_fixed_block(xnz_block_stream* stream, xnz_bitstream* bs, uint32_t flags);
static xnz_status xnz_inflate_dynamic_block(xnz_block_stream* stream, xnz_bitstream* bs, uint32_t flags);
static xnz_status xnz_inflate_custom_block(xnz_block_stream* stream, xnz_bitstream* bs, uint32_t flags);
static xnz_status xnz_inflate_prefix_codes(xnz_block_stream* stream, xnz_bitstream* bs, uint32_t flags);
static const xnz_block_stream_func XNZ_INFLATE_BLOCKS[XNZ_BLOCK_MAX] =
{
xnz_inflate_raw_block,
xnz_inflate_fixed_block,
xnz_inflate_dynamic_block,
xnz_inflate_custom_block,
xnz_inflate_prefix_codes
};
//FYI: client must free returned memory
static unsigned* xnz_convert_to_binary(unsigned* data, unsigned int dataSizeInBits)
{