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, (uint32_t)copyLen) != 0) { 93 return -1; 94 } 95 96 return 0; 97 } 98 /** 99 * @tc.name: DmAppImageInfo::~DmAppImageInfo 100 * @tc.desc: Dm App Image Info destructor 101 * @tc.type: FUNC 102 */ ~DmAppImageInfo()103 ~DmAppImageInfo() 104 { 105 if (appIcon != nullptr) { 106 delete[] appIcon; 107 appIcon = nullptr; 108 } 109 if (appThumbnail != nullptr) { 110 delete[] appThumbnail; 111 appThumbnail = nullptr; 112 } 113 } 114 /** 115 * @tc.name: DmAppImageInfo::DmAppImageInfo 116 * @tc.desc: Dm App Image Info Constructor 117 * @tc.type: FUNC 118 */ DmAppImageInfo(const DmAppImageInfo & other)119 DmAppImageInfo(const DmAppImageInfo &other) 120 { 121 if (this != &other) { 122 *this = other; 123 } 124 } 125 126 DmAppImageInfo &operator=(const DmAppImageInfo &other) 127 { 128 if (this != &other) { 129 SaveData(other.GetAppIcon(), other.GetAppIconLen(), other.GetAppThumbnail(), other.GetAppThumbnailLen()); 130 } 131 return *this; 132 } 133 134 DmAppImageInfo(DmAppImageInfo &&) = delete; 135 DmAppImageInfo &operator=(DmAppImageInfo &&) = delete; 136 /** 137 * @tc.name: DmAppImageInfo::GetAppIconLen 138 * @tc.desc: Dm App Image Info Get App Icon Len 139 * @tc.type: FUNC 140 */ GetAppIconLen()141 int32_t GetAppIconLen() const 142 { 143 return appIconLen; 144 } 145 GetAppIcon()146 const uint8_t *GetAppIcon() const 147 { 148 return appIcon; 149 } 150 /** 151 * @tc.name: DmAppImageInfo::GetAppThumbnailLen 152 * @tc.desc: Dm App Image Info Get App ThumbnailLen 153 * @tc.type: FUNC 154 */ GetAppThumbnailLen()155 int32_t GetAppThumbnailLen() const 156 { 157 return appThumbnailLen; 158 } 159 /** 160 * @tc.name: DmAppImageInfo::GetAppThumbnail 161 * @tc.desc: Dm App Image Info Get App Thumbnail 162 * @tc.type: FUNC 163 */ GetAppThumbnail()164 const uint8_t *GetAppThumbnail() const 165 { 166 return appThumbnail; 167 } 168 169 private: SaveData(const uint8_t * appIcon_,int32_t appIconLen_,const uint8_t * appThumbnail_,int32_t appThumbnailLen_)170 void SaveData(const uint8_t *appIcon_, int32_t appIconLen_, const uint8_t *appThumbnail_, int32_t appThumbnailLen_) 171 { 172 SaveIconData(appIcon_, appIconLen_); 173 SaveThumbnailData(appThumbnail_, appThumbnailLen_); 174 } 175 SaveIconData(const uint8_t * appIcon_,int32_t appIconLen_)176 void SaveIconData(const uint8_t *appIcon_, int32_t appIconLen_) 177 { 178 if (appIconLen_ > 0 && appIconLen_ < ICON_MAX_LEN && appIcon_ != nullptr) { 179 if (appIconLen < appIconLen_) { 180 if (appIcon != nullptr && appIconLen > 0) { 181 delete[] appIcon; 182 appIcon = nullptr; 183 appIconLen = 0; 184 } 185 appIcon = new (std::nothrow) uint8_t[appIconLen_] { 0 }; 186 } 187 if (appIcon != nullptr) { 188 appIconLen = appIconLen_; 189 if (memcpy_s(appIcon, (uint32_t)appIconLen, appIcon_, appIconLen_) != 0) { 190 return; 191 } 192 } 193 } 194 } 195 SaveThumbnailData(const uint8_t * appThumbnail_,int32_t appThumbnailLen_)196 void SaveThumbnailData(const uint8_t *appThumbnail_, int32_t appThumbnailLen_) 197 { 198 if (appThumbnailLen_ > 0 && appThumbnailLen_ < THUMB_MAX_LEN && appThumbnail_ != nullptr) { 199 if (appThumbnailLen < appThumbnailLen_) { 200 if (appThumbnail != nullptr && appThumbnailLen > 0) { 201 delete[] appThumbnail; 202 appThumbnail = nullptr; 203 appThumbnailLen = 0; 204 } 205 appThumbnail = new (std::nothrow) uint8_t[appThumbnailLen_] { 0 }; 206 } 207 if (appThumbnail != nullptr) { 208 appThumbnailLen = appThumbnailLen_; 209 if (memcpy_s(appThumbnail, (uint32_t)appThumbnailLen, appThumbnail_, appThumbnailLen_) != 0) { 210 return; 211 } 212 } 213 } 214 } 215 216 private: 217 int32_t appIconLen { 0 }; 218 uint8_t *appIcon { nullptr }; 219 int32_t appThumbnailLen { 0 }; 220 uint8_t *appThumbnail { nullptr }; 221 const int32_t ICON_MAX_LEN = 32 * 1024; 222 const int32_t THUMB_MAX_LEN = 153 * 1024; 223 }; 224 } // namespace DistributedHardware 225 } // namespace OHOS 226 #endif // OHOS_DM_APP_IMAGE_INFO_H 227