import binascii


def bin_compare_region(fn1, start1, end1, fn2, start2, end2):
  """compare region from two files

  :param fn1: the first file to compare.
  :param start1: start address of the file fn1.
  :param end1: end address of file fn1
  :param fn2: the second file to compare.
  :param start2: start address of the file fn2.
  :param end2: end address of file fn2.
  :returns rtn: True/False.
  """
  rtn = True
  s1, s2 = (end1-start1), (end2-start2)
  size_bytes = s1
  if s1 > s2: size_bytes = s2
  with open(fn1, 'rb') as f1, open(fn2, 'rb') as f2:
    f1.seek(start1)
    f2.seek(start2)
    total, bufsize = 0, 16
    while total < size_bytes:
      temp1, temp2 =f1.read(bufsize), f2.read(bufsize)
      total += 16
      if temp1 != temp2:
        print("index: 0x%x, 0x%x"%(start1+total, start2+total))
        print("%-30s"%fn1, binascii.hexlify(temp1))
        print("%-30s"%fn2, binascii.hexlify(temp2))
        rtn=False
  return rtn