1/*---------------------------------------------------------------------------*
2  Project:  Horizon
3  File:     main.cpp
4
5  Copyright (C)2009-2011 Nintendo Co., Ltd.  All rights reserved.
6
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law.  They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12
13  $Rev: $
14 *---------------------------------------------------------------------------*/
15
16#include <nn.h>
17#include <nn/cx.h>
18#include <nn/applet.h>
19
20void nnMain()
21{
22    // このフラグにより、展開に使用する API が切り替わります
23    //   true  : UncompressBLZ() ......... エラーチェックが不十分だが、Secure 版より速い
24    //   false : SecureUncompressBLZ() ... エラーチェックを行うが、非 Secure 版より遅い
25    const bool uncompressWithErrorCheck = false;
26
27    NN_LOG("demo start\n");
28
29    // ホームメニューからも実行できるよう nn::applet::Enable() のみ呼ぶ
30    // ホームメニュー遷移や電源ボタンクリック、スリープには非対応
31    nn::applet::Enable();
32
33    // fs を初期化し、ROM をマウント
34    nn::fs::Initialize();
35    const size_t ROMFS_BUFFER_SIZE = 1024 * 64;
36    static char buffer[ROMFS_BUFFER_SIZE];
37    NN_UTIL_PANIC_IF_FAILED(nn::fs::MountRom(16, 16, buffer, ROMFS_BUFFER_SIZE));
38
39    // 圧縮ファイル読み込み、および展開後のデータサイズ取得
40    u8 archive[4096];
41    nn::fs::FileReader reader;
42    reader.Initialize("rom:/archive.bin");
43    u32 inSize = reader.GetSize();
44    reader.Read(archive, inSize);
45    // 展開後のデータサイズは、
46    // 「圧縮ファイルのサイズ」+「圧縮ファイル末尾 4 バイトに書かれた値」となる
47    u32 outSize;
48    reader.Seek(-4, nn::fs::POSITION_BASE_END);
49    reader.Read(&outSize, 4);
50    outSize += inSize;
51    reader.Finalize();
52    // バッファ長が不足していないかを確認
53    if (outSize > sizeof(archive))
54    {
55        NN_PANIC("ERROR: buffer is not large enough\n");
56    }
57
58    // BLZ 展開
59    // 展開されたデータは入力バッファに上書きされる
60    if (uncompressWithErrorCheck)
61    {
62        s32 ret = nn::cx::SecureUncompressBLZ(archive, inSize, sizeof(archive));
63        // 展開時にエラーが起きなかったかを確認
64        if (ret != 0)
65        {
66            NN_PANIC("ERROR: decode error has occurred\n");
67        }
68    }
69    else
70    {
71        nn::cx::UncompressBLZ(archive, inSize, sizeof(archive));
72    }
73
74    // オリジナルデータとの比較
75    u8 data[4096];
76    reader.Initialize("rom:/data.bin");
77    // 圧縮ファイルから得られる展開サイズと一致していることを確認
78    if (outSize != reader.GetSize())
79    {
80        NN_PANIC("ERROR: original data size != uncompressed data size\n");
81    }
82    reader.Read(data, outSize);
83    reader.Finalize();
84    for (int i = 0; i < outSize; i++)
85    {
86        if (archive[i] != data[i])
87        {
88            NN_PANIC("ERROR: original data != uncompressed data\n");
89        }
90    }
91
92    NN_LOG("demo end\n");
93}
94
95/*---------------------------------------------------------------------------*
96  End of file
97 *---------------------------------------------------------------------------*/
98