1 /*---------------------------------------------------------------------------*
2 Project: NintendoWare
3 File: ut_BinaryFileFormat.h
4
5 Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. 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 $Revision:$
14 *---------------------------------------------------------------------------*/
15
16 #ifndef NW_UT_BINARYFILEFORMAT_H_
17 #define NW_UT_BINARYFILEFORMAT_H_
18
19 #include <nw/config.h>
20 #include <nw/types.h>
21 #include <nw/ut/ut_BinaryReader.h>
22
23 //------------------------------------------------------------------------
24 // マクロ定義
25 //------------------------------------------------------------------------
26
27 //---- シグネチャ作成マクロ
28
29 #if ( NW_PLATFORM_ENDIAN == NW_ENDIAN_LITTLE )
30
31 //---- little endian
32 #define NW_UT_MAKE_SIGWORD(a,b,c,d) \
33 static_cast<nw::ut::SigWord >( \
34 (static_cast<u8>(a) << 0) \
35 | (static_cast<u8>(b) << 8) \
36 | (static_cast<u8>(c) << 16) \
37 | (static_cast<u8>(d) << 24) \
38 )
39
40 #else
41
42 //---- big endian
43 #define NW_UT_MAKE_SIGWORD(a,b,c,d) \
44 static_cast<nw::ut::SigWord >( \
45 (static_cast<u8>(a) << 24) \
46 | (static_cast<u8>(b) << 16) \
47 | (static_cast<u8>(c) << 8) \
48 | (static_cast<u8>(d) << 0) \
49 )
50 #endif
51
52 #define NW_UT_MAKE_VERSION_2(major, minor) \
53 static_cast<u32>( \
54 (static_cast<u8>(major) << 24) | (static_cast<u8>(minor) << 16) \
55 )
56
57 #define NW_UT_MAKE_VERSION(major, minor, micro, binaryBugFix) \
58 static_cast<u32>( \
59 (static_cast<u8>(major) << 24) | (static_cast<u8>(minor) << 16) \
60 | (static_cast<u8>(micro) << 8) | (static_cast<u8>(binaryBugFix)) \
61 )
62
63 #define NW_UT_VERSION_MAJOR(version) (((version) >> 24) & 0xff)
64 #define NW_UT_VERSION_MINOR(version) (((version) >> 16) & 0xff)
65 #define NW_UT_VERSION_MICRO(version) (((version) >> 8) & 0xff)
66 #define NW_UT_VERSION_BINARYBUGFIX(version) (((version) >> 0) & 0xff)
67
68 namespace nw {
69 namespace ut {
70
71
72 //------------------------------------------------------------------------
73 // 型定義
74 //------------------------------------------------------------------------
75
76 //---- シグネチャ型
77 typedef u32 SigWord; //!< @details :private
78
79
80 //---------------------------------------------------------------------------
81 //! @brief NintendoWare 標準バイナリファイルヘッダ
82 //! @details :private
83 //---------------------------------------------------------------------------
84 struct BinaryFileHeader
85 {
86 SigWord signature; // ファイルシグネチャ : 4 Byte
87 u16 byteOrder; // バイトオーダーマーク : 2 Byte
88 ResU16 headerSize; // ヘッダサイズ : 2 Byte
89 ResU32 version; // ファイルバージョン : 4 Byte
90 ResU32 fileSize; // ファイルサイズ : 4 Byte
91 ResU16 dataBlocks; // ブロック数 : 2 Byte
92 u16 reserved; // 予約 : 2 Byte
93 };
94
95
96 //---------------------------------------------------------------------------
97 //! @brief NintendoWare 標準バイナリブロックヘッダ
98 //! @details :private
99 //---------------------------------------------------------------------------
100 struct BinaryBlockHeader
101 {
102 SigWord kind; // ブロック種別名
103 u32 size; // ブロック全体サイズ
104 };
105
106
107
108 //---------------------------------------------------------------------------
109 //! @brief NintendoWare バイナリリビジョンブロックヘッダ
110 //! @details :private
111 //---------------------------------------------------------------------------
112 struct BinaryRevisionHeader
113 {
114 SigWord signature; // ブロックシグニチャ : 4 Byte
115 ResU32 revision; // リビジョン : 4 Byte
116 };
117
118
119
120 //------------------------------------------------------------------------
121 // 定数定義
122 //------------------------------------------------------------------------
123
124 namespace
125 {
126 const u16 BYTE_ORDER_MARK = 0xFEFF;
127 }
128
129 //----------------------------------------
130 //! @name バイナリファイル関連
131 //@{
132
133 //------------------------------------------------------------------------
134 // 関数プロトタイプ
135 //------------------------------------------------------------------------
136
137 //---------------------------------------------------------------------------
138 //! @brief NintendoWare 標準バイナリファイルヘッダの正当性を
139 //! チェックします。
140 //!
141 //! @param[in] pHeader チェック対象のファイルヘッダへのポインタ
142 //! @param[in] signature 格納されているべきシグネチャ
143 //! @param[in] version 格納されているべきバージョン
144 //! @param[in] minBlocks 格納されているべき最小バイナリブロック数
145 //!
146 //! @return チェックを全てパスすれば true, それ以外なら false を
147 //! 返します。
148 //---------------------------------------------------------------------------
149 bool IsValidBinaryFile(
150 const BinaryFileHeader* pHeader,
151 u32 signature,
152 u32 version,
153 u16 minBlocks = 1 );
154
155 bool IsReverseEndianBinaryFile( const BinaryFileHeader* header );
156
157 BinaryBlockHeader* GetNextBinaryBlockHeader(
158 BinaryFileHeader* pFileHeader,
159 BinaryBlockHeader* pBlockHeader
160 );
161
162 //---------------------------------------------------------------------------
163 //! @brief NintendoWare 標準バイナリファイル内の次のブロックヘッダを
164 //! 取得します。
165 //!
166 //! @param[in] pFileHeader 対象のファイルヘッダへのポインタ
167 //! @param[in] pBlockHeader 現在のブロックヘッダへのポインタ
168 //!
169 //! @return blockHeaderの次のブロックヘッダを返します。
170 //! 次のブロックがない場合は、NULLを返します。
171 //! blockHeaderがNULLの場合は、一番始めのブロックヘッダを返します。
172 //---------------------------------------------------------------------------
173 NW_INLINE const BinaryBlockHeader*
GetNextBinaryBlockHeader(const BinaryFileHeader * pFileHeader,const BinaryBlockHeader * pBlockHeader)174 GetNextBinaryBlockHeader(
175 const BinaryFileHeader* pFileHeader,
176 const BinaryBlockHeader* pBlockHeader
177 )
178 {
179 return static_cast<const BinaryBlockHeader*>(
180 GetNextBinaryBlockHeader(
181 const_cast<BinaryFileHeader*>(pFileHeader),
182 const_cast<BinaryBlockHeader*>(pBlockHeader)
183 )
184 );
185 }
186
187 //@}
188
189 namespace internal {
190
191 //---------------------------------------------------------------------------
192 //! @brief バイナリリソースのリビジョンチェックをおこないます。
193 //!
194 //! @param[in] resRevision バイナリリソースのリビジョンです。
195 //! @param[in] libRevision ライブラリのリビジョンです。
196 //!
197 //! @return 実行可能なバイナリバージョンであれば true、
198 //! そうでなければ false を返します。
199 //---------------------------------------------------------------------------
200 NW_INLINE bool
CheckRevision(u32 resRevision,u32 libRevision)201 CheckRevision( u32 resRevision, u32 libRevision )
202 {
203 // メジャーバージョンが異なる場合は、実行不能。
204 if ( NW_UT_VERSION_MAJOR(libRevision) != NW_UT_VERSION_MAJOR(resRevision) )
205 {
206 return false;
207 }
208
209 // リソースのマイナーバージョンがライブラリよりも新しい場合には、実行不能。
210 if ( NW_UT_VERSION_MINOR(libRevision) < NW_UT_VERSION_MINOR(resRevision) )
211 {
212 return false;
213 }
214
215 // リソースのバイナリバグフィックスバージョンがライブラリよりも古い場合には、実行不能。
216 if ( NW_UT_VERSION_BINARYBUGFIX(libRevision) > NW_UT_VERSION_BINARYBUGFIX(resRevision) )
217 {
218 return false;
219 }
220
221 return true;
222 }
223
224 } /* namespace internal */
225
226
227 } /* namespace ut */
228 } /* namespace nw */
229 #endif // NW_UT_BINARYFILEFORMAT_H_
230