Avoid overflows in check whether there is enough available input space
Avoid overflows in check whether there is enough available input space

bytes_to_write can overflow int32 in a way that makes it look smaller than
AvailableInputSpace(). Instead do the comparison using size_t.
