Move hex_representation() to src/common
We'll reuse the function in pacman with a later commit. Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
parent
831fc568fc
commit
ccdd1e3fd9
3 changed files with 27 additions and 24 deletions
|
@ -1014,30 +1014,6 @@ static int sha256_file(const char *path, unsigned char output[32])
|
||||||
}
|
}
|
||||||
#endif /* HAVE_LIBSSL || HAVE_LIBNETTLE */
|
#endif /* HAVE_LIBSSL || HAVE_LIBNETTLE */
|
||||||
|
|
||||||
/** Create a string representing bytes in hexadecimal.
|
|
||||||
* @param bytes the bytes to represent in hexadecimal
|
|
||||||
* @param size number of bytes to consider
|
|
||||||
* @return a NULL terminated string with the hexadecimal representation of
|
|
||||||
* bytes or NULL on error. This string must be freed.
|
|
||||||
*/
|
|
||||||
static char *hex_representation(const unsigned char *bytes, size_t size)
|
|
||||||
{
|
|
||||||
static const char *hex_digits = "0123456789abcdef";
|
|
||||||
char *str;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
MALLOC(str, 2 * size + 1, return NULL);
|
|
||||||
|
|
||||||
for(i = 0; i < size; i++) {
|
|
||||||
str[2 * i] = hex_digits[bytes[i] >> 4];
|
|
||||||
str[2 * i + 1] = hex_digits[bytes[i] & 0x0f];
|
|
||||||
}
|
|
||||||
|
|
||||||
str[2 * size] = '\0';
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
char SYMEXPORT *alpm_compute_md5sum(const char *filename)
|
char SYMEXPORT *alpm_compute_md5sum(const char *filename)
|
||||||
{
|
{
|
||||||
unsigned char output[16];
|
unsigned char output[16];
|
||||||
|
|
|
@ -25,6 +25,32 @@
|
||||||
#include "util-common.h"
|
#include "util-common.h"
|
||||||
|
|
||||||
|
|
||||||
|
/** Create a string representing bytes in hexadecimal.
|
||||||
|
* @param bytes the bytes to represent in hexadecimal
|
||||||
|
* @param size number of bytes to consider
|
||||||
|
* @return a NULL terminated string with the hexadecimal representation of
|
||||||
|
* bytes or NULL on error. This string must be freed.
|
||||||
|
*/
|
||||||
|
char *hex_representation(const unsigned char *bytes, size_t size)
|
||||||
|
{
|
||||||
|
static const char *hex_digits = "0123456789abcdef";
|
||||||
|
char *str = malloc(2 * size + 1);
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if(!str) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0; i < size; i++) {
|
||||||
|
str[2 * i] = hex_digits[bytes[i] >> 4];
|
||||||
|
str[2 * i + 1] = hex_digits[bytes[i] & 0x0f];
|
||||||
|
}
|
||||||
|
|
||||||
|
str[2 * size] = '\0';
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
/** Parse the basename of a program from a path.
|
/** Parse the basename of a program from a path.
|
||||||
* @param path path to parse basename from
|
* @param path path to parse basename from
|
||||||
*
|
*
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/stat.h> /* struct stat */
|
#include <sys/stat.h> /* struct stat */
|
||||||
|
|
||||||
|
char *hex_representation(const unsigned char *bytes, size_t size);
|
||||||
const char *mbasename(const char *path);
|
const char *mbasename(const char *path);
|
||||||
char *mdirname(const char *path);
|
char *mdirname(const char *path);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue