summaryrefslogtreecommitdiff
path: root/lib/libalpm
diff options
context:
space:
mode:
authorDiogo Sousa <diogogsousa@gmail.com>2011-12-21 23:31:55 +0000
committerDan McGee <dan@archlinux.org>2011-12-21 18:13:17 -0600
commit7b2f68cc21df469342cb1caa7ec8fb27005cd2dd (patch)
tree9144052c6d4473200b1e663d0131f40352581dd9 /lib/libalpm
parentd6ccd4439044978a6dcc01b8c1ab880b6164f9d1 (diff)
downloadpacman-7b2f68cc21df469342cb1caa7ec8fb27005cd2dd.tar.xz
Created hex_representation() in lib/libalpm/util.c
Used in alpm_compute_md5sum() and alpm_compute_sha256sum(). Signed-off-by: Diogo Sousa <diogogsousa@gmail.com> Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'lib/libalpm')
-rw-r--r--lib/libalpm/util.c60
1 files changed, 27 insertions, 33 deletions
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index b2974a48..2e3f765a 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -915,7 +915,29 @@ static int sha2_file(const char *path, unsigned char output[32], int is224)
}
#endif
-static const char *hex_digits = "0123456789abcdef";
+/** 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(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;
+}
/** Get the md5 sum of file.
* @param filename name of the file
@@ -925,29 +947,15 @@ static const char *hex_digits = "0123456789abcdef";
char SYMEXPORT *alpm_compute_md5sum(const char *filename)
{
unsigned char output[16];
- char *md5sum;
- int ret, i;
ASSERT(filename != NULL, return NULL);
- MALLOC(md5sum, (size_t)33, return NULL);
/* defined above for OpenSSL, otherwise defined in md5.h */
- ret = md5_file(filename, output);
-
- if(ret > 0) {
- free(md5sum);
+ if(md5_file(filename, output) > 0) {
return NULL;
}
- /* Convert the result to something readable */
- for(i = 0; i < 16; i++) {
- int pos = i * 2;
- /* high 4 bits are first digit, low 4 are second */
- md5sum[pos] = hex_digits[output[i] >> 4];
- md5sum[pos + 1] = hex_digits[output[i] & 0x0f];
- }
- md5sum[32] = '\0';
- return md5sum;
+ return hex_representation(output, 16);
}
/** Get the sha256 sum of file.
@@ -958,29 +966,15 @@ char SYMEXPORT *alpm_compute_md5sum(const char *filename)
char SYMEXPORT *alpm_compute_sha256sum(const char *filename)
{
unsigned char output[32];
- char *sha256sum;
- int ret, i;
ASSERT(filename != NULL, return NULL);
- MALLOC(sha256sum, (size_t)65, return NULL);
/* defined above for OpenSSL, otherwise defined in sha2.h */
- ret = sha2_file(filename, output, 0);
-
- if(ret > 0) {
- free(sha256sum);
+ if(sha2_file(filename, output, 0) > 0) {
return NULL;
}
- /* Convert the result to something readable */
- for(i = 0; i < 32; i++) {
- int pos = i * 2;
- /* high 4 bits are first digit, low 4 are second */
- sha256sum[pos] = hex_digits[output[i] >> 4];
- sha256sum[pos + 1] = hex_digits[output[i] & 0x0f];
- }
- sha256sum[64] = '\0';
- return sha256sum;
+ return hex_representation(output, 32);
}
/** Calculates a file's MD5 or SHA2 digest and compares it to an expected value.