int deserialize_varint(struct buff *buf, uint64 *val) { int res; uint8 c; res = deserialize_uint8(buf, &c); if (res) { return res; } if (c < 253) { *val = c; } else if (c == 253) { uint16 len16 = 0; res = deserialize_uint16(buf, &len16); *val = len16; } else if (c == 254) { uint32 len32 = 0; res = deserialize_uint32(buf, &len32); *val = len32; } else { uint64 len64 = 0; res = deserialize_uint64(buf, &len64); *val = len64; } return res; }
int deserialize_version(struct buff *buf, btc_msg_version *v) { int res; memset(v, 0, sizeof *v); res = deserialize_uint32(buf, &v->version); res |= deserialize_uint64(buf, &v->services); res |= deserialize_uint64(buf, &v->time); res |= deserialize_addr(BTC_PROTO_MIN, buf, &v->addrTo); res |= deserialize_addr(BTC_PROTO_MIN, buf, &v->addrFrom); res |= deserialize_uint64(buf, &v->nonce); res |= deserialize_str(buf, v->strVersion, sizeof v->strVersion); res |= deserialize_uint32(buf, &v->startingHeight); if (v->version >= BTC_PROTO_FILTERING && buff_space_left(buf) > 0) { res |= deserialize_uint8(buf, &v->relayTx); } else { v->relayTx = 1; } ASSERT(buff_space_left(buf) == 0); return res; }
int deserialize_addr(uint32 protversion, struct buff *buf, btc_msg_address *addr) { int res = 0; addr->time = 0; if (protversion >= BTC_PROTO_ADDR_W_TIME) { res = deserialize_uint32(buf, &addr->time); } res |= deserialize_uint64(buf, &addr->services); res |= deserialize_bytes(buf, addr->ip, ARRAYSIZE(addr->ip)); res |= deserialize_uint16(buf, &addr->port); return res; }
int deserialize_tx(struct buff *buf, btc_msg_tx *tx) { uint64 i; int res; res = deserialize_uint32(buf, &tx->version); res |= deserialize_varint(buf, &tx->in_count); tx->tx_in = safe_malloc(tx->in_count * sizeof *tx->tx_in); for (i = 0; i < tx->in_count; i++) { res |= deserialize_uint256(buf, &tx->tx_in[i].prevTxHash); res |= deserialize_uint32(buf, &tx->tx_in[i].prevTxOutIdx); res |= deserialize_varint(buf, &tx->tx_in[i].scriptLength); tx->tx_in[i].scriptSig = safe_malloc(tx->tx_in[i].scriptLength); res |= deserialize_bytes(buf, tx->tx_in[i].scriptSig, tx->tx_in[i].scriptLength); res |= deserialize_uint32(buf, &tx->tx_in[i].sequence); } res |= deserialize_varint(buf, &tx->out_count); tx->tx_out = safe_malloc(tx->out_count * sizeof *tx->tx_out); for (i = 0; i < tx->out_count; i++) { res |= deserialize_uint64(buf, &tx->tx_out[i].value); res |= deserialize_varint(buf, &tx->tx_out[i].scriptLength); tx->tx_out[i].scriptPubKey = safe_malloc(tx->tx_out[i].scriptLength); res |= deserialize_bytes(buf, tx->tx_out[i].scriptPubKey, tx->tx_out[i].scriptLength); } res |= deserialize_uint32(buf, &tx->lock_time); ASSERT_NOT_TESTED(buff_space_left(buf) == 0); return res; }
static struct tx_ser_data * txdb_deserialize_tx_data(const void *val, size_t vlen) { struct tx_ser_data *tx; struct buff buf; ASSERT(val); buff_init(&buf, (void *)val, vlen); tx = safe_malloc(sizeof *tx); deserialize_uint256(&buf, &tx->blkHash); deserialize_uint64(&buf, &tx->timestamp); deserialize_varint(&buf, &tx->len); tx->buf = safe_calloc(1, tx->len + 1); deserialize_bytes(&buf, tx->buf, tx->len); ASSERT(buff_space_left(&buf) == 0); return tx; }