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/send_object_info_data.h"
16 #include <string>
17 #include "media_log.h"
18 #include "media_mtp_utils.h"
19 #include "mtp_packet_tools.h"
20 using namespace std;
21 namespace OHOS {
22 namespace Media {
23 static constexpr int32_t PARSER_PARAM_SUM = 2;
24
SendObjectInfoData(std::shared_ptr<MtpOperationContext> & context)25 SendObjectInfoData::SendObjectInfoData(std::shared_ptr<MtpOperationContext> &context)
26 : PayloadData(context)
27 {
28 }
29
SendObjectInfoData()30 SendObjectInfoData::SendObjectInfoData()
31 {
32 }
33
~SendObjectInfoData()34 SendObjectInfoData::~SendObjectInfoData()
35 {
36 }
37
Parser(const std::vector<uint8_t> & buffer,int32_t readSize)38 int SendObjectInfoData::Parser(const std::vector<uint8_t> &buffer, int32_t readSize)
39 {
40 if (context_ == nullptr) {
41 MEDIA_ERR_LOG("SendObjectInfoData::parser null");
42 return MTP_ERROR_INVALID_OBJECTHANDLE;
43 }
44
45 if (!context_->indata) {
46 int32_t parameterCount = (readSize - MTP_CONTAINER_HEADER_SIZE) / MTP_PARAMETER_SIZE;
47 if (parameterCount < PARSER_PARAM_SUM) {
48 MEDIA_ERR_LOG("SendObjectInfoData::parser paramCount=%{public}u, needCount=%{public}d",
49 parameterCount, PARSER_PARAM_SUM);
50 return MTP_ERROR_PACKET_INCORRECT;
51 }
52
53 size_t offset = MTP_CONTAINER_HEADER_SIZE;
54 context_->storageID = MtpPacketTool::GetUInt32(buffer, offset);
55 context_->parent = MtpPacketTool::GetUInt32(buffer, offset);
56 } else {
57 size_t offset = MTP_CONTAINER_HEADER_SIZE;
58 int res = ParserData(buffer, offset);
59 if (res != MTP_SUCCESS) {
60 return res;
61 }
62 }
63 return MTP_SUCCESS;
64 }
65
Maker(std::vector<uint8_t> & outBuffer)66 int SendObjectInfoData::Maker(std::vector<uint8_t> &outBuffer)
67 {
68 if (!hasSetParam_) {
69 MEDIA_ERR_LOG("SendObjectInfoData::maker set");
70 return MTP_ERROR_INVALID_OBJECTHANDLE;
71 }
72
73 MtpPacketTool::PutUInt32(outBuffer, storageID_);
74 MtpPacketTool::PutUInt32(outBuffer, parent_);
75 MtpPacketTool::PutUInt32(outBuffer, handle_);
76 return MTP_SUCCESS;
77 }
78
CalculateSize()79 uint32_t SendObjectInfoData::CalculateSize()
80 {
81 std::vector<uint8_t> tmpVar;
82 int res = Maker(tmpVar);
83 if (res != MTP_SUCCESS) {
84 return res;
85 }
86 return tmpVar.size();
87 }
88
SetSetParam(uint32_t storageID,uint32_t parent,uint32_t handle)89 bool SendObjectInfoData::SetSetParam(uint32_t storageID, uint32_t parent, uint32_t handle)
90 {
91 if (hasSetParam_) {
92 return false;
93 }
94 hasSetParam_ = true;
95 storageID_ = storageID;
96 parent_ = parent;
97 handle_ = handle;
98 return true;
99 }
100
ParserData(const std::vector<uint8_t> & buffer,size_t & offset)101 int SendObjectInfoData::ParserData(const std::vector<uint8_t> &buffer, size_t &offset)
102 {
103 uint16_t tmpUse16 = 0;
104 uint32_t tmpUse32 = 0;
105 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) { // storage ID
106 return MTP_ERROR_PACKET_INCORRECT;
107 }
108 if (!MtpPacketTool::GetUInt16(buffer, offset, context_->format)) {
109 return MTP_ERROR_PACKET_INCORRECT;
110 }
111 if (!MtpPacketTool::GetUInt16(buffer, offset, tmpUse16)) { // protection status
112 return MTP_ERROR_PACKET_INCORRECT;
113 }
114 if (!MtpPacketTool::GetUInt32(buffer, offset, context_->sendObjectFileSize)) {
115 return MTP_ERROR_PACKET_INCORRECT;
116 }
117 int res = ParserDataForImageInfo(buffer, offset);
118 if (res != MTP_SUCCESS) {
119 return res;
120 }
121 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) { // parent
122 return MTP_ERROR_PACKET_INCORRECT;
123 }
124 if (!MtpPacketTool::GetUInt16(buffer, offset, tmpUse16)) {
125 return MTP_ERROR_PACKET_INCORRECT;
126 }
127 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) {
128 return MTP_ERROR_PACKET_INCORRECT;
129 }
130 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) { // sequence number
131 return MTP_ERROR_PACKET_INCORRECT;
132 }
133
134 return ParserDataForFileInfo(buffer, offset);
135 }
136
ParserDataForImageInfo(const std::vector<uint8_t> & buffer,size_t & offset)137 int SendObjectInfoData::ParserDataForImageInfo(const std::vector<uint8_t> &buffer, size_t &offset)
138 {
139 uint16_t tmpUse16 = 0;
140 uint32_t tmpUse32 = 0;
141
142 if (!MtpPacketTool::GetUInt16(buffer, offset, tmpUse16)) { // thumb format
143 return MTP_ERROR_PACKET_INCORRECT;
144 }
145 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) { // thumb compressed size
146 return MTP_ERROR_PACKET_INCORRECT;
147 }
148 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) { // thumb pix width
149 return MTP_ERROR_PACKET_INCORRECT;
150 }
151 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) { // thumb pix height
152 return MTP_ERROR_PACKET_INCORRECT;
153 }
154 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) { // image pix width
155 return MTP_ERROR_PACKET_INCORRECT;
156 }
157 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) { // image pix height
158 return MTP_ERROR_PACKET_INCORRECT;
159 }
160 if (!MtpPacketTool::GetUInt32(buffer, offset, tmpUse32)) { // image bit depth
161 return MTP_ERROR_PACKET_INCORRECT;
162 }
163
164 return MTP_SUCCESS;
165 }
166
ParserDataForFileInfo(const std::vector<uint8_t> & buffer,size_t & offset)167 int SendObjectInfoData::ParserDataForFileInfo(const std::vector<uint8_t> &buffer, size_t &offset)
168 {
169 if (!MtpPacketTool::GetString(buffer, offset, context_->name)) { // file name
170 return MTP_ERROR_PACKET_INCORRECT;
171 }
172 if (context_->name.empty()) {
173 MEDIA_ERR_LOG("ParserDataForFileInfo: empty name");
174 return MTP_ERROR_PACKET_INCORRECT;
175 }
176 if (!MtpPacketTool::GetString(buffer, offset, context_->created)) { // date created
177 return MTP_ERROR_PACKET_INCORRECT;
178 }
179 if (!MtpPacketTool::GetString(buffer, offset, context_->modified)) { // date modified
180 return MTP_ERROR_PACKET_INCORRECT;
181 }
182
183 return MTP_SUCCESS;
184 }
185 } // namespace Media
186 } // namespace OHOS