Advertisement
Guest User

Fragmenter 1

a guest
Feb 18th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. class Signature_Element(object):
  2. """docstring for sub_seq_factory"""
  3. def __init__(self):
  4. self.byte_seq_reference = None
  5. self.subseq_min_frag_len = None
  6. self.subseq_position = None
  7. self.subseq_max_offset= None
  8. self.subseq_min_offset = None
  9. self.sequence = None
  10. self.default_shift = None
  11.  
  12.  
  13. def do_grep_grammer_convert(self,sequence):
  14. """converts the sigfile notation for masks e.g. ... to DROID e.g {3}"""
  15. grep_mask_length = sequence.count(".")
  16. sequence = re.sub ("\.+"," {} ",sequence)
  17. grep_insert = "{%s}" % (str(grep_mask_length))
  18. sequence = sequence.replace("{}", grep_insert)
  19. return sequence
  20.  
  21. def do_fragmenter(self,sequence):
  22. string = 1
  23. token = 0
  24. change_next_loop = 0
  25. reserve_start_list = ["(","[","{","*","?"]
  26. reserve_stop_list = [")","]","}","*","?"]
  27. seq_word, token_word = [],[]
  28. seq_dict = {}
  29. sequence = sequence.replace(" ","")
  30. last_item = len(sequence)
  31. sequence_fragment_counter = 1
  32. seq_written_marker = 1
  33. for i, nibble in enumerate(sequence):
  34. print sequence_fragment_counter, nibble #token_word
  35. print string, token, change_next_loop
  36. ## stream switcher ##
  37. if seq_written_marker != sequence_fragment_counter:
  38. if string == 1:
  39. label = "string::"
  40. else:
  41. label = "token::"
  42. seq_dict[sequence_fragment_counter] = label+token_word
  43. seq_written_marker += 1
  44.  
  45. if nibble in reserve_start_list and change_next_loop == 1:
  46. sequence_fragment_counter += 1
  47. token_word = "".join(token_word)
  48. seq_dict[sequence_fragment_counter] = "token::"+token_word
  49. token_word=[]
  50.  
  51. if i == (last_item-1):
  52. if string == 1:
  53. seq_word.append(nibble)
  54. else:
  55. token_word.append(nibble)
  56. token_word = "".join(token_word)
  57. if token_word != "":
  58. seq_dict[sequence_fragment_counter] = "token::"+token_word
  59. seq_word = "".join(seq_word)
  60. if seq_word != "":
  61. seq_dict[sequence_fragment_counter] = "string::"+seq_word
  62. else:
  63. if change_next_loop == 1 and nibble not in reserve_start_list:
  64. string = 1
  65. token = 0
  66. change_next_loop = 0
  67. elif nibble in reserve_start_list:
  68. string = 0
  69. token = 1
  70. change_next_loop = 0
  71. if nibble in reserve_stop_list:
  72. change_next_loop = 1
  73.  
  74. ## split stream maker ##
  75. if string == 1:
  76. seq_word.append(nibble)
  77. if token_word != []:
  78. token_word = "".join(token_word)
  79. seq_dict[sequence_fragment_counter] = "token::"+token_word
  80. sequence_fragment_counter += 1
  81. token_word = []
  82. else:
  83. token_word.append(nibble)
  84. if seq_word != []:
  85. seq_word = "".join(seq_word)
  86. seq_dict[sequence_fragment_counter] = "string::"+seq_word
  87. sequence_fragment_counter += 1
  88. seq_word = []
  89.  
  90. print seq_dict
  91.  
  92.  
  93. def main():
  94. sig= Signature_Element()
  95. sig.sequence = "524946(46|58){4}434452367672736E"
  96. sig.offset = "0"
  97. sig.sequence = sig.do_grep_grammer_convert(sig.sequence)
  98. sig.sequence = sig.do_fragmenter(sig.sequence)
  99.  
  100. if __name__ == '__main__':
  101. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement