/*---------------------------------------------------------------------------*
  Project: CTR_TRIAL
  File:    InspectLog.cpp

  Copyright 2010 Nintendo.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., 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 Nintendo.

 *---------------------------------------------------------------------------*/
#include <nn/fs.h>
#include <nn/crypto/crypto_Sha1.h>
#include <nn/cfg/CTR/cfg_ApiInit.h>
#include <nn/ps/CTR/ps_API.h>
#include <nn/ps/CTR/ps_Parameters.h>
#include <nn/socket.h>
#include <nn/Result.h>
#include <string.h>

#include "InspectLog.h"
#include "sys_Memory.h"
#include "sys_GetSerialNumber.h"
#include "sys_GetMacAddress.h"
#include "sys_GetRegionCode.h"


using namespace uji::seq;
using namespace std;


void InspectLog::Dump(void)
{
//メンバ変数
    NN_LOG("m_fileNameMaster (%ls)\n", m_fileNameMaster.c_str());
    NN_LOG("m_fileNameSub    (%ls)\n", m_fileNameSub.c_str());
    NN_LOG("m_pathMaster     (%ls)\n", m_pathMaster.c_str());
    NN_LOG("m_pathSub        (%ls)\n", m_pathSub.c_str());

//
    NN_LOG("m_DeviceIdHash   (%s)\n",  m_DeviceIdHash.c_str());
    NN_LOG("m_RfuMacAddress  (%s)\n",  m_RfuMacAddress.c_str());
    NN_LOG("m_SerialNumber   (%s)\n",  m_SerialNumber.c_str());
    NN_LOG("m_RegionCode     (%s)\n",  m_RegionCode.c_str());
    NN_LOG("m_BondingOption  (%s)\n",  m_BondingOption.c_str());
    NN_LOG("m_Comment        (%s)\n",  m_Comment.c_str());
    NN_LOG("m_CommentUpdated (%s)\n",  m_CommentUpdated.c_str());

}


// データを初期化する
void InspectLog::Clear_DataStructure(void)
{
    m_DeviceIdHash   = "";
    m_RfuMacAddress  = "";
    m_SerialNumber   = "";
    m_RegionCode     = "";
    m_BondingOption  = "";
    m_Comment        = "";
    m_CommentUpdated = "";
}

