void packetDetails_ethernet(uint8_t *packetData)
{
	ethernetFrame = (ethernetFrame_t*) packetData;

	printf("Ethernet frame\n");

	PRINTINFO("Destination MAC");
	for(i=0; i<5; i++)
		printf("%02" PRIx8 ":", ethernetFrame->destinationMac[i]);
	printf("%02" PRIx8 "\n", ethernetFrame->destinationMac[5]);

	PRINTINFO("Source MAC");
	for(i=0; i<5; i++)
		printf("%02" PRIx8 ":", ethernetFrame->sourceMac[i]);
	printf("%02" PRIx8 "\n", ethernetFrame->sourceMac[5]);

	PRINTINFO("Type");
	printf("%s (0x%04" PRIx16 ")\n", determineEthernetProtocol(swap_uint16(ethernetFrame->type)), swap_uint16(ethernetFrame->type));

	switch(swap_uint16(ethernetFrame->type)){
		case 0x0800:	// IP protocol
			packetDetails_ip(packetData + ETHERNET_FRAME_LENGTH);
			break;
		case 0x0806:	// ARP protocol
			packetDetails_arp(packetData + ETHERNET_FRAME_LENGTH);
			break;
	}
}
void packetDetails_tcp(uint8_t *packetData)
{
	tcpHeader = (tcpHeader_t*)(packetData);

	printf("TCP header\n");

	PRINTINFO("Source port");
	PRINTPORT(tcpHeader->sourcePort);

	PRINTINFO("Destination port");
	PRINTPORT(tcpHeader->destinationPort);

	PRINTINFO("Sequence number");
	PRINTHEX32(swap_uint32(tcpHeader->sequenceNumber));

	PRINTINFO("Acknowledgement number");
	PRINTHEX32(swap_uint32(tcpHeader->acknowledgementNumber));

	// Flags
	uint16_t flag = swap_uint16(tcpHeader->flags);
	PRINTFLAG("Header length");
	PRINTUINT8(((flag & TCP_headerLength) >> TCP_headerLengthPos)*4);

	PRINTFLAG("Reserved");
	PRINTHEX16((flag & TCP_reserved) >> TCP_reservedPos);

	PRINTFLAG("URG");
	printf("%s\n", flagSetOrNot((flag & TCP_urg) >> TCP_urgPos));

	PRINTFLAG("ACK");
	printf("%s\n", flagSetOrNot((flag & TCP_ack) >> TCP_ackPos));

	PRINTFLAG("PSH");
	printf("%s\n", flagSetOrNot((flag & TCP_push) >> TCP_pushPos));

	PRINTFLAG("RST");
	printf("%s\n", flagSetOrNot((flag & TCP_reset) >> TCP_resetPos));

	PRINTFLAG("SYN");
	printf("%s\n", flagSetOrNot((flag & TCP_syn) >> TCP_synPos));

	PRINTFLAG("FIN");
	printf("%s\n", flagSetOrNot((flag & TCP_fin) >> TCP_synPos));

	PRINTINFO("Window size");
	PRINTUINT16(swap_uint16(tcpHeader->windowSize));

	PRINTINFO("Checksum");
	PRINTHEX16(swap_uint16(tcpHeader->checksum));

	PRINTINFO("Urgent pointer");
	PRINTUINT16(swap_uint16(tcpHeader->urgentPointer));

	packetDetails_data(packetData + TCP_LENGTH, (swap_uint16(ipHeader->totalLength) - (ipHeader->headerLength)*4 - TCP_LENGTH));
}
Пример #3
0
END_TEST

