Bug: you must check that datalen > 0 before doing lines 510-513.