// データを作成する
LogResult InspectLog::Create_DataStructure(void)
{
    //ローカルフレンドコードシードを取得
    nn::Result nnr;
    nnr = nn::ps::CTR::Initialize();
    if ( nnr.IsFailure())
    {
        NN_LOG("nn::ps::Initialize failed.\n");
        nn::dbg::PrintResult(nnr);
        return LogResultCode::FUNC_CALL_ERROR;
    }
    bit32 deviceId = 0;
    nnr = nn::ps::CTR::GetDeviceId(&deviceId);
    if ( nnr.IsFailure())
    {
        NN_LOG("nn::ps::GetDeviceId failed.\n");
        nn::dbg::PrintResult(nnr);
        return LogResultCode::FUNC_CALL_ERROR;
    }
    char deviceIdStr[10];
    memset(deviceIdStr, 0x00, sizeof(deviceIdStr));
    sprintf(deviceIdStr, "%08X", deviceId);


    bit32 deviceIdHash = 0;
    deviceIdHash = 0xDECABEEF ^ deviceId; //DECA BEEF
    NN_LOG("%08X\n",deviceIdHash);

    deviceIdHash = nn::socket::NtoHl(deviceIdHash);
    NN_LOG("%08X\n",deviceIdHash);

    char deviceIdHashStr[10];
    memset(deviceIdHashStr, 0x00, sizeof(deviceIdHashStr));
    sprintf(deviceIdHashStr, "%08X", deviceIdHash);

    m_DeviceIdHash = deviceIdHashStr;


    //MACアドレスを取得
    bit8 mac[ nn::uds::MAC_ADDRESS_SIZE ];
    uji::sys::GetMacAddress( mac );
    char macStr[12+5+1];
    memset(macStr, 0x00, sizeof(macStr));
    sprintf(macStr, "%02X-%02X-%02X-%02X-%02X-%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    m_RfuMacAddress = macStr;

    //シリアルナンバー取得
    char serial[nn::cfg::CTR::detail::CFG_SECURE_INFO_SERIAL_NO_LEN+1];
    uji::sys::GetSerialNumber(serial);
    m_SerialNumber   = serial;

    //リージョンコード
    nn::cfg::CTR::CfgRegionCode   crc;
    crc = uji::sys::GetRegionCode();
    char crcStr[10];
    memset(crcStr, 0x00, sizeof(crcStr));
    sprintf(crcStr, "%02d", crc);
    m_RegionCode     = crcStr;

    //ボンディングオプション
    nn::cfg::CTR::detail::LocalFriendCodeSeedBody lfcsb;
    nn::cfg::CTR::detail::LocalFriendCodeSeedBody * p;
    p = &lfcsb;
    nn::cfg::CTR::init::GetLocalFriendCodeSeedBody(p, true);

    char bondingOptionStr[10];
    memset(bondingOptionStr, 0x00, sizeof(bondingOptionStr));
    sprintf(bondingOptionStr, "%02d", lfcsb.bondingOption);
    m_BondingOption = bondingOptionStr;

//(注意！）m_Comment, m_CommentUpdated は
//         Set_Commentにより設定される。

    // DataHash計算
    Calc_DataHash();

    return LogResultCode::SUCCESS;
}


//
// ログファイルを初期化する
//
// この関数はログファイルが存在しない状態でしか、使用できない。
// 既にログファイルがある場合は、FS_WRITE_ERRORを強制的に返す。
LogResult InspectLog::InitFile(const wstring& path)
{
    LogResult lr = LogResultCode::SUCCESS;

    char line[LOG_LINEBUF_LEN];
    s32 writeBytes=0;     //書込みサイズ
    s32 writeBytesTotal=0;//書込みサイズ合計

    NN_LOG("%s (%s) called.\n", NN_FUNCTION, path.c_str());
    nn::fs::FileOutputStream fos(path.c_str(), true);

    if ( fos.GetSize() != 0 )
    {
        //前提条件が間違っているため、
        //ここにきたら、FS_WRITE_ERRORを強制的に返す
        lr = LogResultCode::FS_WRITE_ERROR;
        NN_LOG(" fileSize(%d)\n", path.c_str(),fos.GetSize());
        return lr;
    }
    fos.Finalize();

    // データを作成し、ハッシュ値を計算する
    Clear_DataStructure();
    Create_DataStructure();
    Calc_DataHash();

    // Comment以外のデータを書込む
    lr = WriteFile(path,"");
    return lr;
}

// 行（ログデータ)を解析し、データ構造体に登録する
void InspectLog::Analyze_LogLine(u8 * line, int line_len)
{
    u8 * ptr = NULL;
    u8 * ptr2 = NULL;
    int charCount =0;
    int equalCount = 0;

    char key    [LOG_LINEBUF_LEN];
    char value  [LOG_LINEBUF_LEN];

    memset(key,   0x00, sizeof(key));
    memset(value, 0x00, sizeof(value));

    // = を見つけて、解析していく
    ptr = line;
    ptr2 = ptr;
    bool  equalFound = false;
    for(int i = 0; i < line_len; i++)
    {
        charCount++;
        if ( '=' == *ptr )
        {
            equalCount++;
            if (equalCount == 1)
            {
                // = の前半は key とみなす
                memcpy(key,ptr2,(charCount-1));
                equalFound = true;
                break;
            }
            ptr2 = ptr;
            ptr2++;
            charCount = 0;
        }
        ptr++;
    }

    // = の後半は value とみなす
    if (( equalFound ) && ((line_len - charCount) > 0))
    {
        memcpy(value,ptr+1,(line_len - charCount));
    }


    // equalの数を調べる。
    if (( 0 == equalCount ) && ( 40 == line_len )) // commaCount =0 で、lineLengthが0でない場合
    {
        // 1行目のハッシュ値を m_HashEntryValueにコピーする
        memset(m_HashEntryValue, 0x00, sizeof(m_HashEntryValue));
        memcpy(m_HashEntryValue, line, line_len);
        return;
    }
    else if ( 0 == equalCount)
    {
        // 空行
        //NN_LOG(" commaCount(%d) skip brank line.\n",commaCount);
        return;
    }
    else
    {
        //TODO データなので、 switchでキーと比較した後、メンバ変数に登録する
        NN_LOG("key(%s) value(%s)\n",key,value);

        if ( 0 == strcmp(key, "DeviceIdHash"))  { m_DeviceIdHash   = value ;}
        if ( 0 == strcmp(key, "RfuMacAddress")) { m_RfuMacAddress  = value ;}
        if ( 0 == strcmp(key, "SerialNumber"))  { m_SerialNumber   = value ;}
        if ( 0 == strcmp(key, "RegionCode"))    { m_RegionCode     = value ;}
        if ( 0 == strcmp(key, "BondingOption")) { m_BondingOption  = value ;}
        if ( 0 == strcmp(key, "Comment"))       { m_Comment        = value ;}
        if ( 0 == strcmp(key, "CommentUpdated")){ m_CommentUpdated = value ;}
    }
}



// データをロードする
LogResult InspectLog::LoadData(void * buf)
{
    u8 * ptr = reinterpret_cast<u8 *>(buf);
    u8 * ptr2 = ptr;
    int i = 0;
    int line = 0;
    int charCount = 0;
    u8  lineBuf[LOG_LINEBUF_LEN];

    // データ構造体をクリアする
    Clear_DataStructure();

    // バッファからデータを読み、データ構造体に登録する
    for(i =0; i < m_fileSize; i++)
    {
        charCount++;
        if ( 0x0a == *ptr ) //改行コード
        {
            memset(lineBuf,0x00,sizeof(lineBuf));
            memcpy(lineBuf,ptr2, (charCount-1));
            NN_LOG("(%d)[%s]\n",line,lineBuf);

            Analyze_LogLine(lineBuf, (charCount-1)); //ここで登録

            line++; //行数のカウント
            ptr2 = ptr;
            ptr2++;
            charCount = 0;
        }
//        NN_LOG("%c",*ptr);
        ptr++;
    }//for

    return LogResultCode::SUCCESS;
}

// データをファイルに書く
LogResult InspectLog::WriteFile(const wstring& path, const string & message)
{
    int  i;
    char line[LOG_LINEBUF_LEN];
    s32 writeBytes=0;     //書込みサイズ
    s32 writeBytesTotal=0;//書込みサイズ合計
    nn::Result nnr;

    nn::fs::FileOutputStream fos(path.c_str(), true);
    DEBUG_WRITE("%s [%ls] fileSize(%d)\n", NN_FUNCTION, path.c_str(),fos.GetSize());

    //いったんファイルサイズをゼロにする
    nnr = fos.TrySetSize(0);
    if ( nnr.IsFailure() ) goto ERROR;

#if 0
    //シリアルナンバーを書込み直前に取得する
    char serial[nn::cfg::CTR::detail::CFG_SECURE_INFO_SERIAL_NO_LEN+1];
    uji::sys::GetSerialNumber(serial);
    m_SerialNumber   = serial;
#endif

    //ヘッダの出力
    //(補足)ハッシュ値の計算は、テーブルにエントリを登録するときと、ロード時のみ行うので、
    //      ここでは計算しない。
    memset(line, 0x00,sizeof(line));
    sprintf(line,"%s\n\n",m_DataHash);
    nnr = fos.TryWrite(&writeBytes,line,strlen(line),false);
    if ( nnr.IsFailure() ) goto ERROR;
    writeBytesTotal = writeBytes;
    i = 2;

    //データの出力
    memset(line, 0x00,sizeof(line));
    sprintf(line,"DeviceIdHash=%s\n",m_DeviceIdHash.c_str());
    nnr = fos.TryWrite(&writeBytes,line,strlen(line),false);
    if ( nnr.IsFailure() ) goto ERROR;
    writeBytesTotal = writeBytes + writeBytesTotal;
    i++;

    memset(line, 0x00,sizeof(line));
    sprintf(line,"RfuMacAddress=%s\n",m_RfuMacAddress.c_str());
    nnr = fos.TryWrite(&writeBytes,line,strlen(line),false);
    if ( nnr.IsFailure() ) goto ERROR;
    writeBytesTotal = writeBytes + writeBytesTotal;
    i++;

    memset(line, 0x00,sizeof(line));
    sprintf(line,"SerialNumber=%s\n",m_SerialNumber.c_str());
    nnr = fos.TryWrite(&writeBytes,line,strlen(line),false);
    if ( nnr.IsFailure() ) goto ERROR;
    writeBytesTotal = writeBytes + writeBytesTotal;
    i++;

    memset(line, 0x00,sizeof(line));
    sprintf(line,"RegionCode=%s\n",m_RegionCode.c_str());
    nnr = fos.TryWrite(&writeBytes,line,strlen(line),false);
    if ( nnr.IsFailure() ) goto ERROR;
    writeBytesTotal = writeBytes + writeBytesTotal;
    i++;

    memset(line, 0x00,sizeof(line));
    sprintf(line,"BondingOption=%s\n",m_BondingOption.c_str());
    nnr = fos.TryWrite(&writeBytes,line,strlen(line),false);
    if ( nnr.IsFailure() ) goto ERROR;
    writeBytesTotal = writeBytes + writeBytesTotal;
    i++;

    memset(line, 0x00,sizeof(line));
    sprintf(line,"Comment=%s\n",m_Comment.c_str());
    nnr = fos.TryWrite(&writeBytes,line,strlen(line),false);
    if ( nnr.IsFailure() ) goto ERROR;
    writeBytesTotal = writeBytes + writeBytesTotal;
    i++;

    memset(line, 0x00,sizeof(line));
    sprintf(line,"CommentUpdated=%s\n",m_CommentUpdated.c_str());
    nnr = fos.TryWrite(&writeBytes,line,strlen(line),true); //ここでデータを書き戻しを有効にする
    if ( nnr.IsFailure() ) goto ERROR;
    writeBytesTotal = writeBytes + writeBytesTotal;
    i++;

    fos.Finalize();
    NN_LOG("%s            %s lines(%d) writeBytes(%d) \n",__func__, message.c_str(), i,writeBytesTotal);
    return LogResultCode::SUCCESS;

ERROR:
    fos.Finalize();
    return LogResultCode::FS_WRITE_ERROR;
}

// データのハッシュ値を計算し、m_DataHashの値を更新する。
LogResult InspectLog::Calc_DataHash(void)
{

    // データコピー用バッファ確保
    void * dataBuf = uji::sys::Alloc(m_fileSizeMax);
    if ( NULL == dataBuf)
    {
        DEBUG_WRITE("%s dataBuf is NULL\n", NN_FUNCTION);
        return LogResultCode::ALLOCATE_ERROR;
    }
    memset(dataBuf, 0x00, m_fileSizeMax);

    char * dataPtr = NULL;       //データバッファへのポインタ
    dataPtr = static_cast<char *>(dataBuf);

    int  outputTotal = 0;       //出力データサイズ(バイト)

    //TODO 適当すぎるので書き直し。
    sprintf(dataPtr, "DeviceIdHash=%s\nRfuMacAddress=%s\nSerialNumber=%s\nRegionCode=%s\nBondingOption=%s\nComment=%s\nCommentUpdated=%s\n"
                     ,m_DeviceIdHash.c_str()
                     ,m_RfuMacAddress.c_str()
                     ,m_SerialNumber.c_str()
                     ,m_RegionCode.c_str()
                     ,m_BondingOption.c_str()
                     ,m_Comment.c_str()
                     ,m_CommentUpdated.c_str()
    );

    outputTotal = strlen(dataPtr);


    // ハッシュ計算
    nn::crypto::Sha1Context sha1;
    sha1.Initialize();

    // ハッシュ計算用メモリ確保
    int hashLength = 0;
    hashLength = sha1.GetHashLength();
    void* hashBuf = uji::sys::Alloc(hashLength);
    if ( NULL == hashBuf )
    {
        uji::sys::Free(dataBuf);
        return LogResultCode::ALLOCATE_ERROR;
    }
    memset(hashBuf, 0x00, sizeof(hashBuf));

    // ハッシュ計算
    NN_LOG("outputTotal:%d\n",outputTotal);
    sha1.Update(dataBuf, outputTotal);
    NN_LOG("%s outputTotal=%d\n",NN_FUNCTION,outputTotal);
    sha1.GetHash(hashBuf);

    // ハッシュ値の表示
    u8 * hashPtr;
    hashPtr = static_cast<u8 *>(hashBuf);
    memset(m_DataHash, 0x00, sizeof(m_DataHash));
    NN_LOG(" sha1 DataHash:[");
    for(int i = 0; i < hashLength; i++)
    {
        NN_LOG("%02X",*hashPtr);
        sprintf(&m_DataHash[i*2], "%02X",*hashPtr);
        hashPtr++;
    }
    NN_LOG("]\n");

    NN_LOG(" check m_DataHash[%s]\n",&m_DataHash[0]);

    // 終了処理
    sha1.Finalize();
    sha1.~Sha1Context();

    uji::sys::Free(dataBuf);
    uji::sys::Free(hashBuf);

    return LogResultCode::SUCCESS;
}


// デバイスIDハッシュを取得する
// 表示はデバイスIDハッシュ(中身は次の手順で、localFriendCodeSeedを簡易変換）
string    InspectLog::Get_DeviceIdHash (void)
{
    //1. ローカルフレンドコードシードをエンディアンを変え
    //2. そいつに対してXORで反転させる
    return m_DeviceIdHash;
}

// RfuMacアドレスを取得する
string    InspectLog::Get_RfuMacAddress (void)
{
    return m_RfuMacAddress;
}

// シリアルナンバーを取得する
string    InspectLog::Get_SerialNumber (void)
{
    return m_SerialNumber;
}

// リージョンコードを取得する
string    InspectLog::Get_RegionCode (void)
{
    return m_RegionCode;
}

// ボンディングオプションを取得する
string    InspectLog::Get_BondingOption (void)
{
    return m_BondingOption;
}

// コメントをセットする
LogResult InspectLog::Set_Comment(string comment_str,  string date_str)
{
    m_Comment        = comment_str;
    m_CommentUpdated = date_str;

    return LogResultCode::SUCCESS;
}

// コメントを取得する
string    InspectLog::Get_Comment (void)
{
    return m_Comment;
}

// コメント更新日時を取得する
string    InspectLog::Get_CommentUpdated (void)
{
    return m_CommentUpdated;
}

// リストア先の本体を照合する
// (注意) m_DeviceIdHash の値はあらかじめ更新しておくこと
LogResult InspectLog::Identify_RestoreTarget(void)
{

    //ローカルフレンドコードシードを取得
    nn::Result nnr;
    nnr = nn::ps::CTR::Initialize();
    if ( nnr.IsFailure())
    {
        NN_LOG("nn::ps::Initialize failed.\n");
        nn::dbg::PrintResult(nnr);
        return LogResultCode::FUNC_CALL_ERROR;
    }
    bit32 deviceId = 0;
    nnr = nn::ps::CTR::GetDeviceId(&deviceId);
    if ( nnr.IsFailure())
    {
        NN_LOG("nn::ps::GetDeviceId failed.\n");
        nn::dbg::PrintResult(nnr);
        return LogResultCode::FUNC_CALL_ERROR;
    }
    char deviceIdStr[10];
    memset(deviceIdStr, 0x00, sizeof(deviceIdStr));
    sprintf(deviceIdStr, "%08X", deviceId);

    bit32 deviceIdHash = 0;
    deviceIdHash = 0xDECABEEF ^ deviceId; //DECA BEEF
    NN_LOG("%08X\n",deviceIdHash);

    deviceIdHash = nn::socket::NtoHl(deviceIdHash);
    NN_LOG("%08X\n",deviceIdHash);

    char deviceIdHashStr[10];
    memset(deviceIdHashStr, 0x00, sizeof(deviceIdHashStr));
    sprintf(deviceIdHashStr, "%08X", deviceIdHash);

    // あらかじめSDからロードした値と、実際に取得した値を比較
    if ( 0 != strncmp(m_DeviceIdHash.c_str(), deviceIdHashStr, 8))
    {
        return LogResultCode::IDENTIFY_FAILURE;
    }

    return LogResultCode::SUCCESS;
}



