# HG changeset patch
# Node ID 094418b63495
# Parent  a72067fee822
diff --git a/libpurple/protocols/gg/meson.build b/libpurple/protocols/gg/meson.build
--- a/libpurple/protocols/gg/meson.build
+++ b/libpurple/protocols/gg/meson.build
@@ -45,8 +45,6 @@
 	'xml.h',
 	'oauth/oauth.c',
 	'oauth/oauth.h',
-	'oauth/oauth-parameter.c',
-	'oauth/oauth-parameter.h',
 	'oauth/oauth-purple.c',
 	'oauth/oauth-purple.h'
 ]
diff --git a/libpurple/protocols/gg/oauth/oauth-parameter.h b/libpurple/protocols/gg/oauth/oauth-parameter.h
deleted file mode 100644
--- a/libpurple/protocols/gg/oauth/oauth-parameter.h
+++ /dev/null
@@ -1,35 +0,0 @@
-/* purple
- *
- * Purple is the legal property of its developers, whose names are too numerous
- * to list here.  Please refer to the COPYRIGHT file distributed with this
- * source distribution.
- *
- * Code adapted from libgadu (C) 2008 Wojtek Kaniewski <wojtekka@irc.pl>
- * (http://toxygen.net/libgadu/) during Google Summer of Code 2012
- * by Tomek Wasilczyk (http://www.wasilczyk.pl).
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
- */
-
-#ifndef _GGP_OAUTH_PARAMETER_H
-#define _GGP_OAUTH_PARAMETER_H
-
-typedef struct gg_oauth_parameter gg_oauth_parameter_t;
-
-int gg_oauth_parameter_set(gg_oauth_parameter_t **list, const char *key, const char *value);
-char *gg_oauth_parameter_join(gg_oauth_parameter_t *list, int header);
-void gg_oauth_parameter_free(gg_oauth_parameter_t *list);
-
-#endif /* _GGP_OAUTH_PARAMETER_H */
diff --git a/libpurple/protocols/gg/oauth/oauth-parameter.c b/libpurple/protocols/gg/oauth/oauth-parameter.c
deleted file mode 100644
--- a/libpurple/protocols/gg/oauth/oauth-parameter.c
+++ /dev/null
@@ -1,161 +0,0 @@
-/* purple
- *
- * Purple is the legal property of its developers, whose names are too numerous
- * to list here.  Please refer to the COPYRIGHT file distributed with this
- * source distribution.
- *
- * Code adapted from libgadu (C) 2008 Wojtek Kaniewski <wojtekka@irc.pl>
- * (http://toxygen.net/libgadu/) during Google Summer of Code 2012
- * by Tomek Wasilczyk (http://www.wasilczyk.pl).
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
- */
-
-#include "oauth-parameter.h"
-
-#include <glib.h>
-
-struct gg_oauth_parameter {
-	char *key;
-	char *value;
-	struct gg_oauth_parameter *next;
-};
-
-int gg_oauth_parameter_set(gg_oauth_parameter_t **list, const char *key, const char *value)
-{
-	gg_oauth_parameter_t *p, *new_p;
-	char *new_key;
-	char *new_value;
-
-	if (value == NULL)
-		return 0;
-
-	if (list == NULL)
-		return -1;
-
-	new_key = g_strdup(key);
-
-	if (new_key == NULL)
-		return -1;
-
-	new_value = g_strdup(value);
-
-	if (new_value == NULL) {
-		g_free(new_key);
-		return -1;
-	}
-
-	new_p = g_new0(gg_oauth_parameter_t, 1);
-
-	if (new_p == NULL) {
-		g_free(new_key);
-		g_free(new_value);
-		return -1;
-	}
-
-	memset(new_p, 0, sizeof(gg_oauth_parameter_t));
-	new_p->key = new_key;
-	new_p->value = new_value;
-
-	if (*list != NULL) {
-		p = *list;
-
-		while (p != NULL && p->next != NULL)
-			p = p->next;
-
-		p->next = new_p;
-	} else {
-		*list = new_p;
-	}
-
-	return 0;
-}
-
-char *gg_oauth_parameter_join(gg_oauth_parameter_t *list, int header)
-{
-	gg_oauth_parameter_t *p;
-	int len = 0;
-	char *res, *out;
-
-	if (header)
-		len += strlen("OAuth ");
-
-	for (p = list; p; p = p->next) {
-		gchar *escaped;
-		len += strlen(p->key);
-
-		len += (header) ? 3 : 1;
-
-		escaped = g_uri_escape_string(p->value, NULL, FALSE);
-		len += strlen(escaped);
-		g_free(escaped);
-
-		if (p->next)
-			len += 1;
-	}
-
-	res = g_malloc(len + 1);
-
-	if (res == NULL)
-		return NULL;
-
-	out = res;
-
-	*out = 0;
-
-	if (header) {
-		strcpy(out, "OAuth ");
-		out += strlen(out);
-	}
-
-	for (p = list; p; p = p->next) {
-		gchar *escaped;
-		strcpy(out, p->key);
-		out += strlen(p->key);
-
-		strcpy(out++, "=");
-
-		if (header)
-			strcpy(out++, "\"");
-
-		escaped = g_uri_escape_string(p->value, NULL, FALSE);
-		strcpy(out, escaped);
-		out += strlen(escaped);
-		g_free(escaped);
-
-		if (header)
-			strcpy(out++, "\"");
-
-		if (p->next != NULL)
-			strcpy(out++, (header) ? "," : "&");
-	}
-
-	return res;
-}
-
-void gg_oauth_parameter_free(gg_oauth_parameter_t *list)
-{
-	while (list != NULL) {
-		gg_oauth_parameter_t *next;
-
-		next = list->next;
-
-		g_free(list->key);
-		g_free(list->value);
-		g_free(list);
-
-		list = next;
-	}
-}
diff --git a/libpurple/protocols/gg/oauth/oauth.c b/libpurple/protocols/gg/oauth/oauth.c
--- a/libpurple/protocols/gg/oauth/oauth.c
+++ b/libpurple/protocols/gg/oauth/oauth.c
@@ -25,8 +25,6 @@
 
 #include "oauth.h"
 
