Skip to content

Instantly share code, notes, and snippets.

View sw17ch's full-sized avatar

John VanEnk sw17ch

View GitHub Profile
@sw17ch
sw17ch / toy.c
Created November 10, 2017 15:18
#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <time.h>
@sw17ch
sw17ch / ccc_analyzer_hates_me.c
Last active March 30, 2017 18:47
some weird stuff in scan-build/ccc-analyzer
#include <stdint.h>
#define STACKY_LEN (1024 * STACKY_MULT)
int main(int argc, char * argv[]) {
(void)argc;
(void)argv;
#ifndef GOTTA_GO_FAST
uint8_t const stacky[STACKY_LEN] = {
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
struct xbee_enc_state {
uint8_t const * input;
size_t size;
@sw17ch
sw17ch / a.lua
Created September 13, 2016 00:19
local a = function ()
print("i am a")
end
error("no")
return a
#!/usr/bin/env python
import sys
import os
import hashlib
ALIGN = 256
for f in sys.argv[1:]:
if os.path.isfile(f):
static void signed_promote(void const * in, size_t in_size, void * out, size_t out_size) {
assert(in_size <= out_size);
assert(in_size == 1 || in_size == 2 || in_size == 4 || in_size == 8);
assert(out_size == 1 || out_size == 2 || out_size == 4 || out_size == 8);
uint64_t word_in = 0;
memcpy(&word_in, in, in_size);
if (word_in & (1 << ((in_size * 8) - 1))) {
data Prim
= PU8
| PU16
| PU32
| PU64
| PS8
| PS16
| PS32
| PS64
| PF32
int recursive_remove(const char * const path) {
int e = 0;
const char * const ptrs[] = { path, 0 };
FTS * fts = fts_open((char * const *)ptrs, FTS_NOCHDIR | FTS_PHYSICAL | FTS_XDEV, NULL);
if (0 != errno) {
e = errno;
fprintf(stderr, "ERROR: %s\n", strerror(e));
} else {
FTSENT * ent;

Cauterize version drift detection

Cauterize creates a structural hash of all meaningful schema information. This can be used to detect, at runtime, whether two communicating partners are running the same version of the protocol or if they have drifted from eachother. This approach does not rely on a developer remembering to update a string version.

Below are two schemas that have identical lengths, names, and fields. The only difference is the order in which the fields x and y occur.

@sw17ch
sw17ch / read_exactly.c
Created December 10, 2014 23:05
Read from a file handle until you get all the bytes or an error happens.
/*
* read_exactly
*
* Automatically re-reads a file descriptor until an error occurs or the
* expected number of bytes has been read.
*
* fd - the file descriptor to read from.
* buf - the buffer to read into.
* nbyte - the number of bytes to read into the buffer.
* rbyte - the actual number of bytes read into the buffer. Will always