Ejemplo n.º 1
0
        // TODO: recycle ptr!
        void load() {
            is_loaded = true;
            adjfilesize = get_filesize(filename_adj);
            edatafilesize = get_filesize(filename_edata);
            
            bool async_inedgedata_loading = !svertex_t().computational_edges();
            
#ifdef SUPPORT_DELETIONS
            async_inedgedata_loading = false;  // Currently we encode the deleted status of an edge into the edge value (should be changed!),
                                               // so we need the edge data while loading
#endif
                        
            //preada(adjf, adjdata, adjfilesize, 0);
            
            adj_session = iomgr->open_session(filename_adj, true);
            iomgr->managed_malloc(adj_session, &adjdata, adjfilesize, 0);
            adj_stream_session = streaming_task(iomgr, adj_session, adjfilesize, (char**) &adjdata);
            
            iomgr->launch_stream_reader(&adj_stream_session);            
            /* Initialize edge data asynchonous reading */
            if (!only_adjacency) {
                edata_iosession = iomgr->open_session(filename_edata, false);
                
                iomgr->managed_malloc(edata_iosession, &edgedata, edatafilesize, 0);
                if (async_inedgedata_loading) {
                    iomgr->managed_preada_async(edata_iosession, &edgedata, edatafilesize, 0);
                } else {
                    iomgr->managed_preada_now(edata_iosession, &edgedata, edatafilesize, 0);
                }
            }
        }
