Ejemplo n.º 1
0
uInt
coff_length(Environ *e, uByte *load, uInt loadlen)
{
	unsigned char *data = (unsigned char*)load;
	Fileheader *fhdr = (Fileheader*)data;
	Bool swap = (fhdr->magic[0] == COFF_MAGIC_1);
	Optfileheader *ofhdr = (Optfileheader*)(fhdr + 1);
	Sectionheader *scnhdr = (Sectionheader*)((uPtr)ofhdr +
			SWAPSHORT(swap, fhdr->opthdrlen));
	int numsections = SWAPSHORT(swap, fhdr->sections);
	int i, l, len = 0;

	for (i = 0; i < numsections; i++)
	{
		if (SWAPSHORT(swap, scnhdr[i].flags) & (S_BSS | S_NOLOAD))
			continue;
		
		l = SWAPINT(swap, scnhdr[i].rawdata);
		l += SWAPINT(swap, scnhdr[i].size);

		if (l > len)
			len = l;

		if (len > loadlen)
			return 0;
	}

	return len;
}
Ejemplo n.º 2
0
Bool
coff_is_exec(Environ *e, uByte *load, uInt loadlen)
{
	Fileheader *fhdr = (Fileheader*)load;
	Optfileheader *ofhdr = (Optfileheader*)(fhdr + 1);
	Bool swap = (fhdr->magic[0] == COFF_MAGIC_1);

	DPRINTF(("loading: magic=%#x.%x flags=%#x ctime=%#x opthdrlen=%#x\n", 
			fhdr->magic[0], fhdr->magic[1], SWAPSHORT(swap, fhdr->flags),
			SWAPINT(swap, fhdr->ctime), SWAPSHORT(swap, fhdr->opthdrlen)));

	if (loadlen < sizeof *fhdr)
		return FALSE;

	if (swap)
	{
		uByte b = fhdr->magic[0];
		fhdr->magic[0] = fhdr->magic[1];
		fhdr->magic[1] = b;
	}

	/* verify that it is the right COFF file format */
	if (fhdr->magic[0] == COFF_MAGIC_0 &&
			fhdr->magic[1] == COFF_MAGIC_1 &&
			(SWAPSHORT(swap, fhdr->flags) & F_EXEC) &&
			ofhdr->magic[0] == COFF_OPT_MAGIC_0 &&
			ofhdr->magic[1] == COFF_OPT_MAGIC_1)
		return TRUE;

	return FALSE;
}
Ejemplo n.º 3
0
/* prepare to run the COFF image already verified to be COFF
 */
Retcode
coff_load(Environ *e, uByte *load, uInt loadlen, uLong *entrypoint)
{
	unsigned char *data = (unsigned char*)load;
	Fileheader *fhdr = (Fileheader*)data;
	Bool swap = (fhdr->magic[0] == COFF_MAGIC_1);
	Optfileheader *ofhdr;
	Sectionheader *scnhdr;
	int numsections = SWAPSHORT(swap, fhdr->sections);
	unsigned char *from, *to;
	int len, i;

	/* sanity check */
	if (!coff_is_exec(e, load, loadlen))
		return E_BAD_IMAGE;

	/* copy the headers into a temp-buffer to support overlapping loads */
	len = sizeof *fhdr + SWAPSHORT(swap, fhdr->opthdrlen) +
			sizeof *scnhdr * numsections;
	fhdr = (Fileheader*)malloc(len);

	if (fhdr == NULL)
	{
		cprintf(e, "Cannot allocate enough memory for COFF headers?!?\n");
		return E_OUT_OF_MEMORY;
	}

	memcpy(fhdr, data, len);
	ofhdr = (Optfileheader*)(fhdr + 1);
	scnhdr = (Sectionheader*)((uPtr)ofhdr + SWAPSHORT(swap, fhdr->opthdrlen));

	*entrypoint = SWAPINT(swap, ofhdr->entrypoint);
	DPRINTF(("entrypoint=%#x\n", *entrypoint));

	/* copy the sections to the right addresses */
	for (i = 0; i < numsections; i++)
	{
		from = (unsigned char*)data + SWAPINT(swap, scnhdr[i].rawdata);
		to = (unsigned char*)(uPtr)SWAPINT(swap, scnhdr[i].physaddr);
		len = SWAPINT(swap, scnhdr[i].size);
		DPRINTF(("section %d from=%#x, to=%#x len=%d\n", i, from, to, len));

		if (len == 0)
			continue;

		if (SWAPSHORT(swap, scnhdr[i].flags) & S_BSS)
			memset(to, 0, len);
		else if (from)
			memmove(to, from, len);

	#ifdef MACHINE_CLAIM_MEMORY
		/* claim/map this area if requested */
		MACHINE_CLAIM_MEMORY(e, (char*)to, len);
	#endif /* MACHINE_CLAIM_MEMORY */
	}

	free(fhdr);
	return NO_ERROR;
}
Ejemplo n.º 4
0
u_int32_t wrap_export_translate_value(UserOperate_t* user_op,
                                      ResultContext_t *result_context,
                                      char* original_flag, char* helper, u_int32_t position,
                                      u_int32_t trans_type, void* trans_data, s32 trans_data_int)
{
    int num;
    int i;
    s32 ret;
    struct pcap_info* buf;
    capture_state state;
    u32 copied, malloc_size;

    struct pcap_file_header file_head = {0xd4c3b2a1, SWAPSHORT(2), SWAPSHORT(4), 0, 0, SWAPLONG(0x0000ffff), SWAPLONG(1)};

    if ( strcmp(helper, "download") == 0)
    {
        netmode_cap_callback(NETMODE_SYSCALL_GET_CAP_STATE,&state,sizeof(capture_state));
        num = state.cap_num;
        malloc_size = (sizeof(struct pcap_info))* num;

        buf = malloc(malloc_size);
        if(NULL == buf)
        {
            http_out("Out of memory");
            return DefaultTranslate__I_OK;
        }
        memset(buf, 0, malloc_size);

        copied = 0;
        ret = COPY_CONTINUE;
        while ((COPY_CONTINUE == ret) && malloc_size > 0)
        {
            ret = netmode_cap_callback(NETMODE_SYSCALL_DOWNLOAD_CAP,
                                       (u8*)buf + PCAP_NUMBER*copied, min(malloc_size, PCAP_NUMBER));
            malloc_size -= PCAP_NUMBER;
            copied++;
        }

        if(ret != ERROR_SUCCESS)
        {
            http_out("Syscall fail!");
            return DefaultTranslate__I_OK;
        }
        http_out_buf(&file_head, sizeof(struct pcap_file_header));

        for(i = 0; i < num; i++)
        {
            http_out_buf(&buf[i], sizeof(struct pcap_pkthdr)+SWAPLONG(buf[i].pcap_head.len));
        }

        WEB_SEND_EX_OPERLOG_QUICK(0, "Exported capture file: [capture_file.pcap].");
        free(buf);
        return DefaultTranslate__I_OK;
    }

    return user_op->parent_translateValue(user_op, result_context,
                                          original_flag, helper, position, trans_type, trans_data, trans_data_int);
}
Ejemplo n.º 5
0
static void
swap_hdr(struct pcap_file_header *hp)
{
	hp->version_major = SWAPSHORT(hp->version_major);
	hp->version_minor = SWAPSHORT(hp->version_minor);
	hp->thiszone = SWAPLONG(hp->thiszone);
	hp->sigfigs = SWAPLONG(hp->sigfigs);
	hp->snaplen = SWAPLONG(hp->snaplen);
	hp->linktype = SWAPLONG(hp->linktype);
}
Ejemplo n.º 6
0
/*
 * The DLT_NFLOG "packets" have a mixture of big-endian and host-byte-order
 * data.  They begin with a fixed-length header with big-endian fields,
 * followed by a set of TLVs, where the type and length are in host
 * byte order but the values are either big-endian or are a raw byte
 * sequence that's the same regardless of the host's byte order.
 *
 * When reading a DLT_NFLOG capture file, we need to convert the type
 * and length values from the byte order of the host that wrote the
 * file to the byte order of this host.
 */
