Wirepas SDK
utf8_p.h File Reference

Go to the source code of this file.

Functions

static uint32_t get_utf8 (const uint8_t **buffer, const uint8_t *end)
 

Function Documentation

◆ get_utf8()

static uint32_t get_utf8 ( const uint8_t **  buffer,
const uint8_t *  end 
)
static

Definition at line 32 of file utf8_p.h.

33 {
34  int charsNeeded;
35  uint32_t uc, min_uc;
36  uint8_t b;
37  ptrdiff_t n = end - *buffer;
38  if (n == 0)
39  return ~0U;
40 
41  uc = *(*buffer)++;
42  if (uc < 0x80) {
43  /* single-byte UTF-8 */
44  return uc;
45  }
46 
47  /* multi-byte UTF-8, decode it */
48  if (unlikely(uc <= 0xC1))
49  return ~0U;
50  if (uc < 0xE0) {
51  /* two-byte UTF-8 */
52  charsNeeded = 2;
53  min_uc = 0x80;
54  uc &= 0x1f;
55  } else if (uc < 0xF0) {
56  /* three-byte UTF-8 */
57  charsNeeded = 3;
58  min_uc = 0x800;
59  uc &= 0x0f;
60  } else if (uc < 0xF5) {
61  /* four-byte UTF-8 */
62  charsNeeded = 4;
63  min_uc = 0x10000;
64  uc &= 0x07;
65  } else {
66  return ~0U;
67  }
68 
69  if (n < charsNeeded - 1)
70  return ~0U;
71 
72  /* first continuation character */
73  b = *(*buffer)++;
74  if ((b & 0xc0) != 0x80)
75  return ~0U;
76  uc <<= 6;
77  uc |= b & 0x3f;
78 
79  if (charsNeeded > 2) {
80  /* second continuation character */
81  b = *(*buffer)++;
82  if ((b & 0xc0) != 0x80)
83  return ~0U;
84  uc <<= 6;
85  uc |= b & 0x3f;
86 
87  if (charsNeeded > 3) {
88  /* third continuation character */
89  b = *(*buffer)++;
90  if ((b & 0xc0) != 0x80)
91  return ~0U;
92  uc <<= 6;
93  uc |= b & 0x3f;
94  }
95  }
96 
97  /* overlong sequence? surrogate pair? out or range? */
98  if (uc < min_uc || uc - 0xd800U < 2048U || uc > 0x10ffff)
99  return ~0U;
100 
101  return uc;
102 }
unlikely
#define unlikely(x)
Definition: compilersupport_p.h:189