Ejemplo n.º 2
0
int do_files_differ(
    const char* filename1,
    const char* filename2,
    bool* result_out
)
{
    if(!filename1 || !filename2 || !result_out)
    {
        return -1;
    }

    size_t file1size = 0;
    size_t file2size = 0;

    if(get_filesize(filename1, &file1size) || get_filesize(filename2, &file2size))
    {
        return -1;
    }

    if(file1size == file2size)
    {
        uint8_t* buffer1 = calloc(file1size, 1);
        uint8_t* buffer2 = calloc(file2size, 1);

        if(read_file_into_buffer(filename1, buffer1, file1size))
        {
            free(buffer1);
            free(buffer2);
            return -1;
        }
        if(read_file_into_buffer(filename2, buffer2, file2size))
        {
            free(buffer1);
            free(buffer2);
            return -1;
        }

        *result_out = (memcmp(buffer1, buffer2, file1size) != 0);
        free(buffer1);
        free(buffer2);
        return 0;
    }
    else
    {
        *result_out = true;
        return 0;
    }

    // Should not reach here
    return -1;
}
Ejemplo n.º 3
0
int encode_steg(char *file_cont, char *file_data,
				char *file_output, unsigned char *key)
{
	FILE *fpc, *fpd, *fpo;
	unsigned char digest[16];
	unsigned long max_hide=0;
	//clock_t c1, c2;

	// Open files
	if((fpc = fopen(file_cont, "rb")) == NULL) {
		fprintf(stderr, "Cannot open container file!\n");
		return(-1);
	}
	if((fpd = fopen(file_data, "rb")) == NULL) {
		fprintf(stderr, "Cannot open data file!\n");
		return(-1);
	}
	if((fpo = fopen(file_output, "wb")) == NULL) {
		fprintf(stderr, "Cannot open output file!\n");
		return(-1);
	}

	// Make sure the data will fit in the container
	max_hide = get_max_hide(get_filesize(fpc)-STARTBYTE-(sizeof(struct stash_hdr)*8));
	if(max_hide < get_filesize(fpd)) {
		fprintf(stderr, "Error! Data file cannot fit in %s; %d bytes max!\n",
			file_cont, (int)max_hide);
		return(-1);
	}

	// Get MD5 Checksum of key
	MD5String(key, digest);

	// Seed the PRNG with the md5 checksum of the key
	prng_seed(digest);

	// Encode
	//c1 = clock();
	encode_file(fpc, fpd, fpo);
	//c2 = clock();
	//printf("%f\n", get_speed(c1,c2));
	//system("PAUSE");

	// Close files
	fclose(fpc);
	fclose(fpd);
	fclose(fpo);

	return(0);
}
Ejemplo n.º 4
0
/* Run multiple compression tests on data stored in a file */
int test_compress_file(char *file_name)
{
	int ret = IGZIP_COMP_OK;
	uint32_t in_size;
	uint8_t *in_buf = NULL;
	FILE *in_file = NULL;

	in_file = fopen(file_name, "rb");
	if (!in_file)
		return FILE_READ_FAILED;

	in_size = get_filesize(in_file);
	if (in_size != 0) {
		in_buf = malloc(in_size);
		if (in_buf == NULL)
			return MALLOC_FAILED;
		fread(in_buf, 1, in_size, in_file);
	}

	ret |= test_compress_stateless(in_buf, in_size);
	ret |= test_compress(in_buf, in_size, NO_FLUSH);
	ret |= test_compress(in_buf, in_size, SYNC_FLUSH);
	ret |= test_flush(in_buf, in_size);

	if (ret)
		printf("Failed on file %s\n", file_name);

	if (in_buf != NULL)
		free(in_buf);

	return ret;
}
Ejemplo n.º 5
0
int main(int argc, char *argv[])
{
	FILE *in = NULL;
	unsigned char *in_buf = NULL;
	uint64_t in_file_size;

	if (argc != 2) {
		fprintf(stderr, "Usage: isal_fuzz_inflate <infile>\n");
		exit(1);
	}
	in = fopen(argv[1], "rb");
	if (!in) {
		fprintf(stderr, "Can't open %s for reading\n", argv[1]);
		exit(1);
	}
	in_file_size = get_filesize(in);
	in_buf = malloc(in_file_size);

	if (in_buf == NULL) {
		fprintf(stderr, "Failed to malloc input and outputs buffers\n");
		exit(1);
	}

	fread(in_buf, 1, in_file_size, in);

	return LLVMFuzzerTestOneInput(in_buf, in_file_size);
}
Ejemplo n.º 6
0
Archivo: tags.c Proyecto: dacap/htmlex
/* returns in a human-readable format the file size */
static char *tag_file_size (int argc, char *argv[])
{
  if (argc >= 1) {
    char *filename = process_text (argv[0]);
    int size = get_filesize (filename);
    char format[256];
    /* add a dependency */
    if (size >= 0)
      add_deps (filename);
    /* make the file size in a human-readable format */
    if (size < 0)
      sprintf (format, "%d", size);
    else if (size < 1024)
      sprintf (format, "%d byte%c", size, size == 1 ? 0 : 's');
    else if (size < 1024 * 1024)
      sprintf (format, "%0.1f KB", (float)size / 1024.0);
    else if (size < 1024 * 1024 * 1024)
      sprintf (format, "%0.2f MB", (float)size / 1024.0 / 1024.0);
    else
      sprintf (format, "%0.2f GB", (float)size / 1024.0 / 1024.0 / 1024.0);
    free (filename);
    return strdup (format);
  }
  else
    return NULL;
}
Ejemplo n.º 7
0
char *get_codebuf_from_filepath(const char *filepath) {
    FILE *fp = NULL;
    char *codebuf = NULL;
    long fsize = 0;
    if (filepath == NULL) {
        return NULL;
    }
    fp = fopen(filepath, "rb");
    if (fp == NULL) {
        return NULL;
    }
    fsize = get_filesize(fp);
    if (fsize == 0) {
        fclose(fp);
        return NULL;
    }
    codebuf = (char *) getseg(fsize + 1);
    memset(codebuf, 0, fsize + 1);
    if (fread(codebuf, 1, fsize, fp) == -1) {
        free(codebuf);
        fclose(fp);
    }
    fclose(fp);
    return codebuf;
}
Ejemplo n.º 8
0
//
// Read a file and alloc a buffer for it
//
uint8_t* easy_readfile(const char *filename, int *file_size)
{
	int actual;
	int fd;
	uint8_t *buf;

	*file_size = get_filesize(filename);
	printf("name=%s, size=%d\n", filename, *file_size);

	fd = open(filename, O_RDONLY, 0);

	if (fd == -1) {
		return NULL;
	}
	
	if(! (buf = malloc(*file_size)) )	{
		return NULL;
	}

	actual = read(fd, buf, *file_size);
	close(fd); 

	*file_size = actual;
	return buf;
}
Ejemplo n.º 9
0
void encode_file(FILE *fpc, FILE *fpd, FILE *fpo) {
	size_t n, x;
	char cont[CONTSIZE], data[DATASIZE];
	struct stash_hdr shdr = {0};

	// Write restricted area
	n = fread(cont, 1, STARTBYTE, fpc);
	fwrite(cont, 1, (int)n, fpo);

	// Write header
	shdr.len = get_filesize(fpd);
	memset(cont, 0, sizeof(cont));
	n = fread(cont, 1, sizeof(struct stash_hdr)<<3, fpc);
	encode_data(cont, (int)n, (unsigned char *)&shdr, sizeof(struct stash_hdr), 0); 
	fwrite(cont, 1, n, fpo);

	// Write data
	while((n = fread(cont, 1, sizeof(cont), fpc))) {
		if((x = fread(data, 1, n>>3, fpd)) > 0)
			encode_data(cont, (int)x<<3, data, (int)x, 1);

		fwrite(cont, 1, (int)n, fpo);
	}

	return;
}
Ejemplo n.º 10
0
int main(int argc, char *argv[])
{
	FILE *fp;
	u8 bfr[ALIGNMENT];

	get_args(argc, argv);

	elf_size = get_filesize(elf_name);
	elf = mmap_file(elf_name);

	parse_elf();

	meta_header_size = 0x80 + ehdr.e_phnum * (0x30 + 0x20 + 0x60) + 0x30;
	info_offset = 0x70;
	elf_offset = 0x90;
	phdr_offset = elf_offset + ehdr.e_ehsize;
	sec_offset = round_up(phdr_offset + ehdr.e_phentsize * ehdr.e_phnum, ALIGNMENT);
	version_offset = round_up(sec_offset + ehdr.e_phnum *  0x20, ALIGNMENT);
	ctrl_offset = round_up(version_offset + 0x10, ALIGNMENT);
	meta_offset = round_up(ctrl_offset + 0x70, ALIGNMENT);
	header_size = round_up(meta_offset + meta_header_size, 0x80);

	if (compression)
		compress_elf();
	else
		fill_phdr_map();
	
	build_sce_hdr();
	build_info_hdr();
	build_ctrl_hdr();
	build_sec_hdr();
	build_version_hdr();
	build_meta_hdr();

	self = malloc(header_size + elf_size);
	memset(self, 0, header_size + elf_size);

	build_hdr();
	write_elf();
	calculate_hashes();
	sign_hdr();

	sce_encrypt_data(self);
	sce_encrypt_header(self, &ks);

	fp = fopen(self_name, "wb");
	if (fp == NULL)
		fail("fopen(%s) failed", self_name);

	if (fwrite(self, header_size + compressed_size, 1, fp) != 1)
		fail("unable to write self");

	memset(bfr, 0, sizeof bfr);
	fwrite(bfr, round_up(compressed_size, ALIGNMENT) - compressed_size, 1, fp);

	fclose(fp);

	return 0;
}
Ejemplo n.º 11
0
//@imgVersion: if success, then 0--> oldest version; 1-->img header + n * (item header + item body); 2 --> img header + n * item header + n * item body
static int _img_unpack_check_img_header(FILE* fdResImg, const char* const path_src, 
                int needCheckCrc, AmlResImgHead_t* pImgHead, int* imgVersion)
{
        int ret = 0;
        const unsigned ImgFileSz = get_filesize(path_src);

        if(ImgFileSz <= IMG_HEAD_SZ){
                errorP("file size 0x%x too small\n", ImgFileSz);
                return __LINE__;
        }

        unsigned thisReadSz = ITEM_READ_BUF_SZ > ImgFileSz ? ImgFileSz : ITEM_READ_BUF_SZ;
        unsigned actualReadSz = 0;

        actualReadSz = fread(pImgHead, 1, thisReadSz, fdResImg);
        if(actualReadSz != thisReadSz){
                errorP("Want to read 0x%x, but only read 0x%x\n", thisReadSz, actualReadSz);
                return __LINE__;
        }

        if(!strncmp(AML_RES_IMG_V1_MAGIC, (char*)pImgHead->magic, AML_RES_IMG_V1_MAGIC_LEN))
        {//new version magic matched
                if(ImgFileSz != pImgHead->imgSz){
                        errorP("error, image size in head 0x%x != fileSz 0x%x\n", pImgHead->imgSz, ImgFileSz);
                        return __LINE__;
                }
                if(AML_RES_IMG_VERSION_V2 < pImgHead->version){
                        errorP("Error, version 0x%x not supported\n", pImgHead->version);
                        return __LINE__;
                }
                *imgVersion = pImgHead->version;

                if(needCheckCrc)
                {
                        ret = check_img_crc(fdResImg, 4, pImgHead->crc);
                        if(ret){
                                errorP("Error when check crc\n");
                                return __LINE__;
                        }
                }
                unsigned ImgHeadSz = IMG_HEAD_SZ;
                if(AML_RES_IMG_VERSION_V2 == pImgHead->version) ImgHeadSz += pImgHead->imgItemNum * ITEM_HEAD_SZ;
                fseek(fdResImg, ImgHeadSz, SEEK_SET);//seek escape the image header to prepare for reading item header
        }
        else
        {
                AmlResItemHead_t* pItemHeadInfo = (AmlResItemHead_t*)pImgHead;
                debugP("magic error, try old version image.\n");
                if(IH_MAGIC != pItemHeadInfo->magic){
                        errorP("magic err, old version image header not item header!\n");
                        return __LINE__;
                }
                *imgVersion = 0;
                fseek(fdResImg, 0, SEEK_SET);//seek escape the image header to prepare for reading item header
        }

        debugP("res-img ver is 0x%x\n", *imgVersion);
        return 0;
}
Ejemplo n.º 12
0
box_param_t * gene_boxbyType( int fd, Byte8_t offset, Byte8_t length, char TBox[])
{
  Byte8_t pos;
  Byte_t *data;
  Byte8_t boxlen, headlen;
  char *boxtype;
  box_param_t *foundbox;

  
  if( length==0){ /* set the max length*/
    if( (length = get_filesize( fd) - offset) <= 0)
      return NULL;
  }

  pos = offset;
  while( pos < offset+length-7){ /* LBox+TBox-1=7*/
    
    /* read LBox and TBox*/
    if((data = fetch_bytes( fd, pos, 8))){
      headlen = 8;
      boxlen = (Byte8_t)big4(data);
      boxtype = (char *)(data+4);

      if( boxlen == 1){
	Byte_t *data2;
	headlen = 16;
	/* read XLBox*/
	if((data2 = fetch_bytes( fd, pos+8, 8))){
	  boxlen = big8(data2);
	  free(data2);
	}
	else{
	  fprintf( FCGI_stderr, "Error: error in gene_boxbyType( %d, %lld, %lld, %s)\n", fd, offset, length, TBox);
	  return NULL;
	}
      }
      if( strncmp ( boxtype, TBox, 4) == 0){
	foundbox = (box_param_t *)malloc( sizeof( box_param_t));
	foundbox->fd = fd;
	foundbox->offset = pos;
	foundbox->headlen = headlen;
	foundbox->length = boxlen;
	strncpy( foundbox->type, TBox, 4);
	foundbox->next = NULL;
	free( data);
	return foundbox;
      }
      free( data);
    }
    else{
      fprintf( FCGI_stderr, "Error: error in gene_boxbyType( %d, %lld, %lld, %s)\n", fd, offset, length, TBox);
      return NULL;
    }
    pos+= boxlen;
  }
  fprintf( FCGI_stderr, "Error: Box %s not found\n", TBox);

  return NULL;
}
Ejemplo n.º 13
0
int main()
{
	printf("FILESIZE: %d-bytes\n", get_filesize("tmp.txt"));
	#if defined(__linux)
		system("wc -c tmp.txt");
	#endif
	return 0;
}
Ejemplo n.º 14
0
char* file_block_decompose(const char* filename, uint64_t& size,
                           uint64_t& file_size, uint64_t& offset, MPI_Comm comm,
                           uint64_t alignment, uint32_t extra) {
  // get size of input file
  file_size = get_filesize(filename);

  // get communication parameters
  int32_t p, rank;
  MPI_Comm_size(comm, &p);
  MPI_Comm_rank(comm, &rank);

  const uint64_t num_aligned_blocks = (file_size + alignment - 1) / alignment;
  const uint32_t mod = num_aligned_blocks % p;

  const uint64_t proc_num_aligned_blocks =
      num_aligned_blocks / static_cast<uint64_t>(p) +
      (static_cast<uint64_t>(rank) < mod);

  const uint64_t proc_size = proc_num_aligned_blocks * alignment;

  if (static_cast<uint32_t>(rank) < mod) {
    offset = rank * proc_num_aligned_blocks * alignment;
  } else {
    offset = mod * (proc_num_aligned_blocks + 1) * alignment +
             (rank - mod) * proc_num_aligned_blocks * alignment;
  }

  if (rank < p - 1) {
    size = proc_size;
  } else {
    size = proc_size - (alignment - (file_size % alignment));
  }

  if (rank == 0) {
    fprintf(stdout, "Filesize %zu and block size %zu\n", file_size, size);
  }

  // open file
  std::ifstream t(filename);

  t.seekg(offset);
  char* data;
  try {
    data = new char[size + extra];
  } catch (std::bad_alloc& ba) {
    return NULL;
  }

  if (rank < p - 1) {
    t.readsome(data, size + extra);
  } else {
    t.readsome(data, size);
    for (uint32_t i = 0; i < extra; i++) {
      data[size + i] = 0;
    }
  }
  return data;
}
Ejemplo n.º 15
0
void send_file(int sockfd, int* clientlen, struct sockaddr_in* clientaddr, Request* req) {
  int last_sent = -1;
  FILE *fp = open_file(req->filename);
  int file_size = get_filesize(fp);
  int final_seq_num = (file_size / DATASIZE); // this accounts for 0 byte DataPkt for file_size % 512 == 0
  int window_min = 0;
  // window_max can't be greater than final_seq_num
  int window_max = (req->window_size - 1 > final_seq_num) ? final_seq_num : (req->window_size - 1);
  int num_timeouts = 0;
  int n;
  fd_set read_set;
  struct timeval timeout;
  // printf("data size is %d and final_seq_num is %d\n", file_size, final_seq_num);
  // printf("window_size is %d and window_max is %d\n", req->window_size, window_max);
  // printf("about to send pkts\n");
  last_sent = send_pkts(fp, file_size, last_sent, window_max, sockfd, clientlen, clientaddr);

  while (1) {
    DataPkt ack;
    bzero(ack.data, DATASIZE);
    FD_ZERO(&read_set);
    timeout.tv_sec = 0;
    timeout.tv_usec = TIMEOUT_USEC;
    FD_SET(sockfd, &read_set);
    n = select(sockfd + 1, &read_set, NULL, NULL, &timeout);

    if (n == 0) { // timed out
      // printf("timed out %d times\n", num_timeouts);
      num_timeouts++;

      if (num_timeouts == MAX_TIMEOUTS) {
        return; // stop communication
      }
      last_sent = send_pkts(fp, file_size, window_min - 1, window_max, sockfd, clientlen, clientaddr);
    }
    else {
      n = recvfrom(sockfd, (char *) &ack, (TYPEWINDOWSEQSIZE * 2), 0,
         (struct sockaddr *) clientaddr, clientlen);
      if (n < 0)
        error("ERROR in recvfrom");
      // printf("got ack for pkt %d\n", ack.seq_num);
      if (ack.seq_num >= window_min) { // ACK is new
        if (ack.seq_num == final_seq_num) {
          // printf("TRANSFER DONE!\n");
          return; // completed
        }

        // adjust window accordingly
        window_min = ack.seq_num + 1;
        window_max = (window_min + (req->window_size - 1) > final_seq_num) ? final_seq_num : (window_min + (req->window_size - 1));
        // printf("window_min is now %d and window_max is now %d\n", window_min, window_max);
        // printf("sending next set of pkts\n");
        last_sent = send_pkts(fp, file_size, last_sent, window_max, sockfd, clientlen, clientaddr);
        // printf("last sent %d\n", last_sent);
      }
    }
  }
}
Ejemplo n.º 16
0
/*
 * Convert 'fosfat_file_t' to 'struct stat'.
 */
