bare-buffer 1.2.2 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CMakeLists.txt CHANGED
@@ -4,6 +4,10 @@ project(bare_buffer C)
4
4
 
5
5
  include(bare)
6
6
 
7
+ if(NOT TARGET utf)
8
+ add_subdirectory(vendor/libutf EXCLUDE_FROM_ALL)
9
+ endif()
10
+
7
11
  if(NOT TARGET base64)
8
12
  add_subdirectory(vendor/libbase64 EXCLUDE_FROM_ALL)
9
13
  endif()
@@ -23,6 +27,7 @@ target_sources(
23
27
  target_include_directories(
24
28
  bare_buffer
25
29
  PRIVATE
30
+ $<TARGET_PROPERTY:utf,INTERFACE_INCLUDE_DIRECTORIES>
26
31
  $<TARGET_PROPERTY:base64,INTERFACE_INCLUDE_DIRECTORIES>
27
32
  $<TARGET_PROPERTY:hex,INTERFACE_INCLUDE_DIRECTORIES>
28
33
  )
package/binding.c CHANGED
@@ -6,6 +6,7 @@
6
6
  #include <js/ffi.h>
7
7
  #include <stdlib.h>
8
8
  #include <string.h>
9
+ #include <utf.h>
9
10
 
10
11
  static void
11
12
  bare_buffer_set_zero_fill_enabled_fast (js_ffi_receiver_t *receiver, uint32_t enabled) {
@@ -80,7 +81,7 @@ bare_buffer_to_string_utf8 (js_env_t *env, js_callback_info_t *info) {
80
81
  assert(argc == 1);
81
82
 
82
83
  size_t str_len;
83
- char *str;
84
+ utf8_t *str;
84
85
  err = js_get_typedarray_info(env, argv[0], NULL, (void **) &str, &str_len, NULL, NULL);
85
86
  assert(err == 0);
86
87
 
@@ -119,6 +120,64 @@ bare_buffer_write_utf8 (js_env_t *env, js_callback_info_t *info) {
119
120
  return result;
120
121
  }
121
122
 
123
+ static js_value_t *
124
+ bare_buffer_to_string_utf16le (js_env_t *env, js_callback_info_t *info) {
125
+ int err;
126
+
127
+ size_t argc = 1;
128
+ js_value_t *argv[1];
129
+
130
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
131
+ assert(err == 0);
132
+
133
+ assert(argc == 1);
134
+
135
+ size_t str_len;
136
+ utf16_t *str;
137
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &str, &str_len, NULL, NULL);
138
+ assert(err == 0);
139
+
140
+ str_len /= sizeof(utf16_t);
141
+
142
+ js_value_t *result;
143
+ err = js_create_string_utf16le(env, str, str_len, &result);
144
+ assert(err == 0);
145
+
146
+ return result;
147
+ }
148
+
149
+ static js_value_t *
150
+ bare_buffer_write_utf16le (js_env_t *env, js_callback_info_t *info) {
151
+ int err;
152
+
153
+ size_t argc = 2;
154
+ js_value_t *argv[2];
155
+
156
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
157
+ assert(err == 0);
158
+
159
+ assert(argc == 2);
160
+
161
+ size_t buf_len;
162
+ void *buf;
163
+ err = js_get_typedarray_info(env, argv[0], NULL, &buf, &buf_len, NULL, NULL);
164
+ assert(err == 0);
165
+
166
+ buf_len /= sizeof(utf16_t);
167
+
168
+ size_t str_len;
169
+ err = js_get_value_string_utf16le(env, argv[1], buf, buf_len, &str_len);
170
+ assert(err == 0);
171
+
172
+ str_len *= sizeof(utf16_t);
173
+
174
+ js_value_t *result;
175
+ err = js_create_uint32(env, (uint32_t) str_len, &result);
176
+ assert(err == 0);
177
+
178
+ return result;
179
+ }
180
+
122
181
  static js_value_t *
123
182
  bare_buffer_to_string_base64 (js_env_t *env, js_callback_info_t *info) {
124
183
  int err;
@@ -140,7 +199,7 @@ bare_buffer_to_string_base64 (js_env_t *env, js_callback_info_t *info) {
140
199
  err = base64_encode(buf, buf_len, NULL, &str_len);
141
200
  assert(err == 0);
142
201
 
143
- char *str = malloc(str_len);
202
+ utf8_t *str = malloc(str_len);
144
203
  err = base64_encode(buf, buf_len, str, &str_len);
145
204
  assert(err == 0);
146
205
 
@@ -174,7 +233,7 @@ bare_buffer_write_base64 (js_env_t *env, js_callback_info_t *info) {
174
233
  err = js_get_value_string_utf8(env, argv[1], NULL, 0, &str_len);
175
234
  assert(err == 0);
176
235
 
177
- char *str = malloc(str_len);
236
+ utf8_t *str = malloc(str_len);
178
237
  err = js_get_value_string_utf8(env, argv[1], str, str_len, NULL);
179
238
  assert(err == 0);
180
239
 
@@ -211,7 +270,7 @@ bare_buffer_to_string_hex (js_env_t *env, js_callback_info_t *info) {
211
270
  err = hex_encode(buf, buf_len, NULL, &str_len);
212
271
  assert(err == 0);
213
272
 
214
- char *str = malloc(str_len);
273
+ utf8_t *str = malloc(str_len);
215
274
  err = hex_encode(buf, buf_len, str, &str_len);
216
275
  assert(err == 0);
217
276
 
@@ -245,7 +304,7 @@ bare_buffer_write_hex (js_env_t *env, js_callback_info_t *info) {
245
304
  err = js_get_value_string_utf8(env, argv[1], NULL, 0, &str_len);
246
305
  assert(err == 0);
247
306
 
248
- char *str = malloc(str_len);
307
+ utf8_t *str = malloc(str_len);
249
308
  err = js_get_value_string_utf8(env, argv[1], str, str_len, NULL);
250
309
  assert(err == 0);
251
310
 
@@ -356,6 +415,16 @@ init (js_env_t *env, js_value_t *exports) {
356
415
  js_create_function(env, "writeUTF8", -1, bare_buffer_write_utf8, NULL, &val);
357
416
  js_set_named_property(env, exports, "writeUTF8", val);
358
417
  }
418
+ {
419
+ js_value_t *val;
420
+ js_create_function(env, "toStringUTF16LE", -1, bare_buffer_to_string_utf16le, NULL, &val);
421
+ js_set_named_property(env, exports, "toStringUTF16LE", val);
422
+ }
423
+ {
424
+ js_value_t *val;
425
+ js_create_function(env, "writeUTF16LE", -1, bare_buffer_write_utf16le, NULL, &val);
426
+ js_set_named_property(env, exports, "writeUTF16LE", val);
427
+ }
359
428
  {
360
429
  js_value_t *val;
361
430
  js_create_function(env, "toStringBase64", -1, bare_buffer_to_string_base64, NULL, &val);
package/lib/utf16le.js CHANGED
@@ -1,34 +1,13 @@
1
+ const binding = require('../binding')
2
+
1
3
  exports.byteLength = function byteLength (string) {
2
4
  return string.length * 2
3
5
  }
4
6
 
5
7
  exports.toString = function toString (buffer) {
6
- const len = buffer.byteLength
7
-
8
- let result = ''
9
-
10
- for (let i = 0; i < len - 1; i += 2) {
11
- result += String.fromCharCode(buffer[i] + (buffer[i + 1] * 256))
12
- }
13
-
14
- return result
8
+ return binding.toStringUTF16LE(buffer)
15
9
  }
16
10
 
17
11
  exports.write = function write (buffer, string) {
18
- const len = buffer.byteLength
19
-
20
- let units = len
21
-
22
- for (let i = 0; i < string.length; ++i) {
23
- if ((units -= 2) < 0) break
24
-
25
- const c = string.charCodeAt(i)
26
- const hi = c >> 8
27
- const lo = c % 256
28
-
29
- buffer[i * 2] = lo
30
- buffer[i * 2 + 1] = hi
31
- }
32
-
33
- return len
12
+ return binding.writeUTF16LE(buffer, string)
34
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-buffer",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "Native buffers for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -13,7 +13,9 @@
13
13
  "vendor/libbase64/include",
14
14
  "vendor/libbase64/src",
15
15
  "vendor/libhex/include",
16
- "vendor/libhex/src"
16
+ "vendor/libhex/src",
17
+ "vendor/libutf/include",
18
+ "vendor/libutf/src"
17
19
  ],
18
20
  "scripts": {
19
21
  "test": "standard && bare test/all.js",
@@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.22)
2
2
 
3
3
  project(base64 C)
4
4
 
5
+ if(NOT TARGET utf)
6
+ add_subdirectory(vendor/libutf EXCLUDE_FROM_ALL)
7
+ endif()
8
+
5
9
  add_library(base64 OBJECT)
6
10
 
7
11
  set_target_properties(
@@ -23,6 +27,7 @@ target_include_directories(
23
27
  base64
24
28
  PUBLIC
25
29
  include
30
+ $<TARGET_PROPERTY:utf,INTERFACE_INCLUDE_DIRECTORIES>
26
31
  )
27
32
 
28
33
  add_library(base64_shared SHARED $<TARGET_OBJECTS:base64>)
@@ -40,6 +45,7 @@ set_target_properties(
40
45
  base64_static
41
46
  PROPERTIES
42
47
  OUTPUT_NAME base64
48
+ PREFIX lib
43
49
  )
44
50
 
45
51
  install(TARGETS base64_shared base64_static)
@@ -8,4 +8,4 @@ See [`include/base64.h`](include/base64.h) for the public API.
8
8
 
9
9
  ## License
10
10
 
11
- ISC
11
+ Apache-2.0
@@ -3,16 +3,17 @@
3
3
 
4
4
  #include <stddef.h>
5
5
  #include <stdint.h>
6
+ #include <utf.h>
6
7
 
7
8
  #ifdef __cplusplus
8
9
  extern "C" {
9
10
  #endif
10
11
 
11
12
  int
12
- base64_encode (const uint8_t *buffer, size_t buffer_len, char *string, size_t *string_len);
13
+ base64_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len);
13
14
 
14
15
  int
15
- base64_decode (const char *string, size_t string_len, uint8_t *buffer, size_t *buffer_len);
16
+ base64_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len);
16
17
 
17
18
  #ifdef __cplusplus
18
19
  }
@@ -1,6 +1,7 @@
1
1
  #include <stdbool.h>
2
2
  #include <stddef.h>
3
3
  #include <stdint.h>
4
+ #include <utf.h>
4
5
 
5
6
  #include "../include/base64.h"
6
7
 
@@ -9,7 +10,7 @@ static const char base64_alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm
9
10
  static const char base64_inverse_alphabet[256] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
10
11
 
11
12
  int
12
- base64_encode (const uint8_t *buffer, size_t buffer_len, char *string, size_t *string_len) {
13
+ base64_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len) {
13
14
  size_t len = 4 * ((buffer_len + 2) / 3);
14
15
 
15
16
  if (string == NULL) {
@@ -52,7 +53,7 @@ base64_encode (const uint8_t *buffer, size_t buffer_len, char *string, size_t *s
52
53
  }
53
54
 
54
55
  int
55
- base64_decode (const char *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
56
+ base64_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
56
57
  if (string_len % 4 != 0) return -1;
57
58
 
58
59
  if (string_len == 0) {
@@ -82,7 +83,7 @@ base64_decode (const char *string, size_t string_len, uint8_t *buffer, size_t *b
82
83
  for (size_t j = 0; j < 4; j++) {
83
84
  chunk[j] = string[i + j] == '=' ? 0 : base64_inverse_alphabet[string[i + j]];
84
85
 
85
- if (chunk[j] == -1) return -1;
86
+ if (chunk[j] == (char) -1) return -1;
86
87
  }
87
88
 
88
89
  uint32_t triple = (chunk[0] << 3 * 6) + (chunk[1] << 2 * 6) + (chunk[2] << 1 * 6) + (chunk[3] << 0 * 6);
@@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.22)
2
2
 
3
3
  project(hex C)
4
4
 
5
+ if(NOT TARGET utf)
6
+ add_subdirectory(vendor/libutf EXCLUDE_FROM_ALL)
7
+ endif()
8
+
5
9
  add_library(hex OBJECT)
6
10
 
7
11
  set_target_properties(
@@ -23,6 +27,7 @@ target_include_directories(
23
27
  hex
24
28
  PUBLIC
25
29
  include
30
+ $<TARGET_PROPERTY:utf,INTERFACE_INCLUDE_DIRECTORIES>
26
31
  )
27
32
 
28
33
  add_library(hex_shared SHARED $<TARGET_OBJECTS:hex>)
@@ -40,6 +45,7 @@ set_target_properties(
40
45
  hex_static
41
46
  PROPERTIES
42
47
  OUTPUT_NAME hex
48
+ PREFIX lib
43
49
  )
44
50
 
45
51
  install(TARGETS hex_shared hex_static)
@@ -8,4 +8,4 @@ See [`include/hex.h`](include/hex.h) for the public API.
8
8
 
9
9
  ## License
10
10
 
11
- ISC
11
+ Apache-2.0
@@ -3,16 +3,17 @@
3
3
 
4
4
  #include <stddef.h>
5
5
  #include <stdint.h>
6
+ #include <utf.h>
6
7
 
7
8
  #ifdef __cplusplus
8
9
  extern "C" {
9
10
  #endif
10
11
 
11
12
  int
12
- hex_encode (const uint8_t *buffer, size_t buffer_len, char *string, size_t *string_len);
13
+ hex_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len);
13
14
 
14
15
  int
15
- hex_decode (const char *string, size_t string_len, uint8_t *buffer, size_t *buffer_len);
16
+ hex_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len);
16
17
 
17
18
  #ifdef __cplusplus
18
19
  }
@@ -1,6 +1,7 @@
1
1
  #include <stdbool.h>
2
2
  #include <stddef.h>
3
3
  #include <stdint.h>
4
+ #include <utf.h>
4
5
 
5
6
  #include "../include/hex.h"
6
7
 
@@ -9,7 +10,7 @@ static const char hex_alphabet[16] = "0123456789abcdef";
9
10
  static const char hex_inverse_alphabet[256] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
10
11
 
11
12
  int
12
- hex_encode (const uint8_t *buffer, size_t buffer_len, char *string, size_t *string_len) {
13
+ hex_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len) {
13
14
  size_t len = buffer_len * 2;
14
15
 
15
16
  if (string == NULL) {
@@ -36,7 +37,7 @@ hex_encode (const uint8_t *buffer, size_t buffer_len, char *string, size_t *stri
36
37
  }
37
38
 
38
39
  int
39
- hex_decode (const char *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
40
+ hex_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
40
41
  size_t len = string_len >> 1;
41
42
 
42
43
  if (buffer == NULL) {
@@ -56,7 +57,7 @@ hex_decode (const char *string, size_t string_len, uint8_t *buffer, size_t *buff
56
57
  for (size_t j = 0; j < 2; j++) {
57
58
  chunk[j] = i + j < n ? hex_inverse_alphabet[string[i + j]] : 0;
58
59
 
59
- if (chunk[j] == -1) return -1;
60
+ if (chunk[j] == (char) -1) return -1;
60
61
  }
61
62
 
62
63
  buffer[k++] = (chunk[0] << 4) | chunk[1];
@@ -0,0 +1,59 @@
1
+ cmake_minimum_required(VERSION 3.25)
2
+
3
+ project(utf C)
4
+
5
+ add_library(utf OBJECT)
6
+
7
+ set_target_properties(
8
+ utf
9
+ PROPERTIES
10
+ C_STANDARD 99
11
+ POSITION_INDEPENDENT_CODE ON
12
+ )
13
+
14
+ target_sources(
15
+ utf
16
+ INTERFACE
17
+ include/utf.h
18
+ PRIVATE
19
+ src/endianness.c
20
+ src/utf8/utf16-convert.c
21
+ src/utf8/utf16-length.c
22
+ src/utf8/validate.c
23
+ src/utf16/utf8-convert.c
24
+ src/utf16/utf8-length.c
25
+ src/utf16/validate.c
26
+ )
27
+
28
+ target_include_directories(
29
+ utf
30
+ PUBLIC
31
+ include
32
+ )
33
+
34
+ add_library(utf_shared SHARED $<TARGET_OBJECTS:utf>)
35
+
36
+ set_target_properties(
37
+ utf_shared
38
+ PROPERTIES
39
+ OUTPUT_NAME utf
40
+ WINDOWS_EXPORT_ALL_SYMBOLS ON
41
+ )
42
+
43
+ add_library(utf_static STATIC $<TARGET_OBJECTS:utf>)
44
+
45
+ set_target_properties(
46
+ utf_static
47
+ PROPERTIES
48
+ OUTPUT_NAME utf
49
+ PREFIX lib
50
+ )
51
+
52
+ install(TARGETS utf_shared utf_static)
53
+
54
+ install(FILES include/utf.h DESTINATION include)
55
+
56
+ if(PROJECT_IS_TOP_LEVEL)
57
+ enable_testing()
58
+ add_subdirectory(test)
59
+ endif()
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,11 @@
1
+ # libutf
2
+
3
+ Small library for working with Unicode in C.
4
+
5
+ ## API
6
+
7
+ See [`include/utf.h`](include/utf.h) for the public API.
8
+
9
+ ## License
10
+
11
+ Apache-2.0
@@ -0,0 +1,46 @@
1
+ #ifndef UTF_ENDIANNESS_H
2
+ #define UTF_ENDIANNESS_H
3
+
4
+ #include <stdbool.h>
5
+ #include <stdint.h>
6
+
7
+ typedef enum {
8
+ utf_le,
9
+ utf_be
10
+ } utf_endianness_t;
11
+
12
+ inline utf_endianness_t
13
+ utf_endianness (void) {
14
+ const union {
15
+ uint8_t u8[2];
16
+ uint16_t u16;
17
+ } byte_order = {{1, 0}};
18
+
19
+ return byte_order.u16 == 1 ? utf_le : utf_be;
20
+ }
21
+
22
+ inline bool
23
+ utf_is_le (void) {
24
+ return utf_endianness() == utf_le;
25
+ }
26
+
27
+ inline bool
28
+ utf_is_be (void) {
29
+ return utf_endianness() == utf_be;
30
+ }
31
+
32
+ inline uint16_t
33
+ utf_swap_uint16 (uint16_t n) {
34
+ return ((n & 0x00ff) << 8) |
35
+ ((n & 0xff00) >> 8);
36
+ }
37
+
38
+ inline uint32_t
39
+ utf_swap_uint32 (uint32_t n) {
40
+ return ((n & 0x000000ff) << 24) |
41
+ ((n & 0x0000ff00) << 8) |
42
+ ((n & 0x00ff0000) >> 8) |
43
+ ((n & 0xff000000) >> 24);
44
+ }
45
+
46
+ #endif // UTF_ENDIANNESS_H
@@ -0,0 +1,47 @@
1
+ #ifndef UTF_H
2
+ #define UTF_H
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include <stdbool.h>
9
+ #include <stddef.h>
10
+ #include <stdint.h>
11
+
12
+ #include "utf/endianness.h"
13
+
14
+ typedef uint_least8_t utf8_t;
15
+ typedef uint_least16_t utf16_t;
16
+
17
+ /**
18
+ * UTF-8
19
+ */
20
+
21
+ bool
22
+ utf8_validate (const utf8_t *data, size_t len);
23
+
24
+ size_t
25
+ utf8_length_from_utf16le (const utf16_t *data, size_t len);
26
+
27
+ size_t
28
+ utf8_convert_to_utf16le (const utf8_t *data, size_t len, utf16_t *result);
29
+
30
+ /**
31
+ * UTF-16
32
+ */
33
+
34
+ bool
35
+ utf16le_validate (const utf16_t *data, size_t len);
36
+
37
+ size_t
38
+ utf16_length_from_utf8 (const utf8_t *data, size_t len);
39
+
40
+ size_t
41
+ utf16le_convert_to_utf8 (const utf16_t *data, size_t len, utf8_t *result);
42
+
43
+ #ifdef __cplusplus
44
+ }
45
+ #endif
46
+
47
+ #endif // UTF_H
@@ -0,0 +1,19 @@
1
+ #include <stdbool.h>
2
+ #include <stdint.h>
3
+
4
+ #include "../include/utf/endianness.h"
5
+
6
+ extern utf_endianness_t
7
+ utf_endianness (void);
8
+
9
+ extern bool
10
+ utf_is_le (void);
11
+
12
+ extern bool
13
+ utf_is_be (void);
14
+
15
+ extern uint16_t
16
+ utf_swap_uint16 (uint16_t n);
17
+
18
+ extern uint32_t
19
+ utf_swap_uint32 (uint32_t n);
@@ -0,0 +1,76 @@
1
+ #include <stdbool.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <string.h>
5
+
6
+ #include "../../include/utf.h"
7
+
8
+ /**
9
+ * Modified from https://github.com/simdutf/simdutf
10
+ *
11
+ * Copyright 2020 The simdutf authors
12
+ *
13
+ * Licensed under the Apache License, Version 2.0 (the "License");
14
+ * you may not use this file except in compliance with the License.
15
+ * You may obtain a copy of the License at
16
+ *
17
+ * http://www.apache.org/licenses/LICENSE-2.0
18
+ *
19
+ * Unless required by applicable law or agreed to in writing, software
20
+ * distributed under the License is distributed on an "AS IS" BASIS,
21
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ * See the License for the specific language governing permissions and
23
+ * limitations under the License.
24
+ */
25
+
26
+ size_t
27
+ utf16le_convert_to_utf8 (const utf16_t *data, size_t len, utf8_t *result) {
28
+ size_t pos = 0;
29
+ uint16_t word, diff;
30
+ utf8_t *start = result;
31
+
32
+ while (pos < len) {
33
+ if (pos + 4 <= len) {
34
+ uint64_t v;
35
+ memcpy(&v, data + pos, sizeof(uint64_t));
36
+ if (utf_is_be()) v = (v >> 8) | (v << (64 - 8));
37
+ if ((v & 0xff80ff80ff80ff80) == 0) {
38
+ size_t final_pos = pos + 4;
39
+ while (pos < final_pos) {
40
+ *result++ = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
41
+ pos++;
42
+ }
43
+ continue;
44
+ }
45
+ }
46
+
47
+ word = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
48
+ if ((word & 0xff80) == 0) {
49
+ *result++ = word;
50
+ pos++;
51
+ } else if ((word & 0xf800) == 0) {
52
+ *result++ = (word >> 6) | 0b11000000;
53
+ *result++ = (word & 0b111111) | 0b10000000;
54
+ pos++;
55
+ } else if ((word & 0xf800) != 0xd800) {
56
+ *result++ = (word >> 12) | 0b11100000;
57
+ *result++ = ((word >> 6) & 0b111111) | 0b10000000;
58
+ *result++ = (word & 0b111111) | 0b10000000;
59
+ pos++;
60
+ } else {
61
+ diff = word - 0xd800;
62
+ if (pos + 1 >= len) {
63
+ return 0;
64
+ }
65
+ word = utf_is_be() ? utf_swap_uint16(data[pos + 1]) : data[pos + 1];
66
+ uint32_t value = (diff << 10) + (word - 0xdc00) + 0x10000;
67
+ *result++ = (value >> 18) | 0b11110000;
68
+ *result++ = ((value >> 12) & 0b111111) | 0b10000000;
69
+ *result++ = ((value >> 6) & 0b111111) | 0b10000000;
70
+ *result++ = (value & 0b111111) | 0b10000000;
71
+ pos += 2;
72
+ }
73
+ }
74
+
75
+ return result - start;
76
+ }
@@ -0,0 +1,44 @@
1
+ #include <stdbool.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+
5
+ #include "../../include/utf.h"
6
+
7
+ /**
8
+ * Modified from https://github.com/simdutf/simdutf
9
+ *
10
+ * Copyright 2020 The simdutf authors
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+
25
+ size_t
26
+ utf8_length_from_utf16le (const utf16_t *data, size_t len) {
27
+ size_t counter = 0;
28
+ uint16_t word;
29
+
30
+ for (size_t i = 0; i < len; i++) {
31
+ word = utf_is_be() ? utf_swap_uint16(data[i]) : data[i];
32
+ if (word <= 0x7f) {
33
+ counter++;
34
+ } else if (word <= 0x7ff) {
35
+ counter += 2;
36
+ } else if ((word <= 0xd7ff) || (word >= 0xe000)) {
37
+ counter += 3;
38
+ } else {
39
+ counter += 2;
40
+ }
41
+ }
42
+
43
+ return counter;
44
+ }
@@ -0,0 +1,52 @@
1
+ #include <stdbool.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+
5
+ #include "../../include/utf.h"
6
+
7
+ /**
8
+ * Modified from https://github.com/simdutf/simdutf
9
+ *
10
+ * Copyright 2020 The simdutf authors
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+
25
+ bool
26
+ utf16le_validate (const utf16_t *data, size_t len) {
27
+ uint64_t pos = 0;
28
+ uint16_t word, diff;
29
+
30
+ while (pos < len) {
31
+ word = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
32
+ if ((word & 0xf800) == 0xd800) {
33
+ if (pos + 1 >= len) {
34
+ return false;
35
+ }
36
+ diff = word - 0xd800;
37
+ if (diff > 0x3ff) {
38
+ return false;
39
+ }
40
+ word = utf_is_be() ? utf_swap_uint16(data[pos + 1]) : data[pos + 1];
41
+ diff = word - 0xdc00;
42
+ if (diff > 0x3ff) {
43
+ return false;
44
+ }
45
+ pos += 2;
46
+ } else {
47
+ pos++;
48
+ }
49
+ }
50
+
51
+ return true;
52
+ }
@@ -0,0 +1,89 @@
1
+ #include <stdbool.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <string.h>
5
+
6
+ #include "../../include/utf.h"
7
+
8
+ /**
9
+ * Modified from https://github.com/simdutf/simdutf
10
+ *
11
+ * Copyright 2020 The simdutf authors
12
+ *
13
+ * Licensed under the Apache License, Version 2.0 (the "License");
14
+ * you may not use this file except in compliance with the License.
15
+ * You may obtain a copy of the License at
16
+ *
17
+ * http://www.apache.org/licenses/LICENSE-2.0
18
+ *
19
+ * Unless required by applicable law or agreed to in writing, software
20
+ * distributed under the License is distributed on an "AS IS" BASIS,
21
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ * See the License for the specific language governing permissions and
23
+ * limitations under the License.
24
+ */
25
+
26
+ size_t
27
+ utf8_convert_to_utf16le (const utf8_t *data, size_t len, utf16_t *result) {
28
+ size_t pos = 0;
29
+ utf16_t *start = result;
30
+
31
+ while (pos < len) {
32
+ if (pos + 8 <= len) {
33
+ uint64_t v;
34
+ memcpy(&v, data + pos, sizeof(uint64_t));
35
+ if ((v & 0x8080808080808080) == 0) {
36
+ size_t final_pos = pos + 8;
37
+ while (pos < final_pos) {
38
+ *result++ = utf_is_be() ? utf_swap_uint16(data[pos]) : data[pos];
39
+ pos++;
40
+ }
41
+ continue;
42
+ }
43
+ }
44
+ uint8_t leading_byte = data[pos];
45
+ if (leading_byte < 0b10000000) {
46
+ *result++ = utf_is_be() ? utf_swap_uint16(leading_byte) : leading_byte;
47
+ pos++;
48
+ } else if ((leading_byte & 0b11100000) == 0b11000000) {
49
+ if (pos + 1 >= len) {
50
+ break;
51
+ }
52
+ uint16_t code_point = ((leading_byte & 0b00011111) << 6) | (data[pos + 1] & 0b00111111);
53
+ if (utf_is_be()) {
54
+ code_point = utf_swap_uint16(code_point);
55
+ }
56
+ *result++ = code_point;
57
+ pos += 2;
58
+ } else if ((leading_byte & 0b11110000) == 0b11100000) {
59
+ if (pos + 2 >= len) {
60
+ break;
61
+ }
62
+ uint16_t code_point = ((leading_byte & 0b00001111) << 12) | ((data[pos + 1] & 0b00111111) << 6) | (data[pos + 2] & 0b00111111);
63
+ if (utf_is_be()) {
64
+ code_point = utf_swap_uint16(code_point);
65
+ }
66
+ *result++ = code_point;
67
+ pos += 3;
68
+ } else if ((leading_byte & 0b11111000) == 0b11110000) {
69
+ if (pos + 3 >= len) {
70
+ break;
71
+ }
72
+ uint32_t code_point = ((leading_byte & 0b00000111) << 18) | ((data[pos + 1] & 0b00111111) << 12) | ((data[pos + 2] & 0b00111111) << 6) | (data[pos + 3] & 0b00111111);
73
+ code_point -= 0x10000;
74
+ uint16_t high_surrogate = 0xd800 + (code_point >> 10);
75
+ uint16_t low_surrogate = 0xdc00 + (code_point & 0x3ff);
76
+ if (utf_is_be()) {
77
+ high_surrogate = utf_swap_uint16(high_surrogate);
78
+ low_surrogate = utf_swap_uint16(low_surrogate);
79
+ }
80
+ *result++ = high_surrogate;
81
+ *result++ = low_surrogate;
82
+ pos += 4;
83
+ } else {
84
+ return 0;
85
+ }
86
+ }
87
+
88
+ return result - start;
89
+ }
@@ -0,0 +1,39 @@
1
+ #include <stdbool.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+
5
+ #include "../../include/utf.h"
6
+
7
+ /**
8
+ * Modified from https://github.com/simdutf/simdutf
9
+ *
10
+ * Copyright 2020 The simdutf authors
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+
25
+ size_t
26
+ utf16_length_from_utf8 (const utf8_t *data, size_t len) {
27
+ size_t counter = 0;
28
+
29
+ for (size_t i = 0; i < len; i++) {
30
+ if ((int8_t) data[i] > -65) {
31
+ counter++;
32
+ }
33
+ if (data[i] >= 240) {
34
+ counter++;
35
+ }
36
+ }
37
+
38
+ return counter;
39
+ }
@@ -0,0 +1,107 @@
1
+ #include <stdbool.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <string.h>
5
+
6
+ #include "../../include/utf.h"
7
+
8
+ /**
9
+ * Modified from https://github.com/simdutf/simdutf
10
+ *
11
+ * Copyright 2020 The simdutf authors
12
+ *
13
+ * Licensed under the Apache License, Version 2.0 (the "License");
14
+ * you may not use this file except in compliance with the License.
15
+ * You may obtain a copy of the License at
16
+ *
17
+ * http://www.apache.org/licenses/LICENSE-2.0
18
+ *
19
+ * Unless required by applicable law or agreed to in writing, software
20
+ * distributed under the License is distributed on an "AS IS" BASIS,
21
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ * See the License for the specific language governing permissions and
23
+ * limitations under the License.
24
+ */
25
+
26
+ bool
27
+ utf8_validate (const utf8_t *data, size_t len) {
28
+ uint64_t pos = 0;
29
+ uint32_t code_point;
30
+ uint8_t word;
31
+
32
+ while (pos < len) {
33
+ uint64_t next_pos = pos + 16;
34
+ if (next_pos <= len) {
35
+ uint64_t v1;
36
+ memcpy(&v1, data + pos, sizeof(uint64_t));
37
+ uint64_t v2;
38
+ memcpy(&v2, data + pos + sizeof(uint64_t), sizeof(uint64_t));
39
+ uint64_t v = v1 | v2;
40
+ if ((v & 0x8080808080808080) == 0) {
41
+ pos = next_pos;
42
+ continue;
43
+ }
44
+ }
45
+
46
+ word = data[pos];
47
+ while (word < 0b10000000) {
48
+ if (++pos == len) return true;
49
+ word = data[pos];
50
+ }
51
+
52
+ if ((word & 0b11100000) == 0b11000000) {
53
+ next_pos = pos + 2;
54
+ if (next_pos > len) {
55
+ return false;
56
+ }
57
+ if ((data[pos + 1] & 0b11000000) != 0b10000000) {
58
+ return false;
59
+ }
60
+ code_point = (word & 0b00011111) << 6 | (data[pos + 1] & 0b00111111);
61
+ if ((code_point < 0x80) || (0x7ff < code_point)) {
62
+ return false;
63
+ }
64
+ } else if ((word & 0b11110000) == 0b11100000) {
65
+ next_pos = pos + 3;
66
+ if (next_pos > len) {
67
+ return false;
68
+ }
69
+ if ((data[pos + 1] & 0b11000000) != 0b10000000) {
70
+ return false;
71
+ }
72
+ if ((data[pos + 2] & 0b11000000) != 0b10000000) {
73
+ return false;
74
+ }
75
+ code_point = (word & 0b00001111) << 12 |
76
+ (data[pos + 1] & 0b00111111) << 6 |
77
+ (data[pos + 2] & 0b00111111);
78
+ if ((code_point < 0x800) || (0xffff < code_point) || (0xd7ff < code_point && code_point < 0xe000)) {
79
+ return false;
80
+ }
81
+ } else if ((word & 0b11111000) == 0b11110000) {
82
+ next_pos = pos + 4;
83
+ if (next_pos > len) {
84
+ return false;
85
+ }
86
+ if ((data[pos + 1] & 0b11000000) != 0b10000000) {
87
+ return false;
88
+ }
89
+ if ((data[pos + 2] & 0b11000000) != 0b10000000) {
90
+ return false;
91
+ }
92
+ if ((data[pos + 3] & 0b11000000) != 0b10000000) {
93
+ return false;
94
+ }
95
+ code_point = (word & 0b00000111) << 18 | (data[pos + 1] & 0b00111111) << 12 |
96
+ (data[pos + 2] & 0b00111111) << 6 | (data[pos + 3] & 0b00111111);
97
+ if (code_point <= 0xffff || 0x10ffff < code_point) {
98
+ return false;
99
+ }
100
+ } else {
101
+ return false;
102
+ }
103
+ pos = next_pos;
104
+ }
105
+
106
+ return true;
107
+ }