Voder-Vocoder

The Log of Hal Canary

Navigation: Home | THE LOG | Log Archives | Resume | Contact Info | Public Key | SSL | Math Applets | Site Map | WP Backend | RSS2 | Atom

Archive for the “Computers & Code” Category

« Previous Entries Next Entries »

Excessively long URLs.

I hate unnecessarily long URLs. One of the things I always try to figure out how to leave out is the file name extension. As Tim Berners-Lee says:

"cgi", even ".html" is something which will change. You may not be using HTML for that page in 20 years time, but you might want today’s links to it to still be valid. The canonical way of making links to the W3C site doesn’t use the extension

Here are two ways of doing this with Apache.

1) Insert this line into your ".htaccess" file:

DefaultType text/html

Then when you create any new html pages, call them "foobar" instead of "foobar.html" and then your URL will be:

http://example.com/directory/foobar

and not

http://example.com/directory/foobar.html

Which makes more sense. Now you can at some later point in time change the default to .shtml or .php

2) Put this in your ".htaccess" file:

Options All MultiViews

Then put a file called "foobar.html" in the directory "directory/" and the URL:

http://example.com/directory/foobar

will automagically work.


Other options:

3) Here’s a third way:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html  -f
RewriteRule ^(.*)$           $1.html

Hal Canary | Computers & Code | 2007-06-28 10:14:14 EDT
Permanent Link | Comments Off

Firefox Addons

Favorite Firefox Addons:

Go Up

Lets you go "up" a level in the current website via the provided toolbar button, or by pressing Alt+Up arrow. The "up" is determined by trimming the last section of the URL, e.g. example.org/foo/somepage.html becomes example.org/foo/

I don’t use this much, but it does come in handy every so often.

Flashblock

Flashblock is an extension for the Mozilla, Firefox, and Netscape browsers that takes a pessimistic approach to dealing with Macromedia Flash content on a webpage and blocks ALL Flash content from loading. It then leaves placeholders on the webpage that allow you to click to download and then view the Flash content.

This is a lifesaver. Try moving your mouse cursor over a piece of flash, then hitting the page-down key. Nothing happens, unless the flash is blocked.

MM3-ProxySwitch

In the Firefox Browser (and other Mozilla programs) you can per default configure only the setting for one internet connection. With the MM3-ProxySwitch you can manage different configurations and simply switch over between these.

I use this one on my laptop for managing proxies.

FullerScreen

This extension enhances the Full Screen mode into a really full screen mode, hiding the remaining toolbars and statusbar and making them visible again when the mouse pointer hits an edge of the screen.

It also offers a slideshow mode, enabling @projection CSS rules in a document when full screen mode is turned on. Navigation between slides in implemented in the extension and a slide manager showing thumbnails for the slides is available through shift-F11.

I’m still trying this out, but it seems like a good idea.

Hal Canary | Computers & Code | 2007-06-25 09:17:51 EDT
Permanent Link | 1 Comment

Unix Utility Scripts

This page collects several of the scripts I’ve written for Linux over the years.

Maybe someone else will find them useful.

Hal Canary | Computers & Code | 2007-06-12 10:26:46 EDT
Permanent Link | Comments Off

find-and-sha1sum

#!/bin/sh
# this script will calculate the sha1sum
# for each regular file in the PWD.
# and put it into a file called SHA1SUM.txt
# It prints one "." to the stderr for each
# file processed.
# DTPD

[ -f "SHA1SUM.txt" ] && rm -i SHA1SUM.txt
TMPFILE=`mktemp`
find . -type f \
	-exec sha1sum {} ';' \
	-fprintf /dev/stderr '.' \
	> "${TMPFILE}"
mv "${TMPFILE}" SHA1SUM.txt
echo '' 2>&1

Hal Canary | Computers & Code | 2007-05-17 18:25:57 EDT
Permanent Link | Comments Off

TAOCP/1

I bought a boxed set of Knuth’s Art of Computer Programming last week. As you may or may not know, TAOCP demonstrates programs in assembly for a mythical computer known as the MIX 1009. MIX would not have been out of place in 1965, but is hopelessly outdated today. Nonetheless, I learned enough MIX to write the following program in the MIX Assembly Language.

