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