Advertisement
S0lll0s

splitbyte.py [1.1]

Mar 7th, 2013
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """splits a file at a given delimiter"""
  4.  
  5. __author__ = "S0lll0s"
  6. __copyright__ = "Sol Bekic, 2013"
  7. __license__ = "GPL"
  8. __version__ = "1.1.0"
  9.  
  10. import sys
  11.  
  12. def splitbyte( file, pattern, verbose=False ):
  13.  check = 0
  14.  while 1:
  15.   byte = file.read( len( pattern ) )
  16.  
  17.   if verbose: print "checking", byte
  18.   if byte == '':
  19.    if verbose: print 'Nothing found'
  20.    break
  21.  
  22.   if check > 0: # we need to check the next 'check' bytes
  23.    if pattern[-check:] in byte[ :check ]: # we found the rest
  24.     file.seek( -len( pattern ) + check, 1 ) # rewind to the end of the match
  25.     break
  26.  
  27.   if pattern in byte: # "full" match, we're at the end of the match already
  28.    break
  29.  
  30.   check = 0
  31.   for offset in range( 1, len( pattern ) ):
  32.    if pattern[:-offset] in byte:
  33.     if verbose: print "found with offset", offset
  34.     check = offset
  35.     break
  36.  
  37.  if verbose: print "found: "
  38.  return file.read()
  39.  
  40. if __name__ == "__main__":
  41.  import argparse
  42.  
  43.  parser = argparse.ArgumentParser( description='split files at a given delimiter' )
  44.  
  45.  parser.add_argument( "file", help='the file to search in' )
  46.  parser.add_argument( "delimiter", help='delimiter, in hex notation' )
  47.  parser.add_argument( "-v", "--verbose", action='store_true', help='verbose output' )
  48.  parser.add_argument( "-d", "--direct", action='store_true', help='directly use the delimiter (no hex)' )
  49.  
  50.  args = parser.parse_args()
  51.  
  52.  file = open( args.file, 'r' )
  53.  pattern = ( args.delimiter if args.direct else str( bytearray.fromhex( args.delimiter ) ) )
  54.  
  55.  if args.verbose: print "Pattern: ", ' '.join( hex( ord( x ) )[2:] for x in pattern )
  56.  print splitbyte( file, pattern, args.verbose )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement