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