Adding security checks

This is a first serie of security fixes. It adds checks to not trust packet sizes and read only when we have enough bytes. Please apply.