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 "$@"
objdump -d /usr/bin/* | cut -f3 | grep -oE ^[a-z]+ | sort | uniq -c | sort -nr
@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
@ljmccarthy
ljmccarthy / hello.asm
Created August 26, 2022 15:45
Flat Assembler Hello World (Linux x86-64)
format ELF64 executable 3
segment readable executable
entry $
mov eax, 1 ; sys_write
mov edi, 1 ; fd = STDOUT_FILENO
mov rsi, message ; buf
mov rdx, message.end - message ; count
syscall
@ljmccarthy
ljmccarthy / make_battery_report.cmd
Created July 3, 2022 20:47
Make battery report (Windows)
powercfg /batteryreport /output "C:\battery_report.html"