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     MemLevelInit(env, exports);
43     ReturnStatusInit(env, exports);
44     OffsetReferencePointInit(env, exports);
45     ErrorCodeInit(env, exports);
46     ZlibInit(env, exports);
47     std::vector<std::unique_ptr<NapiExporter>> products;
48     products.emplace_back(std::make_unique<PropNExporter>(env, exports));
49     products.emplace_back(std::make_unique<ChecksumNExporter>(env, exports));
50     products.emplace_back(std::make_unique<ZipNExporter>(env, exports));
51     products.emplace_back(std::make_unique<GZipNExporter>(env, exports));
52     for (auto &&product : products) {
53 #ifdef WIN_PLATFORM
54         std::string nExporterName = product->GetNExporterName();
55 #else
56         std::string nExporterName = product->GetClassName();
57 #endif
58         if (!product->Export()) {
59             APP_LOGE("Failed to export class %{public}s for module fileio", nExporterName.c_str());
60             return nullptr;
61         }
62     }
63     return exports;
64 }
65 EXTERN_C_END
66 
67 /*
68  * The module definition.
69  */
70 static napi_module _module = {
71     .nm_version = 1,
72     .nm_flags = 0,
73     .nm_filename = nullptr,
74     .nm_register_func = Init,
75     .nm_modname = "zlib",
76     .nm_priv = ((void *)0),
77     .reserved = {0}
78 };
79 
80 /*
81  * The module registration.
82  */
RegisterModule(void)83 extern "C" __attribute__((constructor)) void RegisterModule(void)
84 {
85     napi_module_register(&_module);
86 }
87 }  // namespace LIBZIP
88 }  // namespace AppExecFwk
89 }  // namespace OHOS
90