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 #ifndef FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_PACKET_TOOLS_H_ 16 #define FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_PACKET_TOOLS_H_ 17 #include <map> 18 #include <memory> 19 #include <string> 20 #include <vector> 21 #include <time.h> 22 #include "mtp_constants.h" 23 24 // these numbers are defined by protocol, have no exact meaning 25 constexpr int BIT_4 = 4; 26 constexpr int BIT_8 = 8; 27 constexpr int BIT_16 = 16; 28 constexpr int BIT_24 = 24; 29 constexpr int BIT_32 = 32; 30 constexpr int BIT_40 = 40; 31 constexpr int BIT_48 = 48; 32 constexpr int BIT_56 = 56; 33 constexpr int BIT_64 = 64; 34 constexpr int BIT_128 = 128; 35 constexpr int OFFSET_0 = 0; 36 constexpr int OFFSET_1 = 1; 37 constexpr int OFFSET_2 = 2; 38 constexpr int OFFSET_3 = 3; 39 constexpr int OFFSET_4 = 4; 40 constexpr int OFFSET_5 = 5; 41 constexpr int OFFSET_6 = 6; 42 constexpr int OFFSET_7 = 7; 43 constexpr int OFFSET_8 = 8; 44 constexpr int OFFSET_9 = 9; 45 constexpr int OFFSET_10 = 10; 46 constexpr int OFFSET_11 = 11; 47 constexpr int CONTAINER_TYPE_2 = 2; 48 49 namespace OHOS { 50 namespace Media { 51 class MtpPacketTool { 52 public: 53 MtpPacketTool(); 54 virtual ~MtpPacketTool(); 55 56 static uint16_t GetUInt16(uint8_t numFirst, uint8_t numSecond); 57 static uint32_t GetUInt32(uint8_t numFirst, uint8_t numSecond, uint8_t numThird, uint8_t numFourth); 58 59 static void PutUInt8(std::vector<uint8_t> &outBuffer, uint16_t value); 60 static void PutUInt16(std::vector<uint8_t> &outBuffer, uint16_t value); 61 static void PutUInt32(std::vector<uint8_t> &outBuffer, uint32_t value); 62 static void PutUInt64(std::vector<uint8_t> &outBuffer, uint64_t value); 63 static void PutUInt128(std::vector<uint8_t> &outBuffer, uint64_t value); 64 static void PutUInt128(std::vector<uint8_t> &outBuffer, const uint128_t value); 65 static void PutAUInt16(std::vector<uint8_t> &outBuffer, const uint16_t *values, int count); 66 static void PutAUInt32(std::vector<uint8_t> &outBuffer, const uint32_t *values, int count); 67 static void PutInt8(std::vector<uint8_t> &outBuffer, int8_t value); 68 static void PutInt16(std::vector<uint8_t> &outBuffer, int16_t value); 69 static void PutInt32(std::vector<uint8_t> &outBuffer, int32_t value); 70 static void PutInt64(std::vector<uint8_t> &outBuffer, int64_t value); 71 static void PutInt128(std::vector<uint8_t> &outBuffer, int64_t value); 72 static void PutInt128(std::vector<uint8_t> &outBuffer, const int128_t value); 73 static void PutString(std::vector<uint8_t> &outBuffer, const std::string &string); 74 75 static uint8_t GetUInt8(const std::vector<uint8_t> &buffer, size_t &offset); 76 static uint16_t GetUInt16(const std::vector<uint8_t> &buffer, size_t &offset); 77 static uint32_t GetUInt32(const std::vector<uint8_t> &buffer, size_t &offset); 78 static std::shared_ptr<UInt16List> GetAUInt16(const std::vector<uint8_t> &buffer, size_t &offset); 79 static std::shared_ptr<UInt32List> GetAUInt32(const std::vector<uint8_t> &buffer, size_t &offset); 80 static bool GetUInt8(const std::vector<uint8_t> &buffer, size_t &offset, uint8_t &value); 81 static bool GetUInt16(const std::vector<uint8_t> &buffer, size_t &offset, uint16_t &value); 82 static bool GetUInt32(const std::vector<uint8_t> &buffer, size_t &offset, uint32_t &value); 83 static bool GetUInt64(const std::vector<uint8_t> &buffer, size_t &offset, uint64_t &value); 84 static bool GetUInt128(const std::vector<uint8_t> &buffer, size_t &offset, uint128_t &value); 85 static bool GetInt8(const std::vector<uint8_t> &buffer, size_t &offset, int8_t &value); 86 static bool GetInt16(const std::vector<uint8_t> &buffer, size_t &offset, int16_t &value); 87 static bool GetInt32(const std::vector<uint8_t> &buffer, size_t &offset, int32_t &value); 88 static bool GetInt64(const std::vector<uint8_t> &buffer, size_t &offset, int64_t &value); 89 static bool GetInt128(const std::vector<uint8_t> &buffer, size_t &offset, int128_t &value); 90 static std::string GetString(const std::vector<uint8_t> &buffer, size_t &offset); 91 static std::u16string Utf8ToUtf16(const std::string &inputStr); 92 static std::string Utf16ToUtf8(const std::u16string &inputStr); 93 static bool GetString(const std::vector<uint8_t> &buffer, size_t &offset, std::string &str); 94 static std::string FormatDateTime(time_t sec); 95 static const std::string &GetOperationName(uint16_t code); 96 static const std::string &GetEventName(uint16_t code); 97 static const std::string &GetFormatName(uint16_t code); 98 static const std::string &GetObjectPropName(uint16_t code); 99 static const std::string &GetDataTypeName(int type); 100 static const std::string &GetAssociationName(int type); 101 static const std::string &CodeToStrByMap(int type, const std::map<int, std::string> &theMap); 102 static const std::string &CodeToStrByMap(uint32_t code, const std::map<uint32_t, std::string> &theMap); 103 static int GetObjectPropTypeByPropCode(uint16_t propCode); 104 105 static bool Int8ToString(const int8_t &value, std::string &outStr); 106 static bool UInt8ToString(const uint8_t &value, std::string &outStr); 107 static bool Int16ToString(const int16_t &value, std::string &outStr); 108 static bool UInt16ToString(const uint16_t &value, std::string &outStr); 109 static bool Int32ToString(const int32_t &value, std::string &outStr); 110 static bool UInt32ToString(const uint32_t &value, std::string &outStr); 111 static bool Int64ToString(const int64_t &value, std::string &outStr); 112 static bool UInt64ToString(const uint64_t &value, std::string &outStr); 113 static bool Int128ToString(const int128_t &value, std::string &outStr); 114 static bool UInt128ToString(const uint128_t &value, std::string &outStr); 115 static std::string StrToString(const std::string &value); 116 117 static const std::string &GetIndentBlank(); 118 static std::string GetIndentBlank(size_t indent); 119 static bool CanDump(); 120 static void DumpPacket(const std::vector<uint8_t> &outBuffer); 121 static void Dump(const std::vector<uint8_t> &data, uint32_t offset = 0, uint32_t sum = UINT32_MAX); 122 123 private: 124 static bool DumpClear(size_t loc, std::unique_ptr<char[]> &hexBuf, int hexBufSize, 125 std::unique_ptr<char[]> &txtBuf, int txtBufSize); 126 static bool DumpChar(uint8_t u8, std::unique_ptr<char[]> &hexBuf, int hexBufSize, 127 std::unique_ptr<char[]> &txtBuf, int txtBufSize); 128 static void DumpShow(const std::unique_ptr<char[]> &hexBuf, int hexBufSize, 129 const std::unique_ptr<char[]> &txtBuf, int txtBufSize); 130 }; 131 } // namespace Media 132 } // namespace OHOS 133 #endif // FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_PAYLOAD_DATA_H_ 134