1 /*
2 * Copyright (C) 2025-2025 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 "anonymize_adapter.h"
17
18 #include "telephony_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Telephony {
AnonymizeAdapter()22 AnonymizeAdapter::AnonymizeAdapter()
23 {}
24
~AnonymizeAdapter()25 AnonymizeAdapter::~AnonymizeAdapter()
26 {}
27
GetLibAnonymize()28 void *AnonymizeAdapter::GetLibAnonymize()
29 {
30 if (libAnonymize_ != nullptr) {
31 return libAnonymize_;
32 }
33
34 libAnonymize_ = dlopen("libdia_sdk.z.so", RTLD_LAZY);
35 if (libAnonymize_ == nullptr) {
36 TELEPHONY_LOGE("libAnonymize_ is null");
37 }
38
39 return libAnonymize_;
40 }
41
ReleaseLibAnonymize()42 void AnonymizeAdapter::ReleaseLibAnonymize()
43 {
44 TELEPHONY_LOGI("ReleaseLibAnonymize");
45 if (libAnonymize_ != nullptr) {
46 dlclose(libAnonymize_);
47 libAnonymize_ = nullptr;
48 }
49 }
50
InitConfig(void ** config)51 int AnonymizeAdapter::InitConfig(void **config)
52 {
53 libAnonymize_ = GetLibAnonymize();
54 if (libAnonymize_ == nullptr) {
55 return -1;
56 }
57
58 PfnInitConfig func = reinterpret_cast<PfnInitConfig>(dlsym(libAnonymize_, "DIA_InitConfig"));
59 if (func == nullptr) {
60 TELEPHONY_LOGE("func is null");
61 return -1;
62 }
63
64 return func(config);
65 }
66
SetRule(void * config,const DIA_String * key,const DIA_Rule * value)67 int AnonymizeAdapter::SetRule(void *config, const DIA_String *key, const DIA_Rule *value)
68 {
69 libAnonymize_ = GetLibAnonymize();
70 if (libAnonymize_ == nullptr) {
71 return -1;
72 }
73
74 PfnSetRule func = reinterpret_cast<PfnSetRule>(dlsym(libAnonymize_, "DIA_SetRule"));
75 if (func == nullptr) {
76 TELEPHONY_LOGE("func is null");
77 return -1;
78 }
79
80 return func(config, key, value);
81 }
82
CreateAnonymize(void * config,void ** anonymize)83 int AnonymizeAdapter::CreateAnonymize(void *config, void **anonymize)
84 {
85 libAnonymize_ = GetLibAnonymize();
86 if (libAnonymize_ == nullptr) {
87 return -1;
88 }
89
90 PfnCreateAnonymize func = reinterpret_cast<PfnCreateAnonymize>(dlsym(libAnonymize_, "DIA_CreateAnonymize"));
91 if (func == nullptr) {
92 TELEPHONY_LOGE("func is null");
93 return -1;
94 }
95
96 return func(config, anonymize);
97 }
98
IdentifyAnonymize(void * anonymize,const DIA_String * input,DIA_String ** output)99 int AnonymizeAdapter::IdentifyAnonymize(void *anonymize, const DIA_String *input, DIA_String **output)
100 {
101 libAnonymize_ = GetLibAnonymize();
102 if (libAnonymize_ == nullptr) {
103 return -1;
104 }
105
106 PfnIdentifyAnonymize func = reinterpret_cast<PfnIdentifyAnonymize>(dlsym(libAnonymize_, "DIA_IdentifyAnonymize"));
107 if (func == nullptr) {
108 TELEPHONY_LOGE("func is null");
109 return -1;
110 }
111
112 return func(anonymize, input, output);
113 }
114
ReleaseConfig(void ** config)115 int AnonymizeAdapter::ReleaseConfig(void **config)
116 {
117 libAnonymize_ = GetLibAnonymize();
118 if (libAnonymize_ == nullptr) {
119 return -1;
120 }
121
122 PfnReleaseConfig func = reinterpret_cast<PfnReleaseConfig>(dlsym(libAnonymize_, "DIA_ReleaseConfig"));
123 if (func == nullptr) {
124 TELEPHONY_LOGE("func is null");
125 return -1;
126 }
127
128 return func(config);
129 }
130
ReleaseAnonymize(void ** anonymize)131 int AnonymizeAdapter::ReleaseAnonymize(void **anonymize)
132 {
133 libAnonymize_ = GetLibAnonymize();
134 if (libAnonymize_ == nullptr) {
135 return -1;
136 }
137
138 PfnReleaseAonoymize func = reinterpret_cast<PfnReleaseAonoymize>(dlsym(libAnonymize_, "DIA_ReleaseAnonymize"));
139 if (func == nullptr) {
140 TELEPHONY_LOGE("func is null");
141 return -1;
142 }
143
144 return func(anonymize);
145 }
146
ReleaseOutputData(DIA_String ** output)147 int AnonymizeAdapter::ReleaseOutputData(DIA_String **output)
148 {
149 libAnonymize_ = GetLibAnonymize();
150 if (libAnonymize_ == nullptr) {
151 return -1;
152 }
153
154 PfnReleaseOutputData func = reinterpret_cast<PfnReleaseOutputData>(dlsym(libAnonymize_, "DIA_ReleaseOutputData"));
155 if (func == nullptr) {
156 TELEPHONY_LOGE("func is null");
157 return -1;
158 }
159
160 return func(output);
161 }
162 }
163 }