Ejemplo n.º 1
0
static bool get_entry(const char* file_path, index_tbl_entry* entry) {
    bool ret = false;
    char buf[BLOCK_SIZE];
    resource_ptn_header header;
    if (!StorageReadLba(get_ptn_offset(), buf, 1)) {
        LOGE("Failed to read header!");
        goto end;
    }
    memcpy(&header, buf, sizeof(header));

    if (memcmp(header.magic, RESOURCE_PTN_HDR_MAGIC, sizeof(header.magic))) {
        LOGE("Not a resource image(%s)!", image_path);
        goto end;
    }

    //test on pc, switch for be.
    fix_header(&header);

    //TODO: support header_size & tbl_entry_size
    if (header.resource_ptn_version != RESOURCE_PTN_VERSION
            || header.header_size != RESOURCE_PTN_HDR_SIZE
            || header.index_tbl_version != INDEX_TBL_VERSION
            || header.tbl_entry_size != INDEX_TBL_ENTR_SIZE) {
        LOGE("Not supported in this version!");
        goto end;
    }

    int i;
    for (i = 0; i < header.tbl_entry_num; i++) {
        //TODO: support tbl_entry_size
        if (!StorageReadLba(get_ptn_offset() +
                    header.header_size + i * header.tbl_entry_size, buf, 1)) {
            LOGE("Failed to read index entry:%d!", i);
            goto end;
        }
        memcpy(entry, buf, sizeof(*entry));

        if (memcmp(entry->tag, INDEX_TBL_ENTR_TAG, sizeof(entry->tag))) {
            LOGE("Something wrong with index entry:%d!", i);
            goto end;
        }

        if (!strncmp(entry->path, file_path, sizeof(entry->path)))
            break;
    }
    if (i == header.tbl_entry_num) {
        LOGE("Cannot find %s!", file_path);
        goto end;
    }

    //test on pc, switch for be.
    fix_entry(entry);

    printf("Found entry:\n\tpath:%s\n\toffset:%d\tsize:%d\n",
            entry->path, entry->content_offset, entry->content_size);

    ret = true;
end:
    return ret;
}
Ejemplo n.º 2
0
static int process(const char *filename) {

	FILE *fp;
	uint8_t header[HEADER_SIZE];
	
	fp = fopen(filename, "r+b");
	if (fp == NULL) {
		fprintf(stderr, "Failed to open %s for reading/writing\n", filename);
		return -errno;
	}
	
	if (fread(header, 1, HEADER_SIZE, fp) != HEADER_SIZE) {
		fclose(fp);
		fprintf(stderr, "Error reading from %s\n", filename);
		return -errno;
	}
	
	fix_header(header);
	if (fseek(fp, 0, SEEK_SET) < 0) {
		fclose(fp);
		fprintf(stderr, "Failed to rewind file %s\n", filename);
		return -errno;
	}
	
	if (fwrite(header, 1, HEADER_SIZE, fp) != HEADER_SIZE) {
		fclose(fp);
		fprintf(stderr, "Error writing header back to file %s\n", filename);
		return -errno;
	}
	
	fclose(fp);
	return 0;
}
Ejemplo n.º 3
0
int main (int argc, char** argv){

	struct option long_opt[256];
	const char* args = "d:hn:t:T:r:m:s:k:B:b:ZSG";
	int opt_flag = 0;
	int long_opt_idx = 0;
	int* timezaps[2];
	int fzaps[1024];
	int ntimezaps,nfreqzaps;
	double newra;
	double newdec;
	double newtstart;
	int newibeam=-1;
	int newnbeams=-1;
	float mean,sigma;
	char newname[1024];
	int c;
	FILE* file;
	char killfile[20];
	int killswitch=0;
	int arr_size = 1024;
	char zap_mode=REPLACE_SAMPLES;

	newname[0]='\0';
	ntimezaps=0;
	nfreqzaps=0;
	mean=0;
	sigma=1;

	newra =900000001;
	newdec=900000001;
	newtstart=900000001;


	long_opt[long_opt_idx].name = "help";
	long_opt[long_opt_idx].has_arg = no_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'h';

	long_opt[long_opt_idx].name = "time-zap";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 't';

	long_opt[long_opt_idx].name = "mean";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'm';

	long_opt[long_opt_idx].name = "sigma";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 's';

	long_opt[long_opt_idx].name = "ra";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'r';

	long_opt[long_opt_idx].name = "dec";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'd';

	long_opt[long_opt_idx].name = "tstart";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'T';


	long_opt[long_opt_idx].name = "beam";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'b';

	long_opt[long_opt_idx].name = "nbeams";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'B';

	long_opt[long_opt_idx].name = "src-name";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'n';

	long_opt[long_opt_idx].name = "tkill";
	long_opt[long_opt_idx].has_arg = required_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'k';

	long_opt[long_opt_idx].name = "replace-gaussian";
	long_opt[long_opt_idx].has_arg = no_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'G';

	long_opt[long_opt_idx].name = "replace-samples";
	long_opt[long_opt_idx].has_arg = no_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'S';


	long_opt[long_opt_idx].name = "replace-zero";
	long_opt[long_opt_idx].has_arg = no_argument;
	long_opt[long_opt_idx].flag = NULL;
	long_opt[long_opt_idx++].val = 'Z';




	long_opt[long_opt_idx].name = 0;
	long_opt[long_opt_idx].has_arg = 0;
	long_opt[long_opt_idx].flag = 0;
	long_opt[long_opt_idx++].val = 0;

	timezaps[0] = malloc((arr_size)* sizeof(int));
	timezaps[1] = malloc((arr_size)* sizeof(int));

	while ((c = getopt_long(argc, argv, args, long_opt, &long_opt_idx)) != -1) {
		switch (opt_flag) {
			default:
				opt_flag = 0;
				switch (c) {
					case 'h':
						print_usage();
						exit(0);
						break;
					case 't':
						sscanf(optarg,"%d %d",timezaps[0]+ntimezaps,timezaps[1]+ntimezaps);
						ntimezaps++;
						break;
					case 'r':
						newra = atof(optarg);
						break;
					case 'T':
						newtstart = atof(optarg);
						break;
					case 'd':
						newdec=atof(optarg);
						break;
					case 'n':
						strcpy(newname,optarg);
						break;
					case 'm':
						mean=atof(optarg);
						break;
					case 's':
						sigma=atof(optarg);
						break;
					case 'b':
						newibeam=atof(optarg);
						break;
					case 'B':
						newnbeams=atof(optarg);
						break;
					case 'k':
						sscanf(optarg, "%s",killfile);
						killswitch = 1;
						break;
					case 'Z':
						zap_mode=REPLACE_ZERO;
						break;
					case 'G':
						zap_mode=REPLACE_GAUSSIAN;
						break;
					case 'S':
						zap_mode=REPLACE_SAMPLES;
						break;
				}
		}
	}




	if (killswitch){
		printf("Using file %s as sample killer...\n",killfile);
		FILE *killer;
		if ((killer = fopen(killfile, "r")) == NULL){
			printf("Failed to open the killfile: %s\n",killfile);
			exit(-1);
		}
		int a, j=0;
		// while (!feof(killer)){
		// fscanf(killer,"%i",&a);
		// j++;
		// }
		// fclose(killer);


		if ((killer = fopen(killfile, "r")) == NULL){
			printf("Failed to open the killfile: %s\n",killfile);
			exit(-1);
		}
		j=0;
		char byte;
		/* Skip the first line of the file */
		while (!feof(killer)){
			fread(&byte,1,1,killer);

			if( byte == '\n' )break;
		}
		int good_sample=1;
		while (!feof(killer)){

			fscanf(killer,"%i\n",&good_sample);
			if(!good_sample){
				if ( ntimezaps > arr_size){
					arr_size*=2;
					timezaps[0]=realloc(timezaps[0],arr_size*sizeof(int));
					timezaps[1]=realloc(timezaps[1],arr_size*sizeof(int));
				}
				timezaps[0][ntimezaps]=j;
				timezaps[1][ntimezaps]=j+1;
				// printf("%d\t%d\t%d\t%d\n",ntimezaps,j,timezaps[0][0],timezaps[1][0]);
				ntimezaps++;
			}
			j++;
		}
		fclose(killer);
		printf("Zapping %d/%d time samples\n",ntimezaps,j);
	}


	if (optind >= argc) {
		printf("Error: Need a fil file to work on... (use --help for options)\n");
		exit(1);
	}

	//printf("%s\n",argv[optind]);
	if ((file = fopen(argv[optind],"r+")) ==NULL){
		printf("Failed to open file '%s'.\n",argv[optind]);
		exit(-5);
	}

	if ( newname[0]!='\0' || newra < 900000000 || newdec < 900000000 || newibeam >= 0 || newnbeams >= 0 || newtstart < 900000000)fix_header(file,newname,newra,newdec,newibeam,newnbeams,newtstart);

	if(ntimezaps > 0 || nfreqzaps > 0)zap_em(file,timezaps,ntimezaps,fzaps,nfreqzaps,mean,sigma,zap_mode);

	return 0;
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
	int dicsize;
	unsigned int udicsize;
	char *uncompressed;
	Dawg_edge rootnode = { 0, 0, 0, 0, 0 };
	Dawg_edge specialnode = { 0, 0, 0, 0, 0 };

	char *outfilename;
	char outfilenamedefault[] = "dic.dawg";
	clock_t starttime, endtime;

	if (argc < 2) {
		fprintf(stderr, "usage: %s uncompressed_dic [compressed_dic]\n",
			argv[0]);
		exit(1);
	}

	dicsize = file_length(argv[1]);
	if (dicsize < 0) {
		fprintf(stderr, "Cannot stat uncompressed dictionary %s\n",
			argv[1]);
		exit(1);
	}

	udicsize = (unsigned int)dicsize;

	outfilename = (argc == 3) ? argv[2] : outfilenamedefault;

	if ((global_outfile = fopen(outfilename, "wb")) == NULL) {
		fprintf(stderr, "Cannot open output file %s\n", outfilename);
		exit(1);
	}

	if ((uncompressed = load_uncompressed(argv[1], &udicsize)) == NULL) {
		fprintf(stderr,
			"Cannot load uncompressed dictionary into memory\n");
		exit(1);
	}

	global_input = uncompressed;
	global_endofinput = global_input + dicsize;

#define SCALE 0.6
	global_hashtable = hash_init(dicsize * SCALE);
#undef SCALE

	skip_init_header(global_outfile, &global_header);

	specialnode.last = 1;
	write_node(&specialnode, sizeof(specialnode), 1, global_outfile);
	/*
	 * Call makenode with null (relative to stringbuf) prefix;
	 * Initialize string to null; Put index of start node on output
	 */
	starttime = clock();
	rootnode.ptr = makenode(global_endstring = global_stringbuf);
	endtime = clock();
	write_node(&rootnode, sizeof(rootnode), 1, global_outfile);

	fix_header(global_outfile, &global_header);

	print_header_info(&global_header);
	hash_destroy(global_hashtable);
	free(uncompressed);
	fclose(global_outfile);

	printf(" Elapsed time is                 : %f s\n",
	       1.0 * (endtime - starttime) / CLOCKS_PER_SEC);
	printf(" Maximum recursion level reached : %d\n", max_rec);
	return 0;
}
Ejemplo n.º 5
0
int main_pad2unpad(int argc, char *argv[])
{
    samFile *in = 0, *out = 0;
    bam_hdr_t *h = 0, *h_fix = 0;
    faidx_t *fai = 0;
    int c, compress_level = -1, is_long_help = 0;
    char in_mode[5], out_mode[6], *fn_out = 0, *fn_list = 0;
    int ret=0;
    sam_global_args ga = SAM_GLOBAL_ARGS_INIT;

    static const struct option lopts[] = {
        SAM_OPT_GLOBAL_OPTIONS('-', 0, 0, 0, 'T'),
        { NULL, 0, NULL, 0 }
    };

    /* parse command-line options */
    strcpy(in_mode, "r"); strcpy(out_mode, "w");
    while ((c = getopt_long(argc, argv, "SCso:u1T:?", lopts, NULL)) >= 0) {
        switch (c) {
        case 'S': break;
        case 'C': hts_parse_format(&ga.out, "cram"); break;
        case 's': assert(compress_level == -1); hts_parse_format(&ga.out, "sam"); break;
        case 'o': fn_out = strdup(optarg); break;
        case 'u':
            compress_level = 0;
            if (ga.out.format == unknown_format)
                hts_parse_format(&ga.out, "bam");
            break;
        case '1':
            compress_level = 1;
            if (ga.out.format == unknown_format)
                hts_parse_format(&ga.out, "bam");
            break;
        case '?': is_long_help = 1; break;
        default:  if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break;
            fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n\n", c);
            return usage(is_long_help);
        }
    }
    if (argc == optind) return usage(is_long_help);

    strcat(out_mode, "h");
    if (compress_level >= 0) {
        char tmp[2];
        tmp[0] = compress_level + '0'; tmp[1] = '\0';
        strcat(out_mode, tmp);
    }

    // Load FASTA reference (also needed for SAM -> BAM if missing header)
    if (ga.reference) {
        fn_list = samfaipath(ga.reference);
        fai = fai_load(ga.reference);
    }
    // open file handlers
    if ((in = sam_open_format(argv[optind], in_mode, &ga.in)) == 0) {
        fprintf(stderr, "[depad] failed to open \"%s\" for reading.\n", argv[optind]);
        ret = 1;
        goto depad_end;
    }
    if (fn_list && hts_set_fai_filename(in, fn_list) != 0) {
        fprintf(stderr, "[depad] failed to load reference file \"%s\".\n", fn_list);
        ret = 1;
        goto depad_end;
    }
    if ((h = sam_hdr_read(in)) == 0) {
        fprintf(stderr, "[depad] failed to read the header from \"%s\".\n", argv[optind]);
        ret = 1;
        goto depad_end;
    }
    if (fai) {
        h_fix = fix_header(h, fai);
    } else {
        fprintf(stderr, "[depad] Warning - reference lengths will not be corrected without FASTA reference\n");
        h_fix = h;
    }
    char wmode[2];
    strcat(out_mode, sam_open_mode(wmode, fn_out, NULL)==0 ? wmode : "b");
    if ((out = sam_open_format(fn_out? fn_out : "-", out_mode, &ga.out)) == 0) {
        fprintf(stderr, "[depad] failed to open \"%s\" for writing.\n", fn_out? fn_out : "standard output");
        ret = 1;
        goto depad_end;
    }

    // Reference-based CRAM won't work unless we also create a new reference.
    // We could embed this, but for now we take the easy option.
    if (ga.out.format == cram)
        hts_set_opt(out, CRAM_OPT_NO_REF, 1);

    if (sam_hdr_write(out, h_fix) != 0) {
        fprintf(stderr, "[depad] failed to write header.\n");
        ret = 1;
        goto depad_end;
    }

    // Do the depad
    ret = bam_pad2unpad(in, out, h, fai);

depad_end:
    // close files, free and return
    if (fai) fai_destroy(fai);
    if (h) bam_hdr_destroy(h);
    sam_close(in);
    sam_close(out);
    free(fn_list); free(fn_out);
    return ret;
}