1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "MtpDataPacket"
18 
19 #include "MtpDataPacket.h"
20 
21 #include <algorithm>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <usbhost/usbhost.h>
27 #include "MtpStringBuffer.h"
28 #include "IMtpHandle.h"
29 
30 namespace android {
31 
32 namespace {
33 // Reads the exact |count| bytes from |fd| to |buf|.
34 // Returns |count| if it succeed to read the bytes. Otherwise returns -1. If it reaches EOF, the
35 // function regards it as an error.
readExactBytes(int fd,void * buf,size_t count)36 ssize_t readExactBytes(int fd, void* buf, size_t count) {
37     if (count > SSIZE_MAX) {
38         return -1;
39     }
40     size_t read_count = 0;
41     while (read_count < count) {
42         int result = read(fd, static_cast<int8_t*>(buf) + read_count, count - read_count);
43         // Assume that EOF is error.
44         if (result <= 0) {
45             return -1;
46         }
47         read_count += result;
48     }
49     return read_count == count ? count : -1;
50 }
51 }  // namespace
52 
MtpDataPacket()53 MtpDataPacket::MtpDataPacket()
54     :   MtpPacket(MTP_BUFFER_SIZE),   // MAX_USBFS_BUFFER_SIZE
55         mOffset(MTP_CONTAINER_HEADER_SIZE)
56 {
57 }
58 
~MtpDataPacket()59 MtpDataPacket::~MtpDataPacket() {
60 }
61 
reset()62 void MtpDataPacket::reset() {
63     MtpPacket::reset();
64     mOffset = MTP_CONTAINER_HEADER_SIZE;
65 }
66 
setOperationCode(MtpOperationCode code)67 void MtpDataPacket::setOperationCode(MtpOperationCode code) {
68     MtpPacket::putUInt16(MTP_CONTAINER_CODE_OFFSET, code);
69 }
70 
setTransactionID(MtpTransactionID id)71 void MtpDataPacket::setTransactionID(MtpTransactionID id) {
72     MtpPacket::putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id);
73 }
74 
getUInt8(uint8_t & value)75 bool MtpDataPacket::getUInt8(uint8_t& value) {
76     if ((mPacketSize - mOffset < sizeof(value)) || (mOffset >= mBufferSize))
77         return false;
78     value = mBuffer[mOffset++];
79     return true;
80 }
81 
getUInt16(uint16_t & value)82 bool MtpDataPacket::getUInt16(uint16_t& value) {
83     if ((mPacketSize - mOffset < sizeof(value)) || ((mOffset+1) >= mBufferSize))
84         return false;
85     int offset = mOffset;
86     value = (uint16_t)mBuffer[offset] | ((uint16_t)mBuffer[offset + 1] << 8);
87     mOffset += sizeof(value);
88     return true;
89 }
90 
getUInt32(uint32_t & value)91 bool MtpDataPacket::getUInt32(uint32_t& value) {
92     if ((mPacketSize - mOffset < sizeof(value)) || ((mOffset+3) >= mBufferSize))
93         return false;
94     int offset = mOffset;
95     value = (uint32_t)mBuffer[offset] | ((uint32_t)mBuffer[offset + 1] << 8) |
96            ((uint32_t)mBuffer[offset + 2] << 16)  | ((uint32_t)mBuffer[offset + 3] << 24);
97     mOffset += sizeof(value);
98     return true;
99 }
100 
getUInt64(uint64_t & value)101 bool MtpDataPacket::getUInt64(uint64_t& value) {
102     if ((mPacketSize - mOffset < sizeof(value)) || ((mOffset+7) >= mBufferSize))
103         return false;
104     int offset = mOffset;
105     value = (uint64_t)mBuffer[offset] | ((uint64_t)mBuffer[offset + 1] << 8) |
106            ((uint64_t)mBuffer[offset + 2] << 16) | ((uint64_t)mBuffer[offset + 3] << 24) |
107            ((uint64_t)mBuffer[offset + 4] << 32) | ((uint64_t)mBuffer[offset + 5] << 40) |
108            ((uint64_t)mBuffer[offset + 6] << 48)  | ((uint64_t)mBuffer[offset + 7] << 56);
109     mOffset += sizeof(value);
110     return true;
111 }
112 
getUInt128(uint128_t & value)113 bool MtpDataPacket::getUInt128(uint128_t& value) {
114     return getUInt32(value[0]) && getUInt32(value[1]) && getUInt32(value[2]) && getUInt32(value[3]);
115 }
116 
getString(MtpStringBuffer & string)117 bool MtpDataPacket::getString(MtpStringBuffer& string)
118 {
119     return string.readFromPacket(this);
120 }
121 
getAInt8()122 Int8List* MtpDataPacket::getAInt8() {
123     uint32_t count;
124     if (!getUInt32(count))
125         return NULL;
126     Int8List* result = new Int8List;
127     for (uint32_t i = 0; i < count; i++) {
128         int8_t value;
129         if (!getInt8(value)) {
130             delete result;
131             return NULL;
132         }
133         result->push_back(value);
134     }
135     return result;
136 }
137 
getAUInt8()138 UInt8List* MtpDataPacket::getAUInt8() {
139     uint32_t count;
140     if (!getUInt32(count))
141         return NULL;
142     UInt8List* result = new UInt8List;
143     for (uint32_t i = 0; i < count; i++) {
144         uint8_t value;
145         if (!getUInt8(value)) {
146             delete result;
147             return NULL;
148         }
149         result->push_back(value);
150     }
151     return result;
152 }
153 
getAInt16()154 Int16List* MtpDataPacket::getAInt16() {
155     uint32_t count;
156     if (!getUInt32(count))
157         return NULL;
158     Int16List* result = new Int16List;
159     for (uint32_t i = 0; i < count; i++) {
160         int16_t value;
161         if (!getInt16(value)) {
162             delete result;
163             return NULL;
164         }
165         result->push_back(value);
166     }
167     return result;
168 }
169 
getAUInt16()170 UInt16List* MtpDataPacket::getAUInt16() {
171     uint32_t count;
172     if (!getUInt32(count))
173         return NULL;
174     UInt16List* result = new UInt16List;
175     for (uint32_t i = 0; i < count; i++) {
176         uint16_t value;
177         if (!getUInt16(value)) {
178             delete result;
179             return NULL;
180         }
181         result->push_back(value);
182     }
183     return result;
184 }
185 
getAInt32()186 Int32List* MtpDataPacket::getAInt32() {
187     uint32_t count;
188     if (!getUInt32(count))
189         return NULL;
190     Int32List* result = new Int32List;
191     for (uint32_t i = 0; i < count; i++) {
192         int32_t value;
193         if (!getInt32(value)) {
194             delete result;
195             return NULL;
196         }
197         result->push_back(value);
198     }
199     return result;
200 }
201 
getAUInt32()202 UInt32List* MtpDataPacket::getAUInt32() {
203     uint32_t count;
204     if (!getUInt32(count))
205         return NULL;
206     UInt32List* result = new UInt32List;
207     for (uint32_t i = 0; i < count; i++) {
208         uint32_t value;
209         if (!getUInt32(value)) {
210             delete result;
211             return NULL;
212         }
213         result->push_back(value);
214     }
215     return result;
216 }
217 
getAInt64()218 Int64List* MtpDataPacket::getAInt64() {
219     uint32_t count;
220     if (!getUInt32(count))
221         return NULL;
222     Int64List* result = new Int64List;
223     for (uint32_t i = 0; i < count; i++) {
224         int64_t value;
225         if (!getInt64(value)) {
226             delete result;
227             return NULL;
228         }
229         result->push_back(value);
230     }
231     return result;
232 }
233 
getAUInt64()234 UInt64List* MtpDataPacket::getAUInt64() {
235     uint32_t count;
236     if (!getUInt32(count))
237         return NULL;
238     UInt64List* result = new UInt64List;
239     for (uint32_t i = 0; i < count; i++) {
240         uint64_t value;
241         if (!getUInt64(value)) {
242             delete result;
243             return NULL;
244         }
245         result->push_back(value);
246     }
247     return result;
248 }
249 
putInt8(int8_t value)250 void MtpDataPacket::putInt8(int8_t value) {
251     allocate(mOffset + 1);
252     mBuffer[mOffset++] = (uint8_t)value;
253     if (mPacketSize < mOffset)
254         mPacketSize = mOffset;
255 }
256 
putUInt8(uint8_t value)257 void MtpDataPacket::putUInt8(uint8_t value) {
258     allocate(mOffset + 1);
259     mBuffer[mOffset++] = (uint8_t)value;
260     if (mPacketSize < mOffset)
261         mPacketSize = mOffset;
262 }
263 
putInt16(int16_t value)264 void MtpDataPacket::putInt16(int16_t value) {
265     allocate(mOffset + 2);
266     mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
267     mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
268     if (mPacketSize < mOffset)
269         mPacketSize = mOffset;
270 }
271 
putUInt16(uint16_t value)272 void MtpDataPacket::putUInt16(uint16_t value) {
273     allocate(mOffset + 2);
274     mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
275     mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
276     if (mPacketSize < mOffset)
277         mPacketSize = mOffset;
278 }
279 
putInt32(int32_t value)280 void MtpDataPacket::putInt32(int32_t value) {
281     allocate(mOffset + 4);
282     mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
283     mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
284     mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF);
285     mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF);
286     if (mPacketSize < mOffset)
287         mPacketSize = mOffset;
288 }
289 
putUInt32(uint32_t value)290 void MtpDataPacket::putUInt32(uint32_t value) {
291     allocate(mOffset + 4);
292     mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
293     mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
294     mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF);
295     mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF);
296     if (mPacketSize < mOffset)
297         mPacketSize = mOffset;
298 }
299 
putInt64(int64_t value)300 void MtpDataPacket::putInt64(int64_t value) {
301     allocate(mOffset + 8);
302     mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
303     mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
304     mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF);
305     mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF);
306     mBuffer[mOffset++] = (uint8_t)((value >> 32) & 0xFF);
307     mBuffer[mOffset++] = (uint8_t)((value >> 40) & 0xFF);
308     mBuffer[mOffset++] = (uint8_t)((value >> 48) & 0xFF);
309     mBuffer[mOffset++] = (uint8_t)((value >> 56) & 0xFF);
310     if (mPacketSize < mOffset)
311         mPacketSize = mOffset;
312 }
313 
putUInt64(uint64_t value)314 void MtpDataPacket::putUInt64(uint64_t value) {
315     allocate(mOffset + 8);
316     mBuffer[mOffset++] = (uint8_t)(value & 0xFF);
317     mBuffer[mOffset++] = (uint8_t)((value >> 8) & 0xFF);
318     mBuffer[mOffset++] = (uint8_t)((value >> 16) & 0xFF);
319     mBuffer[mOffset++] = (uint8_t)((value >> 24) & 0xFF);
320     mBuffer[mOffset++] = (uint8_t)((value >> 32) & 0xFF);
321     mBuffer[mOffset++] = (uint8_t)((value >> 40) & 0xFF);
322     mBuffer[mOffset++] = (uint8_t)((value >> 48) & 0xFF);
323     mBuffer[mOffset++] = (uint8_t)((value >> 56) & 0xFF);
324     if (mPacketSize < mOffset)
325         mPacketSize = mOffset;
326 }
327 
putInt128(const int128_t & value)328 void MtpDataPacket::putInt128(const int128_t& value) {
329     putInt32(value[0]);
330     putInt32(value[1]);
331     putInt32(value[2]);
332     putInt32(value[3]);
333 }
334 
putUInt128(const uint128_t & value)335 void MtpDataPacket::putUInt128(const uint128_t& value) {
336     putUInt32(value[0]);
337     putUInt32(value[1]);
338     putUInt32(value[2]);
339     putUInt32(value[3]);
340 }
341 
putInt128(int64_t value)342 void MtpDataPacket::putInt128(int64_t value) {
343     putInt64(value);
344     putInt64(value < 0 ? -1 : 0);
345 }
346 
putUInt128(uint64_t value)347 void MtpDataPacket::putUInt128(uint64_t value) {
348     putUInt64(value);
349     putUInt64(0);
350 }
351 
putAInt8(const int8_t * values,int count)352 void MtpDataPacket::putAInt8(const int8_t* values, int count) {
353     putUInt32(count);
354     for (int i = 0; i < count; i++)
355         putInt8(*values++);
356 }
357 
putAUInt8(const uint8_t * values,int count)358 void MtpDataPacket::putAUInt8(const uint8_t* values, int count) {
359     putUInt32(count);
360     for (int i = 0; i < count; i++)
361         putUInt8(*values++);
362 }
363 
putAInt16(const int16_t * values,int count)364 void MtpDataPacket::putAInt16(const int16_t* values, int count) {
365     putUInt32(count);
366     for (int i = 0; i < count; i++)
367         putInt16(*values++);
368 }
369 
putAUInt16(const uint16_t * values,int count)370 void MtpDataPacket::putAUInt16(const uint16_t* values, int count) {
371     putUInt32(count);
372     for (int i = 0; i < count; i++)
373         putUInt16(*values++);
374 }
375 
putAUInt16(const UInt16List * values)376 void MtpDataPacket::putAUInt16(const UInt16List* values) {
377     size_t count = (values ? values->size() : 0);
378     putUInt32(count);
379     for (size_t i = 0; i < count; i++)
380         putUInt16((*values)[i]);
381 }
382 
putAInt32(const int32_t * values,int count)383 void MtpDataPacket::putAInt32(const int32_t* values, int count) {
384     putUInt32(count);
385     for (int i = 0; i < count; i++)
386         putInt32(*values++);
387 }
388 
putAUInt32(const uint32_t * values,int count)389 void MtpDataPacket::putAUInt32(const uint32_t* values, int count) {
390     putUInt32(count);
391     for (int i = 0; i < count; i++)
392         putUInt32(*values++);
393 }
394 
putAUInt32(const UInt32List * list)395 void MtpDataPacket::putAUInt32(const UInt32List* list) {
396     if (!list) {
397         putEmptyArray();
398     } else {
399         size_t size = list->size();
400         putUInt32(size);
401         for (size_t i = 0; i < size; i++)
402             putUInt32((*list)[i]);
403     }
404 }
405 
putAInt64(const int64_t * values,int count)406 void MtpDataPacket::putAInt64(const int64_t* values, int count) {
407     putUInt32(count);
408     for (int i = 0; i < count; i++)
409         putInt64(*values++);
410 }
411 
putAUInt64(const uint64_t * values,int count)412 void MtpDataPacket::putAUInt64(const uint64_t* values, int count) {
413     putUInt32(count);
414     for (int i = 0; i < count; i++)
415         putUInt64(*values++);
416 }
417 
putString(const MtpStringBuffer & string)418 void MtpDataPacket::putString(const MtpStringBuffer& string) {
419     string.writeToPacket(this);
420 }
421 
putString(const char * s)422 void MtpDataPacket::putString(const char* s) {
423     if (s != NULL) {
424         MtpStringBuffer string(s);
425         string.writeToPacket(this);
426     }
427 }
428 
putString(const uint16_t * string)429 void MtpDataPacket::putString(const uint16_t* string) {
430     if (string == NULL) {
431         return;
432     }
433     int count = 0;
434     for (int i = 0; i <= MTP_STRING_MAX_CHARACTER_NUMBER; i++) {
435         if (string[i])
436             count++;
437         else
438             break;
439     }
440     putUInt8(count > 0 ? count + 1 : 0);
441     for (int i = 0; i < count; i++)
442         putUInt16(string[i]);
443     // only terminate with zero if string is not empty
444     if (count > 0)
445         putUInt16(0);
446 }
447 
448 #ifdef MTP_DEVICE
read(IMtpHandle * h)449 int MtpDataPacket::read(IMtpHandle *h) {
450     int ret = h->read(mBuffer, MTP_BUFFER_SIZE);
451     if (ret < MTP_CONTAINER_HEADER_SIZE)
452         return -1;
453     mPacketSize = ret;
454     mOffset = MTP_CONTAINER_HEADER_SIZE;
455     return ret;
456 }
457 
write(IMtpHandle * h)458 int MtpDataPacket::write(IMtpHandle *h) {
459     MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
460     MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
461     int ret = h->write(mBuffer, mPacketSize);
462     return (ret < 0 ? ret : 0);
463 }
464 
writeData(IMtpHandle * h,void * data,uint32_t length)465 int MtpDataPacket::writeData(IMtpHandle *h, void* data, uint32_t length) {
466     allocate(length + MTP_CONTAINER_HEADER_SIZE);
467     memcpy(mBuffer + MTP_CONTAINER_HEADER_SIZE, data, length);
468     length += MTP_CONTAINER_HEADER_SIZE;
469     MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, length);
470     MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
471     int ret = h->write(mBuffer, length);
472     return (ret < 0 ? ret : 0);
473 }
474 
475 #endif // MTP_DEVICE
476 
477 #ifdef MTP_HOST
read(struct usb_request * request)478 int MtpDataPacket::read(struct usb_request *request) {
479     // first read the header
480     request->buffer = mBuffer;
481     request->buffer_length = mBufferSize;
482     int length = transfer(request);
483     if (length >= MTP_CONTAINER_HEADER_SIZE) {
484         // look at the length field to see if the data spans multiple packets
485         uint32_t totalLength = MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET);
486         allocate(totalLength);
487         while (totalLength > static_cast<uint32_t>(length)) {
488             request->buffer = mBuffer + length;
489             request->buffer_length = totalLength - length;
490             int ret = transfer(request);
491             if (ret >= 0)
492                 length += ret;
493             else {
494                 length = ret;
495                 break;
496             }
497         }
498     }
499     if (length >= 0)
500         mPacketSize = length;
501     return length;
502 }
503 
readData(struct usb_request * request,void * buffer,int length)504 int MtpDataPacket::readData(struct usb_request *request, void* buffer, int length) {
505     int read = 0;
506     while (read < length) {
507         request->buffer = (char *)buffer + read;
508         request->buffer_length = length - read;
509         int ret = transfer(request);
510         if (ret < 0) {
511             return ret;
512         }
513         read += ret;
514     }
515     return read;
516 }
517 
518 // Queue a read request.  Call readDataWait to wait for result
readDataAsync(struct usb_request * req)519 int MtpDataPacket::readDataAsync(struct usb_request *req) {
520     if (usb_request_queue(req)) {
521         ALOGE("usb_endpoint_queue failed, errno: %d", errno);
522         return -1;
523     }
524     return 0;
525 }
526 
527 // Wait for result of readDataAsync
readDataWait(struct usb_device * device)528 int MtpDataPacket::readDataWait(struct usb_device *device) {
529     struct usb_request *req = usb_request_wait(device, -1);
530     return (req ? req->actual_length : -1);
531 }
532 
readDataHeader(struct usb_request * request)533 int MtpDataPacket::readDataHeader(struct usb_request *request) {
534     request->buffer = mBuffer;
535     request->buffer_length = request->max_packet_size;
536     int length = transfer(request);
537     if (length >= 0)
538         mPacketSize = length;
539     return length;
540 }
541 
write(struct usb_request * request,UrbPacketDivisionMode divisionMode)542 int MtpDataPacket::write(struct usb_request *request, UrbPacketDivisionMode divisionMode) {
543     if (mPacketSize < MTP_CONTAINER_HEADER_SIZE || mPacketSize > MTP_BUFFER_SIZE) {
544         ALOGE("Illegal packet size.");
545         return -1;
546     }
547 
548     MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
549     MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
550 
551     size_t processedBytes = 0;
552     while (processedBytes < mPacketSize) {
553         const size_t write_size =
554                 processedBytes == 0 && divisionMode == FIRST_PACKET_ONLY_HEADER ?
555                         MTP_CONTAINER_HEADER_SIZE : mPacketSize - processedBytes;
556         request->buffer = mBuffer + processedBytes;
557         request->buffer_length = write_size;
558         const int result = transfer(request);
559         if (result < 0) {
560             ALOGE("Failed to write bytes to the device.");
561             return -1;
562         }
563         processedBytes += result;
564     }
565 
566     return processedBytes == mPacketSize ? processedBytes : -1;
567 }
568 
write(struct usb_request * request,UrbPacketDivisionMode divisionMode,int fd,size_t payloadSize)569 int64_t MtpDataPacket::write(struct usb_request *request,
570                          UrbPacketDivisionMode divisionMode,
571                          int fd,
572                          size_t payloadSize) {
573     // Obtain the greatest multiple of minimum packet size that is not greater than
574     // MTP_BUFFER_SIZE.
575     if (request->max_packet_size <= 0) {
576         ALOGE("Cannot determine bulk transfer size due to illegal max packet size %d.",
577               request->max_packet_size);
578         return -1;
579     }
580     const size_t maxBulkTransferSize =
581             MTP_BUFFER_SIZE - (MTP_BUFFER_SIZE % request->max_packet_size);
582     const size_t containerLength = payloadSize + MTP_CONTAINER_HEADER_SIZE;
583     size_t processedBytes = 0;
584     bool readError = false;
585 
586     // Bind the packet with given request.
587     request->buffer = mBuffer;
588     allocate(maxBulkTransferSize);
589 
590     while (processedBytes < containerLength) {
591         size_t bulkTransferSize = 0;
592 
593         // prepare header.
594         const bool headerSent = processedBytes != 0;
595         if (!headerSent) {
596             MtpPacket::putUInt32(MTP_CONTAINER_LENGTH_OFFSET, containerLength);
597             MtpPacket::putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_DATA);
598             bulkTransferSize += MTP_CONTAINER_HEADER_SIZE;
599         }
600 
601         // Prepare payload.
602         if (headerSent || divisionMode == FIRST_PACKET_HAS_PAYLOAD) {
603             const size_t processedPayloadBytes =
604                     headerSent ? processedBytes - MTP_CONTAINER_HEADER_SIZE : 0;
605             const size_t maxRead = payloadSize - processedPayloadBytes;
606             const size_t maxWrite = maxBulkTransferSize - bulkTransferSize;
607             const size_t bulkTransferPayloadSize = std::min(maxRead, maxWrite);
608             // prepare payload.
609             if (!readError) {
610                 const ssize_t result = readExactBytes(
611                         fd,
612                         mBuffer + bulkTransferSize,
613                         bulkTransferPayloadSize);
614                 if (result < 0) {
615                     ALOGE("Found an error while reading data from FD. Send 0 data instead.");
616                     readError = true;
617                 }
618             }
619             if (readError) {
620                 memset(mBuffer + bulkTransferSize, 0, bulkTransferPayloadSize);
621             }
622             bulkTransferSize += bulkTransferPayloadSize;
623         }
624 
625         // Bulk transfer.
626         mPacketSize = bulkTransferSize;
627         request->buffer_length = bulkTransferSize;
628         const int result = transfer(request);
629         if (result != static_cast<ssize_t>(bulkTransferSize)) {
630             // Cannot recover writing error.
631             ALOGE("Found an error while write data to MtpDevice.");
632             return -1;
633         }
634 
635         // Update variables.
636         processedBytes += bulkTransferSize;
637     }
638 
639     return readError ? -1 : processedBytes;
640 }
641 
642 #endif // MTP_HOST
643 
getData(int * outLength) const644 void* MtpDataPacket::getData(int* outLength) const {
645     int length = mPacketSize - MTP_CONTAINER_HEADER_SIZE;
646     if (length > 0) {
647         void* result = malloc(length);
648         if (result) {
649             memcpy(result, mBuffer + MTP_CONTAINER_HEADER_SIZE, length);
650             *outLength = length;
651             return result;
652         }
653     }
654     *outLength = 0;
655     return NULL;
656 }
657 
658 }  // namespace android
659