Imported Upstream version 7.53.1
[platform/upstream/curl.git] / src / tool_metalink.c
index 3573b05..f417356 100644 (file)
@@ -5,11 +5,11 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
- * are also available at http://curl.haxx.se/docs/copyright.html.
+ * are also available at https://curl.haxx.se/docs/copyright.html.
  *
  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  * copies of the Software, and permit persons to whom the Software is
 #ifdef USE_METALINK
 
 #include <sys/stat.h>
+#include <stdlib.h>
 
 #ifdef HAVE_FCNTL_H
 #  include <fcntl.h>
 #endif
 
-#ifdef USE_SSLEAY
-#  ifdef USE_OPENSSL
-#    include <openssl/md5.h>
-#    include <openssl/sha.h>
-#  else
-#    include <md5.h>
-#    include <sha.h>
-#  endif
+#ifdef USE_OPENSSL
+#  include <openssl/md5.h>
+#  include <openssl/sha.h>
 #elif defined(USE_GNUTLS_NETTLE)
 #  include <nettle/md5.h>
 #  include <nettle/sha.h>
@@ -97,8 +93,6 @@ struct win32_crypto_hash {
 #  error "Can't compile METALINK support without a crypto library."
 #endif
 
-#include "rawstr.h"
-
 #define ENABLE_CURLX_PRINTF
 /* use our own printf() functions */
 #include "curlx.h"
@@ -381,7 +375,7 @@ static void SHA256_Final(unsigned char digest[32], SHA256_CTX *ctx)
   sha256_finish(ctx, digest);
 }
 
-#elif defined(_WIN32) && !defined(USE_SSLEAY)
+#elif defined(_WIN32) && !defined(USE_OPENSSL)
 
 static void win32_crypto_final(struct win32_crypto_hash *ctx,
                                unsigned char *digest,
@@ -568,18 +562,13 @@ int Curl_digest_final(digest_context *context, unsigned char *result)
 
 static unsigned char hex_to_uint(const char *s)
 {
-  int v[2];
-  int i;
-  for(i = 0; i < 2; ++i) {
-    v[i] = Curl_raw_toupper(s[i]);
-    if('0' <= v[i] && v[i] <= '9') {
-      v[i] -= '0';
-    }
-    else if('A' <= v[i] && v[i] <= 'Z') {
-      v[i] -= 'A'-10;
-    }
-  }
-  return (unsigned char)((v[0] << 4) | v[1]);
+  char buf[3];
+  unsigned long val;
+  buf[0] = s[0];
+  buf[1] = s[1];
+  buf[2] = 0;
+  val = strtoul(buf, NULL, 16);
+  return (unsigned char)(val&0xff);
 }
 
 /*
@@ -626,6 +615,10 @@ static int check_hash(const char *filename,
   }
 
   result = malloc(digest_def->dparams->digest_resultlen);
+  if(!result) {
+    close(fd);
+    return -1;
+  }
   while(1) {
     unsigned char buf[4096];
     ssize_t len = read(fd, buf, sizeof(buf));
@@ -681,12 +674,17 @@ static metalink_checksum *new_metalink_checksum_from_hex_digest
   size_t i;
   size_t len = strlen(hex_digest);
   digest = malloc(len/2);
+  if(!digest)
+    return 0;
+
   for(i = 0; i < len; i += 2) {
     digest[i/2] = hex_to_uint(hex_digest+i);
   }
   chksum = malloc(sizeof(metalink_checksum));
-  chksum->digest_def = digest_def;
-  chksum->digest = digest;
+  if(chksum) {
+    chksum->digest_def = digest_def;
+    chksum->digest = digest;
+  }
   return chksum;
 }
 
@@ -694,8 +692,14 @@ static metalink_resource *new_metalink_resource(const char *url)
 {
   metalink_resource *res;
   res = malloc(sizeof(metalink_resource));
-  res->next = NULL;
-  res->url = strdup(url);
+  if(res) {
+    res->next = NULL;
+    res->url = strdup(url);
+    if(!res->url) {
+      free(res);
+      return NULL;
+    }
+  }
   return res;
 }
 
@@ -720,8 +724,15 @@ static metalinkfile *new_metalinkfile(metalink_file_t *fileinfo)
 {
   metalinkfile *f;
   f = (metalinkfile*)malloc(sizeof(metalinkfile));
+  if(!f)
+    return NULL;
+
   f->next = NULL;
   f->filename = strdup(fileinfo->name);
+  if(!f->filename) {
+    free(f);
+    return NULL;
+  }
   f->checksum = NULL;
   f->resource = NULL;
   if(fileinfo->checksums) {
@@ -730,7 +741,7 @@ static metalinkfile *new_metalinkfile(metalink_file_t *fileinfo)
         ++digest_alias) {
       metalink_checksum_t **p;
       for(p = fileinfo->checksums; *p; ++p) {
-        if(Curl_raw_equal(digest_alias->alias_name, (*p)->type) &&
+        if(curl_strequal(digest_alias->alias_name, (*p)->type) &&
            check_hex_digest((*p)->hash, digest_alias->digest_def)) {
           f->checksum =
             new_metalink_checksum_from_hex_digest(digest_alias->digest_def,
@@ -760,10 +771,10 @@ static metalinkfile *new_metalinkfile(metalink_file_t *fileinfo)
          metainfo file URL may be appeared in fileinfo->metaurls.
       */
       if((*p)->type == NULL ||
-         Curl_raw_equal((*p)->type, "http") ||
-         Curl_raw_equal((*p)->type, "https") ||
-         Curl_raw_equal((*p)->type, "ftp") ||
-         Curl_raw_equal((*p)->type, "ftps")) {
+         curl_strequal((*p)->type, "http") ||
+         curl_strequal((*p)->type, "https") ||
+         curl_strequal((*p)->type, "ftp") ||
+         curl_strequal((*p)->type, "ftps")) {
         res = new_metalink_resource((*p)->url);
         tail->next = res;
         tail = res;
@@ -822,8 +833,10 @@ int parse_metalink(struct OperationConfig *config, struct OutStruct *outs,
       url = new_getout(config);
 
     if(url) {
-      metalinkfile *mlfile;
-      mlfile = new_metalinkfile(*files);
+      metalinkfile *mlfile = new_metalinkfile(*files);
+      if(!mlfile)
+        break;
+
       if(!mlfile->checksum) {
         warnings = TRUE;
         fprintf(config->global->errors,
@@ -887,7 +900,7 @@ static int check_content_type(const char *content_type, const char *media_type)
   if(!*ptr) {
     return 0;
   }
-  return Curl_raw_nequal(ptr, media_type, media_type_len) &&
+  return curl_strnequal(ptr, media_type, media_type_len) &&
     (*(ptr+media_type_len) == '\0' || *(ptr+media_type_len) == ' ' ||
      *(ptr+media_type_len) == '\t' || *(ptr+media_type_len) == ';');
 }