summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Drake <tlsa@netsurf-browser.org>2017-01-19 23:33:53 +0000
committerMichael Drake <tlsa@netsurf-browser.org>2017-02-08 17:27:13 +0000
commit2acd90e28a37a80f20e2780be7972764c699d9a0 (patch)
tree3b573bfb3d2a1586112e8ec7239bb0e5ca1c2be9
parent95e5ede775fc842324b87d797ca00c7576b9a359 (diff)
downloadnetsurf-2acd90e28a37a80f20e2780be7972764c699d9a0.tar.gz
netsurf-2acd90e28a37a80f20e2780be7972764c699d9a0.tar.bz2
nsurl: Consolidate conversion to string.
-rw-r--r--utils/nsurl/nsurl.c22
-rw-r--r--utils/nsurl/parse.c95
-rw-r--r--utils/nsurl/private.h32
3 files changed, 76 insertions, 73 deletions
diff --git a/utils/nsurl/nsurl.c b/utils/nsurl/nsurl.c
index b60eb07ca..79dc71663 100644
--- a/utils/nsurl/nsurl.c
+++ b/utils/nsurl/nsurl.c
@@ -248,29 +248,9 @@ bool nsurl_compare(const nsurl *url1, const nsurl *url2, nsurl_component parts)
nserror nsurl_get(const nsurl *url, nsurl_component parts,
char **url_s, size_t *url_l)
{
- struct nsurl_component_lengths str_len = { 0, 0, 0, 0, 0, 0, 0, 0 };
- enum nsurl_string_flags str_flags = 0;
-
assert(url != NULL);
- /* Get the string length and find which parts of url need copied */
- nsurl__get_string_data(&(url->components), parts, url_l,
- &str_len, &str_flags);
-
- if (*url_l == 0) {
- return NSERROR_BAD_URL;
- }
-
- /* Allocate memory for url string */
- *url_s = malloc(*url_l + 1); /* adding 1 for '\0' */
- if (*url_s == NULL) {
- return NSERROR_NOMEM;
- }
-
- /* Copy the required parts into the url string */
- nsurl__get_string(&(url->components), *url_s, &str_len, str_flags);
-
- return NSERROR_OK;
+ return nsurl__string(&(url->components), parts, 0, url_s, url_l);
}
diff --git a/utils/nsurl/parse.c b/utils/nsurl/parse.c
index 0cdbbd4c3..e242689e1 100644
--- a/utils/nsurl/parse.c
+++ b/utils/nsurl/parse.c
@@ -997,8 +997,16 @@ static nserror nsurl__create_from_section(const char * const url_s,
}
-/* Exported function, documented in utils/nsurl/private.h */
-void nsurl__get_string_data(const struct nsurl_components *url,
+/**
+ * Get nsurl string info; total length, component lengths, & components present
+ *
+ * \param url NetSurf URL components
+ * \param parts Which parts of the URL are required in the string
+ * \param url_l Updated to total string length
+ * \param lengths Updated with individual component lengths
+ * \param pflags Updated to contain relevant string flags
+ */
+static void nsurl__get_string_data(const struct nsurl_components *url,
nsurl_component parts, size_t *url_l,
struct nsurl_component_lengths *lengths,
enum nsurl_string_flags *pflags)
@@ -1095,8 +1103,15 @@ void nsurl__get_string_data(const struct nsurl_components *url,
}
-/* Exported function, documented in utils/nsurl/private.h */
-void nsurl__get_string(const struct nsurl_components *url, char *url_s,
+/**
+ * Copy url string into provided buffer
+ *
+ * \param url NetSurf URL components
+ * \param url_s Updated to contain the string
+ * \param l Individual component lengths
+ * \param flags String flags
+ */
+static void nsurl__get_string(const struct nsurl_components *url, char *url_s,
struct nsurl_component_lengths *l,
enum nsurl_string_flags flags)
{
@@ -1166,6 +1181,43 @@ void nsurl__get_string(const struct nsurl_components *url, char *url_s,
}
+/* exported interface, documented in nsurl.h */
+nserror nsurl__string(
+ const struct nsurl_components *components,
+ nsurl_component parts, size_t pre_padding,
+ char **url_s_out, size_t *url_l_out)
+{
+ struct nsurl_component_lengths str_len = { 0, 0, 0, 0, 0, 0, 0, 0 };
+ enum nsurl_string_flags str_flags = 0;
+ size_t url_l;
+ char *url_s;
+
+ assert(components != NULL);
+
+ /* Get the string length and find which parts of url need copied */
+ nsurl__get_string_data(components, parts, &url_l,
+ &str_len, &str_flags);
+
+ if (url_l == 0) {
+ return NSERROR_BAD_URL;
+ }
+
+ /* Allocate memory for url string */
+ url_s = malloc(pre_padding + url_l + 1); /* adding 1 for '\0' */
+ if (url_s == NULL) {
+ return NSERROR_NOMEM;
+ }
+
+ /* Copy the required parts into the url string */
+ nsurl__get_string(components, url_s + pre_padding, &str_len, str_flags);
+
+ *url_s_out = url_s;
+ *url_l_out = url_l;
+
+ return NSERROR_OK;
+}
+
+
/**
* Calculate hash value
*
@@ -1278,8 +1330,6 @@ nserror nsurl_create(const char * const url_s, nsurl **url)
struct nsurl_components c;
size_t length;
char *buff;
- struct nsurl_component_lengths str_len = { 0, 0, 0, 0, 0, 0, 0, 0 };
- enum nsurl_string_flags str_flags = 0;
nserror e = NSERROR_OK;
bool match;
@@ -1327,23 +1377,15 @@ nserror nsurl_create(const char * const url_s, nsurl **url)
}
}
- /* Get the string length and find which parts of url are present */
- nsurl__get_string_data(&c, NSURL_WITH_FRAGMENT, &length,
- &str_len, &str_flags);
-
- /* Create NetSurf URL object */
- *url = malloc(sizeof(nsurl) + length + 1); /* Add 1 for \0 */
- if (*url == NULL) {
- nsurl_destroy_components(&c);
- return NSERROR_NOMEM;
+ e = nsurl__string(&c, NSURL_WITH_FRAGMENT,
+ sizeof(nsurl), (char **)url, &length);
+ if (e != NSERROR_OK) {
+ return e;
}
(*url)->components = c;
(*url)->length = length;
- /* Fill out the url string */
- nsurl__get_string(&c, (*url)->string, &str_len, str_flags);
-
/* Get the nsurl's hash */
nsurl__calc_hash(*url);
@@ -1363,8 +1405,6 @@ nserror nsurl_join(const nsurl *base, const char *rel, nsurl **joined)
char *buff;
char *buff_pos;
char *buff_start;
- struct nsurl_component_lengths str_len = { 0, 0, 0, 0, 0, 0, 0, 0 };
- enum nsurl_string_flags str_flags = 0;
nserror error = 0;
enum {
NSURL_F_REL = 0,
@@ -1575,22 +1615,15 @@ nserror nsurl_join(const nsurl *base, const char *rel, nsurl **joined)
return error;
}
- /* Get the string length and find which parts of url are present */
- nsurl__get_string_data(&c, NSURL_WITH_FRAGMENT, &length,
- &str_len, &str_flags);
-
- /* Create NetSurf URL object */
- *joined = malloc(sizeof(nsurl) + length + 1); /* Add 1 for \0 */
- if (*joined == NULL) {
- return NSERROR_NOMEM;
+ error = nsurl__string(&c, NSURL_WITH_FRAGMENT,
+ sizeof(nsurl), (char **)joined, &length);
+ if (error != NSERROR_OK) {
+ return error;
}
(*joined)->components = c;
(*joined)->length = length;
- /* Fill out the url string */
- nsurl__get_string(&c, (*joined)->string, &str_len, str_flags);
-
/* Get the nsurl's hash */
nsurl__calc_hash(*joined);
diff --git a/utils/nsurl/private.h b/utils/nsurl/private.h
index f8ba51f67..bc3eb8c7e 100644
--- a/utils/nsurl/private.h
+++ b/utils/nsurl/private.h
@@ -107,31 +107,21 @@ enum nsurl_string_flags {
NSURL_F_FRAGMENT = (1 << 11)
};
-/**
- * Get nsurl string info; total length, component lengths, & components present
- *
- * \param url NetSurf URL components
- * \param url_s Updated to contain the string
- * \param l Individual component lengths
- * \param flags String flags
- */
-void nsurl__get_string(const struct nsurl_components *url, char *url_s,
- struct nsurl_component_lengths *l,
- enum nsurl_string_flags flags);
/**
- * Get nsurl string info; total length, component lengths, & components present
+ * Convert a set of nsurl components to a single string
*
- * \param url NetSurf URL components
- * \param parts Which parts of the URL are required in the string
- * \param url_l Updated to total string length
- * \param lengths Updated with individual component lengths
- * \param pflags Updated to contain relevant string flags
+ * \param[in] components The URL components to stitch together.
+ * \param[in] parts The set of parts wanted in the string.
+ * \param[in] pre_padding Amount in bytes to pad the start of the string by.
+ * \param[out] url_s_out Returns allocated URL string.
+ * \param[out] url_l_out Returns byte length of string, excluding pre_padding.
+ * \return NSERROR_OK on success, appropriate error otherwise.
*/
-void nsurl__get_string_data(const struct nsurl_components *url,
- nsurl_component parts, size_t *url_l,
- struct nsurl_component_lengths *lengths,
- enum nsurl_string_flags *pflags);
+nserror nsurl__string(
+ const struct nsurl_components *components,
+ nsurl_component parts, size_t pre_padding,
+ char **url_s_out, size_t *url_l_out);
/**
* Calculate hash value