什么语言之于二进制,就像 Perl 之于文本?

发布于 2024-07-24 13:07:35 字数 238 浏览 7 评论 0 原文

我正在寻找一种脚本(或更高级别的编程)语言(或例如Python或类似语言的模块)来轻松分析和操作文件中的二进制数据(例如核心转储),就像Perl允许非常顺利地操作文本文件一样。

我想做的事情包括以各种形式(二进制、十进制、十六进制)呈现任意数据块,将数据从一种字节顺序转换为另一种字节顺序等。也就是说,您通常会使用 C 或汇编来完成这些操作,但我寻找一种语言,可以快速地为高度特定的一次性目的编写小段代码。

有什么建议么?

I am looking for a scripting (or higher level programming) language (or e.g. modules for Python or similar languages) for effortlessly analyzing and manipulating binary data in files (e.g. core dumps), much like Perl allows manipulating text files very smoothly.

Things I want to do include presenting arbitrary chunks of the data in various forms (binary, decimal, hex), convert data from one endianess to another, etc. That is, things you normally would use C or assembly for, but I'm looking for a language which allows for writing tiny pieces of code for highly specific, one-time purposes very quickly.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

终难愈 2024-07-31 13:07:36

看看 python bitstring,它看起来正是你想要的:)

Take a look at python bitstring, it looks like exactly what you want :)

很酷不放纵 2024-07-31 13:07:36

Python bitstring 模块是为此目的而写的。 它允许您获取二进制数据的任意切片,并通过 Python 属性提供多种不同的解释。 它还提供了大量用于构建和修改二进制数据的工具。

例如:

>>> from bitstring import BitArray, ConstBitStream
>>> s = BitArray('0x00cf')                           # 16 bits long
>>> print(s.hex, s.bin, s.int)                       # Some different views
00cf 0000000011001111 207
>>> s[2:5] = '0b001100001'                           # slice assignment
>>> s.replace('0b110', '0x345')                      # find and replace
2                                                    # 2 replacements made
>>> s.prepend([1])                                   # Add 1 bit to the start
>>> s.byteswap()                                     # Byte reversal
>>> ordinary_string = s.bytes                        # Back to Python string

在位串中也有按位读取和导航的函数,就像在文件中一样; 事实上,这可以直接从文件中完成,无需将其读入内存:

>>> s = ConstBitStream(filename='somefile.ext')
>>> hex_code, a, b = s.readlist('hex:32, uint:7, uint:13')
>>> s.find('0x0001')         # Seek to next occurence, if found
True
    

还有具有不同字节序的视图以及交换字节序的能力等等 - 看看 手册

The Python bitstring module was written for this purpose. It lets you take arbitary slices of binary data and offers a number of different interpretations through Python properties. It also gives plenty of tools for constructing and modifying binary data.

For example:

>>> from bitstring import BitArray, ConstBitStream
>>> s = BitArray('0x00cf')                           # 16 bits long
>>> print(s.hex, s.bin, s.int)                       # Some different views
00cf 0000000011001111 207
>>> s[2:5] = '0b001100001'                           # slice assignment
>>> s.replace('0b110', '0x345')                      # find and replace
2                                                    # 2 replacements made
>>> s.prepend([1])                                   # Add 1 bit to the start
>>> s.byteswap()                                     # Byte reversal
>>> ordinary_string = s.bytes                        # Back to Python string

There are also functions for bit-wise reading and navigation in the bitstring, much like in files; in fact this can be done straight from a file without reading it into memory:

>>> s = ConstBitStream(filename='somefile.ext')
>>> hex_code, a, b = s.readlist('hex:32, uint:7, uint:13')
>>> s.find('0x0001')         # Seek to next occurence, if found
True
    

There are also views with different endiannesses as well as the ability to swap endianness and much more - take a look at the manual.

原野 2024-07-31 13:07:36

我一直使用 010 Editor 查看二进制文件。
它特别适合处理二进制文件。

它有一种易于使用的类似 C 的脚本语言来解析二进制文件并以非常可读的方式呈现它们(作为树、按颜色编码的字段等)。
有一些解析 zip 文件和 bmp 文件的示例脚本。

