Added check to ensure packed.length is never greater than bufsiz to avoid infinite loop condition.