Cygwin and the Case of the Unreadable Blues

Cygwin includes some nice built-in aliases for colorizing terminal output, but the default blue hue is so dark as to be unreadable on the black background. It’s easy to fix, though; just add the following Blue and BoldBlue lines to your ~/.minttyrc file:

Blue=64,64,255
BoldBlue=127,127,255

These lines will use a lighter shade of blue than the default, so you’ll be able to read it in a less-than-pitch-dark room. As an example, here’s my complete ~/.minttyrc file:

BoldAsFont=no
Transparency=high
Font=Consolas
FontHeight=10
ClicksTargetApp=no
Columns=120
Rows=52
ScrollbackLines=100000
Term=xterm-256color
Blue=64,64,255
BoldBlue=127,127,255

Hash strings to integers in PHP with the DJB hashing algorithm

I recently found myself needing a PHP implementation of the DJB hashing algorithm, but ran into a problem—in 64-bit PHP5, integers don’t overflow. Instead, they magically turn into floating point variables large enough to hold the new value. For short strings this isn’t really a problem (the hashing algorithm won’t cause an integer overflow in the first place), but for anything over five or six characters, you end up with numbers that aren’t comparable with other implementations of the algorithm (nor, for that matter, will they fit into any of MySQL’s numeric data types).

So, here’s a short function that uses PHP’s GNU Multiple Precision (GMP) module to perform the arithmetic at the necessary level of precision, then convert the result back to a standard PHP int:

define('PHP_INT_MIN', ~PHP_INT_MAX);

function hash_djb2($str){
	$hash = 5381;
	$length = strlen($str);

	for($i = 0; $i < $length; $i++) {
		$hash = gmp_add(gmp_mul($hash, 33), ord($str[$i]));
	        
		while (gmp_cmp($hash, PHP_INT_MAX) > 0) {
			$diff =	gmp_sub($hash, PHP_INT_MAX);
			$hash =	gmp_add($diff, PHP_INT_MIN);
			$hash =	gmp_sub($hash, 1); // off by 1
		}
	}
	return gmp_intval($hash);
}

I’ve only tested this on 64-bit PHP5 on Linux. PHP seems to handle integers a little differently across different platforms, so your mileage may vary. Enjoy!

PHP ZIP Extension for MAMP

Do you use MAMP as a web development testing environment on your Mac?

Do you need the PHP ZIP extension for dealing with archive files?

Are you running Mac OS X 10.6 Snow Leopard?

I do, and found making these tools play nice together to be far harder than it should have.  If you need to fix a similar setup, here are the steps that finally worked for me (on Mac OS X 10.6.4 with MAMP 1.9):

  1. Install XCode if you don’t already have it (we’re going to be doing a bit of compiling).
  2. Download the MAMP source code components (available towards the bottom of the page).
  3. When the MAMP source code package opens, go into the MAMP_src folder and double-click the php-5.3.2.tar.gz file (if you are using a different version of PHP, replace 5.3.2 with your actual version number).
  4. You should now have a php-5.3.2 folder in your Downloads folder.  Open up Terminal and cd to ~/Downloads/php-5.3.2.:
    cd ~/Downloads/php-5.3.2/
  5. Install the pcre.h header file (we need it to compile the extension):
    sudo cp ext/pcre/pcrelib/pcre.h /usr/include/php/ext/pcre/
  6. Configure PHP for i386 architecture:
    CFLAGS="-arch i386" ./configure
  7. Configure the ZIP extension for i386 architecture:
    cd ext/zip; CFLAGS="-arch i386" ./configure
  8. Build the extension:
    make
  9. Install the extension:
    cp modules/zip.so /Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/
  10. Enable the extension by opening /Applications/MAMP/conf/php5.3/php.ini and appending the following line:
    extension=zip.so
  11. Remove the pcre.h header file we installed earlier, we don’t need it anymore.
  12. Restart MAMP.  If everything worked properly, you’ll be able to goto http://localhost/MAMP, click on phpInfo, and see zip in the list of enabled extensions.

StrayLight Photography

I’ve been meaning to revamp the photography section of this site for a while now; this weekend, I finally found the time to do it. I registered a new domain, straylightphotography.com, and put together a portfolio consisting of my 20 favorite shots (<shamelessPlug>many of which are currently on display at Interzone through February 28th!</shamelessPlug>). I’m hoping to quickly expand the site with themed portfolios (portraits, urban decay, etc.), but… first things first.

Also, the new portfolio has been an excuse to play with CSS3 and jQuery 1.4.  Visitors using Firefox, Safari, Chrome, or Opera should see a site that behaves like it was created with Adobe Flash, but is fully accessible and doesn’t require the proprietary Flash plug-in.  Visitors using Internet Explorer… well… it at least degrades cleanly.  Mostly.