X-Git-Url: https://git.ladys.computer/Pisces/blobdiff_plain/f0c3b510a532ded70c1a72ad753204b21d0f53bb..6e6d4e3261c1c943fe44fa9e381bcf8bf1441fd6:/binary.js?ds=sidebyside diff --git a/binary.js b/binary.js index 5c78d39..5ee2bda 100644 --- a/binary.js +++ b/binary.js @@ -135,10 +135,13 @@ const decodeBase16 = (source) => { ); const { length } = u4s; if (length % 2 == 1) { + // The length is such that an entire letter would be dropped during + // a forgiving decode. throw new RangeError( `Piscēs: Base16 string has invalid length: ${source}.`, ); } else { + // Every letter contributes at least some bits to the result. const dataView = new View(new Buffer(floor(length / 2))); for (let index = 0; index < length - 1;) { call(viewSetUint8, dataView, [ @@ -224,11 +227,15 @@ const decodeBase32 = (source, wrmg) => { }, ); const { length } = u5s; - if (length % 8 == 1) { + const lengthMod8 = length % 8; + if (lengthMod8 == 1 || lengthMod8 == 3 || lengthMod8 == 6) { + // The length is such that an entire letter would be dropped during + // a forgiving decode. throw new RangeError( `Piscēs: Base32 string has invalid length: ${source}.`, ); } else { + // Every letter contributes at least some bits to the result. const dataView = new View(new Buffer(floor(length * 5 / 8))); for (let index = 0; index < length - 1;) { // The final index is not handled; if the string is not divisible @@ -302,10 +309,13 @@ const decodeBase64 = (source, safe = false) => { ); const { length } = u6s; if (length % 4 == 1) { + // The length is such that an entire letter would be dropped during + // a forgiving decode. throw new RangeError( `Piscēs: Base64 string has invalid length: ${source}.`, ); } else { + // Every letter contributes at least some bits to the result. const dataView = new View(new Buffer(floor(length * 3 / 4))); for (let index = 0; index < length - 1;) { // The final index is not handled; if the string is not divisible