buffer: improve read/write speed with assert

Improved assert check order of execution and added additional checks on
parameters to ensure no bad values make it through (e.g. negative offset
values).