static struct stat *
in_stat (fosfat_file_t *file, const char *path)
{
  struct stat *st;
  struct tm time;

  st = calloc (1, sizeof (struct stat));
  if (!st)
    return NULL;

  memset (&time, 0, sizeof (time));

  /* Directory, symlink or file */
  if (file->att.isdir)
  {
    st->st_mode = S_IFDIR | FOS_DIR;
    st->st_nlink = 2;
  }
  else if (file->att.islink)
  {
    st->st_mode = S_IFLNK | FOS_DIR;
    st->st_nlink = 2;
  }
  else
  {
    st->st_mode = S_IFREG | FOS_FILE;
    st->st_nlink = 1;
  }

  /* Size */
  st->st_size = get_filesize (file, path);

  /* Time */
  time.tm_year = file->time_r.year - 1900;
  time.tm_mon  = file->time_r.month - 1;
  time.tm_mday = file->time_r.day;
  time.tm_hour = file->time_r.hour;
  time.tm_min  = file->time_r.minute;
  time.tm_sec  = file->time_r.second;
  st->st_atime = mktime(&time);
  time.tm_year = file->time_w.year - 1900;
  time.tm_mon  = file->time_w.month - 1;
  time.tm_mday = file->time_w.day;
  time.tm_hour = file->time_w.hour;
  time.tm_min  = file->time_w.minute;
  time.tm_sec  = file->time_w.second;
  st->st_mtime = mktime(&time);
  time.tm_year = file->time_c.year - 1900;
  time.tm_mon  = file->time_c.month - 1;
  time.tm_mday = file->time_c.day;
  time.tm_hour = file->time_c.hour;
  time.tm_min  = file->time_c.minute;
  time.tm_sec  = file->time_c.second;
  st->st_ctime = mktime(&time);

  return st;
}
Ejemplo n.º 17
0
size_t load_from_raw(std::string name_file,T*&buffer)
{
    size_t size_file = get_filesize(name_file);
    size_t cnt_items = size_file / sizeof(T);
    if (buffer==NULL)
        buffer = new T[cnt_items];
    std::ifstream is(name_file, std::ifstream::in | std::ifstream::binary);
    is.read(reinterpret_cast<char *>(buffer),size_file);
    return cnt_items;
}
Ejemplo n.º 18
0
 virtual void open_file() {
     if (!use_mmap) {
         filedesc = iomgr->open_session(filename.c_str(), false);
     } else {
         mmap_length = get_filesize(filename);
         filedesc = open(filename.c_str(), O_RDWR);
         mmap_file = (VertexDataType *) mmap(NULL, mmap_length, PROT_WRITE | PROT_READ, MAP_SHARED, filedesc, 0);
         assert(mmap_file);
     }
 }
