Skip to content

Instantly share code, notes, and snippets.

View mildsunrise's full-sized avatar
🦊
*rolls*

Alba Mendez mildsunrise

🦊
*rolls*
View GitHub Profile
@mildsunrise
mildsunrise / README.md
Last active January 25, 2024 01:31
JS code in the PDF for Spanish tax form 145

The Spanish government loves (or loved) using JavaScript in their tax forms.

This gist contains the JS blocks in the official tax form 145 PDF, version 1.0/2022. Enjoy.

The code is reproduced verbatim, except for normalizing the encoding (latin-1 to utf-8) and the newlines (CRLF to LF). Using some iconv command + unix2dos should give you back the exact bytes in the PDF, except for one thing: there's a stray CR without accompanying LF in object828.js:139. Yeah, idk.

@mildsunrise
mildsunrise / kleene.py
Last active September 30, 2023 15:55
Kleene's algorithm for regexes
from typing import TypeVar, Optional
from collections import defaultdict
S = TypeVar('S') # state node
def kleene_algorithm(out_edges: dict[Optional[S], dict[Optional[S], str]]) -> str:
''' calculates a regular expression that is equivalent to the NFA given by `out_edges`.
`out_edges` describes the possible transitions, indexed first by source node and then by destination node.
a transition is an input symbol in the form of a regex, but it's not limited to a single symbol -- any
@mildsunrise
mildsunrise / signalis.py
Created September 16, 2023 19:49
SIGNALIS save data decoder/encoder
#!/usr/bin/python
"""
signalis.py decode <input.png> <output.json>
signalis.py encode <input.json> <output.png>
the decoder does NOT validate the input image! to do that, encode data again and check image pixels match
"""
import os, sys
import numpy as np
@mildsunrise
mildsunrise / crossteaser.rs
Last active September 11, 2023 23:48
traverses the Cross Teaser state graph to calculate puzzle diameter
#![feature(new_uninit)]
use std::mem::MaybeUninit;
// node is stored as 5 bits * 8.
// for bit, the eight S4 elements are separated into sign and A4.
// the A4 are all stored together as a base 12 number (29 bits), and the 8 signs are stored
// together in a bitfield at the MSB part. this concentrates the "islands" of elements together
// and keeps actual RSS at pretty much the real graph size (~3.5GiB/set) despite the ~16GiB/set
// virtual memory allocated. note that to make the code simpler, node stores the elements in

Usage of virglrenderer

Introduction

virglrenderer is a library that gives emulators the necessary tools to implement a [virtio-gpu][] device, in particular one with 3D support. See capability sets below for a summary of the APIs virglrenderer can implement for the guest. It directly implements the logic behind some 3D commands like GET_CAPSET, CTX_CREATE, CTX_SUBMIT_3D, CREATE_RESOURCE_BLOB, and though it closely follows the semantics of virtio-gpu in most cases, it is in theory independent of virtio or any other transport.

Main user is [qemu][qemu-gpu-impl], but there also appears to be a [standalone virtio-gpu vhost-user][qemu-vhost-impl] implementation that uses virglrenderer in qemu/contrib.

virglrenderer's public header is at [src/virglrenderer.h][public-header]. This document attempts to outine the public API and contract, but you should look at the header for an authoritative source of truth since semantics described here could be slightly wrong or have changed, a

@mildsunrise
mildsunrise / perspective.ts
Last active September 30, 2023 09:01
use WebGL to apply a perspective transform to an image
function assertNotNull<T>(x: T | null, message?: string): T {
if (x === null)
throw Error(message ?? 'unexpected null')
return x
}
const vertexShader = `
precision mediump float;
attribute vec2 position;
@mildsunrise
mildsunrise / getPerspectiveTransform.mts
Last active June 24, 2023 15:42
portable / efficient JS version of OpenCV getPerspectiveTransform()
/**
* solve a 2x2 linear system, given its augmented matrix, in place.
* first 2 columns left unchanged.
*/
function solve2x2([ x, y ]: [number[], number[]]) {
let [ a, b, c, d ] = [ x[0], x[1], y[0], y[1] ]
const det = a * d - b * c
if (!det) throw Error('inversion failed')
const k = 1 / det
; (a *= k, b *= k, c *= k, d *= k)
@mildsunrise
mildsunrise / crossteaser.py
Created April 30, 2023 13:51
definition of the Cross Teaser group
'''
definition of the Cross Teaser group, using my groups library:
<https://github.com/mildsunrise/finite-algebra>
the puzzle is tilted 45 degrees, with the front slots in *down right* direction.
crosses are assigned in clockwise order, starting from the upper cross:
0
7 1
6 . 2
@mildsunrise
mildsunrise / tilejpeg.cpp
Created March 31, 2023 17:14 — forked from a4lg/tilejpeg.cpp
Lossless JPEG tiling program for non-optimized images
/*
tilejpeg : lossless JPEG tiling program for non-optimized images
tilejpeg.cpp
Tile JPEG Program
Copyright (C) 2016 Tsukasa OI <floss_tilejpeg@irq.a4lg.com>
Based on the program code on <http://apostata.web.fc2.com/tilejpeg/index.html>.
@mildsunrise
mildsunrise / jpegparser.py
Created March 28, 2023 19:14
🕵️‍♀️ JPEG parser / dissector for the command line
#!/usr/bin/env python3
'''
Portable* JPEG dissector / parser.
Usage: ./jpegparser.py <file name>
(*) Needs Python 3.8+
'''
import sys
import struct