• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 "jsi_utils.h"
17 #include "jsi.h"
18 #include "jsi_types.h"
19 #include "memory.h"
20 #include "securec.h"
21 #include "utils.h"
22 #include "log.h"
23 
24 namespace OHOS {
25 namespace ACELite {
26 
ParseUint8ArrayToBlob(JSIValue value,HcfBlob * blob)27 HcfResult ParseUint8ArrayToBlob(JSIValue value, HcfBlob *blob)
28 {
29     if (!JSI::ValueIsTypedArray(value) || (blob == nullptr)) {
30         LOGE("value is not a typed array!");
31         return HCF_INVALID_PARAMS;
32     }
33     TypedArrayType arrayType;
34     size_t arraySize = 0;
35     size_t byteOffset = 0;
36     JSIValue arrayBuffer = nullptr;
37     HcfResult ret = HCF_SUCCESS;
38     do {
39         uint8_t *dataArray = JSI::GetTypedArrayInfo(value, arrayType, arraySize, arrayBuffer, byteOffset);
40         if (dataArray == nullptr) {
41             ret = HCF_ERR_CRYPTO_OPERATION;
42             break;
43         }
44         if (arrayType != TypedArrayType::JSI_UINT8_ARRAY) {
45             LOGE("value is not a uint8 array");
46             ret = HCF_INVALID_PARAMS;
47             break;
48         }
49         blob->data = reinterpret_cast<uint8_t *>(HcfMalloc(arraySize, 0));
50         if (blob->data == nullptr) {
51             ret = HCF_ERR_MALLOC;
52             break;
53         }
54         memcpy_s(blob->data, arraySize, dataArray + byteOffset, arraySize);
55         blob->len = arraySize;
56     } while (0);
57     if (arrayBuffer != nullptr) {
58         JSI::ReleaseValue(arrayBuffer);
59         arrayBuffer = nullptr;
60     }
61     return ret;
62 }
63 
ConstructJSIReturnResult(const HcfBlob * blob)64 JSIValue ConstructJSIReturnResult(const HcfBlob *blob)
65 {
66     JSIValue res;
67     do {
68         res = JSI::CreateObject();
69         if (res == nullptr) {
70             break;
71         }
72         if (blob->data != nullptr) {
73             uint8_t *arrayBuffer = nullptr;
74             JSIValue buffer = JSI::CreateArrayBuffer(blob->len, arrayBuffer);
75             if (arrayBuffer == nullptr) {
76                 LOGE("create jsi array buffer failed");
77                 JSI::ReleaseValue(buffer);
78                 return res;
79             }
80             (void)memcpy_s(arrayBuffer, blob->len, blob->data, blob->len);
81             JSIValue typedArray = JSI::CreateTypedArray(TypedArrayType::JSI_UINT8_ARRAY, blob->len, buffer, 0);
82             JSI::ReleaseValue(buffer);
83             JSI::SetNamedProperty(res, "data", typedArray);
84         }
85     } while (0);
86     return res;
87 }
88 
89 } // namespace ACELite
90 } // namespace OHOS
91