diff --git a/package.json b/package.json index 30061da..6da5e65 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "base-x", - "version": "4.0.0", + "version": "4.0.1", "description": "Fast base encoding / decoding of any given alphabet", "keywords": [ "base-x", diff --git a/src/index.js b/src/index.js index 11e1a13..f368a05 100644 --- a/src/index.js +++ b/src/index.js @@ -81,8 +81,12 @@ function base (ALPHABET) { var b256 = new Uint8Array(size) // Process the characters. while (source[psz]) { + // Find code of next character + var charCode = source.charCodeAt(psz) + // Base map can not be indexed using char code + if (charCode > 255) { return } // Decode character - var carry = BASE_MAP[source.charCodeAt(psz)] + var carry = BASE_MAP[charCode] // Invalid character if (carry === 255) { return } var i = 0 diff --git a/test/fixtures.json b/test/fixtures.json index f8eedbf..3aadd67 100644 --- a/test/fixtures.json +++ b/test/fixtures.json @@ -660,6 +660,12 @@ "alphabet": "0123456789fabcdef", "description": "poorly formed alphabet", "exception": "^TypeError: f is ambiguous$" + }, + { + "alphabet": "base58", + "description": "character whose code exceeds the highest index of base map (>=256)", + "exception": "^Error: Non-base58 character$", + "string": "\u1000" } ] } diff --git a/ts_src/index.ts b/ts_src/index.ts index 2e71501..f9604b6 100644 --- a/ts_src/index.ts +++ b/ts_src/index.ts @@ -100,8 +100,14 @@ function base (ALPHABET: string): base.BaseConverter { // Process the characters. while (source[psz]) { + // Find code of next character + const charCode = source.charCodeAt(psz) + + // Base map can not be indexed using char code + if (charCode > 255) return + // Decode character - let carry = BASE_MAP[source.charCodeAt(psz)] + let carry = BASE_MAP[charCode] // Invalid character if (carry === 255) return