• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, const_cast<char *>(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, const_cast<char *>(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         return nullptr;
349     }
350 
351     return GetBufferWrapValue(env, thisVar, buffer);
352 }
353 
GetValueOffsetAndBuf(napi_env env,napi_callback_info info,int32_t * pValue,uint32_t * pOffset)354 Buffer *GetValueOffsetAndBuf(napi_env env, napi_callback_info info, int32_t *pValue, uint32_t *pOffset)
355 {
356     napi_value thisVar = nullptr;
357     size_t argc = 0;
358     napi_value args[2] = { 0 };
359     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
360     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
361     NAPI_ASSERT(env, args[0] != nullptr && args[1] != nullptr, "Parameter is empty.");
362 
363     Buffer *buf = nullptr;
364     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
365     NAPI_CALL(env, napi_get_value_int32(env, args[0], pValue));
366     NAPI_CALL(env, napi_get_value_uint32(env, args[1], pOffset));
367     return buf;
368 }
369 
GetOffsetAndBuf(napi_env env,napi_callback_info info,uint32_t * pOffset)370 Buffer *GetOffsetAndBuf(napi_env env, napi_callback_info info, uint32_t *pOffset)
371 {
372     napi_value thisVar = nullptr;
373     size_t argc = 1;
374     napi_value args[1] = { 0 };
375     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
376     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
377     NAPI_ASSERT(env, args[0] != nullptr, "Parameter is empty.");
378 
379     Buffer *buf = nullptr;
380     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
381     NAPI_CALL(env, napi_get_value_uint32(env, args[0], pOffset));
382     return buf;
383 }
384 
WriteInt32BE(napi_env env,napi_callback_info info)385 static napi_value WriteInt32BE(napi_env env, napi_callback_info info)
386 {
387     int32_t value = 0;
388     uint32_t offset = 0;
389     Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
390     if (buf != nullptr) {
391         buf->WriteInt32BE(value, offset);
392     }
393     napi_value result = nullptr;
394     NAPI_CALL(env, napi_get_undefined(env, &result));
395     return result;
396 }
397 
ReadInt32BE(napi_env env,napi_callback_info info)398 static napi_value ReadInt32BE(napi_env env, napi_callback_info info)
399 {
400     uint32_t offset = 0;
401     Buffer *buf = GetOffsetAndBuf(env, info, &offset);
402     int32_t res = 0;
403     if (buf != nullptr) {
404         res = buf->ReadInt32BE(offset);
405     }
406     napi_value result = nullptr;
407     napi_create_int32(env, res, &result);
408     return result;
409 }
410 
SetArray(napi_env env,napi_callback_info info)411 static napi_value SetArray(napi_env env, napi_callback_info info)
412 {
413     napi_value thisVar = nullptr;
414     size_t argc = 1;
415     napi_value args[1] = { 0 };
416     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
417     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
418     NAPI_ASSERT(env, args[0] != nullptr, "Parameter is empty.");
419 
420     bool isArray = false;
421     NAPI_CALL(env, napi_is_array(env, args[0], &isArray));
422     if (isArray) {
423         Buffer *buf = nullptr;
424         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
425         vector<uint8_t> arr = GetArray(env, args[0]);
426         buf->SetArray(arr);
427     }
428     napi_value result = nullptr;
429     NAPI_CALL(env, napi_get_undefined(env, &result));
430     return result;
431 }
432 
GetLength(napi_env env,napi_callback_info info)433 static napi_value GetLength(napi_env env, napi_callback_info info)
434 {
435     napi_value thisVar = nullptr;
436     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
437     Buffer *buf = nullptr;
438     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
439     uint32_t res = buf->GetLength();
440     napi_value result = nullptr;
441     napi_create_uint32(env, res, &result);
442     return result;
443 }
444 
GetByteOffset(napi_env env,napi_callback_info info)445 static napi_value GetByteOffset(napi_env env, napi_callback_info info)
446 {
447     napi_value thisVar = nullptr;
448     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
449     Buffer *buf = nullptr;
450     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
451     uint32_t res = buf->GetByteOffset();
452     napi_value result = nullptr;
453     napi_create_uint32(env, res, &result);
454     return result;
455 }
456 
WriteString(napi_env env,napi_callback_info info)457 static napi_value WriteString(napi_env env, napi_callback_info info)
458 {
459     napi_value thisVar = nullptr;
460     size_t argc = 0;
461     napi_value args[4] = { 0 };
462     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
463     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
464     // 4 : 4 arguments is right
465     NAPI_ASSERT(env, argc == 4, "Wrong number of arguments");
466     NAPI_ASSERT(env, args[0] != nullptr && args[1] != nullptr &&
467                 // 2 : 3 : the third argument and the forth argument
468                 args[2] != nullptr && args[3] != nullptr, "Parameter is empty.");
469 
470     // 3 : the forth argument
471     string encoding = GetStringASCII(env, args[3]);
472     EncodingType encodingType = Buffer::GetEncodingType(encoding);
473     string value = GetString(env, encodingType, args[0]);
474 
475     uint32_t offset = 0;
476     uint32_t length = 0;
477     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
478     // 2 : the third argument
479     NAPI_CALL(env, napi_get_value_uint32(env, args[2], &length));
480 
481     Buffer *buf = nullptr;
482     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
483     length = (value.length() < length) ? value.length() : length;
484     unsigned int lengthWrote = buf->WriteString(value, offset, length, encoding);
485 
486     napi_value result = nullptr;
487     napi_create_uint32(env, lengthWrote, &result);
488     return result;
489 }
490 
FillString(napi_env env,napi_callback_info info)491 static napi_value FillString(napi_env env, napi_callback_info info)
492 {
493     napi_value thisVar = nullptr;
494     size_t argc = 0;
495     napi_value args[4] = { 0 };
496     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
497     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
498     NAPI_ASSERT(env, args[0] != nullptr && args[1] != nullptr &&
499                 // 2 : 3 : the third argument and the forth argument
500                 args[2] != nullptr && args[3] != nullptr, "Parameter is empty.");
501 
502     string encoding = GetStringASCII(env, args[3]);
503     EncodingType encodingType = Buffer::GetEncodingType(encoding);
504     string value = GetString(env, encodingType, args[0]);
505 
506     uint32_t offset = 0;
507     uint32_t end = 0;
508     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
509     // 2 : the third argument
510     NAPI_CALL(env, napi_get_value_uint32(env, args[2], &end));
511 
512     Buffer *buf = nullptr;
513     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
514     buf->FillString(value, offset, end, encoding);
515 
516     napi_value result = nullptr;
517     NAPI_CALL(env, napi_get_undefined(env, &result));
518     return result;
519 }
520 
FillNumbers(napi_env env,napi_callback_info info)521 static napi_value FillNumbers(napi_env env, napi_callback_info info)
522 {
523     napi_value thisVar = nullptr;
524     size_t argc = 0;
525     napi_value args[3] = { 0 };
526     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
527     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
528     // 2 : the third argument
529     NAPI_ASSERT(env, args[0] != nullptr && args[1] != nullptr && args[2] != nullptr, "Parameter is empty.");
530 
531     uint32_t offset = 0;
532     uint32_t end = 0;
533     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
534     // 2 : the third argument
535     NAPI_CALL(env, napi_get_value_uint32(env, args[2], &end));
536 
537     Buffer *buf = nullptr;
538     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
539     vector<uint8_t> arr = GetArray(env, args[0]);
540     buf->FillNumber(arr, offset, end);
541 
542     napi_value result = nullptr;
543     NAPI_CALL(env, napi_get_undefined(env, &result));
544     return result;
545 }
546 
FillBuffer(napi_env env,napi_callback_info info)547 static napi_value FillBuffer(napi_env env, napi_callback_info info)
548 {
549     napi_value thisVar = nullptr;
550     size_t argc = 0;
551     napi_value args[3] = { 0 };
552     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
553     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
554     // 2 : the third argument
555     NAPI_ASSERT(env, args[0] != nullptr && args[1] != nullptr && args[2] != nullptr, "Parameter is empty.");
556 
557     uint32_t offset = 0;
558     uint32_t end = 0;
559     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
560     // 2 : the third argument
561     NAPI_CALL(env, napi_get_value_uint32(env, args[2], &end));
562 
563     Buffer *buffer = nullptr;
564     NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&buffer)));
565     Buffer *ego = nullptr;
566     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&ego)));
567     ego->FillBuffer(buffer, offset, end);
568 
569     napi_value result = nullptr;
570     NAPI_CALL(env, napi_get_undefined(env, &result));
571     return result;
572 }
573 
Utf8ByteLength(napi_env env,napi_callback_info info)574 static napi_value Utf8ByteLength(napi_env env, napi_callback_info info)
575 {
576     napi_value thisVar = nullptr;
577     size_t argc = 1;
578     napi_value args[1] = { 0 };
579     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
580     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
581     NAPI_ASSERT(env, args[0] != nullptr, "Parameter is empty.");
582     size_t byteLen = 0;
583     NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], nullptr, 0, &byteLen));
584     napi_value result = nullptr;
585     napi_create_uint32(env, byteLen, &result);
586     return result;
587 }
588 
GetBufferData(napi_env env,napi_callback_info info)589 static napi_value GetBufferData(napi_env env, napi_callback_info info)
590 {
591     napi_value result = nullptr;
592     napi_value thisVar = nullptr;
593     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
594     Buffer *buf = nullptr;
595     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
596     uint32_t length = buf->GetLength();
597     uint8_t* data = new uint8_t[length];
598     if (data == nullptr) {
599         HILOG_ERROR("buffer:: data is NULL");
600         return result;
601     }
602     buf->ReadBytes(data, 0, length);
603     NAPI_CALL(env, napi_create_array(env, &result));
604     size_t key = 0;
605     napi_value value = nullptr;
606     for (uint32_t i = 0, len = length; i < len; i++) {
607         napi_create_uint32(env, data[i], &value);
608         napi_set_element(env, result, key, value);
609         key++;
610     }
611     delete[] data;
612     data = nullptr;
613     return result;
614 }
615 
Get(napi_env env,napi_callback_info info)616 static napi_value Get(napi_env env, napi_callback_info info)
617 {
618     napi_value thisVar = nullptr;
619     size_t argc = 1;
620     napi_value args[1] = { 0 };
621     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
622     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
623     NAPI_ASSERT(env, args[0] != nullptr, "Parameter is empty.");
624     uint32_t index = 0;
625     NAPI_CALL(env, napi_get_value_uint32(env, args[0], &index));
626     Buffer *buf = nullptr;
627     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
628     int32_t value = buf->Get(index);
629     napi_value result = nullptr;
630     napi_create_int32(env, value, &result);
631     return result;
632 }
633 
Set(napi_env env,napi_callback_info info)634 static napi_value Set(napi_env env, napi_callback_info info)
635 {
636     napi_value thisVar = nullptr;
637     size_t argc = 0;
638     napi_value args[2] = { 0 };
639     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
640     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
641     NAPI_ASSERT(env, args[0] != nullptr && args[1] != nullptr, "Parameter is empty.");
642 
643     Buffer *buf = nullptr;
644     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
645 
646     uint32_t index = 0;
647     int32_t value = 0;
648     NAPI_CALL(env, napi_get_value_uint32(env, args[0], &index));
649     NAPI_CALL(env, napi_get_value_int32(env, args[1], &value));
650     buf->Set(index, value);
651     napi_value result = nullptr;
652     NAPI_CALL(env, napi_get_undefined(env, &result));
653     return result;
654 }
655 
WriteInt32LE(napi_env env,napi_callback_info info)656 static napi_value WriteInt32LE(napi_env env, napi_callback_info info)
657 {
658     int32_t value = 0;
659     uint32_t offset = 0;
660     Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
661     if (buf != nullptr) {
662         buf->WriteInt32LE(value, offset);
663     }
664     napi_value result = nullptr;
665     NAPI_CALL(env, napi_get_undefined(env, &result));
666     return result;
667 }
668 
ReadInt32LE(napi_env env,napi_callback_info info)669 static napi_value ReadInt32LE(napi_env env, napi_callback_info info)
670 {
671     uint32_t offset = 0;
672     Buffer *buf = GetOffsetAndBuf(env, info, &offset);
673     int32_t res = 0;
674     if (buf != nullptr) {
675         res = buf->ReadInt32LE(offset);
676     }
677     napi_value result = nullptr;
678     napi_create_int32(env, res, &result);
679     return result;
680 }
681 
WriteUInt32BE(napi_env env,napi_callback_info info)682 static napi_value WriteUInt32BE(napi_env env, napi_callback_info info)
683 {
684     int32_t value = 0;
685     uint32_t offset = 0;
686     Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
687     if (buf != nullptr) {
688         buf->WriteUInt32BE(value, offset);
689     }
690     napi_value result = nullptr;
691     NAPI_CALL(env, napi_get_undefined(env, &result));
692     return result;
693 }
694 
ReadUInt32BE(napi_env env,napi_callback_info info)695 static napi_value ReadUInt32BE(napi_env env, napi_callback_info info)
696 {
697     uint32_t offset = 0;
698     Buffer *buf = GetOffsetAndBuf(env, info, &offset);
699     uint32_t res = 0;
700     if (buf != nullptr) {
701         res = buf->ReadUInt32BE(offset);
702     }
703     napi_value result = nullptr;
704     napi_create_uint32(env, res, &result);
705     return result;
706 }
707 
WriteUInt32LE(napi_env env,napi_callback_info info)708 static napi_value WriteUInt32LE(napi_env env, napi_callback_info info)
709 {
710     int32_t value = 0;
711     uint32_t offset = 0;
712     Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
713     if (buf != nullptr) {
714         buf->WriteUInt32LE(value, offset);
715     }
716     napi_value result = nullptr;
717     NAPI_CALL(env, napi_get_undefined(env, &result));
718     return result;
719 }
720 
ReadUInt32LE(napi_env env,napi_callback_info info)721 static napi_value ReadUInt32LE(napi_env env, napi_callback_info info)
722 {
723     uint32_t offset = 0;
724     Buffer *buf = GetOffsetAndBuf(env, info, &offset);
725     uint32_t res = 0;
726     if (buf != nullptr) {
727         res = buf->ReadUInt32LE(offset);
728     }
729     napi_value result = nullptr;
730     napi_create_uint32(env, res, &result);
731     return result;
732 }
733 
SubBuffer(napi_env env,napi_callback_info info)734 static napi_value SubBuffer(napi_env env, napi_callback_info info)
735 {
736     napi_value thisVar = nullptr;
737     size_t argc = 0;
738     napi_value args[3] = { 0 };
739     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
740     NAPI_ASSERT(env, argc == 3, "Wrong number of arguments");
741     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
742     NAPI_ASSERT(env, args[0] != nullptr && args[1] != nullptr && args[2] != nullptr, "Parameter is empty.");
743     Buffer *newBuf = nullptr;
744     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&newBuf)));
745     Buffer *targetBuf = nullptr;
746     NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&targetBuf)));
747 
748     uint32_t start = 0;
749     uint32_t end = 0;
750     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &start));
751     NAPI_CALL(env, napi_get_value_uint32(env, args[2], &end));
752     newBuf->SubBuffer(targetBuf, start, end);
753     napi_value result = nullptr;
754     NAPI_CALL(env, napi_get_undefined(env, &result));
755     return result;
756 }
757 
Copy(napi_env env,napi_callback_info info)758 static napi_value Copy(napi_env env, napi_callback_info info)
759 {
760     napi_value thisVar = nullptr;
761     size_t argc = 0;
762     napi_value args[4] = { 0 };
763     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
764     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
765     // 4 : 4 arguments is right
766     NAPI_ASSERT(env, argc == 4, "Wrong number of arguments");
767 
768     uint32_t targetStart = 0;
769     uint32_t sourceStart = 0;
770     uint32_t sourceEnd = 0;
771     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &targetStart));
772     // 2 : the third argument
773     NAPI_CALL(env, napi_get_value_uint32(env, args[2], &sourceStart));
774     // 3 : the forth argument
775     NAPI_CALL(env, napi_get_value_uint32(env, args[3], &sourceEnd));
776     Buffer *targetBuf = nullptr;
777     NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&targetBuf)));
778     Buffer *sBuf = nullptr;
779     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&sBuf)));
780     uint32_t cLength = sBuf->Copy(targetBuf, targetStart, sourceStart, sourceEnd);
781     napi_value result = nullptr;
782     napi_create_int32(env, cLength, &result);
783     return result;
784 }
785 
Compare(napi_env env,napi_callback_info info)786 static napi_value Compare(napi_env env, napi_callback_info info)
787 {
788     napi_value thisVar = nullptr;
789     size_t argc = 0;
790     napi_value args[4] = { 0 };
791     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
792     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
793     uint32_t targetStart = 0;
794     uint32_t sourceStart = 0;
795     uint32_t length = 0;
796     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &targetStart));
797     // 2 : the third argument
798     NAPI_CALL(env, napi_get_value_uint32(env, args[2], &sourceStart));
799     // 3 : the forth argument
800     NAPI_CALL(env, napi_get_value_uint32(env, args[3], &length));
801     Buffer *targetBuf = nullptr;
802     NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&targetBuf)));
803     Buffer *sBuf = nullptr;
804     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&sBuf)));
805     int res = sBuf->Compare(targetBuf, targetStart, sourceStart, length);
806     napi_value result = nullptr;
807     napi_create_int32(env, res, &result);
808     return result;
809 }
810 
ToUtf8(napi_env env,napi_callback_info info)811 static napi_value ToUtf8(napi_env env, napi_callback_info info)
812 {
813     napi_value thisVar = nullptr;
814     napi_value result = nullptr;
815     size_t argc = 0;
816     napi_value args[2] = { 0 };
817     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
818     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
819     uint32_t start = 0;
820     uint32_t end = 0;
821     NAPI_CALL(env, napi_get_value_uint32(env, args[0], &start));
822     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &end));
823     Buffer *buf = nullptr;
824     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&buf)));
825     uint32_t length = end - start;
826     uint8_t* data = new uint8_t[length];
827     if (data == nullptr) {
828         HILOG_ERROR("buffer:: data is NULL");
829         return result;
830     }
831     buf->ReadBytes(data, start, length);
832     napi_create_string_utf8(env, reinterpret_cast<char *>(data), length, &result);
833     delete[] data;
834     data = nullptr;
835     return result;
836 }
837 
ToBase64(napi_env env,napi_callback_info info)838 static napi_value ToBase64(napi_env env, napi_callback_info info)
839 {
840     napi_value thisVar = nullptr;
841     size_t argc = 0;
842     napi_value args[2] = { 0 };
843     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
844     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
845     uint32_t start = 0;
846     uint32_t end = 0;
847     NAPI_CALL(env, napi_get_value_uint32(env, args[0], &start));
848     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &end));
849     Buffer *buf = nullptr;
850     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&buf)));
851     uint32_t length = end - start;
852     std::string str = buf->ToBase64(start, length);
853     napi_value result = nullptr;
854     napi_create_string_latin1(env, str.c_str(), str.length(), &result);
855     return result;
856 }
857 
IndexOf(napi_env env,napi_callback_info info)858 static napi_value IndexOf(napi_env env, napi_callback_info info)
859 {
860     napi_value thisVar = nullptr;
861     size_t argc = 0;
862     napi_value args[4] = { 0 };
863     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
864     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
865     uint32_t offset = 0;
866     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
867 
868     // 2 : the third argument
869     string type = GetStringASCII(env, args[2]);
870     EncodingType eType = Buffer::GetEncodingType(type);
871     std::string str;
872     std::u16string u16Str;
873     uint32_t len = 0;
874     const char *data = nullptr;
875     switch (eType) {
876         case ASCII:
877         case LATIN1:
878         case BINARY:
879             str = GetStringASCII(env, args[0]);
880             data = str.c_str();
881             break;
882         case UTF8:
883             str = GetStringUtf8(env, args[0]);
884             data = str.c_str();
885             break;
886         case UTF16LE: {
887             u16Str = GetStringUtf16LE(env, args[0]);
888             data = reinterpret_cast<char *>(const_cast<char16_t *>(u16Str.c_str()));
889             len = u16Str.length() * 2; // 2 : 2 means the length of wide char String is 2 times of char String
890             break;
891         }
892         case BASE64:
893         case BASE64URL:
894             str = GetStringBase64(env, args[0]);
895             data = str.c_str();
896             break;
897         case HEX:
898             str = GetStringHex(env, args[0]);
899             data = str.c_str();
900             break;
901         default:
902             break;
903     }
904 
905     Buffer *buf = nullptr;
906     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
907     bool isReverse = false;
908     // 3 : the forth argument
909     NAPI_CALL(env, napi_get_value_bool(env, args[3], &isReverse));
910     int index = -1;
911     if (isReverse) {
912         len = (eType == UTF16LE) ? len : str.length();
913         index = buf->LastIndexOf(data, offset, len);
914     } else {
915         len = (eType == UTF16LE) ? len : str.length();
916         index = buf->IndexOf(data, offset, len);
917     }
918     napi_value result = nullptr;
919     napi_create_int32(env, index, &result);
920     return result;
921 }
922 
Utf8StringToNumbers(napi_env env,napi_callback_info info)923 static napi_value Utf8StringToNumbers(napi_env env, napi_callback_info info)
924 {
925     napi_value thisVar = nullptr;
926     size_t argc = 0;
927     napi_value args[1] = { 0 };
928     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
929     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
930 
931     std::string str = GetStringUtf8(env, args[0]);
932     napi_value result = nullptr;
933     NAPI_CALL(env, napi_create_array(env, &result));
934     size_t key = 0;
935     napi_value value = nullptr;
936     for (uint32_t i = 0; i < str.length(); i++) {
937         napi_create_uint32(env, uint32_t(str.at(i) & 0xFF), &value);
938         napi_set_element(env, result, key, value);
939         key++;
940     }
941     return result;
942 }
943 
944 struct PromiseInfo {
945     napi_env env = nullptr;
946     napi_async_work worker = nullptr;
947     napi_deferred deferred = nullptr;
948     napi_ref blobDataRef = nullptr;
949 };
950 
CopiedBlobToString(napi_env env,napi_status status,void * data)951 static void CopiedBlobToString(napi_env env, napi_status status, void *data)
952 {
953     auto promiseInfo = reinterpret_cast<PromiseInfo *>(data);
954     napi_value string = nullptr;
955     napi_get_reference_value(env, promiseInfo->blobDataRef, &string);
956     napi_resolve_deferred(env, promiseInfo->deferred, string);
957     napi_delete_reference(env, promiseInfo->blobDataRef);
958     napi_delete_async_work(env, promiseInfo->worker);
959     delete promiseInfo;
960 }
961 
CopiedBlobToArrayBuffer(napi_env env,napi_status status,void * data)962 static void CopiedBlobToArrayBuffer(napi_env env, napi_status status, void *data)
963 {
964     auto promiseInfo = reinterpret_cast<PromiseInfo *>(data);
965     napi_value arrayBuffer = nullptr;
966     napi_get_reference_value(env, promiseInfo->blobDataRef, &arrayBuffer);
967     napi_resolve_deferred(env, promiseInfo->deferred, arrayBuffer);
968     napi_delete_reference(env, promiseInfo->blobDataRef);
969     napi_delete_async_work(env, promiseInfo->worker);
970     delete promiseInfo;
971 }
972 
ArrayBufferAsync(napi_env env,napi_callback_info info)973 static napi_value ArrayBufferAsync(napi_env env, napi_callback_info info)
974 {
975     napi_value thisVar = nullptr;
976     PromiseInfo *promiseInfo = new PromiseInfo();
977     napi_value resourceName = nullptr;
978     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
979     Blob *blob = nullptr;
980     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&blob)));
981     size_t bufferSize = blob->GetLength();
982     void *bufdata = nullptr;
983     napi_value arrayBuffer = nullptr;
984     napi_value bufferPromise = nullptr;
985     napi_create_arraybuffer(env, bufferSize, &bufdata, &arrayBuffer);
986     blob->ReadBytes(reinterpret_cast<uint8_t *>(bufdata), bufferSize);
987     napi_create_reference(env, arrayBuffer, 1, &promiseInfo->blobDataRef);
988     napi_create_promise(env, &promiseInfo->deferred, &bufferPromise);
989     napi_create_string_utf8(env, "CopyBlobToArrayBuffer", NAPI_AUTO_LENGTH, &resourceName);
990     napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void* data) {}, CopiedBlobToArrayBuffer,
991                            reinterpret_cast<void *>(promiseInfo), &promiseInfo->worker);
992     napi_queue_async_work(env, promiseInfo->worker);
993     return bufferPromise;
994 }
995 
TextAsync(napi_env env,napi_callback_info info)996 static napi_value TextAsync(napi_env env, napi_callback_info info)
997 {
998     napi_value thisVar = nullptr;
999     PromiseInfo *promiseInfo = new PromiseInfo();
1000     napi_value resourceName = nullptr;
1001     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
1002     Blob *blob = nullptr;
1003     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&blob)));
1004     napi_value string = nullptr;
1005     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->GetRaw()), blob->GetLength(), &string);
1006     napi_create_reference(env, string, 1, &promiseInfo->blobDataRef);
1007     napi_value textPromise = nullptr;
1008     napi_create_promise(env, &promiseInfo->deferred, &textPromise);
1009     napi_create_string_utf8(env, "GetPromiseOfString", NAPI_AUTO_LENGTH, &resourceName);
1010     napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void* data) {}, CopiedBlobToString,
1011                            reinterpret_cast<void *>(promiseInfo), &promiseInfo->worker);
1012     napi_queue_async_work(env, promiseInfo->worker);
1013     return textPromise;
1014 }
1015 
GetBytes(napi_env env,napi_callback_info info)1016 static napi_value GetBytes(napi_env env, napi_callback_info info)
1017 {
1018     napi_value thisVar = nullptr;
1019     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
1020     Blob *blob = nullptr;
1021     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&blob)));
1022     napi_value result = nullptr;
1023     NAPI_CALL(env, napi_create_array(env, &result));
1024     size_t key = 0;
1025     napi_value value = nullptr;
1026     for (unsigned int i = 0; i < blob->GetLength(); i++) {
1027         napi_create_uint32(env, uint32_t(blob->GetByte(i) & 0xFF), &value);
1028         napi_set_element(env, result, key, value);
1029         key++;
1030     }
1031     return result;
1032 }
1033 
BufferInit(napi_env env,napi_value exports)1034 static napi_value BufferInit(napi_env env, napi_value exports)
1035 {
1036     string className = "Buffer";
1037     napi_value bufferClass = nullptr;
1038     napi_property_descriptor bufferDesc[] = {
1039         DECLARE_NAPI_FUNCTION("writeInt32BE", WriteInt32BE),
1040         DECLARE_NAPI_FUNCTION("readInt32BE", ReadInt32BE),
1041         DECLARE_NAPI_FUNCTION("writeInt32LE", WriteInt32LE),
1042         DECLARE_NAPI_FUNCTION("readInt32LE", ReadInt32LE),
1043         DECLARE_NAPI_FUNCTION("writeUInt32BE", WriteUInt32BE),
1044         DECLARE_NAPI_FUNCTION("readUInt32BE", ReadUInt32BE),
1045         DECLARE_NAPI_FUNCTION("writeUInt32LE", WriteUInt32LE),
1046         DECLARE_NAPI_FUNCTION("readUInt32LE", ReadUInt32LE),
1047         DECLARE_NAPI_FUNCTION("setArray", SetArray),
1048         DECLARE_NAPI_FUNCTION("getLength", GetLength),
1049         DECLARE_NAPI_FUNCTION("getByteOffset", GetByteOffset),
1050         DECLARE_NAPI_FUNCTION("writeString", WriteString),
1051         DECLARE_NAPI_FUNCTION("fromString", FromString),
1052         DECLARE_NAPI_FUNCTION("fillString", FillString),
1053         DECLARE_NAPI_FUNCTION("fillNumbers", FillNumbers),
1054         DECLARE_NAPI_FUNCTION("fillBuffer", FillBuffer),
1055         DECLARE_NAPI_FUNCTION("getBufferData", GetBufferData),
1056         DECLARE_NAPI_FUNCTION("get", Get),
1057         DECLARE_NAPI_FUNCTION("set", Set),
1058         DECLARE_NAPI_FUNCTION("subBuffer", SubBuffer),
1059         DECLARE_NAPI_FUNCTION("copy", Copy),
1060         DECLARE_NAPI_FUNCTION("compare", Compare),
1061         DECLARE_NAPI_FUNCTION("toUtf8", ToUtf8),
1062         DECLARE_NAPI_FUNCTION("toBase64", ToBase64),
1063         DECLARE_NAPI_FUNCTION("indexOf", IndexOf),
1064     };
1065     NAPI_CALL(env, napi_define_class(env, className.c_str(), className.length(), BufferConstructor,
1066                                      nullptr, sizeof(bufferDesc) / sizeof(bufferDesc[0]), bufferDesc, &bufferClass));
1067     napi_property_descriptor desc[] = {
1068         DECLARE_NAPI_PROPERTY("Buffer", bufferClass),
1069     };
1070     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1071     return exports;
1072 }
1073 
BlobInit(napi_env env,napi_value exports)1074 static napi_value BlobInit(napi_env env, napi_value exports)
1075 {
1076     string className = "Blob";
1077     napi_value blobClass = nullptr;
1078     napi_property_descriptor blobDesc[] = {
1079         DECLARE_NAPI_FUNCTION("arraybuffer", ArrayBufferAsync),
1080         DECLARE_NAPI_FUNCTION("text", TextAsync),
1081         DECLARE_NAPI_FUNCTION("getBytes", GetBytes),
1082     };
1083     NAPI_CALL(env, napi_define_class(env, className.c_str(), className.length(), BlobConstructor,
1084                                      nullptr, sizeof(blobDesc) / sizeof(blobDesc[0]), blobDesc, &blobClass));
1085     napi_property_descriptor desc[] = {
1086         DECLARE_NAPI_PROPERTY("Blob", blobClass),
1087     };
1088     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1089     return exports;
1090 }
1091 
Init(napi_env env,napi_value exports)1092 static napi_value Init(napi_env env, napi_value exports)
1093 {
1094     napi_property_descriptor desc[] = {
1095         DECLARE_NAPI_FUNCTION("utf8ByteLength", Utf8ByteLength),
1096         DECLARE_NAPI_FUNCTION("utf8StringToNumbers", Utf8StringToNumbers),
1097     };
1098     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1099     BufferInit(env, exports);
1100     BlobInit(env, exports);
1101     return exports;
1102 }
1103 
1104 extern "C"
NAPI_buffer_GetJSCode(const char ** buf,int * bufLen)1105 __attribute__((visibility("default"))) void NAPI_buffer_GetJSCode(const char **buf, int *bufLen)
1106 {
1107     if (buf != nullptr) {
1108         *buf = _binary_js_buffer_js_start;
1109     }
1110 
1111     if (bufLen != nullptr) {
1112         *bufLen = _binary_js_buffer_js_end - _binary_js_buffer_js_start;
1113     }
1114 }
1115 
1116 extern "C"
NAPI_buffer_GetABCCode(const char ** buf,int * buflen)1117 __attribute__((visibility("default"))) void NAPI_buffer_GetABCCode(const char** buf, int* buflen)
1118 {
1119     if (buf != nullptr) {
1120         *buf = _binary_buffer_abc_start;
1121     }
1122     if (buflen != nullptr) {
1123         *buflen = _binary_buffer_abc_end - _binary_buffer_abc_start;
1124     }
1125 }
1126 
1127 static napi_module_with_js bufferModule = {
1128     .nm_version = 1,
1129     .nm_flags = 0,
1130     .nm_filename = nullptr,
1131     .nm_register_func = Init,
1132     .nm_modname = "buffer",
1133     .nm_priv = reinterpret_cast<void *>(0),
1134     .nm_get_abc_code = NAPI_buffer_GetABCCode,
1135     .nm_get_js_code = NAPI_buffer_GetJSCode,
1136 };
1137 
BufferRegisterModule()1138 extern "C" __attribute__((constructor)) void BufferRegisterModule()
1139 {
1140     napi_module_with_js_register(&bufferModule);
1141 }
1142 } // namespace OHOS::Buffer
1143