/*---------------------------------------------------------------------------* Project: compress/uncompress library File: CXStreamingUncompression.c Programmer: Makoto Takano Copyright 2005 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. *---------------------------------------------------------------------------*/ #include #include #include "CXUtil.h" /*---------------------------------------------------------------------------* Name: CXInitUncompContextRL Description: Initializes the streaming uncompression context for run-length compressed data Arguments: context Pointer to the run-length uncompressed context dest Destination address for uncompressed data Returns: Can get the data size after decompression. *---------------------------------------------------------------------------*/ void CXInitUncompContextRL( CXUncompContextRL *context, void* dest ) { context->destp = (u8*)dest; context->destCount = 0; context->flags = 0; context->length = 0; context->headerSize = 4; } /*---------------------------------------------------------------------------* Name: CXInitUncompContextLZ Description: Initializes the streaming uncompression context for LZ compressed data. Arguments: context Pointer to the LZ uncompressed context dest Destination address for uncompressed data header a pointer to the leading data for the compressed data *---------------------------------------------------------------------------*/ void CXInitUncompContextLZ( CXUncompContextLZ *context, void* dest ) { context->destp = (u8*)dest; context->destCount = 0; context->flags = 0; context->flagIndex = 0; context->length = 0; context->lengthFlg = 0; context->headerSize = 4; } /*---------------------------------------------------------------------------* Name: CXInitUncompContextHuffman Description: Initializes the streaming uncompression context for Huffman compressed data. Arguments: context Pointer to the Huffman uncompressed context dest Destination address for uncompressed data header a pointer to the leading data for the compressed data *---------------------------------------------------------------------------*/ void CXInitUncompContextHuffman( CXUncompContextHuffman *context, void* dest ) { context->destp = (u8*)dest; context->destCount = 0; context->bitSize = 0; context->treeSize = -1; context->treep = &context->tree[ 0 ]; context->destTmp = 0; context->destTmpCnt = 0; context->srcTmp = 0; context->srcTmpCnt = 0; context->headerSize = 4; } /*---------------------------------------------------------------------------* Name: CXReadUncompRL Description: This function performs streaming uncompression of run-length compressed data. Data is written in units of 8bits. Data cannot be directly uncompressed to VRAM. Arguments: context Pointer to the run-length uncompressed context data Pointer to the next data len Data size Returns: Size of remaining uncompressed data. *---------------------------------------------------------------------------*/ s32 CXReadUncompRL( CXUncompContextRL *context, const void* data, u32 len ) { const u8* srcp = (const u8*)data; u8 srcTmp; // Header analysis while ( context->headerSize > 0 ) { if ( --context->headerSize <= 2 ) { context->destCount |= (*srcp << ((2 - context->headerSize) * 8)); } ++srcp; if ( --len == 0 ) { return (context->headerSize == 0)? context->destCount : -1; } } while ( context->destCount > 0 ) { // process when length is greater than 0 if ( ! (context->flags & 0x80) ) // compressed data has a length not equal to 0 { while ( context->length > 0 ) { *context->destp++ = *srcp++; context->length--; context->destCount--; len--; // end when the prepared buffer has been read in full if ( len == 0 ) { return context->destCount; } } } else if ( context->length > 0 ) // compressed data has a length not equal to 0 { srcTmp = *srcp++; len--; while ( context->length > 0 ) { *context->destp++ = srcTmp; context->length--; context->destCount--; } if ( len == 0 ) { return context->destCount; } } // reading the flag byte context->flags = *srcp++; len--; context->length = (u16)(context->flags & 0x7F); if ( context->flags & 0x80 ) { context->length += 3; } else { context->length += 1; } if ( len == 0 ) { return context->destCount; } } return context->destCount; } /*---------------------------------------------------------------------------* Name: CXReadUncompLZ Description: This function performs streaming uncompression of LZ compressed data. Data is written in units of 8bits. Data cannot be directly uncompressed to VRAM. Arguments: context Pointer to the LZ uncompressed context data Pointer to the next data len Data size Returns: Size of remaining uncompressed data. *---------------------------------------------------------------------------*/ s32 CXReadUncompLZ( CXUncompContextLZ *context, const void* data, u32 len ) { const u8* srcp = (const u8*)data; s32 offset; // Header analysis while ( context->headerSize > 0 ) { if ( --context->headerSize <= 2 ) { context->destCount |= (*srcp << ((2 - context->headerSize) * 8)); } ++srcp; if ( --len == 0 ) { return (context->headerSize == 0)? context->destCount : -1; } } while ( context->destCount > 0 ) { while ( context->flagIndex > 0 ) { if ( len == 0 ) { return context->destCount; } if ( ! (context->flags & 0x80) ) // process for non-compressed data { *context->destp++ = *srcp++; context->destCount--; len--; } else // process for compressed data { if ( context->lengthFlg == 0 ) { context->length = *srcp++; context->lengthFlg = 1; len--; } if ( len == 0 ) { return context->destCount; } offset = (context->length & 0xF) << 8; context->length = (u8)( (context->length >> 4) + 3 ); offset = (offset | *srcp++) + 1; len--; context->lengthFlg = 0; // copy a length amount of data at the offset position while ( context->length > 0 ) { *context->destp = context->destp[ -offset ]; context->destp++; context->destCount--; context->length--; } } if ( context->destCount == 0 ) { return context->destCount; } context->flags <<= 1; context->flagIndex--; } if ( len == 0 ) { return context->destCount; } // read a new flag context->flags = *srcp++; context->flagIndex = 8; len--; } return context->destCount; } // get the next node in the Huffman signed table static inline u8* GetNextNode( const u8* pTree, u32 select ) { return (u8*)( ((u32)pTree & ~0x1) + ( ( (*pTree & 0x3F) + 1 ) * 2 ) + select ); } /*---------------------------------------------------------------------------* Name: CXReadUncompHuffman Description: This function performs streaming uncompression of Huffman compressed data. Arguments: context Pointer to the Huffman uncompressed context data Pointer to the next data len Data size Returns: Size of remaining uncompressed data. *---------------------------------------------------------------------------*/ s32 CXReadUncompHuffman( CXUncompContextHuffman *context, const void* data, u32 len ) { #define TREE_END_MASK 0x80U const u8* srcp = (const u8*)data; u32 select; u32 endFlag; while ( context->headerSize > 0 ) { if ( --context->headerSize <= 2 ) { context->destCount |= (*srcp << ((2 - context->headerSize) * 8)); } else { context->bitSize = (u8)(*srcp & 0xF); } ++srcp; if ( --len == 0 ) { return (context->headerSize == 0)? context->destCount : -1; } } // treeSize is set to -1 in CXInitUncompContextHuffman // when context->treeSize is negative, the data's beginning is used if ( context->treeSize < 0 ) { context->treeSize = (s16)( ( *srcp + 1 ) * 2 - 1); *context->treep++ = *srcp++; len--; } // load the Huffman signed table while ( context->treeSize > 0 ) { if ( len == 0 ) { return context->destCount; } *context->treep++ = *srcp++; context->treeSize--; len--; if ( context->treeSize == 0 ) { context->treep = &context->tree[ 1 ]; } } // decoding process while ( context->destCount > 0 ) { // src data is read in 4-byte units while ( context->srcTmpCnt < 32 ) { if ( len == 0 ) { return context->destCount; } context->srcTmp |= (*srcp++) << context->srcTmpCnt; len--; context->srcTmpCnt += 8; } // the loaded 32 bits are decoded After those 32 bits are processed, the next 4 bytes are read. while ( context->srcTmpCnt > 0 ) { select = context->srcTmp >> 31; endFlag = (*context->treep << select) & TREE_END_MASK; context->treep = GetNextNode( context->treep, select ); context->srcTmp <<= 1; context->srcTmpCnt--; if ( ! endFlag ) { continue; } // When the Huffman tree's terminal flag is set, data is stored // at the end of the offset context->destTmp >>= context->bitSize; context->destTmp |= *context->treep << ( 32 - context->bitSize ); context->treep = &context->tree[ 1 ]; context->destTmpCnt += context->bitSize; // write in 4 byte units if ( context->destTmpCnt == 32 ) { *(u32*)context->destp = CXiConvertEndian_( context->destTmp ); context->destp += 4; context->destCount -= 4; context->destTmpCnt = 0; if ( context->destCount <= 0 ) { return 0; } } } } return 0; }