#!/usr/bin/python

import sys

if len(sys.argv) < 4 or len(sys.argv) > 5:
  print('usage: %s IN OUT BEGIN [END]' % sys.argv[0])
  print(' note: BEGIN and END should be hex values')
  print(' note: if END is ommited, IN is read until the end of file')
  sys.exit(1)

infile = open(sys.argv[1], 'rb')
outfile = open(sys.argv[2], 'wb')
begin = int(sys.argv[3], 16)

infile.seek(begin)

if len(sys.argv) == 5:
  end = int(sys.argv[4], 16)
  outfile.write(infile.read(end - begin))
else:
  outfile.write(infile.read())
