1 /*---------------------------------------------------------------------------*
2   Project:     Compress/uncompress library
3   File:        CXStreamingUncompression.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: CXStreamingUncompression.h,v $
14   Revision 1.16.4.1  2008/09/25 06:01:22  takano_makoto
15   Changed CXiInitXXXFront to CXInitXXXFront.
16 
17   Revision 1.16  2007/10/31 12:52:11  takano_makoto
18   Appended the LH and LRC formats.
19 
20   Revision 1.15  2007/05/30 04:19:52  takano_makoto
21   Added CXiInitUncompContextXXFront()
22 
23   Revision 1.14  2007/05/18 10:50:39  yasuh-to
24   Rollback to SYSTEMMENU2_DEV_BRANCH(HEAD)
25 
26   Revision 1.11  2006/07/27 07:40:19  takano_makoto
27   Added const modifier to CXIsFinishedUncompXXX parameter.
28 
29   Revision 1.10  2006/07/27 04:25:12  takano_makoto
30   Changed Condition of CXIsFinishedUncompXXX()
31 
32   Revision 1.9  2006/07/27 04:12:14  takano_makoto
33   Changed Interface of CXInitUncompContextXXX()
34   Changed Parameter type u8* to void*
35 
36   Revision 1.8  2006/07/12 01:11:41  takano_makoto
37   Fixed comment
38 
39   Revision 1.7  2006/07/10 08:38:17  takano_makoto
40   Added CXIsFinishedUncompXXX() to determine when streaming decompression is complete
41   Changed the spec for CXReadUncompXXX() to include header in passed data
42   (for a simplified interface)
43 
44   Revision 1.6  2006/07/06 09:19:14  takano_makoto
45   Changed the include guard to the REVOLUTION_SDK format
46 
47   Revision 1.5  2006/07/06 04:40:14  takano_makoto
48   Changed RL8 and LZ8 to RL and LZ
49 
50   Revision 1.4  2006/07/05 11:14:43  takano_makoto
51   Revised the include guard
52 
53   Revision 1.3  2006/07/05 08:13:36  takano_makoto
54   Revised comments
55   Deleted context members only needed for 16 bit access functions
56 
57   Revision 1.2  2006/07/04 08:26:20  takano_makoto
58   Changed the prefix from the DS version
59 
60   $NoKeywords: $
61  *---------------------------------------------------------------------------*/
62 
63 
64 #ifndef __CX_STREAMING_UNCOMPRESSION_STREAM_H__
65 #define __CX_STREAMING_UNCOMPRESSION_STREAM_H__
66 
67 #include <dolphin/types.h>
68 #include <revolution/os.h> // For ASSERT use.
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
74 
75 typedef struct
76 {
77     u8 *destp;                         // Write-destination pointer:                     4B
78     s32 destCount;                     // Remaining size to write:                     4B
79     s32 forceDestCount;                // Forcibly set the decompression size             4B
80     u16 length;                        // Remaining size of continuous write:      2B
81     u8  flags;                         // Compression flag:                      1B
82     u8  headerSize;                    // Size of header being read             1B
83                                        //                         Total is 16B
84 }
85 CXUncompContextRL;
86 
87 
88 typedef struct
89 {
90     u8 *destp;                         // Write-destination pointer:                     4B
91     s32 destCount;                     // Remaining size to write:                     4B
92     s32 forceDestCount;                // Forcibly set the decompression size             4B
93     s32 length;                        // Remaining length of continuous write:              4B
94     u8  lengthFlg;                     // 'length' obtained flag:              1B
95     u8  flags;                         // Compression flag:                      1B
96     u8  flagIndex;                     // Current compression flag index:  1B
97     u8  headerSize;                    // Size of header being read:             1B
98     u8  exFormat;                      // LZ77 compression extension option:          1B
99     u8  padding_[3];                   //                                 3B
100                                        //                             Total is 24B
101 }
102 CXUncompContextLZ;
103 
104 
105 typedef struct
106 {
107     u8 *destp;                         // Write-destination pointer:                     4B
108     s32 destCount;                     // Remaining size to write:                     4B
109     s32 forceDestCount;                // Forcibly set the decompression size:             4B
110     u8 *treep;                         // Huffman encoding table, current pointer: 4B
111     u32 srcTmp;                        // Data being read:                   4B
112     u32 destTmp;                       // Data being decoded:                     4B
113     s16 treeSize;                      // Size of Huffman encoding table:             2B
114     u8  srcTmpCnt;                     // Size of data being read:             1B
115     u8  destTmpCnt;                    // Number of bits that have been decoded:                     1B
116     u8  bitSize;                       // Size of encoded bits:                     1B
117     u8  headerSize;                    // Size of header being read:             1B
118     u8  padding_[2];                   //                                        2B
119     u8  tree[0x200];                   // Huffman encoding table:                 512B (32B is enough for 4-bit encoding, but allocated enough for 8-bit encoding)
120                                        //                                   Total = 544B  (60B sufficient if 4-bit encoding)
121 }
122 CXUncompContextHuffman;
123 
124 
125 typedef struct
126 {
127     u8   *destp;                         // Write-destination pointer:                     4B
128     s32  destCount;                      // Remaining size to write:                     4B
129     s32  forceDestCount;                 // Forcibly set the decompression size             4B
130     u16  huffTable9 [ 1 << (9 + 1) ];    // Huffman encoding table: 2048B
131     u16  huffTable12[ 1 << (5 + 1) ];    // Huffman encoding table: 128B
132     u16  *nodep;                         // Node during a Huffman table search: 4B
133     s32  tableSize9;                     // Table size during a load: 4B
134     s32  tableSize12;                    // Table size during a load: 4B
135     u32  tableIdx;                       // Index for the table load position: 4B
136     u32  stream;                         // Bit stream for loading: 4B
137     u32  stream_len;                     // Number of valid stream bits for loading: 4B
138     u16  length;                         // Read length for LZ compression: 2B
139     s8   offset_bits;                    // Bit length for offset information: 1B
140     u8   headerSize;                     // Size of header being read:             1B
141 }                                        //                             Total is 2216B
142 CXUncompContextLH;
143 
144 
145 typedef struct
146 {
147     u8   *destp;                         // Write-destination pointer:                     4B
148     s32  destCount;                      // Remaining size to write:                     4B
149     s32  forceDestCount;                 // Forcibly set the decompression size             4B
150     u32  freq9    [ 1 << 9  ];           // Frequency table for code data (2048 bytes)
151     u32  low_cnt9 [ 1 << 9  ];           // low_cnt table for code data: 2048B
152     u32  freq12   [ 1 << 12 ];           // Frequency table for offset data: 16384B
153     u32  low_cnt12[ 1 << 12 ];           // low_cnt table for offset data: 16384B
154     u32  total9;                         // Total value for code data: 4B
155     u32  total12;                        // Total value for offset data: 4B
156     u32  range;                          // Range state of a range coder: 4B
157     u32  code;                           // Code state of a range coder: 4B
158     u32  low;                            // Low state of a range coder: 4B
159     u32  carry_cnt;                      // Number of carry digits for a range coder: 4B
160     u8   carry;                          // Carry state for a range coder: 1B
161     u8   codeLen;                        // Code length required for a range coder: 1B
162     u16  length;                         // Read length for LZ compression: 2B
163 
164     u8   headerSize;                     // Size of header being read:             1B
165     u8   padding_[3];                    //                                    3B
166 }                                        //                             Total is 36908B
167 CXUncompContextLRC;
168 
169 /*---------------------------------------------------------------------------*
170   Name        : CXInitUncompContextRL
171 
172   Description : Initializes the streaming decompression context for run-length compressed data.
173 
174 
175   Arguments   : context Pointer to the run-length uncompressed context
176                 dest        Destination address for uncompressed data
177 
178   Returns     : Can get the data size after decompression.
179 
180  *---------------------------------------------------------------------------*/
181 void CXInitUncompContextRL( CXUncompContextRL          *context,
182                             void                       *dest );
183 
184 
185 /*---------------------------------------------------------------------------*
186   Name        : CXInitUncompContextLZ
187 
188   Description : Initializes the streaming decompression context for LZ-compressed data.
189 
190   Arguments   : context Pointer to the LZ uncompressed context
191                 dest        Destination address for uncompressed data
192 
193  *---------------------------------------------------------------------------*/
194 void CXInitUncompContextLZ( CXUncompContextLZ          *context,
195                             void                       *dest );
196 
197 /*---------------------------------------------------------------------------*
198   Name        : CXInitUncompContextHuffman
199 
200   Description : Initializes the streaming decompression context for Huffman-compressed data.
201 
202   Arguments   : context Pointer to the Huffman uncompressed context
203                 dest        Destination address for uncompressed data
204 
205  *---------------------------------------------------------------------------*/
206 void CXInitUncompContextHuffman( CXUncompContextHuffman    *context,
207                                  void                      *dest );
208 
209 
210 /*---------------------------------------------------------------------------*
211   Name:         CXInitUncompContextLH
212 
213   Description:  Initializes the streaming decompression context for LZ-Huffman (combined) compressed data.
214 
215   Arguments:    context: Pointer to the decompressed context
216                 dest        Destination address for uncompressed data
217 
218   Returns:      None.
219  *---------------------------------------------------------------------------*/
220 void CXInitUncompContextLH( CXUncompContextLH * context, void* dest );
221 
222 
223 
224 /*---------------------------------------------------------------------------*
225   Name:         CXInitUncompContextLRC
226 
227   Description:  Initializes the streaming decompression context for LZ/Range Coder (combined) compressed data.
228 
229   Arguments:    context: Pointer to the decompressed context
230                 dest        Destination address for uncompressed data
231 
232   Returns:      None.
233  *---------------------------------------------------------------------------*/
234 void CXInitUncompContextLRC( CXUncompContextLRC * context, void* dest );
235 
236 
237 /*---------------------------------------------------------------------------*
238   Name        : CXReadUncompRL
239 
240   Description : This function performs streaming decompression of run-length compressed data.
241 
242   Arguments   : context Pointer to the run-length uncompressed context
243                 data    Pointer to the next data
244                 len     Data size
245 
246   Returns     : Size of remaining uncompressed data.
247 
248  *---------------------------------------------------------------------------*/
249 s32 CXReadUncompRL( CXUncompContextRL *context, const void *data, u32 len );
250 
251 /*---------------------------------------------------------------------------*
252   Name        : CXReadUncompLZ
253 
254   Description : This function performs streaming decompression of LZ-compressed data.
255 
256   Arguments   : context Pointer to the LZ uncompressed context
257                 data    Pointer to the next data
258                 len     Data size
259 
260   Returns     : Size of remaining uncompressed data.
261 
262  *---------------------------------------------------------------------------*/
263 s32 CXReadUncompLZ( CXUncompContextLZ *context, const void *data, u32 len );
264 
265 /*---------------------------------------------------------------------------*
266   Name        : CXReadUncompHuffman
267 
268   Description : This function performs streaming decompression of Huffman-compressed data.
269 
270   Arguments   : context Pointer to the Huffman uncompressed context
271                 data    Pointer to the next data
272                 len     Data size
273 
274   Returns     : Size of remaining uncompressed data.
275 
276  *---------------------------------------------------------------------------*/
277 s32 CXReadUncompHuffman( CXUncompContextHuffman *context, const void *data, u32 len );
278 
279 
280 /*---------------------------------------------------------------------------*
281   Name:         CXReadUncompLH
282 
283   Description:  This function performs streaming decompression of LZ-Huffman (combined) compressed data.
284 
285   Arguments:    *context: Pointer to the decompressed context
286                 data:     Data pointer
287                 len     Data size
288 
289   Returns:      Size of remaining uncompressed data
290  *---------------------------------------------------------------------------*/
291 s32 CXReadUncompLH( CXUncompContextLH *context, const void* data, u32 len );
292 
293 
294 /*---------------------------------------------------------------------------*
295   Name:         CXReadUncompLRC
296 
297   Description:  This function performs streaming decompression of LZ/Range Coder (combined) compressed data.
298 
299   Arguments:    *context: Pointer to the decompressed context
300                 data:     Data pointer
301                 len:    Data size
302 
303   Returns:      Size of remaining uncompressed data
304  *---------------------------------------------------------------------------*/
305 s32 CXReadUncompLRC( CXUncompContextLRC *context, const void* data, u32 len );
306 
307 
308 /*---------------------------------------------------------------------------*
309   Name:         CXIsFinishedUncompRL
310 
311   Description:  This function determines whether streaming decompression for run-length compression has completed or not.
312 
313   Arguments:    *context    Pointer to the run-length decompressed context
314 
315   Returns:      TRUE when decompression is complete
316                 FALSE when data needing to be read still remains
317  *---------------------------------------------------------------------------*/
CXIsFinishedUncompRL(const CXUncompContextRL * context)318 static inline BOOL CXIsFinishedUncompRL( const CXUncompContextRL *context )
319 {
320     return (context->destCount > 0 || context->headerSize > 0)? FALSE : TRUE;
321 }
322 
323 
324 /*---------------------------------------------------------------------------*
325   Name:         CXIsFinishedUncompLZ
326 
327   Description:  This function determines whether streaming decompression for LZ compression has completed or not.
328 
329   Arguments:    *context Pointer to the LZ decompressed context
330 
331   Returns:      TRUE when decompression is complete
332                 FALSE when data needing to be read still remains
333  *---------------------------------------------------------------------------*/
CXIsFinishedUncompLZ(const CXUncompContextLZ * context)334 static inline BOOL CXIsFinishedUncompLZ( const CXUncompContextLZ *context )
335 {
336     return (context->destCount > 0 || context->headerSize > 0)? FALSE : TRUE;
337 }
338 
339 
340 /*---------------------------------------------------------------------------*
341   Name:         CXIsFinishedUncompHuffman
342 
343   Description:  This function determines whether streaming decompression for Huffman compression has completed or not.
344 
345   Arguments:    *context pointer to the Huffman decompressed context
346 
347   Returns:      TRUE when decompression is complete
348                 FALSE when data needing to be read still remains
349  *---------------------------------------------------------------------------*/
CXIsFinishedUncompHuffman(const CXUncompContextHuffman * context)350 static inline BOOL CXIsFinishedUncompHuffman( const CXUncompContextHuffman *context )
351 {
352     return (context->destCount > 0 || context->headerSize > 0)? FALSE : TRUE;
353 }
354 
355 
356 /*---------------------------------------------------------------------------*
357   Name:         CXIsFinishedUncompLH
358 
359   Description:  This function determines if streaming decompression has completed for LZ-Huffman (combined) compression.
360 
361   Arguments:    *context: Pointer to the decompressed context
362 
363   Returns:      TRUE when decompression is complete
364                 FALSE when data needing to be read still remains
365  *---------------------------------------------------------------------------*/
CXIsFinishedUncompLH(const CXUncompContextLH * context)366 static inline BOOL CXIsFinishedUncompLH( const CXUncompContextLH *context )
367 {
368     return (context->destCount > 0 || context->headerSize > 0)? FALSE : TRUE;
369 }
370 
371 /*---------------------------------------------------------------------------*
372   Name:         CXIsFinishedUncompLRC
373 
374   Description:  This function determines if streaming decompression has completed for LZ/Range Coder (combined) compression.
375 
376   Arguments:    *context: Pointer to the decompressed context
377 
378   Returns:      TRUE when decompression is complete
379                 FALSE when data needing to be read still remains
380  *---------------------------------------------------------------------------*/
CXIsFinishedUncompLRC(const CXUncompContextLRC * context)381 static inline BOOL CXIsFinishedUncompLRC( const CXUncompContextLRC *context )
382 {
383     return (context->destCount > 0 || context->headerSize > 0)? FALSE : TRUE;
384 }
385 
386 
387 /*---------------------------------------------------------------------------*
388   Name:         CXInitUncompContextXXFront
389 
390   Description:  Initializes the streaming decompression context for compressed data. (Includes specification for decompression size)
391 
392   Arguments:    *context    Pointer to the run-length decompressed context
393                 *dest       Destination address for decompressed data
394                 destSize    Decompressed size setting
395 
396   Returns:      None.
397  *---------------------------------------------------------------------------*/
CXInitUncompContextRLFront(CXUncompContextRL * context,void * dest,s32 destSize)398 static inline void CXInitUncompContextRLFront( CXUncompContextRL *context, void *dest, s32 destSize )
399 {
400     ASSERT( destSize > 0 );
401     CXInitUncompContextRL( context, dest );
402     context->forceDestCount = destSize;
403 }
404 
CXInitUncompContextLZFront(CXUncompContextLZ * context,void * dest,s32 destSize)405 static inline void CXInitUncompContextLZFront( CXUncompContextLZ *context, void *dest, s32 destSize )
406 {
407     ASSERT( destSize > 0 );
408     CXInitUncompContextLZ( context, dest );
409     context->forceDestCount = destSize;
410 }
411 
CXInitUncompContextHuffmanFront(CXUncompContextHuffman * context,void * dest,s32 destSize)412 static inline void CXInitUncompContextHuffmanFront( CXUncompContextHuffman *context, void *dest, s32 destSize )
413 {
414     ASSERT( destSize > 0 );
415     ASSERT( (destSize % 4) == 0 );
416     CXInitUncompContextHuffman( context, dest );
417     context->forceDestCount = destSize;
418 }
419 
CXInitUncompContextLHFront(CXUncompContextLH * context,void * dest,s32 destSize)420 static inline void CXInitUncompContextLHFront( CXUncompContextLH *context, void *dest, s32 destSize )
421 {
422     ASSERT( destSize > 0 );
423     CXInitUncompContextLH( context, dest );
424     context->forceDestCount = destSize;
425 }
426 
CXInitUncompContextLRCFront(CXUncompContextLRC * context,void * dest,s32 destSize)427 static inline void CXInitUncompContextLRCFront( CXUncompContextLRC *context, void *dest, s32 destSize )
428 {
429     ASSERT( destSize > 0 );
430     CXInitUncompContextLRC( context, dest );
431     context->forceDestCount = destSize;
432 }
433 
434 
435 //=====================================================================
436 // These aliases are defined for compatibility with older versions of the function names.
437 // These may be deleted in the future.
438 //=====================================================================
439 #define CXiInitUncompContextRLFront         CXInitUncompContextRLFront
440 #define CXiInitUncompContextLZFront         CXInitUncompContextLZFront
441 #define CXiInitUncompContextHuffmanFront    CXInitUncompContextHuffmanFront
442 #define CXiInitUncompContextLHFront         CXInitUncompContextLHFront
443 #define CXiInitUncompContextLRCFront        CXInitUncompContextLRCFront
444 
445 #ifdef __cplusplus
446 } /* extern "C" */
447 #endif
448 
449 /* __CX_STREAMING_UNCOMPRESSION_STREAM_H__ */
450 #endif
451