MTP: Compatibility fixes for transferring strings

Change-Id: Ic06d754ee68b0389439cdc34f73adff0f2b33afa
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/media/mtp/MtpDataPacket.cpp b/media/mtp/MtpDataPacket.cpp
index 9bfd00f..27dc796 100644
--- a/media/mtp/MtpDataPacket.cpp
+++ b/media/mtp/MtpDataPacket.cpp
@@ -325,9 +325,12 @@
         else
             break;
     }
-    putUInt8(count);
+    putUInt8(count > 0 ? count + 1 : 0);
     for (int i = 0; i < count; i++)
         putUInt16(string[i]);
+    // only terminate with zero if string is not empty
+    if (count > 0)
+        putUInt16(0);
 }
 
 #ifdef MTP_DEVICE 
diff --git a/media/mtp/MtpDataPacket.h b/media/mtp/MtpDataPacket.h
index b458286..1467aab 100644
--- a/media/mtp/MtpDataPacket.h
+++ b/media/mtp/MtpDataPacket.h
@@ -83,7 +83,7 @@
     void                putString(const MtpStringBuffer& string);
     void                putString(const char* string);
     void                putString(const uint16_t* string);
-    inline void         putEmptyString() { putUInt16(0); }
+    inline void         putEmptyString() { putUInt8(0); }
     inline void         putEmptyArray() { putUInt32(0); }
 
 
diff --git a/media/mtp/MtpStringBuffer.cpp b/media/mtp/MtpStringBuffer.cpp
index 2d3cf69..8bf6731 100644
--- a/media/mtp/MtpStringBuffer.cpp
+++ b/media/mtp/MtpStringBuffer.cpp
@@ -112,7 +112,7 @@
 void MtpStringBuffer::writeToPacket(MtpDataPacket* packet) const {
     int count = mCharCount;
     const uint8_t* src = mBuffer;
-    packet->putUInt8(count);
+    packet->putUInt8(count > 0 ? count + 1 : 0);
 
     // expand utf8 to 16 bit chars
     for (int i = 0; i < count; i++) {
@@ -133,6 +133,9 @@
         }
         packet->putUInt16(ch);
     }
+    // only terminate with zero if string is not empty
+    if (count > 0)
+        packet->putUInt16(0);
 }
 
 }  // namespace android