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 
16 #ifndef OHOS_DM_APP_IMAGE_INFO_H
17 #define OHOS_DM_APP_IMAGE_INFO_H
18 
19 #include <cstdint>
20 
21 #include "securec.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
25 class DmAppImageInfo {
26 public:
27     DmAppImageInfo() = default;
28     /**
29      * @tc.name: DmAppImageInfo::DmAppImageInfo
30      * @tc.desc: Dm App Image Info Save Data
31      * @tc.type: FUNC
32      */
DmAppImageInfo(uint8_t * appIcon,int32_t appIconLen,uint8_t * appThumbnail,int32_t appThumbnailLen)33     explicit DmAppImageInfo(uint8_t *appIcon, int32_t appIconLen, uint8_t *appThumbnail, int32_t appThumbnailLen)
34     {
35         SaveData(appIcon, appIconLen, appThumbnail, appThumbnailLen);
36     }
37     /**
38      * @tc.name: DmAppImageInfo::Reset
39      * @tc.desc: Dm App Image Info Reset
40      * @tc.type: FUNC
41      */
Reset(uint8_t * appIcon,int32_t appIconLen,uint8_t * appThumbnail,int32_t appThumbnailLen)42     void Reset(uint8_t *appIcon, int32_t appIconLen, uint8_t *appThumbnail, int32_t appThumbnailLen)
43     {
44         SaveData(appIcon, appIconLen, appThumbnail, appThumbnailLen);
45     }
46     /**
47      * @tc.name: DmAppImageInfo::ResetIcon
48      * @tc.desc: Dm App Image Info ResetIcon
49      * @tc.type: FUNC
50      */
ResetIcon(uint8_t * appIcon,int32_t appIconLen)51     void ResetIcon(uint8_t *appIcon, int32_t appIconLen)
52     {
53         SaveIconData(appIcon, appIconLen);
54     }
55     /**
56      * @tc.name: DmAppImageInfo::InitThumbnail
57      * @tc.desc: Dm App Image Info Init Thumbnail
58      * @tc.type: FUNC
59      */
InitThumbnail(int32_t appThumbnailLen)60     void InitThumbnail(int32_t appThumbnailLen)
61     {
62         if (appThumbnailLen <= 0 || appThumbnailLen > THUMB_MAX_LEN) {
63             appThumbnailLen_ = 0;
64             appThumbnail_ = nullptr;
65             return;
66         }
67 
68         appThumbnail_ = new (std::nothrow) uint8_t[appThumbnailLen] { 0 };
69         if (appThumbnail_ != nullptr) {
70             appThumbnailLen_ = appThumbnailLen;
71         }
72     }
73     /**
74      * @tc.name: DmAppImageInfo::SetThumbnailData
75      * @tc.desc: Dm App Image Info Init Set Data of Thumbnail
76      * @tc.type: FUNC
77      */
SetThumbnailData(uint8_t * srcBuffer,int32_t srcBufferLen,int32_t copyIndex,int32_t copyLen)78     int32_t SetThumbnailData(uint8_t *srcBuffer, int32_t srcBufferLen, int32_t copyIndex, int32_t copyLen)
79     {
80         if (srcBuffer == nullptr || srcBufferLen <= 0 || copyLen > srcBufferLen || copyIndex < 0) {
81             return -1;
82         }
83 
84         if ((copyIndex + copyLen) > appThumbnailLen_) {
85             return -1;
86         }
87 
88         if (appThumbnail_ == nullptr) {
89             return -1;
90         }
91 
92         if (memcpy_s(appThumbnail_ + copyIndex, appThumbnailLen_ - copyIndex, srcBuffer,
93                      static_cast<uint32_t>(copyLen)) != 0) {
94             return -1;
95         }
96 
97         return 0;
98     }
99     /**
100      * @tc.name: DmAppImageInfo::~DmAppImageInfo
101      * @tc.desc: Dm App Image Info destructor
102      * @tc.type: FUNC
103      */
~DmAppImageInfo()104     ~DmAppImageInfo()
105     {
106         if (appIcon_ != nullptr) {
107             delete[] appIcon_;
108             appIcon_ = nullptr;
109         }
110         if (appThumbnail_ != nullptr) {
111             delete[] appThumbnail_;
112             appThumbnail_ = nullptr;
113         }
114     }
115     /**
116      * @tc.name: DmAppImageInfo::DmAppImageInfo
117      * @tc.desc: Dm App Image Info Constructor
118      * @tc.type: FUNC
119      */
DmAppImageInfo(const DmAppImageInfo & other)120     DmAppImageInfo(const DmAppImageInfo &other)
121     {
122         if (this != &other) {
123             *this = other;
124         }
125     }
126 
127     DmAppImageInfo &operator=(const DmAppImageInfo &other)
128     {
129         if (this != &other) {
130             SaveData(other.GetAppIcon(), other.GetAppIconLen(), other.GetAppThumbnail(), other.GetAppThumbnailLen());
131         }
132         return *this;
133     }
134 
135     DmAppImageInfo(DmAppImageInfo &&) = delete;
136     DmAppImageInfo &operator=(DmAppImageInfo &&) = delete;
137     /**
138      * @tc.name: DmAppImageInfo::GetAppIconLen
139      * @tc.desc: Dm App Image Info Get App Icon Len
140      * @tc.type: FUNC
141      */
GetAppIconLen()142     int32_t GetAppIconLen() const
143     {
144         return appIconLen_;
145     }
146 
GetAppIcon()147     const uint8_t *GetAppIcon() const
148     {
149         return appIcon_;
150     }
151     /**
152      * @tc.name: DmAppImageInfo::GetAppThumbnailLen
153      * @tc.desc: Dm App Image Info Get App ThumbnailLen
154      * @tc.type: FUNC
155      */
GetAppThumbnailLen()156     int32_t GetAppThumbnailLen() const
157     {
158         return appThumbnailLen_;
159     }
160     /**
161      * @tc.name: DmAppImageInfo::GetAppThumbnail
162      * @tc.desc: Dm App Image Info Get App Thumbnail
163      * @tc.type: FUNC
164      */
GetAppThumbnail()165     const uint8_t *GetAppThumbnail() const
166     {
167         return appThumbnail_;
168     }
169 
170 private:
SaveData(const uint8_t * appIcon,int32_t appIconLen,const uint8_t * appThumbnail,int32_t appThumbnailLen)171     void SaveData(const uint8_t *appIcon, int32_t appIconLen, const uint8_t *appThumbnail, int32_t appThumbnailLen)
172     {
173         SaveIconData(appIcon, appIconLen);
174         SaveThumbnailData(appThumbnail, appThumbnailLen);
175     }
176 
SaveIconData(const uint8_t * appIcon,int32_t appIconLen)177     void SaveIconData(const uint8_t *appIcon, int32_t appIconLen)
178     {
179         if (appIconLen > 0 && appIconLen < ICON_MAX_LEN && appIcon != nullptr) {
180             if (appIconLen_ < appIconLen) {
181                 if (appIcon_ != nullptr && appIconLen_ > 0) {
182                     delete[] appIcon_;
183                     appIcon_ = nullptr;
184                     appIconLen_ = 0;
185                 }
186                 appIcon_ = new (std::nothrow) uint8_t[appIconLen] { 0 };
187             }
188             if (appIcon_ != nullptr) {
189                 appIconLen_ = appIconLen;
190                 if (memcpy_s(appIcon_, static_cast<uint32_t>(appIconLen_), appIcon, appIconLen) != 0) {
191                     return;
192                 }
193             }
194         }
195     }
196 
SaveThumbnailData(const uint8_t * appThumbnail,int32_t appThumbnailLen)197     void SaveThumbnailData(const uint8_t *appThumbnail, int32_t appThumbnailLen)
198     {
199         if (appThumbnailLen > 0 && appThumbnailLen < THUMB_MAX_LEN && appThumbnail != nullptr) {
200             if (appThumbnailLen_ < appThumbnailLen) {
201                 if (appThumbnail_ != nullptr && appThumbnailLen_ > 0) {
202                     delete[] appThumbnail_;
203                     appThumbnail_ = nullptr;
204                     appThumbnailLen_ = 0;
205                 }
206                 appThumbnail_ = new (std::nothrow) uint8_t[appThumbnailLen] { 0 };
207             }
208             if (appThumbnail_ != nullptr) {
209                 appThumbnailLen_ = appThumbnailLen;
210                 if (memcpy_s(appThumbnail_, static_cast<uint32_t>(appThumbnailLen_), appThumbnail,
211                              appThumbnailLen) != 0) {
212                     return;
213                 }
214             }
215         }
216     }
217 
218 private:
219     int32_t appIconLen_ { 0 };
220     uint8_t *appIcon_ { nullptr };
221     int32_t appThumbnailLen_ { 0 };
222     uint8_t *appThumbnail_ { nullptr };
223     const int32_t ICON_MAX_LEN = 32 * 1024;
224     const int32_t THUMB_MAX_LEN = 153 * 1024;
225 };
226 } // namespace DistributedHardware
227 } // namespace OHOS
228 #endif // OHOS_DM_APP_IMAGE_INFO_H
229