Add assert()s, remove 'const' on local variables, fix print() buf where it tried to write a '\0' past the end of the buffer if cnt > buffer_size.