1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <algorithm>
17 #include <iostream>
18 #include <vector>
19
20 #include "commonlibrary/ets_utils/js_api_module/buffer/js_blob.h"
21 #include "commonlibrary/ets_utils/js_api_module/buffer/js_buffer.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24
25 using namespace std;
26
27 extern const char _binary_js_buffer_js_start[];
28 extern const char _binary_js_buffer_js_end[];
29 extern const char _binary_buffer_abc_start[];
30 extern const char _binary_buffer_abc_end[];
31
32 namespace OHOS::buffer {
33 enum class ParaType:int32_t {
34 NUMBER = 0,
35 BUFFER,
36 UINT8ARRAY,
37 ARRAYBUFFER,
38 NUMBERS,
39 STRING
40 };
FinalizeBufferCallback(napi_env env,void * finalizeData,void * finalizeHint)41 void FinalizeBufferCallback(napi_env env, void *finalizeData, void *finalizeHint)
42 {
43 if (finalizeData != nullptr) {
44 auto obj = reinterpret_cast<Buffer *>(finalizeData);
45 delete obj;
46 }
47 }
48
FinalizeBlobCallback(napi_env env,void * finalizeData,void * finalizeHint)49 void FinalizeBlobCallback(napi_env env, void *finalizeData, void *finalizeHint)
50 {
51 if (finalizeData != nullptr) {
52 auto obj = reinterpret_cast<Blob *>(finalizeData);
53 delete obj;
54 }
55 }
56
GetStringUtf8(napi_env env,napi_value strValue)57 static string GetStringUtf8(napi_env env, napi_value strValue)
58 {
59 string str = "";
60 size_t strSize = 0;
61 NAPI_CALL(env, napi_get_value_string_utf8(env, strValue, nullptr, 0, &strSize));
62 str.reserve(strSize + 1);
63 str.resize(strSize);
64 NAPI_CALL(env, napi_get_value_string_utf8(env, strValue, const_cast<char *>(str.data()), strSize + 1, &strSize));
65 int pos = count(str.begin(), str.end(), '\0');
66 if (pos != 0) {
67 str.resize(strSize);
68 }
69 return str;
70 }
71
GetStringASCII(napi_env env,napi_value strValue)72 static string GetStringASCII(napi_env env, napi_value strValue)
73 {
74 string str = "";
75 size_t strSize = 0;
76 NAPI_CALL(env, napi_get_value_string_latin1(env, strValue, nullptr, 0, &strSize));
77 str.reserve(strSize + 1);
78 str.resize(strSize);
79 NAPI_CALL(env, napi_get_value_string_latin1(env, strValue, const_cast<char *>(str.data()), strSize + 1, &strSize));
80 return str;
81 }
82
GetString(napi_env env,EncodingType encodingType,napi_value strValue)83 static string GetString(napi_env env, EncodingType encodingType, napi_value strValue)
84 {
85 if (encodingType == BASE64 || encodingType == BASE64URL) {
86 return GetStringASCII(env, strValue);
87 } else {
88 return GetStringUtf8(env, strValue);
89 }
90 }
91
FromStringUtf8(napi_env env,napi_value thisVar,napi_value str)92 static napi_value FromStringUtf8(napi_env env, napi_value thisVar, napi_value str)
93 {
94 string utf8Str = GetStringUtf8(env, str);
95 Buffer *buffer = nullptr;
96 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buffer)));
97 buffer->WriteString(utf8Str, utf8Str.length());
98
99 return thisVar;
100 }
101
FromStringASCII(napi_env env,napi_value thisVar,napi_value str,uint32_t size)102 static napi_value FromStringASCII(napi_env env, napi_value thisVar, napi_value str, uint32_t size)
103 {
104 string asciiStr = GetStringASCII(env, str);
105 Buffer *buffer = nullptr;
106 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buffer)));
107
108 buffer->WriteString(asciiStr, size);
109 return thisVar;
110 }
111
GetStringUtf16LE(napi_env env,napi_value strValue)112 static std::u16string GetStringUtf16LE(napi_env env, napi_value strValue)
113 {
114 string utf8Str = GetStringUtf8(env, strValue);
115 u16string u16Str = Utf8ToUtf16BE(utf8Str);
116 return Utf16BEToLE(u16Str);
117 }
118
FromStringUtf16LE(napi_env env,napi_value thisVar,napi_value str)119 static napi_value FromStringUtf16LE(napi_env env, napi_value thisVar, napi_value str)
120 {
121 string utf8Str = GetStringUtf8(env, str);
122 Buffer *buffer = nullptr;
123 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buffer)));
124 u16string u16Str = Utf8ToUtf16BE(utf8Str);
125 // 2 : the size of string is 2 times of u16str's length
126 buffer->WriteString(u16Str, 0, u16Str.size() * 2);
127
128 return thisVar;
129 }
130
GetStringBase64(napi_env env,napi_value str,EncodingType type)131 static std::string GetStringBase64(napi_env env, napi_value str, EncodingType type)
132 {
133 string base64Str = GetStringASCII(env, str);
134 string strDecoded = Base64Decode(base64Str, type);
135 return strDecoded;
136 }
137
FromStringBase64(napi_env env,napi_value thisVar,napi_value str,uint32_t size,EncodingType type)138 static napi_value FromStringBase64(napi_env env, napi_value thisVar, napi_value str, uint32_t size, EncodingType type)
139 {
140 string strDecoded = GetStringBase64(env, str, type);
141 Buffer *buffer = nullptr;
142 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buffer)));
143
144 buffer->WriteString(strDecoded, size);
145 return thisVar;
146 }
147
GetStringHex(napi_env env,napi_value str)148 static std::string GetStringHex(napi_env env, napi_value str)
149 {
150 string hexStr = GetStringASCII(env, str);
151 string strDecoded = HexDecode(hexStr);
152 return strDecoded;
153 }
154
FromStringHex(napi_env env,napi_value thisVar,napi_value str)155 static napi_value FromStringHex(napi_env env, napi_value thisVar, napi_value str)
156 {
157 string hexStr = GetStringASCII(env, str);
158 Buffer *buffer = nullptr;
159 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&buffer)));
160
161 string strDecoded = HexDecode(hexStr);
162 buffer->WriteString(strDecoded, strDecoded.length());
163 buffer->SetLength(strDecoded.length());
164
165 return thisVar;
166 }
167
FromString(napi_env env,napi_callback_info info)168 static napi_value FromString(napi_env env, napi_callback_info info)
169 {
170 napi_value thisVar = nullptr;
171 size_t argc = 3;
172 napi_value args[3] = { nullptr };
173 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
174 // 2 : the third argument
175 NAPI_ASSERT(env, argc > 2, "Wrong number of arguments");
176
177 uint32_t size = 0;
178 // 2 : the third argument
179 NAPI_CALL(env, napi_get_value_uint32(env, args[2], &size));
180
181 string type = GetStringASCII(env, args[1]);
182 EncodingType eType = Buffer::GetEncodingType(type);
183 switch (eType) {
184 case ASCII:
185 case LATIN1:
186 case BINARY:
187 return FromStringASCII(env, thisVar, args[0], size);
188 case UTF8:
189 return FromStringUtf8(env, thisVar, args[0]);
190 case UTF16LE:
191 return FromStringUtf16LE(env, thisVar, args[0]);
192 case BASE64:
193 case BASE64URL:
194 return FromStringBase64(env, thisVar, args[0], size, eType);
195 case HEX:
196 return FromStringHex(env, thisVar, args[0]);
197 default:
198 break;
199 }
200
201 napi_value result = nullptr;
202 NAPI_CALL(env, napi_get_undefined(env, &result));
203 return result;
204 }
205
GetArray(napi_env env,napi_value arr)206 static vector<uint8_t> GetArray(napi_env env, napi_value arr)
207 {
208 uint32_t length = 0;
209 napi_get_array_length(env, arr, &length);
210 napi_value napiNumber = nullptr;
211 vector<uint8_t> vec;
212 for (size_t i = 0; i < length; i++) {
213 napi_get_element(env, arr, i, &napiNumber);
214 uint32_t num = 0;
215 napi_get_value_uint32(env, napiNumber, &num);
216 // 255 : the max number of one byte unsigned value
217 num = num > 255 ? 0 : num;
218 vec.push_back(num);
219 }
220 return vec;
221 }
222
BlobConstructor(napi_env env,napi_callback_info info)223 static napi_value BlobConstructor(napi_env env, napi_callback_info info)
224 {
225 napi_value thisVar = nullptr;
226 Blob *blob = new Blob();
227 if (blob == nullptr) {
228 return nullptr;
229 }
230 size_t argc = 3;
231 napi_value argv[3] = { nullptr };
232 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
233
234 if (argc == 1) { // Array
235 vector<uint8_t> arr = GetArray(env, argv[0]);
236 blob->Init(arr.data(), arr.size());
237 } else { // Blob
238 Blob *blobIn = nullptr;
239 NAPI_CALL(env, napi_unwrap(env, argv[0], reinterpret_cast<void **>(&blobIn)));
240 int32_t start = -1;
241 NAPI_CALL(env, napi_get_value_int32(env, argv[1], &start));
242 // 2 : the argument's count is 2
243 if (argc == 2) {
244 blob->Init(blobIn, start);
245 } else if (argc == 3) { // 3 : the argument's count is 3
246 int32_t end = -1;
247 // 2 : the third argument
248 NAPI_CALL(env, napi_get_value_int32(env, argv[2], &end));
249 blob->Init(blobIn, start, end);
250 } else {
251 return nullptr;
252 }
253 }
254 napi_status status = napi_wrap(env, thisVar, blob, FinalizeBlobCallback, nullptr, nullptr);
255 if (status != napi_ok) {
256 if (blob != nullptr) {
257 delete blob;
258 }
259 HILOG_ERROR("can not create blob");
260 return nullptr;
261 }
262
263 return thisVar;
264 }
265
GetBufferWrapValue(napi_env env,napi_value thisVar,Buffer * buffer)266 static napi_value GetBufferWrapValue(napi_env env, napi_value thisVar, Buffer *buffer)
267 {
268 napi_status status = napi_wrap(env, thisVar, buffer, FinalizeBufferCallback, nullptr, nullptr);
269 if (status != napi_ok) {
270 if (buffer != nullptr) {
271 delete buffer;
272 }
273 HILOG_ERROR("can not wrap buffer");
274 return nullptr;
275 }
276 return thisVar;
277 }
278
BufferConstructor(napi_env env,napi_callback_info info)279 static napi_value BufferConstructor(napi_env env, napi_callback_info info)
280 {
281 napi_value thisVar = nullptr;
282 Buffer *buffer = new Buffer();
283 if (buffer == nullptr) {
284 return nullptr;
285 }
286 size_t argc = 4;
287 napi_value argv[4] = { nullptr };
288 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
289
290 int32_t pType = -1;
291 NAPI_CALL(env, napi_get_value_int32(env, argv[0], &pType));
292 ParaType paraType = static_cast<ParaType>(pType);
293 if (paraType == ParaType::NUMBER) {
294 uint32_t length = 0;
295 NAPI_CALL(env, napi_get_value_uint32(env, argv[1], &length));
296 buffer->Init(length);
297 } else if (paraType == ParaType::NUMBERS) {
298 vector<uint8_t> arr = GetArray(env, argv[1]);
299 buffer->Init(arr.size());
300 buffer->SetArray(arr);
301 } else if (paraType == ParaType::BUFFER) {
302 if (argc == 2) { // the count of argument is 2
303 Buffer *buf = nullptr;
304 NAPI_CALL(env, napi_unwrap(env, argv[1], reinterpret_cast<void **>(&buf)));
305 buffer->Init(buf);
306 } else if (argc == 4) { // the count of argument is 4
307 Buffer *pool = nullptr;
308 NAPI_CALL(env, napi_unwrap(env, argv[1], reinterpret_cast<void **>(&pool)));
309 uint32_t poolOffset = 0;
310 // 2 : the third argument
311 NAPI_CALL(env, napi_get_value_uint32(env, argv[2], &poolOffset));
312 uint32_t length = 0;
313 // 3 : the forth argument
314 NAPI_CALL(env, napi_get_value_uint32(env, argv[3], &length));
315 buffer->Init(pool, poolOffset, length);
316 } else {
317 return nullptr;
318 }
319 } else if (paraType == ParaType::STRING) {
320 return nullptr;
321 } else if (paraType == ParaType::UINT8ARRAY) {
322 napi_typedarray_type type = napi_int8_array;
323 size_t offset = 0;
324 size_t aryLen = 0;
325 void *resultData = nullptr;
326 napi_value resultBuffer = nullptr;
327 NAPI_CALL(env, napi_get_typedarray_info(env, argv[1], &type, &aryLen,
328 &resultData, &resultBuffer, &offset));
329 buffer->Init(reinterpret_cast<uint8_t *>(resultData) - offset, offset, aryLen);
330 } else if (paraType == ParaType::ARRAYBUFFER) {
331 void *data = nullptr;
332 size_t bufferSize = 0;
333 // 1 : the second argument
334 NAPI_CALL(env, napi_get_arraybuffer_info(env, argv[1], &data, &bufferSize));
335 uint32_t byteOffset = 0;
336 // 2 : the third argument
337 NAPI_CALL(env, napi_get_value_uint32(env, argv[2], &byteOffset));
338 uint32_t length = 0;
339 // 3 : the forth argument
340 NAPI_CALL(env, napi_get_value_uint32(env, argv[3], &length));
341 buffer->Init(reinterpret_cast<uint8_t*>(data), byteOffset, length);
342 } else {
343 napi_throw_error(env, nullptr, "parameter type is error");
344 return nullptr;
345 }
346
347 return GetBufferWrapValue(env, thisVar, buffer);
348 }
349
GetValueOffsetAndBuf(napi_env env,napi_callback_info info,int32_t * pValue,uint32_t * pOffset)350 Buffer *GetValueOffsetAndBuf(napi_env env, napi_callback_info info, int32_t *pValue, uint32_t *pOffset)
351 {
352 napi_value thisVar = nullptr;
353 size_t argc = 2;
354 napi_value args[2] = { nullptr };
355 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
356 NAPI_ASSERT(env, argc > 1, "Wrong number of arguments.");
357
358 Buffer *buf = nullptr;
359 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
360 NAPI_CALL(env, napi_get_value_int32(env, args[0], pValue));
361 NAPI_CALL(env, napi_get_value_uint32(env, args[1], pOffset));
362 return buf;
363 }
364
GetOffsetAndBuf(napi_env env,napi_callback_info info,uint32_t * pOffset)365 Buffer *GetOffsetAndBuf(napi_env env, napi_callback_info info, uint32_t *pOffset)
366 {
367 napi_value thisVar = nullptr;
368 size_t argc = 1;
369 napi_value args[1] = { nullptr };
370 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
371 NAPI_ASSERT(env, argc > 0, "Wrong number of arguments.");
372
373 Buffer *buf = nullptr;
374 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
375 NAPI_CALL(env, napi_get_value_uint32(env, args[0], pOffset));
376 return buf;
377 }
378
WriteInt32BE(napi_env env,napi_callback_info info)379 static napi_value WriteInt32BE(napi_env env, napi_callback_info info)
380 {
381 int32_t value = 0;
382 uint32_t offset = 0;
383 Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
384 if (buf != nullptr) {
385 buf->WriteInt32BE(value, offset);
386 }
387 napi_value result = nullptr;
388 NAPI_CALL(env, napi_get_undefined(env, &result));
389 return result;
390 }
391
ReadInt32BE(napi_env env,napi_callback_info info)392 static napi_value ReadInt32BE(napi_env env, napi_callback_info info)
393 {
394 uint32_t offset = 0;
395 Buffer *buf = GetOffsetAndBuf(env, info, &offset);
396 int32_t res = 0;
397 if (buf != nullptr) {
398 res = buf->ReadInt32BE(offset);
399 }
400 napi_value result = nullptr;
401 napi_create_int32(env, res, &result);
402 return result;
403 }
404
SetArray(napi_env env,napi_callback_info info)405 static napi_value SetArray(napi_env env, napi_callback_info info)
406 {
407 napi_value thisVar = nullptr;
408 size_t argc = 1;
409 napi_value args[1] = { nullptr };
410 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
411 NAPI_ASSERT(env, argc > 0, "Wrong number of arguments.");
412
413 bool isArray = false;
414 NAPI_CALL(env, napi_is_array(env, args[0], &isArray));
415 if (isArray) {
416 Buffer *buf = nullptr;
417 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
418 vector<uint8_t> arr = GetArray(env, args[0]);
419 buf->SetArray(arr);
420 }
421 napi_value result = nullptr;
422 NAPI_CALL(env, napi_get_undefined(env, &result));
423 return result;
424 }
425
GetLength(napi_env env,napi_callback_info info)426 static napi_value GetLength(napi_env env, napi_callback_info info)
427 {
428 napi_value thisVar = nullptr;
429 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
430 Buffer *buf = nullptr;
431 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
432 uint32_t res = buf->GetLength();
433 napi_value result = nullptr;
434 napi_create_uint32(env, res, &result);
435 return result;
436 }
437
GetByteOffset(napi_env env,napi_callback_info info)438 static napi_value GetByteOffset(napi_env env, napi_callback_info info)
439 {
440 napi_value thisVar = nullptr;
441 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
442 Buffer *buf = nullptr;
443 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
444 uint32_t res = buf->GetByteOffset();
445 napi_value result = nullptr;
446 napi_create_uint32(env, res, &result);
447 return result;
448 }
449
WriteString(napi_env env,napi_callback_info info)450 static napi_value WriteString(napi_env env, napi_callback_info info)
451 {
452 napi_value thisVar = nullptr;
453 size_t argc = 4;
454 napi_value args[4] = { nullptr };
455 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
456 // 4 : 4 arguments is right
457 NAPI_ASSERT(env, argc == 4, "Wrong number of arguments.");
458
459 // 3 : the forth argument
460 string encoding = GetStringASCII(env, args[3]);
461 EncodingType encodingType = Buffer::GetEncodingType(encoding);
462 string value = GetString(env, encodingType, args[0]);
463
464 uint32_t offset = 0;
465 uint32_t length = 0;
466 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
467 // 2 : the third argument
468 NAPI_CALL(env, napi_get_value_uint32(env, args[2], &length));
469
470 Buffer *buf = nullptr;
471 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
472 length = (value.length() < length) ? value.length() : length;
473 unsigned int lengthWrote = buf->WriteString(value, offset, length, encoding);
474
475 napi_value result = nullptr;
476 napi_create_uint32(env, lengthWrote, &result);
477 return result;
478 }
479
FillString(napi_env env,napi_callback_info info)480 static napi_value FillString(napi_env env, napi_callback_info info)
481 {
482 napi_value thisVar = nullptr;
483 size_t argc = 4;
484 napi_value args[4] = { nullptr };
485 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
486 NAPI_ASSERT(env, argc > 3, "Wrong number of arguments."); // 3:The number of parameters is 3
487
488 string encoding = GetStringASCII(env, args[3]);
489 EncodingType encodingType = Buffer::GetEncodingType(encoding);
490 string value = GetString(env, encodingType, args[0]);
491
492 uint32_t offset = 0;
493 uint32_t end = 0;
494 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
495 // 2 : the third argument
496 NAPI_CALL(env, napi_get_value_uint32(env, args[2], &end));
497
498 Buffer *buf = nullptr;
499 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
500 buf->FillString(value, offset, end, encoding);
501
502 napi_value result = nullptr;
503 NAPI_CALL(env, napi_get_undefined(env, &result));
504 return result;
505 }
506
FillNumbers(napi_env env,napi_callback_info info)507 static napi_value FillNumbers(napi_env env, napi_callback_info info)
508 {
509 napi_value thisVar = nullptr;
510 size_t argc = 3;
511 napi_value args[3] = { nullptr };
512 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
513 // 2 : the third argument
514 NAPI_ASSERT(env, argc > 2, "Wrong number of arguments.");
515
516 uint32_t offset = 0;
517 uint32_t end = 0;
518 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
519 // 2 : the third argument
520 NAPI_CALL(env, napi_get_value_uint32(env, args[2], &end));
521
522 Buffer *buf = nullptr;
523 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
524 vector<uint8_t> arr = GetArray(env, args[0]);
525 buf->FillNumber(arr, offset, end);
526
527 napi_value result = nullptr;
528 NAPI_CALL(env, napi_get_undefined(env, &result));
529 return result;
530 }
531
FillBuffer(napi_env env,napi_callback_info info)532 static napi_value FillBuffer(napi_env env, napi_callback_info info)
533 {
534 napi_value thisVar = nullptr;
535 size_t argc = 3;
536 napi_value args[3] = { nullptr };
537 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
538 // 2 : the third argument
539 NAPI_ASSERT(env, argc > 2, "Wrong number of arguments.");
540
541 uint32_t offset = 0;
542 uint32_t end = 0;
543 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
544 // 2 : the third argument
545 NAPI_CALL(env, napi_get_value_uint32(env, args[2], &end));
546
547 Buffer *buffer = nullptr;
548 NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&buffer)));
549 Buffer *ego = nullptr;
550 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&ego)));
551 ego->FillBuffer(buffer, offset, end);
552
553 napi_value result = nullptr;
554 NAPI_CALL(env, napi_get_undefined(env, &result));
555 return result;
556 }
557
Utf8ByteLength(napi_env env,napi_callback_info info)558 static napi_value Utf8ByteLength(napi_env env, napi_callback_info info)
559 {
560 napi_value thisVar = nullptr;
561 size_t argc = 1;
562 napi_value args[1] = { nullptr };
563 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
564 NAPI_ASSERT(env, argc > 0, "Wrong number of arguments.");
565 size_t byteLen = 0;
566 NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], nullptr, 0, &byteLen));
567 napi_value result = nullptr;
568 napi_create_uint32(env, byteLen, &result);
569 return result;
570 }
571
GetBufferData(napi_env env,napi_callback_info info)572 static napi_value GetBufferData(napi_env env, napi_callback_info info)
573 {
574 napi_value result = nullptr;
575 napi_value thisVar = nullptr;
576 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
577 Buffer *buf = nullptr;
578 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
579 uint32_t length = buf->GetLength();
580 uint8_t* data = new uint8_t[length];
581 if (data == nullptr) {
582 HILOG_ERROR("buffer:: data is NULL");
583 return result;
584 }
585 buf->ReadBytes(data, 0, length);
586 NAPI_CALL(env, napi_create_array(env, &result));
587 size_t key = 0;
588 napi_value value = nullptr;
589 for (uint32_t i = 0, len = length; i < len; i++) {
590 napi_create_uint32(env, data[i], &value);
591 napi_set_element(env, result, key, value);
592 key++;
593 }
594 delete[] data;
595 data = nullptr;
596 return result;
597 }
598
Get(napi_env env,napi_callback_info info)599 static napi_value Get(napi_env env, napi_callback_info info)
600 {
601 napi_value thisVar = nullptr;
602 size_t argc = 1;
603 napi_value args[1] = { nullptr };
604 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
605 NAPI_ASSERT(env, argc > 0, "Wrong number of arguments.");
606 uint32_t index = 0;
607 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &index));
608 Buffer *buf = nullptr;
609 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
610 int32_t value = buf->Get(index);
611 napi_value result = nullptr;
612 napi_create_int32(env, value, &result);
613 return result;
614 }
615
Set(napi_env env,napi_callback_info info)616 static napi_value Set(napi_env env, napi_callback_info info)
617 {
618 napi_value thisVar = nullptr;
619 size_t argc = 2;
620 napi_value args[2] = { nullptr };
621 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
622 NAPI_ASSERT(env, argc > 1, "Wrong number of arguments.");
623
624 Buffer *buf = nullptr;
625 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
626
627 uint32_t index = 0;
628 int32_t value = 0;
629 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &index));
630 NAPI_CALL(env, napi_get_value_int32(env, args[1], &value));
631 buf->Set(index, value);
632 napi_value result = nullptr;
633 NAPI_CALL(env, napi_get_undefined(env, &result));
634 return result;
635 }
636
WriteInt32LE(napi_env env,napi_callback_info info)637 static napi_value WriteInt32LE(napi_env env, napi_callback_info info)
638 {
639 int32_t value = 0;
640 uint32_t offset = 0;
641 Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
642 if (buf != nullptr) {
643 buf->WriteInt32LE(value, offset);
644 }
645 napi_value result = nullptr;
646 NAPI_CALL(env, napi_get_undefined(env, &result));
647 return result;
648 }
649
ReadInt32LE(napi_env env,napi_callback_info info)650 static napi_value ReadInt32LE(napi_env env, napi_callback_info info)
651 {
652 uint32_t offset = 0;
653 Buffer *buf = GetOffsetAndBuf(env, info, &offset);
654 int32_t res = 0;
655 if (buf != nullptr) {
656 res = buf->ReadInt32LE(offset);
657 }
658 napi_value result = nullptr;
659 napi_create_int32(env, res, &result);
660 return result;
661 }
662
WriteUInt32BE(napi_env env,napi_callback_info info)663 static napi_value WriteUInt32BE(napi_env env, napi_callback_info info)
664 {
665 int32_t value = 0;
666 uint32_t offset = 0;
667 Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
668 if (buf != nullptr) {
669 buf->WriteUInt32BE(value, offset);
670 }
671 napi_value result = nullptr;
672 NAPI_CALL(env, napi_get_undefined(env, &result));
673 return result;
674 }
675
ReadUInt32BE(napi_env env,napi_callback_info info)676 static napi_value ReadUInt32BE(napi_env env, napi_callback_info info)
677 {
678 uint32_t offset = 0;
679 Buffer *buf = GetOffsetAndBuf(env, info, &offset);
680 uint32_t res = 0;
681 if (buf != nullptr) {
682 res = buf->ReadUInt32BE(offset);
683 }
684 napi_value result = nullptr;
685 napi_create_uint32(env, res, &result);
686 return result;
687 }
688
WriteUInt32LE(napi_env env,napi_callback_info info)689 static napi_value WriteUInt32LE(napi_env env, napi_callback_info info)
690 {
691 int32_t value = 0;
692 uint32_t offset = 0;
693 Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
694 if (buf != nullptr) {
695 buf->WriteUInt32LE(value, offset);
696 }
697 napi_value result = nullptr;
698 NAPI_CALL(env, napi_get_undefined(env, &result));
699 return result;
700 }
701
ReadUInt32LE(napi_env env,napi_callback_info info)702 static napi_value ReadUInt32LE(napi_env env, napi_callback_info info)
703 {
704 uint32_t offset = 0;
705 Buffer *buf = GetOffsetAndBuf(env, info, &offset);
706 uint32_t res = 0;
707 if (buf != nullptr) {
708 res = buf->ReadUInt32LE(offset);
709 }
710 napi_value result = nullptr;
711 napi_create_uint32(env, res, &result);
712 return result;
713 }
714
SubBuffer(napi_env env,napi_callback_info info)715 static napi_value SubBuffer(napi_env env, napi_callback_info info)
716 {
717 napi_value thisVar = nullptr;
718 size_t argc = 3;
719 napi_value args[3] = { nullptr };
720 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
721 NAPI_ASSERT(env, argc == 3, "Wrong number of arguments");
722
723 Buffer *newBuf = nullptr;
724 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&newBuf)));
725 Buffer *targetBuf = nullptr;
726 NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&targetBuf)));
727
728 uint32_t start = 0;
729 uint32_t end = 0;
730 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &start));
731 NAPI_CALL(env, napi_get_value_uint32(env, args[2], &end));
732 newBuf->SubBuffer(targetBuf, start, end);
733 napi_value result = nullptr;
734 NAPI_CALL(env, napi_get_undefined(env, &result));
735 return result;
736 }
737
Copy(napi_env env,napi_callback_info info)738 static napi_value Copy(napi_env env, napi_callback_info info)
739 {
740 napi_value thisVar = nullptr;
741 size_t argc = 4;
742 napi_value args[4] = { nullptr };
743 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
744 // 4 : 4 arguments is right
745 NAPI_ASSERT(env, argc == 4, "Wrong number of arguments");
746
747 uint32_t targetStart = 0;
748 uint32_t sourceStart = 0;
749 uint32_t sourceEnd = 0;
750 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &targetStart));
751 // 2 : the third argument
752 NAPI_CALL(env, napi_get_value_uint32(env, args[2], &sourceStart));
753 // 3 : the forth argument
754 NAPI_CALL(env, napi_get_value_uint32(env, args[3], &sourceEnd));
755 Buffer *targetBuf = nullptr;
756 NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&targetBuf)));
757 Buffer *sBuf = nullptr;
758 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&sBuf)));
759 uint32_t cLength = sBuf->Copy(targetBuf, targetStart, sourceStart, sourceEnd);
760 napi_value result = nullptr;
761 napi_create_int32(env, cLength, &result);
762 return result;
763 }
764
Compare(napi_env env,napi_callback_info info)765 static napi_value Compare(napi_env env, napi_callback_info info)
766 {
767 napi_value thisVar = nullptr;
768 size_t argc = 4;
769 napi_value args[4] = { nullptr };
770 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
771 uint32_t targetStart = 0;
772 uint32_t sourceStart = 0;
773 uint32_t length = 0;
774 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &targetStart));
775 // 2 : the third argument
776 NAPI_CALL(env, napi_get_value_uint32(env, args[2], &sourceStart));
777 // 3 : the forth argument
778 NAPI_CALL(env, napi_get_value_uint32(env, args[3], &length));
779 Buffer *targetBuf = nullptr;
780 NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&targetBuf)));
781 Buffer *sBuf = nullptr;
782 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&sBuf)));
783 int res = sBuf->Compare(targetBuf, targetStart, sourceStart, length);
784 napi_value result = nullptr;
785 napi_create_int32(env, res, &result);
786 return result;
787 }
788
ToUtf8(napi_env env,napi_callback_info info)789 static napi_value ToUtf8(napi_env env, napi_callback_info info)
790 {
791 napi_value thisVar = nullptr;
792 napi_value result = nullptr;
793 size_t argc = 2;
794 napi_value args[2] = { nullptr };
795 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
796 uint32_t start = 0;
797 uint32_t end = 0;
798 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &start));
799 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &end));
800 Buffer *buf = nullptr;
801 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&buf)));
802 uint32_t length = end - start;
803 uint8_t* data = new uint8_t[length];
804 if (data == nullptr) {
805 HILOG_ERROR("buffer:: data is NULL");
806 return result;
807 }
808 buf->ReadBytes(data, start, length);
809 napi_create_string_utf8(env, reinterpret_cast<char *>(data), length, &result);
810 delete[] data;
811 data = nullptr;
812 return result;
813 }
814
ToBase64(napi_env env,napi_callback_info info)815 static napi_value ToBase64(napi_env env, napi_callback_info info)
816 {
817 napi_value thisVar = nullptr;
818 size_t argc = 2;
819 napi_value args[2] = { nullptr };
820 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
821 uint32_t start = 0;
822 uint32_t end = 0;
823 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &start));
824 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &end));
825 Buffer *buf = nullptr;
826 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&buf)));
827 uint32_t length = end - start;
828 std::string str = buf->ToBase64(start, length);
829 napi_value result = nullptr;
830 napi_create_string_latin1(env, str.c_str(), str.length(), &result);
831 return result;
832 }
833
ToBase64Url(napi_env env,napi_callback_info info)834 static napi_value ToBase64Url(napi_env env, napi_callback_info info)
835 {
836 napi_value thisVar = nullptr;
837 size_t argc = 2;
838 napi_value args[2] = { nullptr };
839 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
840 uint32_t start = 0;
841 uint32_t end = 0;
842 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &start));
843 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &end));
844 Buffer *buf = nullptr;
845 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&buf)));
846 uint32_t length = end - start;
847 std::string str = buf->ToBase64Url(start, length);
848 napi_value result = nullptr;
849 napi_create_string_latin1(env, str.c_str(), str.length(), &result);
850 return result;
851 }
852
IndexOf(napi_env env,napi_callback_info info)853 static napi_value IndexOf(napi_env env, napi_callback_info info)
854 {
855 napi_value thisVar = nullptr;
856 size_t argc = 4;
857 napi_value args[4] = { nullptr };
858 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
859 uint32_t offset = 0;
860 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
861
862 // 2 : the third argument
863 string type = GetStringASCII(env, args[2]);
864 EncodingType eType = Buffer::GetEncodingType(type);
865 std::string str;
866 std::u16string u16Str;
867 uint32_t len = 0;
868 const char *data = nullptr;
869 switch (eType) {
870 case ASCII:
871 case LATIN1:
872 case BINARY:
873 str = GetStringASCII(env, args[0]);
874 data = str.c_str();
875 break;
876 case UTF8:
877 str = GetStringUtf8(env, args[0]);
878 data = str.c_str();
879 break;
880 case UTF16LE: {
881 u16Str = GetStringUtf16LE(env, args[0]);
882 data = reinterpret_cast<char *>(const_cast<char16_t *>(u16Str.c_str()));
883 len = u16Str.length() * 2; // 2 : 2 means the length of wide char String is 2 times of char String
884 break;
885 }
886 case BASE64:
887 case BASE64URL:
888 str = GetStringBase64(env, args[0], eType);
889 data = str.c_str();
890 break;
891 case HEX:
892 str = GetStringHex(env, args[0]);
893 data = str.c_str();
894 break;
895 default:
896 break;
897 }
898
899 Buffer *buf = nullptr;
900 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
901 bool isReverse = false;
902 // 3 : the forth argument
903 NAPI_CALL(env, napi_get_value_bool(env, args[3], &isReverse));
904 int index = -1;
905 if (isReverse) {
906 len = (eType == UTF16LE) ? len : str.length();
907 index = buf->LastIndexOf(data, offset, len);
908 } else {
909 len = (eType == UTF16LE) ? len : str.length();
910 index = buf->IndexOf(data, offset, len);
911 }
912 napi_value result = nullptr;
913 napi_create_int32(env, index, &result);
914 return result;
915 }
916
Utf8StringToNumbers(napi_env env,napi_callback_info info)917 static napi_value Utf8StringToNumbers(napi_env env, napi_callback_info info)
918 {
919 napi_value thisVar = nullptr;
920 size_t argc = 1;
921 napi_value args[1] = { nullptr };
922 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
923
924 std::string str = GetStringUtf8(env, args[0]);
925 napi_value result = nullptr;
926 NAPI_CALL(env, napi_create_array(env, &result));
927 size_t key = 0;
928 napi_value value = nullptr;
929 for (uint32_t i = 0; i < str.length(); i++) {
930 napi_create_uint32(env, uint32_t(str.at(i) & 0xFF), &value);
931 napi_set_element(env, result, key, value);
932 key++;
933 }
934 return result;
935 }
936
937 struct PromiseInfo {
938 napi_env env = nullptr;
939 napi_async_work worker = nullptr;
940 napi_deferred deferred = nullptr;
941 napi_ref blobDataRef = nullptr;
942 };
943
CopiedBlobToString(napi_env env,napi_status status,void * data)944 static void CopiedBlobToString(napi_env env, napi_status status, void *data)
945 {
946 auto promiseInfo = reinterpret_cast<PromiseInfo *>(data);
947 napi_value string = nullptr;
948 napi_get_reference_value(env, promiseInfo->blobDataRef, &string);
949 napi_resolve_deferred(env, promiseInfo->deferred, string);
950 napi_delete_reference(env, promiseInfo->blobDataRef);
951 napi_delete_async_work(env, promiseInfo->worker);
952 delete promiseInfo;
953 }
954
CopiedBlobToArrayBuffer(napi_env env,napi_status status,void * data)955 static void CopiedBlobToArrayBuffer(napi_env env, napi_status status, void *data)
956 {
957 auto promiseInfo = reinterpret_cast<PromiseInfo *>(data);
958 napi_value arrayBuffer = nullptr;
959 napi_get_reference_value(env, promiseInfo->blobDataRef, &arrayBuffer);
960 napi_resolve_deferred(env, promiseInfo->deferred, arrayBuffer);
961 napi_delete_reference(env, promiseInfo->blobDataRef);
962 napi_delete_async_work(env, promiseInfo->worker);
963 delete promiseInfo;
964 }
965
ArrayBufferAsync(napi_env env,napi_callback_info info)966 static napi_value ArrayBufferAsync(napi_env env, napi_callback_info info)
967 {
968 napi_value thisVar = nullptr;
969 PromiseInfo *promiseInfo = new PromiseInfo();
970 napi_value resourceName = nullptr;
971 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
972 Blob *blob = nullptr;
973 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&blob)));
974 size_t bufferSize = blob->GetLength();
975 void *bufdata = nullptr;
976 napi_value arrayBuffer = nullptr;
977 napi_value bufferPromise = nullptr;
978 napi_create_arraybuffer(env, bufferSize, &bufdata, &arrayBuffer);
979 blob->ReadBytes(reinterpret_cast<uint8_t *>(bufdata), bufferSize);
980 napi_create_reference(env, arrayBuffer, 1, &promiseInfo->blobDataRef);
981 napi_create_promise(env, &promiseInfo->deferred, &bufferPromise);
982 napi_create_string_utf8(env, "CopyBlobToArrayBuffer", NAPI_AUTO_LENGTH, &resourceName);
983 napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void* data) {}, CopiedBlobToArrayBuffer,
984 reinterpret_cast<void *>(promiseInfo), &promiseInfo->worker);
985 napi_queue_async_work_with_qos(env, promiseInfo->worker, napi_qos_user_initiated);
986 return bufferPromise;
987 }
988
TextAsync(napi_env env,napi_callback_info info)989 static napi_value TextAsync(napi_env env, napi_callback_info info)
990 {
991 napi_value thisVar = nullptr;
992 PromiseInfo *promiseInfo = new PromiseInfo();
993 napi_value resourceName = nullptr;
994 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
995 Blob *blob = nullptr;
996 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&blob)));
997 napi_value string = nullptr;
998 napi_create_string_utf8(env, reinterpret_cast<char *>(blob->GetRaw()), blob->GetLength(), &string);
999 napi_create_reference(env, string, 1, &promiseInfo->blobDataRef);
1000 napi_value textPromise = nullptr;
1001 napi_create_promise(env, &promiseInfo->deferred, &textPromise);
1002 napi_create_string_utf8(env, "GetPromiseOfString", NAPI_AUTO_LENGTH, &resourceName);
1003 napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void* data) {}, CopiedBlobToString,
1004 reinterpret_cast<void *>(promiseInfo), &promiseInfo->worker);
1005 napi_queue_async_work_with_qos(env, promiseInfo->worker, napi_qos_user_initiated);
1006 return textPromise;
1007 }
1008
GetBytes(napi_env env,napi_callback_info info)1009 static napi_value GetBytes(napi_env env, napi_callback_info info)
1010 {
1011 napi_value thisVar = nullptr;
1012 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
1013 Blob *blob = nullptr;
1014 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&blob)));
1015 napi_value result = nullptr;
1016 NAPI_CALL(env, napi_create_array(env, &result));
1017 size_t key = 0;
1018 napi_value value = nullptr;
1019 for (unsigned int i = 0; i < blob->GetLength(); i++) {
1020 napi_create_uint32(env, uint32_t(blob->GetByte(i) & 0xFF), &value);
1021 napi_set_element(env, result, key, value);
1022 key++;
1023 }
1024 return result;
1025 }
1026
BufferInit(napi_env env,napi_value exports)1027 static napi_value BufferInit(napi_env env, napi_value exports)
1028 {
1029 string className = "Buffer";
1030 napi_value bufferClass = nullptr;
1031 napi_property_descriptor bufferDesc[] = {
1032 DECLARE_NAPI_FUNCTION("writeInt32BE", WriteInt32BE),
1033 DECLARE_NAPI_FUNCTION("readInt32BE", ReadInt32BE),
1034 DECLARE_NAPI_FUNCTION("writeInt32LE", WriteInt32LE),
1035 DECLARE_NAPI_FUNCTION("readInt32LE", ReadInt32LE),
1036 DECLARE_NAPI_FUNCTION("writeUInt32BE", WriteUInt32BE),
1037 DECLARE_NAPI_FUNCTION("readUInt32BE", ReadUInt32BE),
1038 DECLARE_NAPI_FUNCTION("writeUInt32LE", WriteUInt32LE),
1039 DECLARE_NAPI_FUNCTION("readUInt32LE", ReadUInt32LE),
1040 DECLARE_NAPI_FUNCTION("setArray", SetArray),
1041 DECLARE_NAPI_FUNCTION("getLength", GetLength),
1042 DECLARE_NAPI_FUNCTION("getByteOffset", GetByteOffset),
1043 DECLARE_NAPI_FUNCTION("writeString", WriteString),
1044 DECLARE_NAPI_FUNCTION("fromString", FromString),
1045 DECLARE_NAPI_FUNCTION("fillString", FillString),
1046 DECLARE_NAPI_FUNCTION("fillNumbers", FillNumbers),
1047 DECLARE_NAPI_FUNCTION("fillBuffer", FillBuffer),
1048 DECLARE_NAPI_FUNCTION("getBufferData", GetBufferData),
1049 DECLARE_NAPI_FUNCTION("get", Get),
1050 DECLARE_NAPI_FUNCTION("set", Set),
1051 DECLARE_NAPI_FUNCTION("subBuffer", SubBuffer),
1052 DECLARE_NAPI_FUNCTION("copy", Copy),
1053 DECLARE_NAPI_FUNCTION("compare", Compare),
1054 DECLARE_NAPI_FUNCTION("toUtf8", ToUtf8),
1055 DECLARE_NAPI_FUNCTION("toBase64", ToBase64),
1056 DECLARE_NAPI_FUNCTION("toBase64Url", ToBase64Url),
1057 DECLARE_NAPI_FUNCTION("indexOf", IndexOf),
1058 };
1059 NAPI_CALL(env, napi_define_class(env, className.c_str(), className.length(), BufferConstructor,
1060 nullptr, sizeof(bufferDesc) / sizeof(bufferDesc[0]), bufferDesc, &bufferClass));
1061 napi_property_descriptor desc[] = {
1062 DECLARE_NAPI_PROPERTY("Buffer", bufferClass),
1063 };
1064 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1065 return exports;
1066 }
1067
BlobInit(napi_env env,napi_value exports)1068 static napi_value BlobInit(napi_env env, napi_value exports)
1069 {
1070 string className = "Blob";
1071 napi_value blobClass = nullptr;
1072 napi_property_descriptor blobDesc[] = {
1073 DECLARE_NAPI_FUNCTION("arraybuffer", ArrayBufferAsync),
1074 DECLARE_NAPI_FUNCTION("text", TextAsync),
1075 DECLARE_NAPI_FUNCTION("getBytes", GetBytes),
1076 };
1077 NAPI_CALL(env, napi_define_class(env, className.c_str(), className.length(), BlobConstructor,
1078 nullptr, sizeof(blobDesc) / sizeof(blobDesc[0]), blobDesc, &blobClass));
1079 napi_property_descriptor desc[] = {
1080 DECLARE_NAPI_PROPERTY("Blob", blobClass),
1081 };
1082 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1083 return exports;
1084 }
1085
Init(napi_env env,napi_value exports)1086 static napi_value Init(napi_env env, napi_value exports)
1087 {
1088 napi_property_descriptor desc[] = {
1089 DECLARE_NAPI_FUNCTION("utf8ByteLength", Utf8ByteLength),
1090 DECLARE_NAPI_FUNCTION("utf8StringToNumbers", Utf8StringToNumbers),
1091 };
1092 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1093 BufferInit(env, exports);
1094 BlobInit(env, exports);
1095 return exports;
1096 }
1097
1098 extern "C"
NAPI_buffer_GetJSCode(const char ** buf,int * bufLen)1099 __attribute__((visibility("default"))) void NAPI_buffer_GetJSCode(const char **buf, int *bufLen)
1100 {
1101 if (buf != nullptr) {
1102 *buf = _binary_js_buffer_js_start;
1103 }
1104
1105 if (bufLen != nullptr) {
1106 *bufLen = _binary_js_buffer_js_end - _binary_js_buffer_js_start;
1107 }
1108 }
1109
1110 extern "C"
NAPI_buffer_GetABCCode(const char ** buf,int * buflen)1111 __attribute__((visibility("default"))) void NAPI_buffer_GetABCCode(const char** buf, int* buflen)
1112 {
1113 if (buf != nullptr) {
1114 *buf = _binary_buffer_abc_start;
1115 }
1116 if (buflen != nullptr) {
1117 *buflen = _binary_buffer_abc_end - _binary_buffer_abc_start;
1118 }
1119 }
1120
1121 static napi_module_with_js bufferModule = {
1122 .nm_version = 1,
1123 .nm_flags = 0,
1124 .nm_filename = nullptr,
1125 .nm_register_func = Init,
1126 .nm_modname = "buffer",
1127 .nm_priv = reinterpret_cast<void *>(0),
1128 .nm_get_abc_code = NAPI_buffer_GetABCCode,
1129 .nm_get_js_code = NAPI_buffer_GetJSCode,
1130 };
1131
BufferRegisterModule()1132 extern "C" __attribute__((constructor)) void BufferRegisterModule()
1133 {
1134 napi_module_with_js_register(&bufferModule);
1135 }
1136 } // namespace OHOS::Buffer
1137