Skip to content

Instantly share code, notes, and snippets.

@danielpyon
danielpyon / floodfill.py
Last active August 19, 2022 17:58
flood fill algorithms
# adj list, edge list, adj matrix
# in an adj list, each vertex contains a list of nodes it is adjacent to
# simple undirected, unweighted graph using adjacency list representation
class Graph:
def __init__(self):
self.graph = dict()
def add_node(self, label):
assert label not in self.graph
self.graph[label] = list()
@danielpyon
danielpyon / castle.py
Created August 19, 2022 19:42
USACO Castle
'''
ID: daniel20
LANG: PYTHON3
TASK: castle
'''
global WEST, NORTH, EAST, SOUTH
WEST, NORTH, EAST, SOUTH = 0, 1, 2, 3
def closed(room, direction=NORTH):
@danielpyon
danielpyon / trie.py
Last active August 20, 2022 01:24
simple trie implementation
# simple trie implementation
class Trie:
class Node:
def __init__(self, data=None, end=False):
self.data = data
self.children = dict()
# is this the end of a word?
self.end = end
def __str__(self):
@danielpyon
danielpyon / solve.py
Created August 22, 2022 20:28
pwn template
from pwn import *
import struct
def p64(x):
return struct.pack('Q', x)
def u64(x):
return struct.unpack('Q', x)[0]
def p32(x):
return struct.pack('<I', x)
def u32(x):
@danielpyon
danielpyon / str2bytes.py
Created October 10, 2022 00:50
pack a string
def str2bytes(s):
ret = []
for i in range(0, len(s), 8):
sub = s[i:i+8][::-1]
cur = 0
for x in sub:
cur <<= 8
cur |= ord(x)
ret.append(cur)
return ret[::-1]
@danielpyon
danielpyon / .motd.py
Created April 14, 2023 00:19
message of the day
import os
import random
x=list(map(lambda x: x[:-4], os.listdir('/usr/share/cowsay/cows/')))
cf=x[random.randint(0,len(x)-1)]
c='cowsay' if random.random() < 0.5 else 'cowthink'
os.system(f'fortune -a | {c} -f {cf}')
@danielpyon
danielpyon / solve.cpp
Created July 17, 2023 03:31
ImagePrc Solution
#ifndef UNICODE
#define UNICODE
#endif
#define PIXELDATA_SIZE 90000
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
@danielpyon
danielpyon / traps_and_context_switching.md
Created January 24, 2024 21:47
how traps and context switching works
First, it's important to note the difference between kernel<->userspace switch and kernel process -> another kernel process switch. The kernel<->userspace switch is called traps (syscalls, interrupts, etc), and kernel process -> another kernel process is called context switching.

How kernel<->userspace works
	1. Userspace makes syscall (mov rax, syscall num; syscall)
	2. Syscall instruction finds the interrupt table (in xk: vectors.S), jumps to the correct entry. In the case of a syscall, it's entry 0x40, but for example, timer interrupts will have a different value for this. Additionally,  syscall instruction switches CPU into kernel mode, and saves rip,rsp,eflags onto kernel stack.
	3. The interrupt handler pushes 0 (some systems use this as the error code, ignore this), then the interrupt number (so in the case of syscall, 0x40).
	4. Interrupt handler jumps to alltraps.
	5. Alltraps saves rest of registers onto kernel stack, then jumps to trap(tf) with the trapframe (the set of all saved registers) as