每当我创建二进制文件格式时,我总是为 010 编辑器编写一个小脚本来查看文件。 如果您有一些带有某些结构的头文件,那么为二进制文件创建一个读取器只需几分钟的时间。

I'm using 010 Editor to view binary files all the time to view binary files.
It's especially geared to work with binary files.

It has an easy to use c-like scripting language to parse binary files and present them in a very readable way (as a tree, fields coded by color, stuff like that)..
There are some example scripts to parse zipfiles and bmpfiles.

Whenever I create a binary file format, I always make a little script for 010 editor to view the files. If you've got some header files with some structs, making a reader for binary files is a matter of minutes.

紫﹏色ふ单纯 2024-07-31 13:07:36

任何具有打包/解包功能的高级编程语言都可以。 Perl、Python 和 Ruby 3 个都可以做到。 这是个人喜好的问题。 我在每一个中都编写了一些二进制解析,并认为 Ruby 对于这项任务来说是最简单/最优雅的。

Any high-level programming language with pack/unpack functions will do. All 3 Perl, Python and Ruby can do it. It's matter of personal preference. I wrote a bit of binary parsing in each of these and felt that Ruby was easiest/most elegant for this task.

与之呼应 2024-07-31 13:07:36

为什么不使用 C 解释器呢? 我总是使用它们来试验片段,但您可以使用它们来编写您所描述的脚本,而不需要太多麻烦。

我一直喜欢EiC。 它已经死了,但该项目最近又复活了。 EiC 的能力令人惊讶,而且速度相当快。 还有CINT。 两者都可以针对不同的平台进行编译,尽管我认为 CINT 在 Windows 上需要 Cygwin。

Why not use a C interpreter? I always used them to experiment with snippets, but you could use one to script something like you describe without too much trouble.

I have always liked EiC. It was dead, but the project has been resurrected lately. EiC is surprisingly capable and reasonably quick. There is also CINT. Both can be compiled for different platforms, though I think CINT needs Cygwin on windows.

追风人 2024-07-31 13:07:36

Python 的标准库有一些你需要的东西——特别是 array 模块让您轻松读取二进制文件的部分内容、交换字节顺序等; struct 模块允许对二进制字符串进行更细粒度的解释。 但是,两者都没有您所需要的那么丰富:例如,要以字节或半字形式呈现相同的数据,您需要在两个数组之间复制它(numpy第三方插件对于以几种不同的方式解释相同的内存区域来说更加强大),并且,例如,要以十六进制显示一些字节,没有什么太多的“捆绑”超越简单的循环或列表理解,例如 [hex(b) for b in thebytes[start:stop]]。 我怀疑有可重用的第三方模块可以进一步促进此类任务,但我无法向您指出其中一个......

Python's standard library has some of what you require -- the array module in particular lets you easily read parts of binary files, swap endianness, etc; the struct module allows for finer-grained interpretation of binary strings. However, neither is quite as rich as you require: for example, to present the same data as bytes or halfwords, you need to copy it between two arrays (the numpy third-party add-on is much more powerful for interpreting the same area of memory in several different ways), and, for example, to display some bytes in hex there's nothing much "bundled" beyond a simple loop or list comprehension such as [hex(b) for b in thebytes[start:stop]]. I suspect there are reusable third-party modules to facilitate such tasks yet further, but I can't point you to one...

眉目亦如画i 2024-07-31 13:07:36

Forth 在这方面也很擅长,但有点神秘。

Forth can also be pretty good at this, but it's a bit arcane.

病毒体 2024-07-31 13:07:36

好吧,如果速度不是考虑因素,并且您需要 Perl,那么将二进制的每一行转换为一行字符 - 0 和 1。 是的,我知道二进制中没有换行:),但大概你有一些固定的大小——例如按字节或其他单位,用它们你可以分解二进制 blob。

然后只需对该数据使用 perl 字符串处理:)

Well, if speed is not a consideration, and you want perl, then translate each line of binary into a line of chars - 0's and 1's. Yes, I know there are no linefeeds in binary :) but presumably you have some fixed size -- e.g. by byte or some other unit, with which you can break up the binary blob.

Then just use the perl string processing on that data :)

说不完的你爱 2024-07-31 13:07:36

如果您正在进行二进制级别处理,那么它的级别非常低,并且可能需要非常高效并且具有最少的依赖项/安装要求。

