Example #1
0
void SWCompress::cycleStream() {
	char buf[1024];
	unsigned long len, totlen = 0;

	do {
		len = GetChars(buf, 1024);
		if (len)
			totlen += SendChars(buf, len);
	} while (len == 1024);

	zlen = slen = totlen;
}
Example #2
0
void XzCompress::Decode(void)
{
	direct = 1;	// set direction needed by parent [Get|Send]Chars()

	// get buffer
	char chunk[1024];
	char *zbuf = (char *)calloc(1, 1024);
	char *chunkbuf = zbuf;
	int chunklen;
	unsigned long zlen = 0;
	while((chunklen = GetChars(chunk, 1023))) {
		memcpy(chunkbuf, chunk, chunklen);
		zlen += chunklen;
		if (chunklen < 1023)
			break;
		else	zbuf = (char *)realloc(zbuf, zlen + 1024);
		chunkbuf = zbuf + zlen;
	}

	//printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
	if (zlen) {
		unsigned long blen = zlen*20;	// trust compression is less than 2000%
		char *buf = new char[blen]; 
		//printf("Doing decompress {%s}\n", zbuf);
		slen = 0;
		size_t zpos = 0;
		size_t bpos = 0;

		switch (lzma_stream_buffer_decode(&memlimit, 0, NULL, (const uint8_t*)zbuf, &zpos, (size_t)zlen, (uint8_t*)buf, &bpos, (size_t)&blen)){
			case LZMA_OK: SendChars(buf, bpos); slen = bpos; break;
			case LZMA_FORMAT_ERROR: fprintf(stderr, "ERROR: format error encountered during decompression.\n"); break;
			case LZMA_OPTIONS_ERROR: fprintf(stderr, "ERROR: options error encountered during decompression.\n"); break;
			case LZMA_DATA_ERROR: fprintf(stderr, "ERROR: corrupt data during decompression.\n"); break;
			case LZMA_NO_CHECK: fprintf(stderr, "ERROR: no_check error encountered during decompression.\n"); break;
			case LZMA_UNSUPPORTED_CHECK: fprintf(stderr, "ERROR: unsupported_check error encountered during decompression.\n"); break;
			case LZMA_MEMLIMIT_ERROR: fprintf(stderr, "ERROR: memlimit error encountered during decompression.\n"); break;
			case LZMA_MEM_ERROR: fprintf(stderr, "ERROR: not enough memory during decompression.\n"); break;
			case LZMA_BUF_ERROR: fprintf(stderr, "ERROR: not enough room in the out buffer during decompression.\n"); break;
			case LZMA_PROG_ERROR: fprintf(stderr, "ERROR: program error encountered during decompression.\n"); break;
			default: fprintf(stderr, "ERROR: an unknown error occured during decompression.\n"); break;
		}
		delete [] buf;
	}
	else {
		fprintf(stderr, "ERROR: no buffer to decompress!\n");
	}
	//printf("Finished decoding\n");
	free (zbuf);
}
Example #3
0
void XzCompress::Encode(void)
{
	direct = 0;	// set direction needed by parent [Get|Send]Chars()

	// get buffer
	char chunk[1024];
	char *buf = (char *)calloc(1, 1024);
	char *chunkbuf = buf;
	unsigned long chunklen;
	unsigned long len = 0;
	while((chunklen = GetChars(chunk, 1023))) {
		memcpy(chunkbuf, chunk, chunklen);
		len += chunklen;
		if (chunklen < 1023)
			break;
		else	buf = (char *)realloc(buf, len + 1024);
		chunkbuf = buf+len;
	}

	zlen = (long)lzma_stream_buffer_bound(len);
	char *zbuf = new char[zlen+1];
	size_t zpos = 0;

	if (len)
	{
		//printf("Doing compress\n");
		switch (lzma_easy_buffer_encode(level | LZMA_PRESET_EXTREME, LZMA_CHECK_CRC64, NULL, (const uint8_t*)buf, (size_t)len, (uint8_t*)zbuf, &zpos, (size_t)zlen)) {
		        case LZMA_OK: SendChars(zbuf, zpos);  break;
			case LZMA_BUF_ERROR: fprintf(stderr, "ERROR: not enough room in the out buffer during compression.\n"); break;
			case LZMA_UNSUPPORTED_CHECK: fprintf(stderr, "ERROR: unsupported_check error encountered during decompression.\n"); break;
			case LZMA_OPTIONS_ERROR: fprintf(stderr, "ERROR: options error encountered during decompression.\n"); break;
			case LZMA_MEM_ERROR: fprintf(stderr, "ERROR: not enough memory during compression.\n"); break;
			case LZMA_DATA_ERROR: fprintf(stderr, "ERROR: corrupt data during compression.\n"); break;
			case LZMA_PROG_ERROR: fprintf(stderr, "ERROR: program error encountered during decompression.\n"); break;
			default: fprintf(stderr, "ERROR: an unknown error occured during compression.\n"); break;
		}
	}
	else
	{
		fprintf(stderr, "ERROR: no buffer to compress\n");
	}
	delete [] zbuf;
	free (buf);
}
Example #4
0
ssize_t
_write (int fd __attribute__((unused)), const char* buf /*__attribute__((unused))*/,
	size_t nbyte /*__attribute__((unused))*/)
{
#if defined(TRACE)
  // STDOUT and STDERR are routed to the trace device
  if (fd == 1 || fd == 2)
    {
      return trace_write (buf, nbyte);
    }
#endif // TRACE

  SendChars(buf, nbyte);

  return nbyte;
//
//  errno = ENOSYS;
//  return -1;
}
Example #5
0
void XzCompress::Encode(void)
{
	direct = 0;	// set direction needed by parent [Get|Send]Chars()

	// get buffer
	char chunk[1024];
	char *buf = (char *)calloc(1, 1024);
	char *chunkbuf = buf;
	unsigned long chunklen;
	unsigned long len = 0;
	while((chunklen = GetChars(chunk, 1023))) {
		memcpy(chunkbuf, chunk, chunklen);
		len += chunklen;
		if (chunklen < 1023)
			break;
		else	buf = (char *)realloc(buf, len + 1024);
		chunkbuf = buf+len;
	}


	zlen = (long) (len*1.001)+15;  // TODO: check that this is sufficient
	char *zbuf = new char[zlen+1];
	size_t zpos = 0;
	if (len)
	{
		//printf("Doing compress\n");
		if (lzma_block_buffer_encode(&block, NULL, (const uint8_t*)buf, (size_t)len, (uint8_t*)zbuf, &zpos, (size_t)zlen) != LZMA_OK)
		{
			printf("ERROR in compression\n");
		}
		else {
			SendChars(zbuf, zlen);
		}
	}
	else
	{
		fprintf(stderr, "ERROR: no buffer to compress\n");
	}
	delete [] zbuf;
	free (buf);
}
Example #6
0
void XzCompress::Decode(void)
{
	// get buffer
	char chunk[1024];
	char *zbuf = (char *)calloc(1, 1024);
	char *chunkbuf = zbuf;
	int chunklen;
	unsigned long zlen = 0;
	while((chunklen = GetChars(chunk, 1023))) {
		memcpy(chunkbuf, chunk, chunklen);
		zlen += chunklen;
		if (chunklen < 1023)
			break;
		else	zbuf = (char *)realloc(zbuf, zlen + 1024);
		chunkbuf = zbuf + zlen;
	}

	//printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
	if (zlen) {
		unsigned long blen = zlen*20;	// trust compression is less than 1000%
		char *buf = new char[blen]; 
		//printf("Doing decompress {%s}\n", zbuf);
		slen = 0;
		size_t zpos = 0;
		size_t bpos = 0;

		switch (lzma_block_buffer_decode(&block, NULL, (const uint8_t*)zbuf, &zpos, (size_t)zlen, (uint8_t*)buf, &bpos, (size_t)&blen)){
			case LZMA_OK: SendChars(buf, blen); slen = blen; break;
			case LZMA_MEM_ERROR: fprintf(stderr, "ERROR: not enough memory during decompression.\n"); break;
			case LZMA_BUF_ERROR: fprintf(stderr, "ERROR: not enough room in the out buffer during decompression.\n"); break;
			case LZMA_DATA_ERROR: fprintf(stderr, "ERROR: corrupt data during decompression.\n"); break;
			default: fprintf(stderr, "ERROR: an unknown error occured during decompression.\n"); break;
		}
		delete [] buf;
	}
	else {
		fprintf(stderr, "ERROR: no buffer to decompress!\n");
	}
	//printf("Finished decoding\n");
	free (zbuf);
}
Example #7
0
void Bzip2Compress::Encode(void)
{
	direct = 0;	// set direction needed by parent [Get|Send]Chars()

	// get buffer
	char chunk[1024];
	char *buf = (char *)calloc(1, 1024);
	char *chunkbuf = buf;
	unsigned long chunklen;
	unsigned long len = 0;
	while((chunklen = GetChars(chunk, 1023))) {
		memcpy(chunkbuf, chunk, chunklen);
		len += chunklen;
		if (chunklen < 1023)
			break;
		else	buf = (char *)realloc(buf, len + 1024);
		chunkbuf = buf+len;
	}


	zlen = (long) (len*1.01)+600;
	char *zbuf = new char[zlen+1];
	if (len)
	{
		//printf("Doing compress\n");
		if (BZ2_bzBuffToBuffCompress(zbuf, (unsigned int*)&zlen, buf, len, level, 0, 0) != BZ_OK)
		{
			printf("ERROR in compression\n");
		}
		else {
			SendChars(zbuf, zlen);
		}
	}
	else
	{
		fprintf(stderr, "ERROR: no buffer to compress\n");
	}
	delete [] zbuf;
	free (buf);
}
Example #8
0
void Bzip2Compress::Decode(void)
{
	direct = 1;	// set direction needed by parent [Get|Send]Chars()

	// get buffer
	char chunk[1024];
	char *zbuf = (char *)calloc(1, 1024);
	char *chunkbuf = zbuf;
	int chunklen;
	unsigned long zlen = 0;
	while((chunklen = GetChars(chunk, 1023))) {
		memcpy(chunkbuf, chunk, chunklen);
		zlen += chunklen;
		if (chunklen < 1023)
			break;
		else	zbuf = (char *)realloc(zbuf, zlen + 1024);
		chunkbuf = zbuf + zlen;
	}

	//printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
	if (zlen) {
		unsigned int blen = zlen*20;	// trust compression is less than 1000%
		char *buf = new char[blen]; 
		//printf("Doing decompress {%s}\n", zbuf);
		slen = 0;
		switch (BZ2_bzBuffToBuffDecompress(buf, &blen, zbuf, zlen, 0, 0)){
			case BZ_OK: SendChars(buf, blen); slen = blen; break;
			case BZ_MEM_ERROR: fprintf(stderr, "ERROR: not enough memory during decompression.\n"); break;
			case BZ_OUTBUFF_FULL: fprintf(stderr, "ERROR: not enough room in the out buffer during decompression.\n"); break;
			case BZ_DATA_ERROR: fprintf(stderr, "ERROR: corrupt data during decompression.\n"); break;
			default: fprintf(stderr, "ERROR: an unknown error occured during decompression.\n"); break;
		}
		delete [] buf;
	}
	else {
		fprintf(stderr, "ERROR: no buffer to decompress!\n");
	}
	//printf("Finished decoding\n");
	free (zbuf);
}
Example #9
0
 void TK_Device2PC(void)
 {
    int count=0,i=0;
	uint16_t len=0;
	uint8_t test;		
	TK_CNANNEL_CONFIG tk_channel_config;
	TK_CNANNEL_CONFIG *tkchconfig;
	len=sizeof(TK_CNANNEL_CONFIG);	
	
	
	//get magic id
	i=0;
	count=0;
 	while(1)
	{	
		if(kbhit())	
		{		
			test=GetChar();
			if(test!=MagicId[i++])
			{										
				return;
			}
			if(i==sizeof(MagicId)) break;
		}
		else
		{			
			count++;			
		}
		if(count > DELAY_TIME_OUT) return;
	} 	
	
	i=0;
	count=0;
	while(i<2)
	{
	 	if(kbhit())	
		{
			((uint8_t *)&reclen)[i]=GetChar();
			i++;
		}


	}

	i=0;
	count=0;
	while(i<reclen)
	{
		 if(kbhit())	
		{
			ReceiveBuf[i]=GetChar();
			i++;
		}

	}

	SendChars(MagicIdR,sizeof(MagicIdR));

	count=0;
	switch(ReceiveBuf[0])
	{
		case OP_AUTO_CHANNEL_CONFIG:
		/*Send : MagicidR | len(16) | "START" | progess (5 time) |len<NEXT> (16) */
			len=5+sizeof(len);
				
			memcpy(SendBuf+count,&len,sizeof(len));
			count+=sizeof(len);
			memcpy(SendBuf+count,(uint8_t *)"START",5);
			count+=5;
			len=sizeof(TK_CNANNEL_CONFIG)+5 /*Used by Progress */;
			memcpy(SendBuf+count,&len,2);
			count+=sizeof(len);
			SendChars((uint8_t *)SendBuf,count);
												
			AutoChannelConfig(ReceiveBuf[1],&final_result);
			
			tk_channel_config.channel=ReceiveBuf[1];					
			tk_channel_config.base=final_result.base;
			tk_channel_config.diff=final_result.diff;
			tk_channel_config.current=final_result.current;
			tk_channel_config.div=final_result.div;	
							
			SendChars((uint8_t *)&tk_channel_config,sizeof(TK_CNANNEL_CONFIG));

		break;
		case OP_GET_CONFIG:				
			len=TK_CH_NUM*sizeof(TK_CNANNEL_CONFIG);			
			memcpy( SendBuf+count,(uint8_t *)&len,sizeof(len));		
		   	count+=sizeof(len);
			
			for(i=0;i<TK_CH_NUM;i++)			
			{			
				tk_channel_config.channel=i;					
				tk_channel_config.base=cfg[i].base;
				tk_channel_config.diff=cfg[i].diff;
				tk_channel_config.current=cfg[i].current;
				tk_channel_config.div=cfg[i].div;		
	
				memcpy(SendBuf+count,(uint8_t *)(&tk_channel_config),sizeof(TK_CNANNEL_CONFIG));
				count+=sizeof(TK_CNANNEL_CONFIG);
			}		
			 SendChars(SendBuf,count);

		break;


		case OP_SET_CHANNEL_CONFIG:
		 			
			tkchconfig=(TK_CNANNEL_CONFIG *)(ReceiveBuf+1);
											
			cfg[tkchconfig->channel].base=tkchconfig->base;
			cfg[tkchconfig->channel].diff=tkchconfig->diff;
			cfg[tkchconfig->channel].current=tkchconfig->current;
			cfg[tkchconfig->channel].div=tkchconfig->div;
			len=3;	
			SendBuf[0]='A';
			SendBuf[1]='C';
			SendBuf[2]='K';	
			SendChars((uint8_t *)&len,sizeof(len));					
			SendChars(SendBuf,3);
						
		break;

		case OP_LISTEN_STATUS:
		/*Recive :   magic_id(64) |  len(16) | OP_LISTEN_STATUS(8) | Channel(16)                          */ 		
		/*Send   :   magic_id(32) |  len(16) | struct LISTEN_STATUS(32) | ... | struct LISTEN_STATUS(32)  */
		 		  RecordCount=0;
				  memcpy((uint8_t *)&RecordChannel,(uint8_t *)(&ReceiveBuf[1]),2);
				  Get_TK_Data(RecordChannel);
				  for(i=0;i<MAX_CHANNEL;i++)
				  {
				  	if(RecordChannel & 0x0001)					
						RecordCount++;				
					RecordChannel=RecordChannel>>1;
				  }
				  len=sizeof(LISTEN_STATUS)*RecordCount;
				  SendChars((uint8_t *)&len,sizeof(len));

				  memcpy((uint8_t *)&RecordChannel,(uint8_t *)(&ReceiveBuf[1]),2);
				  for(i=0;i<MAX_CHANNEL;i++)
				  {
				  	if(RecordChannel & 0x0001)
					{
						Record.channel=i;
						Record.data=data[i];
					    SendChars((uint8_t *)&Record,sizeof(Record));
					}
					RecordChannel=RecordChannel>>1;
				  }				
		break;
		
		default:
		break;
	}
	
	return ;

 }
