Skip to content

Instantly share code, notes, and snippets.

View generalmimon's full-sized avatar

Petr Pučil generalmimon

  • Czech Republic
View GitHub Profile
#!/usr/bin/env ruby
=begin
Usage:
$ ruby bench-bytes_terminate.rb
You need to install https://rubygems.org/gems/benchmark-ips first:
$ gem install benchmark-ips
"""
Usage:
$ richbench --times TIMES --markdown .
You need to install https://pypi.org/project/richbench/ first:
$ python -m pip install -U richbench
"""
@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 <[email protected]>
#
# 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
"""
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 <[email protected]>
*
* 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 <[email protected]>
*
* 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()