<s> '''<STR_LIT>''' <EOL> import logging <EOL> import os <EOL> import tempfile <EOL> import time <EOL> import numpy <EOL> import shutil <EOL> import random <EOL> import hashlib <EOL> import itertools <EOL> from . . import debuggers <EOL> from . . fuzztools import hamming , filetools , probability , text <EOL> from . . fuzztools . filetools import delete_files , write_file <EOL> from . . file_handlers . basicfile import BasicFile <EOL> from . . file_handlers . tmp_reaper import TmpReaper <EOL> from . errors import MinimizerError <EOL> try : <EOL> from . . analyzers import pin_calltrace <EOL> from . . analyzers import AnalyzerEmptyOutputError <EOL> from . . debuggers . output_parsers . calltracefile import Calltracefile <EOL> except ImportError : <EOL> pass <EOL> logger = logging . getLogger ( __name__ ) <EOL> MAX_OTHER_CRASHES = <NUM_LIT:20> <EOL> class Minimizer : <EOL> use_watchdog = False <EOL> def __init__ ( self , cfg = None , crash = None , crash_dst_dir = None , <EOL> seedfile_as_target = False , bitwise = False , confidence = <NUM_LIT> , <EOL> logfile = None , tempdir = None , maxtime = <NUM_LIT> , preferx = False , keep_uniq_faddr = False , watchcpu = False ) : <EOL> if not cfg : <EOL> raise MinimizerError ( '<STR_LIT>' ) <EOL> if not crash : <EOL> raise MinimizerError ( '<STR_LIT>' ) <EOL> self . cfg = cfg <EOL> self . crash = crash <EOL> self . seedfile_as_target = seedfile_as_target <EOL> self . bitwise = bitwise <EOL> self . preferx = preferx <EOL> self . max_time = maxtime <EOL> self . use_timer = maxtime > <NUM_LIT:0.0> <EOL> self . start_time = <NUM_LIT:0.0> <EOL> self . keep_uniq_faddr = keep_uniq_faddr <EOL> self . watchcpu = watchcpu <EOL> self . minchar = '<STR_LIT:x>' <EOL> self . save_others = True <EOL> self . ext = self . crash . fuzzedfile . ext <EOL> self . logger = None <EOL> self . log_file_hdlr = None <EOL> logger . setLevel ( logging . INFO ) <EOL> if tempdir and os . path . isdir ( tempdir ) : <EOL> self . tempdir = tempfile . mkdtemp ( prefix = '<STR_LIT>' , dir = tempdir ) <EOL> else : <EOL> self . tempdir = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' , self . tempdir ) <EOL> if self . bitwise : <EOL> self . hd_func = hamming . bitwise_hd <EOL> self . swap_func = self . bitwise_swap2 <EOL> else : <EOL> self . hd_func = hamming . bytewise_hd <EOL> self . swap_func = self . bytewise_swap2 <EOL> if self . seedfile_as_target : <EOL> minf = '<STR_LIT>' % ( self . crash . fuzzedfile . root , self . crash . fuzzedfile . ext ) <EOL> minlog = os . path . join ( self . crash . fuzzedfile . dirname , '<STR_LIT>' ) <EOL> if not os . path . exists ( self . crash . seedfile . path ) : <EOL> raise MinimizerError ( '<STR_LIT>' % <EOL> self . crash . seedfile . path ) <EOL> elif self . preferx : <EOL> minf = '<STR_LIT>' % ( self . crash . fuzzedfile . root , self . minchar , self . crash . fuzzedfile . ext ) <EOL> minlog = os . path . join ( self . crash . fuzzedfile . dirname , '<STR_LIT>' % self . minchar ) <EOL> else : <EOL> minf = '<STR_LIT>' % ( self . crash . fuzzedfile . root , self . crash . fuzzedfile . ext ) <EOL> minlog = os . path . join ( self . crash . fuzzedfile . dirname , '<STR_LIT>' ) <EOL> self . outputfile = os . path . join ( self . crash . fuzzedfile . dirname , minf ) <EOL> if logfile : <EOL> self . minimizer_logfile = logfile <EOL> else : <EOL> self . minimizer_logfile = minlog <EOL> if crash_dst_dir : <EOL> self . crash_dst = crash_dst_dir <EOL> else : <EOL> self . crash_dst = self . crash . fuzzedfile . dirname <EOL> if not os . path . exists ( self . crash_dst ) : <EOL> raise MinimizerError ( "<STR_LIT>" % self . crash_dst ) <EOL> if not os . path . isdir ( self . crash_dst ) : <EOL> raise MinimizerError ( "<STR_LIT>" % self . crash_dst ) <EOL> self . debugger = debuggers . get ( ) <EOL> self . _logger_setup ( ) <EOL> self . logger . info ( "<STR_LIT>" , self . crash . fuzzedfile . path ) <EOL> if not os . path . exists ( self . crash . fuzzedfile . path ) : <EOL> raise MinimizerError ( "<STR_LIT>" % self . crash . fuzzedfile . path ) <EOL> self . crash . set_debugger_template ( '<STR_LIT>' ) <EOL> self . other_crashes = { } <EOL> self . target_size_guess = <NUM_LIT:1> <EOL> self . total_tries = <NUM_LIT:0> <EOL> self . total_misses = <NUM_LIT:0> <EOL> self . consecutive_misses = <NUM_LIT:0> <EOL> self . discard_chance = <NUM_LIT:0> <EOL> self . debugger_runs = <NUM_LIT:0> <EOL> self . min_found = False <EOL> self . confidence_level = confidence <EOL> self . newfuzzed_hd = <NUM_LIT:0> <EOL> self . newfuzzed_md5 = None <EOL> self . n_misses_allowed = <NUM_LIT:0> <EOL> self . newfuzzed = '<STR_LIT>' <EOL> self . crash_sigs_found = { } <EOL> self . files_tried = { } <EOL> self . files_tried_at_hd = { } <EOL> self . files_tried_singlebyte_at_hd = { } <EOL> self . bytemap = [ ] <EOL> self . fuzzed = self . _read_fuzzed ( ) <EOL> self . seed = self . _read_seed ( ) <EOL> if len ( self . seed ) != len ( self . fuzzed ) : <EOL> raise MinimizerError ( '<STR_LIT>' % ( len ( self . seed ) , len ( self . fuzzed ) ) ) <EOL> self . start_distance = self . hd_func ( self . seed , self . fuzzed ) <EOL> self . min_distance = self . start_distance <EOL> f = tempfile . NamedTemporaryFile ( '<STR_LIT:wb>' , delete = True , <EOL> dir = self . tempdir , <EOL> prefix = "<STR_LIT>" , <EOL> suffix = self . crash . fuzzedfile . ext ) <EOL> f . close ( ) <EOL> self . tempfile = f . name <EOL> filetools . copy_file ( self . crash . fuzzedfile . path , self . tempfile ) <EOL> self . debugger_timeout = self . cfg . debugger_timeout <EOL> self . crash_hashes = [ ] <EOL> self . measured_dbg_time = None <EOL> self . _set_crash_hashes ( ) <EOL> if self . measured_dbg_time : <EOL> self . debugger_timeout = min ( self . debugger_timeout , self . measured_dbg_time ) <EOL> self . logger . info ( '<STR_LIT>' , self . debugger_timeout ) <EOL> self . logger . info ( '<STR_LIT>' , self . confidence_level ) <EOL> self . logger . info ( '<STR_LIT>' , self . start_distance ) <EOL> def __enter__ ( self ) : <EOL> if not self . _is_crash_to_minimize ( ) : <EOL> msg = '<STR_LIT>' <EOL> self . logger . info ( msg ) <EOL> raise MinimizerError ( msg ) <EOL> if self . _is_already_minimized ( ) : <EOL> msg = '<STR_LIT>' <EOL> self . logger . info ( msg ) <EOL> raise MinimizerError ( msg ) <EOL> if self . crash . debugger_missed_stack_corruption : <EOL> msg = '<STR_LIT>' <EOL> self . logger . info ( msg ) <EOL> raise MinimizerError ( msg ) <EOL> self . start_time = time . time ( ) <EOL> return self <EOL> def __exit__ ( self , etype , value , traceback ) : <EOL> self . log_file_hdlr . close ( ) <EOL> self . logger . removeHandler ( self . log_file_hdlr ) <EOL> if not etype : <EOL> filetools . delete_files_or_dirs ( [ self . tempdir ] ) <EOL> def _read_fuzzed ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . crash . fuzzedfile . read ( ) <EOL> def _read_seed ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . seedfile_as_target : <EOL> return self . crash . seedfile . read ( ) <EOL> elif self . preferx : <EOL> return self . minchar * len ( self . fuzzed ) <EOL> else : <EOL> return text . metasploit_pattern_orig ( len ( self . fuzzed ) ) <EOL> def _logger_setup ( self ) : <EOL> dirname = os . path . dirname ( self . minimizer_logfile ) <EOL> if not os . path . exists ( dirname ) : <EOL> raise MinimizerError ( '<STR_LIT>' % dirname ) <EOL> if os . path . exists ( self . minimizer_logfile ) : <EOL> raise MinimizerError ( '<STR_LIT>' % self . minimizer_logfile ) <EOL> self . logger = logging . getLogger ( __name__ ) <EOL> self . log_file_hdlr = logging . FileHandler ( self . minimizer_logfile ) <EOL> self . logger . addHandler ( self . log_file_hdlr ) <EOL> def _set_crash_hashes ( self ) : <EOL> if not self . crash_hashes : <EOL> miss_count = <NUM_LIT:0> <EOL> max_misses = probability . misses_until_quit ( <NUM_LIT> , <NUM_LIT:0.5> ) <EOL> sigs_seen = { } <EOL> times = [ ] <EOL> while miss_count < max_misses : <EOL> ( fd , f ) = tempfile . mkstemp ( prefix = '<STR_LIT>' , text = True , dir = self . tempdir ) <EOL> os . close ( fd ) <EOL> delete_files ( f ) <EOL> start = time . time ( ) <EOL> dbg = self . run_debugger ( self . tempfile , f ) <EOL> end = time . time ( ) <EOL> delta = end - start <EOL> if dbg . is_crash : <EOL> times . append ( delta ) <EOL> current_sig = self . get_signature ( dbg , self . cfg . backtracelevels ) <EOL> if os . path . exists ( f ) : <EOL> delete_files ( f ) <EOL> if current_sig : <EOL> if sigs_seen . get ( current_sig ) : <EOL> sigs_seen [ current_sig ] += <NUM_LIT:1> <EOL> miss_count += <NUM_LIT:1> <EOL> else : <EOL> sigs_seen [ current_sig ] = <NUM_LIT:1> <EOL> miss_count = <NUM_LIT:0> <EOL> else : <EOL> miss_count += <NUM_LIT:1> <EOL> self . crash_hashes = sigs_seen . keys ( ) <EOL> avg_time = numpy . average ( times ) <EOL> stdev_time = numpy . std ( times ) <EOL> zscore = <NUM_LIT> <EOL> self . measured_dbg_time = avg_time + ( zscore * stdev_time ) <EOL> return self . crash_hashes <EOL> def run_debugger ( self , infile , outfile ) : <EOL> self . debugger_runs += <NUM_LIT:1> <EOL> cmd_args = self . cfg . get_command_args_list ( infile ) <EOL> dbg = self . debugger ( self . cfg . program , <EOL> cmd_args , <EOL> outfile , <EOL> self . debugger_timeout , <EOL> self . cfg . killprocname , <EOL> template = self . crash . debugger_template , <EOL> exclude_unmapped_frames = self . cfg . exclude_unmapped_frames , <EOL> keep_uniq_faddr = self . keep_uniq_faddr , <EOL> workingdir = self . tempdir , <EOL> watchcpu = self . watchcpu <EOL> ) <EOL> parsed_debugger_output = dbg . go ( ) <EOL> return parsed_debugger_output <EOL> def _crash_builder ( self ) : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> import copy <EOL> newcrash = copy . copy ( self . crash ) <EOL> newcrash_tmpdir = tempfile . mkdtemp ( prefix = '<STR_LIT>' , dir = self . tempdir ) <EOL> sfx = self . crash . fuzzedfile . ext <EOL> if self . crash . seedfile : <EOL> pfx = '<STR_LIT>' % self . crash . seedfile . root <EOL> else : <EOL> pfx = '<STR_LIT>' <EOL> ( fd , f ) = tempfile . mkstemp ( suffix = sfx , prefix = pfx , dir = newcrash_tmpdir ) <EOL> os . close ( fd ) <EOL> delete_files ( f ) <EOL> outfile = f <EOL> if os . path . exists ( outfile ) : <EOL> raise MinimizerError ( '<STR_LIT>' % outfile ) <EOL> self . logger . debug ( '<STR_LIT>' , self . tempfile , outfile ) <EOL> filetools . copy_file ( self . tempfile , outfile ) <EOL> newcrash . fuzzedfile = BasicFile ( outfile ) <EOL> self . logger . debug ( '<STR_LIT>' , newcrash . fuzzedfile . path , newcrash . fuzzedfile . md5 ) <EOL> newcrash . signature = None <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> newcrash . update_crash_details ( ) <EOL> shutil . rmtree ( newcrash_tmpdir ) <EOL> if os . path . exists ( newcrash_tmpdir ) : <EOL> logger . warning ( "<STR_LIT>" , newcrash_tmpdir ) <EOL> return newcrash <EOL> def get_signature ( self , dbg , backtracelevels ) : <EOL> signature = dbg . get_crash_signature ( backtracelevels ) <EOL> if dbg . total_stack_corruption : <EOL> analyzer_instance = pin_calltrace . Pin_calltrace ( self . cfg , self . crash ) <EOL> try : <EOL> analyzer_instance . go ( ) <EOL> except AnalyzerEmptyOutputError : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> if os . path . exists ( analyzer_instance . outfile ) : <EOL> calltrace = Calltracefile ( analyzer_instance . outfile ) <EOL> pinsignature = calltrace . get_crash_signature ( backtracelevels * <NUM_LIT:10> ) <EOL> if pinsignature : <EOL> signature = pinsignature <EOL> return signature <EOL> def is_same_crash ( self ) : <EOL> ( fd , f ) = tempfile . mkstemp ( dir = self . tempdir , prefix = "<STR_LIT>" ) <EOL> os . close ( fd ) <EOL> if os . path . exists ( f ) : <EOL> delete_files ( f ) <EOL> if os . path . exists ( f ) : <EOL> raise MinimizerError ( '<STR_LIT>' ) <EOL> dbg = self . run_debugger ( self . tempfile , f ) <EOL> if dbg . is_crash : <EOL> newfuzzed_hash = self . get_signature ( dbg , self . cfg . backtracelevels ) <EOL> else : <EOL> newfuzzed_hash = None <EOL> if newfuzzed_hash in self . crash_sigs_found : <EOL> self . crash_sigs_found [ newfuzzed_hash ] += <NUM_LIT:1> <EOL> elif not newfuzzed_hash : <EOL> pass <EOL> else : <EOL> self . crash_sigs_found [ newfuzzed_hash ] = <NUM_LIT:1> <EOL> self . logger . info ( '<STR_LIT>' , newfuzzed_hash , dbg . signal ) <EOL> if self . save_others and not newfuzzed_hash in self . crash_hashes : <EOL> newcrash = self . _crash_builder ( ) <EOL> if newcrash . is_crash : <EOL> self . other_crashes [ newfuzzed_hash ] = newcrash <EOL> delete_files ( dbg . file ) <EOL> if os . path . exists ( dbg . file ) : <EOL> raise MinimizerError ( '<STR_LIT>' ) <EOL> return newfuzzed_hash in self . crash_hashes <EOL> def set_discard_chance ( self ) : <EOL> new_dc = <NUM_LIT:1.0> / ( self . target_size_guess + <NUM_LIT:1.0> ) <EOL> min_dc = <NUM_LIT:1.0> - float ( self . target_size_guess ) / float ( self . min_distance ) <EOL> if new_dc >= min_dc : <EOL> return False <EOL> if not self . discard_chance == new_dc : <EOL> self . consecutive_misses = <NUM_LIT:0> <EOL> self . discard_chance = new_dc <EOL> return True <EOL> def set_n_misses ( self ) : <EOL> if self . min_found : <EOL> return False <EOL> keep_chance = <NUM_LIT:1.0> - self . discard_chance <EOL> p = probability . FuzzRun ( self . min_distance , self . target_size_guess , keep_chance ) <EOL> self . n_misses_allowed = p . how_many_misses_until_quit ( self . confidence_level ) <EOL> return True <EOL> def have_we_seen_this_file_before ( self ) : <EOL> if not self . files_tried . get ( self . newfuzzed_md5 ) : <EOL> self . files_tried [ self . newfuzzed_md5 ] = <NUM_LIT:1> <EOL> return False <EOL> self . files_tried [ self . newfuzzed_md5 ] += <NUM_LIT:1> <EOL> return True <EOL> def print_intermediate_log ( self ) : <EOL> if not self . newfuzzed_hd : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> new_hd = self . min_distance <EOL> else : <EOL> new_hd = self . newfuzzed_hd <EOL> if not self . n_misses_allowed : <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> n_misses_allowed = self . consecutive_misses <EOL> else : <EOL> n_misses_allowed = self . n_misses_allowed <EOL> parts = [ ] <EOL> parts . append ( '<STR_LIT>' % self . start_distance ) <EOL> parts . append ( '<STR_LIT>' % self . min_distance ) <EOL> parts . append ( '<STR_LIT>' % self . target_size_guess ) <EOL> parts . append ( '<STR_LIT>' % new_hd ) <EOL> parts . append ( '<STR_LIT>' % self . discard_chance ) <EOL> parts . append ( '<STR_LIT>' % ( self . consecutive_misses , n_misses_allowed ) ) <EOL> parts . append ( '<STR_LIT>' % ( self . total_misses , self . total_tries ) ) <EOL> parts . append ( '<STR_LIT>' % len ( self . crash_sigs_found . items ( ) ) ) <EOL> logstring = '<STR_LIT:U+0020>' . join ( parts ) <EOL> self . logger . info ( logstring ) <EOL> def _crash_hashes_string ( self ) : <EOL> return '<STR_LIT:U+002CU+0020>' . join ( self . crash_hashes ) <EOL> def _is_crash_to_minimize ( self ) : <EOL> return len ( self . crash_hashes ) > <NUM_LIT:0> <EOL> def _is_already_minimized ( self ) : <EOL> return self . min_distance <= <NUM_LIT:1> <EOL> def _time_exceeded ( self ) : <EOL> time_now = time . time ( ) <EOL> elapsed_time = time_now - self . start_time <EOL> return ( self . use_timer and ( elapsed_time > self . max_time ) ) <EOL> def _write_file ( self ) : <EOL> write_file ( '<STR_LIT>' . join ( self . newfuzzed ) , self . tempfile ) <EOL> def go ( self ) : <EOL> filetools . copy_file ( self . crash . fuzzedfile . path , self . outputfile ) <EOL> self . crash . fuzzedfile = BasicFile ( self . outputfile ) <EOL> self . logger . info ( '<STR_LIT>' , self . _crash_hashes_string ( ) ) <EOL> while not self . min_found : <EOL> if not self . set_discard_chance ( ) : <EOL> break <EOL> if not self . set_n_misses ( ) : <EOL> break <EOL> got_hit = False <EOL> while self . consecutive_misses <= self . n_misses_allowed : <EOL> TmpReaper ( ) . clean_tmp ( ) <EOL> if self . use_watchdog : <EOL> open ( self . cfg . watchdogfile , '<STR_LIT:w>' ) . close ( ) <EOL> if self . _time_exceeded ( ) : <EOL> logger . info ( '<STR_LIT>' ) <EOL> self . min_found = True <EOL> break <EOL> if not self . set_discard_chance ( ) : <EOL> break <EOL> if not self . set_n_misses ( ) : <EOL> break <EOL> self . swap_bytes ( ) <EOL> self . total_tries += <NUM_LIT:1> <EOL> is_repeat = self . have_we_seen_this_file_before ( ) <EOL> if not self . files_tried_at_hd . get ( self . min_distance ) : <EOL> self . files_tried_at_hd [ self . min_distance ] = { } <EOL> self . files_tried_singlebyte_at_hd [ self . min_distance ] = { } <EOL> if not self . files_tried_at_hd [ self . min_distance ] . get ( self . newfuzzed_md5 ) : <EOL> self . files_tried_at_hd [ self . min_distance ] [ self . newfuzzed_md5 ] = <NUM_LIT:1> <EOL> if self . newfuzzed_hd == ( self . min_distance - <NUM_LIT:1> ) : <EOL> self . files_tried_singlebyte_at_hd [ self . min_distance ] [ self . newfuzzed_md5 ] = <NUM_LIT:1> <EOL> else : <EOL> self . files_tried_at_hd [ self . min_distance ] [ self . newfuzzed_md5 ] += <NUM_LIT:1> <EOL> if self . newfuzzed_hd == ( self . min_distance - <NUM_LIT:1> ) : <EOL> self . files_tried_singlebyte_at_hd [ self . min_distance ] [ self . newfuzzed_md5 ] += <NUM_LIT:1> <EOL> possible_files = ( <NUM_LIT:2> ** self . min_distance ) - <NUM_LIT:2> <EOL> seen_files = len ( self . files_tried_at_hd [ self . min_distance ] ) <EOL> if seen_files == possible_files : <EOL> self . logger . info ( '<STR_LIT>' , self . min_distance ) <EOL> self . min_found = True <EOL> break <EOL> possible_singlebyte_diff_files = self . min_distance <EOL> singlebyte_diff_files_seen = len ( self . files_tried_singlebyte_at_hd [ self . min_distance ] ) <EOL> if singlebyte_diff_files_seen == possible_singlebyte_diff_files : <EOL> self . logger . info ( '<STR_LIT>' , self . min_distance ) <EOL> self . min_found = True <EOL> break <EOL> self . print_intermediate_log ( ) <EOL> if is_repeat : <EOL> self . consecutive_misses += <NUM_LIT:1> <EOL> self . total_misses += <NUM_LIT:1> <EOL> continue <EOL> if not len ( self . newfuzzed ) : <EOL> raise MinimizerError ( '<STR_LIT>' ) <EOL> self . _write_file ( ) <EOL> if self . is_same_crash ( ) : <EOL> filetools . best_effort_move ( self . tempfile , self . outputfile ) <EOL> self . crash . fuzzedfile = BasicFile ( self . outputfile ) <EOL> self . fuzzed = self . newfuzzed <EOL> self . min_distance = self . newfuzzed_hd <EOL> got_hit = True <EOL> if self . min_distance == <NUM_LIT:1> : <EOL> self . min_found = True <EOL> else : <EOL> self . consecutive_misses = <NUM_LIT:0> <EOL> if not self . set_discard_chance ( ) : <EOL> break <EOL> if not self . set_n_misses ( ) : <EOL> break <EOL> else : <EOL> self . total_misses += <NUM_LIT:1> <EOL> self . consecutive_misses += <NUM_LIT:1> <EOL> if len ( self . other_crashes ) > MAX_OTHER_CRASHES and self . seedfile_as_target : <EOL> logger . info ( '<STR_LIT>' , <EOL> MAX_OTHER_CRASHES ) <EOL> self . min_found = True <EOL> break <EOL> if not got_hit : <EOL> self . target_size_guess += <NUM_LIT:1> <EOL> self . print_intermediate_log ( ) <EOL> self . logger . info ( '<STR_LIT>' , self . _crash_hashes_string ( ) ) <EOL> for ( md5 , count ) in self . crash_sigs_found . items ( ) : <EOL> self . logger . info ( '<STR_LIT>' , md5 , count ) <EOL> if self . fuzzed : <EOL> self . bytemap = hamming . bytemap ( self . seed , self . fuzzed ) <EOL> self . logger . info ( '<STR_LIT>' , self . bytemap ) <EOL> def get_mask ( self ) : <EOL> mask = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:8> ) : <EOL> if random . random ( ) <= self . discard_chance : <EOL> mask ^= <NUM_LIT:1> << i <EOL> return mask <EOL> def swap_bytes ( self ) : <EOL> newfuzzed = [ ] <EOL> newfuzzed_hd = self . min_distance <EOL> if not <NUM_LIT:0.0> < self . discard_chance < <NUM_LIT:1.0> : <EOL> raise MinimizerError ( "<STR_LIT>" ) <EOL> while not ( <NUM_LIT:0> < newfuzzed_hd < self . min_distance ) : <EOL> newfuzzed , newfuzzed_hd = self . swap_func ( self . seed , self . fuzzed ) <EOL> self . newfuzzed = newfuzzed <EOL> self . newfuzzed_hd = newfuzzed_hd <EOL> self . newfuzzed_md5 = hashlib . md5 ( '<STR_LIT>' . join ( self . newfuzzed ) ) . hexdigest ( ) <EOL> def bytewise_swap2 ( self , seed , fuzzed ) : <EOL> swapped = [ ] <EOL> hd = <NUM_LIT:0> <EOL> append = swapped . append <EOL> rand = random . random <EOL> dc = self . discard_chance <EOL> for ( a , b ) in itertools . izip ( seed , fuzzed ) : <EOL> if a != b and rand ( ) > dc : <EOL> append ( b ) <EOL> hd += <NUM_LIT:1> <EOL> else : <EOL> append ( a ) <EOL> return swapped , hd <EOL> def _mask ( self ) : <EOL> mask = <NUM_LIT:0> <EOL> rand = random . random ( ) <EOL> dc = self . discard_chance <EOL> for i in range ( <NUM_LIT:8> ) : <EOL> if rand ( ) <= dc : <EOL> mask ^= <NUM_LIT:1> << i <EOL> return mask <EOL> def bitwise_swap2 ( self , seed , fuzzed ) : <EOL> swapped = [ ] <EOL> hd = <NUM_LIT:0> <EOL> for ( a , b ) in itertools . izip ( seed , fuzzed ) : <EOL> if a != b : <EOL> mask = self . _mask ( ) <EOL> newbyte = chr ( ( ord ( a ) & mask ) ^ ( ord ( b ) & ~ mask ) ) <EOL> hd = hamming . bitwise_hd ( a , newbyte ) <EOL> swapped . append ( newbyte ) <EOL> else : <EOL> swapped . append ( a ) <EOL> return swapped , hd </s>
