ffi-hydrogen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +30 -0
  5. data/.travis.yml +10 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +72 -0
  9. data/Rakefile +46 -0
  10. data/bench/both.rb +86 -0
  11. data/bench/encode.rb +57 -0
  12. data/bench/encrypt.rb +80 -0
  13. data/bench/init.rb +5 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/ffi-hydrogen.gemspec +31 -0
  17. data/lib/ffi/hydrogen.rb +216 -0
  18. data/vendor/.clang-format +2 -0
  19. data/vendor/.gitignore +3 -0
  20. data/vendor/README.md +2 -0
  21. data/vendor/libhydrogen/.clang-format +95 -0
  22. data/vendor/libhydrogen/.gitignore +32 -0
  23. data/vendor/libhydrogen/.travis.yml +22 -0
  24. data/vendor/libhydrogen/LICENSE +18 -0
  25. data/vendor/libhydrogen/Makefile +61 -0
  26. data/vendor/libhydrogen/Makefile.arduino +51 -0
  27. data/vendor/libhydrogen/README.md +29 -0
  28. data/vendor/libhydrogen/hydrogen.c +18 -0
  29. data/vendor/libhydrogen/hydrogen.h +317 -0
  30. data/vendor/libhydrogen/impl/common.h +316 -0
  31. data/vendor/libhydrogen/impl/core.h +220 -0
  32. data/vendor/libhydrogen/impl/gimli-core/portable.h +39 -0
  33. data/vendor/libhydrogen/impl/gimli-core/sse2.h +97 -0
  34. data/vendor/libhydrogen/impl/gimli-core.h +25 -0
  35. data/vendor/libhydrogen/impl/hash.h +138 -0
  36. data/vendor/libhydrogen/impl/hydrogen_p.h +83 -0
  37. data/vendor/libhydrogen/impl/kdf.h +20 -0
  38. data/vendor/libhydrogen/impl/kx.h +441 -0
  39. data/vendor/libhydrogen/impl/pwhash.h +281 -0
  40. data/vendor/libhydrogen/impl/random.h +376 -0
  41. data/vendor/libhydrogen/impl/secretbox.h +236 -0
  42. data/vendor/libhydrogen/impl/sign.h +207 -0
  43. data/vendor/libhydrogen/impl/x25519.h +383 -0
  44. data/vendor/libhydrogen/library.properties +10 -0
  45. data/vendor/libhydrogen/logo.png +0 -0
  46. data/vendor/libhydrogen/tests/tests.c +431 -0
  47. data/vendor/main.c +140 -0
  48. data/vendor/stringencoders/.gitignore +25 -0
  49. data/vendor/stringencoders/.travis.yml +13 -0
  50. data/vendor/stringencoders/AUTHORS +1 -0
  51. data/vendor/stringencoders/COPYING +2 -0
  52. data/vendor/stringencoders/ChangeLog +170 -0
  53. data/vendor/stringencoders/Doxyfile +276 -0
  54. data/vendor/stringencoders/INSTALL +119 -0
  55. data/vendor/stringencoders/LICENSE +22 -0
  56. data/vendor/stringencoders/Makefile.am +3 -0
  57. data/vendor/stringencoders/NEWS +3 -0
  58. data/vendor/stringencoders/README +2 -0
  59. data/vendor/stringencoders/README.md +32 -0
  60. data/vendor/stringencoders/bootstrap.sh +3 -0
  61. data/vendor/stringencoders/configure-gcc-hardened.sh +16 -0
  62. data/vendor/stringencoders/configure.ac +44 -0
  63. data/vendor/stringencoders/doxy/footer.html +34 -0
  64. data/vendor/stringencoders/doxy/header.html +85 -0
  65. data/vendor/stringencoders/indent.sh +9 -0
  66. data/vendor/stringencoders/javascript/base64-speed.html +43 -0
  67. data/vendor/stringencoders/javascript/base64-test.html +209 -0
  68. data/vendor/stringencoders/javascript/base64.html +18 -0
  69. data/vendor/stringencoders/javascript/base64.js +176 -0
  70. data/vendor/stringencoders/javascript/qunit.css +119 -0
  71. data/vendor/stringencoders/javascript/qunit.js +1062 -0
  72. data/vendor/stringencoders/javascript/urlparse-test.html +367 -0
  73. data/vendor/stringencoders/javascript/urlparse.js +328 -0
  74. data/vendor/stringencoders/make-ci.sh +13 -0
  75. data/vendor/stringencoders/makerelease.sh +16 -0
  76. data/vendor/stringencoders/python/b85.py +176 -0
  77. data/vendor/stringencoders/src/Makefile.am +134 -0
  78. data/vendor/stringencoders/src/arraytoc.c +85 -0
  79. data/vendor/stringencoders/src/arraytoc.h +43 -0
  80. data/vendor/stringencoders/src/extern_c_begin.h +3 -0
  81. data/vendor/stringencoders/src/extern_c_end.h +3 -0
  82. data/vendor/stringencoders/src/html_named_entities_generator.py +203 -0
  83. data/vendor/stringencoders/src/modp_ascii.c +159 -0
  84. data/vendor/stringencoders/src/modp_ascii.h +162 -0
  85. data/vendor/stringencoders/src/modp_ascii_data.h +84 -0
  86. data/vendor/stringencoders/src/modp_ascii_gen.c +55 -0
  87. data/vendor/stringencoders/src/modp_b16.c +125 -0
  88. data/vendor/stringencoders/src/modp_b16.h +148 -0
  89. data/vendor/stringencoders/src/modp_b16_data.h +104 -0
  90. data/vendor/stringencoders/src/modp_b16_gen.c +65 -0
  91. data/vendor/stringencoders/src/modp_b2.c +69 -0
  92. data/vendor/stringencoders/src/modp_b2.h +130 -0
  93. data/vendor/stringencoders/src/modp_b2_data.h +44 -0
  94. data/vendor/stringencoders/src/modp_b2_gen.c +36 -0
  95. data/vendor/stringencoders/src/modp_b36.c +108 -0
  96. data/vendor/stringencoders/src/modp_b36.h +170 -0
  97. data/vendor/stringencoders/src/modp_b64.c +254 -0
  98. data/vendor/stringencoders/src/modp_b64.h +236 -0
  99. data/vendor/stringencoders/src/modp_b64_data.h +477 -0
  100. data/vendor/stringencoders/src/modp_b64_gen.c +168 -0
  101. data/vendor/stringencoders/src/modp_b64r.c +254 -0
  102. data/vendor/stringencoders/src/modp_b64r.h +242 -0
  103. data/vendor/stringencoders/src/modp_b64r_data.h +477 -0
  104. data/vendor/stringencoders/src/modp_b64w.c +254 -0
  105. data/vendor/stringencoders/src/modp_b64w.h +231 -0
  106. data/vendor/stringencoders/src/modp_b64w_data.h +477 -0
  107. data/vendor/stringencoders/src/modp_b85.c +109 -0
  108. data/vendor/stringencoders/src/modp_b85.h +171 -0
  109. data/vendor/stringencoders/src/modp_b85_data.h +36 -0
  110. data/vendor/stringencoders/src/modp_b85_gen.c +65 -0
  111. data/vendor/stringencoders/src/modp_bjavascript.c +65 -0
  112. data/vendor/stringencoders/src/modp_bjavascript.h +105 -0
  113. data/vendor/stringencoders/src/modp_bjavascript_data.h +84 -0
  114. data/vendor/stringencoders/src/modp_bjavascript_gen.c +58 -0
  115. data/vendor/stringencoders/src/modp_burl.c +228 -0
  116. data/vendor/stringencoders/src/modp_burl.h +259 -0
  117. data/vendor/stringencoders/src/modp_burl_data.h +136 -0
  118. data/vendor/stringencoders/src/modp_burl_gen.c +121 -0
  119. data/vendor/stringencoders/src/modp_html.c +128 -0
  120. data/vendor/stringencoders/src/modp_html.h +53 -0
  121. data/vendor/stringencoders/src/modp_html_named_entities.h +9910 -0
  122. data/vendor/stringencoders/src/modp_json.c +315 -0
  123. data/vendor/stringencoders/src/modp_json.h +103 -0
  124. data/vendor/stringencoders/src/modp_json_data.h +57 -0
  125. data/vendor/stringencoders/src/modp_json_gen.py +60 -0
  126. data/vendor/stringencoders/src/modp_mainpage.h +120 -0
  127. data/vendor/stringencoders/src/modp_numtoa.c +350 -0
  128. data/vendor/stringencoders/src/modp_numtoa.h +100 -0
  129. data/vendor/stringencoders/src/modp_qsiter.c +76 -0
  130. data/vendor/stringencoders/src/modp_qsiter.h +71 -0
  131. data/vendor/stringencoders/src/modp_stdint.h +43 -0
  132. data/vendor/stringencoders/src/modp_utf8.c +88 -0
  133. data/vendor/stringencoders/src/modp_utf8.h +38 -0
  134. data/vendor/stringencoders/src/modp_xml.c +311 -0
  135. data/vendor/stringencoders/src/modp_xml.h +166 -0
  136. data/vendor/stringencoders/src/stringencoders.pc +10 -0
  137. data/vendor/stringencoders/src/stringencoders.pc.in +10 -0
  138. data/vendor/stringencoders/test/Makefile.am +113 -0
  139. data/vendor/stringencoders/test/apr_base64.c +262 -0
  140. data/vendor/stringencoders/test/apr_base64.h +120 -0
  141. data/vendor/stringencoders/test/cxx_test.cc +482 -0
  142. data/vendor/stringencoders/test/minunit.h +82 -0
  143. data/vendor/stringencoders/test/modp_ascii_test.c +281 -0
  144. data/vendor/stringencoders/test/modp_b16_test.c +288 -0
  145. data/vendor/stringencoders/test/modp_b2_test.c +250 -0
  146. data/vendor/stringencoders/test/modp_b64_test.c +266 -0
  147. data/vendor/stringencoders/test/modp_b85_test.c +130 -0
  148. data/vendor/stringencoders/test/modp_bjavascript_test.c +137 -0
  149. data/vendor/stringencoders/test/modp_burl_test.c +423 -0
  150. data/vendor/stringencoders/test/modp_html_test.c +296 -0
  151. data/vendor/stringencoders/test/modp_json_test.c +336 -0
  152. data/vendor/stringencoders/test/modp_numtoa_test.c +545 -0
  153. data/vendor/stringencoders/test/modp_qsiter_test.c +280 -0
  154. data/vendor/stringencoders/test/modp_utf8_test.c +188 -0
  155. data/vendor/stringencoders/test/modp_xml_test.c +339 -0
  156. data/vendor/stringencoders/test/speedtest.c +241 -0
  157. data/vendor/stringencoders/test/speedtest_ascii.c +345 -0
  158. data/vendor/stringencoders/test/speedtest_msg.c +78 -0
  159. data/vendor/stringencoders/test/speedtest_numtoa.c +276 -0
  160. metadata +314 -0
