Skip to content

Instantly share code, notes, and snippets.

@skeeto
skeeto / demo.c
Created February 13, 2024 15:12
WriteProcessMemory demo
// WriteProcessMemory demo
// $ cc -nostartfiles -o demo.exe demo.c
// $ cl demo.c /link /subsystem:console kernel32.lib
// This is free and unencumbered software released into the public domain.
#include <stddef.h>
#define countof(a) (ptrdiff_t)(sizeof(a) / sizeof(*(a)))
typedef unsigned char u8;
typedef signed int b32;
@skeeto
skeeto / summarize.c
Created March 5, 2023 20:44
P-square summary
// Summarize a numeric sequence
// Ref: https://old.reddit.com/r/commandline/comments/11hw06b
// Ref: https://github.com/ahmedakef/summarize
// This is free and unencumbered software released into the public domain.
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
double sum, err;
@skeeto
skeeto / hotpatch.c
Last active February 7, 2024 20:54
Function hotpatch example
// Function hotpatch example
// $ gcc -nostartfiles -o hotpatch.exe hotpatch.c
// $ clang -nostdlib -Wl,/subsystem:console -o hotpatch.exe hotpatch.c -lkernel32
// This is free and unencumbered software released into the public domain.
#include <stddef.h>
#include <stdint.h>
#define W32 __attribute((dllimport, stdcall))
W32 void *CreateThread(void *, size_t, void *, void *, int, int *);
W32 int FlushInstructionCache(void *, void *, size_t);
W32 void *GetStdHandle(int);
@skeeto
skeeto / demo.c
Created February 2, 2024 22:44
arena stuff
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define assert(c) while (!(c)) *(volatile int *)0 = 0
#define countof(a) (ptrdiff_t)(sizeof(a) / sizeof(*(a)))
#define new(a, t, n) (t *)alloc(a, sizeof(t), n)
#define S(s) (str){(char *)s, countof(s)-1}
@skeeto
skeeto / cleanup.c
Last active February 1, 2024 16:33
"utility to clean up logkeys log files"
// Ref: https://github.com/lionkor/logkeys-cleanup
#include <stdio.h>
typedef struct trie trie;
struct trie {
trie *child[256];
_Bool terminal;
};
static void insert(trie *t, char *word, trie **heap)
@skeeto
skeeto / glove.c
Created January 20, 2024 07:16
Example GloVe index builder
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#define new(a, t) (t *)alloc(a, sizeof(t))
typedef struct {
char *beg;
char *end;
} arena;
@skeeto
skeeto / Benchmark.java
Created April 23, 2021 21:22
EnumTest benchmark
/* EnumSet benchmark
* $ javac Benchmark.java
* $ java -ea Benchmark
* Ref: https://nullprogram.com/blog/2021/04/23/
*/
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
@skeeto
skeeto / meminfo.c
Created January 10, 2024 02:02
/proc/meminfo parser
// Parse /proc/meminfo into a hash map
// This is free and unencumbered software released into the public domain.
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@skeeto
skeeto / aoc2023d15.c
Last active January 4, 2024 17:31
Advent of Code 2023 Day 15
// Advent of Code 2023 Day 15
// https://adventofcode.com/2023/day/15
// https://nrk.neocities.org/articles/aoc23
// This is free and unencumbered software released into the public domain.
#include <stddef.h>
#include <string.h>
#define assert(c) while (!(c)) *(volatile int *)0 = 0
#define new(a, t, n) (t *)alloc(a, sizeof(t)*n)
// $ cc -mwindows stretchdibits.c
#include <windows.h>
enum {
WIDTH = 1280,
HEIGHT = 720,
};
static unsigned char *pixels;