Skip to content

Instantly share code, notes, and snippets.

@mooware
mooware / compose.py
Created June 18, 2014 12:12
Toying around with metaprogramming in Python.
def compose(*funcs):
"""Composes (or chains) two or more functions into a new function.
E.g. with "f = compose(len, str, int)", f('123')" is the same as
len(str(int('123')))."""
if len(funcs) < 2:
raise Exception("too few arguments given")
elif len(funcs) == 2:
(a, b) = funcs
def call(*args, **kwargs):
@mooware
mooware / bitwise_has31days.py
Created January 6, 2015 11:03
Bit operations to determine whether a month has 31 days
# Bit operations to determine whether a month has 31 days.
# Just for fun, inspired by an example in the Coursera course
# 'Hardware Security', https://www.coursera.org/course/hardwaresec
def has31days(month):
return (month & 1) ^ ((month & 8) >> 3)
for i in range(1, 13):
print(i, has31days(i))
@mooware
mooware / cdb-show-dump.cmd
Last active October 21, 2023 01:33
print lots of windows minidump information with CDB
@echo off
:: used cdb commands:
:: !sym noisy -> prints verbose output when searching PDBs
:: .symopt+0x40 -> accept mismatching PDBs
:: .lines -e -> enable source file and line information
:: .kframes 100 -> set max number of display stack frames to 0x100
:: lmv -> list all loaded modules with version information
:: | -> show process status
:: !peb -> show process environment block (command line arguments, environment variables)
@mooware
mooware / foo_up_down_mix_dsp.cpp
Created October 20, 2015 23:43
foobar2k channel mixer plugin to only play on a single of multiple channels
// foobar2k plugin that mixes a multi-channel stream down to mono,
// and then up to multiple channels again, but with all channels but one empty.
// this can be used to play several different tracks simultaneously over a
// multi-channel audio output.
//
// pre-built version can be found here:
// https://dl.dropboxusercontent.com/u/267889/foo_down_up_mix_dsp.dll
//
// to build locally, just add this source file into the SDK sample plugin.
@mooware
mooware / changeres.cpp
Last active October 23, 2023 14:51
Simple program to change the display resolution on Windows
// prebuilt for VC14/VS2015 here:
// https://dl.dropboxusercontent.com/u/267889/changeres.exe
#include <iostream>
#include <sstream>
#include <Windows.h>
int main(int argc, char **argv)
{
bool reset = false;
@mooware
mooware / print-exception-text.cpp
Created February 12, 2016 22:57
An example program to show that FormatMessage() cannot properly return the text for an exception code.
#include <windows.h>
#include <stdio.h>
int main()
{
HMODULE hMod = LoadLibrary("ntdll.dll");
char buf[1024];
DWORD id = 0xc0000005; // code for access violation
@mooware
mooware / measure.cpp
Created March 3, 2016 01:08
Similar to the "time" or "timeit" commands, for Windows.
#include <Windows.h>
#include <Psapi.h>
#include <stdio.h>
#include <stdint.h>
uint64_t fileTimeToUsec(const FILETIME &ft)
{
return ((uint64_t(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) / 10;
}
@mooware
mooware / tiny_string.h
Created September 6, 2016 01:31
An experimental "very short string optimization"
// a fun little experiment i came up with:
// similar to the classic "short string optimization", we could actually use
// the bytes of the pointer itself as the short string buffer.
// a "tagged pointer" is used to distinguish between the internal buffer
// and a heap-allocated string.
#include <cassert>
#include <cstdint>
#include <cstring>
@mooware
mooware / snake.ino
Created June 30, 2017 03:02
Snake for Arduino on a LED matrix
// snake on a MAX7219 led matrix
// NOTE: assuming the chip and pins are on the left side of the led matrix,
// row = 0, col = 0 is at the upper left corner
#include <LedControl.h>
// we're using a membrane keypad as controller
#include <Keypad.h>
@mooware
mooware / twitch_redirect.py
Last active February 2, 2020 00:58
Simple bottle.py application to play twitch.tv streams through HTML5 video
# uses bottle.py as web framework, streamlink for getting the stream URL
# and hls.js to play the HLS stream in an HTML5 video tag
from bottle import *
from streamlink import Streamlink
from urllib.request import urlopen, Request
import sys, re, json
def u(s):
if isinstance(s, str):