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 #include <cstdio>
16 #include <cstring>
17 #include <memory>
18 #include <unistd.h>
19
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "napi_zlib.h"
23 #include "class_checksum/checksum_n_exporter.h"
24 #include "class_zip/zip_n_exporter.h"
25 #include "class_gzip/gzip_n_exporter.h"
26 #include "properties/prop_n_exporter.h"
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace LIBZIP {
30
31 EXTERN_C_START
32 /*
33 * The module initialization.
34 */
Init(napi_env env,napi_value exports)35 static napi_value Init(napi_env env, napi_value exports)
36 {
37 FlushTypeInit(env, exports);
38 CompressLevelInit(env, exports);
39 CompressFlushModeInit(env, exports);
40 CompressMethodInit(env, exports);
41 CompressStrategyInit(env, exports);
42 ParallelStrategyInit(env, exports);
43 MemLevelInit(env, exports);
44 ReturnStatusInit(env, exports);
45 OffsetReferencePointInit(env, exports);
46 ErrorCodeInit(env, exports);
47 ZlibInit(env, exports);
48 std::vector<std::unique_ptr<NapiExporter>> products;
49 products.emplace_back(std::make_unique<PropNExporter>(env, exports));
50 products.emplace_back(std::make_unique<ChecksumNExporter>(env, exports));
51 products.emplace_back(std::make_unique<ZipNExporter>(env, exports));
52 products.emplace_back(std::make_unique<GZipNExporter>(env, exports));
53 for (auto &&product : products) {
54 #ifdef WIN_PLATFORM
55 std::string nExporterName = product->GetNExporterName();
56 #else
57 std::string nExporterName = product->GetClassName();
58 #endif
59 if (!product->Export()) {
60 APP_LOGE("Failed to export class %{public}s for module fileio", nExporterName.c_str());
61 return nullptr;
62 }
63 }
64 return exports;
65 }
66 EXTERN_C_END
67
68 /*
69 * The module definition.
70 */
71 static napi_module _module = {
72 .nm_version = 1,
73 .nm_flags = 0,
74 .nm_filename = nullptr,
75 .nm_register_func = Init,
76 .nm_modname = "zlib",
77 .nm_priv = ((void *)0),
78 .reserved = {0}
79 };
80
81 /*
82 * The module registration.
83 */
RegisterModule(void)84 extern "C" __attribute__((constructor)) void RegisterModule(void)
85 {
86 napi_module_register(&_module);
87 }
88 } // namespace LIBZIP
89 } // namespace AppExecFwk
90 } // namespace OHOS
91