membuffer.cpp

来自「x-wrt is the GUI config tool for openwrt」· C++ 代码 · 共 810 行 · 第 1/2 页

CPP
810
字号
		bufLen-=6;
		freeMemBuffers(false);
	}
	else
		freeMemBuffers(true);


	prepareMemBuffers();
	CMemoryBuffer* memout_tmp=NULL;

 	while (true)
	{	
		if (!IF_OPTION(OPTION_PAQ))
		{
			GETC(i);

			if (i<=0)
				break;

			for (c=0; c<i; c++)
				GETC(s[c]);
		}
		else
		{
#ifdef USE_PAQ_LIBRARY
			i=PAQ_encoder->decompress();

			if (i<=0)
				break;

			for (c=0; c<i; c++)
				s[c]=PAQ_encoder->decompress();
#endif
		}

		std::string str;
		str.append((char*)s,i);

		PRINT_CONTAINERS(("cont=%s\n",str.c_str()));

		if (str=="!data")
			memout_tmp=memout;
		else
		if (str=="!!letters")
			memout_tmp=memout_letters;
		else
		if (str=="!!!words")
			memout_tmp=memout_words;
		else
		if (str=="!!!words2")
			memout_tmp=memout_words2;
		else
		if (str=="!!!words3")
			memout_tmp=memout_words3;
		else
		if (str=="!!!words4")
			memout_tmp=memout_words4;
		else
		if (str=="!num")
			memout_tmp=memout_num;
		else
		if (str=="!numb")
			memout_tmp=memout_numb;
		else
		if (str=="!numc")
			memout_tmp=memout_numc;
		else
		if (str=="!num2")
			memout_tmp=memout_num2;
		else
		if (str=="!num2b")
			memout_tmp=memout_num2b;
		else
		if (str=="!num2c")
			memout_tmp=memout_num2c;
		else
		if (str=="!num3")
			memout_tmp=memout_num3;
		else
		if (str=="!num3b")
			memout_tmp=memout_num3b;
		else
		if (str=="!num3c")
			memout_tmp=memout_num3c;
		else
		if (str=="!num4")
			memout_tmp=memout_num4;
		else
		if (str=="!num4b")
			memout_tmp=memout_num4b;
		else
		if (str=="!num4c")
			memout_tmp=memout_num4c;
		else
		if (str=="!year")
			memout_tmp=memout_year;
		else
		if (str=="!date")
			memout_tmp=memout_date;
		else
		if (str=="!date2")
			memout_tmp=memout_date2;
		else
		if (str=="!date3")
			memout_tmp=memout_date3;
		else
		if (str=="!pages")
			memout_tmp=memout_pages;
		else
		if (str=="!remain")
			memout_tmp=memout_remain;
		else
		if (str=="!time")
			memout_tmp=memout_time;
		else
		if (str=="!remain2")
			memout_tmp=memout_remain2;
		else
		if (str=="!ip")
			memout_tmp=memout_ip;
		else
		if (str=="!hm")
			memout_tmp=memout_hm;
		else
		if (str=="!hms")
			memout_tmp=memout_hms;
		else
		{
			memout_tmp=new CMemoryBuffer(str);
			std::pair<std::string,CMemoryBuffer*> p(str,memout_tmp);
			memmap.insert(p);
		}


		if (IF_OPTION(OPTION_LZMA))
		{
			for (i=0, fileLen=0; i<4; i++)
			{
				GETC(c);
				fileLen=fileLen*256+c;
			}	
		
			memout_tmp->SetSrcBuf(buf, fileLen);
			buf+=fileLen;
			bufLen-=fileLen;
			len+=fileLen;
			lenCompr+=1;
		}
		else
		{
#ifdef USE_PAQ_LIBRARY
			if (IF_OPTION(OPTION_PAQ))
			{
				for (i=0, fileLen=0; i<4; i++)
					fileLen=fileLen*256+PAQ_encoder->decompress();

				int last=ftell(XWRT_file);
				for (ui=0; ui<fileLen; )
				{
					buf[ui++]=PAQ_encoder->decompress();
					if (ui%102400==0)
					{
						printStatus(ftell(XWRT_file)-last,0,false);
						last=ftell(XWRT_file);
					}
				}

				printStatus(ftell(XWRT_file)-last,0,false);
			
				memout_tmp->SetSrcBuf(buf, fileLen);
				buf+=fileLen+3;
				len+=fileLen;
				bufLen-=fileLen+3;
				lenCompr+=1;
			}
			else
#endif
#ifdef USE_PPMVC_LIBRARY
			if (IF_OPTION(OPTION_PPMVC))
			{
				int last=ftell(XWRT_file);
				fileLen=PPMVClib_DecodeFileToMem(PPMVClib_order,XWRT_file,buf,bufLen);
				printStatus(ftell(XWRT_file)-last,0,false);

				memout_tmp->SetSrcBuf(buf, fileLen);
				buf+=fileLen+3;
				len+=fileLen;
				bufLen-=fileLen+3;
				lenCompr+=1;
			}
			else
#endif
#ifdef USE_ZLIB_LIBRARY
			if (IF_OPTION(OPTION_ZLIB))
			{
				fileLen=Zlib_decompress(XWRT_file,zlibBuffer,ZLIB_BUFFER_SIZE,buf,bufLen,i);
				
				PRINT_CONTAINERS(("fileLen=%d\n",fileLen));

				memout_tmp->SetSrcBuf(buf, fileLen);
				buf+=fileLen;
				len+=fileLen;
				lenCompr+=i;
				bufLen-=fileLen;			
			}
			else
#endif
			{
				int c;
				for (i=0, fileLen=0; i<4; i++)
				{
					GETC(c);
					fileLen=fileLen*256+c;
				}
		
				len+=fileLen;
				lenCompr+=fileLen;
				memout_tmp->AllocSrcBuf(fileLen);

				fread_fast(memout_tmp->SourceBuf,memout_tmp->SrcLen,XWRT_file);

				printStatus(fileLen,0,false);
			}
		}

	}

