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