Skip to content

Instantly share code, notes, and snippets.

View benwills's full-sized avatar

Ben Wills benwills

View GitHub Profile
@benwills
benwills / create_lkp_bool.c
Last active August 29, 2015 14:12
Quickly Create a Binary Lookup Table
#include <stdio.h>
#include <string.h>
//
// Create entries for a boolean lookup table.
//
// Example output from list[], below:
// 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
// 0,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,
// 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,
@benwills
benwills / faster_tolower.c
Last active October 11, 2019 07:33
Faster Letter Comparison and Lowercasing in C
#include <time.h>
#include <stdint.h>
#include <sys/types.h>
#include <stdio.h>
#include <ctype.h>
/*
Character-by-character comparisons for equality and lowercasing.
Custom functions vs tolower.
Spoiler: lookup table is the fastest.
@benwills
benwills / timer.c
Created December 29, 2014 16:38
Simple Timer in C
int i;
float tStart, tDiff;
uint32_t runs = 1000;
tStart = (float)clock()/CLOCKS_PER_SEC;
for ( i=0; i<runs; ++i ) {
func();
}
tDiff = (float)clock()/CLOCKS_PER_SEC - tStart;
@benwills
benwills / chomp-line.c
Created December 27, 2014 09:29
Chomp Line in C
//https://github.com/bitly/dablooms/blob/master/src/test_dablooms.c
static void chomp_line(char *word)
{
char *p;
if ((p = strchr(word, '\r'))) {
*p = '\0';
}
if ((p = strchr(word, '\n'))) {
*p = '\0';
unsigned short timestamp = <some value>; // Bits: DDDDDHHHHHMMMMMM
int day = (timestamp >> 11) & 0x1F;
int hour = (timestamp >> 6) & 0x1F;
int min = (timestamp) & 0x3F;
unsigned short dup_timestamp = (short)((day << 11) | (hour << 6) | min);
// Alternative using macros
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
int main(int argc, char *argv[])
#include <stdio.h>
#include <unistd.h> //lseek, write, close
#include <stdlib.h> //exit
#include <fcntl.h> //open
#include <sys/mman.h>
#define Mapmalloc(number, type, filename, fd) load_mmap(filename, &(fd), (number)*sizeof(type), 'y')
#define Mapload(number, type, filename, fd) load_mmap(filename, &(fd), (number)*sizeof(type), 'n')
#define Mapfree(number, type, fd, pointer) releasemmap(pointer, (number)*sizeof(type), fd)
#define Stopifnot(assertion, ...) if (!(assertion)){printf(__VA_ARGS__); exit(1);}
/*
* $Id: simple-tcp-proxy.c,v 1.6 2002/11/27 00:40:31 wessels Exp $
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <signal.h>
/* Copyright (c) 2001 Matej Pfajfar.
* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2013, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file connection.c
* \brief General high-level functions to handle reading and writing
* on connections.
// $Id: simple-proxy.c,v 1.4 2005/02/25 04:23:24 dean Exp $
//
// simple-proxy.c: simple tcp proxy for async bi-directional data transfer
//
// usage: simple-proxy host port
//
// this will open a socket to host:port and copy data from stdin to the socket,
// and from the socket to stdout. errors may appear on stderr.
//