所以我会选择 C ​​- 可以很好地处理字节 - 你可能可以在 google 上搜索一些处理字节的库包。

使用像 Erlang 这样的东西会带来低效率、依赖性和其他你可能不希望使用低级库的包袱。

If you're doing binary level processing, it is very low level and likely needs to be very efficient and have minimal dependencies/install requirements.

So I would go with C - handles bytes well - and you can probably google for some library packages that handle bytes.

Going with something like Erlang introduces inefficiencies, dependencies, and other baggage you probably don't want with a low-level library.

滥情哥ㄟ 2024-07-31 13:07:35

我想做的事情包括以各种形式(二进制、十进制、十六进制)呈现任意数据块,将数据从一种字节顺序转换为另一种字节顺序等。也就是说,您通常会使用 C 或汇编来完成这些事情,但是我正在寻找一种语言,它允许非常快速地为高度特定的一次性目的编写小段代码。

好吧,虽然这看起来可能违反直觉,但我发现 erlang 非常适合于此,即由于其强大的支持对于模式匹配,甚至对于字节和位(称为“Erlang 位语法")。 这使得创建非常高级的程序来处理字节甚至位级别的数据检查和操作变得非常容易:

自 2001 年以来,函数式语言 Erlang 附带了面向字节的数据类型(称为二进制)以及在二进制上进行模式匹配的结构。

并引用informIT.com

(Erlang) 模式匹配真的开始变得简单
与二进制文件结合使用很有趣
类型。 考虑一个应用程序
从网络接收数据包并
然后处理它们。 中的四个字节
数据包可能是网络字节顺序
数据包类型标识符。 在 Erlang 中,你
只需要一个 processPacket
可以将其转换为的函数
内部数据结构
加工。 它看起来会有些东西
像这样:

processPacket(<<1:32/big,RestOfPacket>>) ->
    % Process type one packets
    ...
;
processPacket(<<2:32/big,RestOfPacket>>) ->
    % Process type two packets
    ...

因此,erlang 内置了对模式匹配的支持,并且它是一种函数式语言,因此具有很强的表现力,例如,请参阅 erlang 中 ueencode 的实现:

uuencode(BitStr) ->
<< (X+32):8 || <<X:6>> <= BitStr >>.
uudecode(Text) ->
<< (X-32):6 || <<X:8>> <= Text >>.

有关介绍,请参阅 Erlang 中的位级二进制文件和广义推导式。您可能还想查看以下一些指示:

Things I want to do include presenting arbitrary chunks of the data in various forms (binary, decimal, hex), convert data from one endianess to another, etc. That is, things you normally would use C or assembly for, but I'm looking for a language which allows for writing tiny pieces of code for highly specific, one-time purposes very quickly.

Well, while it may seem counter-intuitive, I found erlang extremely well-suited for this, namely due to its powerful support for pattern matching, even for bytes and bits (called "Erlang Bit Syntax"). Which makes it very easy to create even very advanced programs that deal with inspecting and manipulating data on a byte- and even on a bit-level:

Since 2001, the functional language Erlang comes with a byte-oriented datatype (called binary) and with constructs to do pattern matching on a binary.

And to quote informIT.com:

(Erlang) Pattern matching really starts to get
fun when combined with the binary
type. Consider an application that
receives packets from a network and
then processes them. The four bytes in
a packet might be a network byte-order
packet type identifier. In Erlang, you
would just need a single processPacket
function that could convert this into
a data structure for internal
processing. It would look something
like this:

processPacket(<<1:32/big,RestOfPacket>>) ->
    % Process type one packets
    ...
;
processPacket(<<2:32/big,RestOfPacket>>) ->
    % Process type two packets
    ...

So, erlang with its built-in support for pattern matching and it being a functional language is pretty expressive, see for example the implementation of ueencode in erlang:

uuencode(BitStr) ->
<< (X+32):8 || <<X:6>> <= BitStr >>.
uudecode(Text) ->
<< (X-32):6 || <<X:8>> <= Text >>.

For an introduction, see Bitlevel Binaries and Generalized Comprehensions in Erlang.You may also want to check out some of the following pointers:

分分钟 2024-07-31 13:07:35

Perl 的 pack解压 ?

perl's pack and unpack ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文