@ohos-ports/bare-buffer 3.7.1-beta.0

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.
Files changed (155) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +26 -0
  3. package/binding.c +1077 -0
  4. package/binding.gyp +32 -0
  5. package/binding.js +1 -0
  6. package/build/Release/bare_buffer.node +0 -0
  7. package/deps/libbase64/.clang-format +10 -0
  8. package/deps/libbase64/.gitattributes +1 -0
  9. package/deps/libbase64/.github/workflows/integrate.yml +45 -0
  10. package/deps/libbase64/.github/workflows/nightly.yml +28 -0
  11. package/deps/libbase64/.prettierrc +1 -0
  12. package/deps/libbase64/CMakeLists.txt +71 -0
  13. package/deps/libbase64/LICENSE +201 -0
  14. package/deps/libbase64/NOTICE +13 -0
  15. package/deps/libbase64/README.md +11 -0
  16. package/deps/libbase64/include/base64.h +323 -0
  17. package/deps/libbase64/package.json +25 -0
  18. package/deps/libbase64/src/base64.c +22 -0
  19. package/deps/libbase64/test/CMakeLists.txt +31 -0
  20. package/deps/libbase64/test/decode-invalid.c +35 -0
  21. package/deps/libbase64/test/decode-length.c +23 -0
  22. package/deps/libbase64/test/decode.c +36 -0
  23. package/deps/libbase64/test/encode-length.c +22 -0
  24. package/deps/libbase64/test/encode.c +42 -0
  25. package/deps/libbase64/test/fuzz/CMakeLists.txt +18 -0
  26. package/deps/libbase64/test/fuzz/decode/utf16le.c +53 -0
  27. package/deps/libbase64/test/fuzz/decode/utf8.c +47 -0
  28. package/deps/libbase64/test/fuzz/encode/utf16le.c +56 -0
  29. package/deps/libbase64/test/fuzz/encode/utf8.c +56 -0
  30. package/deps/libhex/.clang-format +10 -0
  31. package/deps/libhex/.gitattributes +1 -0
  32. package/deps/libhex/.github/workflows/integrate.yml +45 -0
  33. package/deps/libhex/.github/workflows/nightly.yml +28 -0
  34. package/deps/libhex/.prettierrc +1 -0
  35. package/deps/libhex/CMakeLists.txt +72 -0
  36. package/deps/libhex/LICENSE +201 -0
  37. package/deps/libhex/NOTICE +13 -0
  38. package/deps/libhex/README.md +11 -0
  39. package/deps/libhex/include/hex.h +163 -0
  40. package/deps/libhex/package.json +25 -0
  41. package/deps/libhex/src/hex.c +16 -0
  42. package/deps/libhex/test/CMakeLists.txt +31 -0
  43. package/deps/libhex/test/decode-invalid.c +37 -0
  44. package/deps/libhex/test/decode-length.c +23 -0
  45. package/deps/libhex/test/decode.c +25 -0
  46. package/deps/libhex/test/encode-length.c +22 -0
  47. package/deps/libhex/test/encode.c +25 -0
  48. package/deps/libhex/test/fuzz/CMakeLists.txt +18 -0
  49. package/deps/libhex/test/fuzz/decode/utf16le.c +53 -0
  50. package/deps/libhex/test/fuzz/decode/utf8.c +47 -0
  51. package/deps/libhex/test/fuzz/encode/utf16le.c +36 -0
  52. package/deps/libhex/test/fuzz/encode/utf8.c +36 -0
  53. package/deps/libutf/.clang-format +10 -0
  54. package/deps/libutf/.gitattributes +1 -0
  55. package/deps/libutf/.github/workflows/integrate.yml +45 -0
  56. package/deps/libutf/.github/workflows/nightly.yml +28 -0
  57. package/deps/libutf/.prettierrc +1 -0
  58. package/deps/libutf/CMakeLists.txt +105 -0
  59. package/deps/libutf/LICENSE +201 -0
  60. package/deps/libutf/NOTICE +13 -0
  61. package/deps/libutf/README.md +11 -0
  62. package/deps/libutf/include/utf/string.h +1655 -0
  63. package/deps/libutf/include/utf.h +132 -0
  64. package/deps/libutf/package.json +24 -0
  65. package/deps/libutf/src/ascii/validate.c +47 -0
  66. package/deps/libutf/src/endianness.c +19 -0
  67. package/deps/libutf/src/endianness.h +54 -0
  68. package/deps/libutf/src/latin1/convert-to-utf16.c +37 -0
  69. package/deps/libutf/src/latin1/convert-to-utf32.c +34 -0
  70. package/deps/libutf/src/latin1/convert-to-utf8.c +58 -0
  71. package/deps/libutf/src/latin1/length-from-utf16.c +26 -0
  72. package/deps/libutf/src/latin1/length-from-utf32.c +26 -0
  73. package/deps/libutf/src/latin1/length-from-utf8.c +34 -0
  74. package/deps/libutf/src/utf16/convert-to-latin1.c +41 -0
  75. package/deps/libutf/src/utf16/convert-to-utf32.c +56 -0
  76. package/deps/libutf/src/utf16/convert-to-utf8.c +80 -0
  77. package/deps/libutf/src/utf16/length-from-latin1.c +26 -0
  78. package/deps/libutf/src/utf16/length-from-utf32.c +33 -0
  79. package/deps/libutf/src/utf16/length-from-utf8.c +38 -0
  80. package/deps/libutf/src/utf16/validate.c +53 -0
  81. package/deps/libutf/src/utf32/convert-to-latin1.c +40 -0
  82. package/deps/libutf/src/utf32/convert-to-utf16.c +56 -0
  83. package/deps/libutf/src/utf32/convert-to-utf8.c +71 -0
  84. package/deps/libutf/src/utf32/length-from-latin1.c +26 -0
  85. package/deps/libutf/src/utf32/length-from-utf16.c +35 -0
  86. package/deps/libutf/src/utf32/length-from-utf8.c +35 -0
  87. package/deps/libutf/src/utf32/string.c +149 -0
  88. package/deps/libutf/src/utf32/validate.c +39 -0
  89. package/deps/libutf/src/utf8/convert-to-latin1.c +70 -0
  90. package/deps/libutf/src/utf8/convert-to-utf16.c +95 -0
  91. package/deps/libutf/src/utf8/convert-to-utf32.c +110 -0
  92. package/deps/libutf/src/utf8/length-from-latin1.c +32 -0
  93. package/deps/libutf/src/utf8/length-from-utf16.c +48 -0
  94. package/deps/libutf/src/utf8/length-from-utf32.c +35 -0
  95. package/deps/libutf/src/utf8/string.c +149 -0
  96. package/deps/libutf/src/utf8/validate.c +107 -0
  97. package/deps/libutf/test/CMakeLists.txt +56 -0
  98. package/deps/libutf/test/fuzz/CMakeLists.txt +26 -0
  99. package/deps/libutf/test/fuzz/corpus/.gitignore +2 -0
  100. package/deps/libutf/test/fuzz/crashes/.gitignore +2 -0
  101. package/deps/libutf/test/fuzz/latin1/convert-to-utf16.c +32 -0
  102. package/deps/libutf/test/fuzz/latin1/convert-to-utf32.c +32 -0
  103. package/deps/libutf/test/fuzz/latin1/convert-to-utf8.c +33 -0
  104. package/deps/libutf/test/fuzz/utf16/convert-to-latin1.c +36 -0
  105. package/deps/libutf/test/fuzz/utf16/convert-to-utf32.c +38 -0
  106. package/deps/libutf/test/fuzz/utf16/convert-to-utf8.c +38 -0
  107. package/deps/libutf/test/fuzz/utf32/convert-to-latin1.c +36 -0
  108. package/deps/libutf/test/fuzz/utf32/convert-to-utf16.c +38 -0
  109. package/deps/libutf/test/fuzz/utf32/convert-to-utf8.c +38 -0
  110. package/deps/libutf/test/fuzz/utf8/convert-to-latin1.c +33 -0
  111. package/deps/libutf/test/fuzz/utf8/convert-to-utf16.c +35 -0
  112. package/deps/libutf/test/fuzz/utf8/convert-to-utf32.c +35 -0
  113. package/deps/libutf/test/utf16/utf8-convert.c +45 -0
  114. package/deps/libutf/test/utf16/utf8-length.c +41 -0
  115. package/deps/libutf/test/utf16/validate-invalid.c +50 -0
  116. package/deps/libutf/test/utf16/validate.c +32 -0
  117. package/deps/libutf/test/utf32/string-append.c +74 -0
  118. package/deps/libutf/test/utf32/string-compare.c +108 -0
  119. package/deps/libutf/test/utf32/string-erase.c +79 -0
  120. package/deps/libutf/test/utf32/string-index.c +102 -0
  121. package/deps/libutf/test/utf32/string-insert.c +92 -0
  122. package/deps/libutf/test/utf32/string-last-index.c +71 -0
  123. package/deps/libutf/test/utf32/string-prepend.c +51 -0
  124. package/deps/libutf/test/utf32/string-replace.c +88 -0
  125. package/deps/libutf/test/utf32/string-reserve.c +99 -0
  126. package/deps/libutf/test/utf32/string-substring.c +93 -0
  127. package/deps/libutf/test/utf8/latin1-convert.c +23 -0
  128. package/deps/libutf/test/utf8/string-append.c +23 -0
  129. package/deps/libutf/test/utf8/string-compare.c +67 -0
  130. package/deps/libutf/test/utf8/string-erase.c +75 -0
  131. package/deps/libutf/test/utf8/string-index.c +99 -0
  132. package/deps/libutf/test/utf8/string-last-index.c +68 -0
  133. package/deps/libutf/test/utf8/string-prepend.c +23 -0
  134. package/deps/libutf/test/utf8/string-replace.c +74 -0
  135. package/deps/libutf/test/utf8/string-reserve.c +51 -0
  136. package/deps/libutf/test/utf8/string-substring.c +68 -0
  137. package/deps/libutf/test/utf8/utf16-convert.c +32 -0
  138. package/deps/libutf/test/utf8/utf16-length.c +29 -0
  139. package/deps/libutf/test/utf8/validate-invalid.c +26 -0
  140. package/deps/libutf/test/utf8/validate.c +21 -0
  141. package/global.d.ts +21 -0
  142. package/global.js +6 -0
  143. package/index.d.ts +769 -0
  144. package/index.js +1339 -0
  145. package/lib/ascii.js +36 -0
  146. package/lib/base64.js +25 -0
  147. package/lib/base64url.js +25 -0
  148. package/lib/checked.js +23 -0
  149. package/lib/constants.d.ts +7 -0
  150. package/lib/constants.js +3 -0
  151. package/lib/hex.js +20 -0
  152. package/lib/latin1.js +14 -0
  153. package/lib/utf16le.js +20 -0
  154. package/lib/utf8.js +18 -0
  155. package/package.json +64 -0
