Add rayman2 source files
This commit is contained in:
375
Rayman_X/cpa/tempgrp/CMP/PC/CMP_DIFF.c
Normal file
375
Rayman_X/cpa/tempgrp/CMP/PC/CMP_DIFF.c
Normal file
@@ -0,0 +1,375 @@
|
||||
#include "CMP_Pub.h"
|
||||
|
||||
#if defined(D_CMP_DIFF)
|
||||
|
||||
#include <stdio.h>
|
||||
#include "CMP_CPA.h"
|
||||
#include "ErmCmp.h"
|
||||
#include "CMP_File.h"
|
||||
|
||||
/**************************************************************************/
|
||||
#define D_CMP_DIFF_CODED
|
||||
/**************************************************************************/
|
||||
#define C_CMP_DIFF_BLOCK_FACTOR 1
|
||||
#define C_CMP_DIFF_BLOCK_SIZE (C_CMP_DIFF_BLOCK_FACTOR<<3)
|
||||
#define C_CMP_DIFF_NB_BLOCKS 256
|
||||
/**************************************************************************/
|
||||
char *CMP_DIFF_g_szCompressionName = "Differential method";
|
||||
char CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS+1][C_CMP_DIFF_BLOCK_SIZE];
|
||||
unsigned char CMP_DIFF_g_ucfinish=0;
|
||||
/**************************************************************************/
|
||||
/*#define D_CMP_DIFF_CODE_DEBUG*/
|
||||
#define C_CMP_DIFF_INIT_CODE 0x53
|
||||
#define C_CMP_DIFF_INIT_CODE1 0xB9
|
||||
#define C_CMP_DIFF_INIT_CODE2 ((unsigned long) C_CMP_DIFF_INIT_CODE * 0x01010101 + 0x01020304)
|
||||
unsigned char CMP_DIFF_g_ucCode=C_CMP_DIFF_INIT_CODE;
|
||||
unsigned char CMP_DIFF_g_ucCheckSum;
|
||||
long CMP_DIFF_g_lNbCodedBytes=0;
|
||||
long CMP_DIFF_g_lNbBytes=0;
|
||||
|
||||
#if defined(D_CMP_DIFF_CODED)
|
||||
/**************************************************************************/
|
||||
unsigned char CMP_DIFF_fn_ucSwapCode(unsigned char code)
|
||||
{
|
||||
unsigned char i,newcode=0;
|
||||
|
||||
for (i=0;i<8;i++)
|
||||
{
|
||||
newcode |= (((code >> i) & 0x1) <<(7-i));
|
||||
}
|
||||
return newcode;
|
||||
}
|
||||
/**************************************************************************/
|
||||
unsigned char CMP_DIFF_fn_ucGetCheckSum(FILE *input)
|
||||
{
|
||||
unsigned char chksum;
|
||||
fread(&chksum,sizeof(chksum),1,input);
|
||||
chksum ^= C_CMP_DIFF_INIT_CODE;
|
||||
#ifdef D_CMP_DIFF_CODE_DEBUG
|
||||
printf("get checksum = %d\n",(int) chksum);
|
||||
#endif
|
||||
return chksum;
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_DIFF_fn_vSetCheckSum(unsigned char chksum,FILE *output)
|
||||
{
|
||||
fseek(output,0L,SEEK_SET);
|
||||
#ifdef D_CMP_DIFF_CODE_DEBUG
|
||||
printf("set checksum = %d\n",(int) chksum);
|
||||
#endif
|
||||
chksum ^= C_CMP_DIFF_INIT_CODE;
|
||||
fwrite(&chksum,sizeof(chksum),1,output);
|
||||
}
|
||||
/**************************************************************************/
|
||||
long CMP_DIFF_fn_lGetSize(FILE *input)
|
||||
{
|
||||
long size;
|
||||
fread(&size,sizeof(size),1,input);
|
||||
size ^= C_CMP_DIFF_INIT_CODE2;
|
||||
#ifdef D_CMP_DIFF_CODE_DEBUG
|
||||
printf("get size = %ld\n",(long) size);
|
||||
#endif
|
||||
return size;
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_DIFF_fn_vSetSize(long size,FILE *output)
|
||||
{
|
||||
#ifdef D_CMP_DIFF_CODE_DEBUG
|
||||
printf("set size = %ld\n",(long) size);
|
||||
#endif
|
||||
size ^= C_CMP_DIFF_INIT_CODE2;
|
||||
fwrite(&size,sizeof(size),1,output);
|
||||
}
|
||||
/**************************************************************************/
|
||||
long CMP_DIFF_fn_lUncodedGetc(FILE *input)
|
||||
{
|
||||
long dummy;
|
||||
|
||||
if ((dummy=getc(input))!=EOF) CMP_DIFF_g_lNbBytes++;
|
||||
return dummy;
|
||||
}
|
||||
/**************************************************************************/
|
||||
long CMP_DIFF_fn_lCodedPutc(int c,FILE *output)
|
||||
{
|
||||
long dummy;
|
||||
|
||||
dummy = c ^ ((int) ( CMP_DIFF_fn_ucSwapCode(CMP_DIFF_g_ucCode++) ^ C_CMP_DIFF_INIT_CODE1 ));
|
||||
CMP_DIFF_g_ucCheckSum = (unsigned char)(CMP_DIFF_g_ucCheckSum + (unsigned char)c);
|
||||
CMP_DIFF_g_lNbCodedBytes++;
|
||||
dummy = putc(dummy,output);
|
||||
|
||||
#ifdef D_CMP_DIFF_CODE_DEBUG
|
||||
printf("line %04d org %03d coded written %03d checksum %03d\n",(int) CMP_DIFF_g_lNbCodedBytes,(int) c,(int) dummy,(int) CMP_DIFF_g_ucCheckSum);
|
||||
#endif
|
||||
|
||||
return dummy;
|
||||
}
|
||||
/**************************************************************************/
|
||||
long CMP_DIFF_fn_lCodedGetc(FILE *input)
|
||||
{
|
||||
int dummy;
|
||||
int c;
|
||||
|
||||
if ((dummy=c=getc(input))!=EOF)
|
||||
{
|
||||
dummy = c ^ ((int) ( CMP_DIFF_fn_ucSwapCode(CMP_DIFF_g_ucCode++) ^ C_CMP_DIFF_INIT_CODE1 ));
|
||||
CMP_DIFF_g_ucCheckSum = (unsigned char)(CMP_DIFF_g_ucCheckSum - (unsigned char)dummy);
|
||||
CMP_DIFF_g_lNbCodedBytes++;
|
||||
|
||||
#ifdef D_CMP_DIFF_CODE_DEBUG
|
||||
printf("line %04d read %03d decoded fin %03d checksum %03d\n",(int) CMP_DIFF_g_lNbCodedBytes,(int) c,(int) dummy,(int) CMP_DIFF_g_ucCheckSum);
|
||||
#endif
|
||||
}
|
||||
|
||||
return dummy;
|
||||
}
|
||||
/**************************************************************************/
|
||||
long CMP_DIFF_fn_lUncodedPutc(int c,FILE *output)
|
||||
{
|
||||
CMP_DIFF_g_lNbBytes--;
|
||||
if (CMP_DIFF_g_lNbBytes<0) CMP_DIFF_g_ucfinish=1;
|
||||
return putc(c,output);
|
||||
}
|
||||
/**************************************************************************/
|
||||
#else /* D_CMP_DIFF_CODED */
|
||||
/**************************************************************************/
|
||||
long CMP_DIFF_fn_lUncodedGetc(FILE *input)
|
||||
{
|
||||
return getc(input);
|
||||
}
|
||||
/**************************************************************************/
|
||||
long CMP_DIFF_fn_lUncodedPutc(int c,FILE *output)
|
||||
{
|
||||
return putc(c,output);
|
||||
}
|
||||
/**************************************************************************/
|
||||
long CMP_DIFF_fn_lCodedGetc(FILE *input)
|
||||
{
|
||||
return getc(input);
|
||||
}
|
||||
/**************************************************************************/
|
||||
long CMP_DIFF_fn_lCodedPutc(int c,FILE *output)
|
||||
{
|
||||
return putc(c,output);
|
||||
}
|
||||
/**************************************************************************/
|
||||
#endif /* D_CMP_DIFF_CODED */
|
||||
/**************************************************************************/
|
||||
void CMP_DIFF_fn_vInitBuffer()
|
||||
{
|
||||
int i,j;
|
||||
|
||||
for(i=0;i<C_CMP_DIFF_NB_BLOCKS;i++)
|
||||
{
|
||||
for (j=0;j<C_CMP_DIFF_BLOCK_SIZE;j++)
|
||||
{
|
||||
CMP_DIFF_g_aa_cBuffer[i][j]=(char)i;
|
||||
}
|
||||
}
|
||||
|
||||
CMP_DIFF_g_lNbCodedBytes=CMP_DIFF_g_lNbBytes=0;
|
||||
CMP_DIFF_g_ucfinish=0;
|
||||
CMP_DIFF_g_ucCode=C_CMP_DIFF_INIT_CODE + 4;
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_DIFF_fn_vCompressFile( FILE *input, FILE *output )
|
||||
{
|
||||
int i,j,currentsize;
|
||||
int bestblock = 0;
|
||||
int bestcount,matchcount;
|
||||
int changeindex = 0,bitvalue;
|
||||
|
||||
CMP_DIFF_fn_vInitBuffer();
|
||||
|
||||
#if defined(D_CMP_DIFF_CODED)
|
||||
CMP_DIFF_fn_vSetCheckSum(CMP_DIFF_g_ucCheckSum=0,output);
|
||||
CMP_DIFF_fn_vSetSize(CMP_DIFF_g_lNbBytes=0,output);
|
||||
#endif
|
||||
|
||||
while(!CMP_DIFF_g_ucfinish)
|
||||
{
|
||||
currentsize=C_CMP_DIFF_BLOCK_SIZE;
|
||||
for(j=0;j<C_CMP_DIFF_BLOCK_SIZE;j++)
|
||||
{
|
||||
i=CMP_DIFF_fn_lUncodedGetc(input);
|
||||
if (i==EOF)
|
||||
{
|
||||
currentsize=j;
|
||||
break;
|
||||
}
|
||||
CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS][j]=(char)i;
|
||||
}
|
||||
|
||||
if (currentsize==0)
|
||||
{
|
||||
CMP_DIFF_g_ucfinish=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
bestcount=-1;
|
||||
for(i=0;i<C_CMP_DIFF_NB_BLOCKS;i++)
|
||||
{
|
||||
matchcount=0;
|
||||
for(j=0;j<currentsize;j++)
|
||||
{
|
||||
if (CMP_DIFF_g_aa_cBuffer[i][j]==CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS][j])
|
||||
{
|
||||
matchcount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (matchcount>bestcount)
|
||||
{
|
||||
bestcount=matchcount;
|
||||
bestblock=i;
|
||||
if (bestcount==currentsize)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CMP_DIFF_fn_lCodedPutc(bestblock,output);
|
||||
|
||||
for (i=0;i<C_CMP_DIFF_BLOCK_FACTOR;i++)
|
||||
{
|
||||
changeindex=0;
|
||||
bitvalue=1;
|
||||
for (j=i*8;j<i*8+8;j++)
|
||||
{
|
||||
if (j>=currentsize)
|
||||
break;
|
||||
|
||||
if (CMP_DIFF_g_aa_cBuffer[bestblock][j] != CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS][j])
|
||||
{
|
||||
changeindex += bitvalue;
|
||||
}
|
||||
bitvalue *= 2;
|
||||
}
|
||||
|
||||
CMP_DIFF_fn_lCodedPutc(changeindex,output);
|
||||
|
||||
for (j=i*8;j<i*8+8;j++)
|
||||
{
|
||||
if (changeindex % 2 == 1)
|
||||
{
|
||||
CMP_DIFF_fn_lCodedPutc(CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS][j],output);
|
||||
}
|
||||
changeindex /= 2;
|
||||
}
|
||||
|
||||
if (currentsize<C_CMP_DIFF_BLOCK_SIZE)
|
||||
{
|
||||
CMP_DIFF_fn_lCodedPutc(currentsize,output);
|
||||
CMP_DIFF_g_ucfinish=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(j=0;j<C_CMP_DIFF_BLOCK_SIZE;j++)
|
||||
{
|
||||
CMP_DIFF_g_aa_cBuffer[bestblock][j]=CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef D_CMP_DIFF_CODED
|
||||
CMP_DIFF_fn_vSetCheckSum(CMP_DIFF_g_ucCheckSum,output);
|
||||
CMP_DIFF_fn_vSetSize(CMP_DIFF_g_lNbBytes,output);
|
||||
#endif
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_DIFF_fn_vExpandFile( FILE *input, FILE *output )
|
||||
{
|
||||
int i,j;
|
||||
int bestblock=-1;
|
||||
int changeindex = 0;
|
||||
|
||||
CMP_DIFF_fn_vInitBuffer();
|
||||
|
||||
#ifdef D_CMP_DIFF_CODED
|
||||
CMP_DIFF_g_ucCheckSum=CMP_DIFF_fn_ucGetCheckSum(input);
|
||||
CMP_DIFF_g_lNbBytes=CMP_DIFF_fn_lGetSize(input);
|
||||
#endif
|
||||
|
||||
while (!CMP_DIFF_g_ucfinish)
|
||||
{
|
||||
if (bestblock==-1)
|
||||
{
|
||||
bestblock=CMP_DIFF_fn_lCodedGetc(input);
|
||||
if (bestblock==EOF)
|
||||
{
|
||||
CMP_DIFF_g_ucfinish=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
changeindex=CMP_DIFF_fn_lCodedGetc(input);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bestblock=CMP_DIFF_fn_lCodedGetc(input);
|
||||
if (bestblock==EOF)
|
||||
{
|
||||
for(j=0;j<C_CMP_DIFF_BLOCK_SIZE;j++)
|
||||
{
|
||||
CMP_DIFF_fn_lUncodedPutc(CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS][j],output);
|
||||
}
|
||||
CMP_DIFF_g_ucfinish=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
changeindex=CMP_DIFF_fn_lCodedGetc(input);
|
||||
if (changeindex==EOF)
|
||||
{
|
||||
for(j=0;j<bestblock;j++)
|
||||
{
|
||||
CMP_DIFF_fn_lUncodedPutc(CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS][j],output);
|
||||
}
|
||||
CMP_DIFF_g_ucfinish=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(j=0;j<C_CMP_DIFF_BLOCK_SIZE;j++)
|
||||
{
|
||||
CMP_DIFF_fn_lUncodedPutc(CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS][j],output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!CMP_DIFF_g_ucfinish)
|
||||
{
|
||||
for (i=0;i<C_CMP_DIFF_BLOCK_FACTOR;i++)
|
||||
{
|
||||
if (i>0)
|
||||
changeindex=CMP_DIFF_fn_lCodedGetc(input);
|
||||
for(j=i*8;j<i*8+8;j++)
|
||||
{
|
||||
if (changeindex % 2 == 1)
|
||||
{
|
||||
CMP_DIFF_g_aa_cBuffer[bestblock][j]=(char)CMP_DIFF_fn_lCodedGetc(input);
|
||||
}
|
||||
|
||||
CMP_DIFF_g_aa_cBuffer[C_CMP_DIFF_NB_BLOCKS][j]=CMP_DIFF_g_aa_cBuffer[bestblock][j];
|
||||
changeindex /= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef D_CMP_DIFF_CODED
|
||||
if (CMP_DIFF_g_lNbBytes==0)
|
||||
{
|
||||
if (CMP_DIFF_g_ucCheckSum!=0)
|
||||
M_CMPFatalError(E_uwCMPCheckSum);
|
||||
}
|
||||
else
|
||||
{
|
||||
M_CMPFatalError(E_uwCMPFileCorrupted);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
/**************************************************************************/
|
||||
|
||||
#endif /* D_CMP_DIFF */
|
7
Rayman_X/cpa/tempgrp/CMP/PC/CMP_DIFF.h
Normal file
7
Rayman_X/cpa/tempgrp/CMP/PC/CMP_DIFF.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#if!defined(__CMP_DIFF_H__)
|
||||
#define __CMP_DIFF_H__
|
||||
|
||||
void CMP_DIFF_fn_vCompressFile( FILE *input , FILE *output );
|
||||
void CMP_DIFF_fn_vExpandFile( FILE *input , FILE *output );
|
||||
|
||||
#endif /* __CMP_DIFF_H__ */
|
156
Rayman_X/cpa/tempgrp/CMP/PC/CMP_File.c
Normal file
156
Rayman_X/cpa/tempgrp/CMP/PC/CMP_File.c
Normal file
@@ -0,0 +1,156 @@
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include "CMP_CPA.h"
|
||||
#include "ErmCmp.h"
|
||||
#include "CMP_File.h"
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
/*#define PACIFIER*/
|
||||
#define PACIFIER_COUNT 2047
|
||||
|
||||
/**************************************************************************/
|
||||
tdstBitFile *CMP_fn_p_stOpenOutputBitFile( char *name )
|
||||
{
|
||||
tdstBitFile *bit_file=NULL;
|
||||
FILE *file=NULL;
|
||||
|
||||
if ((file = fopen( name, "wb" ))!=NULL)
|
||||
{
|
||||
bit_file = (tdstBitFile *) calloc( 1, sizeof( tdstBitFile ) );
|
||||
if ( bit_file == NULL )
|
||||
return( bit_file );
|
||||
bit_file->file = file;
|
||||
bit_file->rack = 0;
|
||||
bit_file->mask = 0x80;
|
||||
bit_file->pacifier_counter = 0;
|
||||
}
|
||||
return( bit_file );
|
||||
}
|
||||
/**************************************************************************/
|
||||
tdstBitFile *CMP_fn_p_stOpenInputBitFile( char *name )
|
||||
{
|
||||
tdstBitFile *bit_file=NULL;
|
||||
FILE *file=NULL;
|
||||
|
||||
if ((file = fopen( name, "rb" ))!=NULL)
|
||||
{
|
||||
bit_file = (tdstBitFile *) calloc( 1, sizeof( tdstBitFile ) );
|
||||
if ( bit_file == NULL )
|
||||
return( bit_file );
|
||||
bit_file->file = file;
|
||||
bit_file->rack = 0;
|
||||
bit_file->mask = 0x80;
|
||||
bit_file->pacifier_counter = 0;
|
||||
}
|
||||
return( bit_file );
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_fn_vCloseOutputBitFile( tdstBitFile *bit_file )
|
||||
{
|
||||
if ( bit_file->mask != 0x80 )
|
||||
if ( putc( bit_file->rack, bit_file->file ) != bit_file->rack )
|
||||
M_CMPFatalError(E_uwCMPCloseFileError);
|
||||
fclose( bit_file->file );
|
||||
free( (char *) bit_file );
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_fn_vCloseInputBitFile( tdstBitFile *bit_file )
|
||||
{
|
||||
fclose( bit_file->file );
|
||||
free( (char *) bit_file );
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_fn_vOutputBit( tdstBitFile *bit_file , int bit )
|
||||
{
|
||||
if ( bit )
|
||||
bit_file->rack |= bit_file->mask;
|
||||
bit_file->mask >>= 1;
|
||||
if ( bit_file->mask == 0 )
|
||||
{
|
||||
if ( putc( bit_file->rack, bit_file->file ) != bit_file->rack )
|
||||
M_CMPFatalError(E_uwCMPOutputBitError);
|
||||
#ifdef PACIFIER
|
||||
else if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
|
||||
putc( '.', stdout );
|
||||
#endif /* PACIFIER */
|
||||
bit_file->rack = 0;
|
||||
bit_file->mask = 0x80;
|
||||
}
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_fn_vOutputBits( tdstBitFile *bit_file , unsigned long code , int count )
|
||||
{
|
||||
unsigned long mask;
|
||||
|
||||
mask = 1L << ( count - 1 );
|
||||
while ( mask != 0)
|
||||
{
|
||||
if ( mask & code )
|
||||
bit_file->rack |= bit_file->mask;
|
||||
bit_file->mask >>= 1;
|
||||
if ( bit_file->mask == 0 )
|
||||
{
|
||||
if ( putc( bit_file->rack, bit_file->file ) != bit_file->rack )
|
||||
M_CMPFatalError(E_uwCMPOutputBitsError);
|
||||
#ifdef PACIFIER
|
||||
else if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
|
||||
putc( '.', stdout );
|
||||
#endif
|
||||
bit_file->rack = 0;
|
||||
bit_file->mask = 0x80;
|
||||
}
|
||||
mask >>= 1;
|
||||
}
|
||||
}
|
||||
/**************************************************************************/
|
||||
long CMP_fn_lInputBit( tdstBitFile *bit_file )
|
||||
{
|
||||
long value;
|
||||
|
||||
if ( bit_file->mask == 0x80 )
|
||||
{
|
||||
bit_file->rack = getc( bit_file->file );
|
||||
if ( bit_file->rack == EOF )
|
||||
M_CMPFatalError(E_uwCMPInputBitError);
|
||||
#ifdef PACIFIER
|
||||
if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
|
||||
putc( '.', stdout );
|
||||
#endif
|
||||
}
|
||||
value = bit_file->rack & bit_file->mask;
|
||||
bit_file->mask >>= 1;
|
||||
if ( bit_file->mask == 0 )
|
||||
bit_file->mask = 0x80;
|
||||
return( value ? 1 : 0 );
|
||||
}
|
||||
/**************************************************************************/
|
||||
unsigned long CMP_fn_ulInputBits( tdstBitFile *bit_file , int bit_count )
|
||||
{
|
||||
unsigned long mask;
|
||||
unsigned long return_value;
|
||||
|
||||
mask = 1L << ( bit_count - 1 );
|
||||
return_value = 0;
|
||||
while ( mask != 0)
|
||||
{
|
||||
if ( bit_file->mask == 0x80 )
|
||||
{
|
||||
bit_file->rack = getc( bit_file->file );
|
||||
if ( bit_file->rack == EOF )
|
||||
M_CMPFatalError(E_uwCMPInputBitsError);
|
||||
#ifdef PACIFIER
|
||||
if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
|
||||
putc( '.', stdout );
|
||||
#endif
|
||||
}
|
||||
if ( bit_file->rack & bit_file->mask )
|
||||
return_value |= mask;
|
||||
mask >>= 1;
|
||||
bit_file->mask >>= 1;
|
||||
if ( bit_file->mask == 0 ) bit_file->mask = 0x80;
|
||||
}
|
||||
return( return_value );
|
||||
}
|
||||
/**************************************************************************/
|
26
Rayman_X/cpa/tempgrp/CMP/PC/CMP_File.h
Normal file
26
Rayman_X/cpa/tempgrp/CMP/PC/CMP_File.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#if !defined(__CMP_File_h__)
|
||||
#define __CMP_File_h__
|
||||
|
||||
#define M_p_stOpenInput(x) fopen((x),"rb")
|
||||
#define M_p_stOpenOutput(x) fopen((x),"wb")
|
||||
#define M_vCloseInput(x) fclose((x))
|
||||
#define M_vCloseOutput(x) fclose((x))
|
||||
|
||||
typedef struct tdstBitFile_
|
||||
{
|
||||
FILE *file;
|
||||
unsigned char mask;
|
||||
int rack;
|
||||
int pacifier_counter;
|
||||
} tdstBitFile;
|
||||
|
||||
tdstBitFile *CMP_fn_p_stOpenInputBitFile( char *name );
|
||||
tdstBitFile *CMP_fn_p_stOpenOutputBitFile( char *name );
|
||||
void CMP_fn_vCloseInputBitFile( tdstBitFile *bit_file );
|
||||
void CMP_fn_vCloseOutputBitFile( tdstBitFile *bit_file );
|
||||
void CMP_fn_vOutputBit( tdstBitFile *bit_file , int bit );
|
||||
void CMP_fn_vOutputBits( tdstBitFile *bit_file , unsigned long code , int count );
|
||||
long CMP_fn_lInputBit( tdstBitFile *bit_file );
|
||||
unsigned long CMP_fn_ulInputBits( tdstBitFile *bit_file , int bit_count );
|
||||
|
||||
#endif /* __CMP_File_h__ */
|
250
Rayman_X/cpa/tempgrp/CMP/PC/CMP_LZSS.c
Normal file
250
Rayman_X/cpa/tempgrp/CMP/PC/CMP_LZSS.c
Normal file
@@ -0,0 +1,250 @@
|
||||
#include "CMP_Pub.h"
|
||||
|
||||
#if defined(D_CMP_LZSS)
|
||||
|
||||
#include <stdio.h>
|
||||
#include "CMP_CPA.h"
|
||||
#include "ErmCmp.h"
|
||||
#include "CMP_File.h"
|
||||
|
||||
/**************************************************************************/
|
||||
#define C_CMP_LZSS_INDEX_BIT_COUNT 12
|
||||
#define C_CMP_LZSS_LENGTH_BIT_COUNT 4
|
||||
#define C_CMP_LZSS_WINDOW_SIZE ( 1 << C_CMP_LZSS_INDEX_BIT_COUNT )
|
||||
#define C_CMP_LZSS_RAW_LOOK_AHEAD_SIZE ( 1 << C_CMP_LZSS_LENGTH_BIT_COUNT )
|
||||
#define C_CMP_LZSS_BREAK_EVEN ( ( 1 + C_CMP_LZSS_INDEX_BIT_COUNT + C_CMP_LZSS_LENGTH_BIT_COUNT ) / 9 )
|
||||
#define C_CMP_LZSS_LOOK_AHEAD_SIZE ( C_CMP_LZSS_RAW_LOOK_AHEAD_SIZE + C_CMP_LZSS_BREAK_EVEN )
|
||||
#define C_CMP_LZSS_TREE_ROOT C_CMP_LZSS_WINDOW_SIZE
|
||||
#define C_CMP_LZSS_END_OF_STREAM 0
|
||||
#define C_CMP_LZSS_UNUSED 0
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
#define M_CMP_LZSS_MOD_WINDOW( a ) ( ( a ) & ( C_CMP_LZSS_WINDOW_SIZE - 1 ) )
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
unsigned char CMP_LZSS_g_a_ucWindow[C_CMP_LZSS_WINDOW_SIZE];
|
||||
|
||||
struct
|
||||
{
|
||||
int parent;
|
||||
int smaller_child;
|
||||
int larger_child;
|
||||
} CMP_LZSS_g_a_stTree[C_CMP_LZSS_WINDOW_SIZE + 1 ];
|
||||
|
||||
char *CMP_LZSS_CompressionName = "LZSS method";
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
void CMP_LZSS_InitTree( int r );
|
||||
void CMP_LZSS_ContractNode( int old_node, int new_node );
|
||||
void CMP_LZSS_ReplaceNode( int old_node, int new_node );
|
||||
int CMP_LZSS_FindNextNode( int node );
|
||||
void CMP_LZSS_DeleteString( int p );
|
||||
int CMP_LZSS_AddString( int new_node, int *match_position );
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
void CMP_LZSS_fn_vInitTree( int r )
|
||||
{
|
||||
CMP_LZSS_g_a_stTree[ C_CMP_LZSS_TREE_ROOT ].larger_child = r;
|
||||
CMP_LZSS_g_a_stTree[ r ].parent = C_CMP_LZSS_TREE_ROOT;
|
||||
CMP_LZSS_g_a_stTree[ r ].larger_child = C_CMP_LZSS_UNUSED;
|
||||
CMP_LZSS_g_a_stTree[ r ].smaller_child = C_CMP_LZSS_UNUSED;
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_LZSS_fn_vContractNode( int old_node , int new_node )
|
||||
{
|
||||
CMP_LZSS_g_a_stTree[ new_node ].parent = CMP_LZSS_g_a_stTree[ old_node ].parent;
|
||||
if ( CMP_LZSS_g_a_stTree[ CMP_LZSS_g_a_stTree[ old_node ].parent ].larger_child == old_node )
|
||||
CMP_LZSS_g_a_stTree[ CMP_LZSS_g_a_stTree[ old_node ].parent ].larger_child = new_node;
|
||||
else
|
||||
CMP_LZSS_g_a_stTree[ CMP_LZSS_g_a_stTree[ old_node ].parent ].smaller_child = new_node;
|
||||
CMP_LZSS_g_a_stTree[ old_node ].parent = C_CMP_LZSS_UNUSED;
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_LZSS_fn_vReplaceNode( int old_node , int new_node )
|
||||
{
|
||||
int parent;
|
||||
|
||||
parent = CMP_LZSS_g_a_stTree[ old_node ].parent;
|
||||
if ( CMP_LZSS_g_a_stTree[ parent ].smaller_child == old_node )
|
||||
CMP_LZSS_g_a_stTree[ parent ].smaller_child = new_node;
|
||||
else
|
||||
CMP_LZSS_g_a_stTree[ parent ].larger_child = new_node;
|
||||
CMP_LZSS_g_a_stTree[ new_node ] = CMP_LZSS_g_a_stTree[ old_node ];
|
||||
CMP_LZSS_g_a_stTree[ CMP_LZSS_g_a_stTree[ new_node ].smaller_child ].parent = new_node;
|
||||
CMP_LZSS_g_a_stTree[ CMP_LZSS_g_a_stTree[ new_node ].larger_child ].parent = new_node;
|
||||
CMP_LZSS_g_a_stTree[ old_node ].parent = C_CMP_LZSS_UNUSED;
|
||||
}
|
||||
/**************************************************************************/
|
||||
int CMP_LZSS_fn_vFindNextNode( int node )
|
||||
{
|
||||
int next;
|
||||
|
||||
next = CMP_LZSS_g_a_stTree[ node ].smaller_child;
|
||||
while ( CMP_LZSS_g_a_stTree[ next ].larger_child != C_CMP_LZSS_UNUSED )
|
||||
next = CMP_LZSS_g_a_stTree[ next ].larger_child;
|
||||
return( next );
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_LZSS_fn_vDeleteString( int p )
|
||||
{
|
||||
int replacement;
|
||||
|
||||
if ( CMP_LZSS_g_a_stTree[ p ].parent == C_CMP_LZSS_UNUSED )
|
||||
return;
|
||||
if ( CMP_LZSS_g_a_stTree[ p ].larger_child == C_CMP_LZSS_UNUSED )
|
||||
CMP_LZSS_fn_vContractNode( p, CMP_LZSS_g_a_stTree[ p ].smaller_child );
|
||||
else if ( CMP_LZSS_g_a_stTree[ p ].smaller_child == C_CMP_LZSS_UNUSED )
|
||||
CMP_LZSS_fn_vContractNode( p, CMP_LZSS_g_a_stTree[ p ].larger_child );
|
||||
else
|
||||
{
|
||||
replacement = CMP_LZSS_fn_vFindNextNode( p );
|
||||
CMP_LZSS_fn_vDeleteString( replacement );
|
||||
CMP_LZSS_fn_vReplaceNode( p, replacement );
|
||||
}
|
||||
}
|
||||
/**************************************************************************/
|
||||
int CMP_LZSS_fn_vAddString( int new_node , int *match_position )
|
||||
{
|
||||
int i;
|
||||
int test_node;
|
||||
int delta = 0;
|
||||
int match_length;
|
||||
int *child;
|
||||
|
||||
if ( new_node == C_CMP_LZSS_END_OF_STREAM )
|
||||
return( 0 );
|
||||
test_node = CMP_LZSS_g_a_stTree[ C_CMP_LZSS_TREE_ROOT ].larger_child;
|
||||
match_length = 0;
|
||||
for ( ; ; )
|
||||
{
|
||||
for ( i = 0 ; i < C_CMP_LZSS_LOOK_AHEAD_SIZE ; i++ )
|
||||
{
|
||||
delta = CMP_LZSS_g_a_ucWindow[ M_CMP_LZSS_MOD_WINDOW( new_node + i ) ] - CMP_LZSS_g_a_ucWindow[ M_CMP_LZSS_MOD_WINDOW( test_node + i ) ];
|
||||
if ( delta != 0 )
|
||||
break;
|
||||
}
|
||||
if ( i >= match_length )
|
||||
{
|
||||
match_length = i;
|
||||
*match_position = test_node;
|
||||
if ( match_length >= C_CMP_LZSS_LOOK_AHEAD_SIZE )
|
||||
{
|
||||
CMP_LZSS_fn_vReplaceNode( test_node, new_node );
|
||||
return( match_length );
|
||||
}
|
||||
}
|
||||
if ( delta >= 0 )
|
||||
child = &CMP_LZSS_g_a_stTree[ test_node ].larger_child;
|
||||
else
|
||||
child = &CMP_LZSS_g_a_stTree[ test_node ].smaller_child;
|
||||
if ( *child == C_CMP_LZSS_UNUSED )
|
||||
{
|
||||
*child = new_node;
|
||||
CMP_LZSS_g_a_stTree[ new_node ].parent = test_node;
|
||||
CMP_LZSS_g_a_stTree[ new_node ].larger_child = C_CMP_LZSS_UNUSED;
|
||||
CMP_LZSS_g_a_stTree[ new_node ].smaller_child = C_CMP_LZSS_UNUSED;
|
||||
return( match_length );
|
||||
}
|
||||
test_node = *child;
|
||||
}
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_LZSS_fn_vCompressFile( FILE *input , tdstBitFile *output )
|
||||
{
|
||||
int i;
|
||||
int c;
|
||||
int look_ahead_bytes;
|
||||
int current_position;
|
||||
int replace_count;
|
||||
int match_length;
|
||||
int match_position;
|
||||
|
||||
current_position = 1;
|
||||
for ( i = 0 ; i < C_CMP_LZSS_LOOK_AHEAD_SIZE ; i++ )
|
||||
{
|
||||
if ( ( c = getc( input ) ) == EOF )
|
||||
break;
|
||||
CMP_LZSS_g_a_ucWindow[ current_position + i ] = (unsigned char) c;
|
||||
}
|
||||
look_ahead_bytes = i;
|
||||
CMP_LZSS_fn_vInitTree( current_position );
|
||||
match_length = 0;
|
||||
match_position = 0;
|
||||
while ( look_ahead_bytes > 0 )
|
||||
{
|
||||
if ( match_length > look_ahead_bytes )
|
||||
match_length = look_ahead_bytes;
|
||||
if ( match_length <= C_CMP_LZSS_BREAK_EVEN )
|
||||
{
|
||||
replace_count = 1;
|
||||
CMP_fn_vOutputBit( output, 1 );
|
||||
CMP_fn_vOutputBits( output,
|
||||
(unsigned long) CMP_LZSS_g_a_ucWindow[ current_position ], 8 );
|
||||
}
|
||||
else
|
||||
{
|
||||
CMP_fn_vOutputBit( output, 0 );
|
||||
CMP_fn_vOutputBits( output,
|
||||
(unsigned long) match_position, C_CMP_LZSS_INDEX_BIT_COUNT );
|
||||
CMP_fn_vOutputBits( output,
|
||||
(unsigned long) ( match_length - ( C_CMP_LZSS_BREAK_EVEN + 1 ) ),
|
||||
C_CMP_LZSS_LENGTH_BIT_COUNT );
|
||||
replace_count = match_length;
|
||||
}
|
||||
for ( i = 0 ; i < replace_count ; i++ )
|
||||
{
|
||||
CMP_LZSS_fn_vDeleteString( M_CMP_LZSS_MOD_WINDOW( current_position + C_CMP_LZSS_LOOK_AHEAD_SIZE ) );
|
||||
if ( ( c = getc( input ) ) == EOF )
|
||||
look_ahead_bytes--;
|
||||
else
|
||||
CMP_LZSS_g_a_ucWindow[ M_CMP_LZSS_MOD_WINDOW( current_position + C_CMP_LZSS_LOOK_AHEAD_SIZE ) ] = (unsigned char) c;
|
||||
current_position = M_CMP_LZSS_MOD_WINDOW( current_position + 1 );
|
||||
if ( look_ahead_bytes )
|
||||
match_length = CMP_LZSS_fn_vAddString( current_position, &match_position );
|
||||
}
|
||||
}
|
||||
CMP_fn_vOutputBit( output, 0 );
|
||||
CMP_fn_vOutputBits( output, (unsigned long) C_CMP_LZSS_END_OF_STREAM, C_CMP_LZSS_INDEX_BIT_COUNT );
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_LZSS_fn_vExpandFile( tdstBitFile *input , FILE *output )
|
||||
{
|
||||
int i;
|
||||
int current_position;
|
||||
int c;
|
||||
int match_length;
|
||||
int match_position;
|
||||
|
||||
current_position = 1;
|
||||
for ( ; ; )
|
||||
{
|
||||
if ( CMP_fn_lInputBit( input ) )
|
||||
{
|
||||
c = (int) CMP_fn_ulInputBits( input, 8 );
|
||||
putc( c, output );
|
||||
CMP_LZSS_g_a_ucWindow[ current_position ] = (unsigned char) c;
|
||||
current_position = M_CMP_LZSS_MOD_WINDOW( current_position + 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
match_position = (int) CMP_fn_ulInputBits( input, C_CMP_LZSS_INDEX_BIT_COUNT );
|
||||
if ( match_position == C_CMP_LZSS_END_OF_STREAM )
|
||||
break;
|
||||
match_length = (int) CMP_fn_ulInputBits( input, C_CMP_LZSS_LENGTH_BIT_COUNT );
|
||||
match_length += C_CMP_LZSS_BREAK_EVEN;
|
||||
for ( i = 0 ; i <= match_length ; i++ )
|
||||
{
|
||||
c = CMP_LZSS_g_a_ucWindow[ M_CMP_LZSS_MOD_WINDOW( match_position + i ) ];
|
||||
putc( c, output );
|
||||
CMP_LZSS_g_a_ucWindow[ current_position ] = (unsigned char) c;
|
||||
current_position = M_CMP_LZSS_MOD_WINDOW( current_position + 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**************************************************************************/
|
||||
#endif /* D_CMP_LZSS */
|
8
Rayman_X/cpa/tempgrp/CMP/PC/CMP_LZSS.h
Normal file
8
Rayman_X/cpa/tempgrp/CMP/PC/CMP_LZSS.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#if!defined(__CMP_LZSS_H__)
|
||||
#define __CMP_LZSS_H__
|
||||
|
||||
|
||||
void CMP_LZSS_fn_vCompressFile( FILE *input , tdstBitFile *output );
|
||||
void CMP_LZSS_fn_vExpandFile( tdstBitFile *input , FILE *output );
|
||||
|
||||
#endif /* __CMP_LZSS_H__ */
|
126
Rayman_X/cpa/tempgrp/CMP/PC/CMP_Main.c
Normal file
126
Rayman_X/cpa/tempgrp/CMP/PC/CMP_Main.c
Normal file
@@ -0,0 +1,126 @@
|
||||
#define D_CMP_Input_VariableDefine
|
||||
|
||||
#include "CMP_CPA.h"
|
||||
#include "ErmCmp.h"
|
||||
#include "CMP_File.h"
|
||||
#include "CMP_Pub.h"
|
||||
#include "CMP_DIFF.h"
|
||||
#include "CMP_LZSS.h"
|
||||
|
||||
#define C_TemporaryFile "Temp.tmp"
|
||||
|
||||
/**************************************************************************/
|
||||
void CMP_fn_vFirstInit()
|
||||
{
|
||||
Erm_M_InitErrMsg(CMP);
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_fn_vCompressFile(char *_szFileName,unsigned char _ucCompressMethod)
|
||||
{
|
||||
rename(_szFileName,C_TemporaryFile);
|
||||
CMP_fn_vCompressFileIn(C_TemporaryFile,_szFileName,_ucCompressMethod);
|
||||
DeleteFile(C_TemporaryFile);
|
||||
}
|
||||
/**************************************************************************/
|
||||
void CMP_fn_vCompressFileIn(char *_szInputFileName,char *_szOutputFileName,unsigned char _ucCompressMethod)
|
||||
{
|
||||
#if defined(D_CMP_LZSS)
|
||||
if (_ucCompressMethod==D_CMP_LZSS)
|
||||
{
|
||||
FILE *p_stInputFile = NULL;
|
||||
tdstBitFile *p_stOutputBitFile = NULL;
|
||||
|
||||
p_stInputFile = M_p_stOpenInput(_szInputFileName);
|
||||
if (p_stInputFile!=NULL)
|
||||
{
|
||||
p_stOutputBitFile = CMP_fn_p_stOpenOutputBitFile(_szOutputFileName);
|
||||
if (p_stOutputBitFile!=NULL)
|
||||
{
|
||||
CMP_LZSS_fn_vCompressFile(p_stInputFile,p_stOutputBitFile);
|
||||
CMP_fn_vCloseOutputBitFile(p_stOutputBitFile);
|
||||
}
|
||||
M_vCloseInput(p_stInputFile);
|
||||
}
|
||||
}
|
||||
#endif /* D_CMP_LZSS */
|
||||
|
||||
#if defined(D_CMP_DIFF)
|
||||
if (_ucCompressMethod==D_CMP_DIFF)
|
||||
{
|
||||
FILE *p_stInputFile = NULL;
|
||||
FILE *p_stOutputFile = NULL;
|
||||
|
||||
p_stInputFile = M_p_stOpenInput(_szInputFileName);
|
||||
if (p_stInputFile!=NULL)
|
||||
{
|
||||
p_stOutputFile = M_p_stOpenOutput(_szOutputFileName);
|
||||
|
||||
if (p_stOutputFile!=NULL)
|
||||
{
|
||||
CMP_DIFF_fn_vCompressFile(p_stInputFile,p_stOutputFile);
|
||||
M_vCloseOutput(p_stOutputFile);
|
||||
}
|
||||
M_vCloseInput(p_stInputFile);
|
||||
}
|
||||
}
|
||||
#endif /* D_CMP_DIFF */
|
||||
}
|
||||
/**************************************************************************/
|
||||
unsigned char CMP_fn_bExpandFile(char *_szFileName,unsigned char _ucCompressMethod)
|
||||
{
|
||||
unsigned char ucReturn = TRUE;
|
||||
rename(_szFileName,C_TemporaryFile);
|
||||
ucReturn = CMP_fn_bExpandFileIn(C_TemporaryFile,_szFileName,_ucCompressMethod);
|
||||
DeleteFile(C_TemporaryFile);
|
||||
|
||||
return(ucReturn);
|
||||
}
|
||||
/**************************************************************************/
|
||||
unsigned char CMP_fn_bExpandFileIn(char *_szInputFileName,char *_szOutputFileName,unsigned char _ucCompressMethod)
|
||||
{
|
||||
unsigned char ucReturn = FALSE;
|
||||
|
||||
#if defined(D_CMP_LZSS)
|
||||
if (_ucCompressMethod==D_CMP_LZSS)
|
||||
{
|
||||
tdstBitFile *p_stInputBitFile = NULL;
|
||||
FILE *p_stOutputFile = NULL;
|
||||
|
||||
p_stInputBitFile = CMP_fn_p_stOpenInputBitFile(_szInputFileName);
|
||||
if (p_stInputBitFile!=NULL)
|
||||
{
|
||||
p_stOutputFile = M_p_stOpenOutput(_szOutputFileName);
|
||||
if (p_stOutputFile!=NULL)
|
||||
{
|
||||
CMP_LZSS_fn_vExpandFile(p_stInputBitFile,p_stOutputFile);
|
||||
ucReturn = TRUE;
|
||||
M_vCloseOutput(p_stOutputFile);
|
||||
}
|
||||
CMP_fn_vCloseInputBitFile(p_stInputBitFile);
|
||||
}
|
||||
}
|
||||
#endif /* D_CMP_LZSS */
|
||||
|
||||
#if defined(D_CMP_DIFF)
|
||||
if (_ucCompressMethod==D_CMP_DIFF)
|
||||
{
|
||||
FILE *p_stInputFile = NULL;
|
||||
FILE *p_stOutputFile = NULL;
|
||||
|
||||
p_stInputFile = M_p_stOpenInput(_szInputFileName);
|
||||
if (p_stInputFile!=NULL)
|
||||
{
|
||||
p_stOutputFile = M_p_stOpenOutput(_szOutputFileName);
|
||||
if (p_stOutputFile!=NULL)
|
||||
{
|
||||
CMP_DIFF_fn_vExpandFile(p_stInputFile,p_stOutputFile);
|
||||
ucReturn = TRUE;
|
||||
M_vCloseOutput(p_stOutputFile);
|
||||
}
|
||||
M_vCloseInput(p_stInputFile);
|
||||
}
|
||||
}
|
||||
#endif /* D_CMP_DIFF */
|
||||
return(ucReturn);
|
||||
}
|
||||
/**************************************************************************/
|
Reference in New Issue
Block a user