• 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 thisVar = nullptr;
592     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
593     Buffer *buf = nullptr;
594     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
595     uint32_t length = buf->GetLength();
596     uint8_t data[length];
597     buf->ReadBytes(data, 0, length);
598     napi_value result = nullptr;
599     NAPI_CALL(env, napi_create_array(env, &result));
600     size_t key = 0;
601     napi_value value = nullptr;
602     for (int i = 0, len = sizeof(data); i < len; i++) {
603         napi_create_uint32(env, data[i], &value);
604         napi_set_element(env, result, key, value);
605         key++;
606     }
607     return result;
608 }
609 
Get(napi_env env,napi_callback_info info)610 static napi_value Get(napi_env env, napi_callback_info info)
611 {
612     napi_value thisVar = nullptr;
613     size_t argc = 1;
614     napi_value args[1] = { 0 };
615     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
616     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
617     NAPI_ASSERT(env, args[0] != nullptr, "Parameter is empty.");
618     uint32_t index = 0;
619     NAPI_CALL(env, napi_get_value_uint32(env, args[0], &index));
620     Buffer *buf = nullptr;
621     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
622     int32_t value = buf->Get(index);
623     napi_value result = nullptr;
624     napi_create_int32(env, value, &result);
625     return result;
626 }
627 
Set(napi_env env,napi_callback_info info)628 static napi_value Set(napi_env env, napi_callback_info info)
629 {
630     napi_value thisVar = nullptr;
631     size_t argc = 0;
632     napi_value args[2] = { 0 };
633     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
634     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
635     NAPI_ASSERT(env, args[0] != nullptr && args[1] != nullptr, "Parameter is empty.");
636 
637     Buffer *buf = nullptr;
638     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
639 
640     uint32_t index = 0;
641     int32_t value = 0;
642     NAPI_CALL(env, napi_get_value_uint32(env, args[0], &index));
643     NAPI_CALL(env, napi_get_value_int32(env, args[1], &value));
644     buf->Set(index, value);
645     napi_value result = nullptr;
646     NAPI_CALL(env, napi_get_undefined(env, &result));
647     return result;
648 }
649 
WriteInt32LE(napi_env env,napi_callback_info info)650 static napi_value WriteInt32LE(napi_env env, napi_callback_info info)
651 {
652     int32_t value = 0;
653     uint32_t offset = 0;
654     Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
655     if (buf != nullptr) {
656         buf->WriteInt32LE(value, offset);
657     }
658     napi_value result = nullptr;
659     NAPI_CALL(env, napi_get_undefined(env, &result));
660     return result;
661 }
662 
ReadInt32LE(napi_env env,napi_callback_info info)663 static napi_value ReadInt32LE(napi_env env, napi_callback_info info)
664 {
665     uint32_t offset = 0;
666     Buffer *buf = GetOffsetAndBuf(env, info, &offset);
667     int32_t res = 0;
668     if (buf != nullptr) {
669         res = buf->ReadInt32LE(offset);
670     }
671     napi_value result = nullptr;
672     napi_create_int32(env, res, &result);
673     return result;
674 }
675 
WriteUInt32BE(napi_env env,napi_callback_info info)676 static napi_value WriteUInt32BE(napi_env env, napi_callback_info info)
677 {
678     int32_t value = 0;
679     uint32_t offset = 0;
680     Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
681     if (buf != nullptr) {
682         buf->WriteUInt32BE(value, offset);
683     }
684     napi_value result = nullptr;
685     NAPI_CALL(env, napi_get_undefined(env, &result));
686     return result;
687 }
688 
ReadUInt32BE(napi_env env,napi_callback_info info)689 static napi_value ReadUInt32BE(napi_env env, napi_callback_info info)
690 {
691     uint32_t offset = 0;
692     Buffer *buf = GetOffsetAndBuf(env, info, &offset);
693     uint32_t res = 0;
694     if (buf != nullptr) {
695         res = buf->ReadUInt32BE(offset);
696     }
697     napi_value result = nullptr;
698     napi_create_uint32(env, res, &result);
699     return result;
700 }
701 
WriteUInt32LE(napi_env env,napi_callback_info info)702 static napi_value WriteUInt32LE(napi_env env, napi_callback_info info)
703 {
704     int32_t value = 0;
705     uint32_t offset = 0;
706     Buffer *buf = GetValueOffsetAndBuf(env, info, &value, &offset);
707     if (buf != nullptr) {
708         buf->WriteUInt32LE(value, offset);
709     }
710     napi_value result = nullptr;
711     NAPI_CALL(env, napi_get_undefined(env, &result));
712     return result;
713 }
714 
ReadUInt32LE(napi_env env,napi_callback_info info)715 static napi_value ReadUInt32LE(napi_env env, napi_callback_info info)
716 {
717     uint32_t offset = 0;
718     Buffer *buf = GetOffsetAndBuf(env, info, &offset);
719     uint32_t res = 0;
720     if (buf != nullptr) {
721         res = buf->ReadUInt32LE(offset);
722     }
723     napi_value result = nullptr;
724     napi_create_uint32(env, res, &result);
725     return result;
726 }
727 
SubBuffer(napi_env env,napi_callback_info info)728 static napi_value SubBuffer(napi_env env, napi_callback_info info)
729 {
730     napi_value thisVar = nullptr;
731     size_t argc = 0;
732     napi_value args[2] = { 0 };
733     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
734     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
735     NAPI_ASSERT(env, args[0] != nullptr && args[1] != nullptr, "Parameter is empty.");
736     Buffer *buf = nullptr;
737     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
738 
739     uint32_t start = 0;
740     uint32_t end = 0;
741     NAPI_CALL(env, napi_get_value_uint32(env, args[0], &start));
742     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &end));
743     Buffer *resBuf = buf->SubBuffer(start, end);
744 
745     return GetBufferWrapValue(env, thisVar, resBuf);
746 }
747 
Copy(napi_env env,napi_callback_info info)748 static napi_value Copy(napi_env env, napi_callback_info info)
749 {
750     napi_value thisVar = nullptr;
751     size_t argc = 0;
752     napi_value args[4] = { 0 };
753     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
754     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
755     // 4 : 4 arguments is right
756     NAPI_ASSERT(env, argc == 4, "Wrong number of arguments");
757 
758     uint32_t targetStart = 0;
759     uint32_t sourceStart = 0;
760     uint32_t sourceEnd = 0;
761     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &targetStart));
762     // 2 : the third argument
763     NAPI_CALL(env, napi_get_value_uint32(env, args[2], &sourceStart));
764     // 3 : the forth argument
765     NAPI_CALL(env, napi_get_value_uint32(env, args[3], &sourceEnd));
766     Buffer *targetBuf = nullptr;
767     NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&targetBuf)));
768     Buffer *sBuf = nullptr;
769     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&sBuf)));
770     uint32_t cLength = sBuf->Copy(targetBuf, targetStart, sourceStart, sourceEnd);
771     napi_value result = nullptr;
772     napi_create_int32(env, cLength, &result);
773     return result;
774 }
775 
Compare(napi_env env,napi_callback_info info)776 static napi_value Compare(napi_env env, napi_callback_info info)
777 {
778     napi_value thisVar = nullptr;
779     size_t argc = 0;
780     napi_value args[4] = { 0 };
781     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
782     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
783     uint32_t targetStart = 0;
784     uint32_t sourceStart = 0;
785     uint32_t length = 0;
786     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &targetStart));
787     // 2 : the third argument
788     NAPI_CALL(env, napi_get_value_uint32(env, args[2], &sourceStart));
789     // 3 : the forth argument
790     NAPI_CALL(env, napi_get_value_uint32(env, args[3], &length));
791     Buffer *targetBuf = nullptr;
792     NAPI_CALL(env, napi_unwrap(env, args[0], reinterpret_cast<void **>(&targetBuf)));
793     Buffer *sBuf = nullptr;
794     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&sBuf)));
795     int res = sBuf->Compare(targetBuf, targetStart, sourceStart, length);
796     napi_value result = nullptr;
797     napi_create_int32(env, res, &result);
798     return result;
799 }
800 
ToUtf8(napi_env env,napi_callback_info info)801 static napi_value ToUtf8(napi_env env, napi_callback_info info)
802 {
803     napi_value thisVar = nullptr;
804     size_t argc = 0;
805     napi_value args[2] = { 0 };
806     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
807     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
808     uint32_t start = 0;
809     uint32_t end = 0;
810     NAPI_CALL(env, napi_get_value_uint32(env, args[0], &start));
811     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &end));
812     Buffer *buf = nullptr;
813     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&buf)));
814     uint32_t length = end - start;
815     uint8_t data[length];
816     buf->ReadBytes(data, start, length);
817     napi_value result = nullptr;
818     napi_create_string_utf8(env, reinterpret_cast<char *>(data), length, &result);
819     return result;
820 }
821 
ToBase64(napi_env env,napi_callback_info info)822 static napi_value ToBase64(napi_env env, napi_callback_info info)
823 {
824     napi_value thisVar = nullptr;
825     size_t argc = 0;
826     napi_value args[2] = { 0 };
827     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
828     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
829     uint32_t start = 0;
830     uint32_t end = 0;
831     NAPI_CALL(env, napi_get_value_uint32(env, args[0], &start));
832     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &end));
833     Buffer *buf = nullptr;
834     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&buf)));
835     uint32_t length = end - start;
836     std::string str = buf->ToBase64(start, length);
837     napi_value result = nullptr;
838     napi_create_string_latin1(env, str.c_str(), str.length(), &result);
839     return result;
840 }
841 
IndexOf(napi_env env,napi_callback_info info)842 static napi_value IndexOf(napi_env env, napi_callback_info info)
843 {
844     napi_value thisVar = nullptr;
845     size_t argc = 0;
846     napi_value args[4] = { 0 };
847     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
848     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
849     uint32_t offset = 0;
850     NAPI_CALL(env, napi_get_value_uint32(env, args[1], &offset));
851 
852     // 2 : the third argument
853     string type = GetStringASCII(env, args[2]);
854     EncodingType eType = Buffer::GetEncodingType(type);
855     std::string str;
856     std::u16string u16Str;
857     uint32_t len = 0;
858     const char *data = nullptr;
859     switch (eType) {
860         case ASCII:
861         case LATIN1:
862         case BINARY:
863             str = GetStringASCII(env, args[0]);
864             data = str.c_str();
865             break;
866         case UTF8:
867             str = GetStringUtf8(env, args[0]);
868             data = str.c_str();
869             break;
870         case UTF16LE: {
871             u16Str = GetStringUtf16LE(env, args[0]);
872             data = reinterpret_cast<char *>(const_cast<char16_t *>(u16Str.c_str()));
873             len = u16Str.length() * 2; // 2 : 2 means the length of wide char String is 2 times of char String
874             break;
875         }
876         case BASE64:
877         case BASE64URL:
878             str = GetStringBase64(env, args[0]);
879             data = str.c_str();
880             break;
881         case HEX:
882             str = GetStringHex(env, args[0]);
883             data = str.c_str();
884             break;
885         default:
886             break;
887     }
888 
889     Buffer *buf = nullptr;
890     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&buf)));
891     bool isReverse = false;
892     // 3 : the forth argument
893     NAPI_CALL(env, napi_get_value_bool(env, args[3], &isReverse));
894     int index = -1;
895     if (isReverse) {
896         len = (eType == UTF16LE) ? len : str.length();
897         index = buf->LastIndexOf(data, offset, len);
898     } else {
899         len = (eType == UTF16LE) ? len : str.length();
900         index = buf->IndexOf(data, offset, len);
901     }
902     napi_value result = nullptr;
903     napi_create_int32(env, index, &result);
904     return result;
905 }
906 
Utf8StringToNumbers(napi_env env,napi_callback_info info)907 static napi_value Utf8StringToNumbers(napi_env env, napi_callback_info info)
908 {
909     napi_value thisVar = nullptr;
910     size_t argc = 0;
911     napi_value args[1] = { 0 };
912     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
913     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
914 
915     std::string str = GetStringUtf8(env, args[0]);
916     napi_value result = nullptr;
917     NAPI_CALL(env, napi_create_array(env, &result));
918     size_t key = 0;
919     napi_value value = nullptr;
920     for (uint32_t i = 0; i < str.length(); i++) {
921         napi_create_uint32(env, uint32_t(str.at(i) & 0xFF), &value);
922         napi_set_element(env, result, key, value);
923         key++;
924     }
925     return result;
926 }
927 
928 struct PromiseInfo {
929     napi_env env = nullptr;
930     napi_async_work worker = nullptr;
931     napi_deferred deferred = nullptr;
932     napi_value promise = nullptr;
933     Blob* jsBlob = nullptr;
934     napi_value arrayBuffer = nullptr;
935     napi_value string = nullptr;
936 };
937 
CopiedBlobToArrayBuffer(napi_env env,napi_status status,void * data)938 static void CopiedBlobToArrayBuffer(napi_env env, napi_status status, void *data)
939 {
940     auto promiseInfo = reinterpret_cast<PromiseInfo *>(data);
941     Blob *blob = promiseInfo->jsBlob;
942     void *bufdata = nullptr;
943     size_t bufferSize = blob->GetLength();
944     napi_create_arraybuffer(env, bufferSize, &bufdata, &promiseInfo->arrayBuffer);
945     blob->ReadBytes(reinterpret_cast<uint8_t *>(bufdata), bufferSize);
946     napi_resolve_deferred(env, promiseInfo->deferred, promiseInfo->arrayBuffer);
947     napi_delete_async_work(env, promiseInfo->worker);
948     delete promiseInfo;
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     Blob *blob = promiseInfo->jsBlob;
955     napi_create_string_utf8(env, reinterpret_cast<char *>(blob->GetRaw()), blob->GetLength(), &promiseInfo->string);
956     napi_resolve_deferred(env, promiseInfo->deferred, promiseInfo->string);
957     napi_delete_async_work(env, promiseInfo->worker);
958     delete promiseInfo;
959 }
960 
ArrayBufferAsync(napi_env env,napi_callback_info info)961 static napi_value ArrayBufferAsync(napi_env env, napi_callback_info info)
962 {
963     napi_value thisVar = nullptr;
964     PromiseInfo *promiseInfo = new PromiseInfo();
965     napi_value resourceName = nullptr;
966     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
967     Blob *blob = nullptr;
968     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&blob)));
969     promiseInfo->jsBlob = blob;
970     napi_create_promise(env, &promiseInfo->deferred, &promiseInfo->promise);
971     napi_create_string_utf8(env, "CopyBlobToArrayBuffer", NAPI_AUTO_LENGTH, &resourceName);
972     napi_create_async_work(env, nullptr, resourceName, nullptr, CopiedBlobToArrayBuffer,
973                            reinterpret_cast<void *>(promiseInfo), &promiseInfo->worker);
974     napi_queue_async_work(env, promiseInfo->worker);
975     return promiseInfo->promise;
976 }
977 
TextAsync(napi_env env,napi_callback_info info)978 static napi_value TextAsync(napi_env env, napi_callback_info info)
979 {
980     napi_value thisVar = nullptr;
981     PromiseInfo *promiseInfo = new PromiseInfo();
982     napi_value resourceName = nullptr;
983     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
984     Blob *blob = nullptr;
985     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&blob)));
986     promiseInfo->jsBlob = blob;
987 
988     napi_create_promise(env, &promiseInfo->deferred, &promiseInfo->promise);
989     napi_create_string_utf8(env, "GetPromiseOfString", NAPI_AUTO_LENGTH, &resourceName);
990     napi_create_async_work(env, nullptr, resourceName, nullptr, CopiedBlobToString,
991                            reinterpret_cast<void *>(promiseInfo), &promiseInfo->worker);
992     napi_queue_async_work(env, promiseInfo->worker);
993 
994     return promiseInfo->promise;
995 }
996 
GetBytes(napi_env env,napi_callback_info info)997 static napi_value GetBytes(napi_env env, napi_callback_info info)
998 {
999     napi_value thisVar = nullptr;
1000     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
1001     Blob *blob = nullptr;
1002     NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void **>(&blob)));
1003     napi_value result = nullptr;
1004     NAPI_CALL(env, napi_create_array(env, &result));
1005     size_t key = 0;
1006     napi_value value = nullptr;
1007     for (unsigned int i = 0; i < blob->GetLength(); i++) {
1008         napi_create_uint32(env, uint32_t(blob->GetByte(i) & 0xFF), &value);
1009         napi_set_element(env, result, key, value);
1010         key++;
1011     }
1012     return result;
1013 }
1014 
BufferInit(napi_env env,napi_value exports)1015 static napi_value BufferInit(napi_env env, napi_value exports)
1016 {
1017     string className = "Buffer";
1018     napi_value bufferClass = nullptr;
1019     napi_property_descriptor bufferDesc[] = {
1020         DECLARE_NAPI_FUNCTION("writeInt32BE", WriteInt32BE),
1021         DECLARE_NAPI_FUNCTION("readInt32BE", ReadInt32BE),
1022         DECLARE_NAPI_FUNCTION("writeInt32LE", WriteInt32LE),
1023         DECLARE_NAPI_FUNCTION("readInt32LE", ReadInt32LE),
1024         DECLARE_NAPI_FUNCTION("writeUInt32BE", WriteUInt32BE),
1025         DECLARE_NAPI_FUNCTION("readUInt32BE", ReadUInt32BE),
1026         DECLARE_NAPI_FUNCTION("writeUInt32LE", WriteUInt32LE),
1027         DECLARE_NAPI_FUNCTION("readUInt32LE", ReadUInt32LE),
1028         DECLARE_NAPI_FUNCTION("setArray", SetArray),
1029         DECLARE_NAPI_FUNCTION("getLength", GetLength),
1030         DECLARE_NAPI_FUNCTION("getByteOffset", GetByteOffset),
1031         DECLARE_NAPI_FUNCTION("writeString", WriteString),
1032         DECLARE_NAPI_FUNCTION("fromString", FromString),
1033         DECLARE_NAPI_FUNCTION("fillString", FillString),
1034         DECLARE_NAPI_FUNCTION("fillNumbers", FillNumbers),
1035         DECLARE_NAPI_FUNCTION("fillBuffer", FillBuffer),
1036         DECLARE_NAPI_FUNCTION("getBufferData", GetBufferData),
1037         DECLARE_NAPI_FUNCTION("get", Get),
1038         DECLARE_NAPI_FUNCTION("set", Set),
1039         DECLARE_NAPI_FUNCTION("subBuffer", SubBuffer),
1040         DECLARE_NAPI_FUNCTION("copy", Copy),
1041         DECLARE_NAPI_FUNCTION("compare", Compare),
1042         DECLARE_NAPI_FUNCTION("toUtf8", ToUtf8),
1043         DECLARE_NAPI_FUNCTION("toBase64", ToBase64),
1044         DECLARE_NAPI_FUNCTION("indexOf", IndexOf),
1045     };
1046     NAPI_CALL(env, napi_define_class(env, className.c_str(), className.length(), BufferConstructor,
1047                                      nullptr, sizeof(bufferDesc) / sizeof(bufferDesc[0]), bufferDesc, &bufferClass));
1048     napi_property_descriptor desc[] = {
1049         DECLARE_NAPI_PROPERTY("Buffer", bufferClass),
1050     };
1051     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1052     return exports;
1053 }
1054 
BlobInit(napi_env env,napi_value exports)1055 static napi_value BlobInit(napi_env env, napi_value exports)
1056 {
1057     string className = "Blob";
1058     napi_value blobClass = nullptr;
1059     napi_property_descriptor blobDesc[] = {
1060         DECLARE_NAPI_FUNCTION("arraybuffer", ArrayBufferAsync),
1061         DECLARE_NAPI_FUNCTION("text", TextAsync),
1062         DECLARE_NAPI_FUNCTION("getBytes", GetBytes),
1063     };
1064     NAPI_CALL(env, napi_define_class(env, className.c_str(), className.length(), BlobConstructor,
1065                                      nullptr, sizeof(blobDesc) / sizeof(blobDesc[0]), blobDesc, &blobClass));
1066     napi_property_descriptor desc[] = {
1067         DECLARE_NAPI_PROPERTY("Blob", blobClass),
1068     };
1069     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1070     return exports;
1071 }
1072 
Init(napi_env env,napi_value exports)1073 static napi_value Init(napi_env env, napi_value exports)
1074 {
1075     napi_property_descriptor desc[] = {
1076         DECLARE_NAPI_FUNCTION("utf8ByteLength", Utf8ByteLength),
1077         DECLARE_NAPI_FUNCTION("utf8StringToNumbers", Utf8StringToNumbers),
1078     };
1079     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1080     BufferInit(env, exports);
1081     BlobInit(env, exports);
1082     return exports;
1083 }
1084 
1085 extern "C"
NAPI_buffer_GetJSCode(const char ** buf,int * bufLen)1086 __attribute__((visibility("default"))) void NAPI_buffer_GetJSCode(const char **buf, int *bufLen)
1087 {
1088     if (buf != nullptr) {
1089         *buf = _binary_js_buffer_js_start;
1090     }
1091 
1092     if (bufLen != nullptr) {
1093         *bufLen = _binary_js_buffer_js_end - _binary_js_buffer_js_start;
1094     }
1095 }
1096 
1097 extern "C"
NAPI_buffer_GetABCCode(const char ** buf,int * buflen)1098 __attribute__((visibility("default"))) void NAPI_buffer_GetABCCode(const char** buf, int* buflen)
1099 {
1100     if (buf != nullptr) {
1101         *buf = _binary_buffer_abc_start;
1102     }
1103     if (buflen != nullptr) {
1104         *buflen = _binary_buffer_abc_end - _binary_buffer_abc_start;
1105     }
1106 }
1107 
1108 static napi_module bufferModule = {
1109     .nm_version = 1,
1110     .nm_flags = 0,
1111     .nm_filename = nullptr,
1112     .nm_register_func = Init,
1113     .nm_modname = "buffer",
1114     .nm_priv = reinterpret_cast<void *>(0),
1115     .reserved = {0},
1116 };
1117 
RegisterModule()1118 extern "C" __attribute__((constructor)) void RegisterModule()
1119 {
1120     napi_module_register(&bufferModule);
1121 }
1122 } // namespace OHOS::Buffer
1123