Ejemplo n.º 19
0
size_t load_vector_from_raw(std::string name_file,std::vector<T>& buffer)
{
    size_t size_file = get_filesize(name_file);
    size_t cnt_items = size_file / sizeof(T);
    buffer.resize(cnt_items);

    std::ifstream is(name_file, std::ifstream::in | std::ifstream::binary);
    is.read(reinterpret_cast<char *>(&buffer[0]),size_file);
    return cnt_items;
}
Ejemplo n.º 20
0
/*
 * FUSE : read the data of a file.
 *
 * path         (foo/bar)
 * buf          buffer for put the data
 * size         size in bytes
 * offset       offset in bytes
 * fi           not used
 * return the size
 */
static int
fos_read (const char *path, char *buf, size_t size,
          off_t offset, struct fuse_file_info *fi)
{
  int res = -ENOENT;
  int length;
  char *location;
  uint8_t *buf_tmp;
  fosfat_file_t *file = NULL;

  (void) fi;

  location = trim_fosname (path);

  /* Get the stats and test if it is a file */
  file = fosfat_get_stat (fosfat, location);
  if (!file)
    goto out;

  if (file->att.isdir)
    goto out;

  length = get_filesize (file, location);

  if (offset < length)
  {
    /* Fix the size in function of the offset */
    if (offset + (signed) size > length)
      size = length - offset;

    /* Read the data */
    buf_tmp = get_buffer (file, location, offset, size);

    /* Copy the data for FUSE */
    if (buf_tmp)
    {
      memcpy (buf, buf_tmp, size);
      free (buf_tmp);
      res = size;
    }
  }
  else
    res = 0;

 out:
  if (file)
    free (file);
  if (location)
    free (location);

  return res;
}
Ejemplo n.º 21
0
 virtual void open_file(std::string base_filename) {
     filename = filename_degree_data(base_filename);
     modified = false;
     if (!use_mmap) {
         iomgr->allow_preloading(filename);
         filedesc = iomgr->open_session(filename.c_str(), false);
     } else {
         mmap_length = get_filesize(filename);
         filedesc = open(filename.c_str(), O_RDWR);
         mmap_file = (degree *) mmap(NULL, mmap_length, PROT_READ | PROT_WRITE, MAP_SHARED, filedesc, 0);
         assert(mmap_file);
     }
 }
