forked from mafemergency/byond-tracy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prof.c
3353 lines (2844 loc) · 107 KB
/
prof.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
/* c11 minimum */
#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 201112L)
# pragma message("minimum supported c standard is c11")
#endif
#if defined(__cplusplus)
# pragma message("compiling as c++ is ill-advised")
#endif
/* platform identification */
#if defined(_WIN32)
# define UTRACY_WINDOWS
# if defined(_WIN64)
# error 64 bit not supported
# endif
# if !defined(_WIN32_WINNT)
# define _WIN32_WINNT 0x0601
# endif
# if !defined(WINVER)
# define WINVER 0x0601
# endif
#elif defined(__linux__)
# define UTRACY_LINUX
# if defined(__x86_64__)
# error 64 bit not supported
# endif
#else
# error platform not detected
#endif
/* compiler identification */
#if defined(__clang__)
# define UTRACY_CLANG
#elif defined(__GNUC__)
# define UTRACY_GCC
#elif defined(_MSC_VER)
# define UTRACY_MSVC
#else
# error compiler not detected
#endif
#if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define likely(expr) __builtin_expect(((expr) != 0), 1)
# define unlikely(expr) __builtin_expect(((expr) != 0), 0)
#else
# define likely(expr) (expr)
# define unlikely(expr) (expr)
#endif
#define UTRACY_ALIGN_DOWN(ptr, size) ((void *) ((uintptr_t) (ptr) & (~((size) - 1))))
/* data type size check */
_Static_assert(sizeof(void *) == 4, "incorrect size");
_Static_assert(sizeof(int) == 4, "incorrect size");
_Static_assert(sizeof(long long) == 8, "incorrect size");
/* linkage and exports */
#if defined(UTRACY_WINDOWS)
# if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define UTRACY_INTERNAL static
# define UTRACY_EXTERNAL __attribute__((visibility("default"))) __attribute__((dllexport))
# define UTRACY_INLINE inline __attribute__((always_inline))
# elif defined(UTRACY_MSVC)
# define UTRACY_INTERNAL static
# define UTRACY_EXTERNAL __declspec(dllexport)
# define UTRACY_INLINE inline __forceinline
# endif
#elif defined(UTRACY_LINUX)
# if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define UTRACY_INTERNAL static
# define UTRACY_EXTERNAL __attribute__((visibility("default")))
# define UTRACY_INLINE inline __attribute__((always_inline))
# endif
#endif
/* calling conventions */
#if defined(UTRACY_WINDOWS)
# define UTRACY_WINDOWS_CDECL __cdecl
# define UTRACY_WINDOWS_STDCALL __stdcall
# define UTRACY_WINDOWS_THISCALL __thiscall
# define UTRACY_LINUX_CDECL
# define UTRACY_LINUX_STDCALL
# define UTRACY_LINUX_THISCALL
# define UTRACY_LINUX_REGPARM(a)
#elif defined(UTRACY_LINUX)
# define UTRACY_WINDOWS_CDECL
# define UTRACY_WINDOWS_STDCALL
# define UTRACY_WINDOWS_THISCALL
# define UTRACY_LINUX_CDECL __attribute__((cdecl))
# define UTRACY_LINUX_STDCALL __attribute__((stdcall))
# define UTRACY_LINUX_THISCALL __attribute__((thiscall))
# define UTRACY_LINUX_REGPARM(a) __attribute__((regparm(a)))
#endif
/* headers */
#if defined(UTRACY_WINDOWS)
# define NOMINMAX
# include <winsock2.h>
# include <ws2tcpip.h>
# include <windows.h>
#elif defined(UTRACY_LINUX)
# define _GNU_SOURCE
# include <errno.h>
# include <unistd.h>
# include <time.h>
# include <sys/syscall.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/mman.h>
# include <netdb.h>
# include <pthread.h>
# include <dlfcn.h>
# include <link.h>
# include <netinet/ip.h>
# include <arpa/inet.h>
# include <poll.h>
/* avoid including stdlib.h */
char *getenv(char const *);
#endif
#include <stddef.h>
#include <assert.h>
#if (__STDC_HOSTED__ == 1)
# include <stdlib.h>
# include <string.h>
#endif
#if !defined(__STDC_NO_ATOMICS__)
# include <stdatomic.h>
#endif
#if (__STDC_HOSTED__ == 0)
void *memset(void *const a, int value, size_t len) {
for(size_t i=0; i<len; i++) {
*((char *) a + i) = value;
}
return a;
}
void *memcpy(void *const restrict dst, void const *const restrict src, size_t len) {
for(size_t i=0; i<len; i++) {
*((char *) dst + i) = *((char *) src + i);
}
return dst;
}
int memcmp(void const *a, void const *b, size_t len) {
for(size_t i=0; i<len; i++) {
char unsigned _a = *(char unsigned *) a;
char unsigned _b = *(char unsigned *) b;
if(_a != _b) {
return (_a - _b);
}
}
return 0;
}
size_t strlen(char const *const a) {
size_t len = 0;
for(char const *p=a; *p; p++) {
len++;
}
return len;
}
#endif
#if defined(max)
# undef max
#endif
#if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define max(a, b) ({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
#elif defined(UTRACY_MSVC)
# define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
#if defined(min)
# undef min
#endif
#if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define min(a, b) ({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
#elif defined(UTRACY_MSVC)
# define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
#if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# if __has_builtin(__builtin_memcpy)
# define UTRACY_MEMCPY __builtin_memcpy
# else
# define UTRACY_MEMCPY memcpy
# endif
# if __has_builtin(__builtin_memset)
# define UTRACY_MEMSET __builtin_memset
# else
# define UTRACY_MEMSET memset
# endif
# if __has_builtin(__builtin_memcmp)
# define UTRACY_MEMCMP __builtin_memcmp
# else
# define UTRACY_MEMCMP memcmp
# endif
#else
# define UTRACY_MEMCPY memcpy
# define UTRACY_MEMSET memset
# define UTRACY_MEMCMP memcmp
#endif
/* debugging */
#if defined(UTRACY_DEBUG) || defined(DEBUG)
# include <stdio.h>
# define LOG_DEBUG_ERROR fprintf(stderr, "err: %s %s:%d\n", __func__, __FILE__, __LINE__)
# define LOG_INFO(...) fprintf(stdout, __VA_ARGS__)
#else
# define LOG_DEBUG_ERROR
# define LOG_INFO(...)
#endif
/* config */
#define UTRACY_L1_LINE_SIZE (64)
#define UTRACY_PAGE_SIZE (4096)
#define EVENT_QUEUE_CAPACITY (1u << 18u)
_Static_assert((EVENT_QUEUE_CAPACITY & (EVENT_QUEUE_CAPACITY - 1)) == 0, "EVENT_QUEUE_CAPACITY must be a power of 2");
#define UTRACY_LATENCY (100000000ll)
#define UTRACY_MAX_FRAME_SIZE (256u * 1024u)
/* byond types */
struct object {
union {
int unsigned padding;
char unsigned type;
};
union {
int unsigned i;
float f;
};
};
struct string {
char *data;
int unsigned id;
struct string *left;
struct string *right;
int unsigned refcount;
int unsigned unk0;
int unsigned len;
};
struct procdef {
int unsigned path;
int unsigned name;
int unsigned desc;
int unsigned category;
int unsigned flags;
int unsigned unk0;
int unsigned bytecode;
int unsigned locals;
int unsigned parameters;
};
struct misc {
struct {
short unsigned len;
int unsigned unk0;
int unsigned *bytecode;
} bytecode;
struct {
short unsigned len;
int unsigned unk0;
int unsigned *locals;
} locals;
struct {
short unsigned len;
int unsigned unk0;
int unsigned *params;
} params;
};
struct proc {
int unsigned procdef;
char unsigned flags;
char unsigned supers;
short unsigned unused;
struct object usr;
struct object src;
struct execution_context *ctx;
int unsigned sequence;
void (*callback)(struct object, int unsigned);
int unsigned callback_arg;
int unsigned argc;
struct object *argv;
int unsigned unk0;
};
/* byond type size check */
_Static_assert(sizeof(struct object) == 8, "incorrect size");
_Static_assert(sizeof(struct string) == 28, "incorrect size");
_Static_assert(sizeof(struct procdef) >= 4, "incorrect size");
_Static_assert(sizeof(struct misc) == 36, "incorrect size");
_Static_assert(sizeof(struct proc) >= 4, "incorrect size");
/*
* LZ4 - Fast LZ compression algorithm
* Header File
* Copyright (C) 2011-2020, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at :
- LZ4 homepage : http://www.lz4.org
- LZ4 source repository : https://github.com/lz4/lz4
*/
#define LZ4_MEMORY_USAGE_MIN 10
//#define LZ4_MEMORY_USAGE_DEFAULT 14
#define LZ4_MEMORY_USAGE_DEFAULT 20
#define LZ4_MEMORY_USAGE_MAX 20
#if !defined(LZ4_MEMORY_USAGE)
# define LZ4_MEMORY_USAGE LZ4_MEMORY_USAGE_DEFAULT
#endif
#if (LZ4_MEMORY_USAGE < LZ4_MEMORY_USAGE_MIN)
# error "LZ4_MEMORY_USAGE is too small !"
#endif
#if (LZ4_MEMORY_USAGE > LZ4_MEMORY_USAGE_MAX)
# error "LZ4_MEMORY_USAGE is too large !"
#endif
#define LZ4_MAX_INPUT_SIZE 0x7E000000
#define LZ4_COMPRESSBOUND(isize) ((unsigned) (isize) > (unsigned) LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize) / 255) + 16)
#define LZ4_ACCELERATION_DEFAULT 1
#define LZ4_ACCELERATION_MAX 65537
#if !defined(LZ4_DISTANCE_MAX)
# define LZ4_DISTANCE_MAX 65535
#endif
#define LZ4_DISTANCE_ABSOLUTE_MAX 65535
#if (LZ4_DISTANCE_MAX > LZ4_DISTANCE_ABSOLUTE_MAX)
# error "LZ4_DISTANCE_MAX is too big : must be <= 65535"
#endif
#define ML_BITS 4u
#define ML_MASK ((1U << ML_BITS) - 1u)
#define RUN_BITS (8u - ML_BITS)
#define RUN_MASK ((1U << RUN_BITS) - 1u)
#define MINMATCH 4
#define WILDCOPYLENGTH 8
#define LASTLITERALS 5
#define MFLIMIT 12
#define MATCH_SAFEGUARD_DISTANCE ((2 * WILDCOPYLENGTH) - MINMATCH)
#define FASTLOOP_SAFE_DISTANCE 64
#define LZ4_minLength (MFLIMIT + 1)
#define LZ4_64Klimit (65536 + (MFLIMIT - 1))
#define LZ4_skipTrigger ((LZ4_u32) 6u)
#define LZ4_HASHLOG (LZ4_MEMORY_USAGE - 2)
#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG)
#define LZ4_STREAMSIZE ((1ul << LZ4_MEMORY_USAGE) + 32u)
#define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void *))
#define LZ4_STATIC_ASSERT(a) _Static_assert((a), "")
typedef char signed LZ4_i8;
typedef char unsigned LZ4_byte;
typedef short unsigned LZ4_u16;
typedef int unsigned LZ4_u32;
typedef long long unsigned LZ4_u64;
typedef union LZ4_stream_u LZ4_stream_t;
typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
typedef size_t reg_t;
typedef enum {
LZ4_clearedTable = 0,
LZ4_byPtr,
LZ4_byU32,
LZ4_byU16
} LZ4_tableType_t;
typedef enum {
LZ4_notLimited = 0,
LZ4_limitedOutput = 1,
LZ4_fillOutput = 2
} LZ4_limitedOutput_directive;
typedef enum {
LZ4_noDict = 0,
LZ4_withPrefix64k,
LZ4_usingExtDict,
LZ4_usingDictCtx
} LZ4_dict_directive;
typedef enum {
LZ4_noDictIssue = 0,
LZ4_dictSmall
} LZ4_dictIssue_directive;
struct LZ4_stream_t_internal {
LZ4_u32 hashTable[LZ4_HASH_SIZE_U32];
LZ4_u32 currentOffset;
LZ4_u32 tableType;
LZ4_byte const *dictionary;
LZ4_stream_t_internal const *dictCtx;
LZ4_u32 dictSize;
};
union LZ4_stream_u {
void *table[LZ4_STREAMSIZE_VOIDP];
LZ4_stream_t_internal internal_donotuse;
};
#pragma pack(push, 1)
typedef union {
LZ4_u16 u16;
LZ4_u32 u32;
reg_t uArch;
} LZ4_unalign;
#pragma pack(pop)
UTRACY_INTERNAL UTRACY_INLINE
LZ4_u16 LZ4_read16(void const *ptr) {
return ((LZ4_unalign const *) ptr)->u16;
}
UTRACY_INTERNAL UTRACY_INLINE
LZ4_u32 LZ4_read32(void const *ptr) {
return ((LZ4_unalign const *) ptr)->u32;
}
UTRACY_INTERNAL UTRACY_INLINE
reg_t LZ4_read_ARCH(void const *ptr) {
return ((LZ4_unalign const *) ptr)->uArch;
}
UTRACY_INTERNAL UTRACY_INLINE
void LZ4_write16(void *memPtr, LZ4_u16 value) {
((LZ4_unalign *) memPtr)->u16 = value;
}
UTRACY_INTERNAL UTRACY_INLINE
void LZ4_write32(void *memPtr, LZ4_u32 value) {
((LZ4_unalign *) memPtr)->u32 = value;
}
#define LZ4_writeLE16 LZ4_write16
UTRACY_INTERNAL UTRACY_INLINE
void LZ4_wildCopy8(void *dstPtr, const void *srcPtr, void *dstEnd) {
LZ4_byte *d = (LZ4_byte *) dstPtr;
LZ4_byte const *s = (LZ4_byte const *) srcPtr;
LZ4_byte *const e = (LZ4_byte *) dstEnd;
do {
(void) UTRACY_MEMCPY(d, s, 8);
d += 8;
s += 8;
} while (d < e);
}
UTRACY_INTERNAL UTRACY_INLINE
unsigned LZ4_NbCommonBytes(reg_t val) {
assert(val != 0);
#if defined(UTRACY_MSVC)
long unsigned r;
_BitScanForward(&r, (LZ4_u32) val);
return (unsigned) r >> 3u;
#elif __has_builtin(__builtin_ctx)
return (unsigned) __builtin_ctz((LZ4_u32) val) >> 3u;
#else
LZ4_u32 const m = 0x01010101u;
return (unsigned) ((((val - 1) ^ val) & (m - 1)) * m) >> 24u;
#endif
}
UTRACY_INTERNAL UTRACY_INLINE
unsigned LZ4_count(LZ4_byte const *pIn, LZ4_byte const *pMatch, LZ4_byte const *pInLimit) {
LZ4_byte const *const pStart = pIn;
if(likely(pIn < pInLimit - (sizeof(reg_t) - 1))) {
reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
if(!diff) {
pIn += sizeof(reg_t);
pMatch += sizeof(reg_t);
} else {
return LZ4_NbCommonBytes(diff);
}
}
while(likely(pIn < pInLimit - (sizeof(reg_t) - 1))) {
reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
if(!diff) {
pIn += sizeof(reg_t);
pMatch += sizeof(reg_t);
continue;
}
pIn += LZ4_NbCommonBytes(diff);
return (unsigned) (pIn - pStart);
}
if((sizeof(reg_t) == 8) && (pIn < (pInLimit - 3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) {
pIn += 4;
pMatch += 4;
}
if((pIn < (pInLimit - 1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) {
pIn += 2;
pMatch += 2;
}
if((pIn < pInLimit) && (*pMatch == *pIn)) {
pIn++;
}
return (unsigned)(pIn - pStart);
}
UTRACY_INTERNAL UTRACY_INLINE
LZ4_u32 LZ4_hash4(LZ4_u32 sequence, LZ4_tableType_t const tableType) {
if(tableType == LZ4_byU16) {
return ((sequence * 2654435761u) >> ((MINMATCH * 8) - (LZ4_HASHLOG + 1)));
} else {
return ((sequence * 2654435761u) >> ((MINMATCH * 8) - LZ4_HASHLOG));
}
}
UTRACY_INTERNAL UTRACY_INLINE
LZ4_u32 LZ4_hash5(LZ4_u64 sequence, LZ4_tableType_t const tableType) {
LZ4_u32 const hashLog = (tableType == LZ4_byU16) ? LZ4_HASHLOG + 1 : LZ4_HASHLOG;
LZ4_u64 const prime5bytes = 889523592379llu;
return (LZ4_u32) (((sequence << 24) * prime5bytes) >> (64 - hashLog));
}
UTRACY_INTERNAL UTRACY_INLINE
LZ4_u32 LZ4_hashPosition(void const *const p, LZ4_tableType_t const tableType) {
if((sizeof(reg_t) == 8) && (tableType != LZ4_byU16)) return LZ4_hash5(LZ4_read_ARCH(p), tableType);
return LZ4_hash4(LZ4_read32(p), tableType);
}
UTRACY_INTERNAL UTRACY_INLINE
void LZ4_clearHash(LZ4_u32 h, void *tableBase, LZ4_tableType_t const tableType) {
switch(tableType) {
default:
case LZ4_clearedTable: {assert(0); return;}
case LZ4_byPtr: {LZ4_byte const **hashTable = (LZ4_byte const **) tableBase; hashTable[h] = NULL; return;}
case LZ4_byU32: {LZ4_u32 *hashTable = (LZ4_u32 *) tableBase; hashTable[h] = 0; return;}
case LZ4_byU16: {LZ4_u16 *hashTable = (LZ4_u16 *) tableBase; hashTable[h] = 0; return;}
}
}
UTRACY_INTERNAL UTRACY_INLINE
void LZ4_putIndexOnHash(LZ4_u32 idx, LZ4_u32 h, void *tableBase, LZ4_tableType_t const tableType) {
switch(tableType) {
default:
case LZ4_clearedTable:
case LZ4_byPtr: {assert(0); return;}
case LZ4_byU32: {LZ4_u32 *hashTable = (LZ4_u32 *) tableBase; hashTable[h] = idx; return;}
case LZ4_byU16: {LZ4_u16 *hashTable = (LZ4_u16 *) tableBase; assert(idx < 65536u); hashTable[h] = (LZ4_u16) idx; return;}
}
}
UTRACY_INTERNAL UTRACY_INLINE
void LZ4_putPositionOnHash(LZ4_byte const *p, LZ4_u32 h, void *tableBase, LZ4_tableType_t const tableType, LZ4_byte const *srcBase) {
switch(tableType) {
case LZ4_clearedTable: {assert(0); return;}
case LZ4_byPtr: {LZ4_byte const **hashTable = (LZ4_byte const **) tableBase; hashTable[h] = p; return;}
case LZ4_byU32: {LZ4_u32 *hashTable = (LZ4_u32 *) tableBase; hashTable[h] = (LZ4_u32) (p - srcBase); return;}
case LZ4_byU16: {LZ4_u16 *hashTable = (LZ4_u16 *) tableBase; hashTable[h] = (LZ4_u16) (p - srcBase); return;}
}
}
UTRACY_INTERNAL UTRACY_INLINE
void LZ4_putPosition(LZ4_byte const *p, void *tableBase, LZ4_tableType_t tableType, LZ4_byte const *srcBase) {
LZ4_u32 const h = LZ4_hashPosition(p, tableType);
LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
}
UTRACY_INTERNAL UTRACY_INLINE
LZ4_u32 LZ4_getIndexOnHash(LZ4_u32 h, void const *tableBase, LZ4_tableType_t tableType) {
LZ4_STATIC_ASSERT(LZ4_MEMORY_USAGE > 2);
if(tableType == LZ4_byU32) {
const LZ4_u32 *const hashTable = (const LZ4_u32 *) tableBase;
assert(h < (1U << (LZ4_MEMORY_USAGE - 2)));
return hashTable[h];
}
if(tableType == LZ4_byU16) {
const LZ4_u16 *const hashTable = (const LZ4_u16 *) tableBase;
assert(h < (1U << (LZ4_MEMORY_USAGE - 1)));
return hashTable[h];
}
assert(0);
return 0;
}
UTRACY_INTERNAL UTRACY_INLINE
LZ4_byte const *LZ4_getPositionOnHash(LZ4_u32 h, void const *tableBase, LZ4_tableType_t tableType, LZ4_byte const *srcBase) {
if(tableType == LZ4_byPtr) {LZ4_byte const *const *hashTable = (LZ4_byte const *const *) tableBase; return hashTable[h];}
if(tableType == LZ4_byU32) {LZ4_u32 const *const hashTable = (LZ4_u32 const *) tableBase; return hashTable[h] + srcBase;}
{
LZ4_u16 const *const hashTable = (LZ4_u16 const *) tableBase;
return hashTable[h] + srcBase;
}
}
UTRACY_INTERNAL UTRACY_INLINE
LZ4_byte const *LZ4_getPosition(LZ4_byte const *p, void const *tableBase, LZ4_tableType_t tableType, LZ4_byte const *srcBase) {
LZ4_u32 const h = LZ4_hashPosition(p, tableType);
return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
}
UTRACY_INTERNAL UTRACY_INLINE
void LZ4_prepareTable(LZ4_stream_t_internal *const cctx, int const inputSize, LZ4_tableType_t const tableType) {
if((LZ4_tableType_t) cctx->tableType != LZ4_clearedTable) {
assert(inputSize >= 0);
if(
(LZ4_tableType_t) cctx->tableType != tableType ||
((tableType == LZ4_byU16) && cctx->currentOffset + (unsigned) inputSize >= 0xFFFFU) ||
((tableType == LZ4_byU32) && cctx->currentOffset > 1073741824u) ||
tableType == LZ4_byPtr ||
inputSize >= 4096
) {
(void) UTRACY_MEMSET(cctx->hashTable, 0, LZ4_HASHTABLESIZE);
cctx->currentOffset = 0;
cctx->tableType = (LZ4_u32) LZ4_clearedTable;
}
}
if(cctx->currentOffset != 0 && tableType == LZ4_byU32) {
cctx->currentOffset += 65536u;
}
cctx->dictCtx = NULL;
cctx->dictionary = NULL;
cctx->dictSize = 0;
}
UTRACY_INTERNAL UTRACY_INLINE
void LZ4_renormDictT(LZ4_stream_t_internal *LZ4_dict, int nextSize) {
assert(nextSize >= 0);
if(LZ4_dict->currentOffset + (unsigned) nextSize > 0x80000000u) {
LZ4_u32 const delta = LZ4_dict->currentOffset - 65536u;
LZ4_byte const *dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
for(int i=0; i<LZ4_HASH_SIZE_U32; i++) {
if(LZ4_dict->hashTable[i] < delta) LZ4_dict->hashTable[i] = 0;
else LZ4_dict->hashTable[i] -= delta;
}
LZ4_dict->currentOffset = 65536u;
if(LZ4_dict->dictSize > 65536u) LZ4_dict->dictSize = 65536u;
LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
}
}
UTRACY_INTERNAL UTRACY_INLINE
int LZ4_compress_generic_validated(LZ4_stream_t_internal *const cctx, char const *const source, char *const dest, int const inputSize, int *inputConsumed, int const maxOutputSize, LZ4_limitedOutput_directive const outputDirective, LZ4_tableType_t const tableType, LZ4_dict_directive const dictDirective, LZ4_dictIssue_directive const dictIssue, int const acceleration) {
int result;
LZ4_byte const *ip = (LZ4_byte const *) source;
LZ4_u32 const startIndex = cctx->currentOffset;
LZ4_byte const *base = (LZ4_byte const *) source - startIndex;
LZ4_byte const *lowLimit;
LZ4_stream_t_internal const *dictCtx = (LZ4_stream_t_internal const *) cctx->dictCtx;
LZ4_byte const *const dictionary = dictDirective == LZ4_usingDictCtx ? dictCtx->dictionary : cctx->dictionary;
LZ4_u32 const dictSize = dictDirective == LZ4_usingDictCtx ? dictCtx->dictSize : cctx->dictSize;
LZ4_u32 const dictDelta = (dictDirective == LZ4_usingDictCtx) ? startIndex - dictCtx->currentOffset : 0;
int const maybe_extMem = (dictDirective == LZ4_usingExtDict) || (dictDirective == LZ4_usingDictCtx);
LZ4_u32 const prefixIdxLimit = startIndex - dictSize;
LZ4_byte const *const dictEnd = dictionary ? dictionary + dictSize : dictionary;
LZ4_byte const *anchor = (LZ4_byte const *) source;
LZ4_byte const *const iend = ip + inputSize;
LZ4_byte const *const mflimitPlusOne = iend - MFLIMIT + 1;
LZ4_byte const *const matchlimit = iend - LASTLITERALS;
LZ4_byte const *dictBase = (dictionary == NULL) ? NULL : (dictDirective == LZ4_usingDictCtx) ? dictionary + dictSize - dictCtx->currentOffset : dictionary + dictSize - startIndex;
LZ4_byte *op = (LZ4_byte *) dest;
LZ4_byte *const olimit = op + maxOutputSize;
LZ4_u32 offset = 0;
LZ4_u32 forwardH;
assert(ip != NULL);
if(outputDirective == LZ4_fillOutput && maxOutputSize < 1) {
return 0;
}
if((tableType == LZ4_byU16) && (inputSize >= LZ4_64Klimit)) {
return 0;
}
if(tableType == LZ4_byPtr) assert(dictDirective == LZ4_noDict);
assert(acceleration >= 1);
lowLimit = (LZ4_byte const *) source - (dictDirective == LZ4_withPrefix64k ? dictSize : 0);
if(dictDirective == LZ4_usingDictCtx) {
cctx->dictCtx = NULL;
cctx->dictSize = (LZ4_u32) inputSize;
} else {
cctx->dictSize += (LZ4_u32) inputSize;
}
cctx->currentOffset += (LZ4_u32) inputSize;
cctx->tableType = (LZ4_u32) tableType;
if(inputSize < LZ4_minLength) goto _last_literals;
LZ4_putPosition(ip, cctx->hashTable, tableType, base);
ip++;
forwardH = LZ4_hashPosition(ip, tableType);
for(;;) {
LZ4_byte const *match;
LZ4_byte *token;
LZ4_byte const *filledIp;
if(tableType == LZ4_byPtr) {
LZ4_byte const *forwardIp = ip;
int step = 1;
int searchMatchNb = acceleration << LZ4_skipTrigger;
do {
LZ4_u32 const h = forwardH;
ip = forwardIp;
forwardIp += step;
step = (searchMatchNb++ >> LZ4_skipTrigger);
if(unlikely(forwardIp > mflimitPlusOne)) goto _last_literals;
assert(ip < mflimitPlusOne);
match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType, base);
forwardH = LZ4_hashPosition(forwardIp, tableType);
LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType, base);
} while((match + LZ4_DISTANCE_MAX < ip) || (LZ4_read32(match) != LZ4_read32(ip)));
} else {
LZ4_byte const *forwardIp = ip;
int step = 1;
int searchMatchNb = acceleration << LZ4_skipTrigger;
do {
LZ4_u32 const h = forwardH;
LZ4_u32 const current = (LZ4_u32) (forwardIp - base);
LZ4_u32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType);
assert(matchIndex <= current);
assert(forwardIp - base < (ptrdiff_t) (2147483648u - 1u));
ip = forwardIp;
forwardIp += step;
step = (searchMatchNb++ >> LZ4_skipTrigger);
if(unlikely(forwardIp > mflimitPlusOne)) goto _last_literals;
assert(ip < mflimitPlusOne);
if(dictDirective == LZ4_usingDictCtx) {
if(matchIndex < startIndex) {
assert(tableType == LZ4_byU32);
matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, LZ4_byU32);
match = dictBase + matchIndex;
matchIndex += dictDelta;
lowLimit = dictionary;
} else {
match = base + matchIndex;
lowLimit = (LZ4_byte const *) source;
}
} else if(dictDirective == LZ4_usingExtDict) {
if(matchIndex < startIndex) {
assert(startIndex - matchIndex >= MINMATCH);
assert(dictBase);
match = dictBase + matchIndex;
lowLimit = dictionary;
} else {
match = base + matchIndex;
lowLimit = (LZ4_byte const *)source;
}
} else {
match = base + matchIndex;
}
forwardH = LZ4_hashPosition(forwardIp, tableType);
LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType);
if((dictIssue == LZ4_dictSmall) && (matchIndex < prefixIdxLimit)) {
continue;
}
assert(matchIndex < current);
if(((tableType != LZ4_byU16) || (LZ4_DISTANCE_MAX < LZ4_DISTANCE_ABSOLUTE_MAX)) && (matchIndex + LZ4_DISTANCE_MAX < current)) {
continue;
}
assert((current - matchIndex) <= LZ4_DISTANCE_MAX);
if(LZ4_read32(match) == LZ4_read32(ip)) {
if(maybe_extMem) offset = current - matchIndex;
break;
}
} while(1);
}
filledIp = ip;
while(((ip > anchor) & (match > lowLimit)) && (unlikely(ip[-1] == match[-1]))) {
ip--;
match--;
}
{
unsigned const litLength = (unsigned) (ip - anchor);
token = op++;
if((outputDirective == LZ4_limitedOutput) && (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength / 255) > olimit))) {
return 0;
}
if((outputDirective == LZ4_fillOutput) && (unlikely(op + (litLength + 240) / 255 + litLength + 2 + 1 + MFLIMIT - MINMATCH > olimit))) {
op--;
goto _last_literals;
}
if(litLength >= RUN_MASK) {
int len = (int) (litLength - RUN_MASK);
*token = (RUN_MASK << ML_BITS);
for(; len >= 255; len -= 255) *op++ = 255;
*op++ = (LZ4_byte) len;
} else {
*token = (LZ4_byte) (litLength << ML_BITS);
}
LZ4_wildCopy8(op, anchor, op + litLength);
op += litLength;
}
_next_match:
if((outputDirective == LZ4_fillOutput) && (op + 2 + 1 + MFLIMIT - MINMATCH > olimit)) {
op = token;
goto _last_literals;
}
if(maybe_extMem) {
assert(offset <= LZ4_DISTANCE_MAX && offset > 0);
LZ4_writeLE16(op, (LZ4_u16) offset);
op += 2;
} else {
assert(ip - match <= LZ4_DISTANCE_MAX);
LZ4_writeLE16(op, (LZ4_u16) (ip - match));
op += 2;
}
{
unsigned matchCode;
if((dictDirective == LZ4_usingExtDict || dictDirective == LZ4_usingDictCtx) && (lowLimit == dictionary)) {
LZ4_byte const *limit = ip + (dictEnd - match);
assert(dictEnd > match);
if(limit > matchlimit) limit = matchlimit;
matchCode = LZ4_count(ip + MINMATCH, match + MINMATCH, limit);
ip += (size_t) matchCode + MINMATCH;
if(ip == limit) {
unsigned const more = LZ4_count(limit, (LZ4_byte const *) source, matchlimit);
matchCode += more;
ip += more;
}
} else {
matchCode = LZ4_count(ip + MINMATCH, match + MINMATCH, matchlimit);
ip += (size_t) matchCode + MINMATCH;
}
if((outputDirective) && (unlikely(op + (1 + LASTLITERALS) + (matchCode + 240) / 255 > olimit))) {
if(outputDirective == LZ4_fillOutput) {
LZ4_u32 newMatchCode = 15 - 1 + ((LZ4_u32) (olimit - op) - 1 - LASTLITERALS) * 255;
ip -= matchCode - newMatchCode;
assert(newMatchCode < matchCode);
matchCode = newMatchCode;
if(unlikely(ip <= filledIp)) {
LZ4_byte const *ptr;
for(ptr = ip; ptr <= filledIp; ++ptr) {
LZ4_u32 const h = LZ4_hashPosition(ptr, tableType);
LZ4_clearHash(h, cctx->hashTable, tableType);
}
}
} else {
assert(outputDirective == LZ4_limitedOutput);
return 0;
}
}
if(matchCode >= ML_MASK) {
*token += ML_MASK;
matchCode -= ML_MASK;
LZ4_write32(op, 0xFFFFFFFFu);
while(matchCode >= 4 * 255) {
op += 4;
LZ4_write32(op, 0xFFFFFFFFu);
matchCode -= 4 * 255;
}
op += matchCode / 255;
*op++ = (LZ4_byte) (matchCode % 255);
} else {
*token += (LZ4_byte)(matchCode);
}
}
assert(!(outputDirective == LZ4_fillOutput && op + 1 + LASTLITERALS > olimit));
anchor = ip;
if(ip >= mflimitPlusOne) {
break;
}
LZ4_putPosition(ip - 2, cctx->hashTable, tableType, base);
if(tableType == LZ4_byPtr) {
match = LZ4_getPosition(ip, cctx->hashTable, tableType, base);
LZ4_putPosition(ip, cctx->hashTable, tableType, base);
if((match + LZ4_DISTANCE_MAX >= ip) && (LZ4_read32(match) == LZ4_read32(ip))) {
token = op++;
*token = 0;
goto _next_match;
}
} else {
LZ4_u32 const h = LZ4_hashPosition(ip, tableType);
LZ4_u32 const current = (LZ4_u32) (ip-base);
LZ4_u32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType);
assert(matchIndex < current);
if(dictDirective == LZ4_usingDictCtx) {
if(matchIndex < startIndex) {
matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, LZ4_byU32);
match = dictBase + matchIndex;
lowLimit = dictionary;
matchIndex += dictDelta;
} else {
match = base + matchIndex;
lowLimit = (LZ4_byte const *) source;
}
} else if(dictDirective == LZ4_usingExtDict) {
if(matchIndex < startIndex) {
assert(dictBase);
match = dictBase + matchIndex;
lowLimit = dictionary;
} else {
match = base + matchIndex;
lowLimit = (LZ4_byte const *) source;
}
} else {
match = base + matchIndex;
}
LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType);
assert(matchIndex < current);
if(((dictIssue == LZ4_dictSmall) ? (matchIndex >= prefixIdxLimit) : 1) && (((tableType == LZ4_byU16) && (LZ4_DISTANCE_MAX == LZ4_DISTANCE_ABSOLUTE_MAX)) ? 1 : (matchIndex + LZ4_DISTANCE_MAX >= current)) && (LZ4_read32(match) == LZ4_read32(ip))) {
token = op++;
*token = 0;
if(maybe_extMem) offset = current - matchIndex;
goto _next_match;
}
}
forwardH = LZ4_hashPosition(++ip, tableType);
}
_last_literals:
{
size_t lastRun = (size_t) (iend - anchor);