## Bit Twiddling Hacks

I recently implemented an algorithm that has to perform checks on all subsets of some large set. A subset of an $n$-sized set can be understood as a binary string of length $n$ where bit $i$ is set if and only if the $i$-th element is in the subset. During my search for code to enumerate such bitstrings, I found the greatest page in the entire internet. If anyone can explain to me how computing the next bit permutation (the last version) works, please do.

## Simple WordPress Update Script

I need to update this wordpress install every once in a while. There are lots of bash scripts on the internet that perform this task, and they are complicated beyond reason. This is what I use:
function cfg {
grep $2$1/wp-config.php | awk 'BEGIN {FS="[, )\x27]*"}; {print $3;}' } echo "> backing up database." mysqldump --user=$(cfg $1 DB_USER) \ --password=$(cfg $1 DB_PASSWORD) \ --host=$(cfg $1 DB_HOST) \$(cfg $1 DB_NAME) > backup.database.sql echo "> backing up website." tar -cjf backup.files.bz2$1

echo "> retrieving latest wordpress."
wget -q https://wordpress.org/latest.zip
unzip -qq latest.zip

echo "> updating wordpress."
rm -r $1/wp-includes$1/wp-admin

## Eclipse CDT with MinGW 64

Let's say you have a Windows PC on a 64 bit machine. Obviously, you have Cygwin because anything else would just be silly. Then you want to develop C-Applications that you can compile and run on large Linux-based servers, so you turn to an an IDE that can work with Cygwin. Now here comes the twist: For whatever stubborn reason, you want to be able to compile your applications native 64 bit, and you want to do it in Eclipse, and you want to be able to debug them. Do you want to know how?

## VirusTotal Python Script

Here's my code for quickly uploading files to virustotal and retrieving the reports.
import postfile
import sys

import json
from StringIO import StringIO

import urllib
import urllib2

import time

import webbrowser

apikey = 'YOUR API KEY ' + \
'  GOES HERE  '
resources = []
for i in range(1, len(sys.argv)):
file = sys.argv[i]
print 'Preparing Scan of %s ...' % file

host = 'www.virustotal.com'
selector = 'https://www.virustotal.com/vtapi/v2/file/scan'
fields = [('apikey', apikey)]
files = [('file', file, file_to_send)]

ret = postfile.post_multipart(host, selector, fields, files)
try:
except ValueError:
print 'Cannot decode server response: '
print ret
exit()

# for k in data: print '%s: %s' % (k, data[k])

resources.append((file, data['resource']))

print 'Retreiving reports...'
i = 1
for resource in resources:
response_code = 0
while response_code == 0:
url = 'https://www.virustotal.com/vtapi/v2/file/report'
parameters = {
'resource': resource[1],
'apikey': apikey
}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
response_code = data['response_code']
#print json.dumps(data, sort_keys=True, indent=4)
if response_code == 0: time.sleep(5)
#print json.dumps(data, sort_keys=True, indent=4)
print '%2i: %s' % (i, resource[0]),
print ': %i / %i' % (data['positives'], data['total'])
i += 1

wb = webbrowser.get()
selection = 0
while selection >= 0 and selection < len(permalinks):
selection = int(raw_input('Open: '))-1
if selection >= 0 and selection < len(permalinks):

P.S.: This is all part of a great plan I'm following at the moment. Edit (2013-09-05): Since the VirusTotal API is now out there for a while, a lot of awesome python libraries have emerged: * https://github.com/Erethon/vta.py * https://github.com/Gawen/virustotal * https://github.com/botherder/virustotal * https://github.com/Xen0ph0n/VirusTotal_API_Tool

## GMP from source with MinGW-64

Under Cygwin, you can install the 64 bit mingw version of GCC, but you don't get the gnu multiprecision library for free with it, you'll much rather have to compile it from source. I ran into a bit of trouble here: It will not suffice to tell the configuration script about the new compiler, there are now mingw-64 versions of all relevant binaries that should be used instead. Basically, you go like
tar -xjf gmp-5.0.4.tar.bz2
cd gmp-5.0.4
./configure                          \
AR=x86_64-w64-mingw32-ar           \
AS=x86_64-w64-mingw32-as           \
DLLTOOL=x86_64-w64-mingw32-dlltool \
DLLWRAP=x86_64-w64-mingw32-dllwrap \
CC=x86_64-w64-mingw32-gcc-4.5.3    \
NM=x86_64-w64-mingw32-nm           \
LD=x86_64-w64-mingw32-ld           \
OBJDUMP=x86_64-w64-mingw32-objdump \
RANLIB=x86_64-w64-mingw32-ranlib   \
STRIP=x86_64-w64-mingw32-strip

I am not sure if all of these are needed, but it won't hurt either. After that, you should
make && make check

the whole thing. Worked perfectly for me, so now I can link with libgmp.a in .libs and native 64 bit bignum action ensues!