Rizin
unix-like reverse engineering framework and cli tools
utf16.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017 kazarmy <kazarmy@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 
7 /* Convert an UTF-16 buf into a unicode RzRune */
8 RZ_API int rz_utf16_decode(const ut8 *ptr, int ptrlen, RzRune *ch, bool bigendian) {
9  if (ptrlen < 1) {
10  return 0;
11  }
12  int high = bigendian ? 0 : 1;
13  int low = bigendian ? 1 : 0;
14  if (ptrlen > 3 && (ptr[high] & 0xdc) == 0xd8 && (ptr[high + 2] & 0xdc) == 0xdc) {
15  if (ch) {
16  *ch = ((ptr[high] & 3) << 24 | ptr[low] << 16 | (ptr[high + 2] & 3) << 8 | ptr[low + 2]) + 0x10000;
17  }
18  return 4;
19  }
20  if (ptrlen > 1 && ptr[high]) {
21  if (ch) {
22  *ch = ptr[high] << 8 | ptr[low];
23  }
24  return 2;
25  }
26  if (ptrlen > 1) {
27  if (ch) {
28  *ch = (ut32)ptr[low];
29  }
30  return 1;
31  }
32  return 0;
33 }
34 
35 /* Convert an UTF-16LE buf into a unicode RzRune */
36 RZ_API int rz_utf16le_decode(const ut8 *ptr, int ptrlen, RzRune *ch) {
37  return rz_utf16_decode(ptr, ptrlen, ch, false);
38 }
39 
40 /* Convert an UTF-16BE buf into a unicode RzRune */
41 RZ_API int rz_utf16be_decode(const ut8 *ptr, int ptrlen, RzRune *ch) {
42  return rz_utf16_decode(ptr, ptrlen, ch, true);
43 }
44 
45 /* Convert a unicode RzRune into a UTF-16LE buf */
47  if (ch < 0x10000) {
48  ptr[0] = ch & 0xff;
49  ptr[1] = ch >> 8 & 0xff;
50  return 2;
51  }
52  if (ch < 0x110000) {
53  ch -= 0x10000;
54  RzRune high = 0xd800 + (ch >> 10 & 0x3ff);
55  RzRune low = 0xdc00 + (ch & 0x3ff);
56  ptr[0] = high & 0xff;
57  ptr[1] = high >> 8 & 0xff;
58  ptr[2] = low & 0xff;
59  ptr[3] = low >> 8 & 0xff;
60  return 4;
61  }
62  return 0;
63 }
#define RZ_API
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
ut32 RzRune
Definition: rz_utf8.h:13
RZ_API int rz_utf16_decode(const ut8 *ptr, int ptrlen, RzRune *ch, bool bigendian)
Definition: utf16.c:8
RZ_API int rz_utf16le_encode(ut8 *ptr, RzRune ch)
Definition: utf16.c:46
RZ_API int rz_utf16le_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf16.c:36
RZ_API int rz_utf16be_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf16.c:41