| 1 | + | #!/usr/bin/env python3 |
| 2 | + | # coding: UTF-8 |
| 3 | + | |
| 4 | + | import argparse |
| 5 | + | import binascii |
| 6 | + | import colorama |
| 7 | + | import pypdf |
| 8 | + | import io |
| 9 | + | |
| 10 | + | from colorama import Fore, Back, Style |
| 11 | + | from PIL import Image, ImageDraw, ImageFont |
| 12 | + | |
| 13 | + | def make_png_data(text, byte_size): |
| 14 | + | image = Image.new('RGB', (516, 729), (255, 255, 255)) # B5, White |
| 15 | + | draw = ImageDraw.Draw(image) |
| 16 | + | draw.text((10, 10), text, fill=(0, 0, 0)) # Black |
| 17 | + | output = io.BytesIO() |
| 18 | + | image.save(output, format='png') |
| 19 | + | png_data = output.getvalue() |
| 20 | + | |
| 21 | + | if len(png_data) > byte_size: |
| 22 | + | with open('dummy.png', 'wb') as f: |
| 23 | + | f.write(png_data) |
| 24 | + | return None |
| 25 | + | |
| 26 | + | # IEND chunk is the last chunk of a PNG file |
| 27 | + | iend_type_index = png_data.find(b'IEND') |
| 28 | + | if iend_type_index == -1: |
| 29 | + | print(Fore.RED + 'Error: IEND chunk not found.') |
| 30 | + | return None |
| 31 | + | |
| 32 | + | # The first 4 bytes of the IEND chunk are data length |
| 33 | + | # IEND chunk type (4 bytes) follows IEND chunk data length (4 bytes) |
| 34 | + | iend_chunk_index = iend_type_index - 4 |
| 35 | + | |
| 36 | + | # 12 = 4 (chunk length) + 4 (chunk type) + 4 (chunk CRC) |
| 37 | + | extra_chunk_length = byte_size - len(png_data) - 12 |
| 38 | + | # private chunk type |
| 39 | + | extra_chunk_type = b'exTr' |
| 40 | + | extra_chunk_data = b'\x00' * extra_chunk_length |
| 41 | + | extra_chunk_crc = binascii.crc32(extra_chunk_type + extra_chunk_data) |
| 42 | + | extra_chunk = extra_chunk_length.to_bytes(4, byteorder='big') |
| 43 | + | extra_chunk += (extra_chunk_type + extra_chunk_data) |
| 44 | + | extra_chunk += extra_chunk_crc.to_bytes(4, byteorder='big') |
| 45 | + | |
| 46 | + | added_png_data = png_data[:iend_chunk_index] + extra_chunk + png_data[iend_chunk_index:] |
| 47 | + | return added_png_data |
| 48 | + | |
| 49 | + | def parse_args(): |
| 50 | + | colorama.init(autoreset=True) |
| 51 | + | parser = argparse.ArgumentParser(description='Create a dummy file of the specified size.') |
| 52 | + | parser.add_argument('-type', '--type', help='File type', choices=['png', 'pdf', 'txt'], required=True) |
| 53 | + | parser.add_argument('-text', '--text', help='Text to be written in the file') |
| 54 | + | parser.add_argument('-size', '--size', help='File size (bytes)', type=int) |
| 55 | + | args = parser.parse_args() |
| 56 | + | if args.type == 'png': |
| 57 | + | added_png_data = make_png_data(args.text, args.size) |
| 58 | + | with open('dummy.png', 'wb') as f: |
| 59 | + | f.write(added_png_data) |
| 60 | + | else: |
| 61 | + | print(Fore.RED + 'Error: Invalid file type.') |
| 62 | + | parser.print_help() |
| 63 | + | |
| 64 | + | if __name__ == '__main__': |
| 65 | + | colorama.init(autoreset=True) |
| 66 | + | png_data = make_png_data('sample png', 400001) |
| 67 | + | with open('dummy.png', 'wb') as f: |
| 68 | + | f.write(png_data) |
| 69 | + | |