<s> from struct import pack , unpack <EOL> from bitarray import bitarray <EOL> class CompressedIO : <EOL> def __init__ ( s , data ) : <EOL> s . b = bitarray ( ) <EOL> s . b . frombytes ( data [ : : - <NUM_LIT:1> ] ) <EOL> s . pos = <NUM_LIT:0> <EOL> def read ( s , length ) : <EOL> data = s . b [ s . pos : s . pos + length ] <EOL> s . pos += length <EOL> return data <EOL> def read01 ( s , length ) : <EOL> return s . read ( length ) . to01 ( ) <EOL> def readnum ( s , length ) : <EOL> return int ( s . read01 ( length ) , <NUM_LIT:2> ) <EOL> def readbyte ( s , length = <NUM_LIT:1> ) : <EOL> return s . read ( length * <NUM_LIT:8> ) . tobytes ( ) <EOL> def tell ( s ) : <EOL> return s . pos <EOL> def seek ( s , offset , t = <NUM_LIT:0> ) : <EOL> if t == <NUM_LIT:0> : <EOL> s . pos = offset <EOL> elif t == <NUM_LIT:1> : <EOL> s . pos += offset <EOL> elif t == <NUM_LIT:2> : <EOL> s . pos = len ( data ) - <NUM_LIT:1> - offset <EOL> def close ( s ) : <EOL> s . b = None <EOL> def deflate_levels ( ) : <EOL> for v in [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:8> ] : <EOL> yield v <EOL> while True : <EOL> yield <NUM_LIT:8> <EOL> from contextlib import nested <EOL> def deflate_crilayla ( data , size , feed ) : <EOL> MINIMAL_REFLEN = <NUM_LIT:3> <EOL> with nested ( closing ( CompressedIO ( data ) ) , closing ( StringIO ( ) ) ) as ( f , out ) : <EOL> while True : <EOL> feed ( f . tell ( ) >> <NUM_LIT:3> , out . tell ( ) ) <EOL> bit = f . read01 ( <NUM_LIT:1> ) <EOL> if bit == '<STR_LIT>' : <EOL> break <EOL> if int ( bit , <NUM_LIT:2> ) : <EOL> offset = f . readnum ( <NUM_LIT> ) + MINIMAL_REFLEN <EOL> refc = MINIMAL_REFLEN <EOL> for lv in deflate_levels ( ) : <EOL> bits = f . read ( lv ) <EOL> refc += int ( bits . to01 ( ) , <NUM_LIT:2> ) <EOL> if not bits . all ( ) : <EOL> break <EOL> assert out . tell ( ) >= offset <EOL> while refc > <NUM_LIT:0> : <EOL> out . seek ( - offset , <NUM_LIT:1> ) <EOL> ref = out . read ( refc ) <EOL> out . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> out . write ( ref ) <EOL> refc -= len ( ref ) <EOL> else : <EOL> b = f . readbyte ( ) <EOL> out . write ( b ) <EOL> return out . getvalue ( ) [ : size ] [ : : - <NUM_LIT:1> ] <EOL> def uncompress ( data , uncompressed_size , extract_size , file_size ) : <EOL> header = data [ : <NUM_LIT> ] <EOL> ( <EOL> marker , uncompressed_size , datasize <EOL> ) = unpack ( '<STR_LIT>' , header ) <EOL> compressed_data = data [ <NUM_LIT> : <NUM_LIT> + datasize ] <EOL> raw_data_header = data [ <NUM_LIT> + datasize : <NUM_LIT> + datasize + <NUM_LIT> ] <EOL> assert marker == '<STR_LIT>' <EOL> assert datasize + <NUM_LIT> == file_size - <NUM_LIT> <EOL> assert uncompressed_size + <NUM_LIT> == extract_size <EOL> data = deflate_crilayla ( compressed_data , uncompressed_size ) <EOL> assert len ( data ) == uncompressed_size <EOL> return raw_data_header + data </s>
