Skip to content

Instantly share code, notes, and snippets.

View panzi's full-sized avatar

Mathias Panzenböck panzi

View GitHub Profile
@panzi
panzi / portable_endian.h
Last active April 18, 2024 20:59
This provides the endian conversion functions form endian.h on Windows, Linux, *BSD, Mac OS X, and QNX. You still need to use -std=gnu99 instead of -std=c99 for gcc. The functions might actually be macros. Functions: htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh. License: I hereby put …
// "License": Public Domain
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
// an example on how to get the endian conversion functions on different platforms.
#ifndef PORTABLE_ENDIAN_H__
#define PORTABLE_ENDIAN_H__
#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
@panzi
panzi / rekey.py
Last active March 9, 2024 05:43
Re-key all the embedded vaults in an Ansible vars file.
#!/usr/bin/env python3
# derived from https://stackoverflow.com/a/67161907/277767
# Changes to the StackOverflow version:
# * delete temporary files that contain vaults!
# * prompt for passwords instead of passing them as program argument
# * more precise vault replacement
# * a bit nicer error messages that points at the line where re-keying failed
# * decryption if no password is provided
@panzi
panzi / export_local_storage.js
Created March 6, 2024 18:51
Bookmarklets to export/import localStorage. Import does *not* clean localStorage, only sets the loaded values.
// bookmarklet:
// javascript:(function()%7Bconst%20blob%3Dnew%20Blob(%5BJSON.stringify(localStorage)%5D%2C%7Btype%3A'application%2Fjson'%7D)%3Bconst%20link%3Ddocument.createElement('a')%3Bconst%20url%3DURL.createObjectURL(blob)%3Blink.href%3Durl%3Blink.download%3D'local_storage.json'%3Blink.style.display%3D'none'%3Bdocument.body.appendChild(link)%3Blink.click()%3BsetTimeout(()%3D%3E%7BURL.revokeObjectURL(url)%3Bdocument.body.removeChild(link)%3B%7D%2C250)%3B%7D)()%3B
function exportLocalStorage(){
const blob = new Blob([JSON.stringify(localStorage)],{type:'application/json'});
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
link.href = url;
link.download = 'local_storage.json';
link.style.display='none';
document.body.appendChild(link);
@panzi
panzi / smart_formatter.py
Last active February 28, 2024 22:32
Python argparse help formatter that keeps new lines and doesn't break words (so URLs are preserved), but still wraps lines. Use with: `argparse.ArgumentParser(formatter_class=SmartFormatter)`
import argparse
from typing import List
class SmartFormatter(argparse.HelpFormatter):
def _split_lines(self, text: str, width: int) -> List[str]:
lines: List[str] = []
for line_str in text.split('\n'):
line: List[str] = []
line_len = 0
for word in line_str.split():
@panzi
panzi / gist:7551381
Created November 19, 2013 19:49
Download currently playing HTML5 audio/video bookmarklet.
Create a new bookmark and set it's URL to this:
javascript:(function(xs)%7Bfor(var%20i%3D0%3Bi%3Cxs.length%3B%2B%2Bi)%7Bif(xs%5Bi%5D.currentSrc)%7Breturn%20window.open(xs%5Bi%5D.currentSrc)%3B%7D%7D%7D)(document.querySelectorAll('audio%2Cvideo'))%3B
When media is playing using HTML5 audio/video you can click this bookmark to open a new tab/window with the media. Then to download it use the context menu action "Save As..." (right mouse button -> Save As...).
If you use Chrome/Safari/recent Opera (WebKit/Blink) you can do better(!) and immediately download the file using this bookmarklet instead:
javascript:(function(xs)%7Bfor(var%20i%3D0%3Bi%3Cxs.length%3B%2B%2Bi)%7Bif(xs%5Bi%5D.currentSrc)%7Bvar%20a%3Ddocument.createElement('a')%3Ba.target%3D'_blank'%3Ba.download%3D''%3Ba.href%3Dxs%5Bi%5D.currentSrc%3Ba.click()%3Breturn%3B%7D%7D%7D)(document.querySelectorAll('audio%2Cvideo'))%3B
@panzi
panzi / truthiness.md
Last active September 14, 2023 12:35
Table of truthiness values in some dynamically typed programming languages.
Value Ruby Python JavaScript PHP Perl
"" true False false false false
"0" true True true false false
" " true True true true true
0 true False false false false
NaN true True false true true
[] true False true false true
{}/new \stdClass true False true true true
nil/null/None false False false false -
(function() {
"use strict";
function padd(x) {
var x = String(x);
return x.length < 2 ? '0' + x : x;
}
var duration = +document.querySelector("video").duration;
var playerData = document.querySelector("ytd-watch-flexy").__data.playerData;
@panzi
panzi / pil2cv.py
Created July 5, 2021 16:00
Python: Convert PIL.Image to OpenCV BGR(A)/grayscale image (NumPy array). This supports the most common image modes, but there are more! Patches for those are welcome.
from PIL import Image
import numpy as np
import cv2
def pil2cv(image: Image) -> np.ndarray:
mode = image.mode
new_image: np.ndarray
if mode == '1':
new_image = np.array(image, dtype=np.uint8)
@panzi
panzi / python-c-quine.c
Last active February 14, 2023 22:33
The first file is a polyglot C and Python program that prints its own source code, i.e. a quine. This means you can run it as a Python script, but you can also compile it as a C program. The second file is the same, but in C and Ruby.
#include<stdio.h>
#define len int main(){char*
#define zip return 0;}
#if 0
def printf(f,*a):print(f%a,end=str())
#endif
len
s="#include<stdio.h>%c#define len int main(){char*%c#define zip return 0;}%c#if 0%cdef printf(f,*a):print(f%%a,end=str())%c#endif%clen%cs=%c%s%c;printf(s,10,10,10,10,10,10,10,34,s,34,10);zip%c";printf(s,10,10,10,10,10,10,10,34,s,34,10);zip
@panzi
panzi / preproc.js
Created October 12, 2022 22:27
A very simple pre-processor that I use in some build step. Could be more efficient and feature rich, but that is not the scope of this.
/**
* Very simple pre-processor.
*
* Syntax:
*
* #if FLAG
* ...
* #elif OTHER_FLAG
* ...
* #else