Initial port from Masaru OKI <oki@netbsd.org>, with KNF, memory leak fixes, denial of service problems, and null pointer fixes from me.