[vlc-devel] [PATCH] Fix patch 1.

Samuel Pitoiset samuel.pitoiset at gmail.com
Fri Mar 9 18:50:06 CET 2012


---
 modules/misc/audioscrobbler.c |   79 ++++++++++++++---------------------------
 1 file changed, 26 insertions(+), 53 deletions(-)

diff --git a/modules/misc/audioscrobbler.c b/modules/misc/audioscrobbler.c
index e9fac07..47a2d50 100644
--- a/modules/misc/audioscrobbler.c
+++ b/modules/misc/audioscrobbler.c
@@ -480,8 +480,8 @@ static void Close(vlc_object_t *p_this)
  *****************************************************************************/
 static char *BuildAuthToken(const char *psz_username, const char *psz_password)
 {
-    char *psz_password_md5, *psz_auth_token_md5;
     struct md5_s p_struct_md5;
+    char *psz_password_md5;
 
     /* generate a md5 hash of the password */
     InitMD5(&p_struct_md5);
@@ -503,11 +503,7 @@ static char *BuildAuthToken(const char *psz_username, const char *psz_password)
 
     free(psz_password_md5);
 
-    psz_auth_token_md5 = psz_md5_hash(&p_struct_md5);
-    if (!psz_auth_token_md5)
-        return NULL;
-
-    return psz_auth_token_md5;
+    return psz_md5_hash(&p_struct_md5);
 }
 
 /*****************************************************************************
@@ -515,9 +511,10 @@ static char *BuildAuthToken(const char *psz_username, const char *psz_password)
  *****************************************************************************/
 static int Sort(const void *elt1, const void *elt2)
 {
-    const audioscrobbler_param_t *p1 = elt1, *p2 = elt2;
+    const audioscrobbler_param_t *const *p1 = elt1;
+    const audioscrobbler_param_t *const *p2 = elt2;
 
-    return strcmp(*(char * const *) p1->key, *(char * const *) p2->key);
+    return strcmp((*p1)->key, (*p2)->key);
 }
 
 /*****************************************************************************
@@ -525,55 +522,29 @@ static int Sort(const void *elt1, const void *elt2)
  *****************************************************************************/
 static char *BuildApiSig(vlc_array_t *p_params, const char *psz_api_secret)
 {
-    char *psz_api_sig, *psz_api_sig_md5;
     audioscrobbler_param_t *p_param;
     struct md5_s p_struct_md5;
-    ssize_t i_len = 0;
-    int i;
 
     /* parameters must be sorted alphabetically */
     qsort(p_params->pp_elems, p_params->i_count, sizeof(audioscrobbler_param_t*),
             Sort);
 
-    /* compute the length of the api signature */
-    for (i = 0; i < p_params->i_count; i++)
-    {
-        p_param = p_params->pp_elems[i];
-        i_len += strlen(p_param->key) + strlen(p_param->val);
-    }
-
-    i_len += 32; /* md5 hash of the api secret */
-
-    psz_api_sig = malloc(i_len);
-    if (!psz_api_sig)
-        return NULL;
+    /* generate a md5 hash of the api signature */
+    InitMD5(&p_struct_md5);
 
     /* build the api signature */
-    p_param = p_params->pp_elems[0];
-    sprintf(psz_api_sig, "%s%s", p_param->key, p_param->val);
-
-    for (i = 1; i < p_params->i_count; i++)
+    for (int i = 0; i < p_params->i_count; i++)
     {
         p_param = p_params->pp_elems[i];
-        strcat(psz_api_sig, p_param->key);
-        strcat(psz_api_sig, p_param->val);
+        AddMD5(&p_struct_md5, (uint8_t*) p_param->key, strlen(p_param->key));
+        AddMD5(&p_struct_md5, (uint8_t*) p_param->val, strlen(p_param->val));
     }
 
-    /* concatenate the api secret key */
-    strcat(psz_api_sig, psz_api_secret);
-
-    /* generate a md5 hash of the api signature */
-    InitMD5(&p_struct_md5);
-    AddMD5(&p_struct_md5, (uint8_t*) psz_api_sig, strlen(psz_api_sig));
+    /* add the api secret key */
+    AddMD5(&p_struct_md5, (uint8_t*) psz_api_secret, 32);
     EndMD5(&p_struct_md5);
 
-    free(psz_api_sig);
-
-    psz_api_sig_md5 = psz_md5_hash(&p_struct_md5);
-    if (!psz_api_sig_md5)
-        return NULL;
-
-    return psz_api_sig_md5;
+    return psz_md5_hash(&p_struct_md5);
 }
 
 /*****************************************************************************
@@ -583,6 +554,10 @@ static void AddParam(vlc_array_t *p_params, const char *psz_key,
                     const char *psz_val)
 {
     audioscrobbler_param_t *p_param = malloc(sizeof(audioscrobbler_param_t));
+    if (!p_param) {
+        /* Out of memory */
+        return;
+    }
 
     p_param->key = psz_key;
     p_param->val = psz_val;
