11import BigNumber from './BigNumber.js'
22import { hash256 } from './Hash.js'
3+ import { assertValidHex , normalizeHex } from './hex.js'
34
45const BufferCtor =
56 typeof globalThis !== 'undefined' ? ( globalThis as any ) . Buffer : undefined
@@ -80,28 +81,19 @@ for (let i = 0; i < 6; i++) {
8081}
8182
8283const hexToArray = ( msg : string ) : number [ ] => {
83- if ( CAN_USE_BUFFER && PURE_HEX_REGEX . test ( msg ) ) {
84- const normalized = msg . length % 2 === 0 ? msg : '0' + msg
84+ assertValidHex ( msg )
85+ const normalized = msg . length % 2 === 0 ? msg : '0' + msg
86+ if ( CAN_USE_BUFFER ) {
8587 return Array . from ( BufferCtor . from ( normalized , 'hex' ) )
8688 }
87- const res : number [ ] = new Array ( Math . ceil ( msg . length / 2 ) )
88- let nibble = - 1
89- let size = 0
90- for ( let i = 0 ; i < msg . length ; i ++ ) {
91- const value = HEX_CHAR_TO_VALUE [ msg . charCodeAt ( i ) ]
92- if ( value === - 1 ) continue
93- if ( nibble === - 1 ) {
94- nibble = value
95- } else {
96- res [ size ++ ] = ( nibble << 4 ) | value
97- nibble = - 1
98- }
99- }
100- if ( nibble !== - 1 ) {
101- res [ size ++ ] = nibble
89+ const out = new Array ( normalized . length / 2 )
90+ let o = 0
91+ for ( let i = 0 ; i < normalized . length ; i += 2 ) {
92+ const hi = HEX_CHAR_TO_VALUE [ normalized . charCodeAt ( i ) ]
93+ const lo = HEX_CHAR_TO_VALUE [ normalized . charCodeAt ( i + 1 ) ]
94+ out [ o ++ ] = ( hi << 4 ) | lo
10295 }
103- if ( size !== res . length ) res . length = size
104- return res
96+ return out
10597}
10698
10799export function base64ToArray ( msg : string ) : number [ ] {
@@ -237,6 +229,7 @@ export const toUTF8 = (arr: number[]): string => {
237229
238230 for ( let i = 0 ; i < arr . length ; i ++ ) {
239231 const byte = arr [ i ]
232+
240233 // this byte is part of a multi-byte sequence, skip it
241234 // added to avoid modifying i within the loop which is considered unsafe.
242235 if ( skip > 0 ) {
@@ -247,41 +240,26 @@ export const toUTF8 = (arr: number[]): string => {
247240 // 1-byte sequence (0xxxxxxx)
248241 if ( byte <= 0x7f ) {
249242 result += String . fromCharCode ( byte )
250- continue
251- }
252-
253- // 2-byte sequence (110xxxxx 10xxxxxx)
254- if ( byte >= 0xc0 && byte <= 0xdf ) {
255- const avail = arr . length - ( i + 1 )
256- const byte2 = avail >= 1 ? arr [ i + 1 ] : 0
257- skip = Math . min ( 1 , avail )
258-
243+ } else if ( byte >= 0xc0 && byte <= 0xdf ) {
244+ // 2-byte sequence (110xxxxx 10xxxxxx)
245+ const byte2 = arr [ i + 1 ]
246+ skip = 1
259247 const codePoint = ( ( byte & 0x1f ) << 6 ) | ( byte2 & 0x3f )
260248 result += String . fromCharCode ( codePoint )
261- continue
262- }
263-
264- // 3-byte sequence (1110xxxx 10xxxxxx 10xxxxxx)
265- if ( byte >= 0xe0 && byte <= 0xef ) {
266- const avail = arr . length - ( i + 1 )
267- const byte2 = avail >= 1 ? arr [ i + 1 ] : 0
268- const byte3 = avail >= 2 ? arr [ i + 2 ] : 0
269- skip = Math . min ( 2 , avail )
270-
249+ } else if ( byte >= 0xe0 && byte <= 0xef ) {
250+ // 3-byte sequence (1110xxxx 10xxxxxx 10xxxxxx)
251+ const byte2 = arr [ i + 1 ]
252+ const byte3 = arr [ i + 2 ]
253+ skip = 2
271254 const codePoint =
272255 ( ( byte & 0x0f ) << 12 ) | ( ( byte2 & 0x3f ) << 6 ) | ( byte3 & 0x3f )
273256 result += String . fromCharCode ( codePoint )
274- continue
275- }
276-
277- // 4-byte sequence (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)
278- if ( byte >= 0xf0 && byte <= 0xf7 ) {
279- const avail = arr . length - ( i + 1 )
280- const byte2 = avail >= 1 ? arr [ i + 1 ] : 0
281- const byte3 = avail >= 2 ? arr [ i + 2 ] : 0
282- const byte4 = avail >= 3 ? arr [ i + 3 ] : 0
283- skip = Math . min ( 3 , avail )
284-
257+ } else if ( byte >= 0xf0 && byte <= 0xf7 ) {
258+ // 4-byte sequence (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)
259+ const byte2 = arr [ i + 1 ]
260+ const byte3 = arr [ i + 2 ]
261+ const byte4 = arr [ i + 3 ]
262+ skip = 3
285263 const codePoint =
286264 ( ( byte & 0x07 ) << 18 ) |
287265 ( ( byte2 & 0x3f ) << 12 ) |
@@ -292,7 +270,6 @@ export const toUTF8 = (arr: number[]): string => {
292270 const surrogate1 = 0xd800 + ( ( codePoint - 0x10000 ) >> 10 )
293271 const surrogate2 = 0xdc00 + ( ( codePoint - 0x10000 ) & 0x3ff )
294272 result += String . fromCharCode ( surrogate1 , surrogate2 )
295- continue
296273 }
297274 }
298275
0 commit comments