/*
 *  upacks xtlv record from buf checks the type
 *  copies data to callers buffer
 *  advances tlv pointer to next record
 *  caller's resposible for dst space check
 */
int
bcm_unpack_xtlv_entry(void **tlv_buf, uint16 xpct_type, uint16 xpct_len, void *dst)
{
	bcm_xtlv_t *ptlv = *tlv_buf;
	uint16 len;
	uint16 type;

	ASSERT(ptlv);
	/* tlv headr is always packed in LE order */
	len = ltoh16(ptlv->len);
	type = ltoh16(ptlv->id);
	if (len == 0) {
		/* z-len tlv headers: allow, but don't process */
		printf("z-len, skip unpack\n");
	} else  {
		if ((type != xpct_type) ||
			(len > xpct_len)) {
			printf("xtlv_unpack Error: found[type:%d,len:%d] != xpct[type:%d,len:%d]\n",
				type, len, xpct_type, xpct_len);
			return BCME_BADARG;
		}
		/* copy tlv record to caller's buffer */
		memcpy(dst, ptlv->data, ptlv->len);
	}
	*tlv_buf += BCM_XTLV_SIZE(ptlv);
	return BCME_OK;
}
/*
 *  unpack all xtlv records from the issue a callback
 *  to set function one call per found tlv record
 */
