<commit_before># extract stream object to be attached to a file, to get Adobe decompression

# takes a "PDF with attached file" template
# an argument, and an object number
# inserts the extracted stream object into the template

# <NAME>, BSD Licence, 2011

#TODO: more robust tags parsing

import sys
with open("structure-attached.pdf", "rt") as f:
	template = f.read()

if len(sys.argv) == 1:
	print "attachstream.py <filename.pdf> <object number>"
	sys.exit()
with open(sys.argv[1], "rb") as f:
	source = f.read()
i = int(sys.argv[2])

off =  source.find("%i 0 obj" % i)

if off == -1:
	print "object not found"
	sys.exit()

source = source[off:]
off = source.find("stream")
stream = source[off:]
stream = stream[:stream.find("endstream") + len("endstream")]

replaceme = template[template.find("%{"): template.find("%}") + 2]
template = template.replace(replaceme, stream)

#TODO: implement parsing of the original filters
template = template.replace("%Filters", "/Filter /FlateDecode")
with open(
        sys.argv[1].replace(".pdf", "-s%i.pdf" % i), 
        "wb"
        ) as f:
	f.write(template)<commit_msg>added warning if encryption found<commit_after># extract stream object to be attached to a file, to get Adobe decompression

# takes a "PDF with attached file" template
# an argument, and an object number
# inserts the extracted stream object into the template

# <NAME>, BSD Licence, 2011

#TODO: more robust tags parsing

import sys
with open("structure-attached.pdf", "rt") as f:
	template = f.read()

if len(sys.argv) == 1:
	print "attachstream.py <filename.pdf> <object number>"
	sys.exit()
with open(sys.argv[1], "rb") as f:
	source = f.read()
i = int(sys.argv[2])

if source.find("/Encrypt") > -1:
    print "/Encrypt object found. This PDF might be encrypted, thus it will not work if it's not decrypted first."
off =  source.find("%i 0 obj" % i)

if off == -1:
	print "object not found"
	sys.exit()

source = source[off:]
off = source.find("stream")
stream = source[off:]
stream = stream[:stream.find("endstream") + len("endstream")]

replaceme = template[template.find("%{"): template.find("%}") + 2]
template = template.replace(replaceme, stream)

#TODO: implement parsing of the original filters
template = template.replace("%Filters", "/Filter /FlateDecode")
with open(
        sys.argv[1].replace(".pdf", "-s%i.pdf" % i), 
        "wb"
        ) as f:
	f.write(template)

print "object extracted and embedded successfully"
{'fraction_non_alphanumeric': 0.15089722675367048, 'fraction_numerical': 0.009380097879282219, 'mean_word_length': 4.252676659528908, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1325756', 'n_tokens_mistral': 812, 'n_tokens_neox': 784, 'n_words': 271}