#!/usr/bin/python3 # Copyright 2022 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. import itertools import struct import sys PAGE_SIZE = 0x1000 # System page size. THRESHOLD = 0x2000 # Minimum size of the file to be aligned. # Read 2 bytes. def read16(data, offset): return struct.unpack_from("= THRESHOLD and page_offset != 0: padding = PAGE_SIZE - page_offset out.extend(itertools.repeat(0x00, padding)) out_offset += padding # Put the new offset into the Table of Contents. write32(out, offset + 4, out_offset - header_size) # Copy the content of the file. out += data[data_file_offset : data_file_offset + size] out_offset += size return out if __name__ == "__main__": # Check arguments. if len(sys.argv) != 3: error_str = "icualign: wrong number of arguments\n\n" help_str = "usage: icualign \n\n" sys.exit(error_str + help_str) # Extract arguments. in_filename = sys.argv[1] out_filename = sys.argv[2] # Read the input file. with open(in_filename, "rb") as in_file: data = in_file.read() # Apply padding to the file to achieve the desired alignment. out_data = pad_data(data) # Write the output file. with open(out_filename, "wb") as out_file: out_file.write(out_data)