2021-01-12 23:11:00 +11:00
-- Copyright (C) 2018-2021 by KittenOS NEO contributors
--
-- Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
-- THIS SOFTWARE.
2020-03-30 08:43:38 +11:00
-- BDIVIDE (r5 edition) and PREPROC (r9 edition)
-- decompression engine for installer
2020-03-31 21:59:58 +11:00
$ bdPPBuffer = " "
$ bdBDBuffer = " "
$ bdBDWindow = ( " \x00 " ) : rep ( 2 ^ 16 )
2020-03-30 08:43:38 +11:00
-- High-level breakdown:
2020-03-31 21:59:58 +11:00
-- q is unescaper.
2020-03-30 08:43:38 +11:00
-- It'll only begin to input if at least 3 bytes are available,
-- so you'll want to throw in 2 extra zeroes at the end of stream as done here.
2020-03-31 03:47:27 +11:00
-- It uses t (input buffer) and p (output buffer).
2020-03-30 08:43:38 +11:00
-- Ignore its second argument, as that's a lie, it's just there for a local.
2020-03-31 03:47:27 +11:00
-- L is the actual decompressor. It has the same quirk as q, wanting two more bytes.
-- It stores to c (compressed), and w (window).
-- It outputs that which goes to the window to q also.
2020-03-30 08:43:38 +11:00
-- And it also uses a fake local.
-- SEE compress.lua FOR THIS FUNCTION
2020-03-31 21:59:58 +11:00
function $ bdPP ( x , y )
2020-03-30 08:43:38 +11:00
if x == 126 then
return string.char ( y ) , 3
elseif x == 127 then
return string.char ( 128 + y ) , 3
elseif x >= 32 then
return string.char ( x ) , 2
elseif x == 31 then
return " \n " , 2
elseif x == 30 then
return " \x00 " , 2
end
2020-03-31 03:47:27 +11:00
return string.char ( ( " enart " ) : byte ( x % 5 + 1 ) , ( " ndtelh " ) : byte ( ( x - x % 5 ) / 5 + 1 ) ) , 2
2020-03-30 08:43:38 +11:00
end
2020-04-01 03:52:24 +11:00
$ {
function $ engineInput ( $ L | lData )
$ bdBDBuffer = $ bdBDBuffer .. $ lData
2020-03-31 21:59:58 +11:00
while # $ bdBDBuffer > 2 do
2020-04-01 03:52:24 +11:00
$ lData = $ bdBDBuffer : byte ( )
if $ lData < 128 then
$ lData = $ bdBDBuffer : sub ( 1 , 1 )
2020-03-31 21:59:58 +11:00
$ bdBDBuffer = $ bdBDBuffer : sub ( 2 )
2020-03-30 08:43:38 +11:00
else
2020-04-01 03:52:24 +11:00
$ {
$ L | bdBDPtr = $ bdBDBuffer : byte ( 2 ) * 256 + $ bdBDBuffer : byte ( 3 ) + 1
$ lData = $ bdBDWindow : sub ( $ bdBDPtr , $ bdBDPtr + $ lData - 125 )
2020-03-31 21:59:58 +11:00
$ bdBDBuffer = $ bdBDBuffer : sub ( 4 )
2020-04-01 03:52:24 +11:00
$ }
2020-03-31 21:59:58 +11:00
end
2020-04-01 03:52:24 +11:00
$ bdPPBuffer = $ bdPPBuffer .. $ lData
$ bdBDWindow = ( $ bdBDWindow .. $ lData ) : sub ( - 2 ^ 16 )
2020-03-31 21:59:58 +11:00
while # $ bdPPBuffer > 1 do
2020-04-01 03:52:24 +11:00
$ {
$ lData , $ L | bdPPAdv = $ bdPP ( $ bdPPBuffer : byte ( ) , $ bdPPBuffer : byte ( 2 ) )
2020-03-31 21:59:58 +11:00
$ bdPPBuffer = $ bdPPBuffer : sub ( $ bdPPAdv )
2020-04-01 03:52:24 +11:00
$ }
$ engineOutput ( $ lData )
2020-03-30 08:43:38 +11:00
end
end
end
2020-04-01 03:52:24 +11:00
$ }
2020-03-30 08:43:38 +11:00