src: refactor buffer bounds checking

Consolidate buffer bounds checking logic into Buffer namespace and use
it consistently throughout the source.