@@ -599,14 +574,13 @@ static char *BuildGetRequest(intf_thread_t *p_this, vlc_array_t *p_params)
     audioscrobbler_param_t *p_param;
     char *psz_request;
     ssize_t i_len = 0;
-    int i;
 
     /* compute the length of the request */
-    i_len += 9; /* for http:// */
+    i_len += 7; /* for http:// */
     i_len += strlen(p_submit_url.psz_host) + strlen(p_submit_url.psz_path);
     i_len += 2; /* for /? */
 
-    for (i = 0; i < p_params->i_count; i++)
+    for (int i = 0; i < p_params->i_count; i++)
     {
         p_param = p_params->pp_elems[i];
         i_len += strlen(p_param->key) + strlen(p_param->val);
@@ -622,7 +596,7 @@ static char *BuildGetRequest(intf_thread_t *p_this, vlc_array_t *p_params)
     sprintf(psz_request, "http://%s%s/?%s=%s", p_submit_url.psz_host,
             p_submit_url.psz_path, p_param->key, p_param->val);
 
-    for (i = 1; i < p_params->i_count; i++)
+    for (int i = 1; i < p_params->i_count; i++)
     {
         p_param = p_params->pp_elems[i];
 
@@ -644,10 +618,9 @@ static char *BuildPostRequest(intf_thread_t *p_this, vlc_array_t *p_params)
     audioscrobbler_param_t *p_param;
     char *psz_data, *psz_request;
     ssize_t i_len = 0;
-    int i;
 
     /* compute the length of the request */
-    for (i = 0; i < p_params->i_count; i++)
+    for (int i = 0; i < p_params->i_count; i++)
     {
         p_param = p_params->pp_elems[i];
         i_len += strlen(p_param->key) + strlen(p_param->val);
@@ -662,7 +635,7 @@ static char *BuildPostRequest(intf_thread_t *p_this, vlc_array_t *p_params)
     p_param = p_params->pp_elems[0];
     sprintf(psz_data, "%s=%s", p_param->key, p_param->val);
 
-    for (i = 1; i < p_params->i_count; i++)
+    for (int i = 1; i < p_params->i_count; i++)
     {
         p_param = p_params->pp_elems[i];
 
@@ -704,7 +677,7 @@ static char *BuildPostRequest(intf_thread_t *p_this, vlc_array_t *p_params)
 static char *BuildRequest(intf_thread_t *p_this, vlc_array_t *p_params,
                             audioscrobbler_request_t e_type)
 {
-    char *psz_api_sig, *psz_request = NULL;
+    char *psz_api_sig;
 
     /* add the api key to the parameters list */
     AddParam(p_params, "api_key", API_KEY);
@@ -721,17 +694,17 @@ static char *BuildRequest(intf_thread_t *p_this, vlc_array_t *p_params,
     switch (e_type)
     {
         case GET:
-            psz_request = BuildGetRequest(p_this, p_params);
+            return BuildGetRequest(p_this, p_params);
             break;
         case POST:
-            psz_request = BuildPostRequest(p_this, p_params);
+            return BuildPostRequest(p_this, p_params);
             break;
         default:
             msg_Err(p_this, "invalid type of request %d", e_type);
             break;
     }
 
-    return psz_request;
+    return NULL;
 }
 
 /*****************************************************************************
-- 
1.7.9.3




More information about the vlc-devel mailing list