Skip to content

Instantly share code, notes, and snippets.

View 61131's full-sized avatar

Rob Casey 61131

  • Melbourne, Australia
View GitHub Profile
@61131
61131 / strrep.c
Last active August 9, 2019 08:58
Perform regular expression substitutions on a string
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <assert.h>
size_t
strlcpy(char * Destination, const char * Source, size_t Size) {
size_t uLength;
@61131
61131 / macros.h
Last active November 19, 2022 16:20
C pre-processor macro abuse to prepend number of arguments to variadic functions
#ifndef _MACROS_H
#define _MACROS_H
#include <stdint.h>
#include <stdarg.h>
/*
The following macros are used to prepend the number of arguments for
variadic functions.
*/
@61131
61131 / sstr.c
Last active December 27, 2021 08:44
Fat pointer implementation for dynamic string allocation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "sstr.h"
static void * __salloc (size_t n);
@61131
61131 / bind.c
Last active September 24, 2021 16:20
LD_PRELOAD library to restrict listening operations to localhost
/* Load through LD_PRELOAD to force listening operations to localhost */
/* cc -Wall -g -fPIC -shared -Wl,-init,init bind.c -o libbind.so -ldl */
/* $ LD_PRELOAD=./libbind.so nc -l 1234 */
#define _GNU_SOURCE
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
@61131
61131 / binnot.h
Last active April 14, 2020 22:51
C pre-processor macro to support binary notation within source
#ifndef _BINNOT_H
#define _BITNOT_H
#include <stdint.h>
/*
This macro provides the ability to use binary notation within C source. For
example - _B(10000001) = 129
*/
@61131
61131 / strl.c
Last active May 24, 2023 03:58
strlcat and strlcpy functions derived from OpenBSD libc
#include <string.h>
size_t
strlcat(char *dst, const char *src, size_t dsize) {
const char *odst = dst;
const char *osrc = src;
size_t count = dsize;
size_t length;
if ((dst == NULL) ||
@61131
61131 / dirtree.c
Last active June 14, 2023 03:00
Build array of all directories under given file location (without recursion!)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
/* See https://gist.github.com/61131/a4d5846dd2fb4aaa22ecf9e05a3b34a1 */
@61131
61131 / sha256.c
Created January 15, 2024 22:52
SHA-256 hash function in C
/* Copyright (c) 2001-2003 Allan Saddi <[email protected]>
* Copyright (c) 2023 Rob Casey <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
@61131
61131 / otp.c
Last active July 28, 2024 03:10
HMAC-based and time-based one-time password generation
#include <stdint.h>
#include <time.h>
#include <openssl/hmac.h>
int powi[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
// RFC 4226 HTOP: An HMAC-Based One-Time Password Algorithm
uint32_t
@61131
61131 / uuid.c
Last active July 30, 2024 00:22
Universally unique identifiers (UUIDv4) generation
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/time.h>