Beispiel #1
0
static int encrypt_file(void)
{
	struct enc_param ep;
	ssize_t src_len;
	unsigned char *buf;
	uint32_t hdrlen;
	ssize_t totlen = 0;
	int err;
	int ret = -1;

	src_len = get_file_size(ifname);
	if (src_len < 0) {
		ERR("unable to get size of '%s'", ifname);
		goto out;
	}

	totlen = enc_compute_buf_len(product, version, src_len);
	hdrlen = enc_compute_header_len(product, version);

	buf = malloc(totlen);
	if (buf == NULL) {
		ERR("no memory for the buffer");
		goto out;
	}

	err = read_file_to_buf(ifname, &buf[hdrlen], src_len);
	if (err) {
		ERR("unable to read from file '%s'", ofname);
		goto free_buf;
	}

	memset(&ep, '\0', sizeof(ep));
	ep.key = (unsigned char *) crypt_key;
	ep.seed = seed;
	ep.longstate = longstate;
	ep.csum = buffalo_csum(src_len, &buf[hdrlen], src_len);
	ep.datalen = src_len;
	strcpy((char *) ep.magic, magic);
	strcpy((char *) ep.product, product);
	strcpy((char *) ep.version, version);

	err = encrypt_buf(&ep, buf, &buf[hdrlen]);
	if (err) {
		ERR("invalid input file");
		goto free_buf;
	}

	err = write_buf_to_file(ofname, buf, totlen);
	if (err) {
		ERR("unable to write to file '%s'", ofname);
		goto free_buf;
	}

	ret = 0;

free_buf:
 	free(buf);
out:
 	return ret;
}
int create_index_on(TCHAR * file_path)//建立索引的接口 命令行和图形界面通用
{

	char mutil_file_path[MAX_PATH]={};
	WideCharToMultiByte(CP_OEMCP,NULL,file_path,-1,mutil_file_path,MAX_PATH,NULL,FALSE);
	MemoryIndexTree * temp_index=make_memory_index_tree_all_file(file_path,NULL);
	THE_NEXT_INDEX * temp_next_index=make_next_index__(temp_index);
	char index_file_name[MAX_PATH]={};
	strcpy(index_file_name,mutil_file_path);
	strcat(index_file_name,INDEX_DB_NAME);

	char next_index_file_name[MAX_PATH]={};
	strcpy(next_index_file_name,mutil_file_path);
	strcat(next_index_file_name,NEXT_INDEX_DB_NAME);
	int res=write_buf_to_file(temp_next_index->next_index__,
		temp_next_index->length/* 这个地方的length是字节数不是个数*/
		,next_index_file_name,"wb");
	if (res<=0)
	{
		printf("二级索引写入失败\n");
	}else
	{
		printf("二级索引写入成功\n");
	}

	if (write_index_to_file(temp_index,index_file_name,"wb")>0)
	{
		printf("一级索引写入成功\n");
	}
	return 0;
}
Beispiel #3
0
static int decrypt_file(void)
{
	struct enc_param ep;
	ssize_t src_len;
	unsigned char *buf = NULL;
	int err;
	int ret = -1;

	src_len = get_file_size(ifname);
	if (src_len < 0) {
		ERR("unable to get size of '%s'", ifname);
		goto out;
	}

	buf = malloc(src_len);
	if (buf == NULL) {
		ERR("no memory for the buffer");
		goto out;
	}

	err = read_file_to_buf(ifname, buf, src_len);
	if (err) {
		ERR("unable to read from file '%s'", ifname);
		goto out;
	}

	memset(&ep, '\0', sizeof(ep));
	ep.key = (unsigned char *) crypt_key;
	ep.longstate = longstate;

	err = decrypt_buf(&ep, buf, src_len);
	if (err) {
		ERR("unable to decrypt '%s'", ifname);
		goto out;
	}

	printf("Magic\t\t: '%s'\n", ep.magic);
	printf("Seed\t\t: 0x%02x\n", ep.seed);
	printf("Product\t\t: '%s'\n", ep.product);
	printf("Version\t\t: '%s'\n", ep.version);
	printf("Data len\t: %u\n", ep.datalen);
	printf("Checksum\t: 0x%08x\n", ep.csum);

	err = write_buf_to_file(ofname, buf, ep.datalen);
	if (err) {
		ERR("unable to write to file '%s'", ofname);
		goto out;
	}

	ret = 0;

out:
	free(buf);
	return ret;
}
Beispiel #4
0
void write_log_to_buf(const char *time_str, const char *log_str)
{
	int str_len = 0;
	int time_str_len = 0;
	int log_str_len  = 0;
	int remain_buf = 0;

	time_str_len = strlen(time_str);
	log_str_len = strlen(log_str);
	str_len = time_str_len + log_str_len;
	
	remain_buf = g_ds_log_buf.end - g_ds_log_buf.last;
	if (str_len > remain_buf || g_ds_log_buf.count >= DS_FLUSH_LOG_COUNT)
	{
		write_buf_to_file(g_ds_log_buf.log_buf);
		ds_log_buf_init();
	}

	memcpy(&g_ds_log_buf.log_buf[g_ds_log_buf.last], time_str, time_str_len);
	g_ds_log_buf.last += time_str_len;

	memcpy(&g_ds_log_buf.log_buf[g_ds_log_buf.last], log_str, log_str_len);
	g_ds_log_buf.last += log_str_len;

	g_ds_log_buf.log_buf[g_ds_log_buf.last] = '\n';
	g_ds_log_buf.last++;
	
	g_ds_log_buf.count++;	
	
	if (g_ds_log_buf.count >= DS_FLUSH_LOG_COUNT)
	{
		write_buf_to_file(g_ds_log_buf.log_buf);
		ds_log_buf_init();
	}

	assert((g_ds_log_buf.end - g_ds_log_buf.last) >= 0);	

}
Beispiel #5
0
void ds_log_print(int log_level, const char * format,...)
{
    char log_str[DS_LOG_MAX_LEN + 1];
    char time_str[DS_LOG_TIME_STR_LEN + 1];
    va_list args; 
	int len = 0;
    
    ds_get_time(time_str,sizeof(time_str));
 
    va_start(args, format);
    len += vsnprintf(log_str , DS_LOG_MAX_LEN - len, format, args);
    va_end(args); 

	log_str[len] = '\0';
	
	pthread_mutex_lock(&g_file_lock);
   	write_buf_to_file(time_str, log_str);
	pthread_mutex_unlock(&g_file_lock);

    return ;
}
Beispiel #6
0
bool file_utils::write_vec_to_file(const char *pPath, const uint8_vec &data)
{
    return write_buf_to_file(pPath, data.get_ptr(), data.size());
}
void main()
{

	char tmpchr;
	char retval;
	SD_addr addr=0;

	//number of characters since the start of the current sentence
	int sentence_start=-1;
	unsigned short sentence_finished=0;
	unsigned char file_created=0;

	unsigned long bytes_received = 0;
	char *rx_read = rx_buffer3;
	char *rx_read_start; 
	WORD fat16_date, fat16_time;

	packet_type pkt_type=UNKNOWN;

	init_light();
	SD_init();
	init_uart();

	init_fat16();
	

//	init_slow_gps();
	//INTCONbits.GIEH=1; //start 'er up

	RCSTA1bits.CREN=1;
	tmpchr = get_uart_byte();
	wait_for_eol();
	while (1)
	{	
		tmpchr = get_uart_byte();
		if (tmpchr == '$')
		{
			sentence_start=0;
			sentence_finished=0;
			pkt_type=UNKNOWN;
		}
		if (tmpchr == '\n')
		{
			sentence_start=0;
			sentence_finished=1;
			pkt_type=UNKNOWN;
		}
	//	light_off();
		//according to the data sheet 1=SPS mode(?), 2=DGPS SBS mode, 6=Dead Reconing(?) mean the fixes are valid
		if (pkt_type == GGA && sentence_start == 43 && !(tmpchr == '1' || tmpchr== '2' || tmpchr=='6'))
		{
			rx_read-=43;
			bytes_received-=43;
			wait_for_eol();
			continue;
		}
		// in an RMC sentence, the "A" field indicates that a fix is valid
		if (pkt_type == RMC && sentence_start == 18 && tmpchr != 'A')
		{
			rx_read-=18;
			bytes_received-=18;
			wait_for_eol();
			continue;
		}
		// only consider $GPGGA and $GPRMC packets by the 4th byte we can figure out if we need this transmission or not
		// if we don't need it, just roll back the pointers/counters in the buffer and throw away the remaining bytes in this sentence

		if (sentence_start == 4)
		{
			if (tmpchr == 'M') 
			{
				pkt_type = RMC;
			}
			else if (tmpchr == 'G')
			{
				pkt_type = GGA;
			}
			else
			{
				rx_read-=4;
				bytes_received-=4;
				wait_for_eol();
				pkt_type = UNKNOWN;
				continue;
			}
		}

		*rx_read = tmpchr;
		rx_read++;
		sentence_start++;
		bytes_received++;
//		light_on();
	
		if (bytes_received >= 512 && sentence_finished)
		{
			RCSTA1bits.CREN=0;	//disable reception for now to avoid overrun errors
			rx_read_start = rx_read - bytes_received;  // should always be the same as rx_buffer3 

			if (!file_created) 
			{
				retval = find_date_time_from_sentence_buffer(&fat16_date, &fat16_time, rx_read_start, bytes_received);				
				if (retval == 0) {
					create_file(
						 fat16_date,
						 fat16_time
					);
				} else {
					create_file(
						 0x3ee7, /* july 7 2010 */
						 0x2108 /*04:08:16am */
							);	
				}
				file_created = 1; 
			}

			//write data to the SD card
			write_buf_to_file((BYTE *)rx_read_start);

/*
			if (retval != 0)
				goto bad;
*/
			bytes_received-=512;
			if (bytes_received > 0) {
				// to make life easy, move the leftover parts of the buffer back to the top and continue copying data below it
				memcpy(rx_read_start, rx_read - bytes_received, bytes_received);
			}
			rx_read-=512;	


			//renable reception
			RCSTA1bits.CREN=1;
			/* odds are we are going to have a fraction of a sentence if we start logging 
				whatever comes in now, so wait for the sentence to end before going on */
			wait_for_eol();
		}
	}
bad:
	SLOW_ERROR();

} // end main
Beispiel #8
0
void ds_log_flush()
{
	pthread_mutex_lock(&g_log_buf_lock);
	write_buf_to_file(g_ds_log_buf.log_buf);
	pthread_mutex_unlock(&g_log_buf_lock);
}