Exemplo n.º 1
0
int zip(char *fsrc,char *fhuf,char *fdst)
{
	int ret=0;

	int i;
	FILE * ftest;

	printf("%s\n",fsrc);
	printf("%s\n",fhuf);
	printf("%s\n",fdst);

	if (mf_open(F_SRC,fsrc)<0)
	{
		ret=-E_SRC;
		goto e_src;
	}
	//if (mf_open(F_WHUF,fhuf)<0)
	//{
	//	ret=-E_HUF;
	//	goto e_huf;
	//}
	if (mf_open(F_DST,fdst)<0)
	{
		ret=-E_DST;
		goto e_dst;
	}

	build_huftree();
	tree_map[512].l=0;
	ftest=fopen(fhuf,"w");
	for (i=0;i<513;i++)
	{
		fprintf(ftest,"%d %d %d\n",huftree[i].num,huftree[i].lchild,huftree[i].rchild);;
	}
	fclose(ftest);
	build_treemap(&huftree[512]);
	//save_huftree();
	mf_close(F_SRC);
	if (mf_open(F_SRC,fsrc)<0)
	{
		ret=-E_SRC;
		goto e_src;
	}
	fzip();

	mf_close(F_SRC);
	//mf_close(F_WHUF);
	mf_close(F_DST);
	goto outt;

e_dst:
	//mf_close(F_WHUF);
	mf_close(F_SRC);
e_src:
outt:
	printf("压缩完成。\n");
	return ret;
}
Exemplo n.º 2
0
int unzip(char *fsrc,char *fhuf,char *fdst)
{
	int ret=0;
	FILE * ftest;
	int i;

	printf("%s\n",fsrc);
	printf("%s\n",fhuf);
	printf("%s\n",fdst);

	if (mf_open(F_SRC,fsrc)<0)
	{
		ret=-E_SRC;
		goto e_src;
	}
	//if (mf_open(F_RHUF,fhuf)<0)
	//{
	//	ret=-E_HUF;
	//	goto e_huf;
	//}
	if (mf_open(F_DST,fdst)<0)
	{
		ret=-E_DST;
		goto e_dst;
	}

	//load_huftree();

	ftest=fopen(fhuf,"r");
	for (i=0;i<513;i++)
	{
		fscanf(ftest,"%d%d%d",&huftree[i].num,&huftree[i].lchild,&huftree[i].rchild);;
	}
	fclose(ftest);

	funzip();

	mf_close(F_SRC);
	//mf_close(F_RHUF);
	mf_close(F_DST);

	goto outt;

e_dst:
	//mf_close(F_RHUF);
	mf_close(F_SRC);
e_src:
outt:
	printf("解压完成。\n");
	return ret;
}
Exemplo n.º 3
0
int main(){
	mf_handle_t file;
	VAC(file = mf_open(FILENAME));

	long it = 0;
	long last_ok = 0;
	for (it = 0; it < FILESIZE; it += rand() % 4096){
		mf_mapmem_handle_t loc_handle;
		void *loc_ptr = mf_map(file, 0, it, &loc_handle);
		if (loc_ptr != NULL){
			last_ok = it;
			mf_unmap(file, loc_handle);
		}
	}

	printf("Last OK: %lgGB\n", ((double)last_ok)/((double)GB));

	char *buf = malloc(SAMPLESIZE);
  	if (buf == NULL)
		return 2;
	long ret = mf_read(file, buf, SAMPLESIZE, last_ok - SAMPLESIZE);
	if (ret != SAMPLESIZE)
		return 3;
	mf_close(file);
	return 0;
}
Exemplo n.º 4
0
int main(int argc, const char * argv[]) {
    errno = 0;
    mf_handle_t file = mf_open("/Users/ivanmatveev/Desktop/speech_2.pages", 0);
    printf("file:%p\n errno = %d\n", file, errno);
    size_t size_buf = 100;
    unsigned char *buf = malloc(size_buf*sizeof(char));
    if (buf == NULL) {
        printf("error\n");
        return 0;
    }
    int i;
    ssize_t ret;
    for (i = 0; i < size_buf; i++)
        buf[i] = 'a';
    errno = 0;
    if ((ret = mf_read(file, 0, size_buf, buf)) < 0){
        printf("error in read with value %zd\nerrno = %d\n", ret, errno);
        printf("%s", strerror(errno));
    } else {
        printf("ret = %zd\n", ret);
        printf("read from file:\n");
        for (i = 0; i < size_buf; i++)
            printf("0x%02x %c ", buf[i], buf[i]);
        printf("\n");
    }
    free(buf);
    return 0;
}
Exemplo n.º 5
0
int ptest_1(const char* filename) {
    mf_handle_t handle = mf_open(filename);
    size_t size = (size_t)mf_file_size(handle);
    size_t bufsize = 32768;
    void *buf = malloc(bufsize);

    int counter = 1337 + 8; // number of ones (1) exists in file
    int counter1 = 1000;
    int counter2 = 300;
    int counter3 = 30;
    int counter4 = 7;
    size_t readbytes = 0;
    char current;
    for (size_t j = 0; readbytes < size; j += bufsize) {
        readbytes += (size_t)mf_read(handle, buf, bufsize, j);
//        printf("buf = %s\n", buf);
        for (int i = 0; i < bufsize; ++i) {
            current = ((char *) buf)[i];
            if (current != '9') counter--;
            if (current == '1') counter1--;
            if (current == '2') counter2--;
            if (current == '3') counter3--;
            if (current == '4') counter4--;
        }
    }
//    printf("\ntotal: %d\n1: %d\n2: %d\n3: %d\n4: %d\n", counter, counter1, counter2, counter3, counter4);
    if (counter || counter1 || counter2 || counter3 || counter4)
        return TESTFAILED;
    free(buf);
    return mf_close(handle);
}
Exemplo n.º 6
0
int main(){
	//printf("1erwerwer\n");
	mf_handle_t mf = mf_open("test");
	//printf("2\n");
	char buf[20	] = "1111";
	mf_write(mf, buf, 4, 3);
	//printf("3\n");
	mf_read(mf, buf, 10, 1);
	//printf("4\n");
	printf("%s\n %d\n", buf, (int)mf_file_size(mf));
	//printf("%d\n", 40*1024*(1024*1024/sysconf(_SC_PAGE_SIZE)));
	
	mf_close(mf);
	//printf("5\n");
	return 0;
}
Exemplo n.º 7
0
int main() {
    //srand( time(0) );

    struct sysinfo info;
    if (sysinfo(&info) != 0) {
        return 0;
    }
    size_t file_size_true = info.totalram*16;
    printf("file_size = %llu", file_size_true);

	int fd = open(Filename, O_RDWR | O_CREAT, 0777);
	ftruncate(fd, file_size_true);
	close(fd);

	mf_handle_t file = mf_open(Filename);
    size_t file_size = mf_file_size(file);

    if (file_size != file_size_true)
        return 555;

    size_t ok_num = 0;

	for (size_t i = 0; i < Iteration_number; i++) {
		mf_mapmem_handle_t loc_handle;
        size_t req_size = rand() % file_size;
        size_t req_offset = rand() % file_size;

		void *loc_ptr = mf_map(file, req_offset, req_size, &loc_handle);
		if (loc_ptr != NULL) {
            ok_num++;
			mf_unmap(file, loc_handle);
		}
	}

    if (ok_num != Iteration_number)
        return 666;

	mf_close(file);

	remove(Filename);
	return 0;
}
Exemplo n.º 8
0
// get k_flag from somewhere, based on command line swiches
void key_mgmt_get_key ( PGM_CTX *pgm_ctx,
			char **ck_flag,
			char **ca_flag,
			unsigned int fd_in,
			struct dibit_file_struct_t *dfs,
			char *argv0 )
{
  char *k_flag = *ck_flag;
  char *a_flag = *ca_flag;
  int chk;

  // decode without -a ???
  if ( pgm_ctx->dibit_d_flag && ! pgm_ctx->dibit_a_flag ) {
    return;
  }

  // decode with -a ???
  if ( pgm_ctx->dibit_d_flag && pgm_ctx->dibit_a_flag ) {
    // yes, get k_flag from file
    unsigned int mrec     = mf_open ( "mfrec", 0, 0 );
    unsigned int mrec_out = mf_open ( "mfrec_out", 0, 0);
    struct stat mrec_sb;
    AES_CFB aes_cfb;
    int *data_len,N;
    unsigned int aes_decoded_fd;
    unsigned int wabbit_decoded_fd;

    if ( trace_flag > 1 )
      printf("%s:%d: aes_cfb_init with a_flag <%s>\n",
	     __FUNCTION__,__LINE__,a_flag);

    memset(dfs,0,sizeof(struct dibit_file_struct_t));

    //
    // wabbit, decrypt, calculate and compare sha1
    //

    // get new temp file
    mf_fstat(fd_in,&mrec_sb);
    wabbit_decoded_fd = mf_open ( "wabbit_decoded_fd", 0, mrec_sb.st_size );

    //printf("%s: calling wabbit_chk\n",__FUNCTION__);

    // chk, decrypt, return TRUE if sha1 ok, else FALSE
    chk = wabbit_chk ( a_flag, fd_in, wabbit_decoded_fd );

    //printf("%s: wabbit_chk = %d\n",__FUNCTION__,chk);

    mf_ftruncate ( wabbit_decoded_fd, mrec_sb.st_size - 20 );

    //
    // First Job: use aes_cfb decrypt the file with a_flag key
    //
    {
      unsigned char work_buf [ AES_BLOCK_SIZE ];
      off_t blk_cnt;
      off_t blk;

      // get file size
      mf_fstat( wabbit_decoded_fd, &mrec_sb );

      // get new temp file
      aes_decoded_fd = mf_open ( "aes_decoded_fd", 0, mrec_sb.st_size );

      blk_cnt = mrec_sb.st_size / AES_BLOCK_SIZE;
      blk     = 0;

      //printf("%s: a_flag = <%s>\n",__FUNCTION__,a_flag);

      aes_cfb_init ( pgm_ctx,
		     &aes_cfb,
		     a_flag );

      mf_lseek(wabbit_decoded_fd, 0, SEEK_SET );

      while ( blk_cnt > 0 ) {
	rw(mf_read,wabbit_decoded_fd,work_buf,AES_BLOCK_SIZE);

#if defined(USE_LAST_BLOCK)
	if ( 1 == blk_cnt )
	  last_block_obscure ( work_buf, a_flag );
	else
#endif
	  aes_cfb_decrypt ( pgm_ctx,
			    &aes_cfb,
			    1,
			    work_buf,
			    work_buf);
	
	rw(mf_write,aes_decoded_fd,work_buf,AES_BLOCK_SIZE);
	
	blk_cnt -= 1;
	blk += 1;
      }

      // now putz with fd's

      // assign (Note: You've really got to understand 'c' to understand why this works.)
      mf_assign ( fd_in, aes_decoded_fd );
    }

    //
    // Second Job: skip pad at end of file
    //
    {
      unsigned char t_buf [ AES_BLOCK_SIZE * 2 ];
      unsigned char *t_buf_ptr;

      mf_lseek(fd_in,-1*(AES_BLOCK_SIZE * 2),SEEK_END);
      rw(mf_read,fd_in,t_buf,AES_BLOCK_SIZE * 2);

#if 0
      {
	printf("%s:%d: after aes decrupt, last two blocks\n",
	       __FUNCTION__,__LINE__);
	debug_show_block ( t_buf, AES_BLOCK_SIZE );
	debug_show_block ( &t_buf[AES_BLOCK_SIZE], AES_BLOCK_SIZE );
      }
#endif
      
      //exit(0);

      dfs->mrec_key_last = mrec_sb.st_size - 1;

      //
      // skip to   xxx10000000000000
      //             ^
      //             |
      //         mrec_key
      //

      t_buf_ptr = &t_buf [ AES_BLOCK_SIZE * 2 ] - 1;
      while ( ! *t_buf_ptr ) {
	// back up
	dfs->mrec_key_last -= 1;
	t_buf_ptr -= 1;
      }
      // skip 1
      if ( *t_buf_ptr != 1 ) {
#if 0
	printf("%s: Error, we backup up,but didn't get a '1', but got 0x%02x instead\n",
	       __FUNCTION__,
	       *t_buf_ptr & 0xff);
	exit(0);
#endif
      } else {
	dfs->mrec_key_last -= 1;
      }
    }

    //
    // Third Job: find the marker
    //
    dfs->marker_offset_start = find_marker ( fd_in, dfs->mrec_key_last );

    //
    // Forth Job: set mrec_key_start and mrec_key_cnt
    //
    dfs->mrec_key_start = dfs->marker_offset_start + AES_BLOCK_SIZE;
    dfs->mrec_key_cnt = dfs->mrec_key_last - dfs->mrec_key_start + 1;

    //
    // Fifth Job: setup dibit_offset_start and dibit_cnt
    //
    dfs->dibit_offset_start = 0;
    dfs->dibit_offset_last = dfs->marker_offset_start - 1;
    dfs->dibit_cnt = dfs->dibit_offset_last - dfs->dibit_offset_start + 1;

    //
    // now, we know the geometry of fd_in
    //

    // make sure we are at the front of our file before proceeding
    rw(mf_lseek,fd_in,0,SEEK_SET);

    //
    // Sixth Job: get mrec key into -> mrec
    //
    {
      //struct stat sb;
      int cnt;

      // get to start of mrec key
      rw(mf_lseek,
	 fd_in,
	 dfs->mrec_key_start,
	 SEEK_SET);
      cnt = dfs->mrec_key_cnt;

      // read it in
      while ( cnt-- > 0 ) {
	unsigned char dat;

	rw(mf_read,fd_in,&dat,1);
	rw(mf_write,mrec,&dat,1);
      }
    }

    // to front of mrec file
    mf_lseek ( mrec, 0, SEEK_SET );
    mf_fstat ( mrec, &mrec_sb );

    //
    // Seventh Job: run mrec through dibit
    //
    {
      int x;
      char lbuf [ 256 ];
      char *c = lbuf;
      int largc;
      char *largv [ 8 ];

      largc = 5;
      
      x = sprintf(c,"%s",argv0);
      largv [ 0 ] = c;
      c += x + 1;
      
      x = sprintf(c,"-n");
      largv [ 1 ] = c;
      c += x + 1;
      
      x = sprintf(c,"-d");
      largv [ 2 ] = c;
      c += x + 1;
      
      x = sprintf(c,"-k");
      largv [ 3 ] = c;
      c += x + 1;
      
      sprintf(c,"%s",a_flag);
      largv [ 4 ] = c;
      
      dibit_main ( largc, largv, mrec, mrec_out );
    }
    
    //
    // Eigth Job: decode mrec_out with aes_cfb
    //

    // to front of file
    mf_lseek ( mrec_out, 0, SEEK_SET );
    mf_fstat ( mrec_out, &mrec_sb );
    //printf("mrec size = %d\n",(int)mrec_sb.st_size);

#if 0
    {
      printf("%s:%d: after dibit decryption, before aes_cfb decryption = %d bytes\n",
	     __FUNCTION__,__LINE__,
	     (int)mrec_sb.st_size);
      debug_show_block ( mf_get_data_ptr(mrec_out),mrec_sb.st_size);
    }
#endif

    aes_cfb_init ( pgm_ctx,
		   &aes_cfb,
		   a_flag );
    
    aes_cfb_decrypt ( pgm_ctx,
		      &aes_cfb,
		      mrec_sb.st_size / 16,
		      mf_get_data_ptr ( mrec_out ),
		      mf_get_data_ptr ( mrec_out ));
    
#if 0
    {
      printf("%s:%d: after dibit aes decryption, mrec_out size = %d bytes\n",
	     __FUNCTION__,__LINE__,
	     (int)mrec_sb.st_size);
      debug_show_block ( mf_get_data_ptr(mrec_out),mrec_sb.st_size);
    }
#endif

    //
    // Ninth Job: parse decoded buffer
    //
    
    if ( k_flag ) {
      free(k_flag);
    }
    k_flag = strdup( mf_get_data_ptr ( mrec_out ) );

#if 0
    // only for test
    if ( strlen(k_flag) > 64 ) {
      printf("%s:%d bad key\n",
	     __FUNCTION__,__LINE__);
      exit(0);
    }
#endif
    
    mf_lseek ( mrec_out, strlen(k_flag) + 1, SEEK_SET );
    data_len = (unsigned int *)mf_get_data_ptr ( mrec_out );
    N = *data_len;
    
    if ( trace_flag > 1 ) printf("key_string = <%s>, N = %d\n",k_flag,N);

    // point to data
    mf_lseek ( mrec_out, 4, SEEK_CUR );
    
    // load data into cache
    memcpy(pgm_ctx->key_file_saved_bits,mf_get_data_ptr(mrec_out),N);
    pgm_ctx->key_file_saved_bits_cnt = N;
    pgm_ctx->key_file_saved_bits_remain = KEY_FILE_SAVED_BITS_MAX - N;
    pgm_ctx->key_file_saved_bits_idx = 0;

#if 0
    {
      printf("%s:%d: data cache is N = %d bytes in size\n",
	     __FUNCTION__,__LINE__,N);
      debug_show_block ( pgm_ctx->key_file_saved_bits, N );
    }
#endif

    //
    // cleanup
    //
    mf_close( mrec );
    mf_close( mrec_out );

    mf_close( wabbit_decoded_fd );

    // return k_flag
    *ck_flag = k_flag;

    // done
    return;

  } // if decode with -a ???

  // encode without -a ???
  if ( !pgm_ctx->dibit_d_flag && !pgm_ctx->dibit_a_flag ) {
    // yes, k_flag is ok
    return;
  }

  // encode with -a ???
  if ( !pgm_ctx->dibit_d_flag && pgm_ctx->dibit_a_flag ) {

    // yes
    // we are encoding
    char *x;
    char wbuf [ 256 ];

    if ( !k_flag ) {
      printf("Error, no key provided to encrypt file\b");
      exit(0);
    }

    x = strchr(k_flag,'-');
    if ( x )
      x += 1;
    else
      x = k_flag;

    // adjust sql_next_key_offset to some random location
    {
      union {
	unsigned char m[4];
	unsigned int r;
      } v;

      // read as bytes so we don't get any 0 or ff
      v.m[0] = urandom_pseudo_get_multi_bit ( pgm_ctx, 8);
      v.m[1] = urandom_pseudo_get_multi_bit ( pgm_ctx, 8);
      v.m[2] = urandom_pseudo_get_multi_bit ( pgm_ctx, 8);
      v.m[3] = urandom_pseudo_get_multi_bit ( pgm_ctx, 8);

      // is file big enough ???
      if ( pgm_ctx->key_file_sb.st_size < KEY_FILE_SAVED_BITS_MAX ) {
	printf("%s: Error, key_file.dat is not big enough to proceed. Go get a new bigger one.\n",__FUNCTION__);
	exit(0);
      }

      // jump out to some random place, make sure we have enough remaining
      // tell key_file module
      pgm_ctx->key_file_offset_start = 
	pgm_ctx->key_file_offset =
	v.r % ( pgm_ctx->key_file_sb.st_size - KEY_FILE_SAVED_BITS_MAX );

      // zero any cache
      pgm_ctx->key_file_prev_offset = -1;

    }

    sprintf(wbuf,"0x%x-%s",
	    pgm_ctx->key_file_offset,
	    x);

    if ( k_flag ) {
      free(k_flag);
      k_flag = strdup(wbuf);
    }

    *ck_flag = k_flag;

    printf("M-Key: <%s>\n",k_flag);

    return;
  } // if encode with -a ???

  printf("%s: can't get here\n",__FUNCTION__);
  exit(0);
}
Exemplo n.º 9
0
//串口接收数据保存在文件中
//轮询timeout次还没数据读入就退出
//cycletime 读文件时轮询扫描的周期 单位ms
void m_creatFile(char * pathname ,int timeout,u16 cycletime)
{
		u8 t=0;	
		mf_open((u8*)pathname,FA_OPEN_ALWAYS| FA_READ|FA_WRITE);
	
		while(1)
		{
				/*
				---------------------串口状态-------------------------
						//串口1中断服务程序
						//注意,读取USARTx->SR能避免莫名其妙的错误   	
						//u8 USART_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.
						//接收状态
						//bit15,	接收完成标志
						//bit14,	接收到0x0d
						//bit13~0,	接收到的有效字节数目
						//u16 USART_RX_STA=0;       //接收状态标记	
				---------------------FATFS函数-------------------------
						//void mymemset(void *s,u8 c,u32 count);	//设置内存
						//void mymemcpy(void *des,void *src,u32 n);//复制内存
						//void myfree(u8 memx,void *ptr);  			//内存释放(外部调用)		
						//u8 mf_write(u8*dat,u16 len);

				*/
				
				//成功读入一行数据
				if(USART_RX_STA&0x8000)
				{

						//串口收到数据长度
						int len = USART_RX_STA&0x3fff; 
					
						//开辟空间
						char * conn_recv = (char*)mymalloc(SRAMIN,len+1);
						mymemset(conn_recv,0,len+1);	//设置内存
						
						//取串口的数据 
						mymemcpy(conn_recv, USART_RX_BUF,len);
				


						//显示到屏幕
						com_statusBar(conn_recv);
					
						//发送$$开始的字符串表示文件接收完成
						if(conn_recv[0]=='$'&&conn_recv[1]=='$')
						{
								//关闭文件
								mf_close();
								//释放内存
								myfree(SRAMIN,conn_recv); 
								//状态复位
								USART_RX_STA=0;
								//break;
								return ;
						}
						//状态复位
						USART_RX_STA=0;
					
						//串口接收到的数据保存到本地
						mf_write((u8*)conn_recv,len);
						//写换行符\n
						mf_write((u8*)"\n",1);
						
						//释放内存
						myfree(SRAMIN,conn_recv); 
						
						t=0;
				}
			
				t++; 
				//超时退出
				if(t == timeout){
					return;
				}
				//串口轮询时间与等于200ms
				delay_ms(cycletime);		 			   
				LED0=!LED0;
		} 
}