Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
@Lucretiel
Lucretiel / dns_label_iter.rs
Last active March 13, 2021 04:45
Iterator for parsing DNS labels (eg, from QNAME, NAME, etc)
// internal iterator struct that parses labels. Use this internally during
// parsing to validate that a payload is valid (just iterate it to completion,
// checking that no Errors occur)
#[derive(Debug, Clone)]
struct ParsingLabels<'a> {
// This must be the *full* DNS packet, including the header bytes
payload: &'a [u8],
// The position of the next label to read
next: usize,
@Lucretiel
Lucretiel / nom_json.rs
Last active March 4, 2021 00:01
This is an (almost) entirely standards-conformant JSON parser written in nom, live-coded during an "into to nom" talk. The only divergences from the spec are that it allows leading 0s in numbers (ie, 014), and it doesn't correctly handle UTF-16 surrogate pairs. This listing fixes the issue during the video, on line 44.
use std::{borrow::Cow, collections::HashMap, num::ParseFloatError};
use nom::{
branch::alt,
bytes::complete::{is_not, tag},
character::complete::{char, digit1, multispace0, satisfy},
combinator::{map, map_opt, map_res, opt, recognize, value},
error::{FromExternalError, ParseError},
multi::fold_many_m_n,
sequence::{preceded, separated_pair, tuple},
@Lucretiel
Lucretiel / array-iter.rs
Last active October 29, 2020 19:41
Proposed simple implementation of IntoIter for [T; N]
#![feature(min_const_generics)]
use core::mem::ManuallyDrop;
use core::iter::FusedIterator;
use core::mem;
struct Array<T, const N: usize> {
data: [T; N],
}
@Lucretiel
Lucretiel / mem_traits.rs
Created October 24, 2020 01:41
Utility traits exposing some common std::mem functions as methods
pub trait Swap {
fn swap(&mut self, other: &mut Self);
fn replace(&mut self, other: Self) -> Self;
}
impl<T: Sized> Swap for T {
fn swap(&mut self, other: &mut Self) {
std::mem::swap(self, other)
}
@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;
@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<()> {
I am the Miaou user with id 5589 and name "Lucretiel" on https://miaou.dystroy.org
@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;
#[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 / 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();