diff --git a/src/cjs/index.cjs b/src/cjs/index.cjs index 07524c6..45c7330 100644 --- a/src/cjs/index.cjs +++ b/src/cjs/index.cjs @@ -82,8 +82,12 @@ function base (ALPHABET) { const b256 = new Uint8Array(size) // Process the characters. while (psz < source.length) { + // 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 } let i = 0 diff --git a/src/esm/index.js b/src/esm/index.js index ce48a14..1593e17 100644 --- a/src/esm/index.js +++ b/src/esm/index.js @@ -80,8 +80,12 @@ function base (ALPHABET) { const b256 = new Uint8Array(size) // Process the characters. while (psz < source.length) { + // 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 } let 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 5f2431a..9fa5c52 100644 --- a/ts_src/index.ts +++ b/ts_src/index.ts @@ -101,8 +101,14 @@ function base (ALPHABET: string): base.BaseConverter { // Process the characters. while (psz < source.length) { + // 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