Guest User

Untitled

a guest
Sep 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. > A file checker that determines MIME types by using the magic numbers.
  2.  
  3.  
  4. #### Usage
  5. > index html. contains the text method and arrayBufferMethod.html contains the array buffer method. Open either in your browser.
  6. Once opened, simply the input button and upload one of the video file types:
  7.  
  8. * video/mp4
  9. * video/quicktime
  10. * video/x-msvideo
  11. * video/x-ms-wmv
  12. * video/webm
  13. * video/ogg
  14.  
  15. ## Approaches:
  16. #### 1. Read first 4 bytes as Text.
  17. This method is 30% faster. I measured the two methods with console.time/console.timeEnd 30 times. See results in excel sheet attached to this submission or email.
  18.  
  19. Steps:
  20.  
  21. * The first four bytes of a binary file contains a file signature describing the file's type (also known as the "magic number")*
  22. * The program slices the first 4 bytes from the uploaded video File as Blob.
  23. * The Blob is read as an encoded UTF-16 text string and iterated over.
  24. * Through each iteration we append get a hold of the byte, conver it to a hexidecimal string, store it in a variable, and use the switch statement to append any lost "leading 0's'" that were truncated when stored in the variable. Finally we concatenate the string to the empty Magic Numbers string.
  25. * The final string result will contain the magic numbers! They are in an order different than you would typically see these bytes because of endianness(or the sequential order multiple bytes are arranged when read by the processor).
  26.  
  27.  
  28. #### 2. Read first 4 bytes as an Array Buffer
  29. This method is slower but will give you at least the typical order you would normally see these bytes.
  30.  
  31. Steps:
  32. * The first four bytes of the video file are sliced off and turned into a blob that is read as an array buffer.
  33. * We view store this blob in the array buffer but can't actually view it. We convert the buffer into a UTF-8 Integer typed array.
  34. * We then iterate through this typed array, append any "leading 0's'" that were lost, and convert one byte at a time into a hexidecimal string.
  35. * Finally, we concatinate each byte into a final string. The final string will contain the magic numbers. These are in order because we are storing and reading data one byte at a time as opposed to two bytes at a time in the previous method. This way there's no endianness effect.
Add Comment
Please, Sign In to add comment