• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "hashstream_ani.h"
17 
18 #include "error_handler.h"
19 #include "filemgmt_libhilog.h"
20 #include "hs_hashstream.h"
21 #include "type_converter.h"
22 
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 namespace ANI {
27 namespace fs = std::filesystem;
28 using namespace std;
29 using namespace OHOS::FileManagement::ModuleFileIO;
30 
Unwrap(ani_env * env,ani_object object)31 HsHashStream *Unwrap(ani_env *env, ani_object object)
32 {
33     ani_long nativePtr;
34     auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr);
35     if (ret != ANI_OK) {
36         HILOGE("Unwrap hashstream err: %{public}d", ret);
37         return nullptr;
38     }
39 
40     uintptr_t ptrValue = static_cast<uintptr_t>(nativePtr);
41     HsHashStream *hashStream = reinterpret_cast<HsHashStream *>(ptrValue);
42     return hashStream;
43 }
44 
Update(ani_env * env,ani_object object,ani_arraybuffer buffer)45 void HashStreamAni::Update(ani_env *env, [[maybe_unused]] ani_object object, ani_arraybuffer buffer)
46 {
47     auto hashStream = Unwrap(env, object);
48     if (hashStream == nullptr) {
49         HILOGE("Cannot unwrap hashStream!");
50         ErrorHandler::Throw(env, EINVAL);
51         return;
52     }
53 
54     auto [succ, arrayBuffer] = TypeConverter::ToArrayBuffer(env, buffer);
55     if (!succ) {
56         HILOGE("illegal array buffer");
57         ErrorHandler::Throw(env, EINVAL);
58         return;
59     }
60 
61     auto ret = hashStream->Update(arrayBuffer);
62     if (!ret.IsSuccess()) {
63         HILOGE("Cannot Update!");
64         const auto &err = ret.GetError();
65         ErrorHandler::Throw(env, err);
66         return;
67     }
68 }
69 
Digest(ani_env * env,ani_object object)70 ani_string HashStreamAni::Digest(ani_env *env, [[maybe_unused]] ani_object object)
71 {
72     auto hashStream = Unwrap(env, object);
73     if (hashStream == nullptr) {
74         HILOGE("Cannot unwrap hashStream!");
75         ErrorHandler::Throw(env, EINVAL);
76         return nullptr;
77     }
78 
79     auto ret = hashStream->Digest();
80     if (!ret.IsSuccess()) {
81         HILOGE("Cannot Digest!");
82         const auto &err = ret.GetError();
83         ErrorHandler::Throw(env, err);
84         return nullptr;
85     }
86 
87     const auto &res = ret.GetData().value();
88     auto [succ, result] = TypeConverter::ToAniString(env, res);
89     if (!succ) {
90         HILOGE("Convert result to ani string failed");
91         ErrorHandler::Throw(env, UNKNOWN_ERR);
92         return nullptr;
93     }
94     return result;
95 }
96 
Constructor(ani_env * env,ani_object obj,ani_string alg)97 void HashStreamAni::Constructor(ani_env *env, ani_object obj, ani_string alg)
98 {
99     auto [succ, algorithm] = TypeConverter::ToUTF8String(env, alg);
100     if (!succ) {
101         HILOGE("Invalid alg");
102         ErrorHandler::Throw(env, EINVAL);
103         return;
104     }
105 
106     auto ret = HsHashStream::Constructor(algorithm);
107     if (!ret.IsSuccess()) {
108         HILOGE("Constructor failed");
109         const auto &err = ret.GetError();
110         ErrorHandler::Throw(env, err);
111         return;
112     }
113 
114     if (ANI_OK !=
115         env->Object_SetFieldByName_Long(obj, "nativePtr", reinterpret_cast<ani_long>(ret.GetData().value()))) {
116         HILOGE("Failed to wrap entity for obj HashStream");
117         ErrorHandler::Throw(env, EIO);
118         return;
119     }
120 }
121 
122 } // namespace ANI
123 } // namespace ModuleFileIO
124 } // namespace FileManagement
125 } // namespace OHOS