Skip to content

Instantly share code, notes, and snippets.

@fay59
fay59 / Quirks of C.md
Last active September 4, 2024 23:07
Quirks of C

Here's a list of mildly interesting things about the C language that I learned mostly by consuming Clang's ASTs. Although surprises are getting sparser, I might continue to update this document over time.

There are many more mildly interesting features of C++, but the language is literally known for being weird, whereas C is usually considered smaller and simpler, so this is (almost) only about C.

1. Combined type and variable/field declaration, inside a struct scope [https://godbolt.org/g/Rh94Go]

struct foo {
   struct bar {
 int x;
use std::cmp;
fn spiral_distance(num: u32) -> u32 {
let ring = (((num as f64).sqrt() - 1.) / 2.).ceil() as u32;
let root = (num as f64 - 1.).sqrt() as u32;
let offset_to_mid = root / 2;
let ring_min = root * root + 1 + offset_to_mid;
let ring_max = (root + 1) * (root + 1) - offset_to_mid;
return ring + cmp::min(
(num as i32 - ring_min as i32).abs(),
@fay59
fay59 / dom_tree.py
Created May 19, 2018 06:42
Finding regions in Python
import networkx as nx
class DominatorTreeNode(object):
__slots__ = ("value", "depth", "parent", "children")
def __init__(self, value):
self.value = value
self.depth = None
self.parent = None
@fay59
fay59 / deriv.swift
Last active December 29, 2017 00:19 — forked from jdh30/deriv.swift
Swift code to compute the nth derivative of x^x
precedencegroup ExponentPrecedence {
associativity: right
higherThan: MultiplicationPrecedence
}
infix operator **: ExponentPrecedence
fileprivate enum Expr: CustomStringConvertible {
case Int(n: Int)
indirect case Var(x: String)
fn digits_to_vec(digits: String) -> Vec<u32> {
let mut result = Vec::new();
for c in digits.chars() {
match c.to_digit(10) {
Some(v) => result.push(v),
None => {}
}
}
return result;
}
fn digits_to_vec(digits: String) -> Vec<u32> {
let mut result = Vec::new();
for c in digits.chars() {
match c.to_digit(10) {
Some(v) => result.push(v),
None => {}
}
}
return result;
}
@fay59
fay59 / realism.py
Last active September 19, 2017 07:29
Solution to realism.py that uses z3
'''
Solution to the realism challenge from CSAW'17 Quals.
https://github.com/isislab/CSAW-CTF-2017-Quals/tree/master/rev/realism
'''
from z3 import *
s = Solver()
# This is a simplification of the array of bytes found at 0x7D90:
# 0xff => 1, 0x00 => 0; where 1 means "include this value in the psadbw sum"
@fay59
fay59 / PhotosGPX.scpt
Last active October 29, 2024 18:22
Set GPS coordinates of media items in Photos using GPX file
(*
To use:
1- Launch Photos
2- Select photos for which you want to set the GPS location based on a .gpx file
3- Adjust date/time of photos (if necessary)
4- Launch script, select GPX file, wait
*)
@fay59
fay59 / comments.lex
Created March 14, 2017 21:13
Tiny Flex lexer that skips C-style block comments
%option noyywrap
%x C_COMMENT
%%
"/*" { BEGIN(C_COMMENT); }
<C_COMMENT>"*/" { BEGIN(INITIAL); }
<C_COMMENT>. { }
<C_COMMENT>\n { }
@fay59
fay59 / icloud-album-download.sh
Last active November 27, 2024 14:55
Download entire iCloud shared albums
#!/bin/bash
# requires jq
# arg 1: iCloud web album URL
# arg 2: folder to download into (optional)
function curl_post_json {
curl -sH "Content-Type: application/json" -X POST -d "@-" "$@"
}