#! /usr/bin/env python # -*- coding: utf-8 -*- """ Created on Fri Jun 9 15:54:32 2017 @author: LY """ import os, struct, sys def usage(argv0): print(""" Usage: %s sparse_image_file [align_unit_K] """ % ( argv0 )) sys.exit(-1) def main(): me = os.path.basename(sys.argv[0]) if len(sys.argv) < 2: print("Error: Not enough param") usage(me) in_file_path = sys.argv[1] out_file_path = in_file_path + ".out" if len(sys.argv) > 2: align_unit = int(sys.argv[2]) else: align_unit = 1024 print("in=%s out=%s align=%u" % (in_file_path, out_file_path, align_unit)) try: fin = open(in_file_path, 'rb') fout = open(out_file_path, 'wb') head_bin = fin.read(28) head_struct = list(struct.unpack(" remain_count: if offset == 0: fout.write(in_chunk) if chunk_type == 0xCAC1: fout.write(fin.read(data_sz)) elif chunk_type == 0xCAC2: fout.write(fill_bin) else: if chunk_struct[0] != 0xCAC3: fout.write(struct.pack("<2H2I",*chunk_struct)) fout.write(buffer[0:offset]) else: chunk_struct[3] = 12 fout.write(struct.pack("<2H2I",*chunk_struct)) if chunk_type == 0xCAC2: fin.seek(-16,os.SEEK_CUR) else: fin.seek(-12,os.SEEK_CUR) i -= 1 out_total_chunks += 1 offset = 0 elif data_sz == remain_count: if offset == 0: fout.write(in_chunk) if chunk_type == 0xCAC1: fout.write(fin.read(data_sz)) elif chunk_type == 0xCAC2: fout.write(fill_bin) else: if chunk_type == 0xCAC1:#raw chunk if chunk_struct[0] != 0xCAC1: chunk_struct[0] = 0xCAC1 chunk_struct[2] += chunk_sz chunk_struct[3] += data_sz buffer[offset:] = fin.read(data_sz) elif chunk_type == 0xCAC2:#fill chunk if chunk_struct[0] != 0xCAC1: chunk_struct[0] = 0xCAC1 chunk_struct[2] += chunk_sz chunk_struct[3] += data_sz buffer[offset:] = [(fill_value % 256) for j in range(data_sz)] elif chunk_type == 0xCAC3:#don't care chunk buffer[offset:] = [0 for j in range(data_sz)] chunk_struct[2] += chunk_sz chunk_struct[3] += data_sz offset += data_sz remain_count -= data_sz if chunk_struct[0] == 0xCAC3: chunk_struct[3] = 12 fout.write(struct.pack("<2H2I",*chunk_struct)) if chunk_struct[0] != 0xCAC3: fout.write(buffer) out_total_chunks += 1 offset = 0 else: if chunk_type == 0xCAC1:#raw chunk if chunk_struct[0] != 0xCAC1: chunk_struct[0] = 0xCAC1 chunk_struct[2] += chunk_sz chunk_struct[3] += data_sz buffer[offset:offset + data_sz] = fin.read(data_sz) elif chunk_type == 0xCAC2:#fill chunk if chunk_struct[0] != 0xCAC1: chunk_struct[0] = 0xCAC1 chunk_struct[2] += chunk_sz chunk_struct[3] += data_sz buffer[offset:offset + data_sz] = [(fill_value % 256) for j in range(data_sz)] elif chunk_type == 0xCAC3:#don't care chunk buffer[offset:offset + data_sz] = [0 for j in range(data_sz)] chunk_struct[2] += chunk_sz chunk_struct[3] += data_sz offset += data_sz remain_count -= data_sz i += 1 if offset > 0: if chunk_struct[0] == 0xCAC3: chunk_struct[3] = 12 fout.write(struct.pack("<2H2I",*chunk_struct)) if chunk_struct[0] != 0xCAC3: fout.write(buffer) out_total_chunks += 1 offset = 0; fout.seek(0, os.SEEK_SET) head_struct[7] = out_total_chunks fout.write(struct.pack("