Skip to content

Instantly share code, notes, and snippets.

View ljmccarthy's full-sized avatar

Luke McCarthy ljmccarthy

View GitHub Profile
@ljmccarthy
ljmccarthy / endian.c
Created January 23, 2024 07:15
C program to detect machine endianness
#include <stdint.h>
#include <stdio.h>
int main(void)
{
union { uint8_t bytes[4]; uint32_t value; } x = { .bytes = {0x11, 0x22, 0x33, 0x44} };
switch (x.value) {
case 0x11223344:
printf("big\n");
return 0;
@ljmccarthy
ljmccarthy / cfun.sh
Created September 27, 2023 14:37
Run C programs for fun!
#!/bin/sh
#
# Run C programs for fun!
#
# Disables all optimizations, enables warnings, debug info and some hardening options.
#
if [ $# -lt 1 ]; then
echo "usage: $0 FILE.c"
exit 1
fi
@ljmccarthy
ljmccarthy / hsl.c
Last active September 26, 2023 10:58
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#define RGB(r,g,b) (((r) & 0xFF) | (((g) & 0xFF) << 8) | (((b) & 0xFF) << 16))
#define RED(rgb) ((rgb) & 0xFF)
#define GRN(rgb) (((rgb) >> 8) & 0xFF)
#define BLU(rgb) (((rgb) >> 16) & 0xFF)
@ljmccarthy
ljmccarthy / gcc-noopt.sh
Created September 20, 2023 09:59
Run GCC with all optimisations disabled
#!/bin/sh
GCC_O0_OPTS_DISABLED=$(gcc -Q -O0 --help=optimizers | awk -v ORS=" " '($1 ~ /^-f/) && ($2 ~ /enabled/) {print "-fno-" substr($1,3)}')
exec gcc -O0 $GCC_O0_OPTS_DISABLED "$@"
@ljmccarthy
ljmccarthy / update-all.sh
Last active March 9, 2023 12:14
YouTube-DL archiving script
#!/bin/sh
download_videos() {
yt-dlp --download-archive archive -ciw -o "%(upload_date)s %(id)s %(title)s.%(ext)s" -v "$@"
}
find . -type d | while read dir; do
if [ -f "$dir/url" ]; then
(cd "$dir" && download_videos "$(cat url)")
fi
objdump -d /usr/bin/* | cut -f3 | grep -oE ^[a-z]+ | sort | uniq -c | sort -nr
@ljmccarthy
ljmccarthy / qemu-winxp.sh
Created November 28, 2015 18:31
QEMU command line for Windows XP
#!/bin/sh
exec qemu-system-i386 -enable-kvm -cpu host -m 1024 -vga std -soundhw ac97 -net nic,model=rtl8139 \
-net user -drive file=winxp.img,format=raw -drive file=/usr/share/virtio/virtio-win.iso,media=cdrom
@ljmccarthy
ljmccarthy / .gitconfig
Last active December 7, 2022 09:13
git config sane defaults
[user]
name = Your Name
email = your.email@example.com
[init]
defaultBranch = main
[pull]
ff = only
rebase = true
autoStash = true
[fetch]
@ljmccarthy
ljmccarthy / http_serve_data.py
Created November 11, 2022 11:11
The world's stupidest HTTP server
# The world's stupidest HTTP server
def http_serve_data(data, bind_address, bind_port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind((bind_address, bind_port))
sock.listen()
conn, addr = sock.accept()
with conn:
conn.recv(1024) # Read and discard request
conn.sendall(b'HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: ' + str(len(data)).encode('ascii') + b'\r\n\r\n')
conn.sendall(data)
@ljmccarthy
ljmccarthy / fnv1a_hash.py
Created September 14, 2022 11:10
FNV-1a hash function
# Fowler–Noll–Vo hash function
# https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
fnv_prime_32 = 2**24 + 2**8 + 0x93
offset_basis_32 = 0x811c9dc5
def fnv1a_hash_32(bs):
r = offset_basis_32
for b in bs:
r = r ^ b