Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
- Loading branch information
1 parent
67cc7b9
commit 1708a1f
Showing
15 changed files
with
727 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "hex.h" | ||
#include <ccan/short_types/short_types.h> | ||
#include <stdio.h> | ||
|
||
static bool char_to_hex(u8 *val, char c) | ||
{ | ||
if (c >= '0' && c <= '9') { | ||
*val = c - '0'; | ||
return true; | ||
} | ||
if (c >= 'a' && c <= 'f') { | ||
*val = c - 'a' + 10; | ||
return true; | ||
} | ||
if (c >= 'A' && c <= 'F') { | ||
*val = c - 'A' + 10; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize) | ||
{ | ||
u8 v1, v2; | ||
u8 *p = buf; | ||
|
||
while (slen > 1) { | ||
if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1])) | ||
return false; | ||
if (!bufsize) | ||
return false; | ||
*(p++) = (v1 << 4) | v2; | ||
str += 2; | ||
slen -= 2; | ||
bufsize--; | ||
} | ||
return slen == 0 && bufsize == 0; | ||
} | ||
|
||
char *to_hex(const tal_t *ctx, const void *buf, size_t bufsize) | ||
{ | ||
const u8 *p = buf; | ||
size_t i; | ||
char *hex = tal_arr(ctx, char, bufsize * 2 + 1); | ||
|
||
for (i = 0; i < bufsize; i++) | ||
sprintf(hex + i*2, "%02x", p[i]); | ||
|
||
return hex; | ||
} |
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,13 @@ | ||
#ifndef PETTYCOIN_HEX_H | ||
#define PETTYCOIN_HEX_H | ||
#include "config.h" | ||
#include <ccan/tal/tal.h> | ||
#include <stdbool.h> | ||
|
||
/* Unpack slen hex digits into buf; fail on bad char or not exact length */ | ||
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize); | ||
|
||
/* Allocate hex string off ctx */ | ||
char *to_hex(const tal_t *ctx, const void *buf, size_t bufsize); | ||
|
||
#endif /* PETTYCOIN_HEX_H */ |
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
Oops, something went wrong.