Ejemplo n.º 1
0
/** Dissects an SIR packet. */
static void
dissect_sir(tvbuff_t *tvb, packet_info *pinfo, proto_tree *root)
{
    gint offset = 0;
    gint bof_offset;
    gint eof_offset;

    while (tvb_length_remaining(tvb, offset) > 0) {
        bof_offset = tvb_find_guint8(tvb, offset, -1, SIR_BOF);
        eof_offset = (bof_offset == -1) ? -1 :
                     tvb_find_guint8(tvb, bof_offset, -1, SIR_EOF);

        if (bof_offset == -1 || eof_offset == -1) {
            if (pinfo->can_desegment) {
                pinfo->desegment_offset = offset;
                pinfo->desegment_len = 1;
            }
            return;
        } else {
            guint preamble_len = bof_offset - offset;
            gint data_offset = bof_offset + 1;
            tvbuff_t* next_tvb = tvb_new_subset(tvb,
                                                data_offset, eof_offset - data_offset, -1);
            next_tvb = unescape_data(next_tvb, pinfo);
            if (root) {
                guint data_len = tvb_length(next_tvb) < 2 ? 0 :
                                 tvb_length(next_tvb) - 2;
                proto_tree* ti = proto_tree_add_protocol_format(root,
                                 proto_sir, tvb, offset, eof_offset - offset + 1,
                                 "Serial Infrared, Len: %d", data_len);
                proto_tree* tree = proto_item_add_subtree(ti, ett_sir);
                if (preamble_len > 0)
                    proto_tree_add_item(tree, hf_sir_preamble, tvb,
                                        offset, preamble_len, ENC_NA);
                proto_tree_add_item(tree, hf_sir_bof, tvb,
                                    bof_offset, 1, ENC_BIG_ENDIAN);
                proto_tree_add_uint(tree, hf_sir_length,
                                    next_tvb, 0, data_len, data_len);
                next_tvb = checksum_data(next_tvb, tree);
                proto_tree_add_item(tree, hf_sir_eof, tvb,
                                    eof_offset, 1, ENC_BIG_ENDIAN);
            } else {
                next_tvb = checksum_data(next_tvb, NULL);
            }
            call_dissector(irda_handle, next_tvb, pinfo, root);
        }
        offset = eof_offset + 1;
    }
}
Ejemplo n.º 2
0
uint64_t addr_key_checksum(const output_point& outpoint)
{
    data_chunk checksum_data(hash_digest_size + 4);
    auto serial = make_serializer(checksum_data.begin());
    serial.write_hash(outpoint.hash);
    serial.write_4_bytes(outpoint.index);
    BITCOIN_ASSERT(serial.iterator() == checksum_data.end());
    hash_digest hash = generate_sha256_hash(checksum_data);
    data_chunk raw_checksum(hash.begin(), hash.begin() + 8);
    return cast_chunk<uint64_t>(raw_checksum);
}
Ejemplo n.º 3
0
bool psxcard_device::transfer(uint8_t to, uint8_t *from)
{
	bool ret=true;

	switch (state)
	{
		case state_illegal:
			if (is_loaded())
			{
//              printf("CARD: begin\n");
				state = state_command;
				*from = 0x00;
			}
			else
			{
				ret = false;
			}
			break;

		case state_command:
			cmd=to;
			*from=0x5a;
			state=state_cmdack;
			break;

		case state_cmdack:
			*from=0x5d;
			state=state_wait;
			break;

		case state_wait:
			*from=0x00;
			state=state_addr_hi;
			break;

		case state_addr_hi:
			addr=(to<<8);
//          printf("addr_hi: %02x, addr = %x\n", to, addr);
			*from=to;
			state=state_addr_lo;
			break;

		case state_addr_lo:
			addr|=(to&0xff);
//          printf("addr_lo: %02x, addr = %x, cmd = %x\n", to, addr, cmd);

			switch (cmd)
			{
				case 'R':   // 0x52
				{
					pkt[0]=*from=0x5c;
					pkt[1]=0x5d;
					pkt[2]=(addr>>8);
					pkt[3]=(addr&0xff);
					read_card(addr,&pkt[4]);
					pkt[4+128]=checksum_data(&pkt[2],128+2);
					pkt[5+128]=0x47;
					pkt_sz=6+128;
					pkt_ptr=1;
					state=state_read;
					break;
				}
				case 'W':   // 0x57
				{
					pkt[0]=addr>>8;
					pkt[1]=addr&0xff;
					pkt_sz=129+2;
					pkt_ptr=2;
					state=state_write;
					*from=to;
					break;
				}
				default:
					state=state_illegal;
					break;
			}
			break;

		case state_read:
			//assert(to==0);
//          printf("state_read: pkt_ptr = %d, pkt_sz = %d\n", pkt_ptr, pkt_sz);
			*from=pkt[pkt_ptr++];
			if (pkt_ptr==pkt_sz)
			{
				#ifdef debug_card
					printf("card: read finished\n");
				#endif

				state=state_end;
			}
			break;

		case state_write:
			*from=to;
			pkt[pkt_ptr++]=to;
			if (pkt_ptr==pkt_sz)
			{
				*from=0x5c;
				state=state_writeack_2;
			}
			break;

		case state_writeack_2:
			*from=0x5d;
			state=state_writechk;
			break;

		case state_writechk:
		{
			unsigned char chk=checksum_data(pkt,128+2);
			if (chk==pkt[128+2])
			{
				#ifdef debug_card
					printf("card: write ok\n");
				#endif

				write_card(addr,pkt+2);

				*from='G';
			} else
			{
				#ifdef debug_card
					printf("card: write fail\n");
				#endif

				*from='N';
			}
			state=state_end;
			break;
		}

		case state_end:
			ret = false;
			state = state_illegal;
			break;

		default: /*assert(0);*/ ret=false; break;
	}

	#ifdef debug_card
//      printf("card: transfer to=%02x from=%02x ret=%c\n",to,*from,ret ? 'T' : 'F');
	#endif

	return ret;
}