Skip to content

Instantly share code, notes, and snippets.

@moyix
Created March 8, 2024 20:57
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 moyix/64092a3f2568ea2da28836e8f0cef3ba to your computer and use it in GitHub Desktop.
Save moyix/64092a3f2568ea2da28836e8f0cef3ba to your computer and use it in GitHub Desktop.
Claude's random GIF generator, based only on the GIF89a spec
from typing import BinaryIO
import random
import struct
def generate_random_input(out: BinaryIO):
# Generate Header
out.write(b'GIF89a') # GIF signature and version
# Generate Logical Screen Descriptor
screen_width = random.randint(1, 65535)
screen_height = random.randint(1, 65535)
packed_fields = random.randint(0, 255)
background_color_index = random.randint(0, 255)
pixel_aspect_ratio = random.randint(0, 255)
out.write(struct.pack('<HHBBB', screen_width, screen_height, packed_fields,
background_color_index, pixel_aspect_ratio))
# Generate Global Color Table (optional)
if packed_fields & 0x80:
color_table_size = 2 ** ((packed_fields & 0x07) + 1)
for _ in range(color_table_size):
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
out.write(struct.pack('BBB', red, green, blue))
# Generate Image Descriptor and Image Data (optional, multiple)
for _ in range(random.randint(0, 5)):
# Generate Image Descriptor
left = random.randint(0, screen_width - 1)
top = random.randint(0, screen_height - 1)
width = random.randint(1, screen_width - left)
height = random.randint(1, screen_height - top)
packed_fields = random.randint(0, 255)
out.write(struct.pack('<BHHHHB', 0x2C, left, top, width, height, packed_fields))
# Generate Local Color Table (optional)
if packed_fields & 0x80:
color_table_size = 2 ** ((packed_fields & 0x07) + 1)
for _ in range(color_table_size):
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
out.write(struct.pack('BBB', red, green, blue))
# Generate Image Data
lzw_minimum_code_size = random.randint(2, 8)
out.write(struct.pack('B', lzw_minimum_code_size))
data_block_size = random.randint(1, 255)
out.write(struct.pack('B', data_block_size))
out.write(bytes(random.randint(0, 255) for _ in range(data_block_size)))
out.write(b'\x00') # Block Terminator
# Generate Extensions (optional, multiple)
for _ in range(random.randint(0, 5)):
extension_label = random.choice([0xF9, 0xFE, 0x01, 0xFF])
out.write(struct.pack('BB', 0x21, extension_label))
if extension_label == 0xF9: # Graphic Control Extension
packed_fields = random.randint(0, 255)
delay_time = random.randint(0, 65535)
transparent_color_index = random.randint(0, 255)
out.write(struct.pack('<BBHBB', 4, packed_fields, delay_time,
transparent_color_index, 0))
elif extension_label == 0xFE: # Comment Extension
comment_data = b''.join(bytes([random.randint(1, 255)]) +
bytes(random.randint(0, 255) for _ in range(random.randint(1, 255)))
for _ in range(random.randint(1, 5)))
out.write(comment_data)
out.write(b'\x00') # Block Terminator
elif extension_label == 0x01: # Plain Text Extension
text_grid_left = random.randint(0, screen_width - 1)
text_grid_top = random.randint(0, screen_height - 1)
text_grid_width = random.randint(1, screen_width - text_grid_left)
text_grid_height = random.randint(1, screen_height - text_grid_top)
character_cell_width = random.randint(1, 255)
character_cell_height = random.randint(1, 255)
text_foreground_color_index = random.randint(0, 255)
text_background_color_index = random.randint(0, 255)
out.write(struct.pack('<BHHHHBBBB', 12, text_grid_left, text_grid_top,
text_grid_width, text_grid_height,
character_cell_width, character_cell_height,
text_foreground_color_index, text_background_color_index))
plain_text_data = b''.join(bytes([random.randint(1, 255)]) +
bytes(random.randint(0, 255) for _ in range(random.randint(1, 255)))
for _ in range(random.randint(1, 5)))
out.write(plain_text_data)
out.write(b'\x00') # Block Terminator
elif extension_label == 0xFF: # Application Extension
application_identifier = bytes(random.randint(32, 255) for _ in range(8))
application_authentication_code = bytes(random.randint(0, 255) for _ in range(3))
out.write(struct.pack('<B8s3s', 11, application_identifier, application_authentication_code))
application_data = b''.join(bytes([random.randint(1, 255)]) +
bytes(random.randint(0, 255) for _ in range(random.randint(1, 255)))
for _ in range(random.randint(1, 5)))
out.write(application_data)
out.write(b'\x00') # Block Terminator
# Generate Trailer
out.write(b'\x3B') # GIF Trailer
import sys
for f in sys.argv[1:]:
with open(f,'wb') as of:
generate_random_input(of)
print(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment