Parsing binary files with regular expressions 1.0

Operating systemsOS : Windows / Linux / Mac OS / BSD / Solaris
Program licensingScript Licensing : Other Free / Open Source License - Python License
CreatedCreated : Jun 6, 2007
Size downloadDownloads : 1
Program licensing
Thank you for voting...

This script allows you to use the regular expression ...

This script allows you to use the regular expression engine to parse binary files, especially those for which the struct module alone is inadequate.
The typical way to parse binary data in Python is to use the unpack method of the struct module. This works well for fixed-width field, but becomes more complicated when you need to parse variable-width fields. Perl's implementation of unpack accepts "*" as the field length, and even allows grouping with parentheses, which mitigates this problem. Python does not currently offer these features. Although you can dynamically generate a format string for unpack with a lot of slicing and calls to calcsize, the resulting code will likely be hard to read and error-prone.
Fortunately, in some cases there is a simpler way to do it: use the regular expression engine to grab each field, and use struct. unpack on the results.
First, you construct a regular expression (RE) describing the entire record structure, grouping each field you'd like to extract with parentheses, and compile it.
To create the regular expression, you just have to remember that one character in the RE equals one byte in the record. So, the expression ". . " would match any short (2 bytes). To match a variable-width field, the RE
engine will have to be able to recognize where the field ends. In a null-terminated string, for example, the field ends with a zero byte. You'd therefore look for any number of characters followed by a null byte: "(. *? )". Note the use of the non-greedy qualifier "? " -- this way, we only match up to the first null, rather than the last null in the buffer.
When compiling, make sure to pass the re. DOTALL flag to the compiler, or it will consider bytes that happen to match ASCII '' to be newlines. Then, you use the findall method of the compiled expression object on your buffer. findall finds all non-overlapping matches, one match for each record. It returns a list of tuples, one for each match; each tuple will contain one element for each field you grouped in the RE.
You still need to unpack the fields in the tuples before using them, since they're still strings rather than usable values. Generally, you'll call unpack once for each field, with only one format character. (You can also group multiple consecutive fixed fields in one set of parentheses in the RE, and then unpack them in one call. But that may get confusing. )
The code above demonstrates how to unpack a binary file that has an indeterminate number of variable-width records, each consisting of a little-endian short, a null-terminated string, and two more shorts. It drops the resulting values into a list and also into a dictionary.
This technique is useful when your variable-width fields are terminated with a sentinel, such as the zero-terminated strings described above. If your field length is embedded in the data, and you can't use the "p" (Pascal string) modifier, you'll probably have to resort to slicing the buffer up manually.
This technique is also applicable even if your fields are all fixed-width. The findall method will operate on the entire buffer at once with a single regular expression, which saves you from having to dynamically create a long format string encapsulating all your data, or alternatively iterating over slices of the buffer.

Parsing binary files with regular expressions 1.0 scripting tags: unpack, expression, binary files parser, field, files management, regular expressions. What is new in Parsing binary files with regular expressions 1.0 software script? - Unable to find Parsing binary files with regular expressions 1.0 news. What is improvements are expecting? Newly-made Parsing binary files with regular expressions 1.1 will be downloaded from here. You may download directly. Please write the reviews of the Parsing binary files with regular expressions. License limitations are unspecified.