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 #include "payload_data/get_thumb_data.h"
16 #include "media_log.h"
17 #include "media_mtp_utils.h"
18 #include "mtp_packet_tools.h"
19 using namespace std;
20 namespace OHOS {
21 namespace Media {
22 static constexpr int32_t PARSER_PARAM_SUM = 1;
23 
GetThumbData(std::shared_ptr<MtpOperationContext> & context)24 GetThumbData::GetThumbData(std::shared_ptr<MtpOperationContext> &context)
25     : PayloadData(context)
26 {
27 }
28 
GetThumbData()29 GetThumbData::GetThumbData()
30 {
31 }
32 
~GetThumbData()33 GetThumbData::~GetThumbData()
34 {
35 }
36 
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)37 int GetThumbData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
38 {
39     if (context_ == nullptr) {
40         MEDIA_ERR_LOG("GetThumbData::parser null");
41         return MTP_ERROR_CONTEXT_IS_NULL;
42     }
43 
44     int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
45     if (parameterCount < PARSER_PARAM_SUM) {
46         MEDIA_ERR_LOG("GetThumbData::parser paramCount=%{public}u, needCount=%{public}d",
47             parameterCount, PARSER_PARAM_SUM);
48         return MTP_ERROR_PACKET_INCORRECT;
49     }
50 
51     size_t offset = MTP_CONTAINER_HEADER_SIZE;
52     context_->handle = MtpPacketTool::GetUInt32(buffer, offset);
53     return MTP_SUCCESS;
54 }
55 
Maker(std::vector<uint8_t> & outBuffer)56 int GetThumbData::Maker(std::vector<uint8_t> &outBuffer)
57 {
58     if ((!hasSetThumb_) || (thumb_ == nullptr)) {
59         MEDIA_ERR_LOG("GetThumbData::maker set or null");
60         return MTP_ERROR_INVALID_OBJECTHANDLE;
61     }
62 
63     outBuffer.insert(outBuffer.end(), thumb_->begin(), thumb_->end());
64     return MTP_SUCCESS;
65 }
66 
CalculateSize()67 uint32_t GetThumbData::CalculateSize()
68 {
69     std::vector<uint8_t> tmpVar;
70     int res = Maker(tmpVar);
71     if (res != MTP_SUCCESS) {
72         return res;
73     }
74     return tmpVar.size();
75 }
76 
SetThumb(std::shared_ptr<UInt8List> & thumb)77 bool GetThumbData::SetThumb(std::shared_ptr<UInt8List> &thumb)
78 {
79     if (hasSetThumb_) {
80         return false;
81     }
82     hasSetThumb_ = true;
83     thumb_ = thumb;
84     return true;
85 }
86 } // namespace Media
87 } // namespace OHOS