TwlIPL/build/tools/makegcdfirm/compress.c
yosiokat 378c048624 ソースファイルのヘッダのSVN自動変換キーワードを修正。
git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/TwlIPL/trunk@3 b08762b0-b915-fc4b-9d8c-17b2551a87ff
2007-09-06 05:58:23 +00:00

294 lines
8.4 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*---------------------------------------------------------------------------*
Project: TwlFirm - tools - makegcdfirm
File: compress.c
Copyright 2007 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.
$Date:: 2007-09-06$
$Rev$
$Author$
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h> // atoi()
#include <string.h> // strcmp()
#include <ctype.h> // isprint()
#include <unistd.h> // chdir()
#include <tchar.h>
#include <limits.h> // UCHAR_MAX
#include <time.h>
#include <sys/stat.h> // stat()
#include "elf.h"
#include "misc.h"
#include "defval.h"
#include "format_rom.h"
#include "format_nlist.h"
#include "makegcdfirm.h"
//#define ADD_HEADER
#define DIFF_CODE_HEADER (0x80)
#define RL_CODE_HEADER (0x30)
#define LZ_CODE_HEADER (0x10)
#define HUFF_CODE_HEADER (0x20)
#define CODE_HEADER_MASK (0xF0)
//===========================================================================
// LZ77ˆ³<CB86>k
//===========================================================================
static u8 SearchLZ(const u8 *nextp, u32 remainSize, u16 *offset);
static u16 windowPos;
static u16 windowLen;
static s16 LZOffsetTable[4096];
static s16 LZByteTable[256];
static s16 LZEndTable[256];
static void LZInitTable(void)
{
u16 i;
for (i = 0; i < 256; i++)
{
LZByteTable[i] = -1;
LZEndTable[i] = -1;
}
windowPos = 0;
windowLen = 0;
}
static void SlideByte(const u8 *srcp)
{
s16 offset;
u8 in_data = *srcp;
u16 insert_offset;
if (windowLen == 4096)
{
u8 out_data = *(srcp - 4096);
if ((LZByteTable[out_data] = LZOffsetTable[LZByteTable[out_data]]) == -1)
{
LZEndTable[out_data] = -1;
}
insert_offset = windowPos;
}
else
{
insert_offset = windowLen;
}
offset = LZEndTable[in_data];
if (offset == -1)
{
LZByteTable[in_data] = insert_offset;
}
else
{
LZOffsetTable[offset] = insert_offset;
}
LZEndTable[in_data] = insert_offset;
LZOffsetTable[insert_offset] = -1;
if (windowLen == 4096)
{
windowPos = (u16)((windowPos + 1) % 0x1000);
}
else
{
windowLen++;
}
}
static void LZSlide(const u8 *srcp, u32 n)
{
u32 i;
for (i = 0; i < n; i++)
{
SlideByte(srcp++);
}
}
/*---------------------------------------------------------------------------*
Name: MI_CompressLZ
Description: LZ77ˆ³<CB86>kð<E2809A>sȤŠÖ<C5A0>
Arguments: srcp ˆ³<CB86>kŒ³ƒf<C692>[ƒ^‚ւ̃|ƒCƒ“ƒ^
size ˆ³<CB86>kŒ³ƒf<C692>[ƒ^ƒTƒCƒY
dstp ˆ³<CB86>k<EFBFBD>æƒf<C692>[ƒ^‚ւ̃|ƒCƒ“ƒ^
ˆ³<CB86>kŒ³ƒf<C692>[ƒ^æèàå«¢ƒTƒCƒỸoƒbƒtƒ@ª•K—vÅ·<E2809A>B
Returns: ˆ³<CB86>kŒã̃f<C692>[ƒ^ƒTƒCƒY<C692>B
ˆ³<CB86>kŒã̃f<C692>[ƒ^ªˆ³<CB86>kOæèàå«­Èé<E2809A>ê<EFBFBD>É͈³<CB86>kðfµ0ð•Եܷ<E2809A>B
*---------------------------------------------------------------------------*/
u32 LZCompWrite(u8 *srcp, u32 size, u8 *dstp, int boundary)
{
u32 LZDstCount; // ˆ³<CB86>kƒf<C692>[ƒ^̃oƒCƒg<C692>
u8 LZCompFlags; // ˆ³<CB86>kÌ—L³ðަ·ƒtƒ‰ƒOŒn—ñ
u8 *LZCompFlagsp; // LZCompFlags ðŠi”[·éƒ<C3A9>ƒƒŠ—̈æðƒ|ƒCƒ“ƒg
u16 lastOffset; // ˆêvƒf<C692>[ƒ^ÜÅ̃IƒtƒZƒbƒg (»ÌŽž“_ÅÌ<E2809A>Å·ˆêvƒf<C692>[ƒ^)
u8 lastLength; // ˆêvƒf<C692>[ƒ^’· (»ÌŽž“_ÅÌ<E2809A>Å·ˆêvƒf<C692>[ƒ^)
u8 i;
u32 dstMax;
#ifdef ADD_HEADER
*(u32 *)dstp = size << 8 | LZ_CODE_HEADER; // ƒf<C692>[ƒ^<5E>Eƒwƒbƒ_
dstp += 4;
#endif
LZDstCount = 4;
dstMax = size;
LZInitTable();
while (size > 0)
{
LZCompFlags = 0;
LZCompFlagsp = dstp++; // ƒtƒ‰ƒOŒn—ñÌŠi”[<5B>æ
LZDstCount++;
// ƒtƒ‰ƒOŒn—ñª8ƒrƒbƒgƒf<C692>[ƒ^ƵĊi”[³êé½ß<E2809A>A8‰ñƒ<C692>[ƒv
for (i = 0; i < 8; i++)
{
LZCompFlags <<= 1; // <20>‰‰ñ (i=0) ‚Í“Á‚ɈӖ¡‚͂Ȃ¢
if (size <= 0)
{
// <20>I[É—ˆ½<E2809A>ê<EFBFBD>̓tƒ‰ƒOð<E2809A>ÅŒãÜŃVƒtƒg³¹Ä©ç<E2809A>I—¹
continue;
}
if ((lastLength = SearchLZ(srcp, size, &lastOffset)))
{
// ˆ³<CB86>k‰Â”\È<E2809A>ê<EFBFBD>̓tƒ‰ƒOð—§Äé
LZCompFlags |= 0x1;
// ƒIƒtƒZƒbƒgÍ<E2809A>ãˆÊ4ƒrƒbƒgƉºˆÊ8ƒrƒbƒgÉ•ª¯ÄŠi”[
*dstp++ = (u8)((lastLength - 3) << 4 | (lastOffset - 1) >> 8);
*dstp++ = (u8)((lastOffset - 1) & 0xff);
LZDstCount += 2;
LZSlide(srcp, lastLength);
srcp += lastLength;
size -= lastLength;
}
else
{
// ˆ³<CB86>kȵ
LZSlide(srcp, 1);
*dstp++ = *srcp++;
size--;
LZDstCount++;
}
} // 8‰ñƒ<C692>[ƒv<C692>I—¹
*LZCompFlagsp = LZCompFlags; // ƒtƒ‰ƒOŒn—ñðŠi”[
}
// 16ƒoƒCƒg«ŠEƒAƒ‰ƒCƒ“ƒ<E2809C>ƒ“ƒg
// ƒAƒ‰ƒCƒ“ƒ<E2809C>ƒ“ƒg—pƒf<C692>[ƒ^0 ̓f<C692>[ƒ^ƒTƒCƒYÉŠÜßé
i = 0;
while (LZDstCount & (boundary - 1))
// while ((LZDstCount + i) & 0x3)
{
*dstp++ = 0;
LZDstCount++;
i++;
}
return LZDstCount;
}
//--------------------------------------------------------
// LZ77ˆ³<CB86>kŃXƒ‰ƒCƒhÌ©ç<E2809A>Å·ˆêv—ñðŒŸ<C592>õµÜ·<E2809A>B
// Arguments: startp ƒf<C692>[ƒ^ÌŠJŽnˆÊuðŽ¦·ƒ|ƒCƒ“ƒ^
// nextp ŒŸ<C592>õðŠJŽn·éƒf<C692>[ƒ^‚̃|ƒCƒ“ƒ^
// remainSize Žcèƒf<C692>[ƒ^ƒTƒCƒY
// offset ˆêvµ½ƒIƒtƒZƒbƒgðŠi”[‚·‚é—̈æ‚ւ̃|ƒCƒ“ƒ^
// Return : ˆêv—ñªŒ©Â©Á½<E2809A>ê<EFBFBD>Í TRUE
// Œ©Â©çÈ©Á½<E2809A>ê<EFBFBD>Í FALSE
//--------------------------------------------------------
static u8 SearchLZ(const u8 *nextp, u32 remainSize, u16 *offset)
{
const u8 *searchp;
const u8 *headp, *searchHeadp;
u16 maxOffset;
u8 maxLength = 2;
u8 tmpLength;
s32 w_offset;
if (remainSize < 3)
{
return 0;
}
w_offset = LZByteTable[*nextp];
while (w_offset != -1)
{
if (w_offset < windowPos)
{
searchp = nextp - windowPos + w_offset;
}
else
{
searchp = nextp - windowLen - windowPos + w_offset;
}
/* ³­Äà—Ç¢ª<E2809A>AÍ©É<E2809A>¬‰»·é */
if (*(searchp + 1) != *(nextp + 1) || *(searchp + 2) != *(nextp + 2))
{
w_offset = LZOffsetTable[w_offset];
continue;
}
if (nextp - searchp < 2)
{
// VRAMÍ2ƒoƒCƒgƒAƒNƒZƒXÈÌÅ (VRAM©çƒf<C692>[ƒ^ð“ÇÝ<E2809A>o·<E2809A>ê<EFBFBD>ª é½ß)<29>A
// ŒŸ<C592>õÎ<E28098>Ûƒf<C692>[ƒ^Í2ƒoƒCƒgO©ç̃f<C692>[ƒ^ɵȯêÎÈçÈ¢<E2809A>B
//
// ƒIƒtƒZƒbƒgÍ12ƒrƒbƒgÅŠi”[³êé½ß<E2809A>A4096ˆÈ‰º
break;
}
tmpLength = 3;
searchHeadp = searchp + 3;
headp = nextp + 3;
while (((u32)(headp - nextp) < remainSize) && (*headp == *searchHeadp))
{
headp++;
searchHeadp++;
tmpLength++;
// ƒf<C692>[ƒ^·Í4ƒrƒbƒgÅŠi”[³êé½ß<E2809A>A18ˆÈ‰º (3̉ºÊðÍ©¹é)
if (tmpLength == (0xF + 3))
{
break;
}
}
if (tmpLength > maxLength)
{
// <20>Åå·ƒIƒtƒZƒbƒgð<E2809A>X<EFBFBD>V
maxLength = tmpLength;
maxOffset = (u16)(nextp - searchp);
if (maxLength == (0xF + 3))
{
// ˆêv·ª<E2809A>ÅåÈÌÅ<E2809A>AŒŸ<C592>õð<E2809A>I—¹·é<E2809A>B
break;
}
}
w_offset = LZOffsetTable[w_offset];
}
if (maxLength < 3)
{
return 0;
}
*offset = maxOffset;
return maxLength;
}