0
Anyone can give me a idea of how implement in C this concept of Variable length integer which encoded the numer of transactions in a block? based on what is described here, I think is something lime that:
if(value=?)
uint8_t number_transactions = length;
else if(value=?)
uint16_t number_transactions = 0xFD+length;
else if(value=?)
uint32_t number_transactions = 0xFE_length;
else
uint64_t number_transactions = 0xFF_length;
where number_transactions would be the value concatenated on the block. But how implement this? This code does the same described in the bitcoin wiki?
UPDATE
char* varint(unsigned long size) {
char* number;
if(size < 252) {
number = malloc(1);
number[0] = (uint8_t)size;
} else if(size>=253 && size<65535) {
number = malloc(3);
number[0] = 0xfd;
memcpy(&number[1], (uint16_t)&size, sizeof(uint16_t));
} else if(size>=65536 && size<4294967295) {
number = malloc(5);
number[0] = 0xfe;
memcpy(&number[1], (uint32_t)&size, sizeof(uint32_t));
} else if(size>=4294967296) {
number = malloc(9);
number[0] = 0xff;
memcpy(&number[1], (uint64_t)&size, sizeof(uint64_t));
}
return NULL;
}
message from compiler:
bitcoin_rpc.c: In function ‘varint’:
bitcoin_rpc.c:230:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
memcpy(&number[1], (uint16_t)&size, sizeof(uint16_t));
^
bitcoin_rpc.c:230:24: warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [-Wint-conversion]
In file included from bitcoin_rpc.c:4:0:
/usr/include/string.h:42:14: note: expected ‘const void * restrict’ but argument is of type ‘short unsigned int’
extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
^~~~~~
bitcoin_rpc.c:234:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
memcpy(&number[1], (uint32_t)&size, sizeof(uint32_t));
^
bitcoin_rpc.c:234:24: warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [-Wint-conversion]
In file included from bitcoin_rpc.c:4:0:
/usr/include/string.h:42:14: note: expected ‘const void * restrict’ but argument is of type ‘unsigned int’
extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
^~~~~~
bitcoin_rpc.c:238:24: warning: passing argument 2 of ‘memcpy’ makes pointer from integer without a cast [-Wint-conversion]
memcpy(&number[1], (uint64_t)&size, sizeof(uint64_t));
^
In file included from bitcoin_rpc.c:4:0:
/usr/include/string.h:42:14: note: expected ‘const void * restrict’ but argument is of type ‘long unsigned int’
extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
I implement a version of this method based on your sugestion, could you check if it's works? Also, there is some compiler warnings; do you have any sugestion of how to solve them? – Kleber Mota – 2017-08-28T02:36:24.570
1You are casting pointers to uints. Don't do that since you are changing the pointer type, not the number type. Instead you should cast the variable first, then get a pointer to it or cast to a type pointer (e.g. uint32_t*) – Andrew Chow – 2017-08-28T03:23:44.823