radare2/libr/util/utf16.c
Khairul Azhar Kasmiran 0ff6f4313d asm.strenc (utf16le) (#7926)
* Fix API of r_utf16le_decode to be consistent with r_utf8_decode

* Fix braces and else-after-return

* Don't use r_mem_mem

* Fix bogus last char at max string length
2017-07-16 08:12:11 +02:00

31 lines
610 B
C

/* radare2 - LGPL - Copyright 2017 - kazarmy */
#include <r_types.h>
#include <r_util.h>
/* Convert an UTF-16LE buf into a unicode RRune */
R_API int r_utf16le_decode(const ut8 *ptr, int ptrlen, RRune *ch) {
if (ptrlen < 1) {
return 0;
}
if (ptrlen > 3 && (ptr[1] & 0xdc) == 0xd8 && (ptr[3] & 0xdc) == 0xdc) {
if (ch) {
*ch = ((ptr[1] & 3) << 24 | ptr[0] << 16 | (ptr[3] & 3) << 8 | ptr[2]) + 0x10000;
}
return 4;
}
if (ptrlen > 1 && ptr[1]) {
if (ch) {
*ch = ptr[1] << 8 | ptr[0];
}
return 2;
}
if (ptrlen > 1) {
if (ch) {
*ch = (ut32)ptr[0];
}
return 1;
}
return 0;
}