Ejemplo n.º 22
0
/**
 * Read Palm Database from open file stream.
 * @param *pdb_f open file stream of PDB file.
 * @param *pdb pointer to location that stores Palm Database.
 * @return uint8_t exit status. Either EXIT_SUCCESS, EXIT_FAILURE_PDB_HEADER 
 *    or EXIT_FAILURE_PDB_RECORDS.
 */
uint8_t read_pdb(FILE *pdb_f, pdb_t *pdb)
{
	init_pdb(pdb);
	pdb->filesize = get_filesize(pdb_f);

	if (!read_pdb_header(pdb_f, pdb))
		return EXIT_FAILURE_PDB_HEADER;

	if (!read_pdb_records(pdb_f, pdb))
		return EXIT_FAILURE_PDB_RECORDS;

	return EXIT_SUCCESS;
}
Ejemplo n.º 23
0
/*****************************************************************************
 * loadSamplesDataIntoMemory() --
 *            Helpler function to simplify life and
 *            load data into memory in one shot.
 *
 * @param int numberOfSamples -- The number of samples you would like to
 *                               load.
 *          
 *        unsigned char* dataArray -- The traffic data array where 
 *                                    suspicious flows are stored in memory.
 *        
 *        int* sampleIndexArray -- 
 *                     Used to keep record of the flows in dataArray.
 *                     The index into this array is the sample #,
 *                     the value inside sampleIndexArray[i] is the
 *                     index into dataArray where this suspicous flow 
 *                     begins.
 *        char* dataArrayInputFileName --
 *                     Input file name of the dataArray (raw data).
 *        char* offsetArrayInputFilename --
 *                     Input file name of the offsetArray - A file that
 *                     contains binary integer data of offsets.
 * @return int -- 1 if successful load, 0 otherwise.
 */