* factorial.mixal
*
* calculate n factorial in MIXAL
*
* loops on rI1 from n down to 0
*
* n! must fit into a 30-byte word, or it overflows
* n must fit into a 12-byte index register,
* n must be positive.
*
* if n! overflows, it will throw an error.
*
* Tested with GNU MDK 1.2.3.
*
* assemble with:
*	mixasm factorial.mixal
* execute with:
*	mixvm --run factorial.mix
*
* This program is dedicated to the public domain.
*      ---Hal Canary, http://halcanary.org/
*
n	EQU	11	* Argument
pp	EQU	1000	* pointer to a memory address.
*			* We make use of pp and pp+1
term    EQU     19	* terminal
        ORIG    3000
start	ENTA	1
	ENTX	0
	ENT1	n
loop	ST1	pp
	MUL	pp	* pp! == pp * (pp-1)!
	CMPA	0	* Check for overflow
	JNE	ovrflw
	SLAX	5	* Shift rX back to rA.
	DEC1	1	* k <- k-1
	J1P	loop	* if (k > 0) loop back
	CHAR	0	* rAX <- sprintf("%i", rA)
	STX	pp+1
	STA	pp
	OUT	pp(term)	* output pp and pp+1
        HLT
ovrflw	OUT	error0(term)
	HLT
error0	ALF	OVERF
	ALF	LOW
        END     start

Notice how I had to write to memory in order to multiply two numbers together! Crazy.

(Factorial calculations are a common assignment in computer science 101, as they are a simple exercise in a language’s looping constructs.)

Here’s the same program, reimplemented for the Emmix 2009:

* factorial.mmo
*
* Calculate factorial(NUMBER) in MMIXAL.
* If it overflows, it exits with no output.
*
* Tested with Donald E. Knuth's MMIXware (v20060918).
*
* Assemble with:
*	mmixal factorial.mms
* Execute with:
*	mmix factorial.mmo
*
* This program is dedicated to the public domain.
*	---Hal Canary, http://halcanary.org/

NUMBER	IS	12

	LOC	Data_Segment
buf	BYTE	0		this buffer will be 21
	LOC	buf+21			   bytes long
pbuf	GREG	@		pointer to the end of
num	OCTA	NUMBER			   the buffer
pnum	GREG	num
n	IS	pnum		the number
r	GREG	0		the remainder
k	IS	r		counter
ov	GREG	0

	LOC	#100
Main	LDO	n,pnum,0	load number from memory
	SUB	k,n,1
1H	MULU	n,n,k
	GET	ov,rH		check for overflow
	BP	ov,overflow
	SUB	k,k,1
	PBP	k,1B

* Print out n, followed by a newline.
	SET	r,0		'\0' character
	STBU	r,pbuf
	SUB	pbuf,pbuf,1
	SET	r,10		'\n' char
	STBU	r,pbuf
2H	SUB	pbuf,pbuf,1
	DIVU	n,n,10
	GET	r,rR		final digit is the remainder
	INCL	r,'0'		convert digit to ASCII char
	STBU	r,pbuf		store digit char in buffer
	PBP	n,2B		if there is more chars, loop
	SET	$255,pbuf	set $255 to point to the
	TRAP	0,Fputs,StdOut		most signif. digit.
	TRAP	0,Halt,0

overflow TRAP	0,Halt,0

This processor required me to (gasp) rewrite ‘printf(”%u”,n);’, which was the hardest part of the program.

Hal Canary | Computers & Code | 2007-05-12 12:19:20 EDT
Permanent Link | Comments Off

because I hate navigating info pages

