MtpStringBuffer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #define LOG_TAG "MtpStringBuffer"
  17. #include <codecvt>
  18. #include <locale>
  19. #include <string>
  20. #include <vector>
  21. #include "MtpDataPacket.h"
  22. #include "MtpStringBuffer.h"
  23. namespace {
  24. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> gConvert;
  25. static std::string utf16ToUtf8(std::u16string input_str) {
  26. return gConvert.to_bytes(input_str);
  27. }
  28. static std::u16string utf8ToUtf16(std::string input_str) {
  29. return gConvert.from_bytes(input_str);
  30. }
  31. } // namespace
  32. namespace android {
  33. MtpStringBuffer::MtpStringBuffer(const char* src)
  34. {
  35. set(src);
  36. }
  37. MtpStringBuffer::MtpStringBuffer(const uint16_t* src)
  38. {
  39. set(src);
  40. }
  41. MtpStringBuffer::MtpStringBuffer(const MtpStringBuffer& src)
  42. {
  43. mString = src.mString;
  44. }
  45. void MtpStringBuffer::set(const char* src) {
  46. mString = std::string(src);
  47. }
  48. void MtpStringBuffer::set(const uint16_t* src) {
  49. mString = utf16ToUtf8(std::u16string((const char16_t*)src));
  50. }
  51. bool MtpStringBuffer::readFromPacket(MtpDataPacket* packet) {
  52. uint8_t count;
  53. if (!packet->getUInt8(count))
  54. return false;
  55. if (count == 0)
  56. return true;
  57. std::vector<char16_t> buffer(count);
  58. for (int i = 0; i < count; i++) {
  59. uint16_t ch;
  60. if (!packet->getUInt16(ch))
  61. return false;
  62. buffer[i] = ch;
  63. }
  64. if (buffer[count-1] != '\0') {
  65. ALOGE("Mtp string not null terminated\n");
  66. return false;
  67. }
  68. mString = utf16ToUtf8(std::u16string(buffer.data()));
  69. return true;
  70. }
  71. void MtpStringBuffer::writeToPacket(MtpDataPacket* packet) const {
  72. std::u16string src16 = utf8ToUtf16(mString);
  73. int count = src16.length();
  74. if (count == 0) {
  75. packet->putUInt8(0);
  76. return;
  77. }
  78. packet->putUInt8(std::min(count + 1, MTP_STRING_MAX_CHARACTER_NUMBER));
  79. int i = 0;
  80. for (char16_t &c : src16) {
  81. if (i == MTP_STRING_MAX_CHARACTER_NUMBER - 1) {
  82. // Leave a slot for null termination.
  83. ALOGI("Mtp truncating long string\n");
  84. break;
  85. }
  86. packet->putUInt16(c);
  87. i++;
  88. }
  89. // only terminate with zero if string is not empty
  90. packet->putUInt16(0);
  91. }
  92. } // namespace android