hecky

Verificar si un chunk IDAT del PNG tiene errores

Nov 22nd, 2011
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import binascii, sys
  3.  
  4. def crc2hex(calcular):
  5.     crc32 = binascii.crc32(calcular)   
  6.     final=''
  7.     for i in range(4):
  8.         tmp=crc32 & 0xFF
  9.         crc32 >>= 8
  10.         final='%02X%s' % (tmp, final)
  11.     return final
  12.  
  13. png = open(sys.argv[1],"rb").read()
  14. print str(png.count("IDAT"))+" IDAT encontrados:\n"
  15. nIDAT = 0
  16. errores = 0
  17. for i in range(0,len(png),1):
  18.     if(png[i:i+4]=="\x49\x44\x41\x54"):
  19.         nIDAT += 1
  20.         lenchunk = png[i-4:i]
  21.         longitud = int(lenchunk.encode("hex"),16)
  22.         offsetCRC = (i+4)+longitud
  23.         crc32sum = crc2hex(png[i:offsetCRC])
  24.         chunkcrc32 = png[offsetCRC:offsetCRC+4]
  25.         chunkcrc32 = str(chunkcrc32.encode("hex")).upper()
  26.         if(crc32sum != chunkcrc32):
  27.             errores += 1
  28.             print "\nError en el chunk IDAT #"+str(nIDAT)+" en la posicion "+str(int(i))+str(" (offset ")+str("%X" % i)+str(")")
  29.             print "\n\t.-Longitud = "+str(longitud)+" bytes"
  30.             print "\t.-CRC32 = "+chunkcrc32+"\tDeberia ser = "+crc32sum+"\n"
  31. if(errores == 0):
  32.     print "No se encontraron errores =)"
  33. elif(errores == 1):
  34.     print "\n\t\t<< 1 error encontrado >>"
  35. else:
  36.     print "\n\t\t<< "+str(errores)+" errores encontrados >>"
  37.  
Advertisement
Add Comment
Please, Sign In to add comment