/* bin2c.c	4/9/1996
 * convert a binary (or text) file to C.
 */

/* SPDX-FileCopyrightText: 1995,1996 Sascha Demetrio
 * SPDX-FileCopyrightText: 2009, 2010, 2015, 2018, 2020 Peter Pentchev
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>

#if !defined(fallthrough)
#if defined(__GNUC__) && __GNUC__ >= 7
#define fallthrough __attribute__((fallthrough))
#else
#define fallthrough /* fallthrough */
#endif
#endif

static const char *usage = "\
usage: bin2c [-t] [-h] [-o outputfile] [-n name] [inputfile]\n\
  -t  you can use this option if the inputfile is a small (< 1K) textfile.\n\
  -h  display this help message.\n\
  -o outputfile\n\
      write the output to outputfile.  the default is <stdout>.\n\
  -n name\n\
      the name of the C variable generated.  the default is `data'\n\
  if no inputfile is specified, the input is read from <stdin>.\n\
";

  int
main(int argc, char **argv)
{
  const char *name = "data";
  FILE *in = stdin, *out = stdout;
  int text_f = 0;
  char *outfile = 0;
  int c;
  int i;

  while ((c = getopt(argc, argv, "htn:o:")) != -1)
    switch (c) {
    case 't':  /* generate a C string.  this option useful only
                * for *very* small text files, since some compilers
		* refuse to compile extremely long strings.  */
      ++text_f;
      break;
    case 'n':  /* select the name of the output C array. */
      name = optarg;
      break;
    case 'o':  /* select the name of the output file. */
      outfile = optarg;
      break;
    default:
      fprintf(stderr, "%s: invalid option -- -%c\n", *argv, (char)c);
      fallthrough;
    case 'h':
    case '?':
      fputs(usage, stderr);
      exit(1);
    }
  if (argc - optind > 1) {
    fputs(usage, stderr);
    exit(1);
  } else if (argc - optind == 1)
    if (!(in = fopen(argv[optind], "r"))) {
      perror(argv[optind]);
      exit(1);
    }
  if (outfile)
    if (!(out = fopen(outfile, "w"))) {
      perror(outfile);
      fclose(in);
      exit(1);
    }

  fprintf(out, "/* %s.c\n * generated by bin2c - do not edit!\n */\n\n", name);
/* SPDX-FileCopyrightText: 1995,1996 Sascha Demetrio
 * SPDX-License-Identifier: BSD-3-Clause
 */
  if (text_f) {
    fprintf(out, "char %s[] = \"\\\n", name);
    for (;;) {
      c = getc(in);
      if (feof(in)) break;
      if (!isprint(c))
	switch (c) {
	case '\n':
	  fprintf(out, "\\n\\\n");
	  break;
	case '\b':
	  fprintf(out, "\\b");
	  break;
	default:
	  fprintf(out, "\\%o", (int)(unsigned char)c);
	}
      else
	switch (c) {
	case '\\':
	  fprintf(out, "\\\\");
	  break;
	case '\"':
	  fprintf(out, "\\\"");
	  break;
	default:
	  putc(c, out);
	}
    }
    fprintf(out, "\";\n\n");
  } else {
    fprintf(out, "char %s[] = {", name);
    for (i = 0;; ++i) {
      c = getc(in);
      if (feof(in)) break;
      if (!(i % 8)) fprintf(out, "\n ");
      fprintf(out, " 0x%02x,", (int)(unsigned char)c);
    }
    if (!(i % 8)) fprintf(out, "\n ");
    fprintf(out, " 0\n};\n\n");
      /* we write a trailing 0 so the array can be used as a string. */
  }
  fprintf(out, "/* end of %s.c */\n\n", name);
  fclose(in);
  fclose(out);
  return 0;
}
/* main */

