diff --git a/frameworks/c/drm_capi/native_mediakeysystem.cpp b/frameworks/c/drm_capi/native_mediakeysystem.cpp index 347a74b51817799c61b13eba8c3ddb391b7f7a36..949ce925fe4187db0adb962614f0114ad71ada88 100644 --- a/frameworks/c/drm_capi/native_mediakeysystem.cpp +++ b/frameworks/c/drm_capi/native_mediakeysystem.cpp @@ -30,7 +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) { DRM_INFO_LOG("OH_MediaKeySystem_IsSupported enter."); @@ -84,22 +97,11 @@ bool OH_MediaKeySystem_IsSupported3(const char *uuid, const char *mimeType, return isSupported; } -Drm_ErrCode OH_MediaKeySystem_GetMediaKeySystems(DRM_MediaKeySystemDescription *description, uint32_t *count) -{ - DRM_INFO_LOG("OH_MediaKeySystem_GetMediaKeySystems enter"); - DRM_CHECK_AND_RETURN_RET_LOG((description != nullptr), DRM_ERR_INVALID_VAL, "description is nullptr"); - 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."); +static Drm_ErrCode FillDescriptions(DRM_MediaKeySystemDescription *description, const std::map &keySystemNames) { 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()); + int 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; @@ -109,16 +111,38 @@ Drm_ErrCode OH_MediaKeySystem_GetMediaKeySystems(DRM_MediaKeySystemDescription * 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); + if(!IsHex(byteStr)){ + return DRM_ERR_INVALID_VAL; + } uint8_t byte = static_cast(std::stoi(byteStr, nullptr, HEXADECIMAL)); description[times].uuid[i/BASE_CONVERSION_OPERATOR] = byte; } } times++; } + return DRM_ERR_OK; +} + +Drm_ErrCode OH_MediaKeySystem_GetMediaKeySystems(DRM_MediaKeySystemDescription *description, uint32_t *count) +{ + DRM_INFO_LOG("OH_MediaKeySystem_GetMediaKeySystems enter"); + DRM_CHECK_AND_RETURN_RET_LOG((description != nullptr), DRM_ERR_INVALID_VAL, "description is nullptr"); + 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((ret == DRM_ERR_OK), DRM_ERR_UNKNOWN, "GetMediaKeySystems call Failed!"); + DRM_CHECK_AND_RETURN_RET_LOG((*count >= keySystemNames.size()), DRM_ERR_INVALID_VAL, "GetMediaKeySystems failed because the count passed by is too small."); if (keySystemNames.size() == 0) { DRM_ERR_LOG("plugin not exist."); return DRM_ERR_UNKNOWN; } + + Drm_ErrCode fillRet = FillDescriptions(description, keySystemNames); + if (fillRet != DRM_ERR_OK) { + return fillRet; + } + *count = keySystemNames.size(); return DRM_ERR_OK; }