int read_hdr(char* filename, struct dsr* hdr)  {
  struct dsr myHdr;
  int size;
  //double cmax, cmin;
  FILE *fp;
	 printf("		[READ_HDR:] Load Header-File %s...", filename);
     	if((fp=fopen(filename,"r"))==NULL)	{
      		printf("Can't open: <%s>\n", filename);
      		exit(0);
  	} else {
		fread(&myHdr,sizeof(struct dsr),1,fp);
		printf("done.\n");
		if(myHdr.dime.dim[0] < 0 || myHdr.dime.dim[0] > 15) swap_hdr(&myHdr);
	}	
   	printf("		[READ_HDR:] Closing Header-File %s...", filename);
	if (fclose(fp) == -1) {
		printf("error on closing.\n");
	} else {
		printf("done.\n");
	}

	//Headerübergabe-Headerinfo an Pointer von main
	hdr->hk.sizeof_hdr = myHdr.hk.sizeof_hdr;
	strcpy(hdr->hk.db_name, myHdr.hk.db_name);
	strcpy(hdr->hk.data_type, myHdr.hk.data_type);
	hdr->hk.extents = myHdr.hk.extents;
	hdr->hk.session_error = myHdr.hk.session_error;
	hdr->hk.regular = myHdr.hk.regular;
	hdr->hk.hkey_un0 = myHdr.hk.hkey_un0;
	
	//Headerübergabe-Dimension an Pointer von main
	int i;
	for (i=0; i<8; i++) hdr->dime.dim[i] = myHdr.dime.dim[i];
	strcpy(hdr->dime.vox_units, myHdr.dime.vox_units);
	strcpy(hdr->dime.cal_units, myHdr.dime.cal_units);
	hdr->dime.unused1 = myHdr.dime.unused1;
	hdr->dime.datatype = myHdr.dime.datatype;
	hdr->dime.bitpix = myHdr.dime.bitpix;
	hdr->dime.dim_un0 = myHdr.dime.dim_un0;
	for (i=0; i<8; i++) hdr->dime.pixdim[i] = myHdr.dime.pixdim[i];
	hdr->dime.vox_offset = myHdr.dime.vox_offset;
	hdr->dime.funused1 = myHdr.dime.funused1;
	hdr->dime.funused2 = myHdr.dime.funused2;
	hdr->dime.funused3 = myHdr.dime.funused3;
	hdr->dime.cal_max = myHdr.dime.cal_max;
	hdr->dime.cal_min = myHdr.dime.cal_min;
	hdr->dime.compressed = myHdr.dime.compressed;
	hdr->dime.verified = myHdr.dime.verified;
	hdr->dime.glmax = myHdr.dime.glmax;
	hdr->dime.glmin = myHdr.dime.glmin;
   return EXIT_SUCCESS;
   }
Exemple #2
0
void Analyze_load_hdr(char *path, AnalyzeHeader *hdr, int *swapped)
{
	FILE	*f;
	int		sz;

	sz=sizeof(AnalyzeHeader);

	f=fopen(path,"r");
	if(f)
	{
		fread(hdr,sz,sizeof(char),f);
		if(f==NULL) return;
		
		*swapped=0;
		if(hdr->sizeof_hdr!=348) // different endianness
		{
			swap_hdr(hdr);
			*swapped=1;
		}
	}
	fclose(f);
}
Exemple #3
0
pcap_t *
priv_pcap_offline(const char *fname, char *errbuf)
{
	pcap_t *p;
	FILE *fp = NULL;
	struct pcap_file_header hdr;
	int linklen, err;

	if (priv_fd < 0)
		errx(1, "%s: called from privileged portion", __func__);

	p = malloc(sizeof(*p));
	if (p == NULL) {
		strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
		return (NULL);
	}

	memset((char *)p, 0, sizeof(*p));

	if (fname[0] == '-' && fname[1] == '\0') {
		p->fd = -1;
		fp = stdin;
	} else {
		write_command(priv_fd, PRIV_OPEN_DUMP);
		p->fd = receive_fd(priv_fd);
		must_read(priv_fd, &err, sizeof(int));
		if (p->fd < 0) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "Failed to open input file %s: %s",
			    fname, strerror(err));
			goto bad;
		}

		fp = fdopen(p->fd, "r");
		if (fp == NULL) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
			    pcap_strerror(errno));
			close(p->fd);
			p->fd = -1;
			goto bad;
		}
	}
	if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
		    pcap_strerror(errno));
		goto bad;
	}

	if (hdr.magic != TCPDUMP_MAGIC) {
		if (swap32(hdr.magic) != TCPDUMP_MAGIC) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "bad dump file format");
			goto bad;
		}
		p->sf.swapped = 1;
		swap_hdr(&hdr);
	}
	if (hdr.version_major < PCAP_VERSION_MAJOR) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
		goto bad;
	}

	p->tzoff = hdr.thiszone;
	p->snapshot = hdr.snaplen;
	p->linktype = hdr.linktype;
	p->sf.rfile = fp;
	p->bufsize = hdr.snaplen;

	/* Align link header as required for proper data alignment */
	/* XXX should handle all types */
	switch (p->linktype) {

	case DLT_EN10MB:
		linklen = 14;
		break;

	case DLT_FDDI:
		linklen = 13 + 8;	/* fddi_header + llc */
		break;

	case DLT_NULL:
	default:
		linklen = 0;
		break;
	}

	if (p->bufsize < 0)
		p->bufsize = BPF_MAXBUFSIZE;
	p->sf.base = malloc(p->bufsize + BPF_ALIGNMENT);
	if (p->sf.base == NULL) {
		strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
		goto bad;
	}
	p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
	p->sf.version_major = hdr.version_major;
	p->sf.version_minor = hdr.version_minor;