static void
swap_nflog_header(const struct pcap_pkthdr *hdr, u_char *buf)
{
	u_char *p = buf;
	nflog_hdr_t *nfhdr = (nflog_hdr_t *)buf;
	nflog_tlv_t *tlv;
	u_int caplen = hdr->caplen;
	u_int length = hdr->len;
	uint16_t size;

	if (caplen < (u_int) sizeof(nflog_hdr_t) ||
	    length < (u_int) sizeof(nflog_hdr_t)) {
		/* Not enough data to have any TLVs. */
		return;
	}

	if (nfhdr->nflog_version != 0) {
		/* Unknown NFLOG version */
		return;
	}

	length -= sizeof(nflog_hdr_t);
	caplen -= sizeof(nflog_hdr_t);
	p += sizeof(nflog_hdr_t);

	while (caplen >= sizeof(nflog_tlv_t)) {
		tlv = (nflog_tlv_t *) p;

		/* Swap the type and length. */
		tlv->tlv_type = SWAPSHORT(tlv->tlv_type);
		tlv->tlv_length = SWAPSHORT(tlv->tlv_length);

		/* Get the length of the TLV. */
		size = tlv->tlv_length;
		if (size % 4 != 0)
			size += 4 - size % 4;

		/* Is the TLV's length less than the minimum? */
		if (size < sizeof(nflog_tlv_t)) {
			/* Yes. Give up now. */
			return;
		}

		/* Do we have enough data for the full TLV? */
		if (caplen < size || length < size) {
			/* No. */
			return;
		}

		/* Skip over the TLV. */
		length -= size;
		caplen -= size;
		p += size;
	}
}
Ejemplo n.º 7
0
void active_door_swap(active_door *ad, int swap)
{
	if (!swap)
		return;
	
	ad->n_parts = SWAPINT(ad->n_parts);
	ad->front_wallnum[0] = SWAPSHORT(ad->front_wallnum[0]);
	ad->front_wallnum[1] = SWAPSHORT(ad->front_wallnum[1]);
	ad->back_wallnum[0] = SWAPSHORT(ad->back_wallnum[0]);
	ad->back_wallnum[1] = SWAPSHORT(ad->back_wallnum[1]);
	ad->time = SWAPINT(ad->time);
}
Ejemplo n.º 8
0
void control_center_triggers_swap(control_center_triggers *cct, int swap)
{
	int i;
	
	if (!swap)
		return;
	
	cct->num_links = SWAPSHORT(cct->num_links);
	for (i = 0; i < MAX_WALLS_PER_LINK; i++)
		cct->seg[i] = SWAPSHORT(cct->seg[i]);
	for (i = 0; i < MAX_WALLS_PER_LINK; i++)
		cct->side[i] = SWAPSHORT(cct->side[i]);
}
Ejemplo n.º 9
0
static cst_lts_phone apply_model(char *vals,cst_lts_addr start,
				 const cst_lts_model *model)
{
    /* because some machines (ipaq/mips) can't deal with addrs not on     */
    /* word boundaries we use a static and copy the rule values each time */
    /* so we know its properly aligned                                    */
    /* Hmm this still might be wrong on some machines that align the      */
    /* structure cst_lts_rules differently                                */
    cst_lts_rule state;
    short nstate;
    static const int sizeof_cst_lts_rule = 6;

    memmove(&state,&model[start*sizeof_cst_lts_rule],sizeof_cst_lts_rule);
    for ( ;
	 state.feat != CST_LTS_EOR;
	)
    {
	if (vals[state.feat] == state.val)
	    nstate = state.qtrue;
	else
	    nstate = state.qfalse;
	/* This should really happen at compilation time */
	if (CST_BIG_ENDIAN)
	    nstate = SWAPSHORT(nstate);

	memmove(&state,&model[nstate*sizeof_cst_lts_rule],sizeof_cst_lts_rule);
    }

    return (cst_lts_phone)state.val;
}
Ejemplo n.º 10
0
void cloaking_wall_swap(cloaking_wall *cw, int swap)
{
	int i;
	
	if (!swap)
		return;
	
	cw->front_wallnum = SWAPSHORT(cw->front_wallnum);
	cw->back_wallnum = SWAPSHORT(cw->back_wallnum);
	for (i = 0; i < 4; i++)
	{
		cw->front_ls[i] = SWAPINT(cw->front_ls[i]);
		cw->back_ls[i] = SWAPINT(cw->back_ls[i]);
	}
	cw->time = SWAPINT(cw->time);
}
Ejemplo n.º 11
0
static cst_lts_phone apply_model(cst_lts_letter *vals,cst_lts_addr start, 
				 const cst_lts_model *model)
{
    /* because some machines (ipaq/mips) can't deal with addrs not on     */
    /* word boundaries we use a static and copy the rule values each time */
    /* so we know its properly aligned                                    */
    /* Hmm this still might be wrong on some machines that align the      */
    /* structure cst_lts_rules differently                                */
    cst_lts_rule state;
    unsigned short nstate;
    static const int sizeof_cst_lts_rule = 6;

    cst_lts_get_state(&state,model,start,sizeof_cst_lts_rule);
    for ( ;
	 state.feat != CST_LTS_EOR;
	)
    {
/*	printf("%s %c %c %d\n",vals,vals[state.feat],state.val,
	(vals[state.feat] == state.val) ? 1 : 0); */
	if (vals[state.feat] == state.val)
	    nstate = state.qtrue;
	else
	    nstate = state.qfalse;
	/* This should really happen at compilation time */
#ifndef FLITE_PLUS_HTS_ENGINE
	if (CST_BIG_ENDIAN)
	    nstate = SWAPSHORT(nstate);
#endif /* !FLITE_PLUS_HTS_ENGINE */

	cst_lts_get_state(&state,model,nstate,sizeof_cst_lts_rule);
    }

    return (cst_lts_phone)state.val;
}
Ejemplo n.º 12
0
Archivo: hu_lib.c Proyecto: yoyz/psp
// sorta called by HU_Erase and just better darn get things straight
void HUlib_eraseTextLine(hu_textline_t* l)
{
    int                 lh;
    int                 y;
    int                 yoffset;
    static boolean      lastautomapactive = true;

    // Only erases when NOT in automap and the screen is reduced,
    // and the text must either need updating or refreshing
    // (because of a recent change back from the automap)

    if ((!automapactive || maponhu) &&
        viewwindowx && l->needsupdate)
    {
        lh = SWAPSHORT(l->f[0]->height) + 1;
        for (y=l->y,yoffset=y*SCREENWIDTH ; y<l->y+lh ; y++,yoffset+=SCREENWIDTH)
        {
            if (y < viewwindowy || y >= viewwindowy + viewheight)
                R_VideoErase(yoffset, SCREENWIDTH); // erase entire line
            else
            {
                R_VideoErase(yoffset, viewwindowx); // erase left border
                R_VideoErase(yoffset + viewwindowx + viewwidth, viewwindowx);
                // erase right border
            }
        }
    }

    lastautomapactive = automapactive;
    if (l->needsupdate) l->needsupdate--;

}
Ejemplo n.º 13
0
static int load_diphs(CONFIG *config)
{
	int i,j;

	if (dico == 0)
	    dico = walloc(FRAME,NFRAMES);

	if((config->dfd=fopen(config->diphone_file,"rb")) == NULL) {
	    fprintf(stderr,"Can't open file %s\n",config->diphone_file);
	    return -1;
	}

	/* zero the first one... */
	for(i=0;i<FR_DATA;i++)
	    dico[0].frame[i] = 0;
	dico[0].frame[2] = FR_SZ;

	/* note the use of 1 to tie in with indexing  */
	for(i=1;(fread((char *)&dico[i],sizeof(FRAME),1,config->dfd) != 0) && 
	    (i < NFRAMES);i++) 
	{
		;
	}

	/* check the first little bit is as we expect...  */
	if ((dico[1].frame[0] != 181) || (dico[1].frame[1] != 176))
	{
	    if ((SWAPSHORT(dico[1].frame[0]) == 181) &&
		(SWAPSHORT(dico[1].frame[1]) == 176))
	    {			/* Its bytes swapped */
		for (j=1;j<i;j++)
		    swap_bytes_short(dico[j].frame,FR_DATA);
	    }
	    else
	    {
		fprintf(stderr,"File %s apparently corrupted\n",
			config->diphone_file);
		fclose(config->dfd);
		return -1;
	    }
	}

	fclose(config->dfd);
	return 0;
}
Ejemplo n.º 14
0
Archivo: hu_lib.c Proyecto: yoyz/psp
void
HUlib_drawTextLine
( hu_textline_t*        l,
  boolean               drawcursor )
{

    int                 i;
    int                 w;
    int                 x;
    unsigned char       c;

    // draw the new stuff
    x = l->x;
    for (i=0;i<l->len;i++)
    {
        c = toupper(l->l[i]);
        if (c != ' '
            && c >= l->sc
            && c <= '_')
        {
            w = SWAPSHORT(l->f[c - l->sc]->width);
            if (x+w > SCREENWIDTH)
                break;
            V_DrawPatchDirect(x, l->y, FG, l->f[c - l->sc]);
            x += w;
        }
        else
        {
            x += 4;
            if (x >= SCREENWIDTH)
                break;
        }
    }

    // draw the cursor if requested
    if (drawcursor
        && x + SWAPSHORT(l->f['_' - l->sc]->width) <= SCREENWIDTH)
    {
        V_DrawPatchDirect(x, l->y, FG, l->f['_' - l->sc]);
    }
}
static struct option_header *
get_opthdr_from_block_data(pcap_t *p, struct block_cursor *cursor, char *errbuf)
{
	struct option_header *opthdr;

	opthdr = get_from_block_data(cursor, sizeof(*opthdr), errbuf);
	if (opthdr == NULL) {
		/*
		 * Option header is cut short.
		 */
		return (NULL);
	}

	/*
	 * Byte-swap it if necessary.
	 */
	if (p->swapped) {
		opthdr->option_code = SWAPSHORT(opthdr->option_code);
		opthdr->option_length = SWAPSHORT((uint32_t)opthdr->option_length);
	}

	return (opthdr);
}
Ejemplo n.º 16
0
Archivo: r_data.c Proyecto: yoyz/psp
//
// R_InitSpriteLumps
// Finds the width and hoffset of all sprites in the wad,
//  so the sprite does not need to be cached completely
//  just for having the header info ready during rendering.
//
void R_InitSpriteLumps (void)
{
    int		i;
    patch_t	*patch;

    firstspritelump = W_GetNumForName ("S_START") + 1;
    lastspritelump = W_GetNumForName ("S_END") - 1;

    numspritelumps = lastspritelump - firstspritelump + 1;
    spritewidth = Z_Malloc (numspritelumps*4, PU_STATIC, 0);
    spriteoffset = Z_Malloc (numspritelumps*4, PU_STATIC, 0);
    spritetopoffset = Z_Malloc (numspritelumps*4, PU_STATIC, 0);

    for (i=0 ; i< numspritelumps ; i++)
    {
	if (!(i&63))
	    printf (".");

	patch = W_CacheLumpNum (firstspritelump+i, PU_CACHE);
	spritewidth[i] = SWAPSHORT(patch->width)<<FRACBITS;
	spriteoffset[i] = SWAPSHORT(patch->leftoffset)<<FRACBITS;
	spritetopoffset[i] = SWAPSHORT(patch->topoffset)<<FRACBITS;
    }
}
Ejemplo n.º 17
0
struct pcapng_option_header *
get_opthdr_from_block_data(struct pcapng_option_header *opthdr, int swapped,
						   struct block_cursor *cursor, char *errbuf)
{
	struct pcapng_option_header *optp;
	
