Last active
November 9, 2020 20:06
-
-
Save Cars-10/c8a7f715ab38c1ff8618f95b9872a784 to your computer and use it in GitHub Desktop.
Validate sizes of chunks in AIFF file to filesize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
from mutagen.aiff import AIFFFile | |
def validate_chunk(chunk, level=0): | |
valid = True | |
indent = " " * level | |
if hasattr(chunk, 'subchunks'): | |
print("{0}{1} {2}\t(offset: {3}, size: {4}, data-size: {5})".format( | |
indent, chunk.id, chunk.name, chunk.offset, chunk.size, | |
chunk.data_size)) | |
subchunk_size = 12 | |
for subchunk in chunk.subchunks(): | |
subchunk_size += subchunk.size | |
valid &= validate_chunk(subchunk, level + 1) | |
sizes_match = subchunk_size == chunk.size | |
valid &= sizes_match | |
print("{0}Total: {1} bytes{2}".format(indent, subchunk_size, | |
"" if sizes_match else " !SIZE MISMATCH!")) | |
else: | |
print("{0}{1}\t(offset: {2}, size: {3}, data-size: {4})".format( | |
indent, chunk.id, chunk.offset, chunk.size, chunk.data_size)) | |
return valid | |
def main(): | |
f = open(sys.argv[1], 'rb') | |
print("Filename: " + sys.argv[1]) | |
aiff = AIFFFile(f) | |
valid = validate_chunk(aiff.root) | |
f.seek(0, 2) | |
size = f.tell() | |
sizes_match = size == aiff.root.size | |
diff = size - aiff.root.size | |
valid &= sizes_match | |
print("\nFile size: {0} bytes{1}".format( | |
size, "" if sizes_match else " !SIZE MISMATCH!")) | |
print("Difference in size: {0} Bytes\n".format(0 if sizes_match else diff)) | |
sys.exit(0 if valid else 1) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment