/*
 * 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 <ft2build.h>
#include FT_FREETYPE_H
#include "../includes/common.h"
#include <freetype/internal/ftdebug.h>

FT_Face face = nullptr;
FT_Library library = nullptr;

void exitHandler(void) {
  if (face) {
    FT_Done_Face(face);
  }
  if (library) {
    FT_Done_FreeType(library);
  }
}

int main(int argc, char **argv) {
  FAIL_CHECK(argc == 3);
  atexit(exitHandler);
  // Initialize FreeType library
  FT_Error error = FT_Init_FreeType(&library);
  FAIL_CHECK(!error);

  // Load the font file
  const int face_index = -1;
  error = FT_New_Face(library, argv[2], face_index, &face);
  FAIL_CHECK(!error);
  if (strcmp(argv[1], "CVE-2022-27406")) {
    return (face->face_index == face_index) ? EXIT_VULNERABLE : EXIT_SUCCESS;
  }
  face->size = nullptr;
  error = FT_Set_Char_Size(face, 0 /* char_width */, 0 /* char_height */,
                           0 /* horz_resolution */, 0 /* vert_resolution */);
  FAIL_CHECK((error == FT_THROW(Invalid_Size_Handle)));
  return EXIT_SUCCESS;
}
