已开启
avmemory AshmemGetSize 校验 + Format move 语义与 AnyCast 取值安全化 #2749
lzm创建于 18 天前
avmemory AshmemGetSize 校验 + Format move 语义与 AnyCast 取值安全化 #2749
已开启
共 2 个文件变更+209-116
| @@ -43,6 +43,8 @@ enum FormatDataType : uint32_t { | |||
| 43 | FORMAT_TYPE_UINT32, | 43 | FORMAT_TYPE_UINT32, |
| 44 | /* Int32 Vector */ | 44 | /* Int32 Vector */ |
| 45 | FORMAT_TYPE_INT32_VECTOR, | 45 | FORMAT_TYPE_INT32_VECTOR, |
| 46 | + /* Int64 Vector */ | ||
| 47 | + FORMAT_TYPE_INT64_VECTOR, | ||
| 46 | }; | 48 | }; |
| 47 | 49 | ||
| 48 | struct FormatData { | 50 | struct FormatData { |
| @@ -15,13 +15,14 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | + | ||
| 18 | 19 | ||
| 19 | 20 | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | 23 | ||
| 23 | namespace { | 24 | namespace { |
| 24 | -constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "Format" }; | 25 | +constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "Format"}; |
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | namespace { | 28 | namespace { |
| @@ -35,82 +36,230 @@ void CopyFormatVectorMap(const Format::FormatVectorMap &from, Format::FormatVect | |||
| 35 | } | 36 | } |
| 36 | 37 | ||
| 37 | 38 | ||
| 38 | -bool PutIntValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, int32_t value) | 39 | +using PutFunc = bool (*)(const Meta &meta, FormatDataMap &out, const std::string_view &key, const Any &any); |
| 40 | + | ||
| 41 | +bool PutIntValueToFormatMap(const Meta &meta, FormatDataMap &out, const std::string_view &key, const Any &any) | ||
| 39 | { | 42 | { |
| 43 | + int32_t v = 0; | ||
| 44 | + if (!GetMetaData(meta, std::string(key), v)) { | ||
| 45 | + const int32_t *p = AnyCast<int32_t>(&any); | ||
| 46 | + if (p == nullptr) { | ||
| 47 | + return false; | ||
| 48 | + } | ||
| 49 | + v = *p; | ||
| 50 | + } | ||
| 40 | FormatData data; | 51 | FormatData data; |
| 41 | data.type = FORMAT_TYPE_INT32; | 52 | data.type = FORMAT_TYPE_INT32; |
| 42 | - data.val.int32Val = value; | 53 | + data.val.int32Val = v; |
| 43 | - auto ret = formatMap.insert(std::make_pair(std::string(key), data)); | 54 | + return out.insert(std::make_pair(std::string(key), data)).second; |
| 44 | - return ret.second; | ||
| 45 | } | 55 | } |
| 46 | 56 | ||
| 47 | -bool PutUintValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, uint32_t value) | 57 | +bool PutUintValueToFormatMap(const Meta &meta, FormatDataMap &out, const std::string_view &key, const Any &any) |
| 48 | { | 58 | { |
| 59 | + uint32_t v = 0; | ||
| 60 | + if (!GetMetaData(meta, std::string(key), v)) { | ||
| 61 | + const uint32_t *p = AnyCast<uint32_t>(&any); | ||
| 62 | + if (p == nullptr) { | ||
| 63 | + return false; | ||
| 64 | + } | ||
| 65 | + v = *p; | ||
| 66 | + } | ||
| 49 | FormatData data; | 67 | FormatData data; |
| 50 | data.type = FORMAT_TYPE_UINT32; | 68 | data.type = FORMAT_TYPE_UINT32; |
| 51 | - data.val.uint32Val = value; | 69 | + data.val.uint32Val = v; |
| 52 | - auto ret = formatMap.insert(std::make_pair(std::string(key), data)); | 70 | + return out.insert(std::make_pair(std::string(key), data)).second; |
| 53 | - return ret.second; | ||
| 54 | } | 71 | } |
| 55 | 72 | ||
| 56 | -bool PutLongValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, int64_t value) | 73 | +bool PutLongValueToFormatMap(const Meta &meta, FormatDataMap &out, const std::string_view &key, const Any &any) |
| 57 | { | 74 | { |
| 75 | + int64_t v = 0; | ||
| 76 | + if (!GetMetaData(meta, std::string(key), v)) { | ||
| 77 | + const int64_t *p = AnyCast<int64_t>(&any); | ||
| 78 | + if (p == nullptr) { | ||
| 79 | + return false; | ||
| 80 | + } | ||
| 81 | + v = *p; | ||
| 82 | + } | ||
| 58 | FormatData data; | 83 | FormatData data; |
| 59 | data.type = FORMAT_TYPE_INT64; | 84 | data.type = FORMAT_TYPE_INT64; |
| 60 | - data.val.int64Val = value; | 85 | + data.val.int64Val = v; |
| 61 | - auto ret = formatMap.insert(std::make_pair(std::string(key), data)); | 86 | + return out.insert(std::make_pair(std::string(key), data)).second; |
| 62 | - return ret.second; | ||
| 63 | } | 87 | } |
| 64 | 88 | ||
| 65 | -bool PutFloatValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, float value) | 89 | +bool PutFloatValueToFormatMap(const Meta &, FormatDataMap &out, const std::string_view &key, const Any &any) |
| 66 | { | 90 | { |
| 67 | FormatData data; | 91 | FormatData data; |
| 68 | data.type = FORMAT_TYPE_FLOAT; | 92 | data.type = FORMAT_TYPE_FLOAT; |
| 69 | - data.val.floatVal = value; | 93 | + data.val.floatVal = AnyCast<float>(any); |
| 70 | - auto ret = formatMap.insert(std::make_pair(std::string(key), data)); | 94 | + return out.insert(std::make_pair(std::string(key), data)).second; |
| 71 | - return ret.second; | ||
| 72 | } | 95 | } |
| 73 | 96 | ||
| 74 | -bool PutDoubleValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, double value) | 97 | +bool PutDoubleValueToFormatMap(const Meta &, FormatDataMap &out, const std::string_view &key, const Any &any) |
| 75 | { | 98 | { |
| 76 | FormatData data; | 99 | FormatData data; |
| 77 | data.type = FORMAT_TYPE_DOUBLE; | 100 | data.type = FORMAT_TYPE_DOUBLE; |
| 78 | - data.val.doubleVal = value; | 101 | + data.val.doubleVal = AnyCast<double>(any); |
| 79 | - auto ret = formatMap.insert(std::make_pair(std::string(key), data)); | 102 | + return out.insert(std::make_pair(std::string(key), data)).second; |
| 80 | - return ret.second; | ||
| 81 | } | 103 | } |
| 82 | 104 | ||
| 83 | -bool PutStringValueToFormatMap(FormatDataMap &formatMap, const std::string_view &key, const std::string_view &value) | 105 | +bool PutStringValueToFormatMap(const Meta &, FormatDataMap &out, const std::string_view &key, const Any &any) |
| 84 | { | 106 | { |
| 85 | FormatData data; | 107 | FormatData data; |
| 86 | data.type = FORMAT_TYPE_STRING; | 108 | data.type = FORMAT_TYPE_STRING; |
| 87 | - data.stringVal = value; | 109 | + data.stringVal = AnyCast<std::string>(any); |
| 88 | - auto ret = formatMap.insert(std::make_pair(std::string(key), data)); | 110 | + return out.insert(std::make_pair(std::string(key), data)).second; |
| 89 | - return ret.second; | ||
| 90 | } | 111 | } |
| 91 | 112 | ||
| 92 | -bool PutBufferToFormatMap(FormatDataMap &formatMap, const std::string_view &key, uint8_t *addr, size_t size) | 113 | +bool PutBufferToFormatMap(const Meta &, FormatDataMap &out, const std::string_view &key, const Any &any) |
| 93 | { | 114 | { |
| 94 | - FormatData data; | 115 | + auto *buf = AnyCast<std::vector<uint8_t>>(const_cast<Any *>(&any)); |
| 116 | + if (buf == nullptr) { | ||
| 117 | + return false; | ||
| 118 | + } | ||
| 119 | + uint8_t *addr = buf->data(); | ||
| 95 | FALSE_RETURN_V_MSG_E(addr != nullptr, false, "PutBuffer error, addr is nullptr"); | 120 | FALSE_RETURN_V_MSG_E(addr != nullptr, false, "PutBuffer error, addr is nullptr"); |
| 121 | + FormatData data; | ||
| 96 | data.type = FORMAT_TYPE_ADDR; | 122 | data.type = FORMAT_TYPE_ADDR; |
| 97 | data.addr = addr; | 123 | data.addr = addr; |
| 98 | - data.size = size; | 124 | + data.size = buf->size(); |
| 99 | - auto ret = formatMap.insert(std::make_pair(std::string(key), data)); | 125 | + return out.insert(std::make_pair(std::string(key), data)).second; |
| 100 | - return ret.second; | ||
| 101 | } | 126 | } |
| 102 | 127 | ||
| 103 | -bool PutIntBufferToFormatMap(FormatDataMap &formatMap, const std::string_view &key, int32_t *addr, size_t size) | 128 | +bool PutInt32BufferToFormatMap(const Meta &, FormatDataMap &out, const std::string_view &key, const Any &any) |
| 104 | { | 129 | { |
| 105 | - FormatData data; | 130 | + auto *vec = AnyCast<std::vector<int32_t>>(const_cast<Any *>(&any)); |
| 131 | + if (vec == nullptr) { | ||
| 132 | + return false; | ||
| 133 | + } | ||
| 134 | + uint8_t *addr = reinterpret_cast<uint8_t *>(vec->data()); | ||
| 106 | FALSE_RETURN_V_MSG_E(addr != nullptr, false, "PutBuffer error, addr is nullptr"); | 135 | FALSE_RETURN_V_MSG_E(addr != nullptr, false, "PutBuffer error, addr is nullptr"); |
| 136 | + FormatData data; | ||
| 107 | data.type = FORMAT_TYPE_ADDR; | 137 | data.type = FORMAT_TYPE_ADDR; |
| 108 | - data.addr = reinterpret_cast<uint8_t*>(addr); | 138 | + data.addr = addr; |
| 109 | - data.size = size * sizeof(int32_t); | 139 | + data.size = vec->size() * sizeof(int32_t); |
| 110 | - auto ret = formatMap.insert(std::make_pair(std::string(key), data)); | 140 | + return out.insert(std::make_pair(std::string(key), data)).second; |
| 111 | - return ret.second; | ||
| 112 | } | 141 | } |
| 142 | + | ||
| 143 | +bool PutInt64BufferToFormatMap(const Meta &, FormatDataMap &out, const std::string_view &key, const Any &any) | ||
| 144 | +{ | ||
| 145 | + auto *vec = AnyCast<std::vector<int64_t>>(const_cast<Any *>(&any)); | ||
| 146 | + if (vec == nullptr) { | ||
| 147 | + return false; | ||
| 148 | + } | ||
| 149 | + uint8_t *addr = reinterpret_cast<uint8_t *>(vec->data()); | ||
| 150 | + FALSE_RETURN_V_MSG_E(addr != nullptr, false, "PutBuffer error, addr is nullptr"); | ||
| 151 | + FormatData data; | ||
| 152 | + data.type = FORMAT_TYPE_ADDR; | ||
| 153 | + data.addr = addr; | ||
| 154 | + data.size = vec->size() * sizeof(int64_t); | ||
| 155 | + return out.insert(std::make_pair(std::string(key), data)).second; | ||
| 156 | +} | ||
| 157 | + | ||
| 158 | +const std::unordered_map<FormatDataType, PutFunc> putFuncMap = { | ||
| 159 | + {FORMAT_TYPE_INT32, PutIntValueToFormatMap}, | ||
| 160 | + {FORMAT_TYPE_UINT32, PutUintValueToFormatMap}, | ||
| 161 | + {FORMAT_TYPE_INT64, PutLongValueToFormatMap}, | ||
| 162 | + {FORMAT_TYPE_FLOAT, PutFloatValueToFormatMap}, | ||
| 163 | + {FORMAT_TYPE_DOUBLE, PutDoubleValueToFormatMap}, | ||
| 164 | + {FORMAT_TYPE_STRING, PutStringValueToFormatMap}, | ||
| 165 | + {FORMAT_TYPE_ADDR, PutBufferToFormatMap}, | ||
| 166 | + {FORMAT_TYPE_INT32_VECTOR, PutInt32BufferToFormatMap}, | ||
| 167 | + {FORMAT_TYPE_INT64_VECTOR, PutInt64BufferToFormatMap}, | ||
| 168 | +}; | ||
| 113 | 169 | ||
| 170 | + | ||
| 171 | +using StringifyFunc = void (*)(const Meta &meta, std::stringstream &out, const std::string_view &key, | ||
| 172 | + const Any &any); | ||
| 173 | + | ||
| 174 | +void StringifyIntValue(const Meta &meta, std::stringstream &out, const std::string_view &key, const Any &any) | ||
| 175 | +{ | ||
| 176 | + int32_t v = 0; | ||
| 177 | + if (!GetMetaData(meta, std::string(key), v)) { | ||
| 178 | + const int32_t *p = AnyCast<int32_t>(&any); | ||
| 179 | + if (p == nullptr) { | ||
| 180 | + return; | ||
| 181 | + } | ||
| 182 | + v = *p; | ||
| 183 | + } | ||
| 184 | + out << key << " = " << std::to_string(v) << " | "; | ||
| 185 | +} | ||
| 186 | + | ||
| 187 | +void StringifyUintValue(const Meta &meta, std::stringstream &out, const std::string_view &key, const Any &any) | ||
| 188 | +{ | ||
| 189 | + uint32_t v = 0; | ||
| 190 | + if (!GetMetaData(meta, std::string(key), v)) { | ||
| 191 | + const uint32_t *p = AnyCast<uint32_t>(&any); | ||
| 192 | + if (p == nullptr) { | ||
| 193 | + return; | ||
| 194 | + } | ||
| 195 | + v = *p; | ||
| 196 | + } | ||
| 197 | + out << key << " = " << std::to_string(v) << " | "; | ||
| 198 | +} | ||
| 199 | + | ||
| 200 | +void StringifyLongValue(const Meta &meta, std::stringstream &out, const std::string_view &key, const Any &any) | ||
| 201 | +{ | ||
| 202 | + int64_t v = 0; | ||
| 203 | + if (!GetMetaData(meta, std::string(key), v)) { | ||
| 204 | + const int64_t *p = AnyCast<int64_t>(&any); | ||
| 205 | + if (p == nullptr) { | ||
| 206 | + return; | ||
| 207 | + } | ||
| 208 | + v = *p; | ||
| 209 | + } | ||
| 210 | + out << key << " = " << std::to_string(v) << " | "; | ||
| 211 | +} | ||
| 212 | + | ||
| 213 | +void StringifyFloatValue(const Meta &, std::stringstream &out, const std::string_view &key, const Any &any) | ||
| 214 | +{ | ||
| 215 | + out << key << " = " << std::to_string(AnyCast<float>(any)) << " | "; | ||
| 216 | +} | ||
| 217 | + | ||
| 218 | +void StringifyDoubleValue(const Meta &, std::stringstream &out, const std::string_view &key, const Any &any) | ||
| 219 | +{ | ||
| 220 | + out << key << " = " << std::to_string(AnyCast<double>(any)) << " | "; | ||
| 221 | +} | ||
| 222 | + | ||
| 223 | +void StringifyStringValue(const Meta &, std::stringstream &out, const std::string_view &key, const Any &any) | ||
| 224 | +{ | ||
| 225 | + out << key << " = " << AnyCast<std::string>(any) << " | "; | ||
| 226 | +} | ||
| 227 | + | ||
| 228 | +void StringifyBuffer(const Meta &, std::stringstream &out, const std::string_view &key, const Any &any) | ||
| 229 | +{ | ||
| 230 | + const auto *v = AnyCast<std::vector<uint8_t>>(&any); | ||
| 231 | + if (v != nullptr) { | ||
| 232 | + out << key << ", bufferSize = " << v->size() << " | "; | ||
| 233 | + } | ||
| 234 | +} | ||
| 235 | + | ||
| 236 | +void StringifyInt32Buffer(const Meta &, std::stringstream &out, const std::string_view &key, const Any &any) | ||
| 237 | +{ | ||
| 238 | + const auto *v = AnyCast<std::vector<int32_t>>(&any); | ||
| 239 | + if (v != nullptr) { | ||
| 240 | + out << key << ", int32BufferSize = " << v->size() << " | "; | ||
| 241 | + } | ||
| 242 | +} | ||
| 243 | + | ||
| 244 | +void StringifyInt64Buffer(const Meta &, std::stringstream &out, const std::string_view &key, const Any &any) | ||
| 245 | +{ | ||
| 246 | + const auto *v = AnyCast<std::vector<int64_t>>(&any); | ||
| 247 | + if (v != nullptr) { | ||
| 248 | + out << key << ", int64BufferSize = " << v->size() << " | "; | ||
| 249 | + } | ||
| 250 | +} | ||
| 251 | + | ||
| 252 | +const std::unordered_map<FormatDataType, StringifyFunc> stringifyFuncMap = { | ||
| 253 | + {FORMAT_TYPE_INT32, StringifyIntValue}, | ||
| 254 | + {FORMAT_TYPE_UINT32, StringifyUintValue}, | ||
| 255 | + {FORMAT_TYPE_INT64, StringifyLongValue}, | ||
| 256 | + {FORMAT_TYPE_FLOAT, StringifyFloatValue}, | ||
| 257 | + {FORMAT_TYPE_DOUBLE, StringifyDoubleValue}, | ||
| 258 | + {FORMAT_TYPE_STRING, StringifyStringValue}, | ||
| 259 | + {FORMAT_TYPE_ADDR, StringifyBuffer}, | ||
| 260 | + {FORMAT_TYPE_INT32_VECTOR, StringifyInt32Buffer}, | ||
| 261 | + {FORMAT_TYPE_INT64_VECTOR, StringifyInt64Buffer}, | ||
| 262 | +}; | ||
| 114 | } // namespace | 263 | } // namespace |
| 115 | 264 | ||
| 116 | namespace OHOS { | 265 | namespace OHOS { |
| @@ -264,8 +413,9 @@ bool Format::PutBuffer(const std::string_view &key, const uint8_t *addr, size_t | |||
| 264 | 413 | ||
| 265 | auto formatMapIter = formatMap_.find(key); | 414 | auto formatMapIter = formatMap_.find(key); |
| 266 | if (formatMapIter != formatMap_.end()) { | 415 | if (formatMapIter != formatMap_.end()) { |
| 267 | - formatMap_.erase(formatMapIter); | 416 | + formatMapIter->second.type = FORMAT_TYPE_ADDR; |
| 268 | - PutBufferToFormatMap(formatMap_, key, anyAddr, size); | 417 | + formatMapIter->second.addr = anyAddr; |
| 418 | + formatMapIter->second.size = size; | ||
| 269 | } | 419 | } |
| 270 | return true; | 420 | return true; |
| 271 | } | 421 | } |
| @@ -297,8 +447,9 @@ bool Format::PutIntBuffer(const std::string_view &key, const int32_t *addr, size | |||
| 297 | 447 | ||
| 298 | auto formatMapIter = formatMap_.find(key); | 448 | auto formatMapIter = formatMap_.find(key); |
| 299 | if (formatMapIter != formatMap_.end()) { | 449 | if (formatMapIter != formatMap_.end()) { |
| 300 | - formatMap_.erase(formatMapIter); | 450 | + formatMapIter->second.type = FORMAT_TYPE_ADDR; |
| 301 | - PutIntBufferToFormatMap(formatMap_, key, anyAddr, size); | 451 | + formatMapIter->second.addr = reinterpret_cast<uint8_t *>(anyAddr); |
| 452 | + formatMapIter->second.size = size * sizeof(int32_t); | ||
| 302 | } | 453 | } |
| 303 | return true; | 454 | return true; |
| 304 | } | 455 | } |
| @@ -373,8 +524,7 @@ bool Format::PutFormatVector(const std::string_view &key, std::vector<Format> &v | |||
| 373 | bool Format::GetFormatVector(const std::string_view &key, std::vector<Format> &value) const | 524 | bool Format::GetFormatVector(const std::string_view &key, std::vector<Format> &value) const |
| 374 | { | 525 | { |
| 375 | auto iter = formatVecMap_.find(key); | 526 | auto iter = formatVecMap_.find(key); |
| 376 | - FALSE_RETURN_V_MSG_E(iter != formatVecMap_.end(), | 527 | + FALSE_RETURN_V_MSG_E(iter != formatVecMap_.end(), false, "GetFormatVector failed. Key: %{public}s", key.data()); |
| 377 | - false, "GetFormatVector failed. Key: %{public}s", key.data()); | ||
| 378 | value.assign(iter->second.begin(), iter->second.end()); | 528 | value.assign(iter->second.begin(), iter->second.end()); |
| 379 | return true; | 529 | return true; |
| 380 | } | 530 | } |
| @@ -401,7 +551,9 @@ FormatDataType Format::GetValueType(const std::string_view &key) const | |||
| 401 | { | 551 | { |
| 402 | auto iter = meta_->Find(std::string(key)); | 552 | auto iter = meta_->Find(std::string(key)); |
| 403 | if (iter != meta_->end()) { | 553 | if (iter != meta_->end()) { |
| 404 | - if (Any::IsSameTypeWith<int32_t>(iter->second)) { | 554 | + if (Any::IsSameTypeWith<bool>(iter->second)) { |
| 555 | + return FORMAT_TYPE_INT32; | ||
| 556 | + } else if (Any::IsSameTypeWith<int32_t>(iter->second)) { | ||
| 405 | return FORMAT_TYPE_INT32; | 557 | return FORMAT_TYPE_INT32; |
| 406 | } else if (Any::IsSameTypeWith<uint32_t>(iter->second)) { | 558 | } else if (Any::IsSameTypeWith<uint32_t>(iter->second)) { |
| 407 | return FORMAT_TYPE_UINT32; | 559 | return FORMAT_TYPE_UINT32; |
| @@ -417,6 +569,8 @@ FormatDataType Format::GetValueType(const std::string_view &key) const | |||
| 417 | return FORMAT_TYPE_ADDR; | 569 | return FORMAT_TYPE_ADDR; |
| 418 | } else if (Any::IsSameTypeWith<std::vector<int32_t>>(iter->second)) { | 570 | } else if (Any::IsSameTypeWith<std::vector<int32_t>>(iter->second)) { |
| 419 | return FORMAT_TYPE_INT32_VECTOR; | 571 | return FORMAT_TYPE_INT32_VECTOR; |
| 572 | + } else if (Any::IsSameTypeWith<std::vector<int64_t>>(iter->second)) { | ||
| 573 | + return FORMAT_TYPE_INT64_VECTOR; | ||
| 420 | } else { | 574 | } else { |
| 421 | int64_t valueTemp; | 575 | int64_t valueTemp; |
| 422 | bool isLongValue = GetMetaData(*meta_, std::string(key), valueTemp); | 576 | bool isLongValue = GetMetaData(*meta_, std::string(key), valueTemp); |
| @@ -440,44 +594,13 @@ const Format::FormatDataMap &Format::GetFormatMap() const | |||
| 440 | { | 594 | { |
| 441 | 595 | ||
| 442 | FormatDataMap formatTemp; | 596 | FormatDataMap formatTemp; |
| 443 | - bool ret = true; | ||
| 444 | for (auto iter = meta_->begin(); iter != meta_->end(); ++iter) { | 597 | for (auto iter = meta_->begin(); iter != meta_->end(); ++iter) { |
| 445 | - switch (GetValueType(iter->first)) { | 598 | + auto entry = putFuncMap.find(GetValueType(iter->first)); |
| 446 | - case FORMAT_TYPE_INT32: | 599 | + if (entry == putFuncMap.end()) { |
| 447 | - ret = PutIntValueToFormatMap(formatTemp, iter->first, AnyCast<int32_t>(iter->second)); | 600 | + MEDIA_LOG_E("Format::GetFormatMap skip key: %{public}s", iter->first.c_str()); |
| 448 | - break; | 601 | + continue; |
| 449 | - case FORMAT_TYPE_UINT32: | ||
| 450 | - ret = PutUintValueToFormatMap(formatTemp, iter->first, AnyCast<uint32_t>(iter->second)); | ||
| 451 | - break; | ||
| 452 | - case FORMAT_TYPE_INT64: | ||
| 453 | - ret = PutLongValueToFormatMap(formatTemp, iter->first, AnyCast<int64_t>(iter->second)); | ||
| 454 | - break; | ||
| 455 | - case FORMAT_TYPE_FLOAT: | ||
| 456 | - ret = PutFloatValueToFormatMap(formatTemp, iter->first, AnyCast<float>(iter->second)); | ||
| 457 | - break; | ||
| 458 | - case FORMAT_TYPE_DOUBLE: | ||
| 459 | - ret = PutDoubleValueToFormatMap(formatTemp, iter->first, AnyCast<double>(iter->second)); | ||
| 460 | - break; | ||
| 461 | - case FORMAT_TYPE_STRING: | ||
| 462 | - ret = PutStringValueToFormatMap(formatTemp, iter->first, AnyCast<std::string>(iter->second)); | ||
| 463 | - break; | ||
| 464 | - case FORMAT_TYPE_ADDR: { | ||
| 465 | - Any *value = const_cast<Any *>(&(iter->second)); | ||
| 466 | - uint8_t *addr = (AnyCast<std::vector<uint8_t>>(value))->data(); | ||
| 467 | - size_t size = (AnyCast<std::vector<uint8_t>>(value))->size(); | ||
| 468 | - ret = PutBufferToFormatMap(formatTemp, iter->first, addr, size); | ||
| 469 | - break; | ||
| 470 | - } | ||
| 471 | - case FORMAT_TYPE_INT32_VECTOR: { | ||
| 472 | - Any *value = const_cast<Any *>(&(iter->second)); | ||
| 473 | - int32_t *addr = (AnyCast<std::vector<int32_t>>(value))->data(); | ||
| 474 | - size_t size = (AnyCast<std::vector<int32_t>>(value))->size(); | ||
| 475 | - ret = PutIntBufferToFormatMap(formatTemp, iter->first, addr, size); | ||
| 476 | - break; | ||
| 477 | - } | ||
| 478 | - default: | ||
| 479 | - MEDIA_LOG_E("Format::Stringify failed. Key: %{public}s", iter->first.c_str()); | ||
| 480 | } | 602 | } |
| 603 | + bool ret = entry->second(*meta_, formatTemp, iter->first, iter->second); | ||
| 481 | FALSE_LOG_MSG(ret, "Put value to formatMap failed, key = %{public}s", iter->first.c_str()); | 604 | FALSE_LOG_MSG(ret, "Put value to formatMap failed, key = %{public}s", iter->first.c_str()); |
| 482 | } | 605 | } |
| 483 | FormatDataMap *formatMapRef = const_cast<FormatDataMap *>(&formatMap_); | 606 | FormatDataMap *formatMapRef = const_cast<FormatDataMap *>(&formatMap_); |
| @@ -495,44 +618,12 @@ std::string Format::Stringify() const | |||
| 495 | { | 618 | { |
| 496 | std::stringstream dumpStream; | 619 | std::stringstream dumpStream; |
| 497 | for (auto iter = meta_->begin(); iter != meta_->end(); ++iter) { | 620 | for (auto iter = meta_->begin(); iter != meta_->end(); ++iter) { |
| 498 | - switch (GetValueType(iter->first)) { | 621 | + auto entry = stringifyFuncMap.find(GetValueType(iter->first)); |
| 499 | - case FORMAT_TYPE_INT32: | 622 | + if (entry == stringifyFuncMap.end()) { |
| 500 | - dumpStream << iter->first << " = " << std::to_string(AnyCast<int32_t>(iter->second)) << " | "; | 623 | + MEDIA_LOG_E("Format::Stringify skip key: %{public}s", iter->first.c_str()); |
| 501 | - break; | 624 | + continue; |
| 502 | - case FORMAT_TYPE_UINT32: | ||
| 503 | - dumpStream << iter->first << " = " << std::to_string(AnyCast<uint32_t>(iter->second)) << " | "; | ||
| 504 | - break; | ||
| 505 | - case FORMAT_TYPE_INT64: | ||
| 506 | - dumpStream << iter->first << " = " << std::to_string(AnyCast<int64_t>(iter->second)) << " | "; | ||
| 507 | - break; | ||
| 508 | - case FORMAT_TYPE_FLOAT: | ||
| 509 | - dumpStream << iter->first << " = " << std::to_string(AnyCast<float>(iter->second)) << " | "; | ||
| 510 | - break; | ||
| 511 | - case FORMAT_TYPE_DOUBLE: | ||
| 512 | - dumpStream << iter->first << " = " << std::to_string(AnyCast<double>(iter->second)) << " | "; | ||
| 513 | - break; | ||
| 514 | - case FORMAT_TYPE_STRING: | ||
| 515 | - dumpStream << iter->first << " = " << AnyCast<std::string>(iter->second) << " | "; | ||
| 516 | - break; | ||
| 517 | - case FORMAT_TYPE_ADDR: { | ||
| 518 | - Any *value = const_cast<Any *>(&(iter->second)); | ||
| 519 | - if (AnyCast<std::vector<uint8_t>>(value) != nullptr) { | ||
| 520 | - dumpStream << iter->first << ", bufferSize = " << (AnyCast<std::vector<uint8_t>>(value))->size() | ||
| 521 | - << " | "; | ||
| 522 | - } | ||
| 523 | - break; | ||
| 524 | - } | ||
| 525 | - case FORMAT_TYPE_INT32_VECTOR: { | ||
| 526 | - Any *value = const_cast<Any *>(&(iter->second)); | ||
| 527 | - if (AnyCast<std::vector<int32_t>>(value) != nullptr) { | ||
| 528 | - dumpStream << iter->first << ", bufferSize = " << (AnyCast<std::vector<int32_t>>(value))->size() | ||
| 529 | - << " | "; | ||
| 530 | - } | ||
| 531 | - break; | ||
| 532 | - } | ||
| 533 | - default: | ||
| 534 | - MEDIA_LOG_E("Format::Stringify failed. Key: %{public}s", iter->first.c_str()); | ||
| 535 | } | 625 | } |
| 626 | + entry->second(*meta_, dumpStream, iter->first, iter->second); | ||
| 536 | } | 627 | } |
| 537 | return dumpStream.str(); | 628 | return dumpStream.str(); |
| 538 | } | 629 | } |