Chromium资源文件.pak解包方法(python)

[python]  view plain copy print ?
  1. import collections  
  2. import struct  
  3. import sys  
  4. def ReadFile(filename, encoding):  
  5.   mode = 'rb' if encoding == 0 else 'rU'  
  6.   with open(filename, mode) as f:  
  7.     data = f.read()  
  8.   if encoding not in (01):  
  9.     data = data.decode(encoding)  
  10.   return data  
  11.   
  12. PACK_FILE_VERSION = 4  
  13. HEADER_LENGTH = 2 * 4 + 1  # Two uint32s. (file version, number of entries) and  
  14.                            # one uint8 (encoding of text resources)  
  15. def UnpackDataPack(input_file):  
  16.   """Reads a data pack file and returns a dictionary."""  
  17.   data = ReadFile(input_file, 0)  
  18.   original_data = data  
  19.   
  20.   # Read the header.  
  21.   version, num_entries, encoding = struct.unpack("<IIB", data[:HEADER_LENGTH])  
  22.   if version != PACK_FILE_VERSION:  
  23.     print "Wrong file version in ", input_file  
  24.     raise WrongFileVersion  
  25.   
  26.   resources = {}  
  27.   if num_entries == 0:  
  28.     return DataPackContents(resources, encoding)  
  29.   
  30.   # Read the index and data.  
  31.   data = data[HEADER_LENGTH:]  
  32.   kIndexEntrySize = 2 + 4  # Each entry is a uint16 and a uint32.  
  33.   for _ in range(num_entries):  
  34.     id, offset = struct.unpack("<HI", data[:kIndexEntrySize])  
  35.     data = data[kIndexEntrySize:]  
  36.     next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize])  
  37.     resources[id] = original_data[offset:next_offset]  
  38.     of = open('{0}.png'.format(id),'wb')  
  39.     of.write(original_data[offset:next_offset])  
  40.     of.close()  
  41. def main():  
  42.   if len(sys.argv) > 1:  
  43.     UnpackDataPack(sys.argv[1])  
  44.   
  45.   
  46. if __name__ == '__main__':  
  47.   main()  




chromium资源文件在linux下以pak格式文件打包,文件格式很简单,并没有压缩。一般没有解包的必要,因为编译时会自动根据源文件变化而自动生成。资源内容只在运行时由chromium根据资源ID获取,所以在.pak文件中只存储了资源的ID,并没有存储资源文件类型,所以只能默认解包成png格式了。

#python unpack.py chrome_100_percent.pak
就可以解包chrome_100_percent.pak中的png图片资源了。

如果非要获取资源文件类型的话,只能从内容判断了,根据不同文件的文件头可以大致分辨类型:


[python]  view plain copy print ?
  1. import collections  
  2. import struct  
  3. import sys  
  4. def ReadFile(filename, encoding):  
  5.   mode = 'rb' if encoding == 0 else 'rU'  
  6.   with open(filename, mode) as f:  
  7.     data = f.read()  
  8.   if encoding not in (01):  
  9.     data = data.decode(encoding)  
  10.   return data  
  11.   
  12. PACK_FILE_VERSION = 4  
  13. HEADER_LENGTH = 2 * 4 + 1  # Two uint32s. (file version, number of entries) and  
  14.                            # one uint8 (encoding of text resources)  
  15. def UnpackDataPack(input_file):  
  16.   """Reads a data pack file and returns a dictionary."""  
  17.   data = ReadFile(input_file, 0)  
  18.   original_data = data  
  19.   
  20.   # Read the header.  
  21.   version, num_entries, encoding = struct.unpack("<IIB", data[:HEADER_LENGTH])  
  22.   if version != PACK_FILE_VERSION:  
  23.     print "Wrong file version in ", input_file  
  24.     raise WrongFileVersion  
  25.   
  26.   resources = {}  
  27.   if num_entries == 0:  
  28.     return DataPackContents(resources, encoding)  
  29.   
  30.   # Read the index and data.  
  31.   data = data[HEADER_LENGTH:]  
  32.   kIndexEntrySize = 2 + 4  # Each entry is a uint16 and a uint32.  
  33.   for _ in range(num_entries):  
  34.     id, offset = struct.unpack("<HI", data[:kIndexEntrySize])  
  35.     data = data[kIndexEntrySize:]  
  36.     next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize])  
  37.     resources[id] = original_data[offset:next_offset]  
  38.     filetype = 'bin'  
  39.     fileheader = ''.join(original_data[offset:offset+1])  
  40.     print ord(fileheader[0])  
  41.     if fileheader == '<':  
  42.       filetype = 'html'  
  43.     if fileheader == '\x89':  
  44.       filetype = 'png'  
  45.     elif fileheader == '/':  
  46.       filetype = 'js'  
  47.     of = open('{0}.{1}'.format(id,filetype),'wb')  
  48.     of.write(original_data[offset:next_offset])  
  49.     of.close()  
  50. def main():  
  51.   if len(sys.argv) > 1:  
  52.     UnpackDataPack(sys.argv[1])  
  53.   
  54.   
  55. if __name__ == '__main__':  
  56.   main()  

这里直接判断每个文件内容的前一个字节,如果是‘<'则应该是html文件,如果是'/'应该是js文件,如果是PNG文件头’\x89PNG',则应该就是png文件了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值