#!/bin/sh
#~/bin/infoless
#DTPD#
if [ $# -ne 1 ] ; then
  echo "which page?"
  exit 1
fi
exec info $1 2> /dev/null | less

Hal Canary | Computers & Code | 2007-05-10 11:25:41 EDT
Permanent Link | Comments Off

Presentation last night

Here’s the link to the presentation I gave last night. It would have been better had there been more people show up at LUG, but that happens.

Check out the fancy javascript on that page!

Hal Canary | Computers & Code | 2007-04-11 14:25:35 EDT
Permanent Link | Comments Off

(X)Ubuntu on my P3-700 laptop

This is a post I wrote almost a month ago and never posted:

2007-03-11

I went to the Linux Users Group the other night. The local LUG tries to have a presentation at every month’s meeting. This is ambitious goal, so I thought I’d help out. I went home and started putting together a presentation on F/OSS crytography tools.

Then I realized that if I was going to give a presentation in front of the Linux group, I ought to get Linux working on my laptop. This has been a problem since I got this used laptop two years ago, since the CDROM drive refuses to read any CDs I put in it. So I did more research this week and found out that I can bootstrap from inside windows a copy of linux to run a network installer.

I chose to install Ubuntu Linux because it has a reputation for good support for wifi cards. Without WiFi, a notebook is mostly useless to me. I had never installed Ubuntu before. In a sense, I still havn’t: I install XUbuntu instead. XUbuntu is a scaled down version of Ubuntu that is supposed to use less resources by replacing Gnome with Xfce.

$ cat /proc/cpuinfo | head -8
vendor_id       : GenuineIntel
cpu family      : 6
model           : 8
model name      : Pentium III (Coppermine)
stepping        : 10
cpu MHz         : 700.000
cache size      : 256 KB

$ cat /proc/meminfo  | head -1
MemTotal:       385828 kB

This thing has got a 700 MHz Pentium III (Coppermine mobile) processor, which makes the processor design almost seven years old.

Thoughts on this setup:

1) (X)Ubuntu works pretty well. I’m in the proccess of learning all about apt-get; otherwise a lot of my RH/Fedora knowledge translates over to Ubuntu.

2) Xfce is still a little rough round the edges, but it does what I want it to. I miss Gnome’s Rhythmbox and Gedit, but I should learn to make do with less.

3) The wireless drivers work perfectly, BUT the GUI frontend (network-admin) is useless. I ended up writing my own scripts. This one lists all the availible networks.

#!/bin/sh
## ~/bin/scanwifi
## 2007 Hal Canary
## Dedicated to the Public Domain
sudo echo 'Restarting and scanning Wifi...'
echo '>>> ifconfig eth1 down >>>'
sudo ifconfig eth1 down
echo '>>> ifconfig eth1 up >>>'
sudo ifconfig eth1 up
echo '>>> iwconfig eth1 essid any >>>'
sudo iwconfig eth1 essid any
echo '>>> iwconfig eth1 >>>'
sudo ifconfig eth1
echo '>>> iwlist eth1 scanning >>>'
sudo iwlist eth1 scanning
exit 0

This one connects to the selected network:

#!/bin/sh
## ~/bin/connectwifi
## 2007 Hal Canary
## Dedicated to the Public Domain
if [ "$#" -lt 1 ] ; then
	echo "useage:"
	echo "  $0 SSID"
	echo "or:"
	echo "  $0 SSID KEY"
	exit 1
fi
echo '########## iwconfig eth1 essid "'$1'" ########'
sudo iwconfig eth1 essid "$1"
if [ ! -z "$2" ] ; then
	echo '########## iwconfig eth1 key "'$2'" ########'
	sudo iwconfig eth1 key "$2"
fi
echo '########## dhclient eth1 ########'
sudo dhclient eth1
exit 0;

And this one connects to my home network (not my real key):

#!/bin/sh
## ~/bin/connectwifi.MySsid
connectwifi MySsid 0c0dc52de9fbc078f3c1411100

Disconnecting can be accomplished with a

sudo ifconfig eth1 down

or by simply removing the network card.

4) In theory this laptop’s battery has only about an hour’s charge on it, but I think that if I keep disc and CPU useage down to a minimum, it can last longer. I’ve been sitting here outside for almost an hour and it’s at 46%. This is to be expected with a LiIon battery this old. If I get a new battery for this thing, it would cost around $100. We got this whole laptop for less than that.

5) It’s geeting cold out, so I’m going to go home now.

6) Ubuntu disallows the use of the login name ‘hal’, reserving it for the hardware abstraction layer daemon. This makes me sad.

7) Ubuntu has a much more lax default security policy. Iptables is off my default. There is no SELinux.

Hal Canary | Computers & Code | 2007-04-11 14:13:25 EDT
Permanent Link | Comments Off

genpasswd

