Skip to content

Instantly share code, notes, and snippets.

@lurch
Created March 23, 2018 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lurch/029674351674daa45511dba0d842fe4a to your computer and use it in GitHub Desktop.
Save lurch/029674351674daa45511dba0d842fe4a to your computer and use it in GitHub Desktop.
Super simple standalone script to parse a bmap file (works on Python2 or Python3)
#!/usr/bin/python
# A super-simple standalone script (which makes various assumptions, and does no
# verification) to show how easily .bmap files can be parsed.
# It only supports uncompressed images, and if the image is named mydata.img it
# assumes the bmap is named mydata.bmap
# MIT License
#
# Copyright (c) 2018 Andrew Scheller
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import xml.etree.ElementTree as ET
import re
import os
if len(sys.argv) != 3:
print("Usage: %s <raw-file> <output-file>" % os.path.basename(sys.argv[0]))
sys.exit(1)
raw_file = sys.argv[1]
output_file = sys.argv[2]
if not os.path.isfile(raw_file):
print("raw-file '%s' doesn't exist" % raw_file)
sys.exit(1)
file_root, file_ext = os.path.splitext(raw_file)
bmap_file = file_root + '.bmap'
if not os.path.isfile(bmap_file):
print("bmap-file '%s' doesn't exist" % bmap_file)
sys.exit(1)
bmap_root = ET.parse(bmap_file).getroot()
blocksize = int(bmap_root.find('BlockSize').text)
with open(raw_file, 'rb') as filedata:
with open(output_file, 'wb') as outdata:
try:
outdata.truncate(int(bmap_root.find('ImageSize').text))
except:
pass
for bmap_range in bmap_root.find('BlockMap').findall('Range'):
blockrange = bmap_range.text
m = re.match('^\s*(\d+)\s*-\s*(\d+)\s*$', blockrange)
if m:
start = int(m.group(1))
end = int(m.group(2))
else:
start = int(blockrange)
end = start
start_offset = start * blocksize
filedata.seek(start_offset, 0)
outdata.seek(start_offset, 0)
for i in range(end - start + 1):
outdata.write(filedata.read(blocksize))
outdata.flush()
os.fsync(outdata.fileno())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment