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