#!/bin/sh
# genpasswd.sh
#   Generate a random password with almost
#   144 bits of randomness, making use of
#   /dev/random.
# Note:
#   Most online services have somewhat
#   arbitrary rules about what characters
#   can be included in a password. So we
#   limit ourselves to A-Za-z0-9.
# Copyright 2007 Hal Canary
# Dedicated to the Public Domain.
echo "Grabbing bits from /dev/random..." 1>&2
head -c 18 /dev/random | base64 | \
        sed 's/\//Z/g;s/+/z/g;'
# If you lack base64 on your system, try:
# head -c 18 /dev/random  | uuenview -b '' | \
#       sed 's/\//Z/g;s/+/z/g;'

Exactly how much entropy do we get?

Each character can be a z or a Z with a probability of 2/64 for each. The other 60 characters have a probability of 1/64 each. Apply the formula:

information entropy = \displaystyle{\sum_{i=1}^np(x_i)\log_2 \left(1/p(x_i)\right)}
= \displaystyle{\sum_{i=1}^{2}\frac{2}{64} \log_2 \left({\frac{64}{2}}\right)} + \displaystyle{\sum_{i=1}^{60}\frac{1}{64} \log_2 \left({\frac{64}{1}}\right)}
= 2 \left({\frac{2}{64}}\right) \log_2 \left({\frac{64}{2}}\right) + 60 \frac{1}{64} \log_2 \left({\frac{64}{1}}\right)
= 2 \left({\frac{2}{64}}\right) \left({5}\right) + 60 \frac{1}{64} \left({6}\right) = \frac{20}{64} + \frac{360}{64} = \frac{380}{64} = 5.9375

Mulitply by 24 for 24 characters, and get 142.5 bits of entropy.

Hal Canary | Computers & Code | 2007-04-05 14:05:04 EDT
Permanent Link | Comments Off

txt2prehtml

#!/bin/sh
# txt2prehtml
# Copyright 2006-2007 Hal Canary
# DTPD
echo -n '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
  content="text/html; charset=utf-8">
<title> </title>
</head>
<body>
<pre>' ;
fold -w 70 -s "$@" | sed 's/\&/\&amp;/g;
s/\"/\&quot;/g;
s/</\&lt;/g;
s/>/\&gt;/g;
s/\\/\&\#0092;/g;' ;
echo '</pre>
</body>
</html>';

Hal Canary | Computers & Code | 2007-03-28 09:11:21 EDT
Permanent Link | Comments Off

makecdrfiles

Some years ago, cdrecord gained the ability to convert .wav files to an audio CD on the fly. No more messing around with sox! But I’ve found that it’s better to feed files through sox anyways, just to make sure that the file format is correct and the sampling rate is correct:

#!/bin/sh

# makecdrfiles - Convert mp3s to
# .cdr format for an audio CD.
# Copyright 2000-2007 Hal Canary
# Dedicated to the Public Domain.

if [ "$#" -lt 1 ] ; then
  echo "  Useage: $0 file.mp3 [more files.mp3]"
  echo ""
  echo "  After you're done, burn with:"
  echo -n '   sudo cdrecord -v -dao -eject dev=$DEV'
  echo ' -pad -audio *.cdr'
  exit 1
fi
for FILE in "$@" ; do
  nice lame --decode "$FILE" - | \
    nice sox -t wav - "$(basename $FILE .mp3).cdr"
done

I burnt four coasters today before I figured this out.

Hal Canary | Computers & Code | 2007-03-23 11:52:20 EDT
Permanent Link | Comments Off

timeinmicroseconds.c

I wrote this today as a demonstration of where /dev/random gets all its randomness.

/* Copyright 2007 Hal Canary, http://halcanary.org/
Dedicated to the Public Domain. */
#include <stdio.h>
#include <sys/time.h>
int main(void) {
        struct timeval x;
        struct timezone t;
        gettimeofday(&x, &t);
        printf("%u.%06u\n",x.tv_sec,x.tv_usec);
        return(0);
}

And then…

$ cc -o timeinmicroseconds timeinmicroseconds.c
$ while read -s -n 1 x ; do ./timeinmicroseconds ; done

Hal Canary | Computers & Code | 2007-03-22 15:50:15 EDT
Permanent Link | Comments Off

« Previous Entries Next Entries »

Copyright 1997-2007 by Hal Canary.
mailto: h3 at halcanary dot org
xmpp:halcanary@jabber.org
aim:halwcanary
http://halcanary.org