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