#ident "$Id: random_file.c,v 1.5 2006/08/17 21:29:10 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 hexadecimal words
 * starting with 0 and increasing by 1.  Word N in the file contains
 * value N
 */

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

#include "standard.h"
#include "rand.h"
#include "types.h"

#ifdef __CYGWIN__
/* This should be declared in stdlib.h, but compiler complains anyway. */
long long atoll(const char *STR);
#endif

/* #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)
#endif


int
main(int argc, char *argv[])
{
    FILE        *raw_dvd;
    char        *raw_name;
    u64         i;
    u64         file_size_bytes, file_size_words;
    randctx     context;

    if (argc == 2) {
        file_size_bytes = atoll(argv[1]);
    }
    else if (argc == 1) {
        file_size_bytes = FILE_SIZE_BYTES;
    }
    else {
        printf ("Usage: %s file_size_in_bytes\n", argv[0]);
        exit(1);
    }
    if ((file_size_bytes & 0x3) != 0) {
        fprintf (stderr, "File size must be a multiple of 4 - exit\n");
        exit(1);
    }

    file_size_words = file_size_bytes >> 2;

    raw_name = "image.random";
    /*
    context.randa = 0xdeadbeef;
    context.randb = 0xcafecafe;
    context.randc = 0xbabebabe;
    */
    context.randa = 0x0;
    context.randb = 0x0;
    context.randc = 0x0;
    for (int i=0; i<256; i++) {
	context.randrsl[i]= (u32) 0;
    }
    randinit(&context, TRUE);

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

    for (i = 0; i < file_size_words; i++) {
        u32        rand_num;

        rand_num = rand(&context);
        fwrite (&rand_num, sizeof(u32), 1, raw_dvd);
    }

    fclose(raw_dvd);
    exit(0);
}