	optp = get_from_block_data(cursor, sizeof(*opthdr), errbuf);
	if (optp == NULL) {
		/*
		 * Option header is cut short.
		 */
		return (NULL);
	}
	*opthdr = *optp;
	/*
	 * Byte-swap it if necessary.
	 */
	if (swapped) {
		opthdr->option_code = SWAPSHORT(opthdr->option_code);
		opthdr->option_length = SWAPSHORT(opthdr->option_length);
	}
	
	return (opthdr);
}
Ejemplo n.º 18
0
int tsFreePhoneImplementation::read_header(CONFIG *config)
{
	char thdr[256]; /* under certain circumstances may not be enuf  */
	short thdr_s[100];
	int type = 0;
	int i = 0;

	/* read header and go back (and point at the beginning of the DATA if necessary)  */

	if(fread(thdr,1,8,config->dfd) == 0) {
		(void)fprintf(stderr,"Problem with file %s\n",config->diphone_file);
	}
	while(thdr[i++] == 'G') {
		type += 1;
	}

	rewind(config->dfd);
	
	/* interesting info except for frame size  */
	/* and number of frames total  */
	if (type == 2) {
		fread((short *)thdr_s,2,8,config->dfd);
		if(FT_LITTLE_ENDIAN) {
			for(i=0;i<8;i++) {
				*(thdr+i) = SWAPSHORT(*(thdr+i));
			}
		}
		config->fr_data = thdr_s[4];
		config->ncoeffs = thdr_s[5];
		config->sr = thdr_s[6];
		config->norm = thdr_s[7];
		fread((short *)thdr_s,2,config->fr_data-8,config->dfd);
	} else {
		(void)fprintf(stderr,"Problem with file %s\n",config->diphone_file);
		exit(2);
	}
	return(type);
}
Ejemplo n.º 19
0
Archivo: hu_lib.c Proyecto: yoyz/psp
void
HUlib_initSText
( hu_stext_t*   s,
  int           x,
  int           y,
  int           h,
  patch_t**     font,
  int           startchar,
  boolean*      on )
{

    int i;

    s->h = h;
    s->on = on;
    s->laston = true;
    s->cl = 0;
    for (i=0;i<h;i++)
        HUlib_initTextLine(&s->l[i],
                           x, y - i*(SWAPSHORT(font[0]->height)+1),
                           font, startchar);

}
Ejemplo n.º 20
0
void fixang_swap(fixang *f)
{
	*f = (fixang)SWAPSHORT((short)*f);
}
Ejemplo n.º 21
0
void short_swap(short *s)
{
	*s = SWAPSHORT(*s);
}
Ejemplo n.º 22
0
/*
 * Check whether this is a pcap savefile and, if it is, extract the
 * relevant information from the header.
 */
