diff --git a/frameworks/c/drm_capi/native_mediakeysystem.cpp b/frameworks/c/drm_capi/native_mediakeysystem.cpp index 347a74b51817799c61b13eba8c3ddb391b7f7a36..6b2bf4ce4bfc7b20223adb9b474ecdb547874e70 100644 --- a/frameworks/c/drm_capi/native_mediakeysystem.cpp +++ b/frameworks/c/drm_capi/native_mediakeysystem.cpp @@ -30,6 +30,20 @@ #include "drm_error_code.h" using namespace OHOS::DrmStandard; +static bool IsHex(const std::string& str) +{ + if (str.empty()) { + return false; + } + + for (char c : str) { + if (!std::isxdigit(c)) { + return false; + } + } + + return true; +} bool OH_MediaKeySystem_IsSupported(const char *uuid) { @@ -84,6 +98,32 @@ bool OH_MediaKeySystem_IsSupported3(const char *uuid, const char *mimeType, return isSupported; } +static Drm_ErrCode FillDescription(DRM_MediaKeySystemDescription &desc, + const std::string &name, + const std::string &uuidStr) +{ + if (!name.empty()) { + int32_t ret = strcpy_s(desc.name, sizeof(desc.name) - 1, name.c_str()); + if (ret != 0) { + DRM_ERR_LOG("OH_MediaKeySystem_GetMediaKeySystems memcpy_s description faild!"); + return DRM_ERR_NO_MEMORY; + } + } + + if (uuidStr.size() == sizeof(desc.uuid) * BASE_CONVERSION_OPERATOR) { + for (size_t i = 0; i < uuidStr.size(); i += BASE_CONVERSION_OPERATOR) { + std::string byteStr = uuidStr.substr(i, BASE_CONVERSION_OPERATOR); + if (!IsHex(byteStr)) { + return DRM_ERR_INVALID_VAL; + } + uint8_t byte = static_cast(std::stoi(byteStr, nullptr, HEXADECIMAL)); + desc.uuid[i / BASE_CONVERSION_OPERATOR] = byte; + } + } + + return DRM_ERR_OK; +} + Drm_ErrCode OH_MediaKeySystem_GetMediaKeySystems(DRM_MediaKeySystemDescription *description, uint32_t *count) { DRM_INFO_LOG("OH_MediaKeySystem_GetMediaKeySystems enter"); @@ -91,34 +131,22 @@ Drm_ErrCode OH_MediaKeySystem_GetMediaKeySystems(DRM_MediaKeySystemDescription * DRM_CHECK_AND_RETURN_RET_LOG((count != nullptr), DRM_ERR_INVALID_VAL, "count is nullptr"); std::map keySystemNames; OHOS::sptr fatory = MediaKeySystemFactoryImpl::GetInstance(); - int32_t ret = fatory->GetMediaKeySystems(keySystemNames); DRM_CHECK_AND_RETURN_RET_LOG((*count >= keySystemNames.size()), DRM_ERR_INVALID_VAL, "GetMediaKeySystems failed because the count passed by is too small."); int32_t times = 0; - DRM_CHECK_AND_RETURN_RET_LOG((ret == DRM_ERR_OK), DRM_ERR_UNKNOWN, - "GetMediaKeySystems call Failed!"); - for (auto it = keySystemNames.begin(); it != keySystemNames.end(); it++) { - if (it->first.size() != 0) { - ret = strcpy_s(description[times].name, sizeof(description[times].name) - 1, it->first.c_str()); - if (ret != 0) { - DRM_ERR_LOG("OH_MediaKeySystem_GetMediaKeySystems memcpy_s description faild!"); - return DRM_ERR_NO_MEMORY; - } - } - if (it->second.size() == (sizeof(description[times].uuid) * BASE_CONVERSION_OPERATOR)) { - for (size_t i = 0; i < sizeof(description[times].uuid) * BASE_CONVERSION_OPERATOR; - i += BASE_CONVERSION_OPERATOR) { - std::string byteStr = it->second.substr(i, BASE_CONVERSION_OPERATOR); - uint8_t byte = static_cast(std::stoi(byteStr, nullptr, HEXADECIMAL)); - description[times].uuid[i/BASE_CONVERSION_OPERATOR] = byte; - } + for (const auto &pair : keySystemNames) { + Drm_ErrCode fillRet = FillDescription(description[times], pair.first, pair.second); + if (fillRet != DRM_ERR_OK) { + return fillRet; } times++; } - if (keySystemNames.size() == 0) { + + if (keySystemNames.empty()) { DRM_ERR_LOG("plugin not exist."); return DRM_ERR_UNKNOWN; } + *count = keySystemNames.size(); return DRM_ERR_OK; }