lz4.k 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // decompression
  2. LZ4HDR:0x184D2204
  3. islz4:LZ4HDR~|4#
  4. flgs:{(vs;b):0 2_(8#2)\x
  5. $[~0 1~vs;`err"unsupported version";]
  6. `bi`bc`cs`cc`"_"`did!b}
  7. bdb:{`"_"`bmx`"_"!2/'0 1 4_(8#2)\x}
  8. frm:{islz4@x
  9. (f;b):(flgs;bdb)@'x[4 5]
  10. off:8 4*`cs`did#f
  11. sz:256/256!|x@7+!off`cs
  12. did:x@7+off[`cs]+!off`did
  13. ((7++/off)_x;f;b;sz;did)}
  14. blks:{(out;in):y
  15. sn:$[x`bi;"";out]
  16. $[0x00000000~s:|4#in;out
  17. 0>*s;(out;""),'(4+0,256/(256!s)-128,3#0)_in
  18. [(b;r):(4+0,256/256!s)_in
  19. r:(4*x`bc)_r / block checksum. Just drop for now.
  20. (out,*(#*|:)f/(sn;b);r)]]}
  21. tk:{1++/&\255=7#x}
  22. f0:{(r;s;l;m):0 0,16 16\**(t;b):0 1_x
  23. $[l=15;[(s;b):_/|1(0,tk@)\b;l:+/l,s];]
  24. $[m=15;m:+/m,#/r:|1 tk\(2+l)_b;]
  25. (l;4+m;256/|b[l+!2];*r;b)}
  26. f:{(l;m;off;r;b):f0[*|x]
  27. m*:lst:~l=#b
  28. (n,m#(-off)#n:(*x),l#b;(l+r+2*lst)_b)}
  29. dcm:{0 4_'(~0x00000000~4#*|:) blks[*flg]/("";**(data;flg;rest):0 1 2_frm x)}
  30. /
  31. // compression
  32. :t:"this his repeats because repeating helps"
  33. /:t:"haaaaaaaaa\0"
  34. *{ \(r;s;lb):y
  35. r:$[(x-1)<@/i:1(*>:)\(*lb)*~|/'a:z=s[+/lb]
  36. r,,((#s)-*i;i:|/'lb@\:*|i);r]
  37. (r;s,z;1 0+(0;,&z=s),'(*lb;a)@\:&0<#'a:(*|lb)@'&'a)}[4]/[(();"";(,-1;,,0));t]
  38. +(!#t;t)
  39. \