#ifdef PCAP_FDDIPAD
	/* XXX what to do with this? */
	/* XXX padding only needed for kernel fcode */
	pcap_fddipad = 0;
#endif
	return (p);

 bad:
	if (fp != NULL && p->fd != -1)
		fclose(fp);
	free(p);
	return (NULL);
}
Exemple #4
0
pcap_t *
pcap_fopen_offline(FILE *fp, char *errbuf)
{
	pcap_t *p;
	struct pcap_file_header hdr;
	int linklen;

	p = calloc(1, sizeof(*p));
	if (p == NULL) {
		strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
		return (NULL);
	}

	/*
	 * Set this field so we don't double-close in pcap_close!
	 */
	p->fd = -1;

	if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
		    pcap_strerror(errno));
		goto bad;
	}
	if (hdr.magic != TCPDUMP_MAGIC) {
		if (SWAPLONG(hdr.magic) != TCPDUMP_MAGIC) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "bad dump file format");
			goto bad;
		}
		p->sf.swapped = 1;
		swap_hdr(&hdr);
	}
	if (hdr.version_major < PCAP_VERSION_MAJOR) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
		goto bad;
	}
	p->tzoff = hdr.thiszone;
	p->snapshot = hdr.snaplen;
	p->linktype = hdr.linktype;
	p->sf.rfile = fp;
	p->bufsize = hdr.snaplen;

	/* Align link header as required for proper data alignment */
	/* XXX should handle all types */
	switch (p->linktype) {

	case DLT_EN10MB:
		linklen = 14;
		break;

	case DLT_FDDI:
		linklen = 13 + 8;	/* fddi_header + llc */
		break;

	case DLT_NULL:
	default:
		linklen = 0;
		break;
	}

	if (p->bufsize < 0)
		p->bufsize = BPF_MAXBUFSIZE;
	p->sf.base = malloc(p->bufsize + BPF_ALIGNMENT);
	if (p->sf.base == NULL) {
		strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
		goto bad;
	}
	p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
	p->sf.version_major = hdr.version_major;
	p->sf.version_minor = hdr.version_minor;
#ifdef PCAP_FDDIPAD
	/* XXX padding only needed for kernel fcode */
	pcap_fddipad = 0;
#endif

	return (p);
 bad:
	free(p);
	return (NULL);
}
Exemple #5
0
pcap_t *
pcap_open_offline(const char *fname, char *errbuf)
{
	register pcap_t *p;
	register FILE *fp;
	struct pcap_file_header hdr;
	int linklen;

	p = (pcap_t *)malloc(sizeof(*p));
	if (p == NULL) {
		strcpy(errbuf, "out of swap");
		return (NULL);
	}

	memset((char *)p, 0, sizeof(*p));
	/*
	 * Set this field so we don't close stdin in pcap_close!
	 */
	p->fd = -1;

	if (fname[0] == '-' && fname[1] == '\0')
		fp = stdin;
	else {
		fp = fopen(fname, "r");
		if (fp == NULL) {
			sprintf(errbuf, "%s: %s", fname, pcap_strerror(errno));
			goto bad;
		}
	}
	if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
		sprintf(errbuf, "fread: %s", pcap_strerror(errno));
		goto bad;
	}
	if (hdr.magic != TCPDUMP_MAGIC) {
		if (SWAPLONG(hdr.magic) != TCPDUMP_MAGIC) {
			sprintf(errbuf, "bad dump file format");
			goto bad;
		}
		p->sf.swapped = 1;
		swap_hdr(&hdr);
	}
	if (hdr.version_major < PCAP_VERSION_MAJOR) {
		sprintf(errbuf, "archaic file format");
		goto bad;
	}
	p->tzoff = hdr.thiszone;
	p->snapshot = hdr.snaplen;
	p->linktype = hdr.linktype;
	p->sf.rfile = fp;
	p->bufsize = hdr.snaplen;

	/* Align link header as required for proper data alignment */
	/* XXX should handle all types */
	switch (p->linktype) {

	case DLT_EN10MB:
		linklen = 14;
		break;

	case DLT_FDDI:
		linklen = 13 + 8;	/* fddi_header + llc */
		break;

	case DLT_NULL:
	default:
		linklen = 0;
		break;
	}

	p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
	p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
	p->sf.version_major = hdr.version_major;
	p->sf.version_minor = hdr.version_minor;
#ifdef PCAP_FDDIPAD
	/* XXX padding only needed for kernel fcode */
	pcap_fddipad = 0;
#endif

	return (p);
 bad:
	free(p);
	return (NULL);
}