Skip to content

Instantly share code, notes, and snippets.

View generalmimon's full-sized avatar

Petr Pučil generalmimon

  • Czech Republic
View GitHub Profile
@generalmimon
generalmimon / expr_gen.py
Last active March 29, 2024 17:46
Python script to generate Kaitai Struct expressions
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2024 Petr Pucil <petr.pucil@seznam.cz>
#
# SPDX-License-Identifier: MIT
"""
expr_gen.py - Python script to generate a pair of files `[expr_gen.ksy,
expr_gen.kst]` with various Kaitai Struct expressions, designed to test that
there are no missing parentheses in the translated expressions in any target
@generalmimon
generalmimon / bench_bytes_terminate.py
Last active September 28, 2023 11:57
bytes_terminate() benchmark for the Kaitai Struct Python runtime library: https://github.com/kaitai-io/kaitai_struct_python_runtime/blob/d457617/kaitaistruct.py#L447
"""
Usage:
$ richbench --markdown .
You need to install https://pypi.org/project/richbench/ first:
$ python -m pip install -U richbench
"""
@generalmimon
generalmimon / ks-serialization-notes.md
Last active December 29, 2022 19:21
Kaitai Struct — serialization

Kaitai Struct — serialization

Notes on individual features

  • fixed contents — mapped to a literal byte array (note: shouldn't it write magic1() instead? but then it would have to be checked again so that it doesn't violate consistency):

        public void _read() {
            this.magic1 = this._io.readBytes(6);

if (!(Arrays.equals(magic1(), new byte[] { 80, 65, 67, 75, 45, 49 }))) {

/**
* SPDX-FileCopyrightText: 2022 Petr Pucil <petr.pucil@seznam.cz>
*
* SPDX-License-Identifier: Apache-2.0
*/
"use strict";
// Base `readBitsIntLe` implemenation: https://github.com/kaitai-io/kaitai_struct_javascript_runtime/blob/abf5542/KaitaiStream.js#L486-L525
// Diff between `readBitsIntLe` and `readBitsIntLeV2` is as follows:
@generalmimon
generalmimon / float-manual-vs-native.php
Created February 11, 2022 14:37
Test which PHP versions support {e,E,g,G} types in unpack
<?php
// See https://3v4l.org/
declare(strict_types=1);
interface FloatImpl {
public function readF4be(string $bytes): float;
public function readF8be(string $bytes): float;
public function readF4le(string $bytes): float;
public function readF8le(string $bytes): float;
/**
* SPDX-FileCopyrightText: 2021 Petr Pucil <petr.pucil@seznam.cz>
*
* SPDX-License-Identifier: Unlicense
*/
// for <https://www.tcpdump.org/linktypes.html>
function assertEqual(actual, expected, message) {
message || (message = null);
# See https://github.com/kaitai-io/kaitai_struct_formats/blob/e4f724e/common/vlq_base128_le.ksy
puts [
groups[0].value,
len >= 2 ? (groups[1].value << 7) : nil,
len >= 3 ? (groups[2].value << 14) : nil,
len >= 4 ? (groups[3].value << 21) : nil,
len >= 5 ? (groups[4].value << 28) : nil,
len >= 6 ? (groups[5].value << 35) : nil,
len >= 7 ? (groups[6].value << 42) : nil,
len >= 8 ? (groups[7].value << 49) : nil,
@generalmimon
generalmimon / rpm_read_tags_md.py
Created October 10, 2021 21:00
Python script to read RPM tags.md file and print KSY enum definition of the tags
import re
from collections import OrderedDict
tag_regex = r"\* (\w+) \((\d+)\)"
heading_regex = r"^## (.+)"
sections = {}
# https://github.com/rpm-software-management/rpm/raw/911448f2/doc/manual/tags.md
with open('tags.md', 'r', encoding='utf-8') as f:
contents = f.read()
@generalmimon
generalmimon / nested-ternary-2000-levels-too-much-recursion.js
Last active September 23, 2021 21:41
JavaScript test code with a ternary operator expression with 2000 levels triggering the "too much recursion" error in Chrome and Firefox
/**
* SPDX-FileCopyrightText: 2021 Petr Pucil <petr.pucil@seznam.cz>
*
* SPDX-License-Identifier: CC0-1.0
*/
(function() {
var v = 1;
return (
v > 1 ? "a" :
@generalmimon
generalmimon / test-edid-bitwise-ops.py
Last active August 3, 2021 09:51
Python test code of bitwise operations for serializing EDID integer layouts
# SPDX-FileCopyrightText: 2021 Petr Pucil <petr.pucil@seznam.cz>
#
# SPDX-License-Identifier: CC0-1.0
# Requires Python 3.6 (see https://www.codingame.com/playgrounds/21/whats-new-in-python-3-6/pep-515-underscores-in-numeric-literals)
# I recommend running at https://tio.run/#python3
a = 0x28
b = 0x35