mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
lws_hex_from_byte_array
This commit is contained in:
parent
f576f317aa
commit
ae0b52c0df
2 changed files with 29 additions and 0 deletions
|
@ -309,6 +309,19 @@ lws_json_simple_strcmp(const char *buf, size_t len, const char *name, const char
|
|||
LWS_VISIBLE LWS_EXTERN int
|
||||
lws_hex_to_byte_array(const char *h, uint8_t *dest, int max);
|
||||
|
||||
/**
|
||||
* lws_hex_from_byte_array(): render byte array as hex char string
|
||||
*
|
||||
* \param src: incoming binary source array
|
||||
* \param slen: length of src in bytes
|
||||
* \param dest: array to fill with hex chars representing src
|
||||
* \param len: max extent of dest
|
||||
*
|
||||
* This converts binary data of length slen at src, into a hex string at dest
|
||||
* of maximum length len. Even if truncated, the result will be NUL-terminated.
|
||||
*/
|
||||
LWS_VISIBLE LWS_EXTERN void
|
||||
lws_hex_from_byte_array(const uint8_t *src, size_t slen, char *dest, size_t len);
|
||||
|
||||
/**
|
||||
* lws_hex_random(): generate len - 1 or - 2 characters of random ascii hex
|
||||
|
|
|
@ -155,6 +155,22 @@ lws_hex_to_byte_array(const char *h, uint8_t *dest, int max)
|
|||
|
||||
static char *hexch = "0123456789abcdef";
|
||||
|
||||
void
|
||||
lws_hex_from_byte_array(const uint8_t *src, size_t slen, char *dest, size_t len)
|
||||
{
|
||||
char *end = &dest[len - 1];
|
||||
|
||||
while (slen-- && dest != end) {
|
||||
uint8_t b = *src++;
|
||||
*dest++ = hexch[b >> 4];
|
||||
if (dest == end)
|
||||
break;
|
||||
*dest++ = hexch[b & 0xf];
|
||||
}
|
||||
|
||||
*dest = '\0';
|
||||
}
|
||||
|
||||
int
|
||||
lws_hex_random(struct lws_context *context, char *dest, size_t len)
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue