The base64 decoder would intermittently throw an out-of-bounds exception when the buffer in `buf.write('', 'base64')` was a zero-sized buffer located at the end of the slab. Fixes #2657.
Honor the length argument in `buf.write(s, 0, buf.length, 'base64')`. Before this commit, the length argument was ignored. The decoder would keep writing until it hit the end of the buffer. Since most buffers in Node are slices of a parent buffer (the slab), this bug would overwrite the content of adjacent buffers. The bug is trivially demonstrated with the following test case: var assert = require('assert'); var a = Buffer(3); var b = Buffer('xxx'); a.write('aaaaaaaa', 'base64'); assert.equal(b.toString(), 'xxx'); This commit coincidentally also fixes a bug where Buffer._charsWritten was not updated for zero length buffers.
Thanks to Jeremy Barnes for pointing it out.
This reverts commit 909a5b3. Will fix inside V8's String::New instead.
Plus make inclusion order a little more consistent in general
+ Utf8Length is really slow but has the same semantics in this case