1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16//#define LOG_NDEBUG 0
17#define LOG_TAG "MediaCas"
18
19#include <media/MediaCasDefs.h>
20#include <utils/Log.h>
21#include <binder/IMemory.h>
22
23namespace android {
24namespace media {
25
26///////////////////////////////////////////////////////////////////////////////
27namespace MediaCas {
28
29status_t ParcelableCasData::readFromParcel(const Parcel* parcel) {
30    return parcel->readByteVector(this);
31}
32
33status_t ParcelableCasData::writeToParcel(Parcel* parcel) const  {
34    return parcel->writeByteVector(*this);
35}
36
37///////////////////////////////////////////////////////////////////////////////
38
39status_t ParcelableCasPluginDescriptor::readFromParcel(const Parcel* /*parcel*/) {
40    ALOGE("CAPluginDescriptor::readFromParcel() shouldn't be called");
41    return INVALID_OPERATION;
42}
43
44status_t ParcelableCasPluginDescriptor::writeToParcel(Parcel* parcel) const {
45    status_t err = parcel->writeInt32(mCASystemId);
46    if (err != NO_ERROR) {
47        return err;
48    }
49    return parcel->writeString16(mName);
50}
51
52} // namespace MediaCas
53///////////////////////////////////////////////////////////////////////////////
54
55namespace MediaDescrambler {
56
57DescrambleInfo::DescrambleInfo() {}
58
59DescrambleInfo::~DescrambleInfo() {}
60
61status_t DescrambleInfo::readFromParcel(const Parcel* parcel) {
62    status_t err = parcel->readInt32((int32_t*)&dstType);
63    if (err != OK) {
64        return err;
65    }
66    if (dstType != kDestinationTypeNativeHandle
67            && dstType != kDestinationTypeVmPointer) {
68        return BAD_VALUE;
69    }
70
71    err = parcel->readInt32((int32_t*)&scramblingControl);
72    if (err != OK) {
73        return err;
74    }
75
76    err = parcel->readUint32((uint32_t*)&numSubSamples);
77    if (err != OK) {
78        return err;
79    }
80    if (numSubSamples > 0xffff) {
81        return BAD_VALUE;
82    }
83
84    subSamples = new DescramblerPlugin::SubSample[numSubSamples];
85    if (subSamples == NULL) {
86        return NO_MEMORY;
87    }
88
89    for (size_t i = 0; i < numSubSamples; i++) {
90        err = parcel->readUint32(&subSamples[i].mNumBytesOfClearData);
91        if (err != OK) {
92            return err;
93        }
94        err = parcel->readUint32(&subSamples[i].mNumBytesOfEncryptedData);
95        if (err != OK) {
96            return err;
97        }
98    }
99
100    srcMem = interface_cast<IMemory>(parcel->readStrongBinder());
101    if (srcMem == NULL) {
102        return BAD_VALUE;
103    }
104
105    err = parcel->readInt32(&srcOffset);
106    if (err != OK) {
107        return err;
108    }
109
110    native_handle_t *nativeHandle = NULL;
111    if (dstType == kDestinationTypeNativeHandle) {
112        nativeHandle = parcel->readNativeHandle();
113        dstPtr = static_cast<void *>(nativeHandle);
114    } else {
115        dstPtr = NULL;
116    }
117
118    err = parcel->readInt32(&dstOffset);
119    if (err != OK) {
120        return err;
121    }
122
123    return OK;
124}
125
126status_t DescrambleInfo::writeToParcel(Parcel* parcel) const {
127    if (dstType != kDestinationTypeNativeHandle
128            && dstType != kDestinationTypeVmPointer) {
129        return BAD_VALUE;
130    }
131
132    status_t err = parcel->writeInt32((int32_t)dstType);
133    if (err != OK) {
134        return err;
135    }
136
137    err = parcel->writeInt32(scramblingControl);
138    if (err != OK) {
139        return err;
140    }
141
142    err = parcel->writeUint32(numSubSamples);
143    if (err != OK) {
144        return err;
145    }
146
147    for (size_t i = 0; i < numSubSamples; i++) {
148        err = parcel->writeUint32(subSamples[i].mNumBytesOfClearData);
149        if (err != OK) {
150            return err;
151        }
152        err = parcel->writeUint32(subSamples[i].mNumBytesOfEncryptedData);
153        if (err != OK) {
154            return err;
155        }
156    }
157
158    err = parcel->writeStrongBinder(IInterface::asBinder(srcMem));
159    if (err != OK) {
160        return err;
161    }
162
163    err = parcel->writeInt32(srcOffset);
164    if (err != OK) {
165        return err;
166    }
167
168    if (dstType == kDestinationTypeNativeHandle) {
169        parcel->writeNativeHandle(static_cast<native_handle_t *>(dstPtr));
170    }
171
172    err = parcel->writeInt32(dstOffset);
173    if (err != OK) {
174        return err;
175    }
176
177    return OK;
178}
179
180} // namespace MediaDescrambler
181
182} // namespace media
183} // namespace android
184
185