1 /*
2  * Copyright (c) 2021 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 "huks_napi_get_sdk_version.h"
17 
18 #include "hks_api.h"
19 #include "hks_log.h"
20 #include "hks_mem.h"
21 #include "hks_param.h"
22 #include "hks_type.h"
23 
24 namespace HuksNapi {
25 namespace {
26 constexpr uint32_t MAX_SDK_VERSION_SIZE = 64;
27 }
28 
HuksNapiGetSdkVersion(napi_env env,napi_callback_info info)29 napi_value HuksNapiGetSdkVersion(napi_env env, napi_callback_info info)
30 {
31     HksBlob *sdkVersion = static_cast<HksBlob *>(HksMalloc(sizeof(HksBlob)));
32     if (sdkVersion == nullptr) {
33         return nullptr;
34     }
35 
36     sdkVersion->data = static_cast<uint8_t *>(HksMalloc(MAX_SDK_VERSION_SIZE));
37     if (sdkVersion->data == nullptr) {
38         HKS_FREE(sdkVersion);
39         return nullptr;
40     }
41 
42     sdkVersion->size = MAX_SDK_VERSION_SIZE;
43 
44     int32_t result = HksGetSdkVersion(sdkVersion);
45     if (result != HKS_SUCCESS) {
46         HKS_FREE(sdkVersion->data);
47         HKS_FREE(sdkVersion);
48         return nullptr;
49     }
50 
51     napi_value version = nullptr;
52     napi_create_string_latin1(env, reinterpret_cast<const char *>(sdkVersion->data), NAPI_AUTO_LENGTH, &version);
53     HKS_FREE(sdkVersion->data);
54     HKS_FREE(sdkVersion);
55     return version;
56 }
57 }  // namespace HuksNapi