START_TEST(test_swap_uint16_neg) {
    uint16 ile;
    uint16_be ibe;
    
    ile = 0xFFFEU;
    ibe = swap_uint16(ile);
#ifndef WORDS_BIGENDIAN
    fail_if(ibe != 0xFEFFU);
#endif
    fail_if(swap_uint16(ibe) != ile);
}
Пример #4
0
static inline void scale_sample_int16_t(int16_t *buf, int i, int vol, int swap)
{
	int32_t sample = swap ? (int16_t)swap_uint16(buf[i]) : buf[i];

	if (sample < 0) {
		sample = (sample * vol - SOFT_VOL_SCALE / 2) / SOFT_VOL_SCALE;
		if (sample < INT16_MIN)
			sample = INT16_MIN;
	} else {
		sample = (sample * vol + SOFT_VOL_SCALE / 2) / SOFT_VOL_SCALE;
		if (sample > INT16_MAX)
			sample = INT16_MAX;
	}
	buf[i] = swap ? swap_uint16(sample) : sample;
}
Пример #5
0
/* read a string */
static amf_data * amf_string_read(amf_read_proc read_proc, void * user_data) {
    uint16_be strsize;
    byte * buffer;

    if (read_proc(&strsize, sizeof(uint16_be), user_data) < sizeof(uint16_be)) {
        return amf_data_error(AMF_ERROR_EOF);
    }

    strsize = swap_uint16(strsize);
    if (strsize == 0) {
        return amf_string_new(NULL, 0);
    }

    buffer = (byte*)calloc(strsize, sizeof(byte));
    if (buffer == NULL) {
        return NULL;
    }

    if (read_proc(buffer, strsize, user_data) == strsize) {
        amf_data * data = amf_string_new(buffer, strsize);
        free(buffer);
        return data;
    }
    else {
        free(buffer);
        return amf_data_error(AMF_ERROR_EOF);
    }
}
Пример #6
0
/* read a string */
static amf0_data * amf0_string_read(read_proc_t read_proc, void * user_data) {
    uint16_t strsize;
    uint8_t * buffer;

    if (read_proc(&strsize, sizeof(uint16_t), user_data) < sizeof(uint16_t)) {
        return amf0_data_error(AMF0_ERROR_EOF);
    }

    strsize = swap_uint16(strsize);
    if (strsize == 0) {
        return amf0_string_new(NULL, 0);
    }

    buffer = calloc(strsize, sizeof(uint8_t));
    if (buffer == NULL) {
        return NULL;
    }

    if (read_proc(buffer, strsize, user_data) == strsize) {
        amf0_data * data = amf0_string_new(buffer, strsize);
        free(buffer);
        return data;
    }
    else {
        free(buffer);
        return amf0_data_error(AMF0_ERROR_EOF);
    }
}
Пример #7
0
uint16_t read_uint16(FILEBUFFER *f,JENV *env)
{
	uint16_t val,tmp;
	unsigned int pos;
	char *p;
	uint16_t *buf;
	assert(env!=NULL);
	assert(f!=NULL);
	if(file_len_dispo(f)<2)
	{
		tjvm_env_add_error_c(env,"Error in file : invalide size");
		return 0;
	}
	//assert(f->pos+2<f->len);
	pos=f->pos;
	buf=(uint16_t *)(f->buf+pos);
	//tmp=*buf[pos+1];
	val=swap_uint16(*buf);
	//pshort=(unsigned short *)f->buf;
		//version_mineur=swap_uint16(*pshort);
		//pshort++;
	f->pos+=2;
	assert(file_valide(f));
	return val;
}
Пример #8
0
static void swap_s16_byte_order(char *buf, int count)
{
	int16_t *b = (int16_t *)buf;
	int i;

	for (i = 0; i < count; i++)
		b[i] = swap_uint16(b[i]);
}
Пример #9
0
static void swap_netmon_header(struct cap_header *h)
{
	swap_uint16(&h->captype);
	swap_uint16(&h->starttime.wYear);
	swap_uint16(&h->starttime.wMonth);
	swap_uint16(&h->starttime.wDayOfWeek);
	swap_uint16(&h->starttime.wDay);
	swap_uint16(&h->starttime.wHour);
	swap_uint16(&h->starttime.wMinute);
	swap_uint16(&h->starttime.wSecond);
	swap_uint16(&h->starttime.wMilliseconds);
	swap_uint32(&h->frameoffset);
	swap_uint32(&h->framelength);
}
Пример #10
0
void NetworkMessage::AddU16(const uint16_t& value)
{
	if (!canAdd(2))
	{
		return;
	}

	*(uint16_t*)(m_MsgBuf + m_ReadPos) = swap_uint16(value);
	m_ReadPos += 2;
	m_MsgSize += 2;
}
void packetDetails_udp(uint8_t *packetData)
{
	udpHeader = (udpHeader_t*)(packetData);

	printf("UDP header\n");

	PRINTINFO("Source port");
	PRINTPORT(udpHeader->sourcePort);

	PRINTINFO("Destination port");
	PRINTPORT(udpHeader->destinationPort);

	PRINTINFO("Length");
	PRINTUINT16(swap_uint16(udpHeader->length));

	PRINTINFO("Checksum");
	PRINTHEX16(swap_uint16(udpHeader->checksum));

	packetDetails_data(packetData + UDP_LENGTH, swap_uint16(udpHeader->length) - UDP_LENGTH);
}
void packetDetails_arp(uint8_t *packetData)
{
	arpHeader = (arpHeader_t*)(packetData);

	printf("ARP header \n");

	PRINTINFO("Hardware type");
	PRINTHEX16(swap_uint16(arpHeader->hardwareType));

	PRINTINFO("Protocol type");
	printf("%s (0x%04" PRIx16 ")\n", determineEthernetProtocol(swap_uint16(arpHeader->protocolType)), swap_uint16(arpHeader->protocolType));

	PRINTINFO("Hardware address length");
	PRINTUINT8(arpHeader->hwAddrLength);

	PRINTINFO("Protocol address length");
	PRINTUINT8(arpHeader->protoAddrLength);

	PRINTINFO("Opcode");
	PRINTUINT8(swap_uint16(arpHeader->opcode));

	PRINTINFO("Sender MAC");
	for(i=0; i<5; i++)
		printf("%02" PRIx8 ":", arpHeader->senderHwAddr[i]);
	printf("%02" PRIx8 "\n", arpHeader->senderHwAddr[5]);

	PRINTINFO("Sender IP");
	for(i=0; i<3; i++)
		printf("%" PRIu8 ".", arpHeader->senderIpAddr[i]);
	printf("%" PRIu8 "\n", arpHeader->senderIpAddr[3]);

	PRINTINFO("Target MAC");
	for(i=0; i<5; i++)
		printf("%02" PRIx8 ":", arpHeader->targetHwAddr[i]);
	printf("%02" PRIx8 "\n", arpHeader->targetHwAddr[5]);

	PRINTINFO("Target IP");
	for(i=0; i<3; i++)
		printf("%" PRIu8 ".", arpHeader->targetIpAddr[i]);
	printf("%" PRIu8 "\n", arpHeader->targetIpAddr[3]);
}
Пример #13
0
/* write a string */
static size_t amf0_string_write(amf0_data * data, write_proc_t write_proc, void * user_data) {
    uint16_t s;
    size_t w = 0;

    s = swap_uint16(data->string_data.size);
    w = write_proc(&s, sizeof(uint16_t), user_data);
    if (data->string_data.size > 0) {
        w += write_proc(data->string_data.mbstr, (size_t)(data->string_data.size), user_data);
    }

    return w;
}
Пример #14
0
static void convert_u16_be_to_s16_le(char *buf, int count)
{
	int16_t *b = (int16_t *)buf;
	int i;

	for (i = 0; i < count; i++) {
		uint16_t u = b[i];
		int sample;

		u = swap_uint16(u);
		sample = (int)u - 32768;
		b[i] = sample;
	}
}
Пример #15
0
/* read a string */
static amf0_data * amf0_string_read(read_proc_t read_proc, void * user_data) {
    uint16_t strsize;
    uint8_t * buffer;
    if (read_proc(&strsize, sizeof(uint16_t), user_data) == sizeof(uint16_t)) {
        strsize = swap_uint16(strsize);
        if (strsize > 0) {
            buffer = (uint8_t*)calloc(strsize, sizeof(uint8_t));
            if (buffer != NULL && read_proc(buffer, strsize, user_data) == strsize) {
                amf0_data * data = amf0_string_new(buffer, strsize);
                free(buffer);
                return data;
            }
        }
        else {
            return amf0_string_new(NULL, 0);
        }
    }
    return NULL;
}
Пример #16
0
/* write an object */
static size_t amf0_object_write(amf0_data * data, write_proc_t write_proc, void * user_data) {
    amf0_node * node;
    size_t w = 0;
    uint16_t filler = swap_uint16(0);
    uint8_t terminator = AMF0_TYPE_OBJECT_END;

    node = amf0_object_first(data);
    while (node != NULL) {
        w += amf0_string_write(amf0_object_get_name(node), write_proc, user_data);
        w += amf0_data_write(amf0_object_get_data(node), write_proc, user_data);
        node = amf0_object_next(node);
    }

    /* empty string is the last element */
    w += write_proc(&filler, sizeof(uint16_t), user_data);
    /* an object ends with 0x09 */
    w += write_proc(&terminator, sizeof(uint8_t), user_data);

    return w;
}
Пример #17
0
/* write an associative array */
static size_t amf_associative_array_write(const amf_data * data, amf_write_proc write_proc, void * user_data) {
    amf_node * node;
    size_t w = 0;
    uint32_be s;
    uint16_be filler = swap_uint16(0);
    uint8 terminator = AMF_TYPE_END;

    s = swap_uint32(data->list_data.size) / 2;
    w += write_proc(&s, sizeof(uint32_be), user_data);
    node = amf_associative_array_first(data);
    while (node != NULL) {
        w += amf_string_write(amf_associative_array_get_name(node), write_proc, user_data);
        w += amf_data_write(amf_associative_array_get_data(node), write_proc, user_data);
        node = amf_associative_array_next(node);
    }

    /* empty string is the last element */
    w += write_proc(&filler, sizeof(uint16_be), user_data);
    /* an object ends with 0x09 */
    w += write_proc(&terminator, sizeof(uint8), user_data);

    return w;
}
Пример #18
0
/* PSD Packbits decompress */
static void PSD_unpackbits(void* out_buf, unsigned chan_count,
                           unsigned width, unsigned height, FILE* psdf) {
    int c;
    uint16_t* len_arr = calloc(height * chan_count, sizeof(uint16_t));
    fread(len_arr, sizeof(uint16_t), height * chan_count, psdf);
    size_t max_in_line = 0;
    for (c=0 ; c<height*chan_count ; ++c) {
#       if __LITTLE_ENDIAN__
        len_arr[c] = swap_uint16(len_arr[c]);
#       endif
        if (len_arr[c] > max_in_line)
            max_in_line = len_arr[c];
    }
    
    void* in_line = malloc(max_in_line);
    for (c=0 ; c<height*chan_count ; ++c) {
        fread(in_line, 1, len_arr[c], psdf);
        PackBitsDecode(out_buf, width, in_line, len_arr[c]);
        out_buf += width;
    }
    free(in_line);
    
    free(len_arr);
}
Пример #19
0
static void swap_netmon_packet(struct cap_packet *p)
{
	swap_uint32(&p->cap_time);
	swap_uint16(&p->len);
	swap_uint16(&p->caplen);
}
Пример #20
0
static void swap_snoop_header(struct snoop_file_header *h)
{
	swap_uint16(&h->major);
	swap_uint16(&h->minor);
	swap_uint32(&h->linktype);
}
Пример #21
0
int manage_ncp_loop(void * packiet_data,uint16_t packiet_len )
{
    int i =0;
    ethernet_header*eh;
    ip_header * ip;
    udp_header * udp;
    ncp_header * ncp;
    get_headers(packiet_data,&eh,&ip,&udp,&ncp);
    input_size = swap_uint16(udp->uh_ulen);

    do
    {
        current_state = next_state;
        switch (current_state)
        {
        case START:

            ncp_register(&m_ncp,NCP_HEADER_SIZE);
            output_size = create_packiet(output_packiet,ETHER_MAX_LEN,m_ncp,NCP_HEADER_SIZE);
            TransmitPacket(output_packiet,output_size);
            // printf("Registered client\n");
            next_state = IDLE;
            break;
        case IDLE:
            if(packiet_len == 0)
                break;

            if(ncp->ncp_comm == NCP_DATA)
            {
                i = memcmp(&(ncp->ncp_client),get_host_uuid(),sizeof(uuid));
                if(i == 0)
                    next_state = PROCESSING ;
            }

            break;
        // wait for processing order
        case PROCESSING:
            output_size = process_ncp(ncp,input_size,m_ncp,NCP_HEADER_SIZE);
            printf ("PROCESSING \n");
            next_state = RESULT;
            break;

        case RESULT:

            ncp_result(m_ncp,NCP_HEADER_SIZE,0,0);
            output_size = create_packiet(output_packiet,ETHER_MAX_LEN,m_ncp,output_size);
            TransmitPacket(output_packiet,output_size);
            packiet_len = 0;
            next_state = IDLE;
            break;
        case MAX_STATE:
            printf ("Manage Ncp: unpredicted state\n");
            break;
        default:
            printf ("Manage Ncp: unknow state\n");
        };


    } while (current_state != next_state);

    return 0;
}
Пример #22
0
uint16_t NetworkMessage::GetU16()
{
	uint16_t v = *(uint16_t*)(m_MsgBuf + m_ReadPos);
	m_ReadPos += 2;
	return swap_uint16(v);
}
Пример #23
0
/* Routine to extract named layer from Photoshop document */
int PSD_decode(const char* file_path, const char* file_path_ext,
               pspl_tm_image_t* image_out) {
    int x,y,c;
    
    // Open file
    FILE* psdf = fopen(file_path, "r");
    if (!psdf)
        pspl_error(-1, "Unable to open PSD", "error opening PSD `%s` - errno: %d (%s)",
                   file_path, errno, strerror(errno));
    
    
#   pragma mark Read Header
    
    // Read header
    PSD_head_t psd_head;
    if(fread(&psd_head, 1, sizeof(PSD_head_t), psdf) != sizeof(PSD_head_t))
        pspl_error(-1, "Unable to read PSD header", "error reading header of PSD `%s`",
                   file_path);
    
    // Validate header
    if (memcmp(psd_head.signature, "8BPS", 4))
        pspl_error(-1, "Invalid PSD signature", "signature of PSD `%s` needs to be '8BPS'",
                   file_path);
    
    // Swap for little-endian platforms
#   ifdef __LITTLE_ENDIAN__
    psd_head.version = swap_uint16(psd_head.version);
    psd_head.num_channels = swap_uint16(psd_head.num_channels);
    psd_head.height = swap_uint32(psd_head.height);
    psd_head.width = swap_uint32(psd_head.width);
    psd_head.depth = swap_uint16(psd_head.depth);
    psd_head.colour_mode = swap_uint16(psd_head.colour_mode);
#   endif
    
    // Validate version
    if (psd_head.version != 1)
        pspl_error(-1, "Only PSD version 1 files supported", "unable to use `%s`; PSB 'BIG' files aren't supported",
                   file_path);
    
    // Validate depth
    if (psd_head.depth != 8)
        pspl_error(-1, "Only 8-bit-per-channel PSD files supported", "unable to use `%s`; PSD %d-bit files aren't supported",
                   file_path, psd_head.depth);
    
    // Validate colour mode
    if (psd_head.colour_mode != PSD_COLOUR_RGB)
        pspl_error(-1, "Only RGB PSD files supported", "unable to use `%s`",
                   file_path);

    
    
#   pragma mark Read Colour Mode Data
    
    uint32_t cm_len = 0;
    fread(&cm_len, 1, sizeof(uint32_t), psdf);
#   ifdef __LITTLE_ENDIAN__
    cm_len = swap_uint32(cm_len);
#   endif
    
    fseek(psdf, cm_len, SEEK_CUR);
    
    
#   pragma mark Read Image Resources Data
    
    uint32_t ir_len = 0;
    fread(&ir_len, 1, sizeof(uint32_t), psdf);
#   ifdef __LITTLE_ENDIAN__
    ir_len = swap_uint32(ir_len);
#   endif
    
    fseek(psdf, ir_len, SEEK_CUR);
    
    
#   pragma mark Read Layer and Mask Data
    
    uint32_t lm_len = 0;
    fread(&lm_len, 1, sizeof(uint32_t), psdf);
#   ifdef __LITTLE_ENDIAN__
    lm_len = swap_uint32(lm_len);
#   endif
    
    if (file_path_ext) {
        if (!lm_len)
            pspl_error(-1, "No layers in PSD", "requested layer '%s' from PSD `%s` which has no layers",
                       file_path_ext, file_path);
        
        // Find named layer and read in

        fseek(psdf, 4, SEEK_CUR);
        int16_t layer_count;
        fread(&layer_count, 1, sizeof(int16_t), psdf);
#       ifdef __LITTLE_ENDIAN__
        layer_count = swap_uint16(layer_count);
#       endif
        if (layer_count < 0)
            layer_count = abs(layer_count);
        
        // If we found proposed layer
        uint8_t found_layer = 0;
        
        // Rectangle of proposed layer
        PSD_rect_t layer_rect;
        
        // Channel data of proposed layer
        uint16_t layer_chan_count;
        PSD_layer_channel layer_chan_arr[4];
        
        // Offset of proposed layer's image data
        size_t layer_chan_data_off = 0;
        
        // Navigate layer info data
        int i;
        for (i=0 ; i<layer_count ; ++i) {
            size_t next_layer_chan_data_off = 0;
            
            // Layer rectangle
            if (found_layer) {
                fseek(psdf, sizeof(PSD_rect_t), SEEK_CUR);
            } else {
                fread(&layer_rect, 1, sizeof(PSD_rect_t), psdf);
#               ifdef __LITTLE_ENDIAN__
                layer_rect.bottom = swap_uint32(layer_rect.bottom);
                layer_rect.left = swap_uint32(layer_rect.left);
                layer_rect.top = swap_uint32(layer_rect.top);
                layer_rect.right = swap_uint32(layer_rect.right);
#               endif
            }
            
            // Advance past channels
            uint16_t chan_count;
            fread(&chan_count, 1, sizeof(uint16_t), psdf);
#           ifdef __LITTLE_ENDIAN__
            chan_count = swap_uint16(chan_count);
#           endif
            if (!found_layer)
                layer_chan_count = (chan_count > 4)?4:chan_count;
            int j;
            for (j=0 ; j<chan_count ; ++j) {
                if (found_layer || j>3) {
                    fseek(psdf, sizeof(PSD_layer_channel), SEEK_CUR);
                } else {
                    PSD_layer_channel* chan = &layer_chan_arr[j];
                    fread(chan, 1, sizeof(PSD_layer_channel), psdf);
#                   ifdef __LITTLE_ENDIAN__
                    chan->chan_id = swap_uint16(chan->chan_id);
                    chan->chan_len = swap_uint32(chan->chan_len);
#                   endif
                    next_layer_chan_data_off += chan->chan_len;
                }
            }
            
            // Ensure '8BIM' occurs here
            char check[4];
            fread(check, 1, 4, psdf);
            if (memcmp(check, "8BIM", 4))
                pspl_error(-1, "Inconsistent PSD detected", "unable to find expected blend-mode signature in `%s`",
                           file_path);
            
            // Advance to layer name
            fseek(psdf, 8, SEEK_CUR);
            uint32_t extra_len;
            fread(&extra_len, 1, sizeof(uint32_t), psdf);
#           ifdef __LITTLE_ENDIAN__
            extra_len = swap_uint32(extra_len);
#           endif
            size_t data_off = ftell(psdf) + extra_len;
            fread(&extra_len, 1, sizeof(uint32_t), psdf);
#           ifdef __LITTLE_ENDIAN__
            extra_len = swap_uint32(extra_len);
#           endif
            fseek(psdf, extra_len, SEEK_CUR);
            fread(&extra_len, 1, sizeof(uint32_t), psdf);
#           ifdef __LITTLE_ENDIAN__
            extra_len = swap_uint32(extra_len);
#           endif
            fseek(psdf, extra_len, SEEK_CUR);
            
            // This should be the name
            uint8_t name_len;
            fread(&name_len, 1, sizeof(uint8_t), psdf);
            char name[256];
            fread(name, 1, name_len, psdf);
            name[name_len] = '\0';
            
            // Check against requested name
            if (!strcmp(name, file_path_ext)) {
                if (found_layer)
                    pspl_error(-1, "Duplicate PSD Layer",
                               "PSPL can't make assumptions about which identically named `%s` layer you'd like", name);
                found_layer = 1;
            }
            
            // Accumulate layer data offset
            if (!found_layer)
                layer_chan_data_off += next_layer_chan_data_off;
            
            // Advance to data
            fseek(psdf, data_off, SEEK_SET);
            
        }

        // Ensure layer was found
        if (!found_layer)
            pspl_error(-1, "Unable to find PSD layer", "PSD `%s` does not contain a layer named '%s'",
                       file_path, file_path_ext);
        
        // Read in layer image data
        fseek(psdf, layer_chan_data_off, SEEK_CUR);
        
        // Decompressed buffer
        size_t im_len = (layer_rect.right-layer_rect.left) * (layer_rect.bottom-layer_rect.top) *
                        layer_chan_count;
        void* im_data = malloc(im_len);
        
        // Read in
        size_t chan_size = (layer_rect.right-layer_rect.left) * (layer_rect.bottom-layer_rect.top);
        void* chan_cur = im_data;
        for (i=0 ; i<layer_chan_count ; ++i) {
            int16_t im_comp_mode = 0;
            fread(&im_comp_mode, 1, sizeof(int16_t), psdf);
#           ifdef __LITTLE_ENDIAN__
            im_comp_mode = swap_uint16(im_comp_mode);
#           endif

            if (im_comp_mode == 0) {
                if (fread(chan_cur, 1, layer_chan_arr[i].chan_len-2, psdf) != layer_chan_arr[i].chan_len-2)
                    pspl_error(-1, "Unexpected end of PSD", "unable to read layer image data from `%s`",
                               file_path);
                chan_cur += chan_size;
            } else if (im_comp_mode == 1) {
                PSD_unpackbits(chan_cur, 1, layer_rect.right-layer_rect.left,
                               layer_rect.bottom-layer_rect.top, psdf);
                chan_cur += chan_size;
            } else
                pspl_error(-1, "Compressed PSD formats unsupported", "PSD `%s` has compressed image data",
                           file_path);
        }
        


        
        // Rearrange data in scanline, channel-interleaved order
        uint8_t* im_dest = malloc(psd_head.width * psd_head.height * layer_chan_count);
        const uint8_t* im_cur_chan = im_data;
        for (c=0 ; c<layer_chan_count ; ++c) {
            int ci = (layer_chan_arr[c].chan_id == -1)?layer_chan_count-1:layer_chan_arr[c].chan_id;
            for (y=0 ; y<psd_head.height ; ++y) {
                const uint8_t* im_cur_line = im_cur_chan + ((y - layer_rect.top) * (layer_rect.right - layer_rect.left));
                uint8_t line_limit = (layer_rect.top > y || layer_rect.bottom < y);
                unsigned line_base = psd_head.width * y * layer_chan_count;
                for (x=0 ; x<psd_head.width ; ++x) {
                    uint8_t limit = (line_limit)?1:(layer_rect.left > x || layer_rect.right < x);
                    unsigned col = x * layer_chan_count;
                    im_dest[line_base+col+ci] = (limit)?0:*(im_cur_line + (x - layer_rect.left));
                }
            }
            im_cur_chan += chan_size;
        }
        
        // Done with read data
        free(im_data);
        
        // Populate TM structure
        image_out->image_type = layer_chan_count;
        image_out->height = psd_head.height;
        image_out->width = psd_head.width;
        image_out->image_buffer = im_dest;
        image_out->index_buffer = NULL;
        
        fclose(psdf);
        return 0;
        
    }
    
    // Skip to next section otherwise
    fseek(psdf, lm_len, SEEK_CUR);
    
    
#   pragma mark Read Merged Image Data (if layer not specified)
    
    if (!file_path_ext) {
        
        uint16_t im_comp_mode = 0;
        if (!fread(&im_comp_mode, 1, sizeof(uint16_t), psdf))
            pspl_error(-1, "No merged image data in PSD", "PSD `%s` was not saved with 'Maximise Compatibility' checked",
                       file_path);
#       ifdef __LITTLE_ENDIAN__
        im_comp_mode = swap_uint16(im_comp_mode);
#       endif
        size_t im_len = psd_head.width * psd_head.height * psd_head.num_channels;
        void* im_data = malloc(im_len);
        if (im_comp_mode == 0) {
            if (fread(im_data, 1, im_len, psdf) != im_len)
                pspl_error(-1, "Unexpected end of PSD", "unable to read all image data from `%s`",
                           file_path);
        } else if (im_comp_mode == 1) {
            PSD_unpackbits(im_data, psd_head.num_channels, psd_head.width, psd_head.height, psdf);
        } else
            pspl_error(-1, "Compressed PSD formats unsupported", "PSD `%s` has compressed merged image data",
                       file_path);

        
        // Rearrange merged data in scanline, channel-interleaved order
        const uint8_t* im_cur = im_data;
        unsigned out_num_channels = (psd_head.num_channels > 4)?4:psd_head.num_channels;
        uint8_t* im_dest = malloc(psd_head.width * psd_head.height * out_num_channels);
        for (c=0 ; c<out_num_channels ; ++c) {
            for (y=0 ; y<psd_head.height ; ++y) {
                unsigned line_base = psd_head.width * y * out_num_channels;
                for (x=0 ; x<psd_head.width ; ++x) {
                    unsigned col = x * out_num_channels;
                    im_dest[line_base+col+c] = *(im_cur++);
                }
            }
        }
        
        // Done with read data
        free(im_data);
        
        // Populate TM structure
        image_out->image_type = out_num_channels;
        image_out->height = psd_head.height;
        image_out->width = psd_head.width;
        image_out->image_buffer = im_dest;
        image_out->index_buffer = NULL;
        
    }
    
    fclose(psdf);
    return 0;
    
}
void packetDetails_ip(uint8_t *packetData)
{
	printf("IP header \n");

	ipHeader = (ipHeader_t*)(packetData);

	PRINTINFO("Version");
	PRINTUINT8(ipHeader->version);

	PRINTINFO("Header length");
	PRINTUINT8(ipHeader->headerLength * 4);

	PRINTINFO("Type of service");
	PRINTHEX8(ipHeader->typeOfService);

	PRINTINFO("Total length");
	PRINTUINT16(swap_uint16(ipHeader->totalLength));

	PRINTINFO("Identification");
	printf("0x%04" PRIx16 " (%" PRIu16 ")\n", swap_uint16(ipHeader->identification), swap_uint16(ipHeader->identification));

	// Flags
	uint16_t flag = swap_uint16(ipHeader->flags);

	PRINTFLAG("Reserved bit");
	printf("%s\n", flagSetOrNot((flag & IP_reservedFlag) >> IP_reservedFlagPos));

	PRINTFLAG("Don't fragment");
	printf("%s\n", flagSetOrNot((flag & IP_dontFragment) >> IP_dontFragmentPos));

	PRINTFLAG("More fragments");
	printf("%s\n", flagSetOrNot((flag & IP_moreFragments) >> IP_moreFragmentsPos));

	PRINTFLAG("Fragment offset");
	PRINTHEX16(flag & IP_fragmentOffset);

	PRINTINFO("Time to live");
	PRINTUINT8(ipHeader->timeToLive);

	PRINTINFO("Protocol");
	printf("%s (%" PRIu8 ")\n", determineIpProtocol(ipHeader->protocol), ipHeader->protocol);

	PRINTINFO("Checksum");
	PRINTHEX16(swap_uint16(ipHeader->checksum));

	PRINTINFO("Source address");
	for(i=0; i<3; i++)
		printf("%" PRIu8 ".", ipHeader->sourceAddress[i]);
	printf("%" PRIu8 "\n", ipHeader->sourceAddress[3]);

	PRINTINFO("Destination address");
	for(i=0; i<3; i++)
		printf("%" PRIu8 ".", ipHeader->destinationAddress[i]);
	printf("%" PRIu8 "\n", ipHeader->destinationAddress[3]);

	uint8_t sizeOfIP = ipHeader->headerLength * 4;
	switch(ipHeader->protocol)
	{
		case 6:
			packetDetails_tcp(packetData + sizeOfIP);
			break;
		case 17:
			packetDetails_udp(packetData + sizeOfIP);
			break;
	}
}
Пример #25
0
void BMS_parseCanMessage(BatteryManagementSystem* bms, IO_CAN_DATA_FRAME* bmsCanMessage){
    ubyte2 utemp16;
//    sbyte1  temp16;
    ubyte4 utemp32;


    switch (bmsCanMessage->id)
    {

    case 0x622:

        bms->state = bmsCanMessage->data[0];
        utemp16 = (ubyte2)bmsCanMessage->data[1] << 8 | bmsCanMessage->data[2];
        bms->timer = swap_uint16(utemp16);
        bms->flags = bmsCanMessage->data[3];
        bms->faultCode = bmsCanMessage->data[4];
        bms->levelFaults = bmsCanMessage->data[5];
        bms->warnings = bmsCanMessage->data[6];
        break;

    case 0x623:

        //    utemp16 = ((bmsCanMessage->data[0] << 8) | (bmsCanMessage->data[1]));
        //    bms->packVoltage = swap_uint16(utemp16);  //65535 = 65535V
        bms->minVtg = (bmsCanMessage->data[2] / 10);    //255 = 25.5V
        bms->minVtgCell = bmsCanMessage->data[3];       //1-254
        bms->maxVtg = (bmsCanMessage->data[4] / 10);    //255 = 25.5V
        bms->maxVtgCell = bmsCanMessage->data[5];       //1-254

        break;

    case 0x624:

        //    temp16 = ((bmsCanMessage->data[0] << 8) | (bmsCanMessage->data[1]));
        //    bms->packCurrent = swap_int16(temp16);

        utemp16 = ((bmsCanMessage->data[2] << 8) | (bmsCanMessage->data[3]));
        bms->chargeLimit = swap_uint16(utemp16);

        utemp16 = ((bmsCanMessage->data[4] << 8) | (bmsCanMessage->data[5]));
        bms->dischargeLimit = swap_uint16(utemp16);

        break;

    case 0x625:

        utemp32 = ((((ubyte4)bmsCanMessage->data[0] << 24) |
                ((ubyte4)bmsCanMessage->data[1] << 16) |
                ((ubyte4)bmsCanMessage->data[2] << 8) |
                (bmsCanMessage->data[3])));
        bms->batteryEnergyIn = swap_uint32(utemp32);

        utemp32 = ((((ubyte4)bmsCanMessage->data[4] << 24) |
                ((ubyte4)bmsCanMessage->data[5] << 16) |
                ((ubyte4)bmsCanMessage->data[6] << 8) |
                ((ubyte4)bmsCanMessage->data[7])));
        bms->batteryEnergyOut = swap_uint32(utemp32);

        break;

    case 0x626:

            bms->SOC = bmsCanMessage->data[0];

        utemp16 = ((bmsCanMessage->data[1] << 8) | (bmsCanMessage->data[2]));
        bms->DOD = swap_uint16(utemp16);

        utemp16 = ((bmsCanMessage->data[3] << 8) | (bmsCanMessage->data[4]));
        bms->capacity = swap_uint16(utemp16);
        bms->SOH = bmsCanMessage->data[6];

        break;

    case 0x627:

        //bms->packTemp = bmsCanMessage->data[0];
        bms->minTemp = bmsCanMessage->data[2];
        bms->minTempCell = bmsCanMessage->data[3];
        bms->maxTemp = bmsCanMessage->data[4];
        bms->maxTempCell = bmsCanMessage->data[5];

        break;

    case 0x628:

        utemp16 = ((bmsCanMessage->data[0] << 8) | (bmsCanMessage->data[1]));
        bms->packRes = ((swap_uint16(utemp16)) / 10000);
//        1 = 100 mOhm
//        10 = 1000 mOhm = 0.001 ohm = 10/10000
//        100 = 10000 mohm = 0.01 ohm
//        1000 = 100000 mOhM = 0.1 ohm
//        10000 = 1000000 mOhm = 1 ohm
        bms->minRes = (bmsCanMessage->data[2] / 10000);
        bms->minResCell = bmsCanMessage->data[3];
        bms->maxRes = (bmsCanMessage->data[4] / 10000);
        bms->maxResCell = bmsCanMessage->data[5];
        break;

    case 0x629:
        //See https://onedrive.live.com/view.aspx?resid=F9BB8F0F8FDB5CF8!36803&ithint=file%2cxlsx&app=Excel&authkey=!AI-YHJrHmtUaWpI
        //Voltage(100mV)[022] - 2 bytes little endian (1=100mV) 0000000000000001 = 1 = 100 mV = 0.1 V
                                                        //        5 = 500 mV = 0.5 V
                                                        //        10 = 1000 mV = 1 V
                                                        //       100 = 10000 mV = 10 V
                                                        //         1000 = 100000 mV = 100 V
        //Current(100mA)[054] - 2 bytes little endian (1=100mA)
        //Max Temp[104] - in C
        //Avg Temp[096] - in C
        //SOC(%)[112] - %
        //DCL(%)[080]- %  //**CHANGED**
        
        bms->packVoltage = (((bmsCanMessage->data[1] << 8) | (bmsCanMessage->data[0])) / 10); //V
        bms->packCurrent = (((bmsCanMessage->data[3] << 8) | (bmsCanMessage->data[2])) / 10); //V
        bms->maxTemp = ((bmsCanMessage->data[4]));  //C
        bms->avgTemp = ((bmsCanMessage->data[5]));  //C
        bms->CCL = ((bmsCanMessage->data[6]));    //%
        bms->DCL = ((bmsCanMessage->data[7]));    //%

        break;
    }
}
Пример #26
0
int main(int argc, char **argv) {
   FILE *fp;
   short *buffer = (short *)malloc(file_frames * frame_size * 4);
   void *handle = PortBurn_Open();
   int count = PortBurn_GetNumDevices(handle);
   int i, j;
   unsigned int swaptest;
   int needswap;

   swaptest = 0x01020304;
   if (((unsigned char *)&swaptest)[0] == 1) {
      printf("Big-endian machine, will do byteswapping\n");
      needswap = 1;
   }
   else {
      printf("Little-endian machine, will not do byteswapping\n");
      needswap = 0;
   }

   if (count == 0) {
      printf("No devices found!\n");
      goto end;
   }

   for(i = 0; i < count; i++) {
      char *name = PortBurn_GetDeviceName(handle, i);
      printf("Device %d: '%s'\n", i, name);
      free(name);
   }

   printf("Using the first device\n");

   FailErr(PortBurn_OpenDevice(handle, 0));

   printf("Press enter when the device is ready\n");
   getchar();

   int state;
   FailErr(PortBurn_GetMediaState(handle, &state));
   if (!(state & pbMediaBlank)) {
      printf("Media is not blank...erase? (y=erase, anything else aborts)\n");
      if (getchar() != 'y') {
         printf("aborting...\n");
         goto end;
      }

      FailErr(PortBurn_StartErasing(handle, pbEraseQuick));
      float complete;
      while (PortBurn_GetEraseStatus(handle, &complete) == pbSuccess) {
         if (complete == 1.0f) {
            break;
         }
#if defined(_WIN32)
//         Sleep(1000);
#else
         sleep(1);
#endif
      }
   }

   printf("Staging audio\n");
#if defined(_WIN32)
   FailErr(PortBurn_StartStaging(handle, "c:\temp"));
#else
   FailErr(PortBurn_StartStaging(handle, "/tmp"));
#endif

   fp = fopen(filename, "r");
   if (!fp) {
      printf("Couldn't open file: %s\n", filename);
      goto end;
   }
   fseek(fp, 44, SEEK_SET);
   fread(buffer, 2, file_frames * frame_size * 2, fp);
   fclose(fp);

   if (needswap) {
      for(i = 0; i < file_frames * frame_size * 2; i++) {
         ((unsigned short *)buffer)[i] =
            swap_uint16(((unsigned short *)buffer)[i]);
      }
   }

   FailErr(PortBurn_StartTrack(handle, "80 loops"));
   for(j = 0; j < 80; j++) {
      for(i = 0; i < file_frames; i++) {
         FailErr(PortBurn_AddFrame(handle, &buffer[i * 2 * frame_size]));
      }
   }
   FailErr(PortBurn_EndTrack(handle));

   FailErr(PortBurn_StartTrack(handle, "100 loops"));
   for(j = 0; j < 100; j++) {
      for(i = 0; i < file_frames; i++) {
         FailErr(PortBurn_AddFrame(handle, &buffer[i * 2 * frame_size]));
      }
   }
   FailErr(PortBurn_EndTrack(handle));

   printf("Burning!!!\n");
   FailErr(PortBurn_StartBurning(handle));

   for(;;) {
      float frac;
      int result;

      result = PortBurn_GetStatus(handle, &frac);
      if (result != 0)
         break;

      printf("Status: %.3f\n", frac);

      if (frac == 1.0f) {
         printf("Completed!!!\n");
         break;
      }
#if defined(_WIN32)
      Sleep(1000);
#else
      sleep(1);
#endif
   }

end:
   printf("Cleaning up\n");
   PortBurn_CloseDevice(handle);

   if (handle) {
      PortBurn_Close(handle);
   }

   printf("Press enter to end\n");
   getchar();


   return 0;
}