Skip to content

Instantly share code, notes, and snippets.

View Quirks of C.md

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;
View adv03.rs
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
Finding regions in Python
View dom_tree.py
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 Dec 29, 2017 — forked from jdh30/deriv.swift
Swift code to compute the nth derivative of x^x
View deriv.swift
precedencegroup ExponentPrecedence {
associativity: right
higherThan: MultiplicationPrecedence
}
infix operator **: ExponentPrecedence
fileprivate enum Expr: CustomStringConvertible {
case Int(n: Int)
indirect case Var(x: String)
View adv01-2.rs
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;
}
View adv01-1.rs
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 Sep 19, 2017
Solution to realism.py that uses z3
View realism.py
'''
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 Apr 9, 2017
Set GPS coordinates of media items in Photos using GPX file
View PhotosGPX.scpt
(*
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 Mar 14, 2017
Tiny Flex lexer that skips C-style block comments
View comments.lex
%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 Dec 19, 2022
Download entire iCloud shared albums
View icloud-album-download.sh
#!/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 "@-" "$@"
}