int
pcap_check_header(pcap_t *p, bpf_u_int32 magic, FILE *fp, char *errbuf)
{
	struct pcap_file_header hdr;
	size_t amt_read;

	/*
	 * Check whether the first 4 bytes of the file are the magic
	 * number for a pcap savefile, or for a byte-swapped pcap
	 * savefile.
	 */
	if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) {
		magic = SWAPLONG(magic);
		if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC)
			return (0);	/* nope */
		p->sf.swapped = 1;
	}

	/*
	 * They are.  Put the magic number in the header, and read
	 * the rest of the header.
	 */
	hdr.magic = magic;
	amt_read = fread(((char *)&hdr) + sizeof hdr.magic, 1,
	    sizeof(hdr) - sizeof(hdr.magic), fp);
	if (amt_read != sizeof(hdr) - sizeof(hdr.magic)) {
		if (ferror(fp)) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "error reading dump file: %s",
			    pcap_strerror(errno));
		} else {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "truncated dump file; tried to read %lu file header bytes, only got %lu",
			    (unsigned long)sizeof(hdr),
			    (unsigned long)amt_read);
		}
		return (-1);
	}

	/*
	 * If it's a byte-swapped capture file, byte-swap the header.
	 */
	if (p->sf.swapped) {
		hdr.version_major = SWAPSHORT(hdr.version_major);
		hdr.version_minor = SWAPSHORT(hdr.version_minor);
		hdr.thiszone = SWAPLONG(hdr.thiszone);
		hdr.sigfigs = SWAPLONG(hdr.sigfigs);
		hdr.snaplen = SWAPLONG(hdr.snaplen);
		hdr.linktype = SWAPLONG(hdr.linktype);
	}

	if (hdr.version_major < PCAP_VERSION_MAJOR) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "archaic pcap savefile format");
		return (-1);
	}
	p->sf.version_major = hdr.version_major;
	p->sf.version_minor = hdr.version_minor;
	p->tzoff = hdr.thiszone;
	p->snapshot = hdr.snaplen;
	p->linktype = linktype_to_dlt(LT_LINKTYPE(hdr.linktype));
	p->linktype_ext = LT_LINKTYPE_EXT(hdr.linktype);

	p->sf.next_packet_op = pcap_next_packet;

	/*
	 * We interchanged the caplen and len fields at version 2.3,
	 * in order to match the bpf header layout.  But unfortunately
	 * some files were written with version 2.3 in their headers
	 * but without the interchanged fields.
	 *
	 * In addition, DG/UX tcpdump writes out files with a version
	 * number of 543.0, and with the caplen and len fields in the
	 * pre-2.3 order.
	 */
	switch (hdr.version_major) {

	case 2:
		if (hdr.version_minor < 3)
			p->sf.lengths_swapped = SWAPPED;
		else if (hdr.version_minor == 3)
			p->sf.lengths_swapped = MAYBE_SWAPPED;
		else
			p->sf.lengths_swapped = NOT_SWAPPED;
		break;

	case 543:
		p->sf.lengths_swapped = SWAPPED;
		break;

	default:
		p->sf.lengths_swapped = NOT_SWAPPED;
		break;
	}

	if (magic == KUZNETZOV_TCPDUMP_MAGIC) {
		/*
		 * XXX - the patch that's in some versions of libpcap
		 * changes the packet header but not the magic number,
		 * and some other versions with this magic number have
		 * some extra debugging information in the packet header;
		 * we'd have to use some hacks^H^H^H^H^Hheuristics to
		 * detect those variants.
		 *
		 * Ethereal does that, but it does so by trying to read
		 * the first two packets of the file with each of the
		 * record header formats.  That currently means it seeks
		 * backwards and retries the reads, which doesn't work
		 * on pipes.  We want to be able to read from a pipe, so
		 * that strategy won't work; we'd have to buffer some
		 * data ourselves and read from that buffer in order to
		 * make that work.
		 */
		p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);

		if (p->linktype == DLT_EN10MB) {
			/*
			 * This capture might have been done in raw mode
			 * or cooked mode.
			 *
			 * If it was done in cooked mode, p->snapshot was
			 * passed to recvfrom() as the buffer size, meaning
			 * that the most packet data that would be copied
			 * would be p->snapshot.  However, a faked Ethernet
			 * header would then have been added to it, so the
			 * most data that would be in a packet in the file
			 * would be p->snapshot + 14.
			 *
			 * We can't easily tell whether the capture was done
			 * in raw mode or cooked mode, so we'll assume it was
			 * cooked mode, and add 14 to the snapshot length.
			 * That means that, for a raw capture, the snapshot
			 * length will be misleading if you use it to figure
			 * out why a capture doesn't have all the packet data,
			 * but there's not much we can do to avoid that.
			 */
			p->snapshot += 14;
		}
	} else
		p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);

	/*
	 * Allocate a buffer for the packet data.
	 */
	p->bufsize = p->snapshot;
	if (p->bufsize <= 0) {
		/*
		 * Bogus snapshot length; use 64KiB as a fallback.
		 */
		p->bufsize = 65536;
	}
	p->buffer = malloc(p->bufsize);
	if (p->buffer == NULL) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
		return (-1);
	}

	return (1);
}
Ejemplo n.º 23
0
void tsFreePhoneImplementation::load_diphs(CONFIG *config)
{
	int i;
	int j;
	char *diphone_file;
	int type;
	int calc_mlp = 1;
	int tot_fr = 0;
	int tot_short = 0;
	short *malloc_ptr;

	diphone_file = (char*) malloc(strlen(config->diphone_file)+10);
	strcpy(diphone_file,config->diphone_file);
	strcat(diphone_file,".dat");

	if((config->dfd=fopen(diphone_file,"r")) == NULL) {
		(void)fprintf(stderr,"Can't open file %s\n",diphone_file);
		exit(1);
	}

	type = read_header(config);
	if(config->norm) {
		calc_mlp = 0;
	}

	malloc_ptr = (short *) malloc((sizeof(short )*config->fr_data)*NFRAMES);
  	for(j=0;j<NFRAMES;j++) {
		dico[j].frame = (short *)(malloc_ptr + config->fr_data*j);
	}

	/* zero the first one... */
	for(i=0;i<config->fr_data;i++) {
		dico[0].frame[i] = 0;
	}

	/* note the use of 1 to tie in with indexing  */
	tot_fr = 0;
	tot_short = 0;
	if((tot_short=fread((short *)(dico[1].frame),sizeof(short),config->fr_data*(NFRAMES-1),config->dfd))!=0) {
		if(FT_LITTLE_ENDIAN) {
			for(i=1;i<=tot_short;i++) {
				*(malloc_ptr+i) = SWAPSHORT(*(malloc_ptr+i));
			}
		}
	}
	tot_fr = tot_short/config->fr_data;
	

	for(i=1;i<=tot_fr;i++) {
		if(calc_mlp) {
			if(dico[i].frame[0] > config->norm) {
				config->norm = dico[i].frame[0];
			}
		}
	}
	config->fr_sz = (int)dico[1].frame[2];
	dico[0].frame[2] = config->fr_sz;
#ifdef DEBUG
	for(i=1;i<=tot_fr;i++) {
		if(config->fr_sz != (int)dico[i].frame[2]) {
			(void)fprintf(stderr,"New frame size (frame %d): %d\n",i,(int)dico[i].frame[2]);
		}
	}
#endif

	fclose(config->dfd);

	free(diphone_file);
}
Ejemplo n.º 24
0
/*
 * The DLT_USB_LINUX and DLT_USB_LINUX_MMAPPED headers are in host
 * byte order when capturing (it's supplied directly from a
 * memory-mapped buffer shared by the kernel).
 *
 * When reading a DLT_USB_LINUX or DLT_USB_LINUX_MMAPPED capture file,
 * we need to convert it from the byte order of the host that wrote
 * the file to this host's byte order.
 */
