Skip to content

Instantly share code, notes, and snippets.

@1995eaton
1995eaton / sfinae.cc
Created September 23, 2014 23:47
SFINAE typedef test
// SFINAE structure that tests if a given
// type contains the iterator typedef
template<typename T>
struct has_iterator {
template<class C>
static char test(typename C::iterator*);
static long test(...);
enum {
value = 1 == sizeof test<T>(0)
@1995eaton
1995eaton / twister.cc
Created October 5, 2014 23:29
Mersenne Twister in C++
#include <iostream>
#include <fstream>
class Twister {
private:
uint32_t data[624],
index = 0;
uint32_t read_device() {
std::ifstream ifs("/dev/urandom");
uint32_t r(0);
@1995eaton
1995eaton / cpuperc.c
Created October 7, 2014 13:10
CPU usage calculator
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
typedef struct {
int64_t user, nice, system,
idle, iowait, irq,
softirq, steal, guest,
@1995eaton
1995eaton / shellshock.c
Last active August 29, 2015 14:07
Shellshock
/* Tested on Linux running apache 2.4.10-1 CGI module
and bash-4.3.018-1 */
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#define HOST "127.0.0.1"
@1995eaton
1995eaton / client.py
Created October 13, 2014 17:47
Simple Python Socket Messenger
#!/usr/bin/python2
from threading import Thread
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 9999,))
class ReplyHandler(Thread):
@1995eaton
1995eaton / marshal.js
Created November 2, 2014 23:45
A Marshal-like Node.js script for data serialization
var fs = require('fs');
function BufferWriter(initialSize) {
initialSize = initialSize || 1024;
this.data = new Buffer(initialSize);
this.clear(this.data);
this.capacity = initialSize;
this.offset = 0;
}
@1995eaton
1995eaton / readin.c
Created November 8, 2014 23:29
C stdin reader example with dynamic memory allocation
#include <stdio.h>
#include <stdlib.h>
static char *
read_stdin (void)
{
size_t cap = 4096, /* Initial capacity for the char buffer */
len = 0; /* Current offset of the buffer */
char *buffer = malloc(cap * sizeof (char));
int c;
@1995eaton
1995eaton / brainfuck.c
Created November 14, 2014 17:32
Brainfuck interpreter in C
#include <stdio.h>
#include <stdlib.h>
static struct {
size_t buffer_size;
char *buffer, *ptr;
} bf;
void bf_init(size_t initial_cap) {
bf.buffer_size = initial_cap;
@1995eaton
1995eaton / fib.S
Last active April 30, 2016 15:51
Fibonacci sequence in GCC assembly
.intel_syntax noprefix
.globl fib
fib:
sub rsp, 24
xor eax, eax
mov ecx, 1
0:
xadd rax, rcx
#include <string>
#include <iostream>
#include <time.h>
using namespace std;
/***************************** Person Class **********************************/
class Person
{