Rizin
unix-like reverse engineering framework and cli tools
utf32.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-32 buf into a unicode RzRune */
8 RZ_API int rz_utf32_decode(const ut8 *ptr, int ptrlen, RzRune *ch, bool bigendian) {
9  if (ptrlen < 1) {
10  return 0;
11  }
12  int low = 0;
13  int high = 3;
14  if (bigendian) {
15  low = 3;
16  high = 0;
17  }
18  if (ptrlen > 3) {
19  int sign = bigendian ? -1 : 1;
20  if (ch) {
21  int i;
22  *ch = (ut32)ptr[low];
23  for (i = 1; i < 4; i++) {
24  *ch |= (ut32)ptr[3 - high + i * sign] << 8 * i;
25  }
26  }
27  if (ptr[high] || ptr[high - 1 * sign]) {
28  return 4;
29  }
30  if (ptr[low + 1 * sign]) {
31  return 2;
32  }
33  return 1;
34  }
35  return 0;
36 }
37 
38 /* Convert an UTF-32LE buf into a unicode RzRune */
39 RZ_API int rz_utf32le_decode(const ut8 *ptr, int ptrlen, RzRune *ch) {
40  return rz_utf32_decode(ptr, ptrlen, ch, false);
41 }
42 
43 /* Convert an UTF-32BE buf into a unicode RzRune */
44 RZ_API int rz_utf32be_decode(const ut8 *ptr, int ptrlen, RzRune *ch) {
45  return rz_utf32_decode(ptr, ptrlen, ch, true);
46 }
lzma_index ** i
Definition: index.h:629
#define RZ_API
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
ut32 RzRune
Definition: rz_utf8.h:13
RZ_API int rz_utf32le_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf32.c:39
RZ_API int rz_utf32_decode(const ut8 *ptr, int ptrlen, RzRune *ch, bool bigendian)
Definition: utf32.c:8
RZ_API int rz_utf32be_decode(const ut8 *ptr, int ptrlen, RzRune *ch)
Definition: utf32.c:44