static void
swap_linux_usb_header(const struct pcap_pkthdr *hdr, u_char *buf,
    int header_len_64_bytes)
{
	pcap_usb_header_mmapped *uhdr = (pcap_usb_header_mmapped *)buf;
	bpf_u_int32 offset = 0;
	usb_isodesc *pisodesc;
	int32_t numdesc, i;

	/*
	 * "offset" is the offset *past* the field we're swapping;
	 * we skip the field *before* checking to make sure
	 * the captured data length includes the entire field.
	 */

	/*
	 * The URB id is a totally opaque value; do we really need to 
	 * convert it to the reading host's byte order???
	 */
	offset += 8;			/* skip past id */
	if (hdr->caplen < offset)
		return;
	uhdr->id = SWAPLL(uhdr->id);

	offset += 4;			/* skip past various 1-byte fields */

	offset += 2;			/* skip past bus_id */
	if (hdr->caplen < offset)
		return;
	uhdr->bus_id = SWAPSHORT(uhdr->bus_id);

	offset += 2;			/* skip past various 1-byte fields */

	offset += 8;			/* skip past ts_sec */
	if (hdr->caplen < offset)
		return;
	uhdr->ts_sec = SWAPLL(uhdr->ts_sec);

	offset += 4;			/* skip past ts_usec */
	if (hdr->caplen < offset)
		return;
	uhdr->ts_usec = SWAPLONG(uhdr->ts_usec);

	offset += 4;			/* skip past status */
	if (hdr->caplen < offset)
		return;
	uhdr->status = SWAPLONG(uhdr->status);

	offset += 4;			/* skip past urb_len */
	if (hdr->caplen < offset)
		return;
	uhdr->urb_len = SWAPLONG(uhdr->urb_len);

	offset += 4;			/* skip past data_len */
	if (hdr->caplen < offset)
		return;
	uhdr->data_len = SWAPLONG(uhdr->data_len);

	if (uhdr->transfer_type == URB_ISOCHRONOUS) {
		offset += 4;			/* skip past s.iso.error_count */
		if (hdr->caplen < offset)
			return;
		uhdr->s.iso.error_count = SWAPLONG(uhdr->s.iso.error_count);

		offset += 4;			/* skip past s.iso.numdesc */
		if (hdr->caplen < offset)
			return;
		uhdr->s.iso.numdesc = SWAPLONG(uhdr->s.iso.numdesc);
	} else
		offset += 8;			/* skip USB setup header */

	if (header_len_64_bytes) {
		/*
		 * This is either the "version 1" header, with
		 * 16 bytes of additional fields at the end, or
		 * a "version 0" header from a memory-mapped
		 * capture, with 16 bytes of zeroed-out padding
		 * at the end.  Byte swap them as if this were
		 * a "version 1" header.
		 */
		offset += 4;			/* skip past interval */
		if (hdr->caplen < offset)
			return;
		uhdr->interval = SWAPLONG(uhdr->interval);

		offset += 4;			/* skip past start_frame */
		if (hdr->caplen < offset)
			return;
		uhdr->start_frame = SWAPLONG(uhdr->start_frame);

		offset += 4;			/* skip past xfer_flags */
		if (hdr->caplen < offset)
			return;
		uhdr->xfer_flags = SWAPLONG(uhdr->xfer_flags);

		offset += 4;			/* skip past ndesc */
		if (hdr->caplen < offset)
			return;
		uhdr->ndesc = SWAPLONG(uhdr->ndesc);
	}	

	if (uhdr->transfer_type == URB_ISOCHRONOUS) {
		/* swap the values in struct linux_usb_isodesc */
		pisodesc = (usb_isodesc *)(void *)(buf+offset);
		numdesc = uhdr->s.iso.numdesc;
		for (i = 0; i < numdesc; i++) {
			offset += 4;		/* skip past status */
			if (hdr->caplen < offset)
				return;
			pisodesc->status = SWAPLONG(pisodesc->status);

			offset += 4;		/* skip past offset */
			if (hdr->caplen < offset)
				return;
			pisodesc->offset = SWAPLONG(pisodesc->offset);

			offset += 4;		/* skip past len */
			if (hdr->caplen < offset)
				return;
			pisodesc->len = SWAPLONG(pisodesc->len);

			offset += 4;		/* skip past padding */

			pisodesc++;
		}
	}
}
/*
 * Check whether this is a pcap-ng savefile and, if it is, extract the
 * relevant information from the header.
 */
pcap_t *
pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf,
    int *err)
{
	size_t amt_read;
	bpf_u_int32 total_length;
	bpf_u_int32 byte_order_magic;
	struct block_header *bhdrp;
	struct section_header_block *shbp;
	pcap_t *p;
	int swapped = 0;
	struct pcap_ng_sf *ps;
	int status;
	struct block_cursor cursor;
	struct interface_description_block *idbp;

	/*
	 * Assume no read errors.
	 */
	*err = 0;

	/*
	 * Check whether the first 4 bytes of the file are the block
	 * type for a pcap-ng savefile.
	 */
	if (magic != BT_SHB) {
		/*
		 * XXX - check whether this looks like what the block
		 * type would be after being munged by mapping between
		 * UN*X and DOS/Windows text file format and, if it
		 * does, look for the byte-order magic number in
		 * the appropriate place and, if we find it, report
		 * this as possibly being a pcap-ng file transferred
		 * between UN*X and Windows in text file format?
		 */
		return (NULL);	/* nope */
	}

	/*
	 * OK, they are.  However, that's just \n\r\r\n, so it could,
	 * conceivably, be an ordinary text file.
	 *
	 * It could not, however, conceivably be any other type of
	 * capture file, so we can read the rest of the putative
	 * Section Header Block; put the block type in the common
	 * header, read the rest of the common header and the
	 * fixed-length portion of the SHB, and look for the byte-order
	 * magic value.
	 */
	amt_read = fread(&total_length, 1, sizeof(total_length), fp);
	if (amt_read < sizeof(total_length)) {
		if (ferror(fp)) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "error reading dump file: %s",
			    pcap_strerror(errno));
			*err = 1;
			return (NULL);	/* fail */
		}

		/*
		 * Possibly a weird short text file, so just say
		 * "not pcap-ng".
		 */
		return (NULL);
	}
	amt_read = fread(&byte_order_magic, 1, sizeof(byte_order_magic), fp);
	if (amt_read < sizeof(byte_order_magic)) {
		if (ferror(fp)) {
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "error reading dump file: %s",
			    pcap_strerror(errno));
			*err = 1;
			return (NULL);	/* fail */
		}

		/*
		 * Possibly a weird short text file, so just say
		 * "not pcap-ng".
		 */
		return (NULL);
	}
	if (byte_order_magic != BYTE_ORDER_MAGIC) {
		byte_order_magic = SWAPLONG(byte_order_magic);
		if (byte_order_magic != BYTE_ORDER_MAGIC) {
			/*
			 * Not a pcap-ng file.
			 */
			return (NULL);
		}
		swapped = 1;
		total_length = SWAPLONG(total_length);
	}

	/*
	 * Check the sanity of the total length.
	 */
	if (total_length < sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer)) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "Section Header Block in pcap-ng dump file has a length of %u < %lu",
		    total_length,
		    (unsigned long)(sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer)));
		*err = 1;
		return (NULL);
	}

	/*
	 * OK, this is a good pcap-ng file.
	 * Allocate a pcap_t for it.
	 */
	p = pcap_open_offline_common(errbuf, sizeof (struct pcap_ng_sf));
	if (p == NULL) {
		/* Allocation failed. */
		*err = 1;
		return (NULL);
	}
	p->swapped = swapped;
	ps = p->priv;

	/*
	 * What precision does the user want?
	 */
	switch (precision) {

	case PCAP_TSTAMP_PRECISION_MICRO:
		ps->user_tsresol = 1000000;
		break;

	case PCAP_TSTAMP_PRECISION_NANO:
		ps->user_tsresol = 1000000000;
		break;

	default:
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "unknown time stamp resolution %u", precision);
		free(p);
		*err = 1;
		return (NULL);
	}

	p->opt.tstamp_precision = precision;

	/*
	 * Allocate a buffer into which to read blocks.  We default to
	 * the maximum of:
	 *
	 *	the total length of the SHB for which we read the header;
	 *
	 *	2K, which should be more than large enough for an Enhanced
	 *	Packet Block containing a full-size Ethernet frame, and
	 *	leaving room for some options.
	 *
	 * If we find a bigger block, we reallocate the buffer.
	 */
	p->bufsize = 2048;
	if (p->bufsize < (int)total_length)
		p->bufsize = total_length;
	p->buffer = malloc(p->bufsize);
	if (p->buffer == NULL) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
		free(p);
		*err = 1;
		return (NULL);
	}

	/*
	 * Copy the stuff we've read to the buffer, and read the rest
	 * of the SHB.
	 */
	bhdrp = (struct block_header *)(void *)p->buffer;
	shbp = (struct section_header_block *)(void *)(p->buffer + sizeof(struct block_header));
	bhdrp->block_type = magic;
	bhdrp->total_length = total_length;
	shbp->byte_order_magic = byte_order_magic;
	if (read_bytes(fp,
	    p->buffer + (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
	    total_length - (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
	    1, errbuf) == -1)
		goto fail;

	if (p->swapped) {
		/*
		 * Byte-swap the fields we've read.
		 */
		shbp->major_version = SWAPSHORT((uint32_t)shbp->major_version);
		shbp->minor_version = SWAPSHORT((uint32_t)shbp->minor_version);

		/*
		 * XXX - we don't care about the section length.
		 */
	}
	if (shbp->major_version != PCAP_NG_VERSION_MAJOR) {
		snprintf(errbuf, PCAP_ERRBUF_SIZE,
		    "unknown pcap-ng savefile major version number %u",
		    shbp->major_version);
		goto fail;
	}
	p->version_major = shbp->major_version;
	p->version_minor = shbp->minor_version;

	/*
	 * Save the time stamp resolution the user requested.
	 */
	p->opt.tstamp_precision = precision;

	/*
	 * Now start looking for an Interface Description Block.
	 */
	for (;;) {
		/*
		 * Read the next block.
		 */
		status = read_block(fp, p, &cursor, errbuf);
		if (status == 0) {
			/* EOF - no IDB in this file */
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "the capture file has no Interface Description Blocks");
			goto fail;
		}
		if (status == -1)
			goto fail;	/* error */
		switch (cursor.block_type) {

		case BT_IDB:
			/*
			 * Get a pointer to the fixed-length portion of the
			 * IDB.
			 */
			idbp = get_from_block_data(&cursor, sizeof(*idbp),
			    errbuf);
			if (idbp == NULL)
				goto fail;	/* error */

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				idbp->linktype = SWAPSHORT((uint16_t)idbp->linktype);
				idbp->snaplen = SWAPLONG(idbp->snaplen);
			}

			/*
			 * Try to add this interface.
			 */
			if (!add_interface(p, &cursor, errbuf))
				goto fail;
			goto done;

		case BT_EPB:
		case BT_SPB:
		case BT_PB:
			/*
			 * Saw a packet before we saw any IDBs.  That's
			 * not valid, as we don't know what link-layer
			 * encapsulation the packet has.
			 */
			snprintf(errbuf, PCAP_ERRBUF_SIZE,
			    "the capture file has a packet block before any Interface Description Blocks");
			goto fail;

		default:
			/*
			 * Just ignore it.
			 */
			break;
		}
	}

