forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move varint handling from tx.c and generalize it. Signed-off-by: Rusty Russell <[email protected]>
- Loading branch information
1 parent
af080d5
commit 6b956ea
Showing
6 changed files
with
94 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "varint.h" | ||
|
||
size_t varint_put(u8 buf[VARINT_MAX_LEN], varint_t v) | ||
{ | ||
u8 *p = buf; | ||
|
||
if (v < 0xfd) { | ||
*(p++) = v; | ||
} else if (v <= 0xffff) { | ||
(*p++) = 0xfd; | ||
(*p++) = v; | ||
(*p++) = v >> 8; | ||
} else if (v <= 0xffffffff) { | ||
(*p++) = 0xfe; | ||
(*p++) = v; | ||
(*p++) = v >> 8; | ||
(*p++) = v >> 16; | ||
(*p++) = v >> 24; | ||
} else { | ||
(*p++) = 0xff; | ||
(*p++) = v; | ||
(*p++) = v >> 8; | ||
(*p++) = v >> 16; | ||
(*p++) = v >> 24; | ||
(*p++) = v >> 32; | ||
(*p++) = v >> 40; | ||
(*p++) = v >> 48; | ||
(*p++) = v >> 56; | ||
} | ||
return p - buf; | ||
} | ||
|
||
size_t varint_get(const u8 *p, size_t max, varint_t *val) | ||
{ | ||
if (max < 1) | ||
return 0; | ||
|
||
switch (*p) { | ||
case 0xfd: | ||
if (max < 3) | ||
return 0; | ||
*val = ((u64)p[1] << 8) + p[0]; | ||
return 3; | ||
case 0xfe: | ||
if (max < 5) | ||
return 0; | ||
*val = ((u64)p[3] << 24) + ((u64)p[2] << 16) | ||
+ ((u64)p[1] << 8) + p[0]; | ||
return 5; | ||
case 0xff: | ||
if (max < 9) | ||
return 0; | ||
*val = ((u64)p[7] << 56) + ((u64)p[6] << 48) | ||
+ ((u64)p[5] << 40) + ((u64)p[4] << 32) | ||
+ ((u64)p[3] << 24) + ((u64)p[2] << 16) | ||
+ ((u64)p[1] << 8) + p[0]; | ||
return 9; | ||
default: | ||
*val = *p; | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef LIGHTNING_BITCOIN_VARINT_H | ||
#define LIGHTNING_BITCOIN_VARINT_H | ||
#include "config.h" | ||
#include <ccan/short_types/short_types.h> | ||
#include <stdlib.h> | ||
|
||
/* We unpack varints for our in-memory representation */ | ||
#define varint_t u64 | ||
|
||
#define VARINT_MAX_LEN 9 | ||
|
||
/* Returns bytes used (up to 9) */ | ||
size_t varint_put(u8 buf[VARINT_MAX_LEN], varint_t v); | ||
|
||
/* Returns bytes used: 0 if max_len too small. */ | ||
size_t varint_get(const u8 *p, size_t max_len, varint_t *val); | ||
#endif /* LIGHTNING_BITCOIN_VARINT_H */ |