-#include "oauth-parameter.h"
-
 #include <time.h>
 
 #include <glib.h>
@@ -34,6 +32,8 @@
 char *gg_oauth_static_nonce;		/* dla unit testów */
 char *gg_oauth_static_timestamp;	/* dla unit testów */
 
+static char *gg_oauth_generate_request(gboolean header, ...) G_GNUC_NULL_TERMINATED;
+
 static void gg_oauth_generate_nonce(char *buf, int len)
 {
 	const char charset[] = "0123456789";
@@ -49,6 +49,46 @@
 	*buf = 0;
 }
 
+/* Returns a comma separated header value if header is true,
+ * or a url-encoded request otherwise
+ */
+static char *
+gg_oauth_generate_request(gboolean header, ...)
+{
+	GString *res;
+	va_list params;
+	const gchar *key;
+	const gchar *quote;
+	gchar separator;
+
+	if (header) {
+		res = g_string_new("OAuth ");
+		quote = "\"";
+		separator = ',';
+	} else {
+		res = g_string_new(NULL);
+		quote = "";
+		separator = '&';
+	}
+
+	va_start(params, header);
+	while ((key = va_arg(params, const gchar *))) {
+		const gchar *value = va_arg(params, const gchar *);
+		gchar *escaped = g_uri_escape_string(value, NULL, FALSE);
+
+		g_string_append_printf(res, "%s=%s%s%s%c",
+		                       key, quote, escaped, quote, separator);
+		g_free(escaped);
+	}
+	va_end(params);
+
+	if (res->len > 0 && res->str[res->len - 1] == separator) {
+		/* remove trailing separator */
+		res = g_string_truncate(res, res->len - 1);
+	}
+	return g_string_free(res, FALSE);
+}
+
 static gchar *gg_hmac_sha1(const char *key, const char *message)
 {
 	GHmac *hmac;
@@ -99,7 +139,6 @@
 {
 	char *request, *signature, *res;
 	char nonce[80], timestamp[16];
-	gg_oauth_parameter_t *params = NULL;
 
 	if (gg_oauth_static_nonce == NULL)
 		gg_oauth_generate_nonce(nonce, sizeof(nonce));
@@ -115,38 +154,32 @@
 		timestamp[sizeof(timestamp) - 1] = 0;
 	}
 
-	gg_oauth_parameter_set(&params, "oauth_consumer_key", consumer_key);
-	gg_oauth_parameter_set(&params, "oauth_nonce", nonce);
-	gg_oauth_parameter_set(&params, "oauth_signature_method", "HMAC-SHA1");
-	gg_oauth_parameter_set(&params, "oauth_timestamp", timestamp);
-	gg_oauth_parameter_set(&params, "oauth_token", token);
-	gg_oauth_parameter_set(&params, "oauth_version", "1.0");
-
-	request = gg_oauth_parameter_join(params, 0);
+	request = gg_oauth_generate_request(FALSE,
+	                                    "oauth_consumer_key", consumer_key,
+	                                    "oauth_nonce", nonce,
+	                                    "oauth_signature_method", "HMAC-SHA1",
+	                                    "oauth_timestamp", timestamp,
+	                                    "oauth_token", token,
+	                                    "oauth_version", "1.0",
+	                                    NULL);
 
 	signature = gg_oauth_generate_signature(method, url, request, consumer_secret, token_secret);
 
 	g_free(request);
 
-	gg_oauth_parameter_free(params);
-	params = NULL;
-
 	if (signature == NULL)
 		return NULL;
 
-	gg_oauth_parameter_set(&params, "oauth_version", "1.0");
-	gg_oauth_parameter_set(&params, "oauth_nonce", nonce);
-	gg_oauth_parameter_set(&params, "oauth_timestamp", timestamp);
-	gg_oauth_parameter_set(&params, "oauth_consumer_key", consumer_key);
-	gg_oauth_parameter_set(&params, "oauth_token", token);
-	gg_oauth_parameter_set(&params, "oauth_signature_method", "HMAC-SHA1");
-	gg_oauth_parameter_set(&params, "oauth_signature", signature);
-
+	res = gg_oauth_generate_request(TRUE,
+	                                "oauth_version", "1.0",
+	                                "oauth_nonce", nonce,
+	                                "oauth_timestamp", timestamp,
+	                                "oauth_consumer_key", consumer_key,
+	                                "oauth_token", token,
+	                                "oauth_signature_method", "HMAC-SHA1",
+	                                "oauth_signature", signature,
+	                                NULL);
 	g_free(signature);
 
-	res = gg_oauth_parameter_join(params, 1);
-
-	gg_oauth_parameter_free(params);
-
 	return res;
 }
