Lab 06: ctypes & Binary Protocols

Objective

Work with binary data in Python: ctypes for C structure definitions, struct for binary serialization/deserialization, array for typed C arrays, memoryview for zero-copy access, and a custom binary file format with header + records.

Background

APIs and file formats often use binary data for compactness and speed. A CSV of 10,000 products might be 500 KB; the same data in a binary protocol is 160 KB and parses 10x faster. Python's struct, ctypes, and array modules give you direct access to binary layouts without C extensions.

Time

30 minutes

Prerequisites

  • Lab 03 (Memory Management)

Tools

  • Docker: zchencow/innozverse-python:latest


Lab Instructions

Step 1: struct — Binary Pack & Unpack

💡 ! (network byte order) in struct format means big-endian — bytes stored most-significant first. Always specify byte order explicitly (!, >, <) in protocols that cross machine boundaries. Without it, the default is native byte order, which differs between x86 (little-endian) and ARM/network (big-endian).

📸 Verified Output:


Step 2: Binary File Format with Header

📸 Verified Output:


Steps 3–8: ctypes Structures, array+memoryview, Binary Search, Bitfields, Checksums, Capstone

📸 Verified Output:


Summary

Tool
Purpose
Format codes

struct.pack/unpack

C-style binary I/O

! big-end, I u32, d double, s bytes

struct.iter_unpack

Stream multiple records

Same format repeated

ctypes.Structure

C struct layout

_fields_ + ctypes types

array.array

Typed C array

'd' double, 'l' long

memoryview

Zero-copy slicing

mv[start:end]

zlib.crc32

Fast checksum

Good for file integrity

hashlib.sha256

Cryptographic integrity

Use for security-sensitive

Further Reading

Last updated