int loadSamplesDataIntoMemory(/*IN*/int numberOfSamples, /*OUT*/unsigned char* dataArray, /*OUT*/int* sampleIndexArray, /*IN*/char* dataArrayInputFileName, /*IN*/char* offsetArrayInputFileName)
{
	FILE* dataInputFile;
	FILE* offsetInputFile;
	
	dataInputFile = fopen( dataArrayInputFileName, "r" );
	fread( dataArray, 1, get_filesize( dataArrayInputFileName ), dataInputFile );
	fclose( dataInputFile );
	offsetInputFile = fopen( offsetArrayInputFileName, "r" );
	fread( sampleIndexArray, sizeof(int), numberOfSamples, offsetInputFile );

	return 1;
}
Ejemplo n.º 24
0
int lz77_decode(FILE* in,FILE* out)
{
  unsigned char* nbuf = 0,* ori_buf = 0,* head = 0,* tail = 0;
  long ori_size = 0,nsize = 0;

  if(! in || ! out)return 1;

  get_filesize(in,nsize); // get file size.
  if(nsize <= 0)return 1;

  ori_size  = (fgetc(in) & 0xff) << 24;
  ori_size += (fgetc(in) & 0xff) << 16;
  ori_size += (fgetc(in) & 0xff) <<  8;
  ori_size += (fgetc(in) & 0xff) <<  0;
  if(ori_size <= 0)return 1;

  nbuf = (unsigned char*)malloc(nsize* sizeof(unsigned char));
  if(! nbuf)return 1;

  ori_buf = (unsigned char*)malloc(ori_size* sizeof(unsigned char));
  if(ori_buf){
    head = tail = ori_buf; // store head and tail of slide-window.

    if(fread(nbuf,sizeof(unsigned char),nsize,in) == (nsize - 4)){
      unsigned char* pt  = nbuf;

      for(;;){
        if(pt >= (nbuf + nsize - 4))break;
        if(! pt[0] && ! pt[1] ){ // \0\0\?
          tail[0] = pt[2];
          tail++;
          pt += 3;
        } else { // \?\?
          unsigned int offset = 0,length = 0,i;
          offset  =  pt[0] << LENGTHBITS;
          offset += (pt[1] >> LENGTHBITS) & 0x0f;
          length  =  pt[1] & 0x0f;
          //fprintf(stderr,"{%d - %d}",offset,length);
          for(i = 0;i < length;i++){
            tail[i] = ((unsigned char*)(tail - offset))[i];
          }
          tail += length;
          pt += 2;
        }
      }
      fwrite(ori_buf,sizeof(unsigned char),ori_size,out);
    }

    free(ori_buf);
  }
Ejemplo n.º 25
0
metadatalist_param_t * const_metadatalist( int fd)
{
  metadatalist_param_t *metadatalist;
  metadata_param_t *metabin;
  boxlist_param_t *toplev_boxlist;
  box_param_t *box, *next;
  placeholderlist_param_t *phldlist;
  placeholder_param_t *phld;
  Byte8_t idx;
  Byte8_t filesize;

  if(!(filesize = (Byte8_t)get_filesize( fd)))
    return NULL;
  
  if( !(toplev_boxlist = get_boxstructure( fd, 0, filesize))){
    fprintf( FCGI_stderr, "Error: Not correctl JP2 format\n");
    return NULL;
  }
  
  phldlist = gene_placeholderlist();
  metadatalist = gene_metadatalist();

  box = toplev_boxlist->first;
  idx = 0;
  while( box){
    next = box->next;
    if( strncmp( box->type, "jP  ",4)!=0 && strncmp( box->type, "ftyp",4)!=0 && strncmp( box->type, "jp2h",4)!=0){
      boxlist_param_t *boxlist = NULL;
      boxcontents_param_t *boxcontents = NULL;

      phld = gene_placeholder( box, ++idx);
      insert_placeholder_into_list( phld, phldlist);

      boxlist = get_boxstructure( box->fd, get_DBoxoff( box), get_DBoxlen(box));
      if( !boxlist)
	boxcontents = gene_boxcontents( get_DBoxoff( box), get_DBoxlen(box));
      
      delete_box_in_list( &box, toplev_boxlist);
      metabin = gene_metadata( idx, boxlist, NULL, boxcontents);
      insert_metadata_into_list( metabin, metadatalist);
    }
    box = next;
  }

  metabin = gene_metadata( 0, toplev_boxlist, phldlist, NULL);
  insert_metadata_into_list( metabin, metadatalist);

  return metadatalist;
}
Ejemplo n.º 26
0
	//-----------------------------------------------------------------//
	bool unzip::get_file(uint32_t index, char* buff)
	{
		if(index < static_cast<uint32_t>(files_.size())) {
			unzSetOffset(hnd_, files_[index].ofs_);

			if(unzOpenCurrentFile(hnd_) != UNZ_OK) return false;

			size_t sz = get_filesize(index);
			unzReadCurrentFile(hnd_, buff, sz);
			unzCloseCurrentFile(hnd_);

			return true;
		}
		return false;
	}
Ejemplo n.º 27
0
void app_tftp(char *arg)
{
    struct command_t *commands;
    struct note_t *note;
    struct pico_tftp_session *session;
    int is_server_enabled = 0;
    int filesize;

    family = IPV6_MODE ? PICO_PROTO_IPV6 : PICO_PROTO_IPV4;

    commands = parse_arguments(arg);
    while (commands) {

        if (toupper(commands->operation) != 'S')
            note = transfer_prepare(&session, commands->operation, commands->filename, &commands->server_address, family);

        switch (commands->operation) {
        case 'S':
        case 's':
            if (!is_server_enabled) {
                pico_tftp_listen(PICO_PROTO_IPV4, (commands->operation == 'S') ? tftp_listen_cb_opt : tftp_listen_cb);
                is_server_enabled = 1;
            }

            break;
        case 'T':
            filesize = get_filesize(commands->filename);
            if (filesize < 0) {
                fprintf(stderr, "TFTP: unable to read size of file %s\n", commands->filename);
                exit(3);
            }

            pico_tftp_set_option(session, PICO_TFTP_OPTION_FILE, filesize);
            start_tx(session, commands->filename, short_be(PICO_TFTP_PORT), cb_tftp_tx_opt, note);
            break;
        case 't':
            start_tx(session, commands->filename, short_be(PICO_TFTP_PORT), cb_tftp_tx, note);
            break;
        case 'R':
            pico_tftp_set_option(session, PICO_TFTP_OPTION_FILE, 0);
            start_rx(session, commands->filename, short_be(PICO_TFTP_PORT), cb_tftp_rx_opt, note);
            break;
        case 'r':
            start_rx(session, commands->filename, short_be(PICO_TFTP_PORT), cb_tftp_rx, note);
        }
        commands = commands->next;
    }
}
Ejemplo n.º 28
0
void IdentifyChromosomes(const string& chrom_file,
                         vector<string>& chrom_files) {
  fprintf(stderr, "[IDENTIFYING CHROMS] ");
  if (isdir(chrom_file.c_str())) {
    read_dir(chrom_file, "fa", chrom_files);
  } else {
    chrom_files.push_back(chrom_file);
  }

  fprintf(stderr, "[DONE]\n");
  fprintf(stderr, "chromosome files found (approx size):\n");
  for (uint32_t i = 0; i < chrom_files.size(); ++i) {
    fprintf(stderr, "%s (%.2lfMbp)\n", chrom_files[i].c_str(),
            roundf(get_filesize(chrom_files[i]) / 1e06));
  }
}
Ejemplo n.º 29
0
index_param_t * parse_jp2file( int fd)
{
  index_param_t *jp2idx;
  box_param_t *cidx;
  metadatalist_param_t *metadatalist;
  boxlist_param_t *toplev_boxlist;
  Byte8_t filesize;

  if( !(filesize = (Byte8_t)get_filesize( fd)))
    return NULL;

  if( !(toplev_boxlist = get_boxstructure( fd, 0, filesize))){
    fprintf( FCGI_stderr, "Error: Not correctl JP2 format\n");
    return NULL;
  }

  if( !check_JP2boxidx( toplev_boxlist)){
    fprintf( FCGI_stderr, "Index format not supported\n");
    delete_boxlist( &toplev_boxlist);
    return NULL;
  }

  if( !(cidx = search_box( "cidx", toplev_boxlist))){
    fprintf( FCGI_stderr, "Box cidx not found\n");
    delete_boxlist( &toplev_boxlist);
    return NULL;
  }

  jp2idx = (index_param_t *)opj_malloc( sizeof(index_param_t));

  if( !set_cidxdata( cidx, jp2idx)){
    fprintf( FCGI_stderr, "Error: Not correctl format in cidx box\n");
    opj_free(jp2idx);
    delete_boxlist( &toplev_boxlist);
    return NULL;
  }
  delete_boxlist( &toplev_boxlist);

  metadatalist = const_metadatalist( fd);
  jp2idx->metadatalist = metadatalist;

#ifndef SERVER
    fprintf( logstream, "local log: code index created\n");
#endif

  return jp2idx;
}
Ejemplo n.º 30
0
void tftp_listen_cb_opt(union pico_address *addr, uint16_t port, uint16_t opcode, char *filename, int32_t len)
{
    struct note_t *note;
    struct pico_tftp_session *session;
    int options;
    uint8_t timeout;
    int32_t filesize;
    int ret;

    printf("TFTP listen callback (OPTIONS) from remote port %" PRIu16 ".\n", short_be(port));
    /* declare the options we want to support */
    ret = pico_tftp_parse_request_args(filename, len, &options, &timeout, &filesize);
    if (ret)
        pico_tftp_reject_request(addr, port, TFTP_ERR_EOPT, "Malformed request");

    if (opcode == PICO_TFTP_RRQ) {
        printf("Received TFTP get request for %s\n", filename);
        note = transfer_prepare(&session, 'T', filename, addr, family);

        if (options & PICO_TFTP_OPTION_TIME)
            pico_tftp_set_option(session, PICO_TFTP_OPTION_TIME, timeout);

        if (options & PICO_TFTP_OPTION_FILE) {
            ret = get_filesize(filename);
            if (ret < 0) {
                pico_tftp_reject_request(addr, port, TFTP_ERR_ENOENT, "File not found");
                return;
            }

            pico_tftp_set_option(session, PICO_TFTP_OPTION_FILE, ret);
        }

        start_tx(session, filename, port, cb_tftp_tx_opt, note);
    } else { /* opcode == PICO_TFTP_WRQ */
        printf("Received TFTP put request for %s\n", filename);

        note = transfer_prepare(&session, 'R', filename, addr, family);
        if (options & PICO_TFTP_OPTION_TIME)
            pico_tftp_set_option(session, PICO_TFTP_OPTION_TIME, timeout);

        if (options & PICO_TFTP_OPTION_FILE)
            pico_tftp_set_option(session, PICO_TFTP_OPTION_FILE, filesize);

        start_rx(session, filename, port, cb_tftp_rx_opt, note);
    }
}