hex2bin() Never Returns If More Than 1 Million Chars
greatkiwi opened this issue · comments
greatkiwi commented
- Have you checked the guidelines in our Contributing document?
Description
"hex2bin()" Never Returns If More Than 1 Million Chars
The following wrapper function explains and fixes this (though obviously, a proper fix of the original function might be better)...
function hex2bin( instr ) {
// -----------------------------------------------------------------------
// This is a wrapper around the Locutus "hex2bin()". Because the Locutus
// "hex2bin()" has a bug. In that it will handle strings of max:-
// 1,000,000
// characters/bytes.
//
// A:-
// 1,000,002
// character/byte long string will crash it (in that it never returns).
// -----------------------------------------------------------------------
var chunk_len = 1000000 ; // 1 million (exactly)
if ( instr.length <= chunk_len ) {
return hex2bin_locutus_original( instr ) ;
}
var chunk_count = Math.ceil( instr.length / chunk_len ) ;
var out = '' , temp ;
for ( var i=0 ; i<chunk_count ; i++ ) {
temp = hex2bin_locutus_original(
instr.substr(
i * chunk_len ,
chunk_len
)
) ;
if ( typeof temp !== 'string' ) {
return temp ;
}
out += temp ;
}
return out ;
// -----------------------------------------------------------------------
}
Rafał Kukawski commented
Hi. Thank you for the bug report. I think the function can be improved to workaround the limit, as the function calls String.fromCharCode
with all the "million" codes at once (which is most likely above the browser-specific limit for function params). In such case I'd expect the function to throw an error. I'll look into this in coming days.