• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <limits.h>  // INT_MAX
2 #include <string.h>
3 #include <js_native_api.h>
4 #include "../common.h"
5 
TestLatin1(napi_env env,napi_callback_info info)6 static napi_value TestLatin1(napi_env env, napi_callback_info info) {
7   size_t argc = 1;
8   napi_value args[1];
9   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
10 
11   NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
12 
13   napi_valuetype valuetype;
14   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
15 
16   NAPI_ASSERT(env, valuetype == napi_string,
17       "Wrong type of argment. Expects a string.");
18 
19   char buffer[128];
20   size_t buffer_size = 128;
21   size_t copied;
22 
23   NAPI_CALL(env,
24       napi_get_value_string_latin1(env, args[0], buffer, buffer_size, &copied));
25 
26   napi_value output;
27   NAPI_CALL(env, napi_create_string_latin1(env, buffer, copied, &output));
28 
29   return output;
30 }
31 
TestUtf8(napi_env env,napi_callback_info info)32 static napi_value TestUtf8(napi_env env, napi_callback_info info) {
33   size_t argc = 1;
34   napi_value args[1];
35   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
36 
37   NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
38 
39   napi_valuetype valuetype;
40   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
41 
42   NAPI_ASSERT(env, valuetype == napi_string,
43       "Wrong type of argment. Expects a string.");
44 
45   char buffer[128];
46   size_t buffer_size = 128;
47   size_t copied;
48 
49   NAPI_CALL(env,
50       napi_get_value_string_utf8(env, args[0], buffer, buffer_size, &copied));
51 
52   napi_value output;
53   NAPI_CALL(env, napi_create_string_utf8(env, buffer, copied, &output));
54 
55   return output;
56 }
57 
TestUtf16(napi_env env,napi_callback_info info)58 static napi_value TestUtf16(napi_env env, napi_callback_info info) {
59   size_t argc = 1;
60   napi_value args[1];
61   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
62 
63   NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
64 
65   napi_valuetype valuetype;
66   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
67 
68   NAPI_ASSERT(env, valuetype == napi_string,
69       "Wrong type of argment. Expects a string.");
70 
71   char16_t buffer[128];
72   size_t buffer_size = 128;
73   size_t copied;
74 
75   NAPI_CALL(env,
76       napi_get_value_string_utf16(env, args[0], buffer, buffer_size, &copied));
77 
78   napi_value output;
79   NAPI_CALL(env, napi_create_string_utf16(env, buffer, copied, &output));
80 
81   return output;
82 }
83 
84 static napi_value
TestLatin1Insufficient(napi_env env,napi_callback_info info)85 TestLatin1Insufficient(napi_env env, napi_callback_info info) {
86   size_t argc = 1;
87   napi_value args[1];
88   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
89 
90   NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
91 
92   napi_valuetype valuetype;
93   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
94 
95   NAPI_ASSERT(env, valuetype == napi_string,
96       "Wrong type of argment. Expects a string.");
97 
98   char buffer[4];
99   size_t buffer_size = 4;
100   size_t copied;
101 
102   NAPI_CALL(env,
103       napi_get_value_string_latin1(env, args[0], buffer, buffer_size, &copied));
104 
105   napi_value output;
106   NAPI_CALL(env, napi_create_string_latin1(env, buffer, copied, &output));
107 
108   return output;
109 }
110 
TestUtf8Insufficient(napi_env env,napi_callback_info info)111 static napi_value TestUtf8Insufficient(napi_env env, napi_callback_info info) {
112   size_t argc = 1;
113   napi_value args[1];
114   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
115 
116   NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
117 
118   napi_valuetype valuetype;
119   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
120 
121   NAPI_ASSERT(env, valuetype == napi_string,
122       "Wrong type of argment. Expects a string.");
123 
124   char buffer[4];
125   size_t buffer_size = 4;
126   size_t copied;
127 
128   NAPI_CALL(env,
129       napi_get_value_string_utf8(env, args[0], buffer, buffer_size, &copied));
130 
131   napi_value output;
132   NAPI_CALL(env, napi_create_string_utf8(env, buffer, copied, &output));
133 
134   return output;
135 }
136 
TestUtf16Insufficient(napi_env env,napi_callback_info info)137 static napi_value TestUtf16Insufficient(napi_env env, napi_callback_info info) {
138   size_t argc = 1;
139   napi_value args[1];
140   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
141 
142   NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
143 
144   napi_valuetype valuetype;
145   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
146 
147   NAPI_ASSERT(env, valuetype == napi_string,
148       "Wrong type of argment. Expects a string.");
149 
150   char16_t buffer[4];
151   size_t buffer_size = 4;
152   size_t copied;
153 
154   NAPI_CALL(env,
155       napi_get_value_string_utf16(env, args[0], buffer, buffer_size, &copied));
156 
157   napi_value output;
158   NAPI_CALL(env, napi_create_string_utf16(env, buffer, copied, &output));
159 
160   return output;
161 }
162 
Utf16Length(napi_env env,napi_callback_info info)163 static napi_value Utf16Length(napi_env env, napi_callback_info info) {
164   size_t argc = 1;
165   napi_value args[1];
166   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
167 
168   NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
169 
170   napi_valuetype valuetype;
171   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
172 
173   NAPI_ASSERT(env, valuetype == napi_string,
174       "Wrong type of argment. Expects a string.");
175 
176   size_t length;
177   NAPI_CALL(env, napi_get_value_string_utf16(env, args[0], NULL, 0, &length));
178 
179   napi_value output;
180   NAPI_CALL(env, napi_create_uint32(env, (uint32_t)length, &output));
181 
182   return output;
183 }
184 
Utf8Length(napi_env env,napi_callback_info info)185 static napi_value Utf8Length(napi_env env, napi_callback_info info) {
186   size_t argc = 1;
187   napi_value args[1];
188   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
189 
190   NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
191 
192   napi_valuetype valuetype;
193   NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
194 
195   NAPI_ASSERT(env, valuetype == napi_string,
196       "Wrong type of argment. Expects a string.");
197 
198   size_t length;
199   NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], NULL, 0, &length));
200 
201   napi_value output;
202   NAPI_CALL(env, napi_create_uint32(env, (uint32_t)length, &output));
203 
204   return output;
205 }
206 
TestLargeUtf8(napi_env env,napi_callback_info info)207 static napi_value TestLargeUtf8(napi_env env, napi_callback_info info) {
208   napi_value output;
209   if (SIZE_MAX > INT_MAX) {
210     NAPI_CALL(env, napi_create_string_utf8(env, "", ((size_t)INT_MAX) + 1, &output));
211   } else {
212     // just throw the expected error as there is nothing to test
213     // in this case since we can't overflow
214     NAPI_CALL(env, napi_throw_error(env, NULL, "Invalid argument"));
215   }
216 
217   return output;
218 }
219 
TestLargeLatin1(napi_env env,napi_callback_info info)220 static napi_value TestLargeLatin1(napi_env env, napi_callback_info info) {
221   napi_value output;
222   if (SIZE_MAX > INT_MAX) {
223     NAPI_CALL(env, napi_create_string_latin1(env, "", ((size_t)INT_MAX) + 1, &output));
224   } else {
225     // just throw the expected error as there is nothing to test
226     // in this case since we can't overflow
227     NAPI_CALL(env, napi_throw_error(env, NULL, "Invalid argument"));
228   }
229 
230   return output;
231 }
232 
TestLargeUtf16(napi_env env,napi_callback_info info)233 static napi_value TestLargeUtf16(napi_env env, napi_callback_info info) {
234   napi_value output;
235   if (SIZE_MAX > INT_MAX) {
236     NAPI_CALL(env, napi_create_string_utf16(env,
237                                             ((const char16_t*)""),
238                                             ((size_t)INT_MAX) + 1, &output));
239   } else {
240     // just throw the expected error as there is nothing to test
241     // in this case since we can't overflow
242     NAPI_CALL(env, napi_throw_error(env, NULL, "Invalid argument"));
243   }
244 
245   return output;
246 }
247 
TestMemoryCorruption(napi_env env,napi_callback_info info)248 static napi_value TestMemoryCorruption(napi_env env, napi_callback_info info) {
249   size_t argc = 1;
250   napi_value args[1];
251   NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
252 
253   NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
254 
255   char buf[10] = { 0 };
256   NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], buf, 0, NULL));
257 
258   char zero[10] = { 0 };
259   if (memcmp(buf, zero, sizeof(buf)) != 0) {
260     NAPI_CALL(env, napi_throw_error(env, NULL, "Buffer overwritten"));
261   }
262 
263   return NULL;
264 }
265 
266 EXTERN_C_START
Init(napi_env env,napi_value exports)267 napi_value Init(napi_env env, napi_value exports) {
268   napi_property_descriptor properties[] = {
269     DECLARE_NAPI_PROPERTY("TestLatin1", TestLatin1),
270     DECLARE_NAPI_PROPERTY("TestLatin1Insufficient", TestLatin1Insufficient),
271     DECLARE_NAPI_PROPERTY("TestUtf8", TestUtf8),
272     DECLARE_NAPI_PROPERTY("TestUtf8Insufficient", TestUtf8Insufficient),
273     DECLARE_NAPI_PROPERTY("TestUtf16", TestUtf16),
274     DECLARE_NAPI_PROPERTY("TestUtf16Insufficient", TestUtf16Insufficient),
275     DECLARE_NAPI_PROPERTY("Utf16Length", Utf16Length),
276     DECLARE_NAPI_PROPERTY("Utf8Length", Utf8Length),
277     DECLARE_NAPI_PROPERTY("TestLargeUtf8", TestLargeUtf8),
278     DECLARE_NAPI_PROPERTY("TestLargeLatin1", TestLargeLatin1),
279     DECLARE_NAPI_PROPERTY("TestLargeUtf16", TestLargeUtf16),
280     DECLARE_NAPI_PROPERTY("TestMemoryCorruption", TestMemoryCorruption),
281   };
282 
283   NAPI_CALL(env, napi_define_properties(
284       env, exports, sizeof(properties) / sizeof(*properties), properties));
285 
286   return exports;
287 }
288 EXTERN_C_END
289