コード例 #1
0
ファイル: chmunpack.c プロジェクト: dreamee21/SomaVaccine
int cli_chm_open(int fd, const char *dirname, chm_metadata_t *metadata, cli_ctx *ctx)
{
	struct stat statbuf;
	int retval;

	cli_dbgmsg("in cli_chm_open\n");
	
	if ((retval = chm_init_metadata(metadata)) != CL_SUCCESS) {
		return retval;
	}

	if (fstat(fd, &statbuf) == 0) {
		if (statbuf.st_size < CHM_ITSF_MIN_LEN) {
			return CL_ESTAT;
		}
		metadata->m_length = statbuf.st_size;
		metadata->map = fmap(fd, 0, metadata->m_length);
		if (!metadata->map) {
			return CL_EMAP;
		}
	} else {
	    char err[128];
	    cli_warnmsg("fstat() failed: %s\n", cli_strerror(errno, err, sizeof(err)));
	    return CL_ESTAT;
	}

	if (!itsf_read_header(metadata)) {
		goto abort;
	}
	itsf_print_header(&metadata->itsf_hdr);

	if (!itsp_read_header(metadata, metadata->itsf_hdr.dir_offset)) {
		goto abort;
	}
	itsp_print_header(&metadata->itsp_hdr);

	metadata->chunk_offset = metadata->itsf_hdr.dir_offset+CHM_ITSP_LEN;
	
	/* TODO: need to check this first calculation,
		currently have no files of this type */
	if (metadata->itsp_hdr.index_head > 0) {
		metadata->chunk_offset += metadata->itsp_hdr.index_head * metadata->itsp_hdr.block_len;
	}

	metadata->num_chunks = metadata->itsp_hdr.index_tail - metadata->itsp_hdr.index_head + 1;
	
	/* Versions before 3 didn't have a data_offset */
	/* TODO: need to check this calculation,
		 currently have no files of this type */
	if (metadata->itsf_hdr.version < 3) {
		metadata->itsf_hdr.data_offset = metadata->itsf_hdr.dir_offset + CHM_ITSP_LEN +
				(metadata->itsp_hdr.block_len*metadata->itsp_hdr.num_blocks);
	}
	
	while (metadata->num_chunks) {
		if (read_chunk(metadata) != CL_SUCCESS) {
			cli_dbgmsg("read_chunk failed\n");
			goto abort;
		}
		if (read_control_entries(metadata) == FALSE) {
			goto abort;
		}
		metadata->num_chunks--;
		metadata->chunk_offset += metadata->itsp_hdr.block_len;
	}

	if (!metadata->sys_content.length || !metadata->sys_control.length || !metadata->sys_reset.length) {
		cli_dbgmsg("sys file missing\n");
		goto abort;
	}
	
	metadata->ufd = chm_decompress_stream(fd, metadata, dirname, ctx);
	if (metadata->ufd == -1) {
		goto abort;
	}
	
	metadata->chunk_entries = 0;
	metadata->chunk_data = NULL;
	metadata->chunk_offset = metadata->itsf_hdr.dir_offset+CHM_ITSP_LEN;
	metadata->num_chunks = metadata->itsp_hdr.index_tail - metadata->itsp_hdr.index_head + 1;

	return CL_SUCCESS;

abort:
	funmap(metadata->map);
	return CL_EFORMAT;
}
コード例 #2
0
ファイル: tinys.c プロジェクト: dan-boa/EE450
int main(void)
{
    char in[3000],  sent[500], code[50], file[200], mime[100], moved[200], length[100], auth[200], auth_dir[500], start[100], end[100];
    char *result=NULL, *hostname, *hostnamef, *lines, *ext=NULL, *extf, *auth_dirf=NULL, *authf=NULL, *rangetmp;
    int buffer_chunks;
    long filesize, range=0;

    get_conf();
    create_and_bind();

    //Important stuff happens here.

    while(1) {
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
            perror("accept");
            continue;
        }

        if (!fork()) {
            close(sockfd);
        if (read(new_fd, in, 3000) == -1) {
    	perror("recive");
        } else {
    	lines = strtok(in, "\n\r");
    	do {
    	    hostname = strtok(NULL, "\n\r");
    		if (hostname[0] == 'R' && hostname[1] == 'a' && hostname[2] == 'n' && hostname[3] == 'g' && hostname[4] == 'e') {
    			rangetmp = hostname;
    			strcpy(code, "206 Partial Content");
    		}
    	} while (hostname[0] != 'H' || hostname[1] != 'o' || hostname[2] != 's' || hostname[3] != 't');
    	hostnamef = strtok(hostname, " ");
    	hostnamef = strtok(NULL, " ");
    	result = strtok(lines, " ");
    	result = strtok(NULL, " ");
    	if (strcmp(code, "206 Partial Content") == 0 ) {
    		rangetmp = strtok(strpbrk(rangetmp, "="), "=-");
    		range = atoi(rangetmp);
    	}

    	strcpy(file, result);
    	if (opendir(file)){
    	    if (file[strlen(file)-1] == '/'){
                    strcat(file, "/index.html");
    		openfile=fopen (file, "r");
                        if (openfile){
                            strcpy(code, "200 OK");
                        } else {
    		    //Here should be some kind of directory listing
    		    strcpy(file, "/404.html");
    		    openfile = fopen (file, "r");
    		    strcpy(code, "404 Not Found");
    		}
    	    } else {
    		strcpy(code, "301 Moved Permanently");
    		strcpy(moved, "Location: http://");
    		strcat(moved, hostnamef);
    		strcat(moved, result);
    		strcat(moved, "/");
    	    }
    	} else {
    	    openfile=fopen (file, "rb");
                if (openfile){
    		if (strlen(code) < 1) {
                    	strcpy (code, "200 OK");
    		}
                } else {
    		strcpy(file, "/404.html");
    		openfile = fopen (file, "r");
                    strcpy(code, "404 Not Found");
                }
                }
        }
        if (strcmp(code, "301 Moved Permanently") != 0){
    	fseek (openfile , 0 , SEEK_END);
                filesize = ftell (openfile);
        	rewind (openfile);
    	if (range > 0) {
    		sprintf(end, "%d", filesize);
    		filesize = filesize - range;
    		sprintf(start, "%d", range);
    		fseek (openfile , range , SEEK_SET);
    	}
    	buffer_chunks = filesize/1048576;
    	if(filesize%1048576 > 0){
    		buffer_chunks++;
    	}
    	sprintf(length, "%d", filesize);
    	buffer_counter = 0;
    	buffer = (char*) malloc (sizeof(char)*1048576);
        }

        if (strcmp(code, "404 Not Found") != 0 && strcmp(code, "301 Moved Permanently") !=0){
    	ext = strtok(file, ".");
            while(ext != NULL){
    	    ext = strtok(NULL, ".");
        	    if (ext != NULL){
    		extf = ext;
    	    }
    	}
        } else {
    	extf="html";
        }

        /* Maybe I should read mime types from a file. At least for now, add here what you need.*/

        if (strcmp(extf, "html") == 0){
    	strcpy (mime, "text/html");
            } else if(strcmp(extf, "jpg") == 0){
    	strcpy (mime, "image/jpeg");
        } else if(strcmp(extf, "gif") == 0){
    	strcpy (mime, "image/gif");
        } else if(strcmp(extf, "css") == 0){
    	strcpy (mime, "text/css");
        } else {
    	strcpy(mime, "application/octet-stream");
        }

        strcpy(sent, "HTTP/1.1 ");
        strcat(sent, code);
        strcat(sent, "\nServer: qshttpd 0.3.0\n");
        if(strcmp(code, "301 Moved Permanently") == 0){
    	strcat(sent, moved);
    	strcat(sent, "\n");
        }

        strcat(sent, "Content-Length: ");
        if(strcmp(code, "301 Moved Permanently") != 0){
            strcat(sent, length);
        } else {
    	strcat(sent, "0");
        }
        if(strcmp(code, "206 Partial Content") == 0) {
    	strcat(sent, "\nContent-Range: bytes ");
    	strcat(sent, start);
    	strcat(sent, "-");
    	strcat(sent, end);
    	strcat(sent, "/");
    	strcat(sent, end);
    }
        strcat(sent, "\nConnection: close\nContent-Type: ");
        strcat(sent, mime);
        strcat(sent, "; charset=");
        strcat(sent, charset);
        strcat(sent, "\n\n");
        write(new_fd, sent, strlen(sent));
    	while (buffer_counter < buffer_chunks) {
    		read_chunk();
    		write(new_fd, buffer, 1048576);
    	}
        close(new_fd);
            exit(0);
        }
        close(new_fd);
    }
    return 0;
}
コード例 #3
0
ファイル: chmunpack.c プロジェクト: rossguide/clamav-devel
int cli_chm_open(const char *dirname, chm_metadata_t *metadata, cli_ctx *ctx)
{
	STATBUF statbuf;
	int retval;

	cli_dbgmsg("in cli_chm_open\n");
	
	if ((retval = chm_init_metadata(metadata)) != CL_SUCCESS) {
		return retval;
	}

	metadata->map = *ctx->fmap;
	if(metadata->map->len < CHM_ITSF_MIN_LEN)
		return CL_ESTAT;
	metadata->m_length = metadata->map->len;

	if (!itsf_read_header(metadata)) {
		goto abort;
	}
	itsf_print_header(&metadata->itsf_hdr);

	if (!itsp_read_header(metadata, metadata->itsf_hdr.dir_offset)) {
		goto abort;
	}
	itsp_print_header(&metadata->itsp_hdr);

	metadata->chunk_offset = metadata->itsf_hdr.dir_offset+CHM_ITSP_LEN;
	
	/* TODO: need to check this first calculation,
		currently have no files of this type */
	if (metadata->itsp_hdr.index_head > 0) {
		metadata->chunk_offset += metadata->itsp_hdr.index_head * metadata->itsp_hdr.block_len;
	}

	metadata->num_chunks = metadata->itsp_hdr.index_tail - metadata->itsp_hdr.index_head + 1;
	
	/* Versions before 3 didn't have a data_offset */
	/* TODO: need to check this calculation,
		 currently have no files of this type */
	if (metadata->itsf_hdr.version < 3) {
		metadata->itsf_hdr.data_offset = metadata->itsf_hdr.dir_offset + CHM_ITSP_LEN +
				(metadata->itsp_hdr.block_len*metadata->itsp_hdr.num_blocks);
	}
	
	while (metadata->num_chunks) {
		if (read_chunk(metadata) != CL_SUCCESS) {
			cli_dbgmsg("read_chunk failed\n");
			goto abort;
		}
		if (read_control_entries(metadata) == FALSE) {
			goto abort;
		}
		metadata->num_chunks--;
		metadata->chunk_offset += metadata->itsp_hdr.block_len;
	}

	if (!metadata->sys_content.length || !metadata->sys_control.length || !metadata->sys_reset.length) {
		cli_dbgmsg("sys file missing\n");
		goto abort;
	}
	
	metadata->ufd = chm_decompress_stream(metadata, dirname, ctx);
	if (metadata->ufd == -1) {
		goto abort;
	}
	
	metadata->chunk_entries = 0;
	metadata->chunk_data = NULL;
	metadata->chunk_offset = metadata->itsf_hdr.dir_offset+CHM_ITSP_LEN;
	metadata->num_chunks = metadata->itsp_hdr.index_tail - metadata->itsp_hdr.index_head + 1;

	return CL_SUCCESS;

abort:
	return CL_EFORMAT;
}
コード例 #4
0
ファイル: tyindex.c プロジェクト: faircreek/tydemux
int open_file_tydemux(tystream_holder_t * tystream, char *filename) {

	chunknumber_t probe_hz;
	chunknumber_t chunk_lnr;
	chunknumber_t int_chunk_nr;
	vstream_t * vstream;
	chunk_t * chunk;


	if(infile) {
		printf("Error input file allready open\n");
		return(0);
	}
	//printf("%s:\n", filename);

	infile = open_input(filename, 0);
	if (infile == -1) {
		totalchunks= -1;		/* Total chunks */
		infile = 0;			/* FD of input file */
		chunk_in_buf = -1;

		printf("Error opening file\n");
		return(0);

	}


	tystream->tivo_probe = 1;

	probe_hz = totalchunks/10;

	tystream->tivo_probe = 1;
	tystream->tivo_version = V_2X;

	for(chunk_lnr=1, int_chunk_nr=0 ;  chunk_lnr < totalchunks; chunk_lnr = chunk_lnr + probe_hz, int_chunk_nr++ ){


		vstream = new_vstream();
		/* Lets fake it and do a read buffer */
		vstream->start_stream = (uint8_t *)malloc(sizeof(uint8_t) * CHUNK_SIZE);
		vstream->size = sizeof(uint8_t) * CHUNK_SIZE ;
		vstream->current_pos = vstream->start_stream;
		vstream->end_stream = vstream->start_stream + vstream->size;

		read_a_chunk(infile, chunk_lnr, vstream->start_stream);

		tystream->vstream = vstream;

		chunk = read_chunk(tystream, (int)chunk_lnr, 1);
		chunk_in_buf = chunk_lnr;

		if(chunk != NULL) {
			tystream->chunks = add_chunk(tystream, chunk, tystream->chunks);
		}

		free_vstream(vstream);
		tystream->vstream=NULL;

	}



	if(!tivo_probe_tystream(tystream)) {
		totalchunks= -1;		/* Total chunks */
		infile = 0;			/* FD of input file */
		chunk_in_buf = -1;
		return(0);
	}

	get_start_chunk_tivo(tystream);

	tystream->tivo_probe = 0;

	/* We assume that the first chunk to be used is chunk 0 */
	read_a_chunk(infile, (int)tystream->start_chunk, buf);
  	chunk_in_buf = (int)tystream->start_chunk;
  	return(1);
}
コード例 #5
0
ファイル: tyindex.c プロジェクト: faircreek/tydemux
void get_start_chunk_tivo(tystream_holder_t * tystream) {

	/* interation */
	int i;


	/* Marker */
	int gotit;

	/* Chunks */
	int chunk_lnr;
	chunk_t * chunk;
	vstream_t * vstream;


	chunk_lnr=0;
	gotit=0;
	vstream = new_vstream();
	/* Lets fake it and do a read buffer */
	vstream->start_stream = (uint8_t *)malloc(sizeof(uint8_t) * CHUNK_SIZE);
	vstream->size = sizeof(uint8_t) * CHUNK_SIZE ;
	vstream->current_pos = vstream->start_stream;
	vstream->end_stream = vstream->start_stream + vstream->size;
	tystream->vstream = vstream;


	while(1) {

		vstream->current_pos = vstream->start_stream;
		vstream->eof=0;

		read_a_chunk(infile, chunk_lnr, vstream->start_stream);

		chunk = read_chunk(tystream, (int64_t)chunk_lnr, 1);
		chunk_in_buf = chunk_lnr;

		if(chunk != NULL) {
			for(i=0; i < chunk->nr_records; i++){
				if(chunk->record_header[i].type == tystream->right_audio){
					gotit=1;
					break;
				}
			}
			free_chunk(chunk);
		}


		if(!gotit) {
			chunk_lnr++;
		} else {
			break;
		}
	}

	free_vstream(vstream);
	tystream->vstream=NULL;


	tystream->start_chunk = (int64_t)chunk_lnr;


	tystream->number_of_chunks = 0;
}
コード例 #6
0
ファイル: unyaffs.c プロジェクト: CyanogenGodbox/unyaffs
int main(int argc, char **argv) {
	int ch;
	int layout = 0;

	/* handle command line options */
	opt_list = 0;
	opt_verbose = 0;
	opt_skip = 0;
	while ((ch = getopt(argc, argv, "l:tsvVh?")) > 0) {
		switch (ch) {
			case 'l':
				if (optarg[0] < '0' ||
				    optarg[0] > '0' + max_layout ||
				    optarg[1] != '\0') usage();
				layout = optarg[0] - '0';
				break;
			case 't':
				opt_list = 1;
				break;
			case 'v':
				opt_verbose = 1;
				break;
			case 's':
				opt_skip = 1;
				break;
			case 'V':
				printf("V%s\n", VERSION);
				exit(0);
				break;
			case 'h':
			case '?':
			default:
				usage();
				break;
    		}
	}

	/* extract rest of command line parameters */
	if ((argc - optind) < 1 || (argc - optind) > 2)
		usage();

	if (strcmp(argv[optind], "-") == 0) {	/* image file from stdin ? */
		img_file = 0;
	} else {
		img_file = open(argv[optind], O_RDONLY);
		if (img_file < 0)
			prt_err(1, errno, "Open image file failed");
	}

	if (layout == 0) {
		detect_chunk_size();
	} else {
		chunk_size = possible_layouts[layout-1].chunk_size;
		spare_size = possible_layouts[layout-1].spare_size;
	}
	spare_data = data + chunk_size;

	// Skip first chunk
	if (opt_skip)
		lseek(img_file, chunk_size+spare_size, SEEK_SET);

	if ((argc - optind) == 2 && !opt_list) {
		if (mkdirpath(argv[optind+1]) < 0)
			prt_err(1, errno, "Can't mkdir %s", argv[optind+1]);
		if (chdir(argv[optind+1]) < 0)
			prt_err(1, errno, "Can't chdir to %s", argv[optind+1]);
	}

	umask(0);

	init_obj_list();
	while (read_chunk()) {
		process_chunk();
	}
	set_dirs_utime();
	close(img_file);
	return 0;
}
コード例 #7
0
ファイル: unlib.cpp プロジェクト: trietptm/usefulres
//--------------------------------------------------------------------------
int main(int argc,char *argv[]) {
  int i;
  fprintf(stderr,"ARM Library unpacker. Copyright 1997 by Ilfak Guilfanov. Version 1.00\n");
  if ( argc < 2 ) fatal("Usage: unlib libfile");
  strcpy(infile,argv[1]);
  FILE *fp = qfopen(infile,"rb");
  if ( fp == NULL ) fatal("Can't open library %s",infile);
  chunk_header_t hd;
  if ( qfread(fp, &hd, sizeof(hd)) != sizeof(hd)
        || (hd.ChunkFileId != AOF_MAGIC && hd.ChunkFileId != AOF_MAGIC_B) )
    fatal("Bad library format");
  if ( hd.ChunkFileId == AOF_MAGIC_B ) {            // BIG ENDIAN
    mf = 1;
    hd.max_chunks = swap(hd.max_chunks);
    hd.num_chunks = swap(hd.num_chunks);
  }

  chunk_entry_t *ce = qnewarray(chunk_entry_t,size_t(hd.max_chunks));
  if ( ce == NULL ) nomem("chunk entries (%d)",size_t(hd.max_chunks));
  qfread(fp, ce, sizeof(chunk_entry_t)*size_t(hd.max_chunks));
  if ( mf ) for ( i=0; i < hd.max_chunks; i++ ) swap_chunk_entry(ce+i);

  int vrsn = -1;
  int diry = -1;
  int data = 0;
  for ( i=0; i < hd.max_chunks; i++ ) {
    if ( ce[i].file_offset == 0 ) continue;
    if ( strncmp(ce[i].chunkId,LIB_DIRY,sizeof(ce[i].chunkId)) == 0 ) diry = i;
    if ( strncmp(ce[i].chunkId,LIB_VRSN,sizeof(ce[i].chunkId)) == 0 ) vrsn = i;
    if ( strncmp(ce[i].chunkId,LIB_DATA,sizeof(ce[i].chunkId)) == 0 ) data++;
  }
  if ( diry == -1 ) fatal("Can't find library directory!");
  if ( data == 0  ) fatal("No modules in the library!");
  if ( vrsn == -1 ) fatal("Can't determine library version!");
  ulong *version = (ulong *)read_chunk(fp,ce,vrsn);
  if ( mf ) *version = swap(*version);
  if ( *version != 1 ) fatal("Wrong library version (%ld)",*version);
  qfree(version);

  ulong *dir = (ulong *)read_chunk(fp,ce,diry);
  ulong *end = dir + size_t(ce[diry].size/4);
  while ( dir < end ) {
    ulong idx  = *dir++;
    /* ulong elen = */ *dir++;
    ulong dlen = *dir++;
    if ( mf ) {
      idx = swap(idx);
      dlen = swap(dlen);
    }
    if ( idx != 0 ) {
      printf("%ld. %s\n",idx,dir);
      strncpy(modname,(char *)dir,sizeof(modname));
      modname[sizeof(modname)-1] = '\0';
      void *core = read_chunk(fp,ce,idx);
      outfp = qfopen(modname,"wb");
      if ( outfp == NULL ) {
        warning("Can't open output file %s",modname);
      } else {
        qfwrite(outfp,core,size_t(ce[size_t(idx)].size));
        qfclose(outfp);
      }
      qfree(core);
    }
    dir += size_t(dlen/4);
  }
  qfree(dir);

  qfclose(fp);
  return 0;
}
コード例 #8
0
ファイル: wavconv.cpp プロジェクト: nekko1119/Sprout
int main(int argc, char* argv[]) {
	if (argc < 2) {
		std::cerr
			<< "#error missing parameter.\n"
			<< std::flush
			;
		return 0;
	}

	std::ifstream ifs(argv[1], std::ios_base::in | std::ios_base::binary);
	std::istreambuf_iterator<char> it(ifs);
	std::istreambuf_iterator<char> last;
	std::cout
		<< "#if defined(COMPOST_LOADING_SOURCE_VERSION)\n"
			"\n"
			"COMPOST_SRC_VERSION(0)\n"
			"\n"
			"#elif defined(COMPOST_LOADING_SOURCE_INFO)\n"
			"\n"
		;

	if (read_chunk(it) != "RIFF") {
		std::cerr
			<< "#error not RIFF file.\n"
			<< std::flush
			;
		return EXIT_FAILURE;
	}
	/*auto file_size = */read_int<std::uint32_t>(it);
	if (read_chunk(it) != "WAVE") {
		std::cerr
			<< "#error not WAVE format.\n"
			<< std::flush
			;
		return EXIT_FAILURE;
	}

	while (read_chunk(it) != "fmt ") {
		auto chunk_size = read_int<std::uint32_t>(it);
		std::advance(it, chunk_size);
	}
	auto fmt_size = read_int<std::uint32_t>(it);
	auto format_tag = read_int<std::uint16_t>(it);
	auto channels = read_int<std::uint16_t>(it);
	auto samples_per_sec = read_int<std::uint32_t>(it);
	auto bytes_per_sec = read_int<std::uint32_t>(it);
	auto block_size = read_int<std::uint16_t>(it);
	auto bits_per_sample = read_int<std::uint16_t>(it);
	std::cout
		<< format_tag << ",\n"
		<< channels << ",\n"
		<< samples_per_sec << ",\n"
		<< bytes_per_sec << ",\n"
		<< block_size << ",\n"
		<< bits_per_sample << ",\n"
		;
	std::advance(it, fmt_size - 16);

	while (read_chunk(it) != "data") {
		auto chunk_size = read_int<std::uint32_t>(it);
		std::advance(it, chunk_size);
	}
	auto data_size = read_int<std::uint32_t>(it);
	std::size_t size = data_size / (bits_per_sample / CHAR_BIT);
	std::cout
		<< size << "\n"
		<< "\n"
			"#elif defined(COMPOST_LOADING_SOURCE_DATA)\n"
			"\n"
		;
	if (size > 0) {
		if (bits_per_sample == 16) {
			for (std::size_t i = 1; i != size; ++i) {
				std::cout
					<< read_int<std::int16_t>(it) << ",\n"
					;
			}
			std::cout
				<< read_int<std::int16_t>(it) << "\n"
				;
		} else if (bits_per_sample == 8) {
			for (std::size_t i = 1; i != size; ++i) {
				std::cout
					<< read_int<std::uint8_t>(it) << ",\n"
					;
			}
			std::cout
				<< read_int<std::uint8_t>(it) << "\n"
				;
		}
	}

	std::cout
		<< "\n"
			"#endif\n"
		<< std::flush
		;
}
コード例 #9
0
ファイル: unyaffs.c プロジェクト: CyanogenGodbox/unyaffs
void process_chunk(void) {
	yaffs_ObjectHeader oh;
	yaffs_PackedTags2 *pt;
	object *obj, *eq_obj;
	int out_file, remain, s;

	oh = *(yaffs_ObjectHeader *)chunk_data;
	pt = (yaffs_PackedTags2 *)spare_data;

	if (pt->t.byteCount == 0xffffffff)	/* empty object */
		return;
	else if (pt->t.byteCount != 0xffff) {	/* not a new object */
		prt_err(0, 0, "Warning: Invalid header at chunk #%d, skipping...",
		        chunk_no);
		if (++warn_count >= MAX_WARN)
			prt_err(1, 0, "Giving up");
		return;
	}

	obj = add_object(&oh, pt);

	/* listing */
	if (opt_verbose)
		prt_node(obj->path_name, &oh);
	else if (opt_list)
		printf("%s\n", obj->path_name);
	if (opt_list) {
		if (oh.type == YAFFS_OBJECT_TYPE_FILE) {
			remain = oh.fileSize;	/* skip over data chunks */
			while(remain > 0) {
				if (!read_chunk())
					prt_err(1, 0, "Broken image file");
				remain -= pt->t.byteCount;
			}
		}
		return;
	}

	switch(oh.type) {
		case YAFFS_OBJECT_TYPE_FILE:
			remain = oh.fileSize;
			out_file = creat(obj->path_name, oh.yst_mode & STD_PERMS);
			if (out_file < 0)
				prt_err(1, errno, "Can't create file %s", obj->path_name);
			while(remain > 0) {
				if (!read_chunk())
					prt_err(1, 0, "Broken image file");
				s = (remain < pt->t.byteCount) ? remain : pt->t.byteCount;
				if (xwrite(out_file, chunk_data, s) < 0)
					prt_err(1, errno, "Can't write to %s", obj->path_name);
				remain -= s;
			}
			close(out_file);
			lchown(obj->path_name, oh.yst_uid, oh.yst_gid);
			if ((oh.yst_mode & EXTRA_PERMS) != 0 &&
			    chmod(obj->path_name, oh.yst_mode) < 0)
				prt_err(0, errno, "Warning: Can't chmod %s", obj->path_name);
			break;
		case YAFFS_OBJECT_TYPE_SYMLINK:
			if (symlink(oh.alias, obj->path_name) < 0)
				prt_err(1, errno, "Can't create symlink %s", obj->path_name);
			lchown(obj->path_name, oh.yst_uid, oh.yst_gid);
			break;
		case YAFFS_OBJECT_TYPE_DIRECTORY:
			if (pt->t.objectId != YAFFS_OBJECTID_ROOT &&
			    mkdir(obj->path_name, oh.yst_mode & STD_PERMS) < 0)
					prt_err(1, errno, "Can't create directory %s", obj->path_name);
			lchown(obj->path_name, oh.yst_uid, oh.yst_gid);
			if ((pt->t.objectId == YAFFS_OBJECTID_ROOT ||
			     (oh.yst_mode & EXTRA_PERMS) != 0) &&
			    chmod(obj->path_name, oh.yst_mode) < 0)
				prt_err(0, errno, "Warning: Can't chmod %s", obj->path_name);
			break;
		case YAFFS_OBJECT_TYPE_HARDLINK:
			eq_obj = get_object(oh.equivalentObjectId);
			if (eq_obj == NULL)
				prt_err(1, 0, "Invalid equivalentObjectId %u in object %u (%s)",
				        oh.equivalentObjectId, pt->t.objectId, oh.name);
			if (link(eq_obj->path_name, obj->path_name) < 0)
				prt_err(1, errno, "Can't create hardlink %s", obj->path_name);
			break;
		case YAFFS_OBJECT_TYPE_SPECIAL:
			if (mknod(obj->path_name, oh.yst_mode, oh.yst_rdev) < 0) {
				if (errno == EPERM || errno == EINVAL)
					prt_err(0, errno, "Warning: Can't create device %s", obj->path_name);
				else
					prt_err(1, errno, "Can't create device %s", obj->path_name);
			}
			lchown(obj->path_name, oh.yst_uid, oh.yst_gid);
			break;
		case YAFFS_OBJECT_TYPE_UNKNOWN:
			break;
	}

	/* set file date and time */
	switch(oh.type) {
		case YAFFS_OBJECT_TYPE_FILE:
		case YAFFS_OBJECT_TYPE_SPECIAL:
#ifdef HAS_LUTIMES
		case YAFFS_OBJECT_TYPE_SYMLINK:
#endif
			set_utime(obj->path_name,
			          oh.yst_atime, oh.yst_mtime);
			break;
		case YAFFS_OBJECT_TYPE_DIRECTORY:
		default:
			break;
	}
}
コード例 #10
0
ファイル: probe.c プロジェクト: faircreek/tydemux
int std_probe_tystream(tystream_holder_t * tystream) {

	/* Iteration */
	off64_t j;

	/* File size */
	off64_t in_file_size;

	/* Chunks */
	chunknumber_t local_nr_chunks;
	chunknumber_t probe_hz;
	chunknumber_t chunk_lnr;
	chunknumber_t int_chunk_nr;


	chunk_t * chunk;

	LOG_USERDIAG("Probing TyStream .....\n");

	chunk = NULL;


	if(tystream->vstream) {
		in_file_size=tystream->vstream->size;
	} else {
		in_file_size=tystream->in_file_size;
	}

	/* Calculate the probe rate */
	local_nr_chunks = (chunknumber_t)(in_file_size/CHUNK_SIZE);
	probe_hz = local_nr_chunks/10;

	for(j=0, chunk_lnr=1,int_chunk_nr=0 ;  j < in_file_size; j = (j + CHUNK_SIZE*probe_hz), chunk_lnr = chunk_lnr + probe_hz, int_chunk_nr++ ){

		chunk = read_chunk(tystream, chunk_lnr, 2048);
		if(chunk != NULL) {
			tystream->chunks = add_chunk(tystream, chunk, tystream->chunks);
			//printf("Chunk %i has %i records\n",int_chunk_nr,chunk->nr_records);
		} else {
			//printf("Chunk was null\n");
		}
	}



	if(tystream->chunks == NULL) {
		LOG_ERROR("Big ERROR - NO CHUNKS\n");
		clear_tystream_audio_probe(tystream, 0);
		return(0);
	}

	if(!probe_tystream_S2(tystream)) {
		LOG_ERROR("Big ERROR - CAN'T DETECT S1/S2\n");
		clear_tystream_audio_probe(tystream, 0);
		return(0);
	}


	if(!probe_tystream_frame_rate(tystream)){
		LOG_ERROR("Big ERROR - CAN'T DETECT FRAME RATE\n");
		clear_tystream_audio_probe(tystream, 0);
		return(0);
	}

	if(!probe_tystream_audio(tystream)) {
		LOG_ERROR("Big ERROR - CAN'T DETECT AUDIO TYPE\n");
		clear_tystream_audio_probe(tystream, 0);
		return(0);
	}

	if(!probe_audio_tick_size(tystream)) {
		LOG_ERROR("Big ERROR - CAN'T DETECT AUDIO SIZE\n");
		clear_tystream_audio_probe(tystream, 1);
		return(0);
	}

	clear_tystream_audio_probe(tystream, 1);
	return(1);


}
コード例 #11
0
ファイル: dhav.c プロジェクト: DeHackEd/FFmpeg
static int dhav_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    DHAVContext *dhav = s->priv_data;
    int64_t start;
    int ret;

    start = avio_tell(s->pb);

    while ((ret = read_chunk(s)) == 0)
        ;

    if (ret < 0)
        return ret;

    if (dhav->type == 0xfd && dhav->video_stream_index == -1) {
        AVStream *st = avformat_new_stream(s, NULL);
        DHAVStream *dst;

        if (!st)
            return AVERROR(ENOMEM);

        st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
        switch (dhav->video_codec) {
        case 0x1: st->codecpar->codec_id = AV_CODEC_ID_MPEG4; break;
        case 0x3: st->codecpar->codec_id = AV_CODEC_ID_MJPEG; break;
        case 0x2:
        case 0x4:
        case 0x8: st->codecpar->codec_id = AV_CODEC_ID_H264;  break;
        case 0xc: st->codecpar->codec_id = AV_CODEC_ID_HEVC;  break;
        default: avpriv_request_sample(s, "Unknown video codec %X\n", dhav->video_codec);
        }
        st->codecpar->width      = dhav->width;
        st->codecpar->height     = dhav->height;
        st->avg_frame_rate.num   = dhav->frame_rate;
        st->avg_frame_rate.den   = 1;
        st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
        if (!st->priv_data)
            return AVERROR(ENOMEM);
        dst->last_timestamp = AV_NOPTS_VALUE;
        dhav->video_stream_index = st->index;

        avpriv_set_pts_info(st, 64, 1, 1000);
    } else if (dhav->type == 0xf0 && dhav->audio_stream_index == -1) {
        AVStream *st = avformat_new_stream(s, NULL);
        DHAVStream *dst;

        if (!st)
            return AVERROR(ENOMEM);

        st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
        switch (dhav->audio_codec) {
        case 0x07: st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;    break;
        case 0x0c: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
        case 0x10: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
        case 0x0a: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
        case 0x16: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
        case 0x0e: st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW;  break;
        case 0x1a: st->codecpar->codec_id = AV_CODEC_ID_AAC;       break;
        case 0x1f: st->codecpar->codec_id = AV_CODEC_ID_MP2;       break;
        case 0x21: st->codecpar->codec_id = AV_CODEC_ID_MP3;       break;
        case 0x0d: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS;  break;
        default: avpriv_request_sample(s, "Unknown audio codec %X\n", dhav->audio_codec);
        }
        st->codecpar->channels    = dhav->audio_channels;
        st->codecpar->sample_rate = dhav->sample_rate;
        st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
        if (!st->priv_data)
            return AVERROR(ENOMEM);
        dst->last_timestamp = AV_NOPTS_VALUE;
        dhav->audio_stream_index  = st->index;

        avpriv_set_pts_info(st, 64, 1, 1000);
    }

    ret = av_get_packet(s->pb, pkt, ret);
    if (ret < 0)
        return ret;
    pkt->stream_index = dhav->type == 0xf0 ? dhav->audio_stream_index : dhav->video_stream_index;
    if (dhav->type != 0xfc)
        pkt->flags   |= AV_PKT_FLAG_KEY;
    if (pkt->stream_index >= 0)
        pkt->pts = get_pts(s, s->streams[pkt->stream_index]->priv_data);
    pkt->duration = 1;
    pkt->pos = start;
    if (avio_rl32(s->pb) != MKTAG('d','h','a','v'))
        return AVERROR_INVALIDDATA;
    avio_skip(s->pb, 4);

    return ret;
}
コード例 #12
0
ファイル: probe.c プロジェクト: faircreek/tydemux
void get_start_chunk(tystream_holder_t * tystream) {

	/* interation */
	chunknumber_t j;
	int i;


	/* Marker */
	int gotit;

	/* Chunks */
	int chunk_lnr;
	chunk_t * chunk;
#if !defined(TIVO)
	vstream_t * vstream;
	uint8_t * buffer;
#endif

	if(tystream->right_audio == 0x3c0) {
		LOG_USERDIAG("Seeking TyStream start of MPEG Layer II audio \n");
	} else {
		LOG_USERDIAG("Seeking TyStream start of AC3 (Dolby Digital) audio \n");
	}



	gotit=0;


	if(tystream->std_alone) {

		for(j=0, chunk_lnr=0;  j < tystream->in_file_size; j = (j + CHUNK_SIZE) + tystream->byte_offset, chunk_lnr++){


			chunk = read_chunk(tystream, chunk_lnr, 1);
			if(chunk != NULL) {
				for(i=0; i < chunk->nr_records; i++){
					if(chunk->record_header[i].type == tystream->right_audio){
						gotit=1;
						break;
					}
				}
				free_chunk(chunk);
			}
			if(gotit){
				break;
			}
		}
	}
#if !defined(TIVO)
	else {
		chunk_lnr=0;

		buffer = (uint8_t *)malloc(sizeof(uint8_t) * CHUNK_SIZE);
		while(1) {

			vstream = new_vstream();
			memset(buffer,0,sizeof(uint8_t) * CHUNK_SIZE);
			thread_pipe_peek(tystream->input_pipe, CHUNK_SIZE, buffer);
			vstream->size = CHUNK_SIZE;
			vstream->start_stream = vstream->current_pos = buffer;
			vstream->end_stream = vstream->start_stream + CHUNK_SIZE;
			tystream->vstream = vstream;

			chunk = read_chunk(tystream, chunk_lnr, 1);
			if(chunk != NULL) {
				for(i=0; i < chunk->nr_records; i++){
					if(chunk->record_header[i].type == tystream->right_audio){
						gotit=1;
						break;
					}
				}
				free_chunk(chunk);
			}

			vstream->start_stream = vstream->end_stream = vstream->current_pos = NULL;
			vstream->size = vstream->eof = 0;
			tystream->vstream=NULL;
			free(vstream);

			if(!gotit) {
				chunk_lnr++;
				thread_pipe_read(tystream->input_pipe, CHUNK_SIZE, buffer);
			} else {
				break;
			}
		}
		free(buffer);
	}
#endif

	tystream->start_chunk = chunk_lnr;

	if(tystream->right_audio == 0x3c0) {
		LOG_USERDIAG("Found start of MPEG Layer II audio \n");
	} else {
		LOG_USERDIAG("Found start of AC3 (Dolby Digital) audio \n");
	}
	LOG_USERDIAG1("Skipping to chunk " I64FORMAT " - reseting chunk numbering \n\n", tystream->start_chunk);

	tystream->number_of_chunks = 0;
}
コード例 #13
0
ファイル: unyaffs.c プロジェクト: ThunderPuppy/unyaffs
int main(int argc, char **argv) {
	int ch;
	char *ep;

	int opt_detect;
	int opt_bad;
	int opt_chunk;
	int opt_spare;

	/* handle command line options */
	opt_detect = 0;
	opt_bad = 0;
	opt_chunk = 0;
	opt_spare = 0;
	opt_list = 0;
	opt_verbose = 0;
	while ((ch = getopt(argc, argv, "dbc:s:tvVh?")) > 0) {
		switch (ch) {
			case 'd':
				opt_detect = 1;
				break;
			case 'b':
				opt_bad = 1;
				break;
			case 'c':
				opt_chunk = strtol(optarg, &ep, 0);
				if (*ep != '\0' ||
				    opt_chunk < 0 ||
				    opt_chunk > (MAX_CHUNK_SIZE / 1024) )
					usage();
				break;
			case 's':
				opt_spare = strtol(optarg, &ep, 0);
				if (*ep != '\0' ||
				    opt_spare < 0 ||
				    opt_spare > MAX_SPARE_SIZE)
					usage();
				break;
			case 't':
				opt_list = 1;
				break;
			case 'v':
				opt_verbose = 1;
				break;
			case 'V':
				printf("V%s\n", VERSION);
				exit(0);
				break;
			case 'h':
			case '?':
			default:
				usage();
				break;
    		}
	}

	/* extract rest of command line parameters */
	if ((argc - optind) < 1 || (argc - optind) > 2)
		usage();

	if (strcmp(argv[optind], "-") == 0) {	/* image file from stdin ? */
		img_file = 0;
	} else {
		img_file = open(argv[optind], O_RDONLY);
		if (img_file < 0)
			prt_err(1, errno, "Open image file failed");
	}

	if (opt_detect) {
		detect_flash_layout(1, 0);
		return 0;
	}

	if (opt_chunk == 0 || opt_spare == 0) {
		detect_flash_layout(0, 1);
		if (opt_verbose)
			prt_err(0, 0,
		        	"Header check OK, chunk size = %dK, spare size = %d, %sbad block info.",
		        	chunk_size/1024, spare_size, spare_off ? "" : "no ");
	} else {
		chunk_size = opt_chunk * 1024;
		spare_size = opt_spare;
		spare_off  = opt_bad ? 2 : 0;
	}
	spare_data = data + chunk_size;

	if ((argc - optind) == 2 && !opt_list) {
		if (mkdirpath(argv[optind+1], 0755) < 0)
			prt_err(1, errno, "Can't mkdir %s", argv[optind+1]);
		if (chdir(argv[optind+1]) < 0)
			prt_err(1, errno, "Can't chdir to %s", argv[optind+1]);
	}

	umask(0);

	init_obj_list();
	saved_chunk.objectId = 0;
	while (read_chunk()) {
		process_chunk();
	}
	set_dirs_utime();
	close(img_file);

	if (warn_chown)
#ifdef __CYGWIN__
		prt_err(0, 0, "Warning: Can't restore owner/group attribute (limitation of Cygwin/Windows)");
#else
		prt_err(0, 0, "Warning: Can't restore owner/group attribute, run unyaffs as root");
#endif

	return 0;
}
コード例 #14
0
ファイル: http_resp.c プロジェクト: program-fans/shiyan
static int
read_body_chunked(http_resp *a_resp,
                  http_req *a_req,
                  http_trans_conn *a_conn)
{
	int   l_rv = 0;
	int   l_done = 0;

	do
	{
		/* read a chunk */
		l_rv = read_chunk(a_resp, a_conn);
		if (l_rv == HTTP_TRANS_ERR)
			return HTTP_TRANS_ERR;
		if ((a_conn->sync == HTTP_TRANS_ASYNC) && (l_rv == HTTP_TRANS_NOT_DONE))
			return HTTP_TRANS_NOT_DONE;
		/* see if it's the first time */
		if (a_conn->sync == HTTP_TRANS_SYNC && a_conn->chunk_len > 0)
		{
			if (a_resp->body == NULL)
			{
				a_resp->body = malloc(a_conn->chunk_len);
				memcpy(a_resp->body, a_conn->io_buf, a_conn->chunk_len);
				a_resp->body_len = a_conn->chunk_len;
			}
			/* append it to the body */
			else
			{
				a_resp->body = realloc(a_resp->body,
				                       (a_resp->body_len + a_conn->chunk_len));
				memcpy(&a_resp->body[a_resp->body_len], a_conn->io_buf, a_conn->chunk_len);
				a_resp->body_len += a_conn->chunk_len;
			}
		}
		/* make sure there's at least 2 bytes in the buffer.
		This happens when a read was 3 bytes as in 0\r\n
		 and there is still 2 bytes ( \r\n ) in the read queue. */
		if ((a_conn->chunk_len == 0) && (a_conn->io_buf_alloc < 2))
		{
			a_conn->io_buf_io_left = ( 2 - a_conn->io_buf_alloc );
			a_conn->io_buf_io_done = 0;
			do
			{
				l_rv = http_trans_read_into_buf(a_conn);
			}
			while (l_rv == HTTP_TRANS_NOT_DONE);
			ghttpDebug("l_rv: %d   chunk_len: %d \n", l_rv, a_conn->chunk_len);
			/* check for an error */
			if (l_rv == HTTP_TRANS_ERR)
				return HTTP_TRANS_ERR;
		}
		if (a_conn->chunk_len == 0)
			l_done = 1;
		else
		{
			/* clip the buffer */
			http_trans_buf_clip(a_conn, &a_conn->io_buf[a_conn->chunk_len + 2]);
		}
		a_conn->chunk_len = 0;
	}while (l_done == 0);
	
	return HTTP_TRANS_DONE;
}
コード例 #15
0
ファイル: regionmap.c プロジェクト: saltire/cmapbash
void render_region_map(image *img, const int32_t rpx, const int32_t rpy, region *reg,
		region *nregions[4], const textures *tex, const options *opts)
{
	open_region_file(reg);
	if (reg == NULL || reg->file == NULL) return;

	for (uint8_t i = 0; i < 4; i++) open_region_file(nregions[i]);

	chunk_data *chunk, *prev_chunk, *new_chunk, *nchunks[4];
	chunk_flags flags = {
		1,
		1,
		opts->dark,
		opts->isometric && !opts->dark && opts->shadows,
		opts->biomes
	};
	chunk_flags nflags = {
		1,
		opts->isometric,
		opts->isometric && opts->dark,
		opts->isometric && !opts->dark && opts->shadows,
		0
	};

	// use rotated chunk coordinates, since we need to draw them from bottom to top for isometric
	for (int8_t rcz = MAX_REGION_CHUNK; rcz >= 0; rcz--)
	{
		for (int8_t rcx = MAX_REGION_CHUNK; rcx >= 0; rcx--)
		{
			// get the actual chunk from its rotated coordinates
			// use the "new" chunk saved by the previous iteration if possible
			chunk = rcx < MAX_REGION_CHUNK && prev_chunk != NULL ? new_chunk :
					read_chunk(reg, rcx, rcz, opts->rotate, &flags, opts->ylimits);
			if (chunk == NULL)
			{
				if (rcx < MAX_REGION_CHUNK) free_chunk(prev_chunk);
				prev_chunk = NULL;
				continue;
			}

			// get neighbouring chunks, either from this region or a neighbouring one
			nchunks[TOP] = rcz > 0 ?
					read_chunk(reg, rcx, rcz - 1, opts->rotate, &flags, opts->ylimits) :
					read_chunk(nregions[TOP], rcx, MAX_REGION_CHUNK, opts->rotate, &nflags,
							opts->ylimits);

			nchunks[RIGHT] = rcx < MAX_REGION_CHUNK ? prev_chunk :
					read_chunk(nregions[RIGHT], 0, rcz, opts->rotate, &nflags, opts->ylimits);

			nchunks[BOTTOM] = rcz < MAX_REGION_CHUNK ?
					read_chunk(reg, rcx, rcz + 1, opts->rotate, &flags, opts->ylimits) :
					read_chunk(nregions[BOTTOM], rcx, 0, opts->rotate, &nflags, opts->ylimits);

			nchunks[LEFT] = rcx > 0 ?
					read_chunk(reg, rcx - 1, rcz, opts->rotate, &flags, opts->ylimits) :
					read_chunk(nregions[LEFT], MAX_REGION_CHUNK, rcz, opts->rotate, &nflags,
							opts->ylimits);

			for (uint8_t i = 0; i < 4; i++)
			{
				if (nchunks[i] == NULL)
				{
					chunk->nbids[i] = NULL;
					chunk->nbdata[i] = NULL;
					chunk->nblight[i] = NULL;
					chunk->nslight[i] = NULL;
				}
				else {
					chunk->nbids[i] = nchunks[i]->bids;
					chunk->nbdata[i] = nchunks[i]->bdata;
					chunk->nblight[i] = nchunks[i]->blight;
					chunk->nslight[i] = nchunks[i]->slight;
				}
			}

			// render chunk image onto region image
			uint32_t cpx, cpy;
			if (opts->isometric)
			{
				// translate orthographic to isometric coordinates
				cpx = rpx + (rcx + MAX_REGION_CHUNK - rcz) * ISO_CHUNK_X_MARGIN;
				cpy = rpy + (rcx + rcz) * ISO_CHUNK_Y_MARGIN;
			}
			else
			{
				cpx = rpx + rcx * CHUNK_BLOCK_LENGTH;
				cpy = rpy + rcz * CHUNK_BLOCK_LENGTH;
			}

			// loop through rotated chunk's blocks
			for (int16_t rbz = MAX_CHUNK_BLOCK; rbz >= 0; rbz--)
				for (int16_t rbx = MAX_CHUNK_BLOCK; rbx >= 0; rbx--)
					if (opts->isometric)
					{
						// translate orthographic to isometric coordinates
						uint32_t px = cpx + (rbx + MAX_CHUNK_BLOCK - rbz) * ISO_BLOCK_WIDTH / 2;
						uint32_t py = cpy + (rbx + rbz) * ISO_BLOCK_TOP_HEIGHT;
						render_iso_column(img, px, py, tex, chunk, rbx, rbz, opts);
					}
					else
						render_ortho_column(img, cpx + rbx, cpy + rbz, tex, chunk, rbx, rbz, opts);

			// free chunks, or save them for the next iteration if we're not at the end of a row
			free_chunk(nchunks[TOP]);
			free_chunk(nchunks[RIGHT]);
			free_chunk(nchunks[BOTTOM]);
			if (rcx == 0)
			{
				free_chunk(chunk);
				free_chunk(nchunks[LEFT]);
			}
			else
			{
				prev_chunk = chunk;
				new_chunk = nchunks[LEFT];
			}
		}
	}

	close_region_file(reg);
	for (uint8_t i = 0; i < 4; i++) close_region_file(nregions[i]);
}
コード例 #16
0
ファイル: EM.cpp プロジェクト: fgvieira/ngsF
int do_EM (params *pars, out_data *output) {
	SIG_COND = true;
	catch_SIG();

	int iter = ( strcmp("e", pars->init_values) == 0 ? 0 : 1 );
	double lkl_epsilon = 0;
	double est_epsilon = 0;
	sem_init(&pars->_launch_thread_semaph, 0, pars->n_threads);
	sem_init(&pars->_running_thread_semaph, 0, 0); // To avoid warnings from Valgrind
	sem_init(&pars->_running_thread_semaph, 0, -pars->n_chunks);
	pthread_mutex_init(&pars->_F_lock, NULL);

	while( (est_epsilon > pars->min_epsilon || lkl_epsilon > pars->min_epsilon || iter <= pars->min_iters) && iter <= pars->max_iters && SIG_COND ) {
	        // Print of initial Lkl
	        if( iter == 1 && pars->verbose >= 1 ) {
		  output->global_lkl = full_HWE_like(pars, output->site_freq, output->indF, 0, pars->n_ind);
		  printf("==> Initial LogLkl: %.15f\n", output->global_lkl);
		  fflush(stdout);
		}

		// Next Iteration...
		time_t iter_start = time(NULL);
		if( iter > 0 && pars->verbose >= 1 )
		  printf("\nIteration %d:\n", iter);



		////////////////////////////////
		// Loop through all chunks... //
		////////////////////////////////
		for(uint64_t c = 0; c < pars->n_chunks; c++) {
			// Wait for room to launch more threads
			while( sem_wait(&pars->_launch_thread_semaph) );

			if( pars->verbose >= 5 ) printf("\tChunk %lu of %lu\n", c+1, pars->n_chunks);

			// Declare structure
			pth_params *pth_struct = new pth_params;
			// Reserve memory for chunk data
			pth_struct->chunk_data = new double* [pars->max_chunk_size];
#ifdef _USE_BGZF
			for(uint64_t s = 0; s < pars->max_chunk_size; s++)
				pth_struct->chunk_data[s] = new double[pars->n_ind * 3];
#endif
			// Fill in PThread structure
			pth_struct->pars = pars;
			pth_struct->chunk_size = read_chunk(pth_struct->chunk_data, pth_struct->pars, c);
			pth_struct->chunk_abs_start_pos = c * pars->max_chunk_size;
			pth_struct->iter = iter;
			pth_struct->output = output;

			// Initialize and set thread detached attribute
			pthread_t thread_id;
			pthread_attr_t pt_attr;
			pthread_attr_init(&pt_attr);
			pthread_attr_setdetachstate(&pt_attr, PTHREAD_CREATE_DETACHED);

			// Launch thread
			int rc = pthread_create(&thread_id, &pt_attr, run_chunk, (void*) pth_struct);
			if(rc) error(__FUNCTION__,"pthread_create() failed!");

			if( pars->verbose >= 6 ) {
				int n_free_threads = 0;
				sem_getvalue(&pars->_launch_thread_semaph, &n_free_threads);
				printf("Thread launched! Available slots: %d\n", n_free_threads);
			}
			fflush(stdout);
		}



		////////////////////////////////////
		// Wait for all threads to finish //
		////////////////////////////////////
		int n_free_threads = 0;
		do {
			while( sem_wait(&pars->_running_thread_semaph) );
			sem_getvalue(&pars->_launch_thread_semaph, &n_free_threads);
			if( pars->verbose >= 6 ) printf("Waiting for all threads to finish: %d\n", pars->n_threads - n_free_threads);
		}while(n_free_threads < (int) pars->n_threads);



		est_epsilon = 0;
		/////////////////////////////////////
		// Indiv post-iteration processing //
		/////////////////////////////////////
		if( pars->verbose >= 2 ) printf("\tInd F:\t");
		for(uint16_t i = 0; i < pars->n_ind; i++) {
			// Get new indF and check for interval...
			double new_indF = check_interv(output->indF_num[i] / output->indF_den[i], false);
			// If LRT, do not estimate indF (it is fixed)
			if(pars->calc_LRT)
			  new_indF = output->indF[i];
			// Calculate iter epsilon
			est_epsilon += pow(new_indF - output->indF[i], 2);
			// Store new indF
			new_indF = ( new_indF == 1 ? 0.9999 : new_indF );
			output->indF[i] = new_indF;

			// Reset variables...
			output->indF_num[i] = 0;
			output->indF_den[i] = 0;

			// Debug
			if( pars->verbose >= 2 ) printf("\t%.9f", output->indF[i]);
		}
		if( pars->verbose >= 2 ) printf("\n");



		////////////////////////////////////
                // Site post-iteration processing //
                ////////////////////////////////////
                if( pars->verbose >= 4 ) printf("\tFreq:\t");
                for(uint64_t s = 0; s < pars->n_sites; s++) {
                  if(output->site_freq[s] == 0) continue;
                  if(!pars->freq_fixed){
                    double new_site_freq = check_interv(output->site_freq_num[s] / output->site_freq_den[s], true);
                    est_epsilon += pow(new_site_freq - output->site_freq[s], 2);
                    output->site_freq[s] = (new_site_freq > 0.99 ? 0.99 : new_site_freq);
                  }

                  // Reset variables...
                  output->site_freq_num[s] = 0;
                  output->site_freq_den[s] = 0;
                  output->site_prob_var[s] = output->site_tmpprob_var[s];
                  output->site_tmpprob_var[s] = 0;

                  // Debug
                  if( pars->verbose >= 4 ) printf("\t%.9f", output->site_freq[s]);
                }
                if( pars->verbose >= 4 ) printf("\n");



		///////////////////
		// Calculate Lkl //
		///////////////////
		double new_global_lkl = 0;
		for(uint16_t i = 0; i < pars->n_ind; i++) {
		  output->ind_lkl[i] = full_HWE_like(pars, output->site_freq, output->indF, i, 1);
		  new_global_lkl += output->ind_lkl[i];
		}
		// Parameter epsilon
		est_epsilon = sqrt(est_epsilon)/(pars->n_ind + pars->n_sites);
		// Lkl epsilon calculation - On first iteration, since there is no global_lkl, calculate Lkl epsilon based on current lkl
		lkl_epsilon = (new_global_lkl - output->global_lkl)/fabs(output->global_lkl);
		output->global_lkl = new_global_lkl;

		// Print iteration info
		if( iter > 0 && pars->verbose >= 1 ) {
		  time_t iter_end = time(NULL);
		  printf("\tLogLkl: %.15f\t epsilon: %.15f %.15f\ttime: %.0f (s)\n", output->global_lkl, lkl_epsilon, est_epsilon, difftime(iter_end, iter_start) );
		}
		iter++;
		fflush(stdout);



		///////////////////////////////
		// Dump iteration parameters //
		///////////////////////////////
		char* pars_file = (char*) malloc( (strlen(pars->out_file)+5+1)*sizeof(char) );
		memset(pars_file, '\0', (strlen(pars->out_file)+5+1)*sizeof(char));
		strcat(pars_file, pars->out_file); strcat(pars_file, ".pars");
		// Write the last iteration to disk
		FILE* last_est_pars = fopen(pars_file, "w");
		if(last_est_pars == NULL)
		  error(__FUNCTION__, "Cannot open PARS file!");
		fwrite(&output->global_lkl, sizeof(double), 1, last_est_pars);
		fwrite(output->ind_lkl, sizeof(double), pars->n_ind, last_est_pars);
		fwrite(output->indF, sizeof(double), pars->n_ind, last_est_pars);
		fwrite(output->site_freq, sizeof(double), pars->n_sites, last_est_pars);
		fclose(last_est_pars);
		free(pars_file);



		///////////////
		// For debug //
		///////////////
		if( pars->quick ) break;
	}


	if( iter > pars->max_iters )
		printf("WARN: Maximum number of iterations reached! Check if analysis converged... \n");

	return 0;
}