diff --git a/libs/fmt/fmt.c b/libs/fmt/fmt.c index 790971ce9..a0da38e85 100644 --- a/libs/fmt/fmt.c +++ b/libs/fmt/fmt.c @@ -750,7 +750,7 @@ static void md5_finish( md5_context *ctx, uint8 digest[16] ) { HL_PRIM void HL_NAME(digest)( vbyte *out, vbyte *in, int length, int format ) { if( format & 256 ) { - in = (vbyte*)hl_to_utf8((uchar*)in); + in = (vbyte*)hl_to_utf8_len((uchar*)in, length); length = (int)strlen((char*)in); } hl_blocking(true); diff --git a/src/hl.h b/src/hl.h index 6220eb369..d042dbc40 100644 --- a/src/hl.h +++ b/src/hl.h @@ -650,6 +650,7 @@ HL_API vbyte *hl_copy_bytes( const vbyte *byte, int size ); HL_API int hl_utf8_length( const vbyte *s, int pos ); HL_API int hl_from_utf8( uchar *out, int outLen, const char *str ); HL_API char *hl_to_utf8( const uchar *bytes ); +HL_API char *hl_to_utf8_len( const uchar *bytes, int len ); HL_API uchar *hl_to_utf16( const char *str ); HL_API uchar *hl_guid_str( int64 guid, uchar buf[14] ); HL_API vdynamic *hl_virtual_make_value( vvirtual *v ); diff --git a/src/std/date.c b/src/std/date.c index ff7af3898..0f0f9ce10 100644 --- a/src/std/date.c +++ b/src/std/date.c @@ -76,7 +76,7 @@ HL_PRIM int hl_date_from_time( double time ) { HL_PRIM int hl_date_from_string( vbyte *b, int len ) { struct tm t; int o = 0; - const char *str = hl_to_utf8((uchar*)b); + const char *str = hl_to_utf8_len((uchar*)b, len); bool recal = true; memset(&t,0,sizeof(struct tm)); switch( strlen(str) ) { diff --git a/src/std/string.c b/src/std/string.c index 73deacd20..bcf2bfa02 100644 --- a/src/std/string.c +++ b/src/std/string.c @@ -200,7 +200,7 @@ HL_PRIM vbyte *hl_utf16_to_utf8( vbyte *str, int len, int *size ) { uchar *end = len == 0 ? NULL : c + len; int utf8bytes = 0; int p = 0; - while( c != end ) { + while( c < end ) { unsigned int v = (unsigned int)*c; if( v == 0 && end == NULL ) break; if( v < 0x80 ) @@ -216,7 +216,7 @@ HL_PRIM vbyte *hl_utf16_to_utf8( vbyte *str, int len, int *size ) { } out = hl_gc_alloc_noptr(utf8bytes + 1); c = (uchar*)str; - while( c != end ) { + while( c < end ) { unsigned int v = (unsigned int)*c; if( v < 0x80 ) { out[p++] = (vbyte)v; @@ -246,6 +246,11 @@ HL_PRIM char *hl_to_utf8( const uchar *bytes ) { return (char*)hl_utf16_to_utf8((vbyte*)bytes, 0, &size); } +HL_PRIM char *hl_to_utf8_len( const uchar *bytes, int len ) { + int size; + return (char*)hl_utf16_to_utf8((vbyte*)bytes, len, &size); +} + static void hl_buffer_hex( hl_buffer *b, int c ) { static const uchar *hex = USTR("0123456789ABCDEF"); hl_buffer_char(b,'%');