• 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 "hash.h"
17 
18 #include <cstring>
19 #include <string_view>
20 #include <tuple>
21 
22 #include "filemgmt_libhilog.h"
23 #include "hash_file.h"
24 
25 namespace OHOS {
26 namespace FileManagement {
27 namespace ModuleFileIO {
28 using namespace std;
29 using namespace OHOS::FileManagement::LibN;
30 
GetHashAlgorithm(const string & alg)31 static HASH_ALGORITHM_TYPE GetHashAlgorithm(const string &alg)
32 {
33     return (algorithmMaps.find(alg) != algorithmMaps.end()) ? algorithmMaps.at(alg) : HASH_ALGORITHM_TYPE_UNSUPPORTED;
34 }
35 
GetHashArgs(napi_env env,const NFuncArg & funcArg)36 static tuple<bool, unique_ptr<char[]>, HASH_ALGORITHM_TYPE, bool> GetHashArgs(napi_env env, const NFuncArg &funcArg)
37 {
38     bool isPromise = false;
39     auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
40     if (!resGetFirstArg) {
41         HILOGE("Invalid path");
42         NError(EINVAL).ThrowErr(env);
43         return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise };
44     }
45 
46     auto [resGetSecondArg, alg, ignore] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String();
47     if (!resGetSecondArg) {
48         HILOGE("Invalid algorithm");
49         NError(EINVAL).ThrowErr(env);
50         return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise };
51     }
52 
53     HASH_ALGORITHM_TYPE algType = GetHashAlgorithm(alg.get());
54     if (algType == HASH_ALGORITHM_TYPE_UNSUPPORTED) {
55         HILOGE("Invalid algorithm");
56         NError(EINVAL).ThrowErr(env);
57         return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise };
58     }
59 
60     if (funcArg.GetArgc() == NARG_CNT::THREE && !NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_function)) {
61         HILOGE("Invalid callback");
62         NError(EINVAL).ThrowErr(env);
63         return { false, nullptr, HASH_ALGORITHM_TYPE_UNSUPPORTED, isPromise };
64     }
65 
66     isPromise = funcArg.GetArgc() == NARG_CNT::TWO;
67     return { true, move(path), algType, isPromise };
68 }
69 
Async(napi_env env,napi_callback_info info)70 napi_value Hash::Async(napi_env env, napi_callback_info info)
71 {
72     NFuncArg funcArg(env, info);
73     if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
74         HILOGE("Number of arguments unmatched");
75         NError(EINVAL).ThrowErr(env);
76         return nullptr;
77     }
78 
79     auto [succ, fpath, algType, isPromise] = GetHashArgs(env, funcArg);
80     if (!succ) {
81         HILOGE("Failed to get hash args");
82         return nullptr;
83     }
84 
85     auto arg = make_shared<string>();
86     auto cbExec = [fpath = string(fpath.release()), arg, algType = algType, env = env]() -> NError {
87         int ret = EIO;
88         string &res = *arg;
89         if (algType == HASH_ALGORITHM_TYPE_MD5) {
90             tie(ret, res) = DistributedFS::HashFile::HashWithMD5(fpath);
91         } else if (algType == HASH_ALGORITHM_TYPE_SHA1) {
92             tie(ret, res) = DistributedFS::HashFile::HashWithSHA1(fpath);
93         } else if (algType == HASH_ALGORITHM_TYPE_SHA256) {
94             tie(ret, res) = DistributedFS::HashFile::HashWithSHA256(fpath);
95         }
96         return NError(ret);
97     };
98 
99     auto cbComplete = [arg](napi_env env, NError err) -> NVal {
100         if (err) {
101             return { NVal(env, err.GetNapiErr(env)) };
102         }
103 
104         return { NVal::CreateUTF8String(env, *arg) };
105     };
106 
107     NVal thisVar(env, funcArg.GetThisVar());
108     if (isPromise) {
109         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_HASH_NAME, cbExec, cbComplete).val_;
110     } else {
111         NVal cb(env, funcArg[NARG_POS::THIRD]);
112         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_HASH_NAME, cbExec, cbComplete).val_;
113     }
114 }
115 
Export()116 bool HashNExporter::Export()
117 {
118     return exports_.AddProp({
119         NVal::DeclareNapiFunction("hash", Hash::Async),
120     });
121 }
122 
GetClassName()123 string HashNExporter::GetClassName()
124 {
125     return HashNExporter::className_;
126 }
127 
HashNExporter(napi_env env,napi_value exports)128 HashNExporter::HashNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
129 
130 } // namespace ModuleFileIO
131 } // namespace FileManagement
132 } // namespace OHOS