lzw.c

来自「redboy for gba 是BPNS为GBA编写的一个小软件。软件的邹形是B」· C语言 代码 · 共 97 行

C
97
字号
#include <csagb.h>
#include <rb_mem.h>
#include <csagb.h>
#include <rb_mem.h>
#include <rb_stdio.h>
#define N		 4096
#define F		   18
#define THRESHOLD	2
#define NIL			N
#define EOF (-1)
typedef struct
{
   char *loc;
   u32 fp;
   u32 size;
} mFILE;
void mputc(int ch,mFILE *fh);
int mgetc(mFILE *fh);
void Decode(mFILE *infile, mFILE *outfile,u8 *text_buf)
{
	int  i, j, k, r, c;
	unsigned int  flags;
	for (i = 0; i < N - F; i++) text_buf[i] = ' ';
	r = N - F;  flags = 0;
	for ( ; ; )
	{
		if (((flags >>= 1) & 256) == 0)
		{
         c=infile->loc[infile->fp++];
         //infile->fp++;
			if (infile->fp>infile->size) break;
			flags = c | 0xff00;
		}
		if (flags & 1)
		{
         c=infile->loc[infile->fp++];
         //infile->fp++;
			if (infile->fp>infile->size) break;
			//mputc(c, outfile);
		   outfile->loc[outfile->fp++]=(char)c;
         //fh->fp++;
         text_buf[r++] = c;  r &= (N - 1);
		}
		else
		{
         i=infile->loc[infile->fp++];
         //infile->fp++;
			if (infile->fp>infile->size) break;
			//if ((i = mgetc(infile)) == EOF) break;
			j=infile->loc[infile->fp++];
         //infile->fp++;
			if (infile->fp>infile->size) break;
			//if ((j = mgetc(infile)) == EOF) break;
			i |= ((j & 0xf0) << 4);  j = (j & 0x0f) + THRESHOLD;
			for (k = 0; k <= j; k++)
			{
				c = text_buf[(i + k) & (N - 1)];
				//mputc(c, outfile);
				outfile->loc[outfile->fp++]=(char)c;
            text_buf[r++] = c;  r &= (N - 1);
			}
		}
	}
}
/*void mputc(int ch,mFILE *fh)
{
   fh->loc[fh->fp]=(char)ch;
   fh->fp++;
}*/
/*int mgetc(mFILE *fh)
{
   int ch=fh->loc[fh->fp];
   if (fh->fp>=fh->size) return EOF;
   fh->fp++;
   return ch;
}*/
void lzss_end(){}
void lzss(void *sou,u32 size,void *de)
{
   u8 text_buf[N + F - 1];
   u32 code[((u32)lzss_end-(u32)Decode)/4+1];
   void (*fun)()=(u32)code+1;
   mFILE sf={(char *)sou,0,size},df={(char *)de,0,0};
   DmaCopy(3,Decode,code,(u32)lzss_end-(u32)Decode,32);
   fun(&sf,&df,text_buf);
   //Decode(&sf,&df,text_buf);
}
/*void lzss()
{
   u32 loc=0x8100000;
   u8 text_buf[N + F - 1];
   mFILE sf={(char *)loc,0,36145},df={NULL,0,0};
   df.loc=(char *)new_malloc(60*1024);
   Decode(&sf,&df,text_buf);
   show_text("test",df.loc);
}*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?