#ifdef USE_LZMA_LIBRARY
	if (IF_OPTION(OPTION_LZMA))
	{
		unsigned int newLen=LZMAlib_GetFileSize(inStream);
		bufLen=maxMemSize+10240;
		bufLen-=6;
		buf=bigBuffer+3;

		if (newLen>bufLen)
			OUT_OF_MEMORY();

		int last=LZMAlib_GetInputFilePos(inStream);
		LZMAlib_DecodeFileToMem(inStream,buf,newLen);
		printStatus(LZMAlib_GetInputFilePos(inStream)-last,0,false);
	}
#endif

	PRINT_DICT(("readMemBuffers() dataSize=%d compr=%d allocated=%d\n",len,lenCompr,maxMemSize+10240));
}

void CContainers::freeMemBuffers(bool freeMem)
{
	mem_stack.clear();

	std::map<std::string,CMemoryBuffer*>::iterator it;

	for (it=memmap.begin(); it!=memmap.end(); it++)
	{
		if (!freeMem)
			it->second->Clear();
		delete(it->second);			
	}

	memmap.clear();
}

#ifdef USE_ZLIB_LIBRARY

int Zlib_compress(FILE* fileout,Byte *uncompr, uLong uncomprLen,Byte *compr, uLong comprLen, int comprLevel)
{
    z_stream c_stream; /* compression stream */
    int err;
	int outSize=0;

    c_stream.zalloc = (alloc_func)0;
    c_stream.zfree = (free_func)0;
    c_stream.opaque = (voidpf)0;

    err = deflateInit(&c_stream, comprLevel);

    CHECK_ERR(err, "deflateInit");


    c_stream.next_out = compr;
    c_stream.avail_out = (uInt)comprLen;
    c_stream.next_in = uncompr;
    c_stream.avail_in = (uInt)uncomprLen;

	do
	{
	    err = deflate(&c_stream, Z_FINISH);
		fprintf(fileout, "%c%c%c", (c_stream.total_out>>16)%256, (c_stream.total_out>>8)%256, (c_stream.total_out)%256);
		fwrite_fast(compr, c_stream.total_out, fileout);
		outSize+=c_stream.total_out;
		printStatus(0,c_stream.total_out,true);
		c_stream.next_out = compr;
	    c_stream.avail_out = (uInt)comprLen;
		c_stream.total_out = 0;
	}
	while (err == Z_OK);


    err = deflateEnd(&c_stream);
    CHECK_ERR(err, "deflateEnd");

	return outSize;
}

int Zlib_decompress(FILE* file,Byte *compr, uLong comprLen,Byte *uncompr, uLong uncomprLen,int& totalIn)
{
    int err;
    z_stream d_stream; /* decompression stream */
 
    d_stream.zalloc = (alloc_func)0;
    d_stream.zfree = (free_func)0;
    d_stream.opaque = (voidpf)0;

    d_stream.next_out = uncompr;
    d_stream.avail_out = (uInt)uncomprLen;

    err = inflateInit(&d_stream);
    CHECK_ERR(err, "inflateInit");

	int i,fileLen;
	while (true)
	{
		for (i=0, fileLen=0; i<3; i++)
		{
			int c=getc(file);
			fileLen=fileLen*256+c;
		}

		if ((int)comprLen<fileLen)
		{
			printf("error: comprLen(%d)<fileLen(%d)\n",comprLen,fileLen);
			OUT_OF_MEMORY();
		}

		fread_fast((unsigned char*)compr,fileLen,file);

		d_stream.next_in  = compr;
		d_stream.avail_in = fileLen; //(int) rd;

		err = inflate(&d_stream, Z_FINISH);

        if (err != Z_BUF_ERROR) 
			break; 
    }


    err = inflateEnd(&d_stream);
    CHECK_ERR(err, "inflateEnd");

	totalIn=d_stream.total_in;
	printStatus(d_stream.total_in,0,false);
	return d_stream.total_out;
} 

#endif

void CContainers::selectMemBuffer(unsigned char* s,int s_size)
{
	while ((s[0]==' ' || s[0]=='<') && s_size>0)
	{
		s++;
		s_size--;
	}
	
	
	if (s_size>0 && s[s_size-1]=='>')
		s_size--;
	
	if (s_size==0)
		return;
	
	
	std::string str;
	str.append((char*)s,s_size);
	
	static std::map<std::string,CMemoryBuffer*>::iterator it;
	
	it=memmap.find(str);
	
	mem_stack.push_back(memout);

	if (it!=memmap.end())
		memout=it->second;
	else
	{
		memout=new CMemoryBuffer(str);
		std::pair<std::string,CMemoryBuffer*> p(str,memout);
		memmap.insert(p);
	}

	return;
}


void CContainers::MemBufferPopBack()
{
	if (mem_stack.size()>0)
	{
		PRINT_STACK(("\nmemout3 pop_back\n"));
		memout=mem_stack.back();
		mem_stack.pop_back();
	}
}

⌨️ 快捷键说明

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