Skip to content

Instantly share code, notes, and snippets.

@dehowell
Created May 5, 2012 11:16
Show Gist options
  • Save dehowell/2601637 to your computer and use it in GitHub Desktop.
Save dehowell/2601637 to your computer and use it in GitHub Desktop.
Python script for searching a large binary file for .NEF file headers.
#!/usr/bin/env python
import sys
# .NEF (Nikon Electronic Format) headers
SIGNATURE = '\x4d\x4d\x00\x2a\x00\x00\x00\x08\x00'
def read_chunks(stream, overlap = len(SIGNATURE), size = 100 * 2 ** 20):
'''Read chunks from the stream with a small overlap.'''
data = stream.read(size)
while True:
yield data
new_data = stream.read(size)
if new_data == '':
break
data = data[-overlap:] + new_data
f = open(sys.argv[1])
for chunk in read_chunks(f):
n = chunk.count(SIGNATURE)
if n > 0:
print 'Found %d potential NEF file headers near chunk starting at %d' % (n, f.tell())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment