Fix Unicode string handling
Linux uses UTF8 but java and MTP both
use UTF16. In a few places, this results
in the top byte of a UTF16 string being
truncated on conversion to UTF8.
Also, the hardcoded UTF to byte serialization
in MtpStringBuffer is incorrect.
Replace it with conversions from std, and
replace usages of MtpString with MtpStringBuffer.
Remove any remaining usages of libutils
and replace them with corresponding std
libraries.
Bug: 70546563
Test: Mtp works, tests pass
Test: file/folder names containing emoji can be transferred to/from
windows
Change-Id: Idbcb73f1beac17ce8a90843fa254e759dd1a6369
diff --git a/media/mtp/MtpServer.cpp b/media/mtp/MtpServer.cpp
index e4ac8b0..86d59dd 100644
--- a/media/mtp/MtpServer.cpp
+++ b/media/mtp/MtpServer.cpp
@@ -102,10 +102,10 @@
};
MtpServer::MtpServer(IMtpDatabase* database, int controlFd, bool ptp,
- const MtpString& deviceInfoManufacturer,
- const MtpString& deviceInfoModel,
- const MtpString& deviceInfoDeviceVersion,
- const MtpString& deviceInfoSerialNumber)
+ const char *deviceInfoManufacturer,
+ const char *deviceInfoModel,
+ const char *deviceInfoDeviceVersion,
+ const char *deviceInfoSerialNumber)
: mDatabase(database),
mPtp(ptp),
mDeviceInfoManufacturer(deviceInfoManufacturer),
@@ -132,14 +132,14 @@
}
void MtpServer::addStorage(MtpStorage* storage) {
- Mutex::Autolock autoLock(mMutex);
+ std::lock_guard<std::mutex> lg(mMutex);
- mStorages.push(storage);
+ mStorages.push_back(storage);
sendStoreAdded(storage->getStorageID());
}
void MtpServer::removeStorage(MtpStorage* storage) {
- Mutex::Autolock autoLock(mMutex);
+ std::lock_guard<std::mutex> lg(mMutex);
auto iter = std::find(mStorages.begin(), mStorages.end(), storage);
if (iter != mStorages.end()) {
sendStoreRemoved(storage->getStorageID());
@@ -284,10 +284,10 @@
}
}
-void MtpServer::addEditObject(MtpObjectHandle handle, MtpString& path,
+void MtpServer::addEditObject(MtpObjectHandle handle, MtpStringBuffer& path,
uint64_t size, MtpObjectFormat format, int fd) {
ObjectEdit* edit = new ObjectEdit(handle, path, size, format, fd);
- mObjectEditList.add(edit);
+ mObjectEditList.push_back(edit);
}
MtpServer::ObjectEdit* MtpServer::getEditObject(MtpObjectHandle handle) {
@@ -305,7 +305,7 @@
ObjectEdit* edit = mObjectEditList[i];
if (edit->mHandle == handle) {
delete edit;
- mObjectEditList.removeAt(i);
+ mObjectEditList.erase(mObjectEditList.begin() + i);
return;
}
}
@@ -318,7 +318,7 @@
bool MtpServer::handleRequest() {
- Mutex::Autolock autoLock(mMutex);
+ std::lock_guard<std::mutex> lg(mMutex);
MtpOperationCode operation = mRequest.getOperationCode();
MtpResponseCode response;
@@ -769,7 +769,7 @@
if (mRequest.getParameterCount() < 1)
return MTP_RESPONSE_INVALID_PARAMETER;
MtpObjectHandle handle = mRequest.getParameter(1);
- MtpString pathBuf;
+ MtpStringBuffer pathBuf;
int64_t fileLength;
MtpObjectFormat format;
int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
@@ -855,7 +855,7 @@
// standard GetPartialObject
length = mRequest.getParameter(3);
}
- MtpString pathBuf;
+ MtpStringBuffer pathBuf;
int64_t fileLength;
MtpObjectFormat format;
int result = mDatabase->getObjectFilePath(handle, pathBuf, fileLength, format);
@@ -892,7 +892,7 @@
}
MtpResponseCode MtpServer::doSendObjectInfo() {
- MtpString path;
+ MtpStringBuffer path;
uint16_t temp16;
uint32_t temp32;
@@ -906,7 +906,7 @@
// special case the root
if (parent == MTP_PARENT_ROOT) {
- path = storage->getPath();
+ path.set(storage->getPath());
parent = 0;
} else {
int64_t length;
@@ -938,7 +938,7 @@
if (!mData.getUInt32(temp32)) return MTP_RESPONSE_INVALID_PARAMETER; // sequence number
MtpStringBuffer name, created, modified;
if (!mData.getString(name)) return MTP_RESPONSE_INVALID_PARAMETER; // file name
- if (name.getCharCount() == 0) {
+ if (name.isEmpty()) {
ALOGE("empty name");
return MTP_RESPONSE_INVALID_PARAMETER;
}
@@ -952,8 +952,8 @@
modifiedTime = 0;
if (path[path.size() - 1] != '/')
- path += "/";
- path += (const char *)name;
+ path.append("/");
+ path.append(name);
// check space first
if (mSendObjectFileSize > storage->getFreeSpace())
@@ -1006,10 +1006,10 @@
MtpObjectHandle parent = mRequest.getParameter(3);
if (!storage)
return MTP_RESPONSE_INVALID_STORAGE_ID;
- MtpString path;
+ MtpStringBuffer path;
MtpResponseCode result;
- MtpString fromPath;
+ MtpStringBuffer fromPath;
int64_t fileLength;
MtpObjectFormat format;
MtpObjectInfo info(objectHandle);
@@ -1022,7 +1022,7 @@
// special case the root
if (parent == 0) {
- path = storage->getPath();
+ path.set(storage->getPath());
} else {
int64_t parentLength;
MtpObjectFormat parentFormat;
@@ -1034,8 +1034,8 @@
}
if (path[path.size() - 1] != '/')
- path += "/";
- path += info.mName;
+ path.append("/");
+ path.append(info.mName);
result = mDatabase->beginMoveObject(objectHandle, parent, storageID);
if (result != MTP_RESPONSE_OK)
@@ -1085,9 +1085,9 @@
MtpObjectHandle parent = mRequest.getParameter(3);
if (!storage)
return MTP_RESPONSE_INVALID_STORAGE_ID;
- MtpString path;
+ MtpStringBuffer path;
- MtpString fromPath;
+ MtpStringBuffer fromPath;
int64_t fileLength;
MtpObjectFormat format;
MtpObjectInfo info(objectHandle);
@@ -1100,7 +1100,7 @@
// special case the root
if (parent == 0) {
- path = storage->getPath();
+ path.set(storage->getPath());
} else {
int64_t parentLength;
MtpObjectFormat parentFormat;
@@ -1116,8 +1116,8 @@
return MTP_RESPONSE_STORAGE_FULL;
if (path[path.size() - 1] != '/')
- path += "/";
- path += info.mName;
+ path.append("/");
+ path.append(info.mName);
MtpObjectHandle handle = mDatabase->beginCopyObject(objectHandle, parent, storageID);
if (handle == kInvalidObjectHandle) {
@@ -1264,7 +1264,7 @@
// FIXME - support deleting all objects if handle is 0xFFFFFFFF
// FIXME - implement deleting objects by format
- MtpString filePath;
+ MtpStringBuffer filePath;
int64_t fileLength;
int result = mDatabase->getObjectFilePath(handle, filePath, fileLength, format);
if (result != MTP_RESPONSE_OK)
@@ -1414,7 +1414,7 @@
return MTP_RESPONSE_GENERAL_ERROR;
}
- MtpString path;
+ MtpStringBuffer path;
int64_t fileLength;
MtpObjectFormat format;
int result = mDatabase->getObjectFilePath(handle, path, fileLength, format);