Skip to content

Instantly share code, notes, and snippets.

@darvell
Created December 13, 2011 00:29

Revisions

  1. darvell created this gist Dec 13, 2011.
    36 changes: 36 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    def CorruptHuffman(filename,chance,maxlen):
    with open(filename,"r+b") as f:
    filesize = os.path.getsize(filename)
    huffmanStart = 0
    huffmanEnd = 0
    found = 0

    for i in range(0,filesize):
    if found == 2:
    break
    f.seek(i)
    if f.read(1) == '\xFF':
    f.seek(i + 1)
    if f.read(1) == '\xC4':
    # Ok, we found a single huffman table, that's good!
    # Let's go hunt for the end of it.
    huffmanStart = i + 2
    found += 1

    found = False

    for i in range(huffmanStart + 2,filesize):
    if found == True:
    break
    f.seek(i)
    if f.read(1) == '\xFF':
    f.seek(i + 1)
    if f.read(1) != '\x00':
    huffmanEnd = i - 2
    found = True

    f.seek(random.randint(huffmanStart,huffmanEnd))
    f.write(hex(random.randint(1,250)))
    print 'Filesize: ' + str(filesize)
    print 'Huffman Start: ' + str(huffmanStart)
    print 'Huffman End: ' + str(huffmanEnd)