<s> from random import randint <EOL> from . ValueFuzz import StringFuzzer , IntegerFuzzer , FuzzableInteger <EOL> class XmlFuzzableAttribute ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , value , element , fileid , start , end ) : <EOL> self . name = name <EOL> self . value = value <EOL> self . element = element <EOL> self . fileid = fileid <EOL> self . start = start <EOL> self . end = end <EOL> def fuzzed_value ( self , strfuzz , intfuzz ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . value , str ) : <EOL> return strfuzz . fuzz_value ( self . value ) <EOL> else : <EOL> return intfuzz . fuzz_value ( self . value ) <EOL> def find_xmlattrs ( files ) : <EOL> """<STR_LIT>""" <EOL> attributes = [ ] <EOL> for fileid , f in enumerate ( files ) : <EOL> file_data = f [ "<STR_LIT:data>" ] <EOL> if file_data . startswith ( "<STR_LIT>" ) : <EOL> off = file_data . find ( "<STR_LIT>" ) + <NUM_LIT:2> <EOL> else : <EOL> off = <NUM_LIT:0> <EOL> try : <EOL> while True : <EOL> st = file_data . index ( "<STR_LIT:<>" , off ) <EOL> en = file_data . index ( "<STR_LIT:>>" , st ) <EOL> off = en + <NUM_LIT:1> <EOL> sp = file_data . find ( "<STR_LIT:U+0020>" , st , en ) <EOL> if sp == - <NUM_LIT:1> : <EOL> continue <EOL> element = file_data [ st + <NUM_LIT:1> : sp ] <EOL> sp += <NUM_LIT:1> <EOL> while True : <EOL> try : <EOL> eq = file_data . index ( "<STR_LIT>" , sp , en ) <EOL> name = file_data [ sp : eq ] <EOL> stq = eq + <NUM_LIT:2> <EOL> enq = file_data . index ( "<STR_LIT>" , stq , en ) <EOL> except ValueError : <EOL> break <EOL> val = file_data [ stq : enq ] <EOL> sp = enq + <NUM_LIT:2> <EOL> if name . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> try : <EOL> val = FuzzableInteger ( val ) <EOL> except ValueError : <EOL> pass <EOL> attributes . append ( XmlFuzzableAttribute ( name , val , element , fileid , stq , enq ) ) <EOL> except ValueError : <EOL> pass <EOL> return attributes <EOL> def fuzz_xmlattrs ( files , attributes , aggression , strfuzzer = None , intfuzzer = None ) : <EOL> """<STR_LIT>""" <EOL> if not aggression : <EOL> return files <EOL> if strfuzzer is None : <EOL> strfuzzer = StringFuzzer ( ) <EOL> if intfuzzer is None : <EOL> intfuzzer = IntegerFuzzer ( ) <EOL> result = [ ] <EOL> nattributes = len ( attributes ) <EOL> if aggression > <NUM_LIT:0> : <EOL> fuzzable = nattributes / aggression <EOL> else : <EOL> fuzzable = - aggression <EOL> fuzzes = sorted ( set ( randint ( <NUM_LIT:0> , nattributes - <NUM_LIT:1> ) for _ in range ( fuzzable ) ) ) <EOL> fuzzes . reverse ( ) <EOL> try : <EOL> tofuzz = attributes [ fuzzes . pop ( ) ] <EOL> except ( IndexError , AttributeError ) : <EOL> tofuzz = None <EOL> for i , cf in enumerate ( files ) : <EOL> if tofuzz is None or tofuzz . fileid != i : <EOL> result . append ( cf ) <EOL> else : <EOL> off = <NUM_LIT:0> <EOL> fileparts = [ ] <EOL> try : <EOL> while tofuzz . fileid == i : <EOL> fileparts . append ( cf [ "<STR_LIT:data>" ] [ off : tofuzz . start ] ) <EOL> off = tofuzz . end <EOL> fileparts . append ( tofuzz . fuzzed_value ( strfuzzer , intfuzzer ) ) <EOL> tofuzz = attributes [ fuzzes . pop ( ) ] <EOL> except IndexError : <EOL> tofuzz = None <EOL> fileparts . append ( cf [ "<STR_LIT:data>" ] [ off : ] ) <EOL> newf = dict ( cf ) <EOL> newf [ "<STR_LIT:data>" ] = "<STR_LIT>" . join ( str ( f ) for f in fileparts ) <EOL> result . append ( newf ) <EOL> return result </s>
