git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Library for parsing binary structures


On 2019-03-28 11:07:22 +0100, dieter wrote:
> Paul Moore <p.f.moore at gmail.com> writes:
> > My real interest is in whether any libraries exist to do this sort
> > of thing (there are plenty of parser libraries for text, pyparsing
> > being the obvious one, but far fewer for binary structures).
> 
> Sure. *BUT* the library must fit your specific binary structure.
> How should a general libary know how to interpret your
> specific "type byte"s or that "(" introduces a homogenous
> sequence of given length which must be terminated by ")"?

Obviously you need some way to describe the specific binary format you
want to parse - in other words, a grammar. The library could then use
the grammar to parse the input - either by interpreting it directly, or
by generating (Python) code from it. The latter has the advantage that
it has to be done only once, not every time you want to parse a file.

If that sounds familiar, it's what yacc does. Except that it does it for
text files, not binary files. I am not aware of any generic binary
parser generator for Python. I have read research papers about such
generators for (I think) C and Java, but I don't remember the names and
I'm not sure if the generators got beyond the proof of concept stage.

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20190329/758bcd4f/attachment.sig>