mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
Functions for encoding reversed hex
This commit is contained in:
committed by
Rusty Russell
parent
bcd49b063b
commit
19116b6561
@@ -64,3 +64,21 @@ bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize)
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool hex_encode_reversed(const void *buf, size_t bufsize, char *dest, size_t destsize)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (destsize < hex_str_size(bufsize))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Start at end of data, go to index 0.
|
||||||
|
for (i = bufsize; i--;) {
|
||||||
|
unsigned int c = ((const unsigned char *)buf)[i];
|
||||||
|
*(dest++) = hexchar(c >> 4);
|
||||||
|
*(dest++) = hexchar(c & 0xF);
|
||||||
|
}
|
||||||
|
*dest = '\0';
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|||||||
@@ -41,6 +41,24 @@ bool hex_decode(const char *str, size_t slen, void *buf, size_t bufsize);
|
|||||||
*/
|
*/
|
||||||
bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize);
|
bool hex_encode(const void *buf, size_t bufsize, char *dest, size_t destsize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* hex_encode_reversed - Create a nul-terminated reversed hex string
|
||||||
|
* @buf: the buffer to read the data from
|
||||||
|
* @bufsize: the length of @buf
|
||||||
|
* @dest: the string to fill
|
||||||
|
* @destsize: the max size of the string
|
||||||
|
*
|
||||||
|
* Returns true if the string, including terminator, fit in @destsize;
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* unsigned char buf[] = { 0x1F, 0x2F };
|
||||||
|
* char str[5];
|
||||||
|
*
|
||||||
|
* if (!hex_encode(buf, sizeof(buf), str, sizeof(str)))
|
||||||
|
* abort();
|
||||||
|
*/
|
||||||
|
bool hex_encode_reversed(const void *buf, size_t bufsize, char *dest, size_t destsize);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* hex_str_size - Calculate how big a nul-terminated hex string is
|
* hex_str_size - Calculate how big a nul-terminated hex string is
|
||||||
* @bytes: bytes of data to represent
|
* @bytes: bytes of data to represent
|
||||||
|
|||||||
@@ -471,6 +471,15 @@ void json_add_hex(struct json_result *result, const char *fieldname,
|
|||||||
json_add_string(result, fieldname, hex);
|
json_add_string(result, fieldname, hex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void json_add_hex_reversed(struct json_result *result, const char *fieldname,
|
||||||
|
const void *data, size_t len)
|
||||||
|
{
|
||||||
|
char hex[hex_str_size(len)];
|
||||||
|
|
||||||
|
hex_encode_reversed(data, len, hex, sizeof(hex));
|
||||||
|
json_add_string(result, fieldname, hex);
|
||||||
|
}
|
||||||
|
|
||||||
void json_add_object(struct json_result *result, ...)
|
void json_add_object(struct json_result *result, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|||||||
@@ -102,6 +102,9 @@ void json_add_null(struct json_result *result, const char *fieldname);
|
|||||||
/* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */
|
/* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */
|
||||||
void json_add_hex(struct json_result *result, const char *fieldname,
|
void json_add_hex(struct json_result *result, const char *fieldname,
|
||||||
const void *data, size_t len);
|
const void *data, size_t len);
|
||||||
|
/* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */
|
||||||
|
void json_add_hex_reversed(struct json_result *result, const char *fieldname,
|
||||||
|
const void *data, size_t len);
|
||||||
void json_add_object(struct json_result *result, ...);
|
void json_add_object(struct json_result *result, ...);
|
||||||
|
|
||||||
const char *json_result_string(const struct json_result *result);
|
const char *json_result_string(const struct json_result *result);
|
||||||
|
|||||||
Reference in New Issue
Block a user