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