127b27ec6Sopenharmony_ci#include <stdint.h>
227b27ec6Sopenharmony_ci#include <stdio.h>
327b27ec6Sopenharmony_ci#include <stdlib.h>
427b27ec6Sopenharmony_ci
527b27ec6Sopenharmony_ci#include "fuzz.h"
627b27ec6Sopenharmony_ci
727b27ec6Sopenharmony_ci/**
827b27ec6Sopenharmony_ci * Main procedure for standalone fuzzing engine.
927b27ec6Sopenharmony_ci *
1027b27ec6Sopenharmony_ci * Reads filenames from the argument array. For each filename, read the file
1127b27ec6Sopenharmony_ci * into memory and then call the fuzzing interface with the data.
1227b27ec6Sopenharmony_ci */
1327b27ec6Sopenharmony_ciint main(int argc, char **argv)
1427b27ec6Sopenharmony_ci{
1527b27ec6Sopenharmony_ci  int ii;
1627b27ec6Sopenharmony_ci  for(ii = 1; ii < argc; ii++)
1727b27ec6Sopenharmony_ci  {
1827b27ec6Sopenharmony_ci    FILE *infile;
1927b27ec6Sopenharmony_ci    printf("[%s] ", argv[ii]);
2027b27ec6Sopenharmony_ci
2127b27ec6Sopenharmony_ci    /* Try and open the file. */
2227b27ec6Sopenharmony_ci    infile = fopen(argv[ii], "rb");
2327b27ec6Sopenharmony_ci    if(infile)
2427b27ec6Sopenharmony_ci    {
2527b27ec6Sopenharmony_ci      uint8_t *buffer = NULL;
2627b27ec6Sopenharmony_ci      size_t buffer_len;
2727b27ec6Sopenharmony_ci
2827b27ec6Sopenharmony_ci      printf("Opened.. ");
2927b27ec6Sopenharmony_ci
3027b27ec6Sopenharmony_ci      /* Get the length of the file. */
3127b27ec6Sopenharmony_ci      fseek(infile, 0L, SEEK_END);
3227b27ec6Sopenharmony_ci      buffer_len = ftell(infile);
3327b27ec6Sopenharmony_ci
3427b27ec6Sopenharmony_ci      /* Reset the file indicator to the beginning of the file. */
3527b27ec6Sopenharmony_ci      fseek(infile, 0L, SEEK_SET);
3627b27ec6Sopenharmony_ci
3727b27ec6Sopenharmony_ci      /* Allocate a buffer for the file contents. */
3827b27ec6Sopenharmony_ci      buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t));
3927b27ec6Sopenharmony_ci      if(buffer)
4027b27ec6Sopenharmony_ci      {
4127b27ec6Sopenharmony_ci        /* Read all the text from the file into the buffer. */
4227b27ec6Sopenharmony_ci        fread(buffer, sizeof(uint8_t), buffer_len, infile);
4327b27ec6Sopenharmony_ci        printf("Read %zu bytes, fuzzing.. ", buffer_len);
4427b27ec6Sopenharmony_ci
4527b27ec6Sopenharmony_ci        /* Call the fuzzer with the data. */
4627b27ec6Sopenharmony_ci        LLVMFuzzerTestOneInput(buffer, buffer_len);
4727b27ec6Sopenharmony_ci
4827b27ec6Sopenharmony_ci        printf("complete !!");
4927b27ec6Sopenharmony_ci
5027b27ec6Sopenharmony_ci        /* Free the buffer as it's no longer needed. */
5127b27ec6Sopenharmony_ci        free(buffer);
5227b27ec6Sopenharmony_ci        buffer = NULL;
5327b27ec6Sopenharmony_ci      }
5427b27ec6Sopenharmony_ci      else
5527b27ec6Sopenharmony_ci      {
5627b27ec6Sopenharmony_ci        fprintf(stderr,
5727b27ec6Sopenharmony_ci                "[%s] Failed to allocate %zu bytes \n",
5827b27ec6Sopenharmony_ci                argv[ii],
5927b27ec6Sopenharmony_ci                buffer_len);
6027b27ec6Sopenharmony_ci      }
6127b27ec6Sopenharmony_ci
6227b27ec6Sopenharmony_ci      /* Close the file as it's no longer needed. */
6327b27ec6Sopenharmony_ci      fclose(infile);
6427b27ec6Sopenharmony_ci      infile = NULL;
6527b27ec6Sopenharmony_ci    }
6627b27ec6Sopenharmony_ci    else
6727b27ec6Sopenharmony_ci    {
6827b27ec6Sopenharmony_ci      /* Failed to open the file. Maybe wrong name or wrong permissions? */
6927b27ec6Sopenharmony_ci      fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
7027b27ec6Sopenharmony_ci    }
7127b27ec6Sopenharmony_ci
7227b27ec6Sopenharmony_ci    printf("\n");
7327b27ec6Sopenharmony_ci  }
7427b27ec6Sopenharmony_ci}
75