1 /*
2  * Copyright (c) 2024 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 #ifndef INTERFACES_KITS_JS_ZIP_NAPI_COMMON_COMMON_FUNC_H
17 #define INTERFACES_KITS_JS_ZIP_NAPI_COMMON_COMMON_FUNC_H
18 
19 #include "napi_func_arg.h"
20 #include "napi_value.h"
21 #include "uv.h"
22 #include "zlib.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace LIBZIP {
27 
28 struct HasZStreamMember {
29     bool hasNextIn = false;
30     bool hasAvailIn = false;
31     bool hasTotalIn = false;
32     bool hasNextOut = false;
33     bool hasAvailOut = false;
34     bool hasTotalOut = false;
35     bool hasDataType = false;
36     bool hasAdler = false;
37 };
38 
39 struct HasGZFileMember {
40     bool hasHave = false;
41     bool hasNext = false;
42     bool hasPos = false;
43 };
44 
45 struct NapiParam {
46     napi_valuetype type;
47     std::string val;
48 };
49 
50 struct CommonFunc {
51     static bool SetZStreamValue(napi_env env, const NapiFuncArg &funcArg);
52     static std::tuple<bool, int64_t, void *, size_t> GetAdler32Arg(napi_env env, const NapiFuncArg &funcArg);
53     static std::tuple<bool, unsigned long, unsigned long, int64_t> GetAdler32CombineArg(
54         napi_env env, const NapiFuncArg &funcArg);
55     static std::tuple<bool, int64_t, void *, size_t> GetCrc64Arg(napi_env env, const NapiFuncArg &funcArg);
56     static std::tuple<bool, z_stream, HasZStreamMember> GetZstreamArg(napi_env env, napi_value zstream);
57     static std::tuple<bool, z_stream, int32_t> GetInflateInitArg(napi_env env, const NapiFuncArg &funcArg);
58     static std::tuple<bool, z_stream, int32_t> GetDeflateInitArg(napi_env env, const NapiFuncArg &funcArg);
59     static std::tuple<bool, z_stream, int32_t, int32_t, int32_t, int32_t, int32_t> GetDeflateInit2Arg(
60         napi_env env, const NapiFuncArg &funcArg);
61     static std::tuple<bool, int32_t> GetDeflateArg(napi_env env, const NapiFuncArg &funcArg);
62     static std::tuple<bool, void *, size_t, void *, int64_t> GetCompressArg(napi_env env, const NapiFuncArg &funcArg);
63     static std::tuple<bool, void *, size_t, void *, size_t, int32_t> GetCompress2Arg(
64         napi_env env, const NapiFuncArg &funcArg);
65     static std::tuple<bool, void*, size_t, void*, int64_t> GetUnCompressArg(napi_env env, const NapiFuncArg& funcArg);
66     static std::tuple<bool, int32_t> GetZErrorArg(napi_env env, const NapiFuncArg &funcArg);
67     static std::tuple<bool, void *, size_t> GetInflateSetDictionaryArg(napi_env env, const NapiFuncArg &funcArg);
68     static std::tuple<bool, int32_t> GetInflateArg(napi_env env, const NapiFuncArg &funcArg);
69     static std::tuple<bool, int32_t> GetInflateReset2Arg(napi_env env, const NapiFuncArg &funcArg);
70     static std::tuple<bool, unsigned long, void*, size_t> GetInflateBackInitArg(
71         napi_env env, const NapiFuncArg& funcArg);
72     static std::tuple<bool, unsigned long, void *, size_t> GetInflateBackArg(napi_env env, const NapiFuncArg &funcArg);
73     static std::tuple<bool, int32_t, int32_t> GetInflatePrimeArg(napi_env env, const NapiFuncArg &funcArg);
74     static std::tuple<bool, int32_t> GetInflateValidateArg(napi_env env, const NapiFuncArg &funcArg);
75     static std::tuple<bool, gz_header> GetInflateGetHeaderArg(napi_env env, const NapiFuncArg &funcArg);
76     static std::tuple<bool, gz_header> GetGZHeaderArg(napi_env env, napi_value argGZheader);
77     static std::tuple<bool, int32_t, int32_t> UnwrapTwoIntParams(napi_env env, const NapiFuncArg &funcArg);
78     static std::tuple<bool, int32_t> UnwrapInt32Params(napi_env env, napi_value value);
79     static std::tuple<bool, uint32_t> UnwrapInt64Params(napi_env env, const NapiFuncArg &funcArg);
80     static std::tuple<bool, int32_t, int32_t, int32_t, int32_t> UnwrapDeflateTuneParams(
81         napi_env env, const NapiFuncArg &funcArg);
82     static std::tuple<bool, void *, size_t> UnwrapArrayBufferParams(napi_env env, const NapiFuncArg &funcArg);
83 
84     static std::tuple<bool, void *, size_t> GzipUnwrapArrayBufferParams(napi_env env, const NapiFuncArg &funcArg);
85     static std::tuple<bool, std::unique_ptr<char[]>, std::unique_ptr<char[]>> GetGZOpenArg(
86         napi_env env, const NapiFuncArg &funcArg);
87     static std::tuple<bool, int32_t, std::unique_ptr<char[]>> GetGZDOpenArg(napi_env env, const NapiFuncArg &funcArg);
88     static std::tuple<bool, uint32_t> GetGZBufferArg(napi_env env, const NapiFuncArg &funcArg);
89     static std::tuple<bool, gzFile_s, HasGZFileMember> GetGZFileArg(napi_env env, napi_value argGZFile);
90     static std::tuple<bool, void *, uint32_t> GetGZReadArg(napi_env env, const NapiFuncArg &funcArg);
91     static std::tuple<bool, void *, int64_t, int64_t> GetGZFReadArg(napi_env env, const NapiFuncArg &funcArg);
92     static std::tuple<bool, void *, int64_t> GetGZWriteArg(napi_env env, const NapiFuncArg &funcArg);
93     static std::tuple<bool, void *, int64_t, int64_t> GetGZFWriteArg(napi_env env, const NapiFuncArg &funcArg);
94     static std::tuple<bool, int32_t> GetGZPutCArg(napi_env env, const NapiFuncArg &funcArg);
95     static std::tuple<bool, std::unique_ptr<char[]>> GetGZPutSArg(napi_env env, const NapiFuncArg &funcArg);
96     static std::tuple<bool, int32_t, int32_t> GetGzSetParamsArg(napi_env env, const NapiFuncArg &funcArg);
97     static void GetLogContent(
98         std::string& formatStr, const std::vector<NapiParam>& params, std::string& ret, uint32_t& pos);
99     static void ParseLogContent(std::string& formatStr, std::vector<NapiParam>& params, std::string& logContent);
100     static void ParseNapiValue(napi_env env, napi_value element, std::vector<NapiParam>& params);
101     static bool ParseNapiValueFromArray(napi_env env, std::vector<NapiParam>& params, const NapiFuncArg& funcArg);
102     static std::tuple<bool, std::unique_ptr<char[]>, std::unique_ptr<char[]>> GetGZPrintFArg(
103         napi_env env, const NapiFuncArg& funcArg);
104     static std::tuple<bool, void *, size_t> GetGZGetSArg(napi_env env, const NapiFuncArg &funcArg);
105     static std::tuple<bool, int64_t, int32_t> GetGZSeekArg(napi_env env, const NapiFuncArg &funcArg);
106     static std::tuple<bool, int32_t> GetGZUnGetCArg(napi_env env, const NapiFuncArg &funcArg);
107     static std::tuple<bool, uint32_t> SetGZFlushArg(napi_env env, const NapiFuncArg &funcArg);
108 };
109 }  // namespace LIBZIP
110 }  // namespace AppExecFwk
111 }  // namespace OHOS
112 #endif  // INTERFACES_KITS_JS_ZIP_NAPI_COMMON_COMMON_FUNC_H