Skip to content

Instantly share code, notes, and snippets.

View JonathonReinhart's full-sized avatar

Jonathon Reinhart JonathonReinhart

View GitHub Profile
// unaligned.h
// Safely access unaligned multi-byte values (e.g on ARM)
// Jonathon Reinhart
// The use of 'packed' forces the compiler to assume no better
// than 1-byte alignment, and issue sequential byte reads/writes.
typedef union __attribte__((packed)) {
int16_t int16;
uint16_t uint16;
int32_t int32;
@JonathonReinhart
JonathonReinhart / handletest.c
Last active August 30, 2015 04:16
typedef provides no type safety
#include <stddef.h>
typedef size_t foo_h;
typedef size_t bar_h;
void foo(foo_h h) { (void)h; }
void bar(bar_h h) { (void)h; }
int main(void)
{
@JonathonReinhart
JonathonReinhart / x86_64_regs.c
Created August 30, 2015 08:40
x86-64 registers structure definition
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#define __packed __attribute__((packed))
/* https://commons.wikimedia.org/wiki/File:Table_of_x86_Registers_svg.svg */
#define REGDEF_hl(Z) \
union __packed { \
@JonathonReinhart
JonathonReinhart / hexdump.c
Created October 28, 2015 21:39
Hexdump in C
// Derived from http://stackoverflow.com/a/7776146/119527
void fhexdump(FILE *f, const void *addr, size_t len, const char *addrfmt, size_t baseaddr)
{
size_t i;
unsigned char buff[17];
const unsigned char *pc = addr;
// Process every byte in the data.
for (i = 0; i < len; i++) {
@JonathonReinhart
JonathonReinhart / .gitconfig
Last active January 22, 2016 14:33
My Git config
[user]
email = jonathon.reinhart@gmail.com
name = Jonathon Reinhart
[color]
ui = true
[core]
# I can exclude my own editor's files; no need to include in project .gitignore
excludesfile = ~/.gitignore_global
editor = vim
[push]
@JonathonReinhart
JonathonReinhart / docker_config_temp.sh
Last active February 19, 2016 18:18
Prevent docker client from saving credentials to disk
#!/bin/sh
# This prevents docker config from persisting to disk.
# Place this file in /etc/profile.d/ to happen automatically at login for all users.
# JRR 2015-12-17
# See:
# https://github.com/docker/docker/issues/10318
# https://github.com/docker/docker/issues/18708
dockercfgdir="/dev/shm/dockercfg-$(id -un)"
@JonathonReinhart
JonathonReinhart / vimstrace
Created July 28, 2016 14:56
Vim frontend to strace
#!/bin/sh
# Using Vim to view strace output
strace -o'!vim -R -' "$@"
@JonathonReinhart
JonathonReinhart / a.h
Created October 15, 2016 23:08
Circular includes
#ifndef A_DOT_H_INCLUDED
#define A_DOT_H_INCLUDED
#include "b.h"
struct a
{
struct b *b;
};
Windows (PowerShell) (DER format):
Invoke-WebRequest -UseBasicParsing http://example.com/example.cer -OutFile example.cer
certutil.exe -addStore "Root" example.cer
Debian 8 (PEM format):
# wget -O /usr/local/share/ca-certificates/example.crt http://example.com/example.cer
# update-ca-certificates
@JonathonReinhart
JonathonReinhart / search.c
Created March 14, 2017 20:13
Places "goto" can improve C code
void *search_without_goto(const char *key)
{
int i;
void *result;
for (i = 0; i < some_length; i++) {
if (strcmp(some_collection[i].key, key) == 0)
break;
}
if (i == some_length)