Skip to content

Instantly share code, notes, and snippets.

View kulp's full-sized avatar
💬
ceci n'est pas un statut

Darren Kulp kulp

💬
ceci n'est pas un statut
View GitHub Profile
@kulp
kulp / dehex.c
Created April 21, 2009 20:56
Convert a hex string into integer or character buffer
static inline char _hexify(const char c)
{
return (((c | ('A' ^ 'a')) - '0') % 39);
}
/// Converts a len-digit hex number into its unsigned native representation.
unsigned int dehex_to_int(int ilen, const char ibuf[ilen])
{
unsigned int result = 0;
@kulp
kulp / bin2hex.c
Created April 28, 2009 13:40
Convert N-byte string into hex
/// Convert N-byte string into hex
char* tohex(int len, const char input[len])
{
char *buf = malloc(2 * len + 1);
int pos = 0;
for (const char *i = input; i < input + len; i++)
pos += sprintf(&buf[pos], "%02x", *i);
return buf;
}
@kulp
kulp / svnbackup.sh
Created May 16, 2009 21:42
Back up Subversion repositories incrementally (easily run from cron).
#!/bin/bash
# Back up Subversion repositories. Run from cron.
# Somewhat ironic that I post this to github ...
svndir=$HOME/svn
destdir=$HOME/svnbackups
dumpcmd="svnadmin dump --quiet --incremental"
repos=`find "$svndir" -maxdepth 1 -mindepth 1 -type d -exec basename {} \;`
for repo in $repos; do
oldend=`ls -1t $destdir/$repo.*.*.* 2>/dev/null | head -n1 | cut -d. -f 3`
/* Find set bits by value. process_faster() is between 40% and 200% faster than process_naive(), depending on optimizations and environment. */
void process_naive(long long x)
{
long long val = 1;
while (x) {
if (x & 1)
record(val);
x >>= 1;
val <<= 1;
}
@kulp
kulp / cyclo.diff
Created May 28, 2009 15:07
Make cyclo show only C function names instead of concatenating pieces of the signature. Won't be nice to C++.
--- cyclo-2.0/lib.C 1994-07-03 13:12:34.000000000 -0500
+++ cyclo-2.0-kulp/lib.C 2009-05-28 10:08:23.000000000 -0500
@@ -23,35 +23,16 @@
extern "C" // called from 'C' scan.c
{
-char *strndup(char *str, int n)
-{
- char *t, *t2;
- t2=t=(char *)malloc(n+1*sizeof(char));
@kulp
kulp / cyc.sh
Created May 28, 2009 15:34
Show C functions sorted by decreasing cyclomatic complexity.
function cyc()
{
mcstrip $1 | cyclo -c -n 0 | perl -le 'print for map "@$_", sort { $b->[1] <=> $a->[1] } map [ split ], <>' | column -t
}
@kulp
kulp / undefined_syms
Created May 28, 2009 19:09
Finds undefined symbols in OBJECTS that are not defined in SUPPORT_OBJECTS.
undefsym = (nm -pAP $(2); nm -puAP $(1)) | cut -d' ' -f1-2 | sort -k2 | uniq -u -f1 | grep '^$(1)' | cut -d: -f2 | cut -b2-
OBJECTS = work.o
SUPPORT_OBJECTS = support.o
.PHONY: need
need: ; @$(call undefsym,$(OBJECTS),$(SUPPORT_OBJECTS))
@kulp
kulp / c_encode_string.c
Created June 8, 2009 19:43
Copies a string, replacing unprintable and some punctuation characters with their C-escaped versions.
/**
* Copies a string from 'in' to 'out', replacing unprintable and some
* punctuation characters with their escaped versions.
*
* To be safe, the output buffer should be allocated with (4 * ilen) characters
* of space, for the worst case scenario, where all characters are unprintable.
*
* @param ilen the length of the input string
* @param in the input string
* @param olen the length of the output buffer
#define NEW_OF_TYPE(T,...) \
(memcpy(malloc(sizeof(T)), &(T){ __VA_ARGS__ }, sizeof (T)))
@kulp
kulp / setuid.c
Created February 11, 2010 16:10
fake sudo for people with temporary ability to create setuid binaries
main(int c,char**v){c<2||(setuid(0),execvp(v[1],&v[1]));}