1 /*---------------------------------------------------------------------------*
2   Project:     Compress/uncompress library
3   File:        CXUncompression.h
4 
5   Copyright 2005 Nintendo.  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   $Log: CXUncompression.h,v $
14   Revision 1.14  2007/05/18 10:50:39  yasuh-to
15   Rollback to SYSTEMMENU2_DEV_BRANCH(HEAD)
16 
17   Revision 1.11  2006/09/13 01:01:31  takano_makoto
18   Expanded max file size.
19 
20   Revision 1.10  2006/07/14 05:23:56  takano_makoto
21   Added CXUncompressAny()
22   Bug fix in CXGetCompressionType()
23 
24   Revision 1.9  2006/07/06 09:19:14  takano_makoto
25   Changed the include guard to the REVOLUTION_SDK format
26 
27   Revision 1.8  2006/07/06 05:29:48  takano_makoto
28   Added CXUnfilterDiff
29 
30   Revision 1.7  2006/07/06 04:39:50  takano_makoto
31   Revised duplicate definitions in CXGetCompressionType, CXGetUncompressedSize
32 
33   Revision 1.6  2006/07/06 04:13:04  takano_makoto
34   Added CXGetCompressionType, CXGetUncompressedSize
35 
36   Revision 1.5  2006/07/05 11:14:43  takano_makoto
37   Revised the include guard
38 
39   Revision 1.4  2006/07/05 08:13:03  takano_makoto
40   Revised comments
41 
42   Revision 1.3  2006/07/04 08:38:39  takano_makoto
43   Fixed copyright header
44 
45   $NoKeywords: $
46  *---------------------------------------------------------------------------*/
47 
48 #ifndef __CX_UNCOMPRESSION_H__
49 #define __CX_UNCOMPRESSION_H__
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 #include <dolphin/types.h>
56 
57 
58 //---- Compression type
59 typedef enum
60 {
61     CX_COMPRESSION_LZ           = 0x10,     // LZ77
62     CX_COMPRESSION_HUFFMAN      = 0x20,     // Huffman
63     CX_COMPRESSION_RL           = 0x30,     // Run Length
64     CX_COMPRESSION_DIFF         = 0x80,     // Differential filter
65 
66     CX_COMPRESSION_TYPE_MASK    = 0xF0,
67     CX_COMPRESSION_TYPE_EX_MASK = 0xFF
68 }
69 CXCompressionType;
70 
71 
72 //----------------------------------------------------------------
73 // Compressed data header
74 //
75 typedef struct
76 {
77     u8  compType;   // Compression type
78     u8  compParam;  // Compression parameter
79     u8  padding_[2];
80     u32 destSize;   // Expanded size
81 }
82 CXCompressionHeader;
83 
84 
85 /*---------------------------------------------------------------------------*
86   Name:         CXGetCompressionHeader
87 
88   Description:  Gets header information from the first four bytes in the compression data.
89 
90   Arguments:    data    Pointer to the first four bytes of data in the compressed data
91 
92   Returns:      None.
93  *---------------------------------------------------------------------------*/
94 CXCompressionHeader CXGetCompressionHeader( const void *data );
95 
96 /*---------------------------------------------------------------------------*
97   Name:         CXGetCompressionType
98 
99   Description:  Gets compression type from the first one byte in the compressed data
100 
101   Arguments:    srcp :  Compressed data address
102 
103   Returns:      Compression type.
104                 CX_COMPREESION_LZ      : LZ77 compressed data
105                 CX_COMPREESION_HUFFMAN : Huffman compressed data
106                 CX_COMPREESION_RL      : Run-length compressed data
107                 CX_COMPRESSION_DIFF    : Data converted with a differential filter
108  *---------------------------------------------------------------------------*/
CXGetCompressionType(const void * data)109 static inline CXCompressionType   CXGetCompressionType( const void *data )
110 {
111     return (CXCompressionType)( *(u8*)data & 0xF0 );
112 }
113 
114 //======================================================================
115 //          Expanding compressed data
116 //======================================================================
117 
118 /*---------------------------------------------------------------------------*
119   Name:         CXGetUncompressedSize
120 
121   Description:  Gets the data size after decompression.
122                 This function can be used for data in all compression formats handled by CX.
123 
124   Arguments:    srcp :  Pointer to the first four bytes of data in the compressed data
125 
126   Returns:      Data size after decompression
127  *---------------------------------------------------------------------------*/
128 u32 CXGetUncompressedSize( const void *srcp );
129 
130 
131 /*---------------------------------------------------------------------------*
132   Name:         CXUncompressAny
133 
134   Description:  Determines the compression type from the data header and performs the appropriate decompression.
135 
136                 Since decompression processes for all types of compression are linked, it may be better to execute separate functions for each for each compression type when not using formats other than the specific compression formats.
137 
138 
139 
140   Arguments:    srcp    Source address
141                 destp   Destination address
142 
143   Returns:      None.
144  *---------------------------------------------------------------------------*/
145 void CXUncompressAny( const void* srcp, void* destp );
146 
147 
148 /*---------------------------------------------------------------------------*
149   Name:         CXUncompressRL
150 
151   Description:  8-bit decompression of run-length compressed data
152 
153   - Decompresses run-length compressed data, writing in 8 bit units.
154   - Use 4 byte alignment for the source address.
155 
156   - Data header
157       u32 :4                  Reserved
158           compType:4          Compression type( = 3)
159           destSize:24         Data size after decompression
160 
161   - Flag data format
162       u8  length:7            Decompressed data length - 1 (When not compressed)
163                               Decompressed data length - 3 (only compress when the contiguous length is 3 bytes or greater)
164           flag:1              (0, 1) = (not compressed, compressed)
165 
166   Arguments:    *srcp   Source address
167                 *destp  Destination address
168 
169   Returns:      None.
170  *---------------------------------------------------------------------------*/
171 void CXUncompressRL( const void *srcp, void *destp );
172 
173 
174 /*---------------------------------------------------------------------------*
175   Name:         CXUncompressLZ
176 
177   Description:  8-bit decompression of LZ77 compressed data
178 
179   - Decompresses LZ77 compressed data, writing in 8 bit units.
180   - Use 4 byte alignment for the source address.
181 
182   - Data header
183       u32 :4                  Reserved
184           compType:4          Compression type( = 1)
185           destSize:24         Data size after decompression
186 
187   - Flag data format
188       u8  flags               Compression/no compression flag
189                               (0, 1) = (not compressed, compressed)
190   - Code data format (Big Endian)
191       u16 length:4            Decompressed data length - 3 (only compress when the match length is 3 bytes or greater)
192           offset:12           Match data offset - 1
193 
194   Arguments:    *srcp   Source address
195                 *destp  Destination address
196 
197   Returns:      None.
198  *---------------------------------------------------------------------------*/
199 void CXUncompressLZ( const void *srcp, void *destp );
200 
201 
202 /*---------------------------------------------------------------------------*
203   Name:         CXUncompressHuffman
204 
205   Description:  Decompression of Huffman compressed data
206 
207   - Decompresses Huffman compressed data, writing in 32 bit units.
208   - Use 4 byte alignment for the source address.
209   - Use 4 byte alignment for the destination address.
210   - The target decompression buffer size must be prepared in 4 byte multiples.
211 
212   - Data header
213       u32 bitSize:4           1 data bit size (Normally 4|8)
214           compType:4          Compression type( = 2)
215           destSize:24         Data size after decompression
216 
217   - Tree table
218       u8           treeSize        Tree table size/2 - 1
219       TreeNodeData nodeRoot        Root node
220 
221       TreeNodeData nodeLeft        Root left node
222       TreeNodeData nodeRight       Root right node
223 
224       TreeNodeData nodeLeftLeft    Left left node
225       TreeNodeData nodeLeftRight   Left right node
226 
227       TreeNodeData nodeRightLeft   Right left node
228       TreeNodeData nodeRightRight  Right right node
229 
230               .
231               .
232 
233     The compressed data itself follows
234 
235   - TreeNodeData structure
236       u8  nodeNextOffset:6    Offset to the next node data - 1 (2 byte units)
237           rightEndFlag:1      Right node end flag
238           leftEndzflag:1      Left node end flag
239                               When the end flag is set
240                               There is data in next node
241 
242   Arguments:    *srcp   Source address
243                 *destp  Destination address
244 
245   Returns:      None.
246  *---------------------------------------------------------------------------*/
247 void CXUncompressHuffman( const void *srcp, void *destp );
248 
249 
250 /*---------------------------------------------------------------------------*
251   Name:         CXUnfilterDiff
252 
253   Description:  8-bit decompression to restore differential filter conversion.
254 
255     - Restores a differential filter, writing in 8 bit units.
256     - Cannot decompress directly into VRAM
257     - If the size of compressed data is not a multiple of four, adjust as necessary by padding with 0s.
258 
259     - Use 4 byte alignment for the source address.
260 
261   Arguments:    *srcp   Source address
262                 *destp  Destination address
263 
264   Returns:      None.
265  *---------------------------------------------------------------------------*/
266 void CXUnfilterDiff( register const void *srcp, register void *destp );
267 
268 
269 //================================================================================
270 
271 
272 /*---------------------------------------------------------------------------*
273   Name:         CXiUncompressBackward
274 
275   Description:  Uncompress special archive for module compression
276 
277   Arguments:    bottom      = Bottom adrs of packed archive + 1
278                 bottom[-12] = offset for top    of compressed data
279                                  inp_top = bottom + bottom[-12]
280                 bottom[ -8] = offset for bottom of compressed data
281                                  inp     = bottom + bottom[ -8]
282                 bottom[ -4] = offset for bottom of original data
283                                  outp    = bottom + bottom[ -4]
284 
285                 typedef struct
286                 {
287                     int         bufferTop;
288                     int         compressBottom;
289                     int         originalBottom;
290                 }   CompFooter;
291 
292   Returns:      None.
293  *---------------------------------------------------------------------------*/
294 // !!!! Coded in libraries/init/ARM9/crt0.c
295 // void CXiUncompressBackward( void *bottom );
296 
297 
298 
299 
300 #ifdef __cplusplus
301 } /* extern "C" */
302 #endif
303 
304 /* __CX_UNCOMPRESSION_H__ */
305 #endif
306