done:
	p->tzoff = 0;	/* XXX - not used in pcap */
	p->snapshot = idbp->snaplen;
	p->linktype = linktype_to_dlt(idbp->linktype);
	p->linktype_ext = 0;

	p->next_packet_op = pcap_ng_next_packet;
	p->cleanup_op = pcap_ng_cleanup;

	return (p);

fail:
	free(ps->ifaces);
	free(p->buffer);
	free(p);
	*err = 1;
	return (NULL);
}
Ejemplo n.º 26
0
Archivo: interp.c Proyecto: paud/d2x-xl
inline void FixAngSwap (fixang *f)
{
*f = (fixang) SWAPSHORT ((short)*f);
}
/*
 * Read and return the next packet from the savefile.  Return the header
 * in hdr and a pointer to the contents in data.  Return 0 on success, 1
 * if there were no more packets, and -1 on an error.
 */
static int
pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
{
	struct pcap_ng_sf *ps = p->priv;
	struct block_cursor cursor;
	int status;
	struct enhanced_packet_block *epbp;
	struct simple_packet_block *spbp;
	struct packet_block *pbp;
	bpf_u_int32 interface_id = 0xFFFFFFFF;
	struct interface_description_block *idbp;
	struct section_header_block *shbp;
	FILE *fp = p->rfile;
	u_int64_t t, sec, frac;

	/*
	 * Look for an Enhanced Packet Block, a Simple Packet Block,
	 * or a Packet Block.
	 */
	for (;;) {
		/*
		 * Read the block type and length; those are common
		 * to all blocks.
		 */
		status = read_block(fp, p, &cursor, p->errbuf);
		if (status == 0)
			return (1);	/* EOF */
		if (status == -1)
			return (-1);	/* error */
		switch (cursor.block_type) {

		case BT_EPB:
			/*
			 * Get a pointer to the fixed-length portion of the
			 * EPB.
			 */
			epbp = get_from_block_data(&cursor, sizeof(*epbp),
			    p->errbuf);
			if (epbp == NULL)
				return (-1);	/* error */

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				/* these were written in opposite byte order */
				interface_id = SWAPLONG(epbp->interface_id);
				hdr->caplen = SWAPLONG(epbp->caplen);
				hdr->len = SWAPLONG(epbp->len);
				t = ((u_int64_t)SWAPLONG(epbp->timestamp_high)) << 32 |
				    SWAPLONG(epbp->timestamp_low);
			} else {
				interface_id = epbp->interface_id;
				hdr->caplen = epbp->caplen;
				hdr->len = epbp->len;
				t = ((u_int64_t)epbp->timestamp_high) << 32 |
				    epbp->timestamp_low;
			}
			goto found;

		case BT_SPB:
			/*
			 * Get a pointer to the fixed-length portion of the
			 * SPB.
			 */
			spbp = get_from_block_data(&cursor, sizeof(*spbp),
			    p->errbuf);
			if (spbp == NULL)
				return (-1);	/* error */

			/*
			 * SPB packets are assumed to have arrived on
			 * the first interface.
			 */
			interface_id = 0;

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				/* these were written in opposite byte order */
				hdr->len = SWAPLONG(spbp->len);
			} else
				hdr->len = spbp->len;

			/*
			 * The SPB doesn't give the captured length;
			 * it's the minimum of the snapshot length
			 * and the packet length.
			 */
			hdr->caplen = hdr->len;
			if ((int)hdr->caplen > p->snapshot)
				hdr->caplen = p->snapshot;
			t = 0;	/* no time stamps */
			goto found;

		case BT_PB:
			/*
			 * Get a pointer to the fixed-length portion of the
			 * PB.
			 */
			pbp = get_from_block_data(&cursor, sizeof(*pbp),
			    p->errbuf);
			if (pbp == NULL)
				return (-1);	/* error */

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				/* these were written in opposite byte order */
				interface_id = SWAPSHORT((uint32_t)pbp->interface_id);
				hdr->caplen = SWAPLONG(pbp->caplen);
				hdr->len = SWAPLONG(pbp->len);
				t = ((u_int64_t)SWAPLONG(pbp->timestamp_high)) << 32 |
				    SWAPLONG(pbp->timestamp_low);
			} else {
				interface_id = pbp->interface_id;
				hdr->caplen = pbp->caplen;
				hdr->len = pbp->len;
				t = ((u_int64_t)pbp->timestamp_high) << 32 |
				    pbp->timestamp_low;
			}
			goto found;

		case BT_IDB:
			/*
			 * Interface Description Block.  Get a pointer
			 * to its fixed-length portion.
			 */
			idbp = get_from_block_data(&cursor, sizeof(*idbp),
			    p->errbuf);
			if (idbp == NULL)
				return (-1);	/* error */

			/*
			 * Byte-swap it if necessary.
			 */
			if (p->swapped) {
				idbp->linktype = SWAPSHORT((uint32_t)idbp->linktype);
				idbp->snaplen = SWAPLONG(idbp->snaplen);
			}

			/*
			 * If the link-layer type or snapshot length
			 * differ from the ones for the first IDB we
			 * saw, quit.
			 *
			 * XXX - just discard packets from those
			 * interfaces?
			 */
			if (p->linktype != idbp->linktype) {
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "an interface has a type %u different from the type of the first interface",
				    idbp->linktype);
				return (-1);
			}
			if (p->snapshot != (int)idbp->snaplen) {
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "an interface has a snapshot length %u different from the type of the first interface",
				    idbp->snaplen);
				return (-1);
			}

			/*
			 * Try to add this interface.
			 */
			if (!add_interface(p, &cursor, p->errbuf))
				return (-1);
			break;

		case BT_SHB:
			/*
			 * Section Header Block.  Get a pointer
			 * to its fixed-length portion.
			 */
			shbp = get_from_block_data(&cursor, sizeof(*shbp),
			    p->errbuf);
			if (shbp == NULL)
				return (-1);	/* error */

			/*
			 * Assume the byte order of this section is
			 * the same as that of the previous section.
			 * We'll check for that later.
			 */
			if (p->swapped) {
				shbp->byte_order_magic =
				    SWAPLONG(shbp->byte_order_magic);
				shbp->major_version =
				    SWAPSHORT((uint32_t)shbp->major_version);
			}

			/*
			 * Make sure the byte order doesn't change;
			 * pcap_is_swapped() shouldn't change its
			 * return value in the middle of reading a capture.
			 */
			switch (shbp->byte_order_magic) {

			case BYTE_ORDER_MAGIC:
				/*
				 * OK.
				 */
				break;

			case SWAPLONG(BYTE_ORDER_MAGIC):
				/*
				 * Byte order changes.
				 */
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "the file has sections with different byte orders");
				return (-1);

			default:
				/*
				 * Not a valid SHB.
				 */
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "the file has a section with a bad byte order magic field");
				return (-1);
			}

			/*
			 * Make sure the major version is the version
			 * we handle.
			 */
			if (shbp->major_version != PCAP_NG_VERSION_MAJOR) {
				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
				    "unknown pcap-ng savefile major version number %u",
				    shbp->major_version);
				return (-1);
			}

			/*
			 * Reset the interface count; this section should
			 * have its own set of IDBs.  If any of them
			 * don't have the same interface type, snapshot
			 * length, or resolution as the first interface
			 * we saw, we'll fail.  (And if we don't see
			 * any IDBs, we'll fail when we see a packet
			 * block.)
			 */
			ps->ifcount = 0;
			break;

		default:
			/*
			 * Not a packet block, IDB, or SHB; ignore it.
			 */
			break;
		}
	}