@@ -0,0 +1,32 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ // Each Latin-1 byte maps to one UTF-16 unit.
12
+ utf16_t *utf16 = size > 0 ? malloc(size * sizeof(utf16_t)) : NULL;
13
+
14
+ size_t utf16_len = latin1_convert_to_utf16le((const latin1_t *) data, size, utf16);
15
+
16
+ assert(utf16_len == size);
17
+
18
+ if (size > 0) {
19
+ latin1_t *latin1 = malloc(size);
20
+
21
+ size_t latin1_len = utf16le_convert_to_latin1(utf16, utf16_len, latin1);
22
+
23
+ assert(latin1_len == size);
24
+ assert(memcmp(latin1, data, size) == 0);
25
+
26
+ free(latin1);
27
+ }
28
+
29
+ free(utf16);
30
+
31
+ return 0;
32
+ }
@@ -0,0 +1,32 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ // Each Latin-1 byte maps to one UTF-32 unit.
12
+ utf32_t *utf32 = size > 0 ? malloc(size * sizeof(utf32_t)) : NULL;
13
+
14
+ size_t utf32_len = latin1_convert_to_utf32((const latin1_t *) data, size, utf32);
15
+
16
+ assert(utf32_len == size);
17
+
18
+ if (size > 0) {
19
+ latin1_t *latin1 = malloc(size);
20
+
21
+ size_t latin1_len = utf32_convert_to_latin1(utf32, utf32_len, latin1);
22
+
23
+ assert(latin1_len == size);
24
+ assert(memcmp(latin1, data, size) == 0);
25
+
26
+ free(latin1);
27
+ }
28
+
29
+ free(utf32);
30
+
31
+ return 0;
32
+ }
@@ -0,0 +1,33 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ // Each Latin-1 byte expands to at most 2 UTF-8 bytes.
12
+ utf8_t *utf8 = size > 0 ? malloc(size * 2) : NULL;
13
+
14
+ size_t utf8_len = latin1_convert_to_utf8((const latin1_t *) data, size, utf8);
15
+
16
+ assert(utf8_len >= size);
17
+ assert(utf8_len <= size * 2);
18
+
19
+ if (size > 0) {
20
+ latin1_t *latin1 = malloc(size);
21
+
22
+ size_t latin1_len = utf8_convert_to_latin1(utf8, utf8_len, latin1);
23
+
24
+ assert(latin1_len == size);
25
+ assert(memcmp(latin1, data, size) == 0);
26
+
27
+ free(latin1);
28
+ }
29
+
30
+ free(utf8);
31
+
32
+ return 0;
33
+ }
@@ -0,0 +1,36 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ size_t units = size / sizeof(utf16_t);
12
+ const utf16_t *input = (const utf16_t *) data;
13
+
14
+ // Each UTF-16 unit maps to at most one Latin-1 byte.
15
+ latin1_t *latin1 = units > 0 ? malloc(units) : NULL;
16
+
17
+ size_t latin1_len = utf16le_convert_to_latin1(input, units, latin1);
18
+
19
+ assert(latin1_len <= units);
20
+
21
+ if (latin1_len > 0) {
22
+ // Each Latin-1 byte maps to one UTF-16 unit.
23
+ utf16_t *utf16 = malloc(latin1_len * sizeof(utf16_t));
24
+
25
+ size_t utf16_len = latin1_convert_to_utf16le(latin1, latin1_len, utf16);
26
+
27
+ assert(utf16_len == units);
28
+ assert(memcmp(utf16, input, units * sizeof(utf16_t)) == 0);
29
+
30
+ free(utf16);
31
+ }
32
+
33
+ free(latin1);
34
+
35
+ return 0;
36
+ }
@@ -0,0 +1,38 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ size_t units = size / sizeof(utf16_t);
12
+ const utf16_t *input = (const utf16_t *) data;
13
+
14
+ if (!utf16le_validate(input, units)) return -1;
15
+
16
+ // UTF-32 unit count never exceeds UTF-16 unit count.
17
+ utf32_t *utf32 = units > 0 ? malloc(units * sizeof(utf32_t)) : NULL;
18
+
19
+ size_t utf32_len = utf16le_convert_to_utf32(input, units, utf32);
20
+
21
+ assert(utf32_len <= units);
22
+
23
+ if (units > 0) {
24
+ // Each UTF-32 unit expands to at most 2 UTF-16 units (surrogate pair).
25
+ utf16_t *utf16 = malloc(utf32_len * 2 * sizeof(utf16_t));
26
+
27
+ size_t utf16_len = utf32_convert_to_utf16le(utf32, utf32_len, utf16);
28
+
29
+ assert(utf16_len == units);
30
+ assert(memcmp(utf16, input, units * sizeof(utf16_t)) == 0);
31
+
32
+ free(utf16);
33
+ }
34
+
35
+ free(utf32);
36
+
37
+ return 0;
38
+ }
@@ -0,0 +1,38 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ size_t units = size / sizeof(utf16_t);
12
+ const utf16_t *input = (const utf16_t *) data;
13
+
14
+ if (!utf16le_validate(input, units)) return -1;
15
+
16
+ // Each UTF-16 unit expands to at most 3 UTF-8 bytes.
17
+ utf8_t *utf8 = units > 0 ? malloc(units * 3) : NULL;
18
+
19
+ size_t utf8_len = utf16le_convert_to_utf8(input, units, utf8);
20
+
21
+ assert(utf8_len <= units * 3);
22
+
23
+ if (units > 0) {
24
+ // UTF-16 unit count never exceeds UTF-8 byte count.
25
+ utf16_t *utf16 = malloc(utf8_len * sizeof(utf16_t));
26
+
27
+ size_t utf16_len = utf8_convert_to_utf16le(utf8, utf8_len, utf16);
28
+
29
+ assert(utf16_len == units);
30
+ assert(memcmp(utf16, input, units * sizeof(utf16_t)) == 0);
31
+
32
+ free(utf16);
33
+ }
34
+
35
+ free(utf8);
36
+
37
+ return 0;
38
+ }
@@ -0,0 +1,36 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ size_t units = size / sizeof(utf32_t);
12
+ const utf32_t *input = (const utf32_t *) data;
13
+
14
+ // Each UTF-32 unit maps to at most one Latin-1 byte.
15
+ latin1_t *latin1 = units > 0 ? malloc(units) : NULL;
16
+
17
+ size_t latin1_len = utf32_convert_to_latin1(input, units, latin1);
18
+
19
+ assert(latin1_len <= units);
20
+
21
+ if (latin1_len > 0) {
22
+ // Each Latin-1 byte maps to one UTF-32 unit.
23
+ utf32_t *utf32 = malloc(latin1_len * sizeof(utf32_t));
24
+
25
+ size_t utf32_len = latin1_convert_to_utf32(latin1, latin1_len, utf32);
26
+
27
+ assert(utf32_len == units);
28
+ assert(memcmp(utf32, input, units * sizeof(utf32_t)) == 0);
29
+
30
+ free(utf32);
31
+ }
32
+
33
+ free(latin1);
34
+
35
+ return 0;
36
+ }
@@ -0,0 +1,38 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ size_t units = size / sizeof(utf32_t);
12
+ const utf32_t *input = (const utf32_t *) data;
13
+
14
+ if (!utf32_validate(input, units)) return -1;
15
+
16
+ // Each UTF-32 unit expands to at most 2 UTF-16 units (surrogate pair).
17
+ utf16_t *utf16 = units > 0 ? malloc(units * 2 * sizeof(utf16_t)) : NULL;
18
+
19
+ size_t utf16_len = utf32_convert_to_utf16le(input, units, utf16);
20
+
21
+ assert(utf16_len <= units * 2);
22
+
23
+ if (units > 0) {
24
+ // UTF-32 unit count never exceeds UTF-16 unit count.
25
+ utf32_t *utf32 = malloc(utf16_len * sizeof(utf32_t));
26
+
27
+ size_t utf32_len = utf16le_convert_to_utf32(utf16, utf16_len, utf32);
28
+
29
+ assert(utf32_len == units);
30
+ assert(memcmp(utf32, input, units * sizeof(utf32_t)) == 0);
31
+
32
+ free(utf32);
33
+ }
34
+
35
+ free(utf16);
36
+
37
+ return 0;
38
+ }
@@ -0,0 +1,38 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ size_t units = size / sizeof(utf32_t);
12
+ const utf32_t *input = (const utf32_t *) data;
13
+
14
+ if (!utf32_validate(input, units)) return -1;
15
+
16
+ // Each UTF-32 unit expands to at most 4 UTF-8 bytes.
17
+ utf8_t *utf8 = units > 0 ? malloc(units * 4) : NULL;
18
+
19
+ size_t utf8_len = utf32_convert_to_utf8(input, units, utf8);
20
+
21
+ assert(utf8_len <= units * 4);
22
+
23
+ if (units > 0) {
24
+ // UTF-32 unit count never exceeds UTF-8 byte count.
25
+ utf32_t *utf32 = malloc(utf8_len * sizeof(utf32_t));
26
+
27
+ size_t utf32_len = utf8_convert_to_utf32(utf8, utf8_len, utf32);
28
+
29
+ assert(utf32_len == units);
30
+ assert(memcmp(utf32, input, units * sizeof(utf32_t)) == 0);
31
+
32
+ free(utf32);
33
+ }
34
+
35
+ free(utf8);
36
+
37
+ return 0;
38
+ }
@@ -0,0 +1,33 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ // Latin-1 output is never longer than the UTF-8 input.
12
+ latin1_t *latin1 = size > 0 ? malloc(size) : NULL;
13
+
14
+ size_t latin1_len = utf8_convert_to_latin1((const utf8_t *) data, size, latin1);
15
+
16
+ assert(latin1_len <= size);
17
+
18
+ if (latin1_len > 0) {
19
+ // Each Latin-1 byte expands to at most 2 UTF-8 bytes.
20
+ utf8_t *utf8 = malloc(latin1_len * 2);
21
+
22
+ size_t utf8_len = latin1_convert_to_utf8(latin1, latin1_len, utf8);
23
+
24
+ assert(utf8_len == size);
25
+ assert(memcmp(utf8, data, size) == 0);
26
+
27
+ free(utf8);
28
+ }
29
+
30
+ free(latin1);
31
+
32
+ return 0;
33
+ }
@@ -0,0 +1,35 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ if (!utf8_validate((const utf8_t *) data, size)) return -1;
12
+
13
+ // UTF-16 unit count never exceeds UTF-8 byte count.
14
+ utf16_t *utf16 = size > 0 ? malloc(size * sizeof(utf16_t)) : NULL;
15
+
16
+ size_t utf16_len = utf8_convert_to_utf16le((const utf8_t *) data, size, utf16);
17
+
18
+ assert(utf16_len <= size);
19
+
20
+ if (size > 0) {
21
+ // Each UTF-16 unit expands to at most 3 UTF-8 bytes.
22
+ utf8_t *utf8 = malloc(utf16_len * 3);
23
+
24
+ size_t utf8_len = utf16le_convert_to_utf8(utf16, utf16_len, utf8);
25
+
26
+ assert(utf8_len == size);
27
+ assert(memcmp(utf8, data, size) == 0);
28
+
29
+ free(utf8);
30
+ }
31
+
32
+ free(utf16);
33
+
34
+ return 0;
35
+ }
@@ -0,0 +1,35 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+
7
+ #include "../../../include/utf.h"
8
+
9
+ int
10
+ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11
+ if (!utf8_validate((const utf8_t *) data, size)) return -1;
12
+
13
+ // UTF-32 unit count never exceeds UTF-8 byte count.
14
+ utf32_t *utf32 = size > 0 ? malloc(size * sizeof(utf32_t)) : NULL;
15
+
16
+ size_t utf32_len = utf8_convert_to_utf32((const utf8_t *) data, size, utf32);
17
+
18
+ assert(utf32_len <= size);
19
+
20
+ if (size > 0) {
21
+ // Each UTF-32 unit expands to at most 4 UTF-8 bytes.
22
+ utf8_t *utf8 = malloc(utf32_len * 4);
23
+
24
+ size_t utf8_len = utf32_convert_to_utf8(utf32, utf32_len, utf8);
25
+
26
+ assert(utf8_len == size);
27
+ assert(memcmp(utf8, data, size) == 0);
28
+
29
+ free(utf8);
30
+ }
31
+
32
+ free(utf32);
33
+
34
+ return 0;
35
+ }
@@ -0,0 +1,45 @@
1
+ #include <assert.h>
2
+ #include <string.h>
3
+
4
+ #include "../../include/utf.h"
5
+
6
+ #define test_convert(string, len, expected, written) \
7
+ { \
8
+ utf8_t result[len * 3]; \
9
+ assert(utf16le_convert_to_utf8((utf16_t *) string, len, result) == written); \
10
+ assert(memcmp(expected, result, written) == 0); \
11
+ }
12
+
13
+ int
14
+ main() {
15
+ // ASCII only
16
+ test_convert("\x61\x00\x62\x00\x63\x00", 3, "abc", 3);
17
+
18
+ // Greek alphabet
19
+ test_convert("\xb1\x03\xb2\x03\xb3\x03", 3, "\xce\xb1\xce\xb2\xce\xb3", 6);
20
+
21
+ // Chinese characters
22
+ test_convert("\x4e\x67\xf3\x97\x2d\x4e", 3, "\xe6\x9d\x8e\xe9\x9f\xb3\xe4\xb8\xad", 9);
23
+
24
+ // Mix of ASCII, Greek and Chinese
25
+ test_convert("\x61\x00\xb1\x03\x4e\x67", 3, "a\xce\xb1\xe6\x9d\x8e", 6);
26
+
27
+ // Surrogate pair
28
+ test_convert("\x00\xd8\x37\xdc", 2, "\xf0\x90\x80\xb7", 4);
29
+
30
+ // Lone high surrogate followed by ASCII, must skip the surrogate (not consume the ASCII)
31
+ test_convert("\x00\xd8\x41\x00", 2, "\x41", 1);
32
+
33
+ // Lone low surrogate followed by ASCII, must skip the surrogate
34
+ test_convert("\x00\xdc\x41\x00", 2, "\x41", 1);
35
+
36
+ // High surrogate followed by another high surrogate that begins a valid pair
37
+ test_convert("\x00\xd8\x00\xd8\x37\xdc", 3, "\xf0\x90\x80\xb7", 4);
38
+
39
+ // Lone trailing high surrogate (truncated), must not OOB-read
40
+ {
41
+ utf8_t result[1] = {0xaa};
42
+ assert(utf16le_convert_to_utf8((utf16_t *) "\x00\xd8", 1, result) == 0);
43
+ assert(result[0] == 0xaa);
44
+ }
45
+ }
@@ -0,0 +1,41 @@
1
+ #include <assert.h>
2
+
3
+ #include "../../include/utf.h"
4
+
5
+ #define test_length(string, len, expected) \
6
+ { \
7
+ assert(utf8_length_from_utf16le((utf16_t *) string, len) == expected); \
8
+ }
9
+
10
+ int
11
+ main() {
12
+ // ASCII character 'A'
13
+ test_length("\x41\x00", 1, 1);
14
+
15
+ // Greek capital letter 'Ω'
16
+ test_length("\xa9\x03", 1, 2);
17
+
18
+ // Snowman symbol '☃'
19
+ test_length("\x03\x26", 1, 3);
20
+
21
+ // Chinese character '中'
22
+ test_length("\x2d\x4e", 1, 3);
23
+
24
+ // Face with tears of joy emoji '😂'
25
+ test_length("\x3d\xd8\x02\xde", 2, 4);
26
+
27
+ // Multiple characters 'AΩ☃中😂'
28
+ test_length("\x41\x00\xa9\x03\x03\x26\x2d\x4e\x3d\xd8\x02\xde", 6, 13);
29
+
30
+ // Lone high surrogate followed by ASCII, surrogate contributes 0
31
+ test_length("\x00\xd8\x41\x00", 2, 1);
32
+
33
+ // Lone low surrogate followed by ASCII, surrogate contributes 0
34
+ test_length("\x00\xdc\x41\x00", 2, 1);
35
+
36
+ // Lone trailing high surrogate, contributes 0
37
+ test_length("\x00\xd8", 1, 0);
38
+
39
+ // High surrogate not followed by low surrogate, first contributes 0, pair contributes 4
40
+ test_length("\x00\xd8\x00\xd8\x37\xdc", 3, 4);
41
+ }
@@ -0,0 +1,50 @@
1
+ #include <assert.h>
2
+
3
+ #include "../../include/utf.h"
4
+
5
+ #define test_validate(string, len) \
6
+ { \
7
+ assert(!utf16le_validate((utf16_t *) string, len)); \
8
+ }
9
+
10
+ int
11
+ main() {
12
+ // An isolated high surrogate
13
+ test_validate("\x01\xd8", 1);
14
+
15
+ // An isolated low surrogate
16
+ test_validate("\x37\xdc", 1);
17
+
18
+ // A high surrogate followed by another high surrogate
19
+ test_validate("\x01\xd8\x02\xd8", 2);
20
+
21
+ // A low surrogate followed by another low surrogate
22
+ test_validate("\x37\xdc\x38\xdc", 2);
23
+
24
+ // A high surrogate followed by a non-surrogate
25
+ // test_validate("\x01\xd8\xe8\x00", 2);
26
+
27
+ // A low surrogate followed by a non-surrogate
28
+ // test_validate("\x37\xdc\xe8\x00", 2);
29
+
30
+ // A non-surrogate followed by a low surrogate
31
+ // test_validate("\xe8\x00\x37\xdc");
32
+
33
+ // A non-surrogate followed by a high surrogate
34
+ // test_validate("\xe8\x00\x01\xd8");
35
+
36
+ // A surrogate pair in the wrong order
37
+ test_validate("\x37\xdc\x01\xd8", 2);
38
+
39
+ // Two high surrogates followed by a low surrogate
40
+ test_validate("\x01\xd8\x02\xd8\x37\xdc", 3);
41
+
42
+ // A high surrogate followed by two low surrogates
43
+ test_validate("\x01\xd8\x37\xdc\x38\xdc", 3);
44
+
45
+ // Two high surrogates in a row
46
+ test_validate("\x01\xd8\x02\xd8", 2);
47
+
48
+ // Two low surrogates in a row
49
+ test_validate("\x37\xdc\x38\xdc", 2);
50
+ }
@@ -0,0 +1,32 @@
1
+ #include <assert.h>
2
+
3
+ #include "../../include/utf.h"
4
+
5
+ #define test_validate(string, len) \
6
+ { \
7
+ assert(utf16le_validate((utf16_t *) string, len)); \
8
+ }
9
+
10
+ int
11
+ main() {
12
+ // Single ASCII character, 'a'
13
+ test_validate("\x61\x00", 1);
14
+
15
+ // String of ASCII characters, "Hello"
16
+ test_validate("\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00", 5);
17
+
18
+ // Single 2-byte UTF-16LE character, 'è'
19
+ test_validate("\xe8\x00", 1);
20
+
21
+ // String of 2-byte UTF-16LE characters, "èéêë"
22
+ test_validate("\xe8\x00\xe9\x00\xea\x00\xeb\x00", 4);
23
+
24
+ // Single 4-byte UTF-16LE character (represented as a surrogate pair), '𐐷'
25
+ test_validate("\x01\xd8\x37\xdc", 2);
26
+
27
+ // String of 4-byte UTF-16LE characters (represented as surrogate pairs), "𐐷𐑏"
28
+ test_validate("\x01\xd8\x37\xdc\x3f\xd8\x1f\xdc", 4);
29
+
30
+ // A mix of 2-byte and 4-byte UTF-16LE characters, "Hello𐐷"
31
+ test_validate("\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00\x01\xd8\x37\xdc", 7);
32
+ }
@@ -0,0 +1,74 @@
1
+ #include <assert.h>
2
+ #include <stddef.h>
3
+
4
+ #include "../../include/utf.h"
5
+ #include "../../include/utf/string.h"
6
+
7
+ int
8
+ main() {
9
+ int e;
10
+
11
+ static const utf32_t hello[] = {'h', 'e', 'l', 'l', 'o', 0};
12
+ static const utf32_t world[] = {' ', 'w', 'o', 'r', 'l', 'd', 0};
13
+ static const utf32_t expected[] = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0};
14
+
15
+ // A length of (size_t) -1 takes the length of a null terminated sequence of
16
+ // code points, standing in for the `strlen` of the UTF-8 string.
17
+ {
18
+ utf32_string_t string;
19
+ utf32_string_init(&string);
20
+
21
+ e = utf32_string_append_literal(&string, hello, (size_t) -1);
22
+ assert(e == 0);
23
+
24
+ e = utf32_string_append_literal(&string, world, (size_t) -1);
25
+ assert(e == 0);
26
+
27
+ assert(string.len == 11);
28
+ assert(utf32_string_compare_literal(&string, expected, (size_t) -1) == 0);
29
+
30
+ utf32_string_destroy(&string);
31
+ }
32
+
33
+ // Appending code point by code point grows past the inline buffer, keeping
34
+ // everything that came before.
35
+ {
36
+ utf32_string_t string;
37
+ utf32_string_init(&string);
38
+
39
+ for (size_t i = 0; i < UTF32_STRING_INLINE_CAPACITY * 4; i++) {
40
+ e = utf32_string_append_character(&string, (utf32_t) (0x10000 + i));
41
+ assert(e == 0);
42
+ }
43
+
44
+ assert(string.len == UTF32_STRING_INLINE_CAPACITY * 4);
45
+ assert(string.data != string.buf);
46
+
47
+ for (size_t i = 0; i < string.len; i++) {
48
+ assert(string.data[i] == 0x10000 + i);
49
+ }
50
+
51
+ utf32_string_destroy(&string);
52
+ }
53
+
54
+ // Appending a string and a view leaves the same result.
55
+ {
56
+ utf32_string_t a, b;
57
+ utf32_string_init(&a);
58
+ utf32_string_init(&b);
59
+
60
+ e = utf32_string_append_literal(&a, hello, (size_t) -1);
61
+ assert(e == 0);
62
+
63
+ e = utf32_string_append(&b, &a);
64
+ assert(e == 0);
65
+
66
+ e = utf32_string_append_view(&b, utf32_string_view_init(world, 6));
67
+ assert(e == 0);
68
+
69
+ assert(utf32_string_compare_literal(&b, expected, (size_t) -1) == 0);
70
+
71
+ utf32_string_destroy(&a);
72
+ utf32_string_destroy(&b);
73
+ }
74
+ }