2017-07-17 17:07:38 +00:00
|
|
|
/* radare2 - LGPL - Copyright 2017 - kazarmy */
|
|
|
|
|
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_util.h>
|
|
|
|
|
2019-11-16 10:27:07 +00:00
|
|
|
/* Convert an UTF-32 buf into a unicode RRune */
|
|
|
|
R_API int r_utf32_decode(const ut8 *ptr, int ptrlen, RRune *ch, bool bigendian) {
|
2017-07-17 17:07:38 +00:00
|
|
|
if (ptrlen < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-16 10:27:07 +00:00
|
|
|
int low = 0;
|
|
|
|
int high = 3;
|
|
|
|
if (bigendian) {
|
|
|
|
low = 3;
|
|
|
|
high = 0;
|
|
|
|
}
|
2017-07-17 17:07:38 +00:00
|
|
|
if (ptrlen > 3) {
|
2019-11-16 10:27:07 +00:00
|
|
|
int sign = bigendian ? -1 : 1;
|
|
|
|
if (ch) {
|
|
|
|
int i;
|
|
|
|
*ch = (ut32)ptr[low];
|
|
|
|
for (i = 1; i < 4; i++) {
|
|
|
|
*ch |= (ut32)ptr[3 - high + i * sign] << 8 * i;
|
2017-07-17 17:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-16 10:27:07 +00:00
|
|
|
if (ptr[high] || ptr[high - 1 * sign]) {
|
2017-07-17 17:07:38 +00:00
|
|
|
return 4;
|
|
|
|
}
|
2019-11-16 10:27:07 +00:00
|
|
|
if (ptr[low + 1 * sign]) {
|
2017-07-17 17:07:38 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-16 10:27:07 +00:00
|
|
|
|
|
|
|
/* Convert an UTF-32LE buf into a unicode RRune */
|
|
|
|
R_API int r_utf32le_decode(const ut8 *ptr, int ptrlen, RRune *ch) {
|
|
|
|
return r_utf32_decode (ptr, ptrlen, ch, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert an UTF-32BE buf into a unicode RRune */
|
|
|
|
R_API int r_utf32be_decode(const ut8 *ptr, int ptrlen, RRune *ch) {
|
|
|
|
return r_utf32_decode (ptr, ptrlen, ch, true);
|
|
|
|
}
|