Repository navigation
Expand file tree
/
Copy pathsquish.c
More file actions
2202 lines (2034 loc) · 85.3 KB
/
Copy pathsquish.c
File metadata and controls
2202 lines (2034 loc) · 85.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2026 Paige Julianne Sullivan
*
* 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/>.
*/
/* ============================================================================
* squish.c — libsquish implementation
*
* SQUISH is a context-mixing compressor. Per bit:
* 1. Ten models predict P(next bit = 1):
* - order-0..order-6 byte contexts (hashed adaptive counters)
* - word model (hash of current alphanumeric word — text)
* - record model (auto-detected record length R: byte one row up,
* and column-position context — tables/spreadsheets/rasters)
* - match model (6-byte rolling hash -> longest recent repeat)
* 2. A logistic mixer (1024 context-selected weight sets, trained online
* by gradient descent) fuses the predictions in the log-odds domain.
* 3. Two chained APM/SSE stages recalibrate the mixed probability.
* 4. A carryless 32-bit binary arithmetic coder codes the bit.
* Decompression runs the identical model pipeline in lockstep.
*
* There is one on-disk format, the SQUISH archive (magic "SQSH", see
* docs/FORMAT.md). Its atom is a coded *block*:
* [0] mode: 0 = arithmetic-coded, 1 = stored (incompressible fallback)
* [1..] payload
* [last4] FNV-1a 64 of the block's original bytes, folded to 32 bits, LE
* A block's original and compressed sizes are recorded by the enclosing
* archive index, so the block itself carries no magic or length. A member's
* data is one or more blocks (chunked for parallelism); the archive header,
* member blocks, and a compressed index make up the file.
*
* All state lives in a heap-allocated squish_ctx => no global mutable state,
* safe for concurrent use from multiple threads.
* ==========================================================================*/
#include "squish.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t i32;
typedef int64_t i64;
/* ---------------- tunables (fixed: they define the stream format) -------- */
#define TBITS 22 /* per-model hash table: 4M counters (16MB) */
#define TSIZE (1u<<TBITS)
#define TMASK (TSIZE-1)
#define MHBITS 22 /* match-model hash table */
#define MHSIZE (1u<<MHBITS)
#define MHMASK (MHSIZE-1)
#define NTAB 8 /* hashed models: o1 o2 o3 o4 o6 word recA recB */
#define NIN 11 /* mixer inputs: bias + o0 + 8 tables + match */
#define NSETS 1024 /* mixer weight sets */
#define MIX_SHIFT 11 /* mixer learning-rate shift */
#define WCLAMP (1<<24)
#define CNT_CAP 255 /* counter adaptation cap */
#define MINMATCH 6
#define RECWIN 2048 /* record-length vote window */
/* A coded block: 1 mode byte + payload + 4-byte checksum. Its original and
* compressed sizes come from the archive index, not the block. */
#define BLK_MODE 1
#define CKS_SIZE 4
#define BLK_OVER (BLK_MODE + CKS_SIZE) /* fixed per-block overhead = 5 */
enum { MODE_CM = 0, MODE_STORED = 1 };
/* Largest number of blocks a single member can hold (MAX_INPUT / MIN_CHUNK),
* used to bound forged index allocations. */
#define MAX_BLOCKS 65536u
/* ---------------- context: every bit of mutable state -------------------- */
typedef struct squish_ctx {
/* logistic tables (deterministic, built per context: no global state) */
int sq[4096]; /* squash: d(-2048..2047) -> p(0..4095) */
short st[4096]; /* stretch: p(0..4095) -> d(-2047..2047) */
u16 rate[CNT_CAP+1]; /* counter learning rates */
/* models */
u32 *tab[NTAB]; /* hashed counter tables */
u32 o0[256]; /* order-0 direct table */
u32 msm[64]; /* match counters: bucket*2 + expected_bit */
u32 *mt; /* match hash -> position */
i32 w[NSETS*NIN]; /* mixer weights */
u16 *apm1, *apm2; /* SSE tables: 256*33 and 65536*33 */
/* history */
u8 *buf; /* processed bytes so far (window = whole file) */
int buf_write; /* 0 when buf aliases the caller's src */
u64 pos;
u64 c8; /* last 8 bytes */
u32 c0; /* partial byte with leading-1 sentinel */
int bitn;
u32 b1; /* previous byte */
u32 cxt[NTAB];
int have[NTAB];
u32 hword; /* rolling word hash */
u32 lastpos[256]; /* record-length detection */
u32 rvote[RECWIN+1];
u32 R, rcd;
u64 mptr; u32 mlen; /* match model */
int mvalid; u8 mbyte;
/* per-bit scratch */
int stin[NIN];
u32 tidx[NTAB];
int msmidx;
u32 mixsel;
int pmix;
u32 a1idx, a2idx;
/* arithmetic coder + buffer I/O */
u32 x1, x2, x;
const u8 *in; size_t in_pos, in_end; /* decode source */
size_t starved; /* filler bytes read past end */
u8 *out; size_t out_pos, out_cap; /* encode sink */
int overflow;
} Ctx;
/* ---------------- small helpers ------------------------------------------ */
static inline int squash(const Ctx *S, int d) {
if (d < -2047) d = -2047;
if (d > 2047) d = 2047;
return S->sq[d+2048];
}
static inline int cnt_p(u32 c) { return (int)(c >> 16); }
static inline u32 cnt_upd(const Ctx *S, u32 c, int y) {
int p = (int)(c >> 16), n = (int)(c & 0xffff);
p += ((y ? 65535 : 0) - p) * S->rate[n] >> 16;
if (n < CNT_CAP) n++;
return ((u32)p << 16) | (u32)n;
}
static inline u32 hh(u64 x, u64 salt) {
x = (x + salt) * 0x9E3779B97F4A7C15ull;
x ^= x >> 29; x *= 0xBF58476D1CE4E5B9ull; x ^= x >> 32;
return (u32)x;
}
static u64 fnv1a64(const u8 *p, size_t n) {
u64 h = 0xcbf29ce484222325ull;
for (size_t i = 0; i < n; i++) { h ^= p[i]; h *= 0x100000001b3ull; }
return h;
}
/* ---------------- buffer-backed arithmetic coder ------------------------- */
static inline void sink_put(Ctx *S, u8 b) {
if (S->out_pos < S->out_cap) S->out[S->out_pos++] = b;
else S->overflow = 1;
}
static inline int src_get(Ctx *S) { /* 255 past end, like EOF */
if (S->in_pos < S->in_end) return S->in[S->in_pos++];
S->starved++;
return 255;
}
static void enc_init(Ctx *S) { S->x1 = 0; S->x2 = 0xffffffff; }
static inline void enc_bit(Ctx *S, int y, int p16) {
u32 r = S->x2 - S->x1;
u32 xmid = S->x1 + (r>>16)*(u32)p16 + (((r&0xffff)*(u32)p16)>>16);
if (y) S->x2 = xmid; else S->x1 = xmid + 1;
while (((S->x1 ^ S->x2) & 0xff000000u) == 0) {
sink_put(S, (u8)(S->x2 >> 24));
S->x1 <<= 8; S->x2 = (S->x2 << 8) | 255;
}
}
static void enc_flush(Ctx *S) {
for (int i = 0; i < 4; i++) { sink_put(S, (u8)(S->x1 >> 24)); S->x1 <<= 8; }
}
static void dec_init(Ctx *S) {
S->x1 = 0; S->x2 = 0xffffffff; S->x = 0;
for (int i = 0; i < 4; i++) S->x = (S->x << 8) | (u32)src_get(S);
}
static inline int dec_bit(Ctx *S, int p16) {
u32 r = S->x2 - S->x1;
u32 xmid = S->x1 + (r>>16)*(u32)p16 + (((r&0xffff)*(u32)p16)>>16);
int y = (S->x <= xmid);
if (y) S->x2 = xmid; else S->x1 = xmid + 1;
while (((S->x1 ^ S->x2) & 0xff000000u) == 0) {
S->x1 <<= 8; S->x2 = (S->x2 << 8) | 255;
S->x = (S->x << 8) | (u32)src_get(S);
}
return y;
}
/* ---------------- APM / SSE stage ---------------------------------------- */
static int apm_alloc(Ctx *S, u16 **t, u32 n) {
*t = (u16*)malloc((size_t)n * 33 * sizeof(u16));
if (!*t) return 0;
for (u32 c = 0; c < n; c++)
for (u32 j = 0; j < 33; j++)
(*t)[c*33+j] = (u16)(squash(S, ((int)j - 16) * 128) << 4);
return 1;
}
static inline int apm_pp(const Ctx *S, u16 *t, u32 cx, int pr, u32 *save) {
int s = S->st[pr] + 2048; /* 1..4095 */
int j = s >> 7, wg = s & 127;
u32 i = cx * 33 + (u32)j;
*save = i + (u32)((wg >> 6) & 1);
return (t[i]*(128-wg) + t[i+1]*wg) >> 11;
}
static inline void apm_upd(u16 *t, u32 idx, int y) {
int v = t[idx];
v += ((y ? 65535 : 0) - v) >> 7;
t[idx] = (u16)v;
}
/* ---------------- predictor ---------------------------------------------- */
static void set_contexts(Ctx *S) {
S->cxt[0] = hh(S->c8 & 0xffull, 0x11);
S->cxt[1] = hh(S->c8 & 0xffffull, 0x22);
S->cxt[2] = hh(S->c8 & 0xffffffull, 0x33);
S->cxt[3] = hh(S->c8 & 0xffffffffull, 0x44);
S->cxt[4] = hh(S->c8 & 0xffffffffffffull, 0x66);
S->have[0]=S->have[1]=S->have[2]=S->have[3]=S->have[4]=1;
if (S->hword) { S->cxt[5] = hh(S->hword, 0x77); S->have[5] = 1; }
else S->have[5] = 0;
if (S->R >= 2 && S->pos >= 2*(u64)S->R) {
u8 up1 = S->buf[S->pos - S->R], up2 = S->buf[S->pos - 2*(u64)S->R];
S->cxt[6] = hh(((u64)S->R << 8) | up1, 0x88);
S->cxt[7] = hh(((u64)(S->pos % S->R) << 16) | ((u64)up1 << 8) | up2,
((u64)S->R << 8) ^ 0x99);
S->have[6] = S->have[7] = 1;
} else S->have[6] = S->have[7] = 0;
if (S->mlen) { S->mvalid = 1; S->mbyte = S->buf[S->mptr]; }
else S->mvalid = 0;
S->mixsel = ((S->mlen ? 1u : 0u) << 9) | ((S->R >= 2 ? 1u : 0u) << 8) | S->b1;
}
static void byte_update(Ctx *S, u8 b) {
if (S->buf_write) S->buf[S->pos] = b;
S->pos++;
S->b1 = b;
S->c8 = (S->c8 << 8) | b;
/* word hash: letters, digits, non-ASCII continue a word */
if ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') ||
(b >= '0' && b <= '9') || b >= 128)
S->hword = (S->hword + (u32)(b | 32) + 1) * 0x9E3779B1u;
else S->hword = 0;
/* record-length voting */
{
u32 d = (u32)(S->pos - S->lastpos[b]);
if (d >= 2 && d <= RECWIN) S->rvote[d]++;
S->lastpos[b] = (u32)S->pos;
if (--S->rcd == 0) {
S->rcd = RECWIN;
u32 vmax = 0, dmax = 0;
for (u32 d2 = 2; d2 <= RECWIN; d2++) {
if (S->rvote[d2] > vmax) { vmax = S->rvote[d2]; dmax = d2; }
S->rvote[d2] >>= 1;
}
if (vmax >= 600) S->R = dmax;
else if (vmax < 300) S->R = 0;
}
}
/* match model: extend or re-anchor */
if (S->mlen) {
if (S->buf[S->mptr] == b) { S->mptr++; if (S->mlen < 65535) S->mlen++; }
else S->mlen = 0;
}
if (S->pos >= MINMATCH) {
u32 mh = hh(S->c8 & 0xffffffffffffull, 0xABCD) & MHMASK;
if (!S->mlen) {
u32 cand = S->mt[mh];
if (cand && cand < S->pos && cand >= 3 &&
S->buf[cand-1] == S->buf[S->pos-1] &&
S->buf[cand-2] == S->buf[S->pos-2] &&
S->buf[cand-3] == S->buf[S->pos-3]) {
S->mptr = cand; S->mlen = MINMATCH;
}
}
S->mt[mh] = (u32)S->pos;
}
set_contexts(S);
}
/* returns 12-bit P(bit=1), 1..4094 */
static inline int predict(Ctx *S) {
S->stin[0] = 128; /* mixer bias */
S->stin[1] = S->st[cnt_p(S->o0[S->c0]) >> 4];
for (int k = 0; k < NTAB; k++) {
if (S->have[k]) {
S->tidx[k] = (S->cxt[k] ^ (S->c0 * 2654435761u)) & TMASK;
S->stin[2+k] = S->st[cnt_p(S->tab[k][S->tidx[k]]) >> 4];
} else S->stin[2+k] = 0;
}
if (S->mvalid) {
int expbit = (S->mbyte >> (7 - S->bitn)) & 1;
int bucket = S->mlen < 32 ? (int)S->mlen : 31;
S->msmidx = bucket * 2 + expbit;
S->stin[10] = S->st[cnt_p(S->msm[S->msmidx]) >> 4];
} else { S->msmidx = -1; S->stin[10] = 0; }
i64 dot = 0;
const i32 *wp = &S->w[S->mixsel * NIN];
for (int i = 0; i < NIN; i++) dot += (i64)wp[i] * S->stin[i];
int d = (int)(dot >> 16);
if (d < -2047) d = -2047;
if (d > 2047) d = 2047;
S->pmix = S->sq[d + 2048];
int pr = S->pmix;
pr = (pr + 3 * apm_pp(S, S->apm1, S->c0, pr, &S->a1idx)) >> 2;
pr = (pr + 3 * apm_pp(S, S->apm2, (S->b1 << 8) | S->c0, pr, &S->a2idx)) >> 2;
if (pr < 1) pr = 1;
if (pr > 4094) pr = 4094;
return pr;
}
static inline void update(Ctx *S, int y) {
int err = (y << 12) - S->pmix;
i32 *wp = &S->w[S->mixsel * NIN];
for (int i = 0; i < NIN; i++) {
i32 w = wp[i] + ((S->stin[i] * err) >> MIX_SHIFT);
if (w < -WCLAMP) w = -WCLAMP;
if (w > WCLAMP) w = WCLAMP;
wp[i] = w;
}
S->o0[S->c0] = cnt_upd(S, S->o0[S->c0], y);
for (int k = 0; k < NTAB; k++)
if (S->have[k]) S->tab[k][S->tidx[k]] = cnt_upd(S, S->tab[k][S->tidx[k]], y);
if (S->msmidx >= 0) S->msm[S->msmidx] = cnt_upd(S, S->msm[S->msmidx], y);
apm_upd(S->apm1, S->a1idx, y);
apm_upd(S->apm2, S->a2idx, y);
if (S->mvalid) {
int expbit = (S->mbyte >> (7 - S->bitn)) & 1;
if (y != expbit) S->mvalid = 0;
}
S->c0 = (S->c0 << 1) | (u32)y;
if (++S->bitn == 8) {
u8 b = (u8)(S->c0 & 0xff);
S->c0 = 1; S->bitn = 0;
byte_update(S, b);
}
}
/* ---------------- context lifecycle -------------------------------------- */
static void ctx_free(Ctx *S) {
if (!S) return;
for (int k = 0; k < NTAB; k++) free(S->tab[k]);
free(S->mt); free(S->apm1); free(S->apm2);
free(S);
}
/* buf: history window (caller's src for compress, dst for decompress) */
static Ctx *ctx_new(u8 *buf, int buf_write) {
Ctx *S = (Ctx*)calloc(1, sizeof(Ctx));
if (!S) return NULL;
for (int d = -2048; d < 2048; d++) {
double v = 4096.0 / (1.0 + exp(-d / 256.0));
int r = (int)v; if (r > 4095) r = 4095; if (r < 0) r = 0;
S->sq[d+2048] = r;
}
int pi = 0;
for (int d = -2047; d <= 2047; d++) {
int p = S->sq[d+2048];
while (pi <= p && pi < 4096) S->st[pi++] = (short)d;
}
while (pi < 4096) S->st[pi++] = 2047;
for (int n = 0; n <= CNT_CAP; n++) S->rate[n] = (u16)(65536 / (n + 2));
for (int k = 0; k < NTAB; k++) {
S->tab[k] = (u32*)malloc((size_t)TSIZE * sizeof(u32));
if (!S->tab[k]) { ctx_free(S); return NULL; }
for (u32 i = 0; i < TSIZE; i++) S->tab[k][i] = 32768u << 16;
}
S->mt = (u32*)calloc(MHSIZE, sizeof(u32));
if (!S->mt) { ctx_free(S); return NULL; }
for (int i = 0; i < 256; i++) S->o0[i] = 32768u << 16;
for (int b = 0; b < 32; b++) {
S->msm[b*2+0] = 12000u << 16; /* expected bit 0 -> P(1) low */
S->msm[b*2+1] = 53536u << 16; /* expected bit 1 -> P(1) high */
}
for (int i = 0; i < NSETS*NIN; i++) S->w[i] = 65536 / NIN;
if (!apm_alloc(S, &S->apm1, 256) || !apm_alloc(S, &S->apm2, 65536)) {
ctx_free(S); return NULL;
}
S->buf = buf; S->buf_write = buf_write;
S->c0 = 1; S->rcd = RECWIN;
set_contexts(S);
return S;
}
/* ---------------- container helpers -------------------------------------- */
static void put_le(u8 *p, u64 v, int nb) {
for (int i = 0; i < nb; i++) p[i] = (u8)(v >> (8*i));
}
static u64 get_le(const u8 *p, int nb) {
u64 v = 0;
for (int i = 0; i < nb; i++) v |= (u64)p[i] << (8*i);
return v;
}
/* ---------------- thread portability shim -------------------------------- */
#if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
typedef HANDLE sq_thread;
typedef CRITICAL_SECTION sq_mutex;
typedef DWORD sq_thread_ret;
# define SQ_THREAD_CALL WINAPI
# define SQ_THREAD_DONE 0
static void sq_mutex_init(sq_mutex *m) { InitializeCriticalSection(m); }
static void sq_mutex_destroy(sq_mutex *m) { DeleteCriticalSection(m); }
static void sq_mutex_lock(sq_mutex *m) { EnterCriticalSection(m); }
static void sq_mutex_unlock(sq_mutex *m) { LeaveCriticalSection(m); }
typedef sq_thread_ret (SQ_THREAD_CALL *sq_thread_fn)(void *);
static int sq_thread_start(sq_thread *t, sq_thread_fn fn, void *arg) {
*t = CreateThread(NULL, 0, fn, arg, 0, NULL);
return *t != NULL;
}
static void sq_thread_join(sq_thread t) {
WaitForSingleObject(t, INFINITE);
CloseHandle(t);
}
static int sq_ncpu(void) {
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwNumberOfProcessors > 0 ? (int)si.dwNumberOfProcessors : 1;
}
#else
# include <pthread.h>
# include <unistd.h>
typedef pthread_t sq_thread;
typedef pthread_mutex_t sq_mutex;
typedef void * sq_thread_ret;
# define SQ_THREAD_CALL
# define SQ_THREAD_DONE NULL
static void sq_mutex_init(sq_mutex *m) { pthread_mutex_init(m, NULL); }
static void sq_mutex_destroy(sq_mutex *m) { pthread_mutex_destroy(m); }
static void sq_mutex_lock(sq_mutex *m) { pthread_mutex_lock(m); }
static void sq_mutex_unlock(sq_mutex *m) { pthread_mutex_unlock(m); }
typedef sq_thread_ret (*sq_thread_fn)(void *);
static int sq_thread_start(sq_thread *t, sq_thread_fn fn, void *arg) {
return pthread_create(t, NULL, fn, arg) == 0;
}
static void sq_thread_join(sq_thread t) { pthread_join(t, NULL); }
static int sq_ncpu(void) {
long n = sysconf(_SC_NPROCESSORS_ONLN);
return n > 0 ? (int)n : 1;
}
#endif
/* ---------------- public API: introspection ------------------------------*/
SQUISH_API const char *squish_version(void) { return SQUISH_VERSION_STRING; }
SQUISH_API const char *squish_strerror(int code) {
switch (code) {
case SQUISH_OK: return "success";
case SQUISH_E_PARAM: return "invalid argument";
case SQUISH_E_NOMEM: return "out of memory";
case SQUISH_E_FORMAT: return "not a SQUISH archive or archive corrupt";
case SQUISH_E_DSTSIZE: return "destination buffer too small";
case SQUISH_E_TOOBIG: return "input exceeds 4 GiB limit";
case SQUISH_E_IO: return "file I/O error";
case SQUISH_E_CHECKSUM: return "integrity check failed";
default: return "unknown error";
}
}
SQUISH_API int squish_threads(void) { return sq_ncpu(); }
SQUISH_API void squish_free(void *p) { free(p); }
/* Number of coded blocks a member of `orig` bytes uses at block size `chunk`
* (chunk >= 1): 0 for an empty member, else ceil(orig/chunk). */
static u64 member_blocks(u64 orig, u64 chunk) {
return orig ? (orig + chunk - 1) / chunk : 0;
}
/* ---------------- coded block: the compression atom ----------------------- *
* A block is [mode u8][payload][checksum u32]; its original and compressed
* sizes live in the enclosing index. Stored mode caps a block at n+BLK_OVER. */
static int block_compress(const u8 *src, size_t n, u8 *dst, size_t cap,
size_t *outlen, squish_progress_fn cb, void *user) {
if ((u64)n >= SQUISH_MAX_INPUT) return SQUISH_E_TOOBIG;
size_t stored_size = BLK_OVER + n;
if (cap < BLK_OVER) return SQUISH_E_DSTSIZE;
u64 cks = fnv1a64(src, n);
Ctx *S = ctx_new((u8*)(uintptr_t)src, /*buf_write=*/0);
if (!S) return SQUISH_E_NOMEM;
S->out = dst + BLK_MODE;
S->out_cap = (cap < stored_size ? cap : stored_size) - BLK_OVER;
enc_init(S);
for (size_t j = 0; j < n && !S->overflow; j++) {
if (cb && (j & 0xFFFFu) == 0) cb(j, n, user);
u8 b = src[j];
for (int i = 7; i >= 0; i--) {
int y = (b >> i) & 1;
int pr = predict(S);
enc_bit(S, y, (pr << 4) | 8);
update(S, y);
}
}
if (!S->overflow) enc_flush(S);
int overflow = S->overflow;
size_t coded = S->out_pos;
ctx_free(S);
if (!overflow) {
dst[0] = MODE_CM;
put_le(dst + BLK_MODE + coded, cks & 0xffffffff, 4);
*outlen = BLK_MODE + coded + CKS_SIZE;
if (cb) cb(n, n, user);
return SQUISH_OK;
}
if (cap < stored_size) return SQUISH_E_DSTSIZE;
dst[0] = MODE_STORED;
if (n) memcpy(dst + BLK_MODE, src, n);
put_le(dst + BLK_MODE + n, cks & 0xffffffff, 4);
*outlen = stored_size;
if (cb) cb(n, n, user);
return SQUISH_OK;
}
static int block_decompress(const u8 *src, size_t csize, u8 *dst, size_t n,
squish_progress_fn cb, void *user) {
if (csize < BLK_OVER) return SQUISH_E_FORMAT;
int mode = src[0];
if (mode != MODE_CM && mode != MODE_STORED) return SQUISH_E_FORMAT;
u32 want = (u32)get_le(src + csize - CKS_SIZE, 4);
if (mode == MODE_STORED) {
if (csize != BLK_OVER + n) return SQUISH_E_FORMAT;
if (n) memcpy(dst, src + BLK_MODE, n);
} else {
Ctx *S = ctx_new(dst, /*buf_write=*/1);
if (!S) return SQUISH_E_NOMEM;
S->in = src; S->in_pos = BLK_MODE; S->in_end = csize - CKS_SIZE;
dec_init(S);
for (u64 j = 0; j < n; j++) {
/* A well-formed block consumes exactly the encoder's bytes, never
* the past-end filler. Sustained filler reads mean a truncated or
* forged block; bail instead of decoding up to 4 GiB of noise. */
if (S->starved > 64) { ctx_free(S); return SQUISH_E_FORMAT; }
if (cb && (j & 0xFFFFu) == 0) cb(j, n, user);
for (int i = 7; i >= 0; i--) {
int pr = predict(S);
int y = dec_bit(S, (pr << 4) | 8);
update(S, y);
}
}
ctx_free(S);
}
if ((u32)(fnv1a64(dst, n) & 0xffffffff) != want) return SQUISH_E_CHECKSUM;
if (cb) cb(n, n, user);
return SQUISH_OK;
}
SQUISH_API size_t squish_compress_bound(size_t src_len) {
/* header + member blocks (stored worst case) + a one-entry index block */
size_t k = src_len / SQUISH_MIN_CHUNK + 1; /* max blocks */
return 64 + src_len + BLK_OVER * k /* member data region */
+ (25 + 8 * k + BLK_OVER) /* index: 1 entry, stored */
+ 64; /* slack */
}
/* ---------------- multi-block progress fan-in ----------------------------- */
typedef struct {
squish_progress_fn cb; void *user;
u64 total, agg;
u64 *cdone; /* per-block bytes reported so far */
sq_mutex mu;
} mt_prog;
typedef struct { mt_prog *p; u32 idx; } chunk_prog;
static void mt_prog_cb(u64 done, u64 total, void *user) {
chunk_prog *cp = (chunk_prog*)user;
mt_prog *p = cp->p;
(void)total;
sq_mutex_lock(&p->mu);
p->agg += done - p->cdone[cp->idx];
p->cdone[cp->idx] = done;
p->cb(p->agg, p->total, p->user);
sq_mutex_unlock(&p->mu);
}
static int mt_prog_init(mt_prog *p, squish_progress_fn cb, void *user,
u64 total, u32 nchunks, chunk_prog **cps) {
*cps = NULL;
if (!cb || nchunks == 0) return 1;
p->cb = cb; p->user = user; p->total = total; p->agg = 0;
p->cdone = (u64*)calloc(nchunks, sizeof(u64));
*cps = (chunk_prog*)malloc((size_t)nchunks * sizeof(chunk_prog));
if (!p->cdone || !*cps) {
free(p->cdone); free(*cps); *cps = NULL;
return 0;
}
for (u32 i = 0; i < nchunks; i++) { (*cps)[i].p = p; (*cps)[i].idx = i; }
sq_mutex_init(&p->mu);
return 1;
}
static void mt_prog_destroy(mt_prog *p, chunk_prog *cps) {
if (!cps) return;
sq_mutex_destroy(&p->mu);
free(p->cdone); free(cps);
}
/* Workers take blocks i = tid, tid + T, ...: equal-sized blocks make static
* striding balance as well as a shared queue, with no synchronization. */
typedef struct {
/* compress */
const u8 *src; u64 src_len, chunk;
u8 **bufs; size_t *lens;
/* decompress */
const u8 *s; u8 *dst;
const u64 *coff, *doff, *olen, *clen;
/* shared */
int *rcs; chunk_prog *cps;
u32 nchunks, tid, nthreads;
} mt_args;
static sq_thread_ret SQ_THREAD_CALL compress_worker(void *arg) {
mt_args *a = (mt_args*)arg;
for (u32 i = a->tid; i < a->nchunks; i += a->nthreads) {
u64 off = (u64)i * a->chunk;
size_t len = (size_t)(a->chunk <= a->src_len - off ? a->chunk
: a->src_len - off);
size_t outn = len + BLK_OVER;
a->bufs[i] = (u8*)malloc(outn);
if (!a->bufs[i]) { a->rcs[i] = SQUISH_E_NOMEM; continue; }
a->rcs[i] = block_compress(a->src + off, len, a->bufs[i], outn, &outn,
a->cps ? mt_prog_cb : NULL,
a->cps ? (void*)&a->cps[i] : NULL);
a->lens[i] = outn;
}
return SQ_THREAD_DONE;
}
static sq_thread_ret SQ_THREAD_CALL decompress_worker(void *arg) {
mt_args *a = (mt_args*)arg;
for (u32 i = a->tid; i < a->nchunks; i += a->nthreads) {
a->rcs[i] = block_decompress(a->s + a->coff[i], (size_t)a->clen[i],
a->dst + a->doff[i], (size_t)a->olen[i],
a->cps ? mt_prog_cb : NULL,
a->cps ? (void*)&a->cps[i] : NULL);
}
return SQ_THREAD_DONE;
}
/* Run `fn` over `nchunks` blocks on `nthreads` threads (worker 0 runs on the
* calling thread; a failed spawn folds that stripe into the caller too). */
static void mt_run(sq_thread_fn fn, mt_args *proto, u32 nthreads,
mt_args *args, sq_thread *tids) {
u32 started = 0;
for (u32 t = 1; t < nthreads; t++) {
args[t] = *proto;
args[t].tid = t;
args[t].nthreads = nthreads;
if (sq_thread_start(&tids[started], fn, &args[t])) started++;
else { args[t].nthreads = 0; } /* mark: run inline below */
}
args[0] = *proto;
args[0].tid = 0;
args[0].nthreads = nthreads;
fn(&args[0]);
for (u32 t = 1; t < nthreads; t++)
if (args[t].nthreads == 0) { args[t].nthreads = nthreads; fn(&args[t]); }
for (u32 t = 0; t < started; t++) sq_thread_join(tids[t]);
}
/* Compress a member's bytes into k = ceil(src_len/chunk) coded blocks (0 for
* an empty member). On success returns malloc'd arrays *bufs (each a block)
* and *lens, and *k; the caller frees every *bufs[i] plus *bufs and *lens.
* `chunk` is the effective (>= MIN, non-zero) block size actually used. */
static int compress_member(const u8 *src, size_t src_len, int nthreads,
u64 chunk, u8 ***bufs_out, size_t **lens_out,
u32 *k_out, squish_progress_fn cb, void *user) {
*bufs_out = NULL; *lens_out = NULL; *k_out = 0;
if ((u64)src_len >= SQUISH_MAX_INPUT) return SQUISH_E_TOOBIG;
if (src_len == 0) return SQUISH_OK; /* empty: no blocks */
u32 k = (u32)member_blocks(src_len, chunk);
int T = nthreads > 0 ? nthreads : sq_ncpu();
if ((u32)T > k) T = (int)k;
u8 **bufs = (u8**)calloc(k, sizeof(u8*));
size_t *lens = (size_t*)calloc(k, sizeof(size_t));
int *rcs = (int*)calloc(k, sizeof(int));
mt_args *args = (mt_args*)calloc(T, sizeof(mt_args));
sq_thread *tids = (sq_thread*)calloc(T, sizeof(sq_thread));
mt_prog prog; chunk_prog *cps = NULL;
int rc = SQUISH_OK;
if (!bufs || !lens || !rcs || !args || !tids ||
!mt_prog_init(&prog, cb, user, src_len, k, &cps)) {
rc = SQUISH_E_NOMEM; goto done;
}
{
mt_args proto;
memset(&proto, 0, sizeof proto);
proto.src = src; proto.src_len = src_len; proto.chunk = chunk;
proto.bufs = bufs; proto.lens = lens;
proto.rcs = rcs; proto.cps = cps; proto.nchunks = k;
mt_run(compress_worker, &proto, (u32)T, args, tids);
}
for (u32 i = 0; i < k && rc == SQUISH_OK; i++) rc = rcs[i];
done:
mt_prog_destroy(&prog, cps);
free(rcs); free(args); free(tids);
if (rc != SQUISH_OK) {
if (bufs) for (u32 i = 0; i < k; i++) free(bufs[i]);
free(bufs); free(lens);
return rc;
}
*bufs_out = bufs; *lens_out = lens; *k_out = k;
return SQUISH_OK;
}
/* Decode a member: `data` points at its first block; `clen[i]` is each block's
* compressed length (blocks are contiguous). Writes `orig` bytes to dst. */
static int decompress_member(const u8 *data, u64 data_len, const u64 *clen,
u32 k, u64 orig, u64 chunk, u8 *dst, int nthreads,
squish_progress_fn cb, void *user) {
if (k == 0) {
if (orig != 0) return SQUISH_E_FORMAT;
if (cb) cb(0, 0, user);
return SQUISH_OK;
}
u64 *coff = (u64*)malloc((size_t)k * sizeof(u64));
u64 *doff = (u64*)malloc((size_t)k * sizeof(u64));
u64 *olen = (u64*)malloc((size_t)k * sizeof(u64));
int *rcs = (int*)calloc(k, sizeof(int));
mt_args *args = NULL; sq_thread *tids = NULL;
mt_prog prog; chunk_prog *cps = NULL;
int rc = SQUISH_OK;
if (!coff || !doff || !olen || !rcs) { rc = SQUISH_E_NOMEM; goto done; }
{ /* block table: contiguous compressed spans tiling [0, data_len);
* per-block original sizes derived from the uniform chunk. */
u64 co = 0, dof = 0;
for (u32 i = 0; i < k; i++) {
coff[i] = co; doff[i] = dof;
if (clen[i] < BLK_OVER || clen[i] > data_len - co) {
rc = SQUISH_E_FORMAT; goto done;
}
olen[i] = chunk <= orig - dof ? chunk : orig - dof;
co += clen[i]; dof += olen[i];
}
if (co != data_len || dof != orig) { rc = SQUISH_E_FORMAT; goto done; }
}
{
int T = nthreads > 0 ? nthreads : sq_ncpu();
if ((u32)T > k) T = (int)k;
args = (mt_args*)calloc(T, sizeof(mt_args));
tids = (sq_thread*)calloc(T, sizeof(sq_thread));
if (!args || !tids || !mt_prog_init(&prog, cb, user, orig, k, &cps)) {
rc = SQUISH_E_NOMEM; goto done;
}
mt_args proto;
memset(&proto, 0, sizeof proto);
proto.s = data; proto.dst = dst;
proto.coff = coff; proto.doff = doff; proto.olen = olen; proto.clen = clen;
proto.rcs = rcs; proto.cps = cps; proto.nchunks = k;
mt_run(decompress_worker, &proto, (u32)T, args, tids);
}
for (u32 i = 0; i < k && rc == SQUISH_OK; i++) rc = rcs[i];
if (rc == SQUISH_OK && cb) cb(orig, orig, user);
done:
mt_prog_destroy(&prog, cps);
free(coff); free(doff); free(olen); free(rcs); free(args); free(tids);
return rc;
}
/* ============================================================================
* SQUISH archive: the one on-disk format (see docs/FORMAT.md)
*
* A header, one independently-compressed member per file (each a list of
* coded blocks), and a compressed index of (path, mode, size, block layout).
* A reader inflates the small index at open time, then reaches any single
* member by seeking to its blocks — the rest is never touched. LE integers.
*
* 0 8 magic "SQSH\r\n\x1a\n" 16 8 entry_count u64
* 8 4 version u32 (1) 24 8 total_size u64 (sum of file sizes)
* 12 4 flags u32 (0) 32 8 chunk_size u64
* 40 8 index_offset u64 48 8 index_size u64 (compressed)
* 56 8 index_orig_size u64 64 ... member blocks, then index block
*
* Index entry: type u8 | mode u32 | orig u64 | data_off u64 |
* (ceil(orig/chunk)) x (csize u64) | plen u32 | path[plen]
* (a directory carries orig/data_off = 0 and no block sizes.)
* ==========================================================================*/
#include <errno.h>
#if defined(_WIN32)
# include <direct.h>
# include <io.h>
#else
# include <sys/types.h>
# include <sys/stat.h>
# include <dirent.h>
#endif
static const u8 ARC_MAGIC[8] = { 'S','Q','S','H', 0x0D, 0x0A, 0x1A, 0x0A };
#define ARC_VERSION 1u
#define ARC_FLAG_SINGLE 1u /* archive is one file/blob, not a tree */
#define ARC_HDR 64u /* fixed container header size */
#define ARC_ENT_FIXED 25u /* type1+mode4+orig8+doff8+plen4 */
#define ARC_MAX_PATH 65535u
#if defined(_WIN32)
# define sq_fseek64(f,o) _fseeki64((f),(long long)(o),SEEK_SET)
# define sq_ftell64(f) _ftelli64(f)
#else
# define sq_fseek64(f,o) fseeko((f),(off_t)(o),SEEK_SET)
# define sq_ftell64(f) ftello(f)
#endif
/* ---------------- growable byte buffer ----------------------------------- */
typedef struct { u8 *p; size_t len, cap; } gbuf;
static int gbuf_put(gbuf *b, const void *d, size_t n) {
if (!n) return 0;
if (b->len + n < b->len) return -1; /* size_t wrap */
if (b->len + n > b->cap) {
size_t nc = b->cap ? b->cap : 4096;
while (nc < b->len + n) {
if (nc > (size_t)-1 / 2) { nc = b->len + n; break; }
nc *= 2;
}
u8 *np = (u8*)realloc(b->p, nc);
if (!np) return -1;
b->p = np; b->cap = nc;
}
memcpy(b->p + b->len, d, n);
b->len += n;
return 0;
}
static int gbuf_u8 (gbuf *b, u8 v) { return gbuf_put(b, &v, 1); }
static int gbuf_u32(gbuf *b, u32 v) { u8 t[4]; put_le(t, v, 4); return gbuf_put(b, t, 4); }
static int gbuf_u64(gbuf *b, u64 v) { u8 t[8]; put_le(t, v, 8); return gbuf_put(b, t, 8); }
/* ---------------- path helpers ------------------------------------------- */
static char *path_join(const char *a, const char *b) {
size_t la = a ? strlen(a) : 0, lb = strlen(b);
char *r = (char*)malloc(la + 1 + lb + 1);
if (!r) return NULL;
if (la) { memcpy(r, a, la); r[la] = '/'; memcpy(r + la + 1, b, lb + 1); }
else { memcpy(r, b, lb + 1); }
return r;
}
static char *strip_trailing_sep(const char *path) {
size_t n = strlen(path);
while (n > 1 && (path[n-1] == '/' || path[n-1] == '\\')) n--;
char *r = (char*)malloc(n + 1);
if (!r) return NULL;
memcpy(r, path, n); r[n] = '\0';
return r;
}
/* A stored path is safe iff it is relative and every component is non-empty,
* not "." or "..", and free of ':' and '\\' — so an archive can never write
* outside the extraction root. The empty path is reserved for the unnamed
* member of a buffer/blob archive and is handled separately by callers. */
static int arc_path_safe(const char *p, size_t n) {
if (n == 0 || p[0] == '/') return 0;
for (size_t i = 0; i < n; ) {
size_t j = i;
while (j < n && p[j] != '/') j++;
size_t clen = j - i;
if (clen == 0) return 0;
if (clen == 1 && p[i] == '.') return 0;
if (clen == 2 && p[i] == '.' && p[i+1] == '.') return 0;
for (size_t k = i; k < j; k++)
if (p[k] == '\\' || p[k] == ':' || p[k] == '\0') return 0;
i = (j < n) ? j + 1 : j;
}
return 1;
}
/* ---------------- filesystem probes -------------------------------------- */
static int path_is_dir(const char *p) {
#if defined(_WIN32)
DWORD a = GetFileAttributesA(p);
return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else
struct stat s;
return stat(p, &s) == 0 && S_ISDIR(s.st_mode);
#endif
}
static int path_is_regular(const char *p) {
#if defined(_WIN32)
DWORD a = GetFileAttributesA(p);
return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY) == 0;
#else
struct stat s;
return stat(p, &s) == 0 && S_ISREG(s.st_mode);
#endif
}
static unsigned path_mode(const char *p, unsigned dflt) {
#if defined(_WIN32)
(void)p; return dflt;
#else
struct stat s;
return stat(p, &s) == 0 ? (unsigned)(s.st_mode & 0777) : dflt;
#endif
}
static u64 path_size(const char *p) {
#if defined(_WIN32)
WIN32_FILE_ATTRIBUTE_DATA d;
if (GetFileAttributesExA(p, GetFileExInfoStandard, &d))
return ((u64)d.nFileSizeHigh << 32) | d.nFileSizeLow;
return 0;
#else
struct stat s;
return stat(p, &s) == 0 ? (u64)s.st_size : 0;
#endif
}
static int make_dir(const char *p) {
#if defined(_WIN32)
if (_mkdir(p) == 0) return 0;
#else
if (mkdir(p, 0777) == 0) return 0;
#endif
return errno == EEXIST ? 0 : -1;
}
static int make_dirs(const char *out_root, const char *rel, int include_last) {
if (make_dir(out_root) != 0) return -1;
size_t rl = strlen(out_root);
char *w = (char*)malloc(rl + 1 + strlen(rel) + 1);
if (!w) return -1;
memcpy(w, out_root, rl); w[rl] = '\0';
size_t wl = rl;
int rc = 0;
for (const char *c = rel; *c; ) {
const char *slash = strchr(c, '/');
size_t clen = slash ? (size_t)(slash - c) : strlen(c);
if (!slash && !include_last) break;
w[wl++] = '/';
memcpy(w + wl, c, clen); wl += clen; w[wl] = '\0';
if (make_dir(w) != 0) { rc = -1; break; }
if (!slash) break;
c = slash + 1;
}
free(w);
return rc;
}
static int write_file_bytes(const char *path, const u8 *d, size_t n, unsigned mode) {
FILE *f = fopen(path, "wb");
if (!f) return -1;
int ok = !(n && fwrite(d, 1, n, f) != n);
if (fclose(f) != 0) ok = 0;
if (!ok) { remove(path); return -1; }
#if !defined(_WIN32)
if (mode) chmod(path, (mode_t)(mode & 0777));
#else
(void)mode;
#endif
return 0;
}
/* ---------------- file slurp/spew ---------------------------------------- */
static int read_whole(const char *path, u8 **data, size_t *len) {
FILE *f = fopen(path, "rb");
if (!f) return SQUISH_E_IO;
if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return SQUISH_E_IO; }
long long n = sq_ftell64(f);
if (n < 0) { fclose(f); return SQUISH_E_IO; }
if ((unsigned long long)n > (size_t)-1) { fclose(f); return SQUISH_E_TOOBIG; }
rewind(f);
u8 *buf = (u8*)malloc(n ? (size_t)n : 1);
if (!buf) { fclose(f); return SQUISH_E_NOMEM; }
if (n && fread(buf, 1, (size_t)n, f) != (size_t)n) {
free(buf); fclose(f); return SQUISH_E_IO;
}
fclose(f);
*data = buf; *len = (size_t)n;
return SQUISH_OK;
}
/* ---------------- directory listing (sorted, reproducible) --------------- */
static int name_cmp(const void *a, const void *b) {
return strcmp(*(const char *const *)a, *(const char *const *)b);
}
static void free_names(char **names, size_t n) {
for (size_t i = 0; i < n; i++) free(names[i]);
free(names);