1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: main.cpp
4
5 Copyright (C)2009-2012 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: 46365 $
14 *---------------------------------------------------------------------------*/
15
16 #include <nn.h>
17 #include <nn/cx.h>
18 #include <nn/applet.h>
19
nnMain()20 void nnMain()
21 {
22 // With this flag, the API used for expansion is switched
23 // false : UncompressBLZ() ......... Although the error check is insufficient, it is faster than the Secure version
24 // true : SecureUncompressBLZ() ... Error check is performed, but is slower than the non-Secure version
25 const bool uncompressWithErrorCheck = false;
26
27 NN_LOG("UncompressBlz demo\n");
28
29 // Call only nn::applet::Enable to also allow execution from the HOME Menu
30 // HOME Menu transitions, POWER Button presses, and sleep are all unsupported
31 nn::applet::Enable();
32
33 // Initialize fs, mount 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 // Load compressed file, and get the data size after expansion
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 // The data size after decompression is "size of compressed file" + "value written to the last 4 bytes of the compressed file"
46 //
47 u32 outSize;
48 reader.Seek(-4, nn::fs::POSITION_BASE_END);
49 reader.Read(&outSize, 4);
50 outSize += inSize;
51 reader.Finalize();
52 // Confirm if the buffer length is insufficient
53 if (outSize > sizeof(archive))
54 {
55 NN_PANIC("ERROR: buffer is not large enough\n");
56 }
57
58 // BLZ decompression
59 // The expanded data is overwritten to the input buffer
60 if (uncompressWithErrorCheck)
61 {
62 s32 ret = nn::cx::SecureUncompressBLZ(archive, inSize, sizeof(archive));
63 // Confirm if an error occur during expansion
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 // Comparison to original data
75 u8 data[4096];
76 reader.Initialize("rom:/data.bin");
77 // Confirm if this matches the expansion size obtained from the compressed file
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("UncompressBlz demo finished\n");
93 }
94
95 /*---------------------------------------------------------------------------*
96 End of file
97 *---------------------------------------------------------------------------*/
98