#ident "$Id: decomp_common.c,v 1.7 2008/08/08 18:32:27 cnguyen 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.
 *
 */

/*
 * 
 */

#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 <netinet/in.h>         // For ntohl()
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>           // For struct stat
#include <sys/types.h>          // For off_t
#include <unistd.h>

#include "disk_format.h"
#include "decomp_common.h"
#include "tool_common.h"


const char* partitionTypeToString[] = {"BootGame", "Update", "Game"};
const int   NumPartitionTypes = 3;

extern char* ProgName;

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

bool
readPartitionLengths(FILE *inFile,
                     const char* const inFileName,
                     const u32 numPartitions,
                     const DF_PartitionInfo* const partitionInfoArray,
                     const s64 diskOffsetToFile,
                     off_t *partitionLengths)
{
    bool        returnVal = false;
    off_t       fileLength, netFileLength;

    if (fileSize(inFileName, &fileLength) == false) {
        fprintf(stderr, "%s: Can't get length of file %s - exit\n",
                ProgName, inFileName);
        goto end;
    }
    netFileLength = fileLength + diskOffsetToFile;

    for (int i = 0; i < numPartitions; i++) {
        partition_t     partition;
        off_t           partitionAddr, partitionDataStart;
        off_t           lengthToDataArea;

        partitionAddr = (off_t) (partitionInfoArray[i].gamePartition);
        partitionAddr = UNDO_DISK_ADDR(partitionAddr);


        if (fseeko(inFile, partitionAddr - diskOffsetToFile, SEEK_SET) == -1) {
            fprintf (stderr, "%s: Seeking to partition offset failed\n",
                     ProgName);
            goto end;
        }
        if (fread(&partition, sizeof(partition_t), 1, inFile) != 1) {
            fprintf(stderr, "%s: Can't read partiton %d from file\n",
                    ProgName, i);
            goto end;
        }
        lengthToDataArea = UNDO_DISK_ADDR((off_t)partition.encryptedAreaOffset);
        partitionDataStart = partitionAddr + lengthToDataArea;
                            
        partitionLengths[i] = ((u64) ntohl(partition.partDataLengthWords)) << 2;
        /*
         * Partition length of 0 means partition was generated before
         * field was defined.
         * Partitions are also always a multiple of 32K, so check for
         * that as well.
         * If partition length isn't usable, use the start of the
         * next partition as the end of the current partition,
         * or the remaining length of the file.
         */
        if (partitionLengths[i] == 0 || partitionLengths[i] & 0x7fff) {
            if (i < numPartitions - 1) {
                off_t nextPartitionStart;

                nextPartitionStart = UNDO_DISK_ADDR(partitionInfoArray[i + 1].gamePartition);
                partitionLengths[i] = nextPartitionStart -  partitionDataStart;
            }
            else {
                partitionLengths[i] = netFileLength - partitionDataStart;
            }
        }
    }
    returnVal = true;

end:
    return(returnVal);
} // readPartitionLengths
/* ------------------------------------------------------------------------ */

/*
 * RETURNS:
 *          Pointer to the tmd on success
 *          NULL - otherwise
 */
ESTitleMeta *
readTmdFromPartition(FILE *inFile,
                     const partition_t* const partition,
                     const off_t partitionAddr,
                     const s64 diskOffsetToFile)

{
    ESTitleMeta         *tmd = NULL;
    u32                 tmdSize;
    off_t               tmdAddr;

    tmdAddr = partitionAddr - diskOffsetToFile
              + UNDO_DISK_ADDR(partition->tmdOffset);
    tmdSize = ntohl(partition->tmdSize);

    if (fseeko(inFile, tmdAddr, SEEK_SET) == -1) {
        fprintf (stderr, "%s: Seeking to partition offset failed\n",
                 ProgName);
        goto end;
    }

    tmd = (ESTitleMeta *) malloc(tmdSize);
    if (tmd == NULL) {
        fprintf(stderr, "%s: Can't allocate space for TMD\n",
                ProgName);
        goto end;
    }
    if (fread(tmd, tmdSize, 1, inFile) != 1) {
        fprintf(stderr, "%s: Can't read TMD from file\n", ProgName);
        free(tmd);
        tmd = NULL;
        goto end;
    }
end:
    return(tmd);
}

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

void *
readCertBlobFromPartition (FILE *inFile,
                           const partition_t* const partition,
                           const off_t partitionAddr,
                           const s64 diskOffsetToFile)
{

    void                *certBlob = NULL;
    u32                 certBlobSize;
    off_t               certAddr;

    certAddr = partitionAddr - diskOffsetToFile
              + UNDO_DISK_ADDR(partition->certBlobOffset);
    certBlobSize = ntohl(partition->certBlobSize);

    if (fseeko(inFile, certAddr, SEEK_SET) == -1) {
        fprintf (stderr, "%s: Seeking to partition's certificates failed\n",
                 ProgName);
        goto end;
    }

    certBlob = (void *) malloc(certBlobSize);

    if (certBlob == NULL) {
        fprintf(stderr, "%s: Can't allocate space for certificates\n",
                ProgName);
        goto end;
    }
    if (fread(certBlob, certBlobSize, 1, inFile) != 1) {
        fprintf(stderr, "%s: Can't read certificates from file\n", ProgName);
        free(certBlob);
        certBlob = NULL;
        goto end;
    }
end:
    return(certBlob);
}
/* ------------------------------------------------------------------------ */
