• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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