Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2017 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "DescramblerImpl" |
| 19 | |
| 20 | #include <media/cas/DescramblerAPI.h> |
| 21 | #include <media/DescramblerImpl.h> |
| 22 | #include <media/SharedLibrary.h> |
Chong Zhang | 0211980 | 2017-10-31 15:02:52 -0700 | [diff] [blame] | 23 | #include <media/stagefright/foundation/AUtils.h> |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 24 | #include <binder/IMemory.h> |
Chong Zhang | 0211980 | 2017-10-31 15:02:52 -0700 | [diff] [blame] | 25 | #include <utils/Log.h> |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | static Status getBinderStatus(status_t err) { |
| 30 | if (err == OK) { |
| 31 | return Status::ok(); |
| 32 | } |
| 33 | if (err == BAD_VALUE) { |
| 34 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 35 | } |
| 36 | if (err == INVALID_OPERATION) { |
| 37 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 38 | } |
| 39 | return Status::fromServiceSpecificError(err); |
| 40 | } |
| 41 | |
| 42 | static String8 sessionIdToString(const CasSessionId &sessionId) { |
| 43 | String8 result; |
| 44 | for (size_t i = 0; i < sessionId.size(); i++) { |
| 45 | result.appendFormat("%02x ", sessionId[i]); |
| 46 | } |
| 47 | if (result.isEmpty()) { |
| 48 | result.append("(null)"); |
| 49 | } |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | DescramblerImpl::DescramblerImpl( |
| 54 | const sp<SharedLibrary>& library, DescramblerPlugin *plugin) : |
| 55 | mLibrary(library), mPlugin(plugin) { |
| 56 | ALOGV("CTOR: mPlugin=%p", mPlugin); |
| 57 | } |
| 58 | |
| 59 | DescramblerImpl::~DescramblerImpl() { |
| 60 | ALOGV("DTOR: mPlugin=%p", mPlugin); |
| 61 | release(); |
| 62 | } |
| 63 | |
| 64 | Status DescramblerImpl::setMediaCasSession(const CasSessionId& sessionId) { |
| 65 | ALOGV("setMediaCasSession: sessionId=%s", |
| 66 | sessionIdToString(sessionId).string()); |
| 67 | |
| 68 | return getBinderStatus(mPlugin->setMediaCasSession(sessionId)); |
| 69 | } |
| 70 | |
| 71 | Status DescramblerImpl::requiresSecureDecoderComponent( |
| 72 | const String16& mime, bool *result) { |
| 73 | *result = mPlugin->requiresSecureDecoderComponent(String8(mime)); |
| 74 | |
| 75 | return getBinderStatus(OK); |
| 76 | } |
| 77 | |
Chong Zhang | 0211980 | 2017-10-31 15:02:52 -0700 | [diff] [blame] | 78 | static inline bool validateRangeForSize( |
| 79 | uint64_t offset, uint64_t length, uint64_t size) { |
| 80 | return isInRange<uint64_t, uint64_t>(0, size, offset, length); |
| 81 | } |
| 82 | |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 83 | Status DescramblerImpl::descramble( |
| 84 | const DescrambleInfo& info, int32_t *result) { |
| 85 | ALOGV("descramble"); |
| 86 | |
Chong Zhang | 0211980 | 2017-10-31 15:02:52 -0700 | [diff] [blame] | 87 | if (info.srcMem == NULL || info.srcMem->pointer() == NULL) { |
| 88 | ALOGE("srcMem is invalid"); |
| 89 | return getBinderStatus(BAD_VALUE); |
| 90 | } |
| 91 | |
| 92 | // use 64-bit here to catch bad subsample size that might be overflowing. |
| 93 | uint64_t totalBytesInSubSamples = 0; |
| 94 | for (size_t i = 0; i < info.numSubSamples; i++) { |
| 95 | totalBytesInSubSamples += (uint64_t)info.subSamples[i].mNumBytesOfClearData + |
| 96 | info.subSamples[i].mNumBytesOfEncryptedData; |
| 97 | } |
| 98 | // validate if the specified srcOffset and requested total subsample size |
| 99 | // is consistent with the source shared buffer size. |
| 100 | if (!validateRangeForSize(info.srcOffset, totalBytesInSubSamples, info.srcMem->size())) { |
| 101 | ALOGE("Invalid srcOffset and subsample size: " |
| 102 | "srcOffset %llu, totalBytesInSubSamples %llu, srcMem size %llu", |
| 103 | (unsigned long long) info.srcOffset, |
| 104 | (unsigned long long) totalBytesInSubSamples, |
| 105 | (unsigned long long) info.srcMem->size()); |
| 106 | android_errorWriteLog(0x534e4554, "67962232"); |
| 107 | return getBinderStatus(BAD_VALUE); |
| 108 | } |
| 109 | void *dstPtr = NULL; |
| 110 | if (info.dstType == DescrambleInfo::kDestinationTypeVmPointer) { |
| 111 | // When using shared memory, src buffer is also used as dst |
| 112 | dstPtr = info.srcMem->pointer(); |
| 113 | |
| 114 | // In this case the dst and src would be the same buffer, need to validate |
| 115 | // dstOffset against the buffer size too. |
| 116 | if (!validateRangeForSize(info.dstOffset, totalBytesInSubSamples, info.srcMem->size())) { |
| 117 | ALOGE("Invalid dstOffset and subsample size: " |
| 118 | "dstOffset %llu, totalBytesInSubSamples %llu, srcBuffer size %llu", |
| 119 | (unsigned long long) info.dstOffset, |
| 120 | (unsigned long long) totalBytesInSubSamples, |
| 121 | (unsigned long long) info.srcMem->size()); |
| 122 | android_errorWriteLog(0x534e4554, "67962232"); |
| 123 | return getBinderStatus(BAD_VALUE); |
| 124 | } |
| 125 | } else { |
| 126 | dstPtr = info.dstPtr; |
| 127 | } |
| 128 | |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 129 | *result = mPlugin->descramble( |
| 130 | info.dstType != DescrambleInfo::kDestinationTypeVmPointer, |
| 131 | info.scramblingControl, |
| 132 | info.numSubSamples, |
| 133 | info.subSamples, |
| 134 | info.srcMem->pointer(), |
| 135 | info.srcOffset, |
Chong Zhang | 0211980 | 2017-10-31 15:02:52 -0700 | [diff] [blame] | 136 | dstPtr, |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 137 | info.dstOffset, |
| 138 | NULL); |
| 139 | |
| 140 | return getBinderStatus(*result >= 0 ? OK : *result); |
| 141 | } |
| 142 | |
| 143 | Status DescramblerImpl::release() { |
| 144 | ALOGV("release: mPlugin=%p", mPlugin); |
| 145 | |
| 146 | if (mPlugin != NULL) { |
| 147 | delete mPlugin; |
| 148 | mPlugin = NULL; |
| 149 | } |
| 150 | return Status::ok(); |
| 151 | } |
| 152 | |
| 153 | } // namespace android |
| 154 | |