Example #10
0
void AutoChannelConfig(uint8_t ch,best_cfg* final_result)
{
	uint16_t div, i;
	uint16_t current = 1;
	S_TK_CH_CFG ch_cfg = {1, 0, 0xFFFF, 0x0000};
	uint8_t sen_level, sen_state,tmp_div;
	uint16_t sen_data;	
	int8_t tmp_score;
	uint8_t tmp_cur;
	uint8_t progress=0;

	final_result->current = 1;
	final_result->div = 0;
	final_result->base = 0;
	final_result->diff = 0;

	base_rank[0].val = 0xFFFF;							
	base_rank[0].div = 0;
	base_rank[0].current = 1;
	diff_rank[0].val = 0;
	diff_rank[0].div = 0;
	diff_rank[0].current = 1;
	
			progress=20;
			SendChars((uint8_t *)&progress,sizeof(progress));

			for(div = 1; div < 12; div++) {			
				for(current = 1; current <= 15; current++) {
					ch_cfg.u8Level = current;
					ch_cfg.u8Div = div;
					TK_ConfigChannel(ch, &ch_cfg);		
					result[div][current - 1].level_off = 0;	
					result[div][current - 1].data_off = 0;					
		
					for(i = 0; i < 2; i++) {
						complete = 0;
						TK_Start(1 << ch);
						while(complete != 1);
						TK_ReadStatus(&sen_level, &sen_state, NULL);
						sen_data = TK_ReadData(ch);
						if(sen_state != 0 || sen_data == 0 || sen_data == 0xFFFF) {
							result[div][current - 1].level_off = 0;	
							result[div][current - 1].data_off = 0;
							break;
						} else {
							result[div][current - 1].level_off += sen_level;
							result[div][current - 1].data_off += (sen_data >> 1);						
						}									
					}
					result[div][current - 1].level_off >>= 1;
				}
			}
			
			
			progress=40;
			SendChars((uint8_t *)&progress,sizeof(progress));
	
		 	while(1)
			{	
				if(kbhit())												
					if(GetChar()==progress)															
						break;				
			} 	

			for(div = 1; div < 12; div++) {				
				for(current = 1; current <= 15; current++) {
					ch_cfg.u8Level = current;
					ch_cfg.u8Div = div;
					TK_ConfigChannel(ch, &ch_cfg);
		
					result[div][current - 1].level_on = 0;	
					result[div][current - 1].data_on = 0;					
					if(result[div][current - 1].level_off == 0)
						continue;
					for(i = 0; i < 2; i++) {
						complete = 0;
						TK_Start(1 << ch);
						while(complete != 1);
						TK_ReadStatus(&sen_level, &sen_state, NULL);
						sen_data = TK_ReadData(ch);
						if(sen_state != 0 || sen_data == 0 || sen_data == 0xFFFF) {
							result[div][current - 1].level_on = 0;	
							result[div][current - 1].data_on = 0;
							break;
						} else {
							result[div][current - 1].level_on += sen_level;
							result[div][current - 1].data_on += (sen_data >> 1);
						
						}									
					}
					result[div][current - 1].level_on >>= 1;
				}
			}


			// calculate sense level, timer divider, and change current score
			for(div = 1; div < 12; div++) {
				for(current = 1; current <= 15; current++) {

					result[div][current - 1].score = 0;

					if((result[div][current - 1].level_off != 0) &&
					   (result[div][current - 1].level_on != 0) &&	
					   (result[div][current - 1].data_on > result[div][current - 1].data_off)) {
						
						result[div][current - 1].score += (div_score[div] + cur_score[current] + lvl_score[result[div][current - 1].level_off]);
		
					}
				}
			}	
			
			// find out entry with highest diff
			for(div = 1; div < 12; div++) {
				for(current = 1; current <= 15; current++) {
					if(result[div][current - 1].score != 0) {
						if(((result[div][current - 1].data_on - result[div][current - 1].data_off) > diff_rank[0].val) &&
						   (result[div][current - 1].data_on > result[div][current - 1].data_off) &&
						   ((result[div][current - 1].data_on - result[div][current - 1].data_off) > 0x100)) {
							diff_rank[0].val = (result[div][current - 1].data_on - result[div][current - 1].data_off);
							diff_rank[0].current = current;
							diff_rank[0].div = div;
							break;
						}	
					}
				}
			}				

		   progress=60;
			SendChars((uint8_t *)&progress,sizeof(progress));

			// give score base on the differences
			for(div = 1; div < 12; div++) {
				for(current = 1; current <= 15; current++) {
					if(result[div][current - 1].score > 0) {											
						if(result[div][current - 1].data_on < result[div][current - 1].data_off)
							result[div][current - 1].score -=	100;
						else if((result[div][current - 1].data_on - result[div][current - 1].data_off) < 0x50)
							result[div][current - 1].score -=	100;	
						else
							result[div][current - 1].score += (8 - (diff_rank[0].val - (result[div][current - 1].data_on - result[div][current - 1].data_off)) / 0x800);
					}
				}
			}

			
			// find out lowest base
			for(div = 1; div < 12; div++) {
				for(current = 1; current <= 15; current++) {
					if(result[div][current - 1].score > 0) {
						if((result[div][current - 1].data_off < base_rank[0].val) && (result[div][current - 1].data_off < 0xF000)) {
							base_rank[0].val = result[div][current - 1].data_off;
							base_rank[0].current = current;
							base_rank[0].div = div;
							break;
						}
	
					}
				}
			}

			progress=80;
			SendChars((uint8_t *)&progress,sizeof(progress));

			// give score base on the differences
			for(div = 1; div < 12; div++) {
				for(current = 1; current <= 15; current++) {
					if(result[div][current - 1].score > 0) {											
							result[div][current - 1].score += (4 - (diff_rank[0].val - result[div][current - 1].data_off) / 0x1000);
					}
				}
			}

			// find the entry with highest score
			tmp_score = 0;
			tmp_cur = 1;  // eliminate compilation warning
			tmp_div = 0;						 // eliminate compilation warning
			for(div = 1; div < 12; div++) {
				for(current = 1; current <= 15; current++) {
					if(result[div][current - 1].score > tmp_score) {
						tmp_score = result[div][current - 1].score;
						tmp_div = div;
						tmp_cur = current;
							
					}
				}
			}

		   	progress=100;
			SendChars((uint8_t *)&progress,sizeof(progress));

			if(tmp_score == 0) {
				final_result->base=0;
				final_result->diff=0;
				final_result->current=0;
				final_result->div=0;
			} else {
				final_result->base = result[tmp_div][tmp_cur - 1].data_off;
				final_result->diff = result[tmp_div][tmp_cur - 1].data_on - result[tmp_div][tmp_cur - 1].data_off;
				final_result->current = tmp_cur;
				final_result->div = tmp_div;
			}
}