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