@@ -0,0 +1,280 @@
1
+ /* we compile as C90 but use snprintf */
2
+ #define _ISOC99_SOURCE
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+
8
+ #include "minunit.h"
9
+
10
+ #include "modp_qsiter.h"
11
+
12
+ /**
13
+ * Test empty input to encode and decode
14
+ */
15
+ static char* test_qs_init()
16
+ {
17
+ struct qsiter_t qsi;
18
+
19
+ const char* s = "foobar";
20
+
21
+ qsiter_reset(&qsi, s, strlen(s));
22
+
23
+ mu_assert_int_equals(qsi.pos, 0);
24
+ mu_assert_int_equals(qsi.len, strlen(s));
25
+ mu_assert_int_equals(qsi.keylen, 0);
26
+ mu_assert_int_equals(qsi.vallen, 0);
27
+
28
+ return 0;
29
+ }
30
+
31
+ static char* test_qs_parse1()
32
+ {
33
+ struct qsiter_t qsi;
34
+
35
+ const char* s = "foo=bar1";
36
+
37
+ qsiter_reset(&qsi, s, strlen(s));
38
+
39
+ int ok = qsiter_next(&qsi);
40
+ mu_assert(ok);
41
+ mu_assert_int_equals(qsi.pos, strlen(s));
42
+ mu_assert_int_equals(qsi.len, strlen(s));
43
+ mu_assert_int_equals(qsi.keylen, 3);
44
+ mu_assert_int_equals(qsi.vallen, 4);
45
+ mu_assert(!memcmp("foo", qsi.key, qsi.keylen));
46
+ mu_assert(!memcmp("bar1", qsi.val, qsi.vallen));
47
+
48
+ ok = qsiter_next(&qsi);
49
+ mu_assert(!ok);
50
+
51
+ return 0;
52
+ }
53
+
54
+ static char* test_qs_parse2()
55
+ {
56
+ struct qsiter_t qsi;
57
+
58
+ const char* s = "foobar";
59
+
60
+ qsiter_reset(&qsi, s, strlen(s));
61
+
62
+ int ok = qsiter_next(&qsi);
63
+ mu_assert(ok);
64
+ mu_assert_int_equals(qsi.pos, strlen(s));
65
+ mu_assert_int_equals(qsi.len, strlen(s));
66
+ mu_assert_int_equals(qsi.keylen, 6);
67
+ mu_assert_int_equals(qsi.vallen, 0);
68
+ mu_assert(!memcmp("foobar", qsi.key, qsi.keylen));
69
+ mu_assert(!memcmp("", qsi.val, qsi.vallen));
70
+
71
+ ok = qsiter_next(&qsi);
72
+ mu_assert(!ok);
73
+
74
+ return 0;
75
+ }
76
+
77
+ static char* test_qs_parse3()
78
+ {
79
+ struct qsiter_t qsi;
80
+
81
+ const char* s = "foo=bar&ding=bat";
82
+
83
+ qsiter_reset(&qsi, s, strlen(s));
84
+
85
+ int ok = qsiter_next(&qsi);
86
+ mu_assert(ok);
87
+ mu_assert_int_equals(qsi.pos, 8);
88
+ mu_assert_int_equals(qsi.len, strlen(s));
89
+ mu_assert_int_equals(qsi.keylen, 3);
90
+ mu_assert_int_equals(qsi.vallen, 3);
91
+ mu_assert(!memcmp("foo", qsi.key, qsi.keylen));
92
+ mu_assert(!memcmp("bar", qsi.val, qsi.vallen));
93
+
94
+ ok = qsiter_next(&qsi);
95
+ mu_assert(ok);
96
+ mu_assert_int_equals(qsi.pos, strlen(s));
97
+ mu_assert_int_equals(qsi.len, strlen(s));
98
+ mu_assert_int_equals(qsi.keylen, 4);
99
+ mu_assert_int_equals(qsi.vallen, 3);
100
+ mu_assert(!memcmp("ding", qsi.key, qsi.keylen));
101
+ mu_assert(!memcmp("bat", qsi.val, qsi.vallen));
102
+
103
+ ok = qsiter_next(&qsi);
104
+ mu_assert(!ok);
105
+
106
+ return 0;
107
+ }
108
+
109
+ static char* test_qs_parse4()
110
+ {
111
+ struct qsiter_t qsi;
112
+
113
+ const char* s = "";
114
+
115
+ qsiter_reset(&qsi, s, strlen(s));
116
+
117
+ int ok = qsiter_next(&qsi);
118
+ mu_assert(!ok);
119
+
120
+ return 0;
121
+ }
122
+
123
+ static char* test_qs_parse5()
124
+ {
125
+ struct qsiter_t qsi;
126
+
127
+ const char* s = "=";
128
+
129
+ qsiter_reset(&qsi, s, strlen(s));
130
+
131
+ int ok = qsiter_next(&qsi);
132
+ mu_assert(ok);
133
+ mu_assert_int_equals(qsi.pos, strlen(s));
134
+ mu_assert_int_equals(qsi.len, strlen(s));
135
+ mu_assert_int_equals(qsi.keylen, 0);
136
+ mu_assert_int_equals(qsi.vallen, 0);
137
+ mu_assert(!memcmp("", qsi.key, qsi.keylen));
138
+ mu_assert(!memcmp("", qsi.val, qsi.vallen));
139
+
140
+ ok = qsiter_next(&qsi);
141
+ mu_assert(!ok);
142
+
143
+ return 0;
144
+ }
145
+
146
+ static char* test_qs_parse6()
147
+ {
148
+ struct qsiter_t qsi;
149
+
150
+ const char* s = "&";
151
+
152
+ qsiter_reset(&qsi, s, strlen(s));
153
+
154
+ int ok = qsiter_next(&qsi);
155
+ mu_assert(ok);
156
+ mu_assert_int_equals(qsi.pos, strlen(s));
157
+ mu_assert_int_equals(qsi.len, strlen(s));
158
+ mu_assert_int_equals(qsi.keylen, 0);
159
+ mu_assert_int_equals(qsi.vallen, 0);
160
+ mu_assert(!memcmp("", qsi.key, qsi.keylen));
161
+ mu_assert(!memcmp("", qsi.val, qsi.vallen));
162
+
163
+ ok = qsiter_next(&qsi);
164
+ mu_assert(!ok);
165
+
166
+ return 0;
167
+ }
168
+
169
+ static char* test_qs_parse7()
170
+ {
171
+ struct qsiter_t qsi;
172
+
173
+ const char* s = "=&";
174
+
175
+ qsiter_reset(&qsi, s, strlen(s));
176
+
177
+ int ok = qsiter_next(&qsi);
178
+ mu_assert(ok);
179
+ mu_assert_int_equals(qsi.pos, strlen(s));
180
+ mu_assert_int_equals(qsi.len, strlen(s));
181
+ mu_assert_int_equals(qsi.keylen, 0);
182
+ mu_assert_int_equals(qsi.vallen, 0);
183
+ mu_assert(!memcmp("", qsi.key, qsi.keylen));
184
+ mu_assert(!memcmp("", qsi.val, qsi.vallen));
185
+
186
+ ok = qsiter_next(&qsi);
187
+ mu_assert(!ok);
188
+
189
+ return 0;
190
+ }
191
+
192
+ static char* test_qs_parse8()
193
+ {
194
+ struct qsiter_t qsi;
195
+
196
+ const char* s = "&&";
197
+
198
+ qsiter_reset(&qsi, s, strlen(s));
199
+
200
+ int ok = qsiter_next(&qsi);
201
+ mu_assert(ok);
202
+ mu_assert_int_equals(qsi.pos, 1);
203
+ mu_assert_int_equals(qsi.len, strlen(s));
204
+ mu_assert_int_equals(qsi.keylen, 0);
205
+ mu_assert_int_equals(qsi.vallen, 0);
206
+ mu_assert(!memcmp("", qsi.key, qsi.keylen));
207
+ mu_assert(!memcmp("", qsi.val, qsi.vallen));
208
+
209
+ ok = qsiter_next(&qsi);
210
+ mu_assert(ok);
211
+ mu_assert_int_equals(qsi.pos, 2);
212
+ mu_assert_int_equals(qsi.len, strlen(s));
213
+ mu_assert_int_equals(qsi.keylen, 0);
214
+ mu_assert_int_equals(qsi.vallen, 0);
215
+ mu_assert(!memcmp("", qsi.key, qsi.keylen));
216
+ mu_assert(!memcmp("", qsi.val, qsi.vallen));
217
+
218
+ ok = qsiter_next(&qsi);
219
+ mu_assert(!ok);
220
+
221
+ return 0;
222
+ }
223
+
224
+ static char* test_qs_parse9()
225
+ {
226
+ struct qsiter_t qsi;
227
+
228
+ const char* s = "&&foo=bar";
229
+
230
+ qsiter_reset(&qsi, s, strlen(s));
231
+
232
+ int ok = qsiter_next(&qsi);
233
+ mu_assert(ok);
234
+ mu_assert_int_equals(qsi.pos, 1);
235
+ mu_assert_int_equals(qsi.len, strlen(s));
236
+ mu_assert_int_equals(qsi.keylen, 0);
237
+ mu_assert_int_equals(qsi.vallen, 0);
238
+ mu_assert(!memcmp("", qsi.key, qsi.keylen));
239
+ mu_assert(!memcmp("", qsi.val, qsi.vallen));
240
+
241
+ ok = qsiter_next(&qsi);
242
+ mu_assert(ok);
243
+ mu_assert_int_equals(qsi.pos, 2);
244
+ mu_assert_int_equals(qsi.len, strlen(s));
245
+ mu_assert_int_equals(qsi.keylen, 0);
246
+ mu_assert_int_equals(qsi.vallen, 0);
247
+ mu_assert(!memcmp("", qsi.key, qsi.keylen));
248
+ mu_assert(!memcmp("", qsi.val, qsi.vallen));
249
+
250
+ ok = qsiter_next(&qsi);
251
+ mu_assert(ok);
252
+ mu_assert_int_equals(qsi.pos, strlen(s));
253
+ mu_assert_int_equals(qsi.len, strlen(s));
254
+ mu_assert_int_equals(qsi.keylen, 3);
255
+ mu_assert_int_equals(qsi.vallen, 3);
256
+ mu_assert(!memcmp("foo", qsi.key, qsi.keylen));
257
+ mu_assert(!memcmp("bar", qsi.val, qsi.vallen));
258
+
259
+ ok = qsiter_next(&qsi);
260
+ mu_assert(!ok);
261
+
262
+ return 0;
263
+ }
264
+
265
+ static char* all_tests()
266
+ {
267
+ mu_run_test(test_qs_init);
268
+ mu_run_test(test_qs_parse1);
269
+ mu_run_test(test_qs_parse2);
270
+ mu_run_test(test_qs_parse3);
271
+ mu_run_test(test_qs_parse4);
272
+ mu_run_test(test_qs_parse5);
273
+ mu_run_test(test_qs_parse6);
274
+ mu_run_test(test_qs_parse7);
275
+ mu_run_test(test_qs_parse8);
276
+ mu_run_test(test_qs_parse9);
277
+ return 0;
278
+ }
279
+
280
+ UNITTESTS
@@ -0,0 +1,188 @@
1
+ /* we compile as C90 but use snprintf */
2
+ #define _ISOC99_SOURCE
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+
8
+ #include "minunit.h"
9
+
10
+ #include "modp_utf8.h"
11
+ /**
12
+ * Test empty input to decode
13
+ */
14
+ static char* testUtf8ValidateEmpty(void)
15
+ {
16
+ int d = modp_utf8_validate("", (size_t)0);
17
+ mu_assert_int_equals(d, 0);
18
+ return 0;
19
+ }
20
+
21
+ static char* testUtf8ValidateAscii(void)
22
+ {
23
+ int d = modp_utf8_validate("abc", (size_t)3);
24
+ mu_assert_int_equals(d, 0);
25
+
26
+ /* with null and highest ASCII character 127 */
27
+ d = modp_utf8_validate("a\x00\x7Fz", (size_t)4);
28
+ mu_assert_int_equals(d, 0);
29
+ return 0;
30
+ }
31
+
32
+ static char* testUtf8Validate2(void)
33
+ {
34
+ /*
35
+ * examples taken from Wikipedia
36
+ * http://en.wikipedia.org/wiki/UTF-8
37
+ */
38
+ int d;
39
+ const char* s2 = "\xC2\xA2";
40
+ mu_assert_int_equals(strlen(s2), 2);
41
+ d = modp_utf8_validate(s2, strlen(s2));
42
+ mu_assert_int_equals(d, 0);
43
+
44
+ d = modp_utf8_validate(s2, strlen(s2) - 1);
45
+ mu_assert_int_equals(d, MODP_UTF8_SHORT);
46
+
47
+ /* overlong, decodes to 0 */
48
+ s2 = "\xC0\x80";
49
+ d = modp_utf8_validate(s2, strlen(s2));
50
+ mu_assert_int_equals(d, MODP_UTF8_OVERLONG);
51
+
52
+ /* overlong, decodes to 127 */
53
+ s2 = "\xC1\xBF";
54
+ d = modp_utf8_validate(s2, strlen(s2));
55
+ mu_assert_int_equals(d, MODP_UTF8_OVERLONG);
56
+
57
+ s2 = "\xC2\xC0";
58
+ d = modp_utf8_validate(s2, strlen(s2));
59
+ mu_assert_int_equals(d, MODP_UTF8_INVALID);
60
+
61
+ s2 = "\xC2\xFF";
62
+ d = modp_utf8_validate(s2, strlen(s2));
63
+ mu_assert_int_equals(d, MODP_UTF8_INVALID);
64
+
65
+ s2 = "\xC2\x01";
66
+ d = modp_utf8_validate(s2, strlen(s2));
67
+ mu_assert_int_equals(d, MODP_UTF8_INVALID);
68
+
69
+ return 0;
70
+ }
71
+
72
+ static char* testUtf8Validate3(void)
73
+ {
74
+ int d;
75
+ const char* s3 = "\xE2\x82\xAC";
76
+ mu_assert_int_equals(strlen(s3), 3);
77
+ d = modp_utf8_validate(s3, strlen(s3));
78
+ mu_assert_int_equals(d, 0);
79
+
80
+ d = modp_utf8_validate(s3, strlen(s3) - 1);
81
+ mu_assert_int_equals(d, MODP_UTF8_SHORT);
82
+
83
+ s3 = "\xEF\xBF\xBF";
84
+ d = modp_utf8_validate(s3, strlen(s3));
85
+ mu_assert_int_equals(d, MODP_UTF8_OK);
86
+
87
+ s3 = "\xE0\x80\x80";
88
+ d = modp_utf8_validate(s3, strlen(s3));
89
+ mu_assert_int_equals(d, MODP_UTF8_OVERLONG);
90
+
91
+ s3 = "\xE2\x01\xAC";
92
+ d = modp_utf8_validate(s3, strlen(s3));
93
+ mu_assert_int_equals(d, MODP_UTF8_INVALID);
94
+
95
+ s3 = "\xE2\x82\x01";
96
+ d = modp_utf8_validate(s3, strlen(s3));
97
+ mu_assert_int_equals(d, MODP_UTF8_INVALID);
98
+ return 0;
99
+ }
100
+
101
+ static char* testUtf8Validate4(void)
102
+ {
103
+ int d;
104
+ const char* s4 = "\xF0\xA4\xAD\xA2";
105
+ mu_assert_int_equals(strlen(s4), 4);
106
+ d = modp_utf8_validate(s4, strlen(s4));
107
+ mu_assert_int_equals(d, 0);
108
+
109
+ d = modp_utf8_validate(s4, strlen(s4) - 1);
110
+ mu_assert_int_equals(d, MODP_UTF8_SHORT);
111
+
112
+ /* maximum value */
113
+ s4 = "\xF7\xBF\xBF\xBF";
114
+ d = modp_utf8_validate(s4, strlen(s4));
115
+ mu_assert_int_equals(d, MODP_UTF8_OK);
116
+
117
+ s4 = "\xF0\x01\xAD\xA2";
118
+ d = modp_utf8_validate(s4, strlen(s4));
119
+ mu_assert_int_equals(d, MODP_UTF8_INVALID);
120
+
121
+ s4 = "\xF0\xA4\x01\xA2";
122
+ d = modp_utf8_validate(s4, strlen(s4));
123
+ mu_assert_int_equals(d, MODP_UTF8_INVALID);
124
+
125
+ s4 = "\xF0\xA4\xAD\x01";
126
+ d = modp_utf8_validate(s4, strlen(s4));
127
+ mu_assert_int_equals(d, MODP_UTF8_INVALID);
128
+
129
+ return 0;
130
+ }
131
+ static char* testUtf8Validate5(void)
132
+ {
133
+ int d;
134
+ const char* s = "\xF8";
135
+ mu_assert_int_equals(strlen(s), 1);
136
+ d = modp_utf8_validate(s, strlen(s));
137
+ mu_assert_int_equals(d, MODP_UTF8_CODEPOINT);
138
+
139
+ s = "\xF9";
140
+ mu_assert_int_equals(strlen(s), 1);
141
+ d = modp_utf8_validate(s, strlen(s));
142
+ mu_assert_int_equals(d, MODP_UTF8_CODEPOINT);
143
+
144
+ s = "\xFA";
145
+ mu_assert_int_equals(strlen(s), 1);
146
+ d = modp_utf8_validate(s, strlen(s));
147
+ mu_assert_int_equals(d, MODP_UTF8_CODEPOINT);
148
+
149
+ s = "\xFB";
150
+ mu_assert_int_equals(strlen(s), 1);
151
+ d = modp_utf8_validate(s, strlen(s));
152
+ mu_assert_int_equals(d, MODP_UTF8_CODEPOINT);
153
+
154
+ s = "\xFC";
155
+ mu_assert_int_equals(strlen(s), 1);
156
+ d = modp_utf8_validate(s, strlen(s));
157
+ mu_assert_int_equals(d, MODP_UTF8_CODEPOINT);
158
+
159
+ s = "\xFD";
160
+ mu_assert_int_equals(strlen(s), 1);
161
+ d = modp_utf8_validate(s, strlen(s));
162
+ mu_assert_int_equals(d, MODP_UTF8_CODEPOINT);
163
+
164
+ s = "\xFE";
165
+ mu_assert_int_equals(strlen(s), 1);
166
+ d = modp_utf8_validate(s, strlen(s));
167
+ mu_assert_int_equals(d, MODP_UTF8_CODEPOINT);
168
+
169
+ s = "\xFF";
170
+ mu_assert_int_equals(strlen(s), 1);
171
+ d = modp_utf8_validate(s, strlen(s));
172
+ mu_assert_int_equals(d, MODP_UTF8_CODEPOINT);
173
+
174
+ return 0;
175
+ }
176
+
177
+ static char* all_tests(void)
178
+ {
179
+ mu_run_test(testUtf8ValidateEmpty);
180
+ mu_run_test(testUtf8ValidateAscii);
181
+ mu_run_test(testUtf8Validate2);
182
+ mu_run_test(testUtf8Validate3);
183
+ mu_run_test(testUtf8Validate4);
184
+ mu_run_test(testUtf8Validate5);
185
+ return 0;
186
+ }
187
+
188
+ UNITTESTS