1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     font_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: 24675 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_FONT_DETAIL_FONT_BINARY_FILE_FORMAT_H_
17 #define NN_FONT_DETAIL_FONT_BINARY_FILE_FORMAT_H_
18 
19 #include <nn/types.h>
20 
21 #if ( NN_ENDIAN == NN_ENDIAN_VALUE_LITTLE )
22 //---- little endian
23 #define NN_FONT_MAKE_SIGWORD(a, b, c, d)                        \
24     static_cast<nn::font::detail::SigWord>(                     \
25         (static_cast<u8>(a) <<  0) | (static_cast<u8>(b) <<  8) \
26       | (static_cast<u8>(c) << 16) | (static_cast<u8>(d) << 24) \
27     )
28 #else
29 //---- big endian
30 #define NN_FONT_MAKE_SIGWORD(a, b, c, d)                        \
31     static_cast<nn::font::detail::SigWord>(                     \
32         (static_cast<u8>(a) << 24) | (static_cast<u8>(b) << 16) \
33       | (static_cast<u8>(c) <<  8) | (static_cast<u8>(d) <<  0) \
34     )
35 #endif
36 #define NN_FONT_MAKE_VERSION(major, minor, micro, binaryBugFix)          \
37     static_cast<u32>(                                                    \
38         (static_cast<u8>(major) << 24) | (static_cast<u8>(minor) << 16)  \
39       | (static_cast<u8>(micro) <<  8) | (static_cast<u8>(binaryBugFix)) \
40     )
41 
42 #define NN_FONT_VERSION_MAJOR(version)        (((version) >> 24) & 0xff)
43 #define NN_FONT_VERSION_MINOR(version)        (((version) >> 16) & 0xff)
44 #define NN_FONT_VERSION_MICRO(version)        (((version) >>  8) & 0xff)
45 #define NN_FONT_VERSION_BINARYBUGFIX(version) (((version) >>  0) & 0xff)
46 
47 namespace nn { namespace font { namespace detail {
48 
49 // #define NN_SWAP_ENDIAN
50 
51 #if !defined( NN_SWAP_ENDIAN )
52     typedef u8  ResU8;          //!< @details :private
53     typedef s8  ResS8;          //!< @details :private
54     typedef u16 ResU16;         //!< @details :private
55     typedef s16 ResS16;         //!< @details :private
56     typedef u32 ResU32;         //!< @details :private
57     typedef s32 ResS32;         //!< @details :private
58     typedef f32 ResF32;         //!< @details :private
59     typedef u32 ResU64;         //!< @details :private
60     typedef s32 ResS64;         //!< @details :private
61     typedef f32 ResF64;         //!< @details :private
62 #else // if defined( NN_SWAP_ENDIAN )
63 namespace Endian {
64     typedef union
65     {
66         u64 UInt64;
67         s64 SInt64;
68 #if defined( NN_ENABLE_FLOAT64 )
69         f64 Float64;
70 #endif
71     } Type64;
72 
73     typedef union
74     {
75         u32 UInt32;
76         s32 SInt32;
77         f32 Float32;
78     } Type32;
79 
80     typedef union
81     {
82         u16 UInt16;
83         s16 SInt16;
84     } Type16;
85 
86     static u64 BSwap( u64 val )
87     {
88         const u64 MASK  = 0xFF00FF00FF00FF00ULL;
89         const u64 MASK2 = 0xFFFF0000FFFF0000ULL;
90         val = ( (val & MASK) >> 8 ) | ( (val << 8) & MASK );
91         val = ( (val & MASK2) >> 16 ) | ( (val << 16) & MASK2 );
92         return (val >> 32) | (val << 32);
93     }
94 
95     static s64 BSwap( s64 val )
96     {
97         Type64 data;
98         data.SInt64 = val;
99         data.UInt64 = BSwap( data.UInt64 );
100         return data.SInt64;
101     }
102 
103 #if defined( NN_ENABLE_FLOAT64 )
104     static f64 BSwap( f64 val )
105     {
106         Type64 data;
107         data.Float64 = val;
108         data.UInt64 = BSwap( data.UInt64 );
109         return data.Float64;
110     }
111 #endif
112 
113     static u32 BSwap( u32 val )
114     {
115         const u32 MASK = 0xFF00FF00;
116         val = ( (val & MASK) >> 8 ) | ( (val << 8) & MASK );
117         return (val >> 16) | (val << 16);
118     }
119 
120     static s32 BSwap( s32 val )
121     {
122         Type32 data;
123         data.SInt32 = val;
124         data.UInt32 = BSwap( data.UInt32 );
125         return data.SInt32;
126     }
127 
128     static f32 BSwap( f32 val )
129     {
130         Type32 data;
131         data.Float32 = val;
132         data.UInt32 = BSwap( data.UInt32 );
133         return data.Float32;
134     }
135 
136     static u16 BSwap( u16 val )
137     {
138         return (u16)( (val >> 8) | (val << 8) );
139     }
140 
141     static s16 BSwap( s16 val )
142     {
143         return (s16)( ((u16)val >> 8) | ((u16)val << 8) );
144     }
145 } /* namespace Endian */
146 
147     template <typename T>
148     class ResNum
149     {
150     public:
151         /* ctor */ ResNum() {}
152         /* ctor */ ResNum(const ResNum& other) : bits(other.bits) {}
153         /* ctor */ ResNum(const T val ) : bits( Endian::BSwap( val ) ) {}
154 
155         void operator = (const ResNum& other) { bits = other.bits; }
156         /* T */ operator T () const { return Endian::BSwap( bits ); }
157         void operator = (T val) { bits = Endian::BSwap( val ); }
158     private:
159         T   bits;
160     };
161 
162     typedef u8          ResU8;      //!< @details :private
163     typedef s8          ResS8;      //!< @details :private
164     typedef ResNum<u16> ResU16;     //!< @details :private
165     typedef ResNum<s16> ResS16;     //!< @details :private
166     typedef ResNum<u32> ResU32;     //!< @details :private
167     typedef ResNum<s32> ResS32;     //!< @details :private
168     typedef ResNum<f32> ResF32;     //!< @details :private
169     typedef ResNum<u64> ResU64;     //!< @details :private
170     typedef ResNum<s64> ResS64;     //!< @details :private
171     typedef ResNum<f64> ResF64;     //!< @details :private
172 #endif // define( NN_SWAP_ENDIAN )
173 
174 
175 //---- シグネチャ型
176 typedef u32 SigWord;
177 
178 namespace
179 {
180     const u16 BYTE_ORDER_MARK = 0xFEFF;
181 }
182 
183 /*!--------------------------------------------------------------------------*
184   @brief       NintendoWare 標準バイナリファイルヘッダ
185   @details :private
186  *---------------------------------------------------------------------------*/
187 struct BinaryFileHeader
188 {
189     SigWord signature;                  // ファイルシグネチャ   : 4 Byte
190     u16     byteOrder;                  // バイトオーダーマーク : 2 Byte
191     ResU16  headerSize;                 // ヘッダサイズ         : 2 Byte
192     ResU32  version;                    // ファイルバージョン   : 4 Byte
193     ResU32  fileSize;                   // ファイルサイズ       : 4 Byte
194     ResU16  dataBlocks;                 // ブロック数           : 2 Byte
195     u16     reserved;                   // 予約                 : 2 Byte
196 };
197 
198 /*!--------------------------------------------------------------------------*
199   @brief       NintendoWare 標準バイナリブロックヘッダ
200   @details :private
201  *---------------------------------------------------------------------------*/
202 struct BinaryBlockHeader
203 {
204     SigWord kind;                       // ブロック種別名
205     u32     size;                       // ブロック全体サイズ
206 };
207 
208 bool IsValidBinaryFile(
209     const BinaryFileHeader* /*pHeader*/,
210     u32                     /*signature*/,
211     u32                     /*version*/,
212     u16                     /*minBlocks   = 1*/ );
213 
214 }}} // namespace nn::font::detail
215 
216 #endif // NN_FONT_DETAIL_FONT_BINARY_FILE_FORMAT_H_
217