Skip to content

Instantly share code, notes, and snippets.

@mooware
mooware / ld-preload-intercept-method.cpp
Last active November 29, 2023 16:59
Intercept C++ methods with LD_PRELOAD
// this file is an example of how to intercept a C++ method by using the
// LD_PRELOAD environment variable of the GNU dynamic linker.
//
// it works like this:
//
// 1) define a method that will have the same symbol as the intercepted
// method when compiled. For example, the method Foo::getValue()
// defined here has the mangled symbol "_ZNK3Foo8getValueEv".
// tools like nm, objdump or readelf can display the symbols of
// binaries. note that depending on compiler and linker options,
@mooware
mooware / gnu-libc-print-stacktrace.c
Last active April 25, 2024 12:29
Print a stacktrace on Linux with GNU libc
// this code fragment shows how to print a stack trace (to stderr)
// on Linux using the functions provided by the GNU libc
#include <execinfo.h>
#define MAX_STACK_LEVELS 50
// helper-function to print the current stack trace
void print_stacktrace()
{
@mooware
mooware / amazon-cloud-player-download.js
Last active October 13, 2015 14:18
Amazon Cloud Player Download Bookmarklet
// I wrote this bookmarklet because Amazon has ended Linux support for downloading MP3s,
// and now I have to download songs separately through Amazon Cloud Player.
// This bookmarklet helps a bit with this, but it's far from perfect.
// When a list of songs is shown in Amazon Cloud Player, it clicks the song checkbox and
// the download button separately for each song, while waiting a few seconds in between songs.
// I encountered the issue that I cannot download more than a few songs simultaneously;
// the rest will just not show a download window. Not sure whether my browser or the server
// is doing this, but you might want to set the value of songdelayms high enough.
// The script is not really that useful, but it was fun to play around with this.
@mooware
mooware / fm4-musik.rb
Last active December 13, 2015 21:58
Ruby script to scrape termine.orf.at for FM4 music events and turn the result into an RSS feed, with one post per run.
#!/usr/bin/env ruby
# Ruby script to scrape termine.orf.at for FM4 music events and
# turn the result into an RSS feed.
#
# Usage: ruby fm4-musik.rb <result-file> <data-file> [-digest]
#
# The result file is where the feed xml will be written to,
# and the data file stores some state necessary to track
# the events that were already posted.
@mooware
mooware / sizeof_array.hpp
Last active December 14, 2015 17:58
Type-safe way to get the length of an array in C++.
// In C/C++, the idiom (sizeof(arr) / sizeof(*arr)) is often used to get the length of an array.
// But this will return incorrect results if arr is not actually an array, but a pointer.
// The following function template provides a type-safe way to get the length of an array,
// by "destructuring" the type and reading the array length from there.
#include <cstddef> // for size_t
template <typename T, size_t N>
constexpr size_t sizeof_array(T (&)[N])
{
@mooware
mooware / bytes2ascii.py
Created April 2, 2013 20:24
Python script that takes lines of text containing byte hex values, and appends a printable representation of the bytes to each line. It's similar to xxd, but it takes already dumped bytes instead of the original binary data. Example: "70 79 74 68 6f 6e" -> "70 79 74 68 6f 6e python"
#!/usr/bin/env python
import fileinput
import re
# pattern to match lines of hex byte values
PATTERN = re.compile("^[a-zA-Z0-9]{1,2}( [a-zA-Z0-9]{1,2})*$")
# margin between bytes and text
MARGIN = 2
@mooware
mooware / nullptr_t.cpp
Last active December 16, 2015 06:29
Implementation of the C++11 feature nullptr_t and an example of why it works better than the C #define NULL
struct nullptr_t
{
template <typename T>
operator T*() { return static_cast<T*>(0); }
};
nullptr_t nullptr;
#include <iostream>
@mooware
mooware / challenge8-palindrome-number.rb
Last active December 31, 2015 13:49
challenge #8: find the largest palindrome made from the product of two 3-digit numbers
# challenge #8
# 'find the largest palindrome made from the product of two 3-digit numbers'
# see also http://projecteuler.net/index.php?section=problems&id=4
# simple one-line solution, seems fast enough
# => [906609, 993, 913]
999.downto(100).map { |a| 999.downto(100).map { |b| [a*b, a, b] }.select { |arr| s = arr.first.to_s; s == s.reverse } }.flatten(1).sort { |a, b| a.first <=> b.first }.reverse.uniq.first
# more performant solution for n-digit numbers
# 4 => [99000099, 9999, 9901]
@mooware
mooware / .vimrc
Created January 8, 2014 22:20
My .vimrc for work
set nocompatible
syntax on
filetype plugin on
filetype plugin indent on
" by itself, jellybeans doesnt enable 256 colors
set t_Co=256
set background=dark
@mooware
mooware / colorstreamhandler.py
Last active July 20, 2023 16:16
Colored log output for Python logging framework. Works on Windows, Linux, and probably Mac as well.
# colored stream handler for python logging framework (use the ColorStreamHandler class).
#
# based on:
# http://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output/1336640#1336640
# how to use:
# i used a dict-based logging configuration, not sure what else would work.
#
# import logging, logging.config, colorstreamhandler
#