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 #ifndef FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_UTILS_H
16 #define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_UTILS_H
17 #include <chrono>
18 #include <ctime>
19 #include <ratio>
20 #include <string>
21 #include <errno.h>
22 #include <stddef.h>
23 #include <functional>
24 
25 #include "event_handler.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace LIBZIP {
30 
31 using CALLBACK = std::function<void(int)>;
32 
33 #if !defined(DISALLOW_ASSIGN)
34 // Put this in the declarations for a class to be unassignable.
35 #define DISALLOW_ASSIGN(TypeName) void operator=(const TypeName &) = delete
36 #endif
37 
38 // A macro to disallow the copy constructor and operator= functions
39 // This should be used in the private: declarations for a class
40 // We define this macro conditionally as it may be defined by another libraries.
41 #if !defined(DISALLOW_COPY_AND_ASSIGN)
42 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
43     TypeName(const TypeName &);            \
44     void operator = (const TypeName &)
45 #endif
46 
47 using PlatformFile = int;
48 using char16 = wchar_t;
49 using string16 = std::wstring;
50 
51 // zip Error
52 enum ErrorCode {
53     ERROR_CODE_OK = 0,
54     ERROR_CODE_STREAM_END = 1,
55     ERROR_CODE_NEED_DICT = 2,
56     ERROR_CODE_ERRNO = -1,
57     ERROR_CODE_STREAM_ERROR = -2,
58     ERROR_CODE_DATA_ERROR = -3,
59     ERROR_CODE_MEM_ERROR = -4,
60     ERROR_CODE_BUF_ERROR = -5,
61     ERROR_CODE_VERSION_ERROR = -6
62 };
63 
64 // Constant definitions related to zlib Library
65 enum FlushType {
66     FLUSH_TYPE_NO_FLUSH = 0,
67     FLUSH_TYPE_PARTIAL_FLUSH = 1,
68     FLUSH_TYPE_SYNC_FLUSH = 2,
69     FLUSH_TYPE_FULL_FLUSH = 3,
70     FLUSH_TYPE_FINISH = 4,
71     FLUSH_TYPE_BLOCK = 5,
72     FLUSH_TYPE_TREES = 6
73 };
74 using FLUSH_TYPE = enum FlushType;
75 
76 // Compress Level
77 enum CompressLevel {
78     COMPRESS_LEVEL_NO_COMPRESSION = 0,
79     COMPRESS_LEVEL_BEST_SPEED = 1,
80     COMPRESS_LEVEL_BEST_COMPRESSION = 9,
81     COMPRESS_LEVEL_DEFAULT_COMPRESSION = -1
82 };
83 using COMPRESS_LEVEL = enum CompressLevel;
84 
85 // Compress Strategy
86 enum CompressStrategy {
87     COMPRESS_STRATEGY_DEFAULT_STRATEGY = 0,
88     COMPRESS_STRATEGY_FILTERED = 1,
89     COMPRESS_STRATEGY_HUFFMAN_ONLY = 2,
90     COMPRESS_STRATEGY_RLE = 3,
91     COMPRESS_STRATEGY_FIXED = 4
92 };
93 using COMPRESS_STRATEGY = enum CompressStrategy;
94 
95 // Memory Level
96 enum MemoryLevel { MEM_LEVEL_MIN_MEMLEVEL = 1, MEM_LEVEL_DEFAULT_MEMLEVEL = 8, MEM_LEVEL_MAX_MEMLEVEL = 9 };
97 using MEMORY_LEVEL = enum MemoryLevel;
98 
99 // Compression Options
100 struct Options {
101     FLUSH_TYPE flush;
102     FLUSH_TYPE finishFlush;
103     int chunkSize;          // Buffer size, > 64
104     COMPRESS_LEVEL level;   // Compression level. The compression level is a number from 0 to 9. 0 has the fastest
105                             // compression speed (compression process), 9 has the slowest compression speed, the largest
106                             // compression rate, and 0 does not compress
107     MEMORY_LEVEL memLevel;  // Internal compression status, how much memory should be allocated
108     COMPRESS_STRATEGY strategy;  // CompressStrategy
109 
110     // default constructor
OptionsOptions111     Options()
112     {
113         CompressDefaultValue();
114     }
115 
116     // compression default options
CompressDefaultValueOptions117     void CompressDefaultValue()
118     {
119         flush = FLUSH_TYPE_NO_FLUSH;
120         finishFlush = FLUSH_TYPE_SYNC_FLUSH;
121         chunkSize = 128;
122         level = COMPRESS_LEVEL_DEFAULT_COMPRESSION;
123         memLevel = MEM_LEVEL_DEFAULT_MEMLEVEL;
124         strategy = COMPRESS_STRATEGY_DEFAULT_STRATEGY;
125     }
126 };
127 using OPTIONS = struct Options;
128 
129 constexpr PlatformFile kInvalidPlatformFile = -1;
130 
131 struct tm *GetCurrentSystemTime(void);
132 bool StartsWith(const std::string &str, const std::string &searchFor);
133 bool EndsWith(const std::string &str, const std::string &searchFor);
134 void PostTask(const OHOS::AppExecFwk::InnerEvent::Callback &callback);
135 bool FilePathCheckValid(const std::string &str);
136 }  // namespace LIBZIP
137 }  // namespace AppExecFwk
138 }  // namespace OHOS
139 
140 #endif  // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_UTILS_H