import os import sys import json # a=os.path.split("E:\\abc/def.bin") # a=os.path.splitext("E:\\abc/def/") # print(a) def _bytes_to_int(d:bytearray): ret=0 num=len(d) for i in d: ret>>=8 ret|=i<<(8*(num-1)) return ret def _bytes_to_str(d:bytearray): d=d.replace(b'\0xff',b'\0x00') text=d.strip(b'\0x00').decode("utf-8") return text def _detail_elf(name:str): with open(name,"rb") as f: data=f.read() off_index=16+4*4 sh_off=_bytes_to_int(data[off_index:off_index+4]) off_index+=2*4+3*2 sh_size=_bytes_to_int(data[off_index:off_index+2]) off_index+=2 sh_num=_bytes_to_int(data[off_index:off_index+2]) addr=sh_off+sh_num*sh_size size=len(data) ret=[" |文件类型:批检仪/赋码仪主板程序"] ret+=[" |打包文件为:"] while (addr>0)&0x7f model=(id>>7)&0x1f chip=(id>>12)&0xf day=(id>>16)&0x1f month=(id>>21)&0xf year=((id>>25)&0x7f)+2022 s="{y}-{m}-{d},chip:{chip},model:{model},id:{id}".format(y=year,m=month,d=day,chip=chip,model=model,id=sid) return s with open(name,"rb") as f: j=json.loads(f.read()) if(file_name!="cfg.json"): ret=[" |文件类型:检测方案文件"] ret.append(" |方案描述:{d}".format(d=j["PlanBrief"])) ret.append(" |方案ID:{id}".format(id=j["PlanID"])) ret.append(" |方案ID解析:{s}".format(s=scheme_decode_id(j["PlanID"]))) else: ret=[" |文件类型:批检仪/赋码仪主板配置文件"] return ret def _detail_sh(name:str): ret=[" |文件类型:批检仪/赋码仪主板程序启动脚本"] return ret def _detail_lua(name:str): ret=[" |文件类型:批检仪/赋码仪检测结果判定脚本"] return ret def _detail_pkt(name:str): ret=[" |文件类型:MCU程序在线升级文件"] with open(name,"rb") as f: data=f.read() ret.append(" |打包时间:{d}".format(d=_bytes_to_str(data[4:4+20]))) ret.append(" |主机接口:{d}".format(d=_bytes_to_str(data[40:40+8]))) ret.append(" |设备类型:{d}".format(d=_bytes_to_str(data[48:48+12]))) return ret def _detail_py(name:str): return ["暂未实现"] def _detail_service(name:str): return ["暂未实现"] def _detail_dtb(name:str): return ["暂未实现"] _fun_table={ ".elf":_detail_elf, ".axf":_detail_axf, ".bin":_detail_bin, ".json":_detail_json, ".sh":_detail_sh, ".lua":_detail_lua, ".pkt":_detail_pkt, ".py":_detail_py, ".service":_detail_service, ".dtb":_detail_dtb } def detail(path:str): tailfix=os.path.splitext(path) file_name=os.path.split(path) fun=_fun_table[tailfix[-1]] ret=[file_name[-1]] if(fun is not None): ret+=fun(path) return ret ret+=["未找到此类文件的解析器"] return ret if __name__ == "__main__": print(_bytes_to_int([0x12,0x34,0x56,0x78])) print(_bytes_to_int([0x12,0x34]))