pyChecknum.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import shutil
  5. import argparse
  6. import struct
  7. import binascii
  8. def intToBytes(value, length):
  9. result = []
  10. for i in range(0, length):
  11. result.append(value >> (i * 8) & 0xff)
  12. result.reverse()
  13. return result
  14. #查看文件后的16进制数据
  15. def ReadBinFile(in_file, type="sum32", en='little', block_size=1024):
  16. filename, file_type = os.path.splitext(in_file)
  17. out_name = None
  18. checknum_bytes = None
  19. # 方式一:累加和
  20. if type == "sum32":
  21. out_name0 = filename+file_type+".sum32"
  22. f = open(in_file, "rb")
  23. o = open(out_name0, "wb+")
  24. if f and o: #二进制打开文件
  25. checknum_all = 0
  26. if block_size:
  27. while True:
  28. # 读一块
  29. datas = f.read(block_size)
  30. if len(datas) == 0:
  31. break
  32. # 这一块的checknum
  33. checknum = 0
  34. for j in range(len(datas)):
  35. checknum += datas[j]
  36. checknum_all += datas[j] # 总文件的checknum
  37. checknum_bytes = checknum.to_bytes(4, en) #"little", "big" # 大端序
  38. # 写入数据和checknum
  39. o.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
  40. o.write(datas)
  41. o.write(bytes(checknum_bytes))
  42. else:
  43. while True:
  44. datas = f.read(1)
  45. if len(datas) == 0:
  46. break
  47. checknum_all += datas[0] # 总文件的checknum
  48. o.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
  49. o.write(datas)
  50. f.close()
  51. o.close()
  52. checknum_all_bytes = checknum_all.to_bytes(4, en) #"little", "big" # 大端序
  53. checknum_all_str = checknum_all_bytes.hex()
  54. print(in_file, os.path.getsize(in_file), "checknum", checknum_all_str)
  55. # 输出文件名
  56. out_name1 = filename+file_type+".sum32["+checknum_all_str+"]"
  57. shutil.move(out_name0, out_name1)
  58. # 追加到文件最后
  59. with open(out_name1, "rb+") as f:
  60. f.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
  61. f.write(bytes(checknum_all_bytes))
  62. f.close()
  63. elif type == "crc32":
  64. # 方式2:crc32
  65. out_name0 = filename+file_type+".crc32"
  66. f = open(in_file, "rb")
  67. o = open(out_name0, "wb+")
  68. if f and o: #二进制打开文件
  69. checknum_all = 0
  70. if block_size:
  71. while True:
  72. # 读一块
  73. datas = f.read(block_size)
  74. if len(datas) == 0:
  75. break
  76. # 这一块的checknum
  77. checknum = binascii.crc32(datas)
  78. checknum_bytes = checknum.to_bytes(4, en) #"little", "big" # 大端序
  79. # 写入数据和checknum
  80. o.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
  81. o.write(datas)
  82. o.write(bytes(checknum_bytes))
  83. else:
  84. datas = f.read()
  85. o.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
  86. o.write(datas)
  87. f.seek(0,0)
  88. datas = f.read()
  89. checknum_all = binascii.crc32(datas)
  90. f.close()
  91. o.close()
  92. checknum_all_bytes = checknum_all.to_bytes(4, en) #"little", "big" # 大端序
  93. checknum_all_str = checknum_all_bytes.hex()
  94. print(in_file, os.path.getsize(in_file), "checknum", checknum_all_str)
  95. # 输出文件名
  96. out_name1 = filename+file_type+".crc32["+checknum_all_str+"]"
  97. shutil.move(out_name0, out_name1)
  98. # 追加到文件最后
  99. with open(out_name1, "rb+") as f:
  100. f.seek(0,2) #位移到最后 SEEK_END(值为2) SEEK_CUR(值为1) SEEK_SET(值为0)
  101. f.write(bytes(checknum_all_bytes))
  102. f.close()
  103. if __name__ == "__main__":
  104. parser = argparse.ArgumentParser()
  105. parser.add_argument("n", help="bin文件名,譬如 COAB21232D.bin")
  106. parser.add_argument("-t", help="校验方式:sum32,crc32, 默认sum32")
  107. parser.add_argument("-e", help="校验码大小端:little,big, 默认little")
  108. parser.add_argument("-s", help="分块大小:0,1024,2048, 默认0不分块")
  109. args = parser.parse_args()
  110. project = str(args.n)
  111. type = "sum32" #默认 累加和
  112. en = "little" #默认 checknum 小端
  113. block_size = 0 #默认 0
  114. if args.t:
  115. type = str(args.t)
  116. if args.e:
  117. en = str(args.e)
  118. if args.s:
  119. block_size = int(args.s)
  120. # print(project, type, en)
  121. ReadBinFile(project, type, en, block_size)