-
Notifications
You must be signed in to change notification settings - Fork 91
/
utils.c
45 lines (41 loc) · 949 Bytes
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "utils.h"
#include <stdlib.h>
#include <string.h>
int append_string(char* buf, const char* string)
{
int len = (int)strlen(string);
if (buf == NULL)
{
return len;
}
strncpy(buf, string, len + 1);
return len;
}
void bytes_swap(void* bytes, size_t len)
{
unsigned char *start, *end;
for (start = (unsigned char*)bytes, end = start + len - 1; start < end; ++start, --end)
{
unsigned char swap = *start;
*start = *end;
*end = swap;
}
}
char* bytes_to_hexstring(uint8_t* bytes, size_t len)
{
const char* hexdigs = "0123456789abcdef";
size_t k = len * 2 + 1;
char* out = malloc(k);
for (int i = 0; i < len; i++)
{
out[i * 2] = hexdigs[bytes[i] >> 4];
out[i * 2 + 1] = hexdigs[bytes[i] & 0x0f];
}
out[k - 1] = '\0';
return out;
}