int
bcm_unpack_xtlv_buf(void *ctx, void *tlv_buf, uint16 buflen, bcm_set_var_from_tlv_cbfn_t *cbfn)
{
	uint16 len;
	uint16 type;
	int res = 0;
	bcm_xtlv_t *ptlv = tlv_buf;
	int sbuflen = buflen;

	ASSERT(ptlv);
	ASSERT(cbfn);

	while (sbuflen >= 0) {
		ptlv = tlv_buf;

		/* tlv header is always packed in LE order */
		len = ltoh16(ptlv->len);
		if (len == 0)	/* can't be zero */
			break;
		type = ltoh16(ptlv->id);

		sbuflen -= (BCM_XTLV_HDR_SIZE + len);

		/* check for possible buffer overrun */
		if (sbuflen < 0)
			break;

		if ((res = cbfn(ctx, &tlv_buf, type, len)) != BCME_OK)
			break;
	}
	return res;
}
示例#3
0
文件: image.c 项目: rossy/bfingers
image_texture* image_totexture(uint8_t* data)
{
	bmpfile_header* header = (void*)data + 2;
	bmpfile_infoheader* infoheader = (void*)data + 14;
	uint8_t* bmpdata = data + ltoh32(header->bmp_offset), * flipped;
	image_texture* ret = malloc(sizeof(image_texture));
	long i, width, height;
	
	if (data[0] != 'B' || data[1] != 'M')
		return 0;
	
	width = ltoh32(infoheader->width);
	height = ltoh32(infoheader->height);
	
	if (ltoh32(infoheader->header_sz) < 40 || ltoh16(infoheader->nplanes) > 1 || ltoh16(infoheader->bitspp) != 32 || ltoh32(infoheader->compress_type) != 0
		|| !width || height <= 0)
		return 0;
	
	glGenTextures(1, &ret->texture);
	glBindTexture(GL_TEXTURE_2D, ret->texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	
	if (width > 0)
	{
		unsigned long pitch = width * 4;
		
		flipped = malloc(height * pitch);
		for (i = height * pitch - pitch; i >= 0; i -= pitch, bmpdata += pitch)
			memcpy(flipped + i, bmpdata, pitch);
	}
	
	ret->refs = 1;
	ret->width = width;
	ret->height = height;
	ret->texwidth = width;
	ret->texheight = height;
	ret->ratx = 1.0;
	ret->raty = 1.0;
	
	if (width > 0)
	{
		glTexImage2D(GL_TEXTURE_2D, 0, 4, ret->texwidth, ret->texheight, 0, GL_BGRA, GL_UNSIGNED_BYTE, flipped);
		free(flipped);
	}
	else
		glTexImage2D(GL_TEXTURE_2D, 0, 4, ret->texwidth, ret->texheight, 0, GL_BGRA, GL_UNSIGNED_BYTE, bmpdata);
	
	return ret;
}
示例#4
0
void process_data (PacketProtoDecoder *enc)
{
    int was_error = 0;
    
    do {
        uint8_t *data = enc->buf + enc->buf_start;
        int left = enc->buf_used;
        
        // check if header was received
        if (left < sizeof(struct packetproto_header)) {
            break;
        }
        struct packetproto_header header;
        memcpy(&header, data, sizeof(header));
        data += sizeof(struct packetproto_header);
        left -= sizeof(struct packetproto_header);
        int data_len = ltoh16(header.len);
        
        // check data length
        if (data_len > enc->output_mtu) {
            BLog(BLOG_NOTICE, "error: packet too large");
            was_error = 1;
            break;
        }
        
        // check if whole packet was received
        if (left < data_len) {
            break;
        }
        
        // update buffer
        enc->buf_start += sizeof(struct packetproto_header) + data_len;
        enc->buf_used -= sizeof(struct packetproto_header) + data_len;
        
        // submit packet
        PacketPassInterface_Sender_Send(enc->output, data, data_len);
        return;
    } while (0);
    
    if (was_error) {
        // reset buffer
        enc->buf_start = 0;
        enc->buf_used = 0;
    } else {
        // if we reached the end of the buffer, wrap around to allow more data to be received
        if (enc->buf_start + enc->buf_used == enc->buf_size) {
            memmove(enc->buf, enc->buf + enc->buf_start, enc->buf_used);
            enc->buf_start = 0;
        }
    }
    
    // receive data
    StreamRecvInterface_Receiver_Recv(enc->input, enc->buf + (enc->buf_start + enc->buf_used), enc->buf_size - (enc->buf_start + enc->buf_used));
    
    // if we had error, report it
    if (was_error) {
        enc->handler_error(enc->user);
        return;
    }
}
示例#5
0
/* calculates BIP; mic must be at least AES_BLOCK_SZ */
static void bip_calc(const struct dot11_header *hdr,
    const uint8 *data, int data_len, const uint8 *key, uint8 * mic)
{
    uint len;
	uint8* micdata; 
	uint16 fc; 

	memset(mic, 0 , AES_BLOCK_SZ);
	micdata = malloc(2 + 3 * ETHER_ADDR_LEN + data_len);
	if (!micdata) 
		return;

	fc = htol16(ltoh16(hdr->fc) & ~(FC_RETRY | FC_PM | FC_MOREDATA));

	len = 0;
    memcpy((char *)&micdata[len], (uint8 *)&fc, 2);
    len += 2;
    memcpy(&micdata[len], (uint8 *)&hdr->a1, ETHER_ADDR_LEN);
    len += ETHER_ADDR_LEN;
    memcpy(&micdata[len], (uint8 *)&hdr->a2, ETHER_ADDR_LEN);
    len += ETHER_ADDR_LEN;
    memcpy(&micdata[len], (uint8 *)&hdr->a3, ETHER_ADDR_LEN);
    len += ETHER_ADDR_LEN;

    memcpy(&micdata[len], data, data_len);
    len += data_len;

    aes_cmac_calc(micdata, len, key, BIP_KEY_SIZE, mic);
	free(micdata);
}
int
bcm_skip_xtlv(void **tlv_buf)
{
	bcm_xtlv_t *ptlv = *tlv_buf;
	uint16 len;
	uint16 type;

	ASSERT(ptlv);
	/* tlv headr is always packed in LE order */
	len = ltoh16(ptlv->len);
	type = ltoh16(ptlv->id);

	printf("xtlv_skip: Skipping tlv [type:%d,len:%d]\n", type, len);

	*tlv_buf += BCM_XTLV_SIZE(ptlv);
	return BCME_OK;
}
static void
dhd_bta_flush_hcidata(dhd_pub_t *pub, uint16 llh)
{
	int prec;
	struct pktq *q;
	uint count = 0;

	q = dhd_bus_txq(pub->bus);
	if (q == NULL)
		return;

	DHD_BTA(("dhd: flushing HCI ACL data for logical link %u...\n", llh));

	dhd_os_sdlock_txq(pub);

	/* Walk through the txq and toss all HCI ACL data packets */
	PKTQ_PREC_ITER(q, prec) {
		void *head_pkt = NULL;

		while (pktq_ppeek(q, prec) != head_pkt) {
			void *pkt = pktq_pdeq(q, prec);
			int ifidx;

			PKTPULL(pub->osh, pkt, dhd_bus_hdrlen(pub->bus));
			dhd_prot_hdrpull(pub, &ifidx, pkt);

			if (PKTLEN(pub->osh, pkt) >= RFC1042_HDR_LEN) {
				struct ether_header *eh =
				        (struct ether_header *)PKTDATA(pub->osh, pkt);

				if (ntoh16(eh->ether_type) < ETHER_TYPE_MIN) {
					struct dot11_llc_snap_header *lsh =
					        (struct dot11_llc_snap_header *)&eh[1];

					if (bcmp(lsh, BT_SIG_SNAP_MPROT,
					         DOT11_LLC_SNAP_HDR_LEN - 2) == 0 &&
					    ntoh16(lsh->type) == BTA_PROT_L2CAP) {
						amp_hci_ACL_data_t *ACL_data =
						        (amp_hci_ACL_data_t *)&lsh[1];
						uint16 handle = ltoh16(ACL_data->handle);

						if (HCI_ACL_DATA_HANDLE(handle) == llh) {
							PKTFREE(pub->osh, pkt, TRUE);
							count ++;
							continue;
						}
					}
				}
			}

			dhd_prot_hdrpush(pub, ifidx, pkt);
			PKTPUSH(pub->osh, pkt, dhd_bus_hdrlen(pub->bus));

			if (head_pkt == NULL)
				head_pkt = pkt;
			pktq_penq(q, prec, pkt);
		}
	}
void UnitState::setFromNetworkUnitState(const NetworkUnitState& state)
{
    select = false;

    unit_type = state.unit_type;
    location.x = ltoh32(state.location_x);
    location.y = ltoh32(state.location_y);
    bbox.min.x = ltoh32(state.bbox_min_x);
    bbox.min.y = ltoh32(state.bbox_min_y);
    bbox.max.x = ltoh32(state.bbox_max_x);
    bbox.max.y = ltoh32(state.bbox_max_y);

    body_angle.setFromNetworkAngleInt(state.body_angle);
    turret_angle.setFromNetworkAngleInt(state.turret_angle);

    orientation = ltoh16(state.orientation);
    speed_rate = ltoh16(state.speed_rate);
    speed_factor = ltoh16(state.speed_factor);
    
    reload_time = ltoh16(state.reload_time);
    max_hit_points = ltoh16(state.max_hit_points);
    hit_points = ltoh16(state.hit_points);
    damage_factor = ltoh16(state.damage_factor);
    weapon_range = ltoh32(state.weapon_range);
    defend_range = ltoh32(state.defend_range);

    threat_level = state.threat_level;
    lifecycle_state = state.lifecycle_state;
}
示例#9
0
/* Unpack 16-bit vectors */
int
bcm_xdr_unpack_uint16_vec(bcm_xdr_buf_t *b, uint len, void *vec)
{
	int err = 0, i;
	uint16	*vec16 = (uint16*)vec;
	ASSERT((len % sizeof(uint16)) == 0);

	err = bcm_xdr_unpack_opaque_cpy(b, len, vec);

	/* Do the 16 bit swapping in the copied buffer */
	for (i = 0; i < (int)(len/sizeof(uint16)); i++)
		vec16[i] = ltoh16(vec16[i]);

	return err;
}
示例#10
0
文件: hnddma.c 项目: ariavie/bcm
/* returns a pointer to the next frame received, or NULL if there are no more */
void*
dma_rx(dma_info_t *di)
{
	void *p;
	uint len;
	int skiplen = 0;

	while ((p = dma_getnextrxp(di, FALSE))) {
		/* skip giant packets which span multiple rx descriptors */
		if (skiplen > 0) {
			skiplen -= di->rxbufsize;
			if (skiplen < 0)
				skiplen = 0;
			PKTFREE(di->drv, p, FALSE);
			continue;
		}

		len = ltoh16(*(uint16*)(PKTDATA(di->drv, p)));
		DMA_TRACE(("%s: dma_rx len %d\n", di->name, len));

		/* bad frame length check */
		if (len > (di->rxbufsize - di->rxoffset)) {
			DMA_ERROR(("%s: dma_rx: bad frame length (%d)\n", di->name, len));
			if (len > 0)
				skiplen = len - (di->rxbufsize - di->rxoffset);
			PKTFREE(di->drv, p, FALSE);
			di->hnddma.rxgiants++;
			continue;
		}

		/* set actual length */
		PKTSETLEN(di->drv, p, (di->rxoffset + len));

		break;
	}

	return (p);
}
示例#11
0
static int
sb_read_config(sb_t *sbh, uint bus, uint dev, uint func, uint off, void *buf, int len)
{
	pci_config_regs *cfg;

	if (dev >= SB_MAXCORES || (off + len) > sizeof(pci_config_regs))
		return -1;
	cfg = &sb_config_regs[dev];

	ASSERT(ISALIGNED(off, len));
	ASSERT(ISALIGNED((uintptr)buf, len));

	if (len == 4)
		*((uint32 *) buf) = ltoh32(*((uint32 *)((ulong) cfg + off)));
	else if (len == 2)
		*((uint16 *) buf) = ltoh16(*((uint16 *)((ulong) cfg + off)));
	else if (len == 1)
		*((uint8 *) buf) = *((uint8 *)((ulong) cfg + off));
	else
		return -1;

	return 0;
}
void PlayerState::setFromNetworkPlayerState(const NetworkPlayerState* state)
{
    char tmp[64];
    memcpy(tmp, state->name, 64); 
    tmp[63] = 0;
    name = tmp;
    flag = state->flag;
    if ( ! ResourceManager::isFlagActive(flag) ) 
    {
        LOGGER.warning("Received flag is not registered: %u", flag);
    }
    
    player_index = ltoh16(state->playerindex_id);
    status = state->status;
    kills = ltoh16(state->kills);
    kill_points = ltoh16(state->kill_points);
    losses = ltoh16(state->losses);
    loss_points = ltoh16(state->loss_points);
    total = ltoh16(state->total);
    objectives_held = ltoh16(state->objectives_held);
    setColor(ltoh32(state->colorIndex));
}
 UnitID getUnitID() const
 {
     return ltoh16(unit_id);
 }
 UnitID getUnitToDestroy() const
 {
     return ltoh16(unit_to_destroy);
 }
Uint16 ConnectMesgServerGameSettings::getMaxUnits() const
{
    return ltoh16(max_units);
}
Uint16 ConnectMesgServerGameSettings::getMaxPlayers() const
{
    return ltoh16(max_players);
}
 iXY getMapPos() const
 {
     return iXY(ltoh16(map_x), ltoh16(map_y));
 }
 Uint16 getKillByPlayerIndex() const
 {
     return ltoh16(kill_by_player_index);
 }
 Uint16 getClientPlayerIndex() const
 {
     return ltoh16(client_player_index);
 }
 iXY getTarget() const
 {
     return iXY(ltoh16(x), ltoh16(y));
 }
 UnitID getTargetUnitID() const
 {
     return ltoh16(targetUnitID);
 }
 iXY getTarget() const
 {
     return iXY(ltoh16(target_x), ltoh16(target_y));
 }
Uint16 NetworkPlayerState::getPlayerIndex() const
{
    return ltoh16(playerindex_id);
}
 Uint16 getAllieByPlayerIndex() const
 {
     return ltoh16(allie_by_player_index);
 }
 Uint16 getAllieWithPlayerIndex() const
 {
     return ltoh16(allie_with_player_index);
 }
 Uint16 getPlayerID() const
 {
     return ltoh16(player_index);
 }
 Uint16 getKillOnPlayerIndex() const
 {
     return ltoh16(kill_on_player_index);
 }
示例#28
0
static void receiver_recv_handler_send (DPReceiveReceiver *o, uint8_t *packet, int packet_len)
{
    DebugObject_Access(&o->d_obj);
    DPReceivePeer *peer = o->peer;
    DPReceiveDevice *device = peer->device;
    ASSERT(packet_len >= 0)
    ASSERT(packet_len <= device->packet_mtu)
    
    uint8_t *data = packet;
    int data_len = packet_len;
    
    int local = 0;
    DPReceivePeer *src_peer;
    DPReceivePeer *relay_dest_peer = NULL;
    
    // check header
    if (data_len < sizeof(struct dataproto_header)) {
        BLog(BLOG_WARNING, "no dataproto header");
        goto out;
    }
    struct dataproto_header header;
    memcpy(&header, data, sizeof(header));
    data += sizeof(header);
    data_len -= sizeof(header);
    uint8_t flags = ltoh8(header.flags);
    peerid_t from_id = ltoh16(header.from_id);
    int num_ids = ltoh16(header.num_peer_ids);
    
    // check destination ID
    if (!(num_ids == 0 || num_ids == 1)) {
        BLog(BLOG_WARNING, "wrong number of destinations");
        goto out;
    }
    peerid_t to_id = 0; // to remove warning
    if (num_ids == 1) {
        if (data_len < sizeof(struct dataproto_peer_id)) {
            BLog(BLOG_WARNING, "missing destination");
            goto out;
        }
        struct dataproto_peer_id id;
        memcpy(&id, data, sizeof(id));
        to_id = ltoh16(id.id);
        data += sizeof(id);
        data_len -= sizeof(id);
    }
    
    // check remaining data
    if (data_len > device->device_mtu) {
        BLog(BLOG_WARNING, "frame too large");
        goto out;
    }
    
    // inform sink of received packet
    if (peer->dp_sink) {
        DataProtoSink_Received(peer->dp_sink, !!(flags & DATAPROTO_FLAGS_RECEIVING_KEEPALIVES));
    }
    
    if (num_ids == 1) {
        // find source peer
        if (!(src_peer = find_peer(device, from_id))) {
            BLog(BLOG_INFO, "source peer %d not known", (int)from_id);
            goto out;
        }
        
        // is frame for device or another peer?
        if (device->have_peer_id && to_id == device->peer_id) {
            // let the frame decider analyze the frame
            FrameDeciderPeer_Analyze(src_peer->decider_peer, data, data_len);
            
            // pass frame to device
            local = 1;
        } else {
            // check if relaying is allowed
            if (!peer->is_relay_client) {
                BLog(BLOG_WARNING, "relaying not allowed");
                goto out;
            }
            
            // provided source ID must be the peer sending the frame
            if (src_peer != peer) {
                BLog(BLOG_WARNING, "relay source must be the sending peer");
                goto out;
            }
            
            // find destination peer
            DPReceivePeer *dest_peer = find_peer(device, to_id);
            if (!dest_peer) {
                BLog(BLOG_INFO, "relay destination peer not known");
                goto out;
            }
            
            // destination cannot be source
            if (dest_peer == src_peer) {
                BLog(BLOG_WARNING, "relay destination cannot be the source");
                goto out;
            }
            
            relay_dest_peer = dest_peer;
        }
    }
    
out:
    // accept packet
    PacketPassInterface_Done(&o->recv_if);
    
    // pass packet to device
    if (local) {
        o->device->output_func(o->device->output_func_user, data, data_len);
    }
    
    // relay frame
    if (relay_dest_peer) {
        DPRelayRouter_SubmitFrame(&device->relay_router, &src_peer->relay_source, &relay_dest_peer->relay_sink, data, data_len, device->relay_flow_buffer_size, device->relay_flow_inactivity_time);
    }
}
 Sint16 getHitPoints() const
 {
     return ltoh16(hit_points);
 }
Sint16 ConnectClientSettings::getPlayerFlag() const
{
    return ltoh16(player_flag);
}