<s> """<STR_LIT>""" <EOL> import collections <EOL> import hashlib <EOL> if not ( hasattr ( __builtins__ , "<STR_LIT>" ) ) or str is bytes : <EOL> def bytes ( var , * args ) : <EOL> try : <EOL> return '<STR_LIT>' . join ( map ( chr , var ) ) <EOL> except TypeError : <EOL> return map ( ord , var ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> def rsyncdelta ( datastream , remotesignatures , blocksize = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> remote_weak , remote_strong = remotesignatures <EOL> match = True <EOL> matchblock = - <NUM_LIT:1> <EOL> deltaqueue = collections . deque ( ) <EOL> while True : <EOL> if match and datastream is not None : <EOL> window = collections . deque ( bytes ( datastream . read ( blocksize ) ) ) <EOL> checksum , a , b = weakchecksum ( window ) <EOL> try : <EOL> matchblock = remote_weak . index ( checksum , matchblock + <NUM_LIT:1> ) <EOL> stronghash = hashlib . sha256 ( bytes ( window ) ) . hexdigest ( ) <EOL> matchblock = remote_strong . index ( stronghash , matchblock ) <EOL> match = True <EOL> deltaqueue . append ( matchblock ) <EOL> if datastream . closed : <EOL> break <EOL> continue <EOL> except ValueError : <EOL> match = False <EOL> try : <EOL> if datastream : <EOL> newbyte = ord ( datastream . read ( <NUM_LIT:1> ) ) <EOL> window . append ( newbyte ) <EOL> except TypeError : <EOL> newbyte = <NUM_LIT:0> <EOL> tailsize = datastream . tell ( ) % blocksize <EOL> datastream = None <EOL> if datastream is None and len ( window ) <= tailsize : <EOL> deltaqueue . append ( window ) <EOL> break <EOL> oldbyte = window . popleft ( ) <EOL> checksum , a , b = rollingchecksum ( oldbyte , newbyte , a , b , blocksize ) <EOL> try : <EOL> deltaqueue [ - <NUM_LIT:1> ] . append ( oldbyte ) <EOL> except ( AttributeError , IndexError ) : <EOL> deltaqueue . append ( [ oldbyte ] ) <EOL> deltastructure = [ blocksize ] <EOL> for element in deltaqueue : <EOL> if isinstance ( element , int ) : <EOL> deltastructure . append ( element ) <EOL> elif element : <EOL> deltastructure . append ( bytes ( element ) ) <EOL> return deltastructure <EOL> def blockchecksums ( instream , blocksize = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> weakhashes = list ( ) <EOL> stronghashes = list ( ) <EOL> read = instream . read ( blocksize ) <EOL> while read : <EOL> weakhashes . append ( weakchecksum ( bytes ( read ) ) [ <NUM_LIT:0> ] ) <EOL> stronghashes . append ( hashlib . sha256 ( read ) . hexdigest ( ) ) <EOL> read = instream . read ( blocksize ) <EOL> return weakhashes , stronghashes <EOL> def patchstream ( instream , outstream , delta ) : <EOL> """<STR_LIT>""" <EOL> blocksize = delta [ <NUM_LIT:0> ] <EOL> for element in delta [ <NUM_LIT:1> : ] : <EOL> if isinstance ( element , int ) and blocksize : <EOL> instream . seek ( element * blocksize ) <EOL> element = instream . read ( blocksize ) <EOL> outstream . write ( element ) <EOL> def rollingchecksum ( removed , new , a , b , blocksize = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> a -= removed - new <EOL> b -= removed * blocksize - a <EOL> return ( b << <NUM_LIT:16> ) | a , a , b <EOL> def weakchecksum ( data ) : <EOL> """<STR_LIT>""" <EOL> a = b = <NUM_LIT:0> <EOL> l = len ( data ) <EOL> for i in range ( l ) : <EOL> a += data [ i ] <EOL> b += ( l - i ) * data [ i ] <EOL> return ( b << <NUM_LIT:16> ) | a , a , b </s>