found:
	/*
	 * Is the interface ID an interface we know?
	 */
	if (interface_id >= ps->ifcount) {
		/*
		 * Yes.  Fail.
		 */
		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
		    "a packet arrived on interface %u, but there's no Interface Description Block for that interface",
		    interface_id);
		return (-1);
	}

	/*
	 * Convert the time stamp to seconds and fractions of a second,
	 * with the fractions being in units of the file-supplied resolution.
	 */
	sec = t / ps->ifaces[interface_id].tsresol + ps->ifaces[interface_id].tsoffset;
	frac = t % ps->ifaces[interface_id].tsresol;

	/*
	 * Convert the fractions from units of the file-supplied resolution
	 * to units of the user-requested resolution.
	 */
	switch (ps->ifaces[interface_id].scale_type) {

	case PASS_THROUGH:
		/*
		 * The interface resolution is what the user wants,
		 * so we're done.
		 */
		break;

	case SCALE_UP:
	case SCALE_DOWN:
		/*
		 * The interface resolution is different from what the
		 * user wants; convert the fractions to units of the
		 * resolution the user requested by multiplying by the
		 * quotient of the user-requested resolution and the
		 * file-supplied resolution.  We do that by multiplying
		 * by the user-requested resolution and dividing by the
		 * file-supplied resolution, as the quotient might not
		 * fit in an integer.
		 *
		 * XXX - if ps->ifaces[interface_id].tsresol is a power
		 * of 10, we could just multiply by the quotient of
		 * ps->user_tsresol and ps->ifaces[interface_id].tsresol
		 * in the scale-up case, and divide by the quotient of
		 * ps->ifaces[interface_id].tsresol and ps->user_tsresol
		 * in the scale-down case, as we know those will be integers.
		 * That would involve fewer arithmetic operations, and
		 * would run less risk of overflow.
		 *
		 * Is there something clever we could do if
		 * ps->ifaces[interface_id].tsresol is a power of 2?
		 */
		frac *= ps->user_tsresol;
		frac /= ps->ifaces[interface_id].tsresol;
		break;
	}
	hdr->ts.tv_sec = sec;
	hdr->ts.tv_usec = (suseconds_t)frac;

	/*
	 * Get a pointer to the packet data.
	 */
	*data = get_from_block_data(&cursor, hdr->caplen, p->errbuf);
	if (*data == NULL)
		return (-1);

	if (p->swapped)
		swap_pseudo_headers(p->linktype, hdr, *data);

	return (0);
}
Ejemplo n.º 28
0
Archivo: r_data.c Proyecto: yoyz/psp
//
// R_GenerateLookup
//
void R_GenerateLookup (int texnum)
{
    texture_t*		texture;
    byte*		patchcount = NULL; // patchcount[texture->width]
    texpatch_t*		patch;
    patch_t*		realpatch;
    int			x;
    int			x1;
    int			x2;
    int			i;
    short*		collump;
    unsigned short*	colofs;

    texture = textures[texnum];

    // Composited texture not created yet.
    texturecomposite[texnum] = 0;

    texturecompositesize[texnum] = 0;
    collump = texturecolumnlump[texnum];
    colofs = texturecolumnofs[texnum];

    // Now count the number of columns
    //  that are covered by more than one patch.
    // Fill in the lump / offset, so columns
    //  with only a single patch are all done.
    if ((patchcount = (byte *)malloc (texture->width)) == NULL)
      I_Error ("malloc(%d) failed", texture->width);
    memset (patchcount, 0, texture->width);
    patch = texture->patches;

    for (i=0 , patch = texture->patches;
	 i<texture->patchcount;
	 i++, patch++)
    {
	realpatch = W_CacheLumpNum (patch->patch, PU_CACHE);
	x1 = patch->originx;
	x2 = x1 + SWAPSHORT(realpatch->width);

	if (x1 < 0)
	    x = 0;
	else
	    x = x1;

	if (x2 > texture->width)
	    x2 = texture->width;
	for ( ; x<x2 ; x++)
	{
	    patchcount[x]++;
	    collump[x] = patch->patch;
	    colofs[x] = SWAPLONG(realpatch->columnofs[x-x1])+3;
	}
    }

    for (x=0 ; x<texture->width ; x++)
    {
	if (!patchcount[x])
	{
	    printf ("R_GenerateLookup: column without a patch (%s)\n",
		    texture->name);
	    return;
	}
	// I_Error ("R_GenerateLookup: column without a patch");

	if (patchcount[x] > 1)
	{
	    // Use the cached block.
	    collump[x] = -1;
	    colofs[x] = texturecompositesize[texnum];

	    if (texturecompositesize[texnum] > 0x10000-texture->height)
	    {
		I_Error ("R_GenerateLookup: texture %i is >64k",
			 texnum);
	    }

	    texturecompositesize[texnum] += texture->height;
	}
    }
    if (patchcount != NULL)
      free (patchcount);
}
Ejemplo n.º 29
0
Archivo: r_data.c Proyecto: yoyz/psp
//
// R_GenerateComposite
// Using the texture definition,
//  the composite texture is created from the patches,
//  and each column is cached.
//
void R_GenerateComposite (int texnum)
{
    byte*		block;
    texture_t*		texture;
    texpatch_t*		patch;
    patch_t*		realpatch;
    int			x;
    int			x1;
    int			x2;
    int			i;
    column_t*		patchcol;
    short*		collump;
    unsigned short*	colofs;

    texture = textures[texnum];

    block = Z_Malloc (texturecompositesize[texnum],
		      PU_STATIC,
		      &texturecomposite[texnum]);

    collump = texturecolumnlump[texnum];
    colofs = texturecolumnofs[texnum];

    // Composite the columns together.
    patch = texture->patches;

    for (i=0 , patch = texture->patches;
	 i<texture->patchcount;
	 i++, patch++)
    {
	realpatch = W_CacheLumpNum (patch->patch, PU_CACHE);
	x1 = patch->originx;
	x2 = x1 + SWAPSHORT(realpatch->width);

	if (x1<0)
	    x = 0;
	else
	    x = x1;

	if (x2 > texture->width)
	    x2 = texture->width;

	for ( ; x<x2 ; x++)
	{
	    // Column does not have multiple patches?
	    if (collump[x] >= 0)
		continue;

	    patchcol = (column_t *)((byte *)realpatch
				    + SWAPLONG(realpatch->columnofs[x-x1]));
	    R_DrawColumnInCache (patchcol,
				 block + colofs[x],
				 patch->originy,
				 texture->height);
	}

    }

    // Now that the texture has been built in column cache,
    //  it is purgable from zone memory.
    Z_ChangeTag (block, PU_CACHE);
}
Ejemplo n.º 30
0
Archivo: r_data.c Proyecto: yoyz/psp
//
// R_InitTextures
// Initializes the texture list
//  with the textures from the world map.
//
void R_InitTextures (void)
{
    maptexture_t*	mtexture;
    texture_t*		texture;
    mappatch_t*		mpatch;
    texpatch_t*		patch;

    int			i;
    int			j;

    int*		maptex;
    int*		maptex2;
    int*		maptex1;

    char		name[9];
    char*		names;
    char*		name_p;

    int*		patchlookup = NULL;

    int			totalwidth;
    int			nummappatches;
    int			offset;
    int			maxoff;
    int			maxoff2;
    int			numtextures1;
    int			numtextures2;

    int*		directory;

    int			temp1;
    int			temp2;
    int			temp3;


    // Load the patch names from pnames.lmp.
    name[8] = 0;
    names = W_CacheLumpName ("PNAMES", PU_STATIC);
    nummappatches = SWAPLONG( *((int *)names) );
    name_p = names+4;
    if ((patchlookup = malloc (nummappatches*sizeof(*patchlookup))) == NULL)
      I_Error ("malloc(%d) failed", nummappatches*sizeof(*patchlookup));

    for (i=0 ; i<nummappatches ; i++)
    {
	strncpy (name,name_p+i*8, 8);
	patchlookup[i] = W_CheckNumForName (name);
    }
    Z_Free (names);

    // Load the map texture definitions from textures.lmp.
    // The data is contained in one or two lumps,
    //  TEXTURE1 for shareware, plus TEXTURE2 for commercial.
    maptex = maptex1 = W_CacheLumpName ("TEXTURE1", PU_STATIC);
    numtextures1 = SWAPLONG(*maptex);
    maxoff = W_LumpLength (W_GetNumForName ("TEXTURE1"));
    directory = maptex+1;

    if (W_CheckNumForName ("TEXTURE2") != -1)
    {
	maptex2 = W_CacheLumpName ("TEXTURE2", PU_STATIC);
	numtextures2 = SWAPLONG(*maptex2);
	maxoff2 = W_LumpLength (W_GetNumForName ("TEXTURE2"));
    }
    else
    {
	maptex2 = NULL;
	numtextures2 = 0;
	maxoff2 = 0;
    }
    numtextures = numtextures1 + numtextures2;

    textures = Z_Malloc (numtextures*4, PU_STATIC, 0);
    texturecolumnlump = Z_Malloc (numtextures*4, PU_STATIC, 0);
    texturecolumnofs = Z_Malloc (numtextures*4, PU_STATIC, 0);
    texturecomposite = Z_Malloc (numtextures*4, PU_STATIC, 0);
    texturecompositesize = Z_Malloc (numtextures*4, PU_STATIC, 0);
    texturewidthmask = Z_Malloc (numtextures*4, PU_STATIC, 0);
    textureheight = Z_Malloc (numtextures*4, PU_STATIC, 0);

    totalwidth = 0;

    //	Really complex printing shit...
    temp1 = W_GetNumForName ("S_START");  // P_???????
    temp2 = W_GetNumForName ("S_END") - 1;
    temp3 = ((temp2-temp1+63)/64) + ((numtextures+63)/64);
    printf("[");
    for (i = 0; i < temp3; i++)
	printf(" ");
    printf("]");
    for (i = 0; i < temp3; i++)
	printf("\x8");
    printf("\x8");

    for (i=0 ; i<numtextures ; i++, directory++)
    {
	if (!(i&63))
	    printf (".");

	if (i == numtextures1)
	{
	    // Start looking in second texture file.
	    maptex = maptex2;
	    maxoff = maxoff2;
	    directory = maptex+1;
	}

	offset = SWAPLONG(*directory);

	if (offset > maxoff)
	    I_Error ("R_InitTextures: bad texture directory");

	mtexture = (maptexture_t *) ( (byte *)maptex + offset);

	texture = textures[i] =
	    Z_Malloc (sizeof(texture_t)
		      + sizeof(texpatch_t)*(SWAPSHORT(mtexture->patchcount)-1),
		      PU_STATIC, 0);

	texture->width = SWAPSHORT(mtexture->width);
	texture->height = SWAPSHORT(mtexture->height);
	texture->patchcount = SWAPSHORT(mtexture->patchcount);

	memcpy (texture->name, mtexture->name, sizeof(texture->name));
	mpatch = &mtexture->patches[0];
	patch = &texture->patches[0];

	for (j=0 ; j<texture->patchcount ; j++, mpatch++, patch++)
	{
	    patch->originx = SWAPSHORT(mpatch->originx);
	    patch->originy = SWAPSHORT(mpatch->originy);
	    patch->patch = patchlookup[SWAPSHORT(mpatch->patch)];
	    if (patch->patch == -1)
	    {
		I_Error ("R_InitTextures: Missing patch in texture %s",
			 texture->name);
	    }
	}
	texturecolumnlump[i] = Z_Malloc (texture->width*2, PU_STATIC,0);
	texturecolumnofs[i] = Z_Malloc (texture->width*2, PU_STATIC,0);

	j = 1;
	while (j*2 <= texture->width)
	    j<<=1;

	texturewidthmask[i] = j-1;
	textureheight[i] = texture->height<<FRACBITS;

	totalwidth += texture->width;
    }

    Z_Free (maptex1);
    if (maptex2)
	Z_Free (maptex2);

    // Precalculate whatever possible.
    for (i=0 ; i<numtextures ; i++)
	R_GenerateLookup (i);

    // Create translation table for global animation.
    texturetranslation = Z_Malloc ((numtextures+1)*4, PU_STATIC, 0);

    for (i=0 ; i<numtextures ; i++)
	texturetranslation[i] = i;

    if (patchlookup != NULL)
      free (patchlookup);
}