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 / lz_string.md
Last active July 23, 2024 23:30
description + simplified implementation of the (cursed) compression scheme of the popular lz-string library

[lz-string][] is a very popular library that compresses UTF-16 strings using a variation of the [LZ78][] algorithm where literals are only encoded once (and referred as dictionary indexes afterwards).

Despite its impressive 10 million downloads per week at the time of this writing, there is no official documentation on the wire format implemented by this library, and the horrible code quality makes it hard to understand from it. Furthermore, the code contains many bugs / gotchas, some of which are enumerated below. This is probably why many of the [numerous ports][ports] of the library blindly copy its code, doing little more

@mildsunrise
mildsunrise / create-object.py
Last active June 10, 2024 22:57
recursively creates git objects (trees, blobs) mirroring a directory in disk
import os, stat
from contextlib import contextmanager
from subprocess import run
@contextmanager
def fd_context(fd: int):
try:
yield fd
finally:
os.close(fd)
@mildsunrise
mildsunrise / polynomials.agda
Last active April 13, 2024 20:54
polynomial algebra over a ring
open import Level using (suc; _⊔_)
open import Function using (id; _∘_)
open import Data.List as List using (
List; []; _∷_; [_]; map; reverse; align; alignWith; foldr; head; last; drop; length
)
import Data.List.Properties as List
import Data.List.Relation.Unary.All as All
import Data.List.Relation.Binary.Pointwise as PW
@mildsunrise
mildsunrise / jni.md
Last active March 30, 2024 16:32
JNI ABI magic numbers

JNIEnv

(keep in mind that JNI methods get a JNIEnv *, not a direct JNIEnv)

given JNIEnv x, you can write ((void**)x)[N] to access a function pointer, where N is:

     4	GetVersion
     5	DefineClass
 6	FindClass
@mildsunrise
mildsunrise / Main.java
Created March 30, 2024 13:37
copy a test image to the clipboard using Java AWT
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.image.*;
import java.io.*;
public class Main {
public static void main(String[] arg) {
var flavorMap = (FlavorTable) SystemFlavorMap.getDefaultFlavorMap();
System.out.println("formats for image flavor: ");
for (var format : flavorMap.getNativesForFlavor(DataFlavor.imageFlavor))
@mildsunrise
mildsunrise / radix_sort.rs
Last active March 4, 2024 12:22
in-place radix sort with sentinel
use std::{ops::{Add, AddAssign}, convert::TryInto};
/// In-place unstable radix sort with sentinel
///
/// - `L` is the type used for bin counts (must be able to hold `arr.len()`).
/// - `key: F` is a function taking an array item + a level, and returning an alphabet symbol.
/// - `AL` is the alphabet length, i.e. number of bins. it must hold that `key(...) < AL`.
///
/// 0 is the sentinel, meaning that for any item, `key(item, max_level) == 0`.
/// `max_level` may differ among items.
@mildsunrise
mildsunrise / rtmp2flv.py
Last active March 1, 2024 11:43
convert a captured TCP stream of an RTMP connection into FLV
'''
Reads the contents of a TCP stream carrying [one side of] an
RTMP connection, and blindly dumps streams above 0 in an FLV.
$ ./rtmp2flv.py < tcp-stream > video.flv
'''
from enum import IntEnum, unique
from dataclasses import dataclass, field
from typing import NamedTuple, Self, Optional
@mildsunrise
mildsunrise / sllv2tossl.c
Created February 27, 2024 23:30
converts an SLLv2 pcap into an SLL one (because tools like tcpflow and tcpreplay lack support for SLLv2 and it's maddening)
// compile with: clang -Wpedantic -Wall -Wextra $(pkg-config --cflags libpcap) sllv2tosll.c $(pkg-config --libs libpcap) -o sllv2tossl
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <pcap/pcap.h>
char error_buffer[PCAP_ERRBUF_SIZE];
@mildsunrise
mildsunrise / sa-is.hs
Last active February 26, 2024 14:25
Haskell implementation of SA-IS
{-# LANGUAGE TupleSections #-}
import Data.Array (accumArray, listArray, elems)
import Data.List.Extra (findIndices, groupSortOn, chunksOf)
import Data.Vector ((!), toList)
import Control.Monad (forM_, when, zipWithM)
import qualified Data.Vector as Vec
import qualified Data.Vector.Mutable as MVec
fill v = zipWithM (MVec.write v) [0..MVec.length v - 1]
@mildsunrise
mildsunrise / block.rs
Last active February 18, 2024 18:13
simple linked list allocator in Rust
use std::{mem, ptr};
use crate::BufferAlloc;
unsafe fn write_ptr(n: *mut usize, ptr: Option<*mut usize>) {
debug_assert!(!ptr.is_some_and(|ptr| n == ptr));
ptr::write(n.cast(), ptr.unwrap_or(n))
}
unsafe fn read_ptr(n: *mut usize) -> Option<*mut usize> {
let ptr = ptr::read(n.cast());