Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
@Lucretiel
Lucretiel / add_trap.bash
Last active April 6, 2022 02:53
Trap a command in bash without removing the previous trap command
#!/bin/bash
# This function adds an additional command to the trap stack. Each command
# added in this way will be called in reverse order when the signal is received.
#
# This function works with a simple concatenation with the currently existing
# trap function. Popping something from the trap stack without running it is
# left as an exercise to the reader; it requires at least a parser capable of
# matching { } correctly.
#
@Lucretiel
Lucretiel / useKeyedCallback.tsx
Last active May 18, 2019 23:39
React hook for creating callback functions in a loop to pass to individual components
import {memoize} from "lodash"
/**
* Helper for passing a callback to each component in a loop. Given a callback
* with the signature (key, ..args), returns a new factory with the signature
* (key) => (...args). When called with a key, returns a wrapper function taking
* (...args) wich calls the original callback. This wrapper function is guaranteed
* to be the same for any given key.
*
* The callback must have NO dependencies that might change between renders. It
@Lucretiel
Lucretiel / playground.rs
Created June 22, 2019 01:48 — forked from rust-play/playground.rs
Code shared from the Rust Playground
trait Truthy: Sized {
fn is_truthy(&self) -> bool;
fn as_option(self) -> Option<Self> {
if self.is_truthy() {
Some(self)
} else {
None
}
}
}
@Lucretiel
Lucretiel / playground.rs
Last active October 7, 2020 06:11 — forked from rust-play/playground.rs
Code shared from the Rust Playground
trait Truthy: Sized {
fn is_truthy(&self) -> bool;
fn as_option(self) -> Option<Self> {
if self.is_truthy() {
Some(self)
} else {
None
}
}
fn or(self, default: Self) -> Self {
@Lucretiel
Lucretiel / AoC2019D2V1.rs
Created December 5, 2019 04:50
A sample implementation of Advent of Code 2019 Day 2 in Rust
fn solve(input: &str) -> impl Display {
let init: Vec<Cell<usize>> = input
.trim()
.split(',')
.map(|value| value.parse::<usize>().unwrap().into())
.collect();
let mut memory = Vec::new();
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Sign {
Positive,
Negative,
}
/// Partially parse a signed integer. Return the sign (if present) and
/// the unsigned integer part
fn parse_signed<E>(input: &str) -> IResult<&str, (Option<Sign>, u64), E> {
pair(
@Lucretiel
Lucretiel / parse_string.rs
Created December 20, 2019 05:43
Parse an escaped string, using (kind of) the JSON rules
use std::convert::TryFrom;
use nom::branch::alt;
use nom::bytes::streaming::{is_not, take};
use nom::character::streaming::char;
use nom::combinator::{map, map_res, value, verify};
use nom::error::ParseError;
use nom::multi::fold_many0;
use nom::sequence::{delimited, preceded};
use nom::IResult;
I am the Miaou user with id 5589 and name "Lucretiel" on https://miaou.dystroy.org
@Lucretiel
Lucretiel / bof.rs
Created May 13, 2020 20:23
Program for solving pwnable bof
use std::io::{self, BufReader, BufWriter, Read};
use std::net;
#[derive(Debug, Copy, Clone)]
struct SlamParams<'a> {
prefix_length: usize,
payload: &'a [u8],
}
fn deliver_payload(mut dest: impl io::Write, params: SlamParams) -> io::Result<()> {
@Lucretiel
Lucretiel / async_read_hash.rs
Last active October 1, 2020 04:29
A wrapper async reader that hashes the content that's read through it
use std::{
hash::Hasher,
io,
pin::Pin,
task::{Context, Poll},
};
use futures::AsyncRead;
use pin_project::pin_project;