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