1 /*---------------------------------------------------------------------------*
2   Project:     Compress/uncompress library
3   File:        CXSecureUncompression.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: CXSecureUncompression.h,v $
14   Revision 1.7  2009/01/29 02:42:44  takano_makoto
15   Fixed comment.
16 
17   Revision 1.6  2007/10/31 12:52:11  takano_makoto
18   Appended the LH and LRC formats.
19 
20   Revision 1.5  2007/05/30 04:20:25  takano_makoto
21   Minor fixes.
22 
23   Revision 1.4  2007/05/18 10:50:39  yasuh-to
24   Rollback to SYSTEMMENU2_DEV_BRANCH(HEAD)
25 
26   Revision 1.1.2.1  2006/12/06 09:55:16  yasuh-to
27   Initial commit.
28 
29   Revision 1.1  2006/12/04 13:34:46  takano_makoto
30   Initial update.
31 
32   $NoKeywords: $
33  *---------------------------------------------------------------------------*/
34 
35 #ifndef __CX_SECURE_UNCOMPRESSION_H__
36 #define __CX_SECURE_UNCOMPRESSION_H__
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #include <dolphin/types.h>
43 #include <revolution/cx/CXUncompression.h>
44 
45 #define CX_ERR_SUCCESS              0
46 #define CX_ERR_UNSUPPORTED          -1
47 #define CX_ERR_SRC_SHORTAGE         -2
48 #define CX_ERR_SRC_REMAINDER        -3
49 #define CX_ERR_DEST_OVERRUN         -4
50 #define CX_ERR_ILLEGAL_TABLE        -5
51 
52 //======================================================================
53 //          Expanding compressed data
54 //======================================================================
55 
56 /*---------------------------------------------------------------------------*
57   Name:         CXSecureUncompressAny
58 
59   Description:  Determines the compression type from the data header and performs the appropriate decompression.
60 
61                 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.
62 
63 
64 
65   Arguments:    srcp    Source address
66                 srcSize Source data size
67                 destp   Destination address
68 
69   Returns:      Returns 0 when conversion succeeds
70                 Returns a negative error code if failed.
71  *---------------------------------------------------------------------------*/
72 s32 CXSecureUncompressAny( const void* srcp, u32 srcSize, void* destp );
73 
74 
75 /*---------------------------------------------------------------------------*
76   Name:         CXSecureUncompressRL
77 
78   Description:  8-bit decompression of run-length compressed data
79 
80   - Decompresses run-length compressed data, writing in 8-bit units.
81   - Use 4 byte alignment for the source address.
82 
83   - Data header
84       u32 :4  :                Reserved
85           compType:4          Compression type( = 3)
86           destSize:24 :        Data size after decompression
87 
88   - Flag data format
89       u8  length:7 :           Decompressed data length - 1 (When not compressed)
90                               Decompressed data length - 3 (only compress when the contiguous length is 3 bytes or greater)
91           flag:1 :             (0, 1) = (not compressed, compressed)
92 
93   Arguments:    *srcp   Source address
94                 srcSize Source data size
95                 *destp  Destination address
96 
97   Returns:      Returns 0 when conversion succeeds
98                 Returns a negative error code if failed.
99  *---------------------------------------------------------------------------*/
100 s32 CXSecureUncompressRL( const void *srcp, u32 srcSize, void *destp );
101 
102 
103 /*---------------------------------------------------------------------------*
104   Name:         CXSecureUncompressLZ
105 
106   Description:  8-bit decompression of LZ77 compressed data
107 
108   - Expands LZ77 compressed data, and writes it in 8-bit units.
109   - Use 4 byte alignment for the source address.
110 
111   - Data header
112       u32 :4  :                Reserved
113           compType:4 :         Compression type( = 1)
114           destSize:24 :        Data size after decompression
115 
116   - Flag data format
117       u8  flags :              Compression/no compression flag
118                               (0, 1) = (not compressed, compressed)
119   - Code data format (Big Endian)
120       u16 length:4 :           Decompressed data length - 3 (only compress when the match length is 3 bytes or greater)
121           offset:12 :          Match data offset - 1
122 
123   Arguments:    *srcp   Source address
124                 srcSize Source data size
125                 *destp  Destination address
126 
127   Returns:      Returns 0 when conversion succeeds
128                 Returns a negative error code if failed.
129  *---------------------------------------------------------------------------*/
130 s32 CXSecureUncompressLZ( const void *srcp, u32 srcSize, void *destp );
131 
132 
133 /*---------------------------------------------------------------------------*
134   Name:         CXSecureUncompressHuffman
135 
136   Description:  Decompression of Huffman compressed data
137 
138   - Decompresses Huffman compressed data, writing in 32 bit units.
139   - Use 4 byte alignment for the source address.
140   - Use 4 byte alignment for the destination address.
141   - The target decompression buffer size must be prepared in 4 byte multiples.
142 
143   - Data header
144       u32 bitSize:4 :          Bit size of 1 data (Normally 4|8)
145           compType:4          Compression type( = 2)
146           destSize:24 :        Data size after decompression
147 
148   - Tree table
149       u8           treeSize :       tree table size/2 - 1
150       TreeNodeData nodeRoot :       Root node
151 
152       TreeNodeData nodeLeft :       Root left node
153       TreeNodeData nodeRight :      Root right node
154 
155       TreeNodeData nodeLeftLeft :   Left left node
156       TreeNodeData nodeLeftRight :  Left right node
157 
158       TreeNodeData nodeRightLeft :  Right left node
159       TreeNodeData nodeRightRight : Right right node
160 
161               .
162               .
163 
164     The compressed data itself follows
165 
166   - TreeNodeData structure
167       u8  nodeNextOffset:6 :   Offset to the next node data - 1 (2 byte units)
168           rightEndFlag:1 :     Right node end flag
169           leftEndzflag:1 :     Left node end flag
170                               When the end flag is set
171                               There is data in next node
172 
173   Arguments:    *srcp   Source address
174                 srcSize Source data size
175                 *destp  Destination address
176 
177   Returns:      Returns 0 when conversion succeeds
178                 Returns a negative error code if failed.
179  *---------------------------------------------------------------------------*/
180 s32 CXSecureUncompressHuffman( const void *srcp, u32 srcSize, void *destp );
181 
182 /*---------------------------------------------------------------------------*
183   Name:         CXSecureUncompressLH
184 
185   Description:  Decompress LZ-Huffman (combined) compression.
186 
187   Arguments:    *srcp   Source address
188                 srcSize Source size
189                 *destp  Destination address
190                 *work  Working buffer
191                         The required size is CX_UNCOMPRESS_LH_WORK_SIZE (2176B)
192 
193   Returns:      None.
194  *---------------------------------------------------------------------------*/
195 s32 CXSecureUncompressLH( const u8* srcp, u32 srcSize, u8* dstp, void* work );
196 
197 
198 /*---------------------------------------------------------------------------*
199   Name:         CXSecureUncompressLRC
200 
201   Description:  Decompress LZ-RangeCoder (combined) compression.
202 
203   Arguments:    *srcp   Source address
204                 srcSize Source size
205                 *destp  Destination address
206                 *work  Working buffer
207                         The required size is CX_UNCOMPRESS_LRC_WORK_SIZE (36864B)
208 
209   Returns:      None.
210  *---------------------------------------------------------------------------*/
211 s32 CXSecureUncompressLRC( const u8* srcp, u32 srcSize, u8* dstp, void* work );
212 
213 
214 
215 /*---------------------------------------------------------------------------*
216   Name:         CXSecureUnfilterDiff
217 
218   Description:  8-bit decompression to restore differential filter conversion.
219 
220     - Decompresses a differential filter, writing in 8 bit units.
221     - Cannot decode directly into VRAM.
222     - If the compressed data size was not a multiple of four, adjust by padding with 0s as much as possible.
223 
224     - Use 4 byte alignment for the source address.
225 
226   Arguments:    *srcp   Source address
227                 srcSize Source size
228                 *destp  Destination address
229 
230   Returns:      Returns 0 when conversion succeeds
231                 Returns a negative error code if failed.
232  *---------------------------------------------------------------------------*/
233 s32 CXSecureUnfilterDiff( register const void *srcp, u32 srcSize, register void *destp );
234 
235 
236 
237 //================================================================================
238 
239 
240 /*---------------------------------------------------------------------------*
241   Name:         CXiUncompressBackward
242 
243   Description:  Uncompress special archive for module compression.
244 
245   Arguments:    bottom      = Bottom adrs of packed archive + 1
246                 bottom[-12] = offset for top    of compressed data
247                                  inp_top = bottom + bottom[-12]
248                 bottom[ -8] = offset for bottom of compressed data
249                                  inp     = bottom + bottom[ -8]
250                 bottom[ -4] = offset for bottom of original data
251                                  outp    = bottom + bottom[ -4]
252 
253                 typedef struct
254                 {
255                     int         bufferTop;
256                     int         compressBottom;
257                     int         originalBottom;
258                 }   CompFooter;
259 
260   Returns:      Error Codes
261  *---------------------------------------------------------------------------*/
262 // !!!! Coded in libraries/init/ARM9/crt0.c
263 // void CXiUncompressBackward( void *bottom );
264 
265 
266 
267 
268 #ifdef __cplusplus
269 } /* extern "C" */
270 #endif
271 
272 /* __CX_SECURE_UNCOMPRESSION_H__ */
273 #endif
274