Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2016 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 
     17 #include <memory>
     18 
     19 #include <binder/Parcel.h>
     20 
     21 #include "UidRangeTest.h"
     22 
     23 using android::net::UidRange;
     24 
     25 extern "C"
     26 JNIEXPORT jbyteArray Java_android_net_UidRangeTest_readAndWriteNative(JNIEnv* env, jclass,
     27         jbyteArray inParcel) {
     28     const UidRange range = unmarshall(env, inParcel);
     29     return marshall(env, range);
     30 }
     31 
     32 extern "C"
     33 JNIEXPORT jint Java_android_net_UidRangeTest_getStart(JNIEnv* env, jclass, jbyteArray inParcel) {
     34     const UidRange range = unmarshall(env, inParcel);
     35     return range.getStart();
     36 }
     37 
     38 extern "C"
     39 JNIEXPORT jint Java_android_net_UidRangeTest_getStop(JNIEnv* env, jclass, jbyteArray inParcel) {
     40     const UidRange range = unmarshall(env, inParcel);
     41     return range.getStop();
     42 }
     43 
     44 
     45 /**
     46  * Reads exactly one UidRange from 'parcelData' assuming that it is a Parcel. Any bytes afterward
     47  * are ignored.
     48  */
     49 UidRange unmarshall(JNIEnv* env, jbyteArray parcelData) {
     50     const int length = env->GetArrayLength(parcelData);
     51 
     52     std::unique_ptr<uint8_t> bytes(new uint8_t[length]);
     53     env->GetByteArrayRegion(parcelData, 0, length, reinterpret_cast<jbyte*>(bytes.get()));
     54 
     55     android::Parcel p;
     56     p.setData(bytes.get(), length);
     57 
     58     UidRange range;
     59     range.readFromParcel(&p);
     60     return range;
     61 }
     62 
     63 /**
     64  * Creates a Java byte[] array and writes the contents of 'range' to it as a Parcel containing
     65  * exactly one object.
     66  *
     67  * Every UidRange maps to a unique parcel object, so both 'marshall(e, unmarshall(e, x))' and
     68  * 'unmarshall(e, marshall(e, x))' should be fixed points.
     69  */
     70 jbyteArray marshall(JNIEnv* env, const UidRange& range) {
     71     android::Parcel p;
     72     range.writeToParcel(&p);
     73     const int length = p.dataSize();
     74 
     75     jbyteArray parcelData = env->NewByteArray(length);
     76     env->SetByteArrayRegion(parcelData, 0, length, reinterpret_cast<const jbyte*>(p.data()));
     77 
     78     return parcelData;
     79 }
     80