#ident "$Id: file_pattern.c,v 1.4 2006/10/06 19:21:35 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.
 *
 */

/*
 * This program generates a disk file consisting of a
 * pattern specified in this file
 */

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

#include "types.h"

/* #define FILE_SIZE_BYTES    (1024ULL * 1024ULL * 4) */
/* Number of bytes in 4.7 Gbyte disk		      */
// #define FILE_SIZE_BYTES    4669979776ULL
/* #define FILE_SIZE_BYTES    (1024 * 1024 * 25)      */
/* #define FILE_SIZE_BYTES       (128 * 256)          */
/* #define FILE_SIZE_BYTES       (2 * 1024)           */
#define FILE_SIZE_BYTES    (1024ULL * 1024 * 1024)

#ifdef __CYGWIN__
#define fopen64(name, mode)     _fopen64(name,mode)
#define ftello64(stream)        _ftello64(stream)
/* This should be declared in stdlib.h, but compiler complains anyway. */
long long atoll(const char *STR);
#endif

/*
 u8      pattern[] = {0xde, 0xad, 0xde, 0xad, 0xde, 0xad, 0xde, 0xad,
                      0xbe, 0xef, 0xbe, 0xef, 0xbe, 0xef, 0xbe, 0xef};
*/

u8      pattern[] = {0xfa, 0xce, 0xfa, 0xce, 0xfa, 0xce, 0xfa, 0xce,
                     0xca, 0xfe, 0xca, 0xfe, 0xca, 0xfe, 0xca, 0xfe};

char    *ProgName;
#define WRITE_BUF_SIZE  (1024 * 1024)
int
main(int argc, char *argv[])
{
    FILE        *rawDvd;
    char        *rawName;
    int         bufSizeMultiple, bufSize;
    u8          *buf, *bufPtr;

    u64         fileSizeBytes, bytesToWrite;

    rawName  = "pattern.file";
    ProgName = argv[0];

    if (argc == 2) {
        fileSizeBytes = atoll(argv[1]);
    }
    else if (argc == 1) {
        fileSizeBytes = FILE_SIZE_BYTES;
    }
    else {
        printf ("Usage: %s file_size_in_bytes\n", argv[0]);
        exit(1);
    }
    bufSizeMultiple = WRITE_BUF_SIZE / sizeof(pattern);
    bufSize = bufSizeMultiple * sizeof(pattern);
    buf = (u8 *) malloc (bufSize);
    if (buf == NULL) {
        fprintf(stderr, "Malloc of pattern buffer failed\n");
        exit(1);
    }
    bufPtr = buf;
    for (int i = 0; i < bufSizeMultiple; i++, bufPtr += sizeof(pattern)) {
        memcpy(bufPtr, pattern, sizeof(pattern));
    }

    if ( (rawDvd = fopen (rawName, "wb")) == NULL) {
        printf ("Error: can't open %s for writing - exit\n", rawName);
        exit(1);
    }
    bytesToWrite = fileSizeBytes;

    while (bytesToWrite > bufSize) {
        if (fwrite(buf, bufSize, 1, rawDvd) != 1) {
            fprintf (stderr, "%s: Can't write whole buffer - exit\n", ProgName);
            exit(1);
        }
        bytesToWrite -= bufSize;
    }

    if (fwrite(buf, 1, bytesToWrite, rawDvd) != bytesToWrite) {
        fprintf (stderr, "%s: Can't write whole buffer - exit\n", ProgName);
        exit(1);
    }

    fclose(rawDvd);
    exit(0);
}
