Skip to content

Instantly share code, notes, and snippets.

@saagarjha
saagarjha / bn_install.py
Created May 22, 2026 18:39
Installs Binary Ninja given a license file
#!/usr/bin/env python3
"""Install Binary Ninja from license.dat alone. Pure stdlib + `openssl` CLI.
bn_install.py LICENSE TARGET [--channel dev|release|test]
[--platform linux-arm|linux|macosx|win64]
Each layer is the same primitive — IV(16) || AES-128-CBC(zlib(payload)) — and
hands you the AES key for the next. /manifest and /channel additionally have a
256-byte RSA-PSS signature prefix.
"""
@saagarjha
saagarjha / fetch_cuda_headers.py
Created May 1, 2026 10:12
Fetches CUDA headers for clangd to be happy
#!/usr/bin/env python3
"""Fetch a minimal CUDA headers tree for clangd, from NVIDIA's apt repo.
Stdlib-only. Works anywhere with Python 3.9+.
"""
from __future__ import annotations
import argparse
import gzip
@saagarjha
saagarjha / pleroma_fe.py
Last active March 26, 2026 06:07
Simple read-only HTML frontend for Pleroma
#!/usr/bin/env python3
import datetime
import html
import html.parser
import http.server
import json
import sys
import urllib.request
import urllib.parse
@saagarjha
saagarjha / a14.plist.txt
Created January 16, 2024 04:08
plutil -p /usr/share/kpep/a14.plist
{
"internal" => 0
"name" => "a14"
"system" => {
"cpu" => {
"aliases" => {
"Cycles" => "FIXED_CYCLES"
"Instructions" => "FIXED_INSTRUCTIONS"
}
"architecture" => "arm64"
@saagarjha
saagarjha / file_drain.c
Created November 11, 2023 10:01
"Drain" files while they are processed to reduce free disk space requirements
// Sometimes you have a large file on a small disk and would like to "transform"
// it in some way: for example, by decompressing it. However, you might not have
// enough space on disk to keep both the the compressed file and the
// decompressed results. If the process can be done in a streaming fashion, it
// would be nice if the file could be "drained"; that is, the file would be
// sequentially deleted as it is consumed. At the start you'd have 100% of the
// original file, somewhere in the middle you'd have about half of the original
// file and half of your output, and by the end the original file will be gone
// and you'll be left with just the results. If you do it this way, you might
// be able to do the entire operation without extra space!
@saagarjha
saagarjha / mmap_vs_read.c
Created September 29, 2023 10:42
Test whether mmap or read is faster on your computer
// As seen on:
// https://federated.saagarjha.com/notice/AaEMQpJBSbxhLyxYzg
// https://twitter.com/_saagarjha/status/1707423903969341949
// Compiling: gcc mmap_vs_read.c -O3 -o mmap_vs_read
// Usage: ./mmap_vs_read <bigfile> <mmap|read>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
@saagarjha
saagarjha / MetadataExtractor.js
Created February 17, 2023 09:36
Apple's metadata extraction code for link previews in Messages, taken from macOS Ventura 13.3 Beta (22E5219e)
//
// LinkPresentation
// Copyright © 2015-2020 Apple Inc. All rights reserved.
//
// FIXME: Twitter equivalents?
(function () {
var MetadataExtractor = {
@saagarjha
saagarjha / FB11988552.html
Created February 12, 2023 16:44
Leak memory in Safari from a website
<!DOCTYPE html>
<html>
<head>
<script>
var database;
async function foo() {
let key = await window.crypto.subtle.generateKey(
{
name: "HMAC",
// Usage should be fairly self-explanatory, just paste this in a header and use
// CRASH_WITH_MESSAGE("foobar") in your function.
// Example backtrace:
// Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
// 0 ??? 0x1022d8000 CRASHING IN test.c:20 (foobar) + 0
// 1 a.out 0x1022d7f60 main + 24
// 2 dyld 0x195f07e50 start + 2544
#define STRINGIFY(a) #a
#define CRASH_FUNCTION_NAME(file, line, message) "CRASHING IN " file ":" STRINGIFY(line) " (" message ")"
@saagarjha
saagarjha / fix_FB11645580.mm
Last active January 1, 2024 04:09
Fix an Xcode hang caused by FB11645580 due to IDERunDestination registering thousands of duplicate KVO observers
// https://gist.github.com/saagarjha/ed701e3369639410b5d5303612964557
#import "swizzler.h"
#import <Foundation/Foundation.h>
#import <cstddef>
#import <cstdlib>
#import <dlfcn.h>
#import <mach-o/dyld.h>
#import <mutex>
#import <string>
#import <tuple>