#ident "$Id: bin2include.c,v 1.2 2006/06/13 18:15:21 craig Exp $"
/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/*
 */


#include <ctype.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>


#if !defined(__CYGWIN__)
static struct option longOptions[] = {
    {"help",            0, 0, 'h'},     /* Print usage */
    {"output",          1, 0, 'o'},     /* Output image name */
    {0, 0, 0, 0}
};
#endif

static char* options = "go:";


char            *ProgName;
static bool     Verbose = false;
bool            generateInclude(const char* const inFileName,
                                const char* const outFileName);
/* ------------------------------------------------------------------------ */

void
usage()
{
    fprintf (stderr, "Usage: %s [options] binary_file\n", ProgName);
    fprintf (stderr,
        " -h, --help                - print usage message\n"
        " -o, --out <file>          - output image name [default: output.image]\n"
            );
}
/* ------------------------------------------------------------------------ */

/*
 * Process the program arguments.
 * OUTPUT:
 *              Sets the titleKey
 */

static void
processOptions(int argc, char* argv[], char **inputName, char **outputName)
{
    ProgName = argv[0];

    // Default values
    *outputName = NULL;

    while (1) {
#if defined(__CYGWIN__)
        int ch = getopt(argc, argv, options);
#else
        int ch = getopt_long_only(argc, argv, options, longOptions, 0);
#endif

        if (ch == -1)
            break;

        switch (ch) {

        case 'h':
            usage();
            exit(0);
            break;

        case 'o':
            *outputName = optarg;
            break;

        case 'v':
            Verbose = true;
            break;


        default:
            usage();
            exit(1);
        }
    }
    if (optind >= argc) {
        fprintf (stderr, "%s: Need input file name\n", argv[0]);
        usage();
        exit(1);
    }
    *inputName = argv[optind];

    if (*outputName == NULL) {
        *outputName = malloc (strlen(*inputName) + strlen(".h") + 1);
        if (*outputName == NULL) {
            fprintf (stderr, "Allocating space for output name failed\n");
            exit(1);
        }
        strcpy(*outputName, *inputName);
        strcat(*outputName, ".h");
    }
} // processOptions
/* ------------------------------------------------------------------------ */

int
main(int argc, char *argv[])
{
    char        *inputFileName, *outputFileName;

    ProgName = argv[0];

    processOptions(argc, argv, &inputFileName, &outputFileName);

    if (generateInclude(inputFileName, outputFileName) == false) {
        fprintf (stderr, "%s: Generation of file failed\n",
                 ProgName);
        exit(1);
    }
    exit(0);
}


/* ------------------------------------------------------------------------ */

bool
generateInclude(const char* const inFileName, const char* const outFileName)
{
    FILE        *inFile, *outFile;
    uint8_t     byte;
    char        buffer[16];
    bool        firstOutput = true, done = false;
    int         byteTotal;

    if ( (inFile = fopen(inFileName, "r")) == NULL) {
        fprintf (stderr, "%s: Can't open file '%s' for reading\n", ProgName,
                                                                   inFileName);
        return(false);
    }
    if ( (outFile = fopen(outFileName, "w")) == NULL) {
        fprintf (stderr, "%s: Can't open file '%s' for writing\n", ProgName,
                                                                   outFileName);
        return(false);
    }
    fprintf (outFile, "/* This is an automatically generated file */\n");
    fprintf (outFile, "\n\n");
    fprintf (outFile, "#include <stdint.h>\n");
    fprintf (outFile, "uint8_t %s_array[] = {\n", outFileName);
    byteTotal = 0;
    do {
        if (fread(&byte, 1, 1, inFile) != 1) {
            done = true;
            if (ferror(inFile)) {
                printf ("Error when reading input file\n");
            }
        }
        else {
            if (firstOutput == true) {
                firstOutput = false;
                sprintf (buffer, "0x%x", byte);
            }
            else {
                sprintf (buffer, ", 0x%x", byte);
            }
            fprintf(outFile, buffer);
            byteTotal++;
            if (byteTotal % 10 == 0) {
                fprintf(outFile, "\n");
            }
        }
    } while (! done);

    fprintf(outFile, "};\n");
    fclose(inFile);
    fclose(outFile);
    return(true);
} // generateInclude
/* ------------------------------------------------------------------------ */
