Construct (python Library)
   HOME

TheInfoList



OR:

Construct is a
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
library for the construction and deconstruction of
data structures In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, a ...
in a declarative fashion. In this context, construction, or building, refers to the process of converting (
serializing In computing, serialization (or serialisation) is the process of translating a data structure or object state into a format that can be stored (e.g. files in secondary storage devices, data buffers in primary storage devices) or transmitted ( ...
) a programmatic object into a binary representation. Deconstruction, or parsing, refers to the opposite process of converting (deserializing) binary data into a programmatic object. Being declarative means that user code defines the data structure, instead of the convention of writing
procedural code Procedural programming is a programming paradigm, derived from imperative programming, based on the concept of the ''procedure call''. Procedures (a type of routine or subroutine) simply contain a series of computational steps to be carried ...
to accomplish the goal. Construct can work seamlessly with
bit The bit is the most basic unit of information in computing and digital communications. The name is a portmanteau of binary digit. The bit represents a logical state with one of two possible values. These values are most commonly represente ...
- and
byte The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit ...
-level data granularity and various byte-ordering. Using declarative code has many benefits. For example, the same code that can parse can also build (symmetrical), debugging and testing are much simpler (provable to some extent), creating new constructs is easy (wrapping components), and many more. If one is familiar with the
C (programming language) C (''pronounced like the letter c'') is a General-purpose language, general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly ref ...
, one can think of constructs as
casting Casting is a manufacturing process in which a liquid material is usually poured into a mold, which contains a hollow cavity of the desired shape, and then allowed to solidify. The solidified part is also known as a ''casting'', which is ejected ...
from char * to struct foo * and vice versa, rather than writing code that unpacks the data.


Example

The following example show how a
TCP/IP The Internet protocol suite, commonly known as TCP/IP, is a framework for organizing the set of communication protocols used in the Internet and similar computer networks according to functional criteria. The foundational protocols in the suit ...
protocol stack The protocol stack or network stack is an implementation of a computer networking protocol suite or protocol family. Some of these terms are used interchangeably but strictly speaking, the ''suite'' is the definition of the communication protoco ...
might be defined using Construct. Some code is omitted for brevity and simplicity. Also note that the following code is just Python code that creates objects. First, the
Ethernet Ethernet () is a family of wired computer networking technologies commonly used in local area networks (LAN), metropolitan area networks (MAN) and wide area networks (WAN). It was commercially introduced in 1980 and first standardized in 198 ...
header (layer 2): ethernet = Struct( "destination" / Bytes(6), "source" / Bytes(6), "type" / Enum(Int16ub, IPv4=0x0800, ARP=0x0806, RARP=0x8035, X25=0x0805, IPX=0x8137, IPv6=0x86DD, ), ) Next, the IP header (layer 3): ip = Struct( "header" / BitStruct( "version" / Const(Nibble, 4), "header_length" / Nibble, ), "tos" / BitStruct( "precedence" / Bytes(3), "minimize_delay" / Flag, "high_throuput" / Flag, "high_reliability" / Flag, "minimize_cost" / Flag, Padding(1), ), "total_length" / Int16ub, # ... ) And finally, the TCP header (layer 4): tcp = Struct( "source" / Int16ub, "destination" / Int16ub, "seq" / Int32ub, "ack" / Int32ub, # ... ) Now define the hierarchy of the protocol stack. The following code "binds" each pair of adjacent protocols into a separate unit. Each such unit will "select" the proper next layer based on its contained protocol. layer4tcp = Struct( tcp, # ... payload ) layer3ip = Struct( ip, "next" / Switch(this.protocol, ), ) layer2ethernet = Struct( ethernet, "next" / Switch(this.type, { "IP" : layer3ip, } ), ) At this point, the code can parse captured TCP/IP frames into "packet" objects and build these packet objects back into binary representation. tcpip_stack = layer2ethernet packet = tcpip_stack.parse(b"...raw captured packet...") raw_data = tcpip_stack.build(packet)


Ports and spin-offs


Perl

Data::ParseBinary
is a
CPAN The Comprehensive Perl Archive Network (CPAN) is a repository of over 250,000 software modules and accompanying documentation for 39,000 distributions, written in the Perl programming language by over 12,000 contributors. ''CPAN'' can denote eith ...
module that originated as a port of Construct to the Perl programming language. (se
its main POD document
for its inspiration). Since the initial version, some parts of the original API have been deprecated.


Java

A port to Java is available o
GitHub
Examples in Java, the
Ethernet Ethernet () is a family of wired computer networking technologies commonly used in local area networks (LAN), metropolitan area networks (MAN) and wide area networks (WAN). It was commercially introduced in 1980 and first standardized in 198 ...
header (layer 2): Construct ethernet_header = Struct("ethernet_header", MacAddress("destination"), MacAddress("source"), Enum(UBInt16("type"), "IPv4", 0x0800, "ARP", 0x0806, "RARP", 0x8035, "X25", 0x0805, "IPX", 0x8137, "IPv6", 0x86DD, "_default_", Pass ));


External links


Construct's repository

Construct's documentation
Python (programming language) libraries Parser generators