1import lzma
2import os
3import random
4
5def create_valid_xz_files(directory, num_files=10):
6    for i in range(num_files):
7        file_content = os.urandom(random.randint(100, 10000))  # Random binary content
8        file_path = os.path.join(directory, f'valid_file_{i}.xz')
9
10        with lzma.open(file_path, 'wb', preset=random.choice([0, 9])) as f:
11            f.write(file_content)
12
13def create_malformed_xz_files(directory, num_files=10):
14    for i in range(num_files):
15        valid_file_path = random.choice(os.listdir(directory))
16        with open(os.path.join(directory, valid_file_path), 'rb') as f:
17            content = f.read()
18
19        malformed_content = corrupt_data(content)
20
21        with open(os.path.join(directory, f'malformed_file_{i}.xz'), 'wb') as f:
22            f.write(malformed_content)
23
24def corrupt_data(data):
25    # Introduce random corruption in data
26    index = random.randint(0, len(data) - 1)
27    corrupted_data = data[:index] + random.randint(0, 255).to_bytes(1, 'little') + data[index + 1:]
28    return corrupted_data
29
30def main():
31    corpus_directory = 'xz_corpus'
32    os.makedirs(corpus_directory, exist_ok=True)
33
34    # Create valid .xz files
35    create_valid_xz_files(corpus_directory, num_files=50)
36
37    # Create malformed .xz files
38    create_malformed_xz_files(corpus_directory, num_files=50)
39
40if __name__ == '__main__':
41    main()
42