#ident "$Id: disk_common.c,v 1.6 2007/02/21 00:08:01 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.
 *
 */

/*
 * Functions common to one or more partition commands.
 */

#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#else
#define fseeko(file, where, whence)     fseeko64(file, where, whence)
#define ftello(file)                    ftello64(file)
#endif

/*
 * Need to define this, or else on Cygwin the ticket and tmd
 * structures are laid out differently than on Linux
 */
#define BROADWAY_REV
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <netinet/in.h>         // For htonl()
#include <stdbool.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>

#include "types.h"
#include "disk_common.h"

extern char     *ProgName;

/* Buffer size is arbitrary - make it bigger or smaller if you like */
#define COPY_BUFFER_SIZE        (128 * 1024)

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

/*
 * Copy the contents of file named 'inFileName' starting at position
 * 'startPosition' and of maximum length 'length' to the file
 * pointed to by outFile.  If length is 0, copy the entire file.
 * Open the file in binary mode, which prevents end-of-line translation
 * under Cygwin.
 * The output file is left open.
 */

bool
copyPartialBinFileToOutFile(FILE *outFile, const char* const inFileName,
                            const off_t startPosition, const off_t length)
{
    FILE                *inFile;
    u8                  *buffer = NULL;
    size_t              numRead, numWritten;
    bool                done = false, returnVal = false;
    struct stat         statBuf;
    off_t               bytesToCopy;

    if (stat(inFileName, &statBuf) == -1) {
        fprintf(stderr, "%s: Can't access file '%s' (%s)\n",
        ProgName, inFileName, strerror(errno));
        return(false);
    }
    if (length == 0) {
        bytesToCopy = statBuf.st_size;
    }
    else {
        bytesToCopy = MIN(statBuf.st_size - startPosition, length);
    }
    if ((inFile = fopen(inFileName, "rb")) == NULL) {
        fprintf (stderr, "(%s) Error: Can't open file '%s'\n",
                 ProgName, inFileName);
        return(false);
    }
    if (fseeko(inFile, startPosition, SEEK_SET) != 0) {
        fprintf (stderr, "(%s) Error: Can't seek on file '%s' [%s]\n",
                 ProgName, inFileName, strerror(errno));
        goto end;
    }
    buffer = (u8 *) malloc (COPY_BUFFER_SIZE);
    do {
        size_t  bytesToWrite;

        numRead = fread(buffer, 1, COPY_BUFFER_SIZE, inFile);
        if (numRead != COPY_BUFFER_SIZE) {
            done = true;
            if (ferror(inFile)) {
                fprintf(stderr, "%s: Read error on file %s\n",
                                 ProgName, inFileName);
                goto end;
            }
        }
        bytesToWrite = MIN(numRead, bytesToCopy);
        numWritten = fwrite(buffer, 1, bytesToWrite, outFile);
        if (numWritten != bytesToWrite) {
            fprintf(stderr, "%s: Write error when copying file %s\n",
                    ProgName, inFileName);
            fclose(inFile);
            return(false);
        }
        bytesToCopy -= numWritten;
    } while (! done && bytesToCopy > 0);

    returnVal = true;

end:
    if (inFile != NULL) {
        fclose(inFile);
    }
    if (buffer != NULL) {
        free(buffer);
    }

    return(returnVal);
} // copyFileToOutFile

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


/*
 * Copy the contents of file named 'inFileName' to the file
 * pointed to by outFile.
 * Open the file in binary mode, which prevents end-of-line translation
 * under Cygwin.
 * The output file is left open.
 */

bool
copyBinFileToOutFile(FILE *outFile, const char* const inFileName)
{
    return(copyPartialBinFileToOutFile(outFile, inFileName, 0, 0));
} // copyFileToOutFile

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

/*
 * Read the contents of the file 'fileName' into an array allocated
 * in this routine.  Output the array and its size.
 * RETURNS:     true            if everything went OK
 *              false           otherwise
 *
 * The memory pointed to by *array should be freed by the caller.
 */

bool
readBinFileIntoArray(const char* const fileName, u8 **array, u32 *size)
{
    FILE        *inFile;
    struct stat statBuf;

    *size = 0;
    *array = NULL;

    if (stat(fileName, &statBuf) == -1) {
        fprintf(stderr, "%s: Can't access file '%s' (%s)\n",
        ProgName, fileName, strerror(errno));
        return(false);
    }
    *size = (u32) statBuf.st_size;
    *array = (u8 *) malloc(*size);
    if (*array == NULL) {
        fprintf(stderr,
        "%s: Can't allocate %u bytes to hold the contents of file %s\n",
        ProgName, *size, fileName);
        return(false);
    }
    if ( (inFile = fopen(fileName, "rb")) == NULL) {
        fprintf (stderr, "%s: Can't open file %s\n", ProgName, fileName);
        free(*array);
        return(false);
    }
    if (fread(*array, *size, 1, inFile) != 1) {
        fprintf (stderr, "%s: Can't read file %s\n", ProgName, fileName);
        free(*array);
        fclose(inFile);
        return(false);
    }
    fclose(inFile);
    return(true);
}

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