From 7e4118a6e2f0f07bcd818658c8691198e918eb0f Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Thu, 24 Feb 2011 00:21:40 +0100
Subject: [PATCH] Added xbps_get_file_hash_from_dict(), returns the sha256
 string obj in a dictionary.

---
 include/xbps_api.h | 18 +++++++++++++++++-
 lib/util.c         | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/include/xbps_api.h b/include/xbps_api.h
index ab085496..0f4f36ee 100644
--- a/include/xbps_api.h
+++ b/include/xbps_api.h
@@ -53,7 +53,7 @@
  * @def XBPS_RELVER
  * Current library release date.
  */
-#define XBPS_RELVER		"20110222"
+#define XBPS_RELVER		"20110224"
 
 /** 
  * @def XBPS_META_PATH
@@ -1253,6 +1253,22 @@ char *xbps_xasprintf(const char *fmt, ...);
  */
 char *xbps_get_file_hash(const char *file);
 
+/**
+ * Returns a string with the sha256 hash for the file specified
+ * by \a file in an array with key \a key in the proplib dictionary
+ * \a d.
+ *
+ * @param[in] d Proplib dictionary to look in.
+ * @param[in] key Array key to match in dictionary.
+ * @param[in] file Pathname to a file.
+ *
+ * @return The sha256 hash string if found, NULL otherwise
+ * and errno is set appropiately.
+ */
+const char *xbps_get_file_hash_from_dict(prop_dictionary_t d,
+					 const char *key,
+					 const char *file);
+
 /**
  * Compares the sha256 hash of the file \a file with the sha256
  * string specified by \a sha256.
diff --git a/lib/util.c b/lib/util.c
index 59cba6e4..10c227b5 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -145,6 +145,41 @@ xbps_check_file_hash(const char *file, const char *sha256)
 	return 0;
 }
 
+const char *
+xbps_get_file_hash_from_dict(prop_dictionary_t d,
+			     const char *key,
+			     const char *file)
+{
+	prop_object_t obj;
+	prop_object_iterator_t iter;
+	const char *curfile, *sha256;
+
+	assert(d != NULL);
+	assert(key != NULL);
+	assert(file != NULL);
+
+	curfile = sha256 = NULL;
+
+	iter = xbps_get_array_iter_from_dict(d, key);
+	if (iter == NULL)
+		return NULL;
+	while ((obj = prop_object_iterator_next(iter)) != NULL) {
+		prop_dictionary_get_cstring_nocopy(obj,
+		    "file", &curfile);
+		if (strstr(file, curfile) == NULL)
+			continue;
+		/* file matched */
+		prop_dictionary_get_cstring_nocopy(obj,
+		    "sha256", &sha256);
+		break;
+	}
+	prop_object_iterator_release(iter);
+	if (sha256 == NULL)
+		errno = ENOENT;
+
+	return sha256;
+}
+
 bool
 xbps_check_is_repository_uri_remote(const char *uri)
 {