简体   繁体   English

将 bash 翻译成 python; 从内容中提取特定字节(十六进制)

[英]Translating bash to python; extracting specific bytes(hex) from content

I'm trying to transform a bash script to python, and I have a piece of code that I want to understands better.我正在尝试将 bash 脚本转换为 python,并且我有一段代码想要更好地理解。 The script:剧本:

magic="$(readhex "$file" $end 6)" || break
test "$magic" = 070701 || test "$magic" = 070702 || break
namesize=0x$(readhex "$initramfs" $((end + 94)) 8)
filesize=0x$(readhex "$initramfs" $((end + 54)) 8)
end=$(((end + 110)))
end=$(((end + namesize + 3) & ~3))
end=$(((end + filesize + 3) & ~3))

Looks like is reading some hexdata from a file(or content), and getting some sizes of attributes.看起来是从文件(或内容)中读取一些十六进制数据,并获取一些大小的属性。

with open("initramfs", "rb") as f:
    byte = f.read(1)
    while byte != "":
        byte = f.read(1)

I believe I need to use seek, but it is not very clear what magic should represent.我相信我需要使用seek,但不是很清楚魔术应该代表什么。

Magic bytes are like a file signature used to determine the type of file.魔术字节就像用于确定文件类型的文件签名。 The first 6 bytes 070701 and 070702 tell the software reading the file that it is a CPIO archive, a common method of packing up the initramfs it appears to be reading.前 6 个字节 070701 和 070702 告诉读取文件的软件它是一个 CPIO 存档,这是打包它似乎正在读取的 initramfs 的常用方法。 If it was a JPEG, for example, it would be FFD8.例如,如果它是 JPEG,它将是 FFD8。

The rest of this is reading addresses and moving to them.剩下的就是读取地址并移动到它们。 A CPIO archive will have a table of the addresses of the files it contains and their metadata (name, size etc.). CPIO 存档将有一个包含文件地址及其元数据(名称、大小等)的表格。 Writing a method of unpacking archives is non-trivial and poses a potential security risk unless thoroughly tested.编写解压档案的方法并非易事,除非经过彻底测试,否则会带来潜在的安全风险。 There's lots of off the shelf Python libraries you can use to do this that will give you nice APIs with which to access the contents of various archive formats.您可以使用许多现成的 Python 库来执行此操作,这些库将为您提供用于访问各种存档格式内容的出色 API。

Should you wish to proceed you are correct in your assertion, you would use seek then read in to "end" bytes.如果您希望继续,您的断言是正确的,您可以使用 seek 然后读入“结束”字节。 The magic part is only testing it's the correct type of file that has been passed to it.神奇的部分只是测试传递给它的文件类型是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM