2020-09-12 21:30:13 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-09-10 21:33:37 -05:00
|
|
|
#include "purr.h"
|
|
|
|
|
2020-09-12 21:30:13 -05:00
|
|
|
/*
|
|
|
|
* Set print to true to print value, otherwise returns dynamic string with the number
|
|
|
|
*/
|
|
|
|
char *print_hex(uint8_t *buf, int len, bool print)
|
2020-09-10 21:33:37 -05:00
|
|
|
{
|
2020-09-12 21:30:13 -05:00
|
|
|
char *rv = NULL;
|
|
|
|
|
|
|
|
if (print) {
|
|
|
|
// just print number into stdin
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
printf("%02x", buf[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// print number into new string
|
|
|
|
// new string has to be big enough to fit the whole number
|
|
|
|
// 2 chars for each byte + 1 byte for NULL
|
|
|
|
rv = malloc(len * 2 + 1);
|
|
|
|
if (rv == NULL) {
|
|
|
|
perror("allocation failure");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *pos = rv;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
// will add the null terminator in the last run
|
|
|
|
sprintf(pos, "%02x", buf[i]);
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2020-09-10 21:33:37 -05:00
|
|
|
}
|
2020-09-12 21:30:13 -05:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// buffer to base64
|
|
|
|
// base64 to buffer
|