diff options
Diffstat (limited to 'src/H5FDs3comms.c')
-rw-r--r-- | src/H5FDs3comms.c | 1067 |
1 files changed, 400 insertions, 667 deletions
diff --git a/src/H5FDs3comms.c b/src/H5FDs3comms.c index d958675..ee5b5ec 100644 --- a/src/H5FDs3comms.c +++ b/src/H5FDs3comms.c @@ -140,18 +140,16 @@ curlwritecallback(char *ptr, size_t size, size_t nmemb, void *userdata) size_t product = (size * nmemb); size_t written = 0; - if (sds->magic != S3COMMS_CALLBACK_DATASTRUCT_MAGIC) { + if (sds->magic != S3COMMS_CALLBACK_DATASTRUCT_MAGIC) return written; - } if (size > 0) { - HDmemcpy(&(sds->data[sds->size]), ptr, product); + H5MM_memcpy(&(sds->data[sds->size]), ptr, product); sds->size += product; written = product; } return written; - } /* end curlwritecallback() */ /*---------------------------------------------------------------------------- @@ -224,21 +222,19 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) FUNC_ENTER_NOAPI_NOINIT #if S3COMMS_DEBUG - HDfprintf(stdout, "called H5FD_s3comms_hrb_node_set.\n"); + HDfprintf(stdout, "called H5FD_s3comms_hrb_node_set."); HDprintf("NAME: %s\n", name); HDprintf("VALUE: %s\n", value); HDprintf("LIST:\n->"); - for (node_ptr = (*L); node_ptr != NULL; node_ptr = node_ptr->next) { + for (node_ptr = (*L); node_ptr != NULL; node_ptr = node_ptr->next) HDfprintf(stdout, "{%s}\n->", node_ptr->cat); - } HDprintf("(null)\n"); - fflush(stdout); + HDfflush(stdout); node_ptr = NULL; #endif - if (name == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to operate on null name.\n"); - } + if (name == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to operate on null name"); namelen = HDstrlen(name); /*********************** @@ -248,12 +244,10 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) /* copy and lowercase name */ lowername = (char *)H5MM_malloc(sizeof(char) * (namelen + 1)); - if (lowername == NULL) { - HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for lowercase name copy.\n"); - } - for (i = 0; i < namelen; i++) { - lowername[i] = (char)tolower((int)name[i]); - } + if (lowername == NULL) + HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for lowercase name copy."); + for (i = 0; i < namelen; i++) + lowername[i] = (char)HDtolower((int)name[i]); lowername[namelen] = 0; /* If value supplied, copy name, value, and concatenated "name: value". @@ -267,33 +261,28 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) size_t catwrite = catlen + 3; /* 3 not 1 to quiet compiler warning */ namecpy = (char *)H5MM_malloc(sizeof(char) * (namelen + 1)); - if (namecpy == NULL) { - HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for name copy.\n"); - } - HDmemcpy(namecpy, name, (namelen + 1)); + if (namecpy == NULL) + HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for name copy."); + H5MM_memcpy(namecpy, name, (namelen + 1)); valuecpy = (char *)H5MM_malloc(sizeof(char) * (valuelen + 1)); - if (valuecpy == NULL) { - HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for value copy.\n"); - } - HDmemcpy(valuecpy, value, (valuelen + 1)); + if (valuecpy == NULL) + HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for value copy."); + H5MM_memcpy(valuecpy, value, (valuelen + 1)); nvcat = (char *)H5MM_malloc(sizeof(char) * catwrite); - if (nvcat == NULL) { - HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for concatenated string.\n"); - } + if (nvcat == NULL) + HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for concatenated string."); ret = HDsnprintf(nvcat, catwrite, "%s: %s", name, value); - if (ret < 0 || (size_t)ret > catlen) { + if (ret < 0 || (size_t)ret > catlen) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cannot concatenate `%s: %s", name, value); - } HDassert(catlen == HDstrlen(nvcat)); /* create new_node, should we need it */ new_node = (hrb_node_t *)H5MM_malloc(sizeof(hrb_node_t)); - if (new_node == NULL) { - HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for new set.\n"); - } + if (new_node == NULL) + HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "cannot make space for new set."); new_node->magic = S3COMMS_HRB_NODE_MAGIC; new_node->name = NULL; @@ -308,13 +297,12 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) ***************/ if (*L == NULL) { - if (value == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "trying to remove node from empty list"); - } + if (value == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "trying to remove node from empty list") else { #if S3COMMS_DEBUG HDprintf("CREATE NEW\n"); - fflush(stdout); + HDfflush(stdout); #endif /******************* * CREATE NEW LIST * @@ -338,14 +326,14 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) /* Check whether to modify/remove first node in list */ - if (strcmp(lowername, node_ptr->lowername) == 0) { + if (HDstrcmp(lowername, node_ptr->lowername) == 0) { is_looking = FALSE; if (value == NULL) { #if S3COMMS_DEBUG HDprintf("REMOVE HEAD\n"); - fflush(stdout); + HDfflush(stdout); #endif /*************** * REMOVE HEAD * @@ -355,39 +343,39 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) #if S3COMMS_DEBUG HDprintf("FREEING CAT (node)\n"); - fflush(stdout); + HDfflush(stdout); #endif H5MM_xfree(node_ptr->cat); #if S3COMMS_DEBUG HDprintf("FREEING LOWERNAME (node)\n"); - fflush(stdout); + HDfflush(stdout); #endif H5MM_xfree(node_ptr->lowername); #if S3COMMS_DEBUG HDprintf("FREEING NAME (node)\n"); - fflush(stdout); + HDfflush(stdout); #endif H5MM_xfree(node_ptr->name); #if S3COMMS_DEBUG HDprintf("FREEING VALUE (node)\n"); - fflush(stdout); + HDfflush(stdout); #endif H5MM_xfree(node_ptr->value); #if S3COMMS_DEBUG HDprintf("MAGIC OK? %s\n", (node_ptr->magic == S3COMMS_HRB_NODE_MAGIC) ? "YES" : "NO"); - fflush(stdout); + HDfflush(stdout); #endif HDassert(node_ptr->magic == S3COMMS_HRB_NODE_MAGIC); node_ptr->magic += 1ul; #if S3COMMS_DEBUG HDprintf("FREEING POINTER\n"); - fflush(stdout); + HDfflush(stdout); #endif H5MM_xfree(node_ptr); #if S3COMMS_DEBUG HDprintf("FREEING WORKING LOWERNAME\n"); - fflush(stdout); + HDfflush(stdout); #endif H5MM_xfree(lowername); lowername = NULL; @@ -395,7 +383,7 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) else { #if S3COMMS_DEBUG HDprintf("MODIFY HEAD\n"); - fflush(stdout); + HDfflush(stdout); #endif /*************** * MODIFY HEAD * @@ -416,17 +404,16 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) new_node = NULL; } } - else if (strcmp(lowername, node_ptr->lowername) < 0) { + else if (HDstrcmp(lowername, node_ptr->lowername) < 0) { is_looking = FALSE; - if (value == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "trying to remove a node 'before' head"); - } + if (value == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "trying to remove a node 'before' head") else { #if S3COMMS_DEBUG HDprintf("PREPEND NEW HEAD\n"); - fflush(stdout); + HDfflush(stdout); #endif /******************* * INSERT NEW HEAD * @@ -450,19 +437,18 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) is_looking = FALSE; - if (value == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "trying to remove absent node"); - } + if (value == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "trying to remove absent node") else { #if S3COMMS_DEBUG HDprintf("APPEND A NODE\n"); - fflush(stdout); + HDfflush(stdout); #endif /******************* * APPEND NEW NODE * *******************/ - HDassert(strcmp(lowername, node_ptr->lowername) > 0); + HDassert(HDstrcmp(lowername, node_ptr->lowername) > 0); new_node->name = namecpy; new_node->value = valuecpy; new_node->lowername = lowername; @@ -470,23 +456,22 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) node_ptr->next = new_node; } } - else if (strcmp(lowername, node_ptr->next->lowername) < 0) { + else if (HDstrcmp(lowername, node_ptr->next->lowername) < 0) { is_looking = FALSE; - if (value == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "trying to remove absent node"); - } + if (value == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "trying to remove absent node") else { #if S3COMMS_DEBUG HDprintf("INSERT A NODE\n"); - fflush(stdout); + HDfflush(stdout); #endif /******************* * INSERT NEW NODE * *******************/ - HDassert(strcmp(lowername, node_ptr->lowername) > 0); + HDassert(HDstrcmp(lowername, node_ptr->lowername) > 0); new_node->name = namecpy; new_node->value = valuecpy; new_node->lowername = lowername; @@ -495,7 +480,7 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) node_ptr->next = new_node; } } - else if (strcmp(lowername, node_ptr->next->lowername) == 0) { + else if (HDstrcmp(lowername, node_ptr->next->lowername) == 0) { is_looking = FALSE; @@ -509,7 +494,7 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) #if S3COMMS_DEBUG HDprintf("REMOVE A NODE\n"); - fflush(stdout); + HDfflush(stdout); #endif H5MM_xfree(tmp->cat); H5MM_xfree(tmp->lowername); @@ -526,7 +511,7 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) else { #if S3COMMS_DEBUG HDprintf("MODIFY A NODE\n"); - fflush(stdout); + HDfflush(stdout); #endif /***************** * MODIFY A NODE * @@ -560,20 +545,15 @@ H5FD_s3comms_hrb_node_set(hrb_node_t **L, const char *name, const char *value) done: if (ret_value == FAIL) { - /* clean up - */ - if (nvcat != NULL) { + /* clean up */ + if (nvcat != NULL) H5MM_xfree(nvcat); - } - if (namecpy != NULL) { + if (namecpy != NULL) H5MM_xfree(namecpy); - } - if (lowername != NULL) { + if (lowername != NULL) H5MM_xfree(lowername); - } - if (valuecpy != NULL) { + if (valuecpy != NULL) H5MM_xfree(valuecpy); - } if (new_node != NULL) { HDassert(new_node->magic == S3COMMS_HRB_NODE_MAGIC); new_node->magic += 1ul; @@ -634,9 +614,8 @@ H5FD_s3comms_hrb_destroy(hrb_t **_buf) if (_buf != NULL && *_buf != NULL) { buf = *_buf; - if (buf->magic != S3COMMS_HRB_MAGIC) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "pointer's magic does not match.\n"); - } + if (buf->magic != S3COMMS_HRB_MAGIC) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "pointer's magic does not match."); H5MM_xfree(buf->verb); H5MM_xfree(buf->version); @@ -697,68 +676,55 @@ H5FD_s3comms_hrb_init_request(const char *_verb, const char *_resource, const ch HDfprintf(stdout, "called H5FD_s3comms_hrb_init_request.\n"); #endif - if (_resource == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "resource string cannot be null.\n"); - } + if (_resource == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "resource string cannot be null."); - /* populate valid NULLs with defaults - */ - if (_verb == NULL) { + /* populate valid NULLs with defaults */ + if (_verb == NULL) _verb = "GET"; - } - - if (_http_version == NULL) { + if (_http_version == NULL) _http_version = "HTTP/1.1"; - } - /* malloc space for and prepare structure - */ + /* malloc space for and prepare structure */ request = (hrb_t *)H5MM_malloc(sizeof(hrb_t)); - if (request == NULL) { + if (request == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, NULL, "no space for request structure"); - } request->magic = S3COMMS_HRB_MAGIC; request->body = NULL; request->body_len = 0; request->first_header = NULL; - /* malloc and copy strings for the structure - */ + /* malloc and copy strings for the structure */ reslen = HDstrlen(_resource); if (_resource[0] == '/') { res = (char *)H5MM_malloc(sizeof(char) * (reslen + 1)); - if (res == NULL) { + if (res == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, NULL, "no space for resource string"); - } - HDmemcpy(res, _resource, (reslen + 1)); + H5MM_memcpy(res, _resource, (reslen + 1)); } else { res = (char *)H5MM_malloc(sizeof(char) * (reslen + 2)); - if (res == NULL) { + if (res == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, NULL, "no space for resource string"); - } *res = '/'; - HDmemcpy((&res[1]), _resource, (reslen + 1)); + H5MM_memcpy((&res[1]), _resource, (reslen + 1)); HDassert((reslen + 1) == HDstrlen(res)); } /* end if (else resource string not starting with '/') */ verblen = HDstrlen(_verb) + 1; verb = (char *)H5MM_malloc(sizeof(char) * verblen); - if (verb == NULL) { + if (verb == NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "no space for verb string"); - } HDstrncpy(verb, _verb, verblen); vrsnlen = HDstrlen(_http_version) + 1; vrsn = (char *)H5MM_malloc(sizeof(char) * vrsnlen); - if (vrsn == NULL) { + if (vrsn == NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "no space for http-version string"); - } HDstrncpy(vrsn, _http_version, vrsnlen); - /* place new copies into structure - */ + /* place new copies into structure */ request->resource = res; request->verb = verb; request->version = vrsn; @@ -766,8 +732,7 @@ H5FD_s3comms_hrb_init_request(const char *_verb, const char *_resource, const ch ret_value = request; done: - /* if there is an error, clean up after ourselves - */ + /* if there is an error, clean up after ourselves */ if (ret_value == NULL) { if (request != NULL) H5MM_xfree(request); @@ -818,12 +783,10 @@ H5FD_s3comms_s3r_close(s3r_t *handle) HDfprintf(stdout, "called H5FD_s3comms_s3r_close.\n"); #endif - if (handle == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle cannot be null.\n"); - } - if (handle->magic != S3COMMS_S3R_MAGIC) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has invalid magic.\n"); - } + if (handle == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle cannot be null."); + if (handle->magic != S3COMMS_S3R_MAGIC) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has invalid magic."); curl_easy_cleanup(handle->curlhandle); @@ -834,9 +797,8 @@ H5FD_s3comms_s3r_close(s3r_t *handle) HDassert(handle->httpverb != NULL); H5MM_xfree(handle->httpverb); - if (FAIL == H5FD_s3comms_free_purl(handle->purl)) { + if (FAIL == H5FD_s3comms_free_purl(handle->purl)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to release parsed url structure") - } H5MM_xfree(handle); @@ -871,9 +833,8 @@ H5FD_s3comms_s3r_get_filesize(s3r_t *handle) FUNC_ENTER_NOAPI_NOINIT_NOERR - if (handle != NULL) { + if (handle != NULL) ret_value = handle->filesize; - } FUNC_LEAVE_NOAPI(ret_value) } /* H5FD_s3comms_s3r_get_filesize */ @@ -927,45 +888,33 @@ H5FD_s3comms_s3r_getsize(s3r_t *handle) HDfprintf(stdout, "called H5FD_s3comms_s3r_getsize.\n"); #endif - if (handle == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle cannot be null.\n"); - } - if (handle->magic != S3COMMS_S3R_MAGIC) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has invalid magic.\n"); - } - if (handle->curlhandle == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has bad (null) curlhandle.\n") - } + if (handle == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle cannot be null."); + if (handle->magic != S3COMMS_S3R_MAGIC) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has invalid magic."); + if (handle->curlhandle == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has bad (null) curlhandle.") /******************** * PREPARE FOR HEAD * ********************/ curlh = handle->curlhandle; + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_NOBODY, 1L)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "error while setting CURL option (CURLOPT_NOBODY)."); - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_NOBODY, 1L)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "error while setting CURL option (CURLOPT_NOBODY). " - "(placeholder flags)"); - } - - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HEADERDATA, &sds)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "error while setting CURL option (CURLOPT_HEADERDATA). " - "(placeholder flags)"); - } + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HEADERDATA, &sds)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "error while setting CURL option (CURLOPT_HEADERDATA)."); HDassert(handle->httpverb == NULL); handle->httpverb = (char *)H5MM_malloc(sizeof(char) * 16); - if (handle->httpverb == NULL) { + if (handle->httpverb == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "unable to allocate space for S3 request HTTP verb"); - } - HDmemcpy(handle->httpverb, "HEAD", 5); + H5MM_memcpy(handle->httpverb, "HEAD", 5); headerresponse = (char *)H5MM_malloc(sizeof(char) * CURL_MAX_HTTP_HEADER); - if (headerresponse == NULL) { + if (headerresponse == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "unable to allocate space for curl header response"); - } sds.data = headerresponse; /******************* @@ -976,55 +925,44 @@ H5FD_s3comms_s3r_getsize(s3r_t *handle) * but, with a NULL destination and NOBODY and HEADERDATA supplied above, * only http metadata will be sent by server and recorded by s3comms */ - if (FAIL == H5FD_s3comms_s3r_read(handle, 0, 0, NULL)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem in reading during getsize.\n"); - } + if (FAIL == H5FD_s3comms_s3r_read(handle, 0, 0, NULL)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem in reading during getsize."); - if (sds.size > CURL_MAX_HTTP_HEADER) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "HTTP metadata buffer overrun\n"); - } - else if (sds.size == 0) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "No HTTP metadata\n"); + if (sds.size > CURL_MAX_HTTP_HEADER) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "HTTP metadata buffer overrun") + else if (sds.size == 0) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "No HTTP metadata") #if S3COMMS_DEBUG - } - else { + else HDfprintf(stderr, "GETSIZE: OK\n"); #endif - } /****************** * PARSE RESPONSE * ******************/ - start = strstr(headerresponse, "\r\nContent-Length: "); - if (start == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not find \"Content-Length\" in response.\n"); - } + start = HDstrstr(headerresponse, "\r\nContent-Length: "); + if (start == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not find \"Content-Length\" in response."); - /* move "start" to beginning of value in line; find end of line - */ + /* move "start" to beginning of value in line; find end of line */ start = start + HDstrlen("\r\nContent-Length: "); - end = strstr(start, "\r\n"); - if (end == NULL) { + end = HDstrstr(start, "\r\n"); + if (end == NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not find end of content length line"); - } /* place null terminator at end of numbers */ *end = '\0'; - content_length = strtoumax((const char *)start, NULL, 0); + content_length = HDstrtoumax((const char *)start, NULL, 0); + if (UINTMAX_MAX > SIZE_MAX && content_length > SIZE_MAX) + HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "content_length overflows size_t"); - if (UINTMAX_MAX > SIZE_MAX && content_length > SIZE_MAX) { - HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "content_length overflows size_t\n"); - } - - if (content_length == 0 || errno == ERANGE) /* errno set by strtoumax*/ - { + if (content_length == 0 || errno == ERANGE) /* errno set by HDstrtoumax*/ HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not convert found \"Content-Length\" response (\"%s\")", start); /* range is null-terminated, remember */ - } handle->filesize = (size_t)content_length; @@ -1032,24 +970,17 @@ H5FD_s3comms_s3r_getsize(s3r_t *handle) * UNDO HEAD SETTINGS * **********************/ - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_NOBODY, NULL)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "error while setting CURL option (CURLOPT_NOBODY). " - "(placeholder flags)"); - } + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_NOBODY, NULL)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "error while setting CURL option (CURLOPT_NOBODY)."); - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HEADERDATA, NULL)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "error while setting CURL option (CURLOPT_HEADERDATA). " - "(placeholder flags)"); - } + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HEADERDATA, NULL)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "error while setting CURL option (CURLOPT_HEADERDATA)."); done: H5MM_xfree(headerresponse); sds.magic += 1; /* set to bad magic */ FUNC_LEAVE_NOAPI(ret_value); - } /* H5FD_s3comms_s3r_getsize */ /*---------------------------------------------------------------------------- @@ -1106,21 +1037,19 @@ H5FD_s3comms_s3r_open(const char *url, const char *region, const char *id, const HDfprintf(stdout, "called H5FD_s3comms_s3r_open.\n"); #endif - if (url == NULL || url[0] == '\0') { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "url cannot be null.\n"); - } + if (url == NULL || url[0] == '\0') + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "url cannot be null."); - if (FAIL == H5FD_s3comms_parse_url(url, &purl)) { + if (FAIL == H5FD_s3comms_parse_url(url, &purl)) /* probably a malformed url, but could be internal error */ HGOTO_ERROR(H5E_ARGS, H5E_CANTCREATE, NULL, "unable to create parsed url structure"); - } + HDassert(purl != NULL); /* if above passes, this must be true */ HDassert(purl->magic == S3COMMS_PARSED_URL_MAGIC); handle = (s3r_t *)H5MM_malloc(sizeof(s3r_t)); - if (handle == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, NULL, "could not malloc space for handle.\n"); - } + if (handle == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, NULL, "could not malloc space for handle."); handle->magic = S3COMMS_S3R_MAGIC; handle->purl = purl; @@ -1135,40 +1064,33 @@ H5FD_s3comms_s3r_open(const char *url, const char *region, const char *id, const *************************************/ if ((region != NULL && *region != '\0') || (id != NULL && *id != '\0') || (signing_key != NULL)) { - /* if one exists, all three must exist - */ - if (region == NULL || region[0] == '\0') { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "region cannot be null.\n"); - } - if (id == NULL || id[0] == '\0') { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "secret id cannot be null.\n"); - } - if (signing_key == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "signing key cannot be null.\n"); - } - /* copy strings - */ + /* if one exists, all three must exist */ + if (region == NULL || region[0] == '\0') + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "region cannot be null."); + if (id == NULL || id[0] == '\0') + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "secret id cannot be null."); + if (signing_key == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "signing key cannot be null."); + + /* copy strings */ tmplen = HDstrlen(region) + 1; handle->region = (char *)H5MM_malloc(sizeof(char) * tmplen); - if (handle->region == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "could not malloc space for handle region copy.\n"); - } - HDmemcpy(handle->region, region, tmplen); + if (handle->region == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "could not malloc space for handle region copy."); + H5MM_memcpy(handle->region, region, tmplen); tmplen = HDstrlen(id) + 1; handle->secret_id = (char *)H5MM_malloc(sizeof(char) * tmplen); - if (handle->secret_id == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "could not malloc space for handle ID copy.\n"); - } - HDmemcpy(handle->secret_id, id, tmplen); + if (handle->secret_id == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "could not malloc space for handle ID copy."); + H5MM_memcpy(handle->secret_id, id, tmplen); tmplen = SHA256_DIGEST_LENGTH; handle->signing_key = (unsigned char *)H5MM_malloc(sizeof(unsigned char) * tmplen); - if (handle->signing_key == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "could not malloc space for handle key copy.\n"); - } - HDmemcpy(handle->signing_key, signing_key, tmplen); + if (handle->signing_key == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "could not malloc space for handle key copy."); + H5MM_memcpy(handle->signing_key, signing_key, tmplen); } /* if authentication information provided */ /************************ @@ -1176,40 +1098,23 @@ H5FD_s3comms_s3r_open(const char *url, const char *region, const char *id, const ************************/ curlh = curl_easy_init(); + if (curlh == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "problem creating curl easy handle!"); - if (curlh == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "problem creating curl easy handle!\n"); - } - - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HTTPGET, 1L)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, - "error while setting CURL option (CURLOPT_HTTPGET). " - "(placeholder flags)"); - } + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HTTPGET, 1L)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "error while setting CURL option (CURLOPT_HTTPGET)."); - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, - "error while setting CURL option (CURLOPT_HTTP_VERSION). " - "(placeholder flags)"); - } + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "error while setting CURL option (CURLOPT_HTTP_VERSION)."); - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_FAILONERROR, 1L)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, - "error while setting CURL option (CURLOPT_FAILONERROR). " - "(placeholder flags)"); - } + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_FAILONERROR, 1L)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "error while setting CURL option (CURLOPT_FAILONERROR)."); - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_WRITEFUNCTION, curlwritecallback)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, - "error while setting CURL option (CURLOPT_WRITEFUNCTION). " - "(placeholder flags)"); - } + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_WRITEFUNCTION, curlwritecallback)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "error while setting CURL option (CURLOPT_WRITEFUNCTION)."); - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_URL, url)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, - "error while setting CURL option (CURLOPT_URL). " - "(placeholder flags)"); - } + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_URL, url)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "error while setting CURL option (CURLOPT_URL)."); #if S3COMMS_CURL_VERBOSITY > 1 /* CURL will print (to stdout) information for each operation @@ -1225,40 +1130,35 @@ H5FD_s3comms_s3r_open(const char *url, const char *region, const char *id, const * GET FILE SIZE * *******************/ - if (FAIL == H5FD_s3comms_s3r_getsize(handle)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "problem in H5FD_s3comms_s3r_getsize.\n"); - } + if (FAIL == H5FD_s3comms_s3r_getsize(handle)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "problem in H5FD_s3comms_s3r_getsize."); /********************* * FINAL PREPARATION * *********************/ HDassert(handle->httpverb != NULL); - HDmemcpy(handle->httpverb, "GET", 4); + H5MM_memcpy(handle->httpverb, "GET", 4); ret_value = handle; done: if (ret_value == NULL) { - if (curlh != NULL) { + if (curlh != NULL) curl_easy_cleanup(curlh); - } - if (FAIL == H5FD_s3comms_free_purl(purl)) { + if (FAIL == H5FD_s3comms_free_purl(purl)) HDONE_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "unable to free parsed url structure") - } if (handle != NULL) { H5MM_xfree(handle->region); H5MM_xfree(handle->secret_id); H5MM_xfree(handle->signing_key); - if (handle->httpverb != NULL) { + if (handle->httpverb != NULL) H5MM_xfree(handle->httpverb); - } H5MM_xfree(handle); } } FUNC_LEAVE_NOAPI(ret_value) - } /* H5FD_s3comms_s3r_open */ /*---------------------------------------------------------------------------- @@ -1328,22 +1228,17 @@ H5FD_s3comms_s3r_read(s3r_t *handle, haddr_t offset, size_t len, void *dest) * ABSOLUTELY NECESSARY SANITY-CHECKS * **************************************/ - if (handle == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle cannot be null.\n"); - } - if (handle->magic != S3COMMS_S3R_MAGIC) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has invalid magic.\n"); - } - if (handle->curlhandle == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has bad (null) curlhandle.\n") - } - if (handle->purl == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has bad (null) url.\n") - } + if (handle == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle cannot be null."); + if (handle->magic != S3COMMS_S3R_MAGIC) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has invalid magic."); + if (handle->curlhandle == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has bad (null) curlhandle.") + if (handle->purl == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle has bad (null) url.") HDassert(handle->purl->magic == S3COMMS_PARSED_URL_MAGIC); - if (offset > handle->filesize || (len + offset) > handle->filesize) { + if (offset > handle->filesize || (len + offset) > handle->filesize) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to read past EoF") - } curlh = handle->curlhandle; @@ -1353,18 +1248,15 @@ H5FD_s3comms_s3r_read(s3r_t *handle, haddr_t offset, size_t len, void *dest) if (dest != NULL) { sds = (struct s3r_datastruct *)H5MM_malloc(sizeof(struct s3r_datastruct)); - if (sds == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "could not malloc destination datastructure.\n"); - } + if (sds == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "could not malloc destination datastructure."); sds->magic = S3COMMS_CALLBACK_DATASTRUCT_MAGIC; sds->data = (char *)dest; sds->size = 0; - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_WRITEDATA, sds)) { + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_WRITEDATA, sds)) HGOTO_ERROR(H5E_ARGS, H5E_UNINITIALIZED, FAIL, - "error while setting CURL option (CURLOPT_WRITEDATA). " - "(placeholder flags)"); - } + "error while setting CURL option (CURLOPT_WRITEDATA)."); } /********************* @@ -1373,21 +1265,18 @@ H5FD_s3comms_s3r_read(s3r_t *handle, haddr_t offset, size_t len, void *dest) if (len > 0) { rangebytesstr = (char *)H5MM_malloc(sizeof(char) * (S3COMMS_MAX_RANGE_STRING_SIZE + 1)); - if (rangebytesstr == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "could not malloc range format string.\n"); - } - ret = HDsnprintf(rangebytesstr, (S3COMMS_MAX_RANGE_STRING_SIZE), - "bytes=" H5_PRINTF_HADDR_FMT "-" H5_PRINTF_HADDR_FMT, offset, offset + len - 1); + if (rangebytesstr == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "could not malloc range format string."); + ret = HDsnprintf(rangebytesstr, (S3COMMS_MAX_RANGE_STRING_SIZE), "bytes=%" PRIuHADDR "-%" PRIuHADDR, + offset, offset + len - 1); if (ret <= 0 || ret >= S3COMMS_MAX_RANGE_STRING_SIZE) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to format HTTP Range value"); } else if (offset > 0) { rangebytesstr = (char *)H5MM_malloc(sizeof(char) * (S3COMMS_MAX_RANGE_STRING_SIZE + 1)); - if (rangebytesstr == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "could not malloc range format string.\n"); - } - ret = HDsnprintf(rangebytesstr, (S3COMMS_MAX_RANGE_STRING_SIZE), "bytes=" H5_PRINTF_HADDR_FMT "-", - offset); + if (rangebytesstr == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "could not malloc range format string."); + ret = HDsnprintf(rangebytesstr, (S3COMMS_MAX_RANGE_STRING_SIZE), "bytes=%" PRIuHADDR "-", offset); if (ret <= 0 || ret >= S3COMMS_MAX_RANGE_STRING_SIZE) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to format HTTP Range value"); } @@ -1397,22 +1286,19 @@ H5FD_s3comms_s3r_read(s3r_t *handle, haddr_t offset, size_t len, void *dest) *******************/ if (handle->signing_key == NULL) { - /* Do not authenticate. - */ + /* Do not authenticate. */ if (rangebytesstr != NULL) { - /* Pass in range directly - */ + /* Pass in range directly */ char *bytesrange_ptr = NULL; /* pointer past "bytes=" portion */ - bytesrange_ptr = strchr(rangebytesstr, '='); + bytesrange_ptr = HDstrchr(rangebytesstr, '='); HDassert(bytesrange_ptr != NULL); bytesrange_ptr++; /* move to first char past '=' */ HDassert(*bytesrange_ptr != '\0'); - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_RANGE, bytesrange_ptr)) { + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_RANGE, bytesrange_ptr)) HGOTO_ERROR(H5E_VFL, H5E_UNINITIALIZED, FAIL, "error while setting CURL option (CURLOPT_RANGE). "); - } } } else { @@ -1446,98 +1332,71 @@ H5FD_s3comms_s3r_read(s3r_t *handle, haddr_t offset, size_t len, void *dest) /**** VERIFY INFORMATION EXISTS ****/ - if (handle->region == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null region.\n"); - } - if (handle->secret_id == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null secret_id.\n"); - } - if (handle->signing_key == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null signing_key.\n"); - } - if (handle->httpverb == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null httpverb.\n"); - } - if (handle->purl->host == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null host.\n"); - } - if (handle->purl->path == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null resource.\n"); - } + if (handle->region == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null region."); + if (handle->secret_id == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null secret_id."); + if (handle->signing_key == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null signing_key."); + if (handle->httpverb == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null httpverb."); + if (handle->purl->host == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null host."); + if (handle->purl->path == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "handle must have non-null resource."); /**** CREATE HTTP REQUEST STRUCTURE (hrb_t) ****/ request = H5FD_s3comms_hrb_init_request((const char *)handle->httpverb, (const char *)handle->purl->path, "HTTP/1.1"); - if (request == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not allocate hrb_t request.\n"); - } + if (request == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not allocate hrb_t request."); HDassert(request->magic == S3COMMS_HRB_MAGIC); now = gmnow(); - if (ISO8601NOW(iso8601now, now) != (ISO8601_SIZE - 1)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not format ISO8601 time.\n"); - } + if (ISO8601NOW(iso8601now, now) != (ISO8601_SIZE - 1)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not format ISO8601 time."); - if (FAIL == H5FD_s3comms_hrb_node_set(&headers, "x-amz-date", (const char *)iso8601now)) { + if (FAIL == H5FD_s3comms_hrb_node_set(&headers, "x-amz-date", (const char *)iso8601now)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to set x-amz-date header") - } - if (headers == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "problem building headers list. " - "(placeholder flags)\n"); - } + if (headers == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem building headers list."); HDassert(headers->magic == S3COMMS_HRB_NODE_MAGIC); - if (FAIL == H5FD_s3comms_hrb_node_set(&headers, "x-amz-content-sha256", (const char *)EMPTY_SHA256)) { + if (FAIL == H5FD_s3comms_hrb_node_set(&headers, "x-amz-content-sha256", (const char *)EMPTY_SHA256)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to set x-amz-content-sha256 header") - } - if (headers == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "problem building headers list. " - "(placeholder flags)\n"); - } + if (headers == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem building headers list."); HDassert(headers->magic == S3COMMS_HRB_NODE_MAGIC); if (rangebytesstr != NULL) { - if (FAIL == H5FD_s3comms_hrb_node_set(&headers, "Range", (const char *)rangebytesstr)) { + if (FAIL == H5FD_s3comms_hrb_node_set(&headers, "Range", rangebytesstr)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to set range header") - } - if (headers == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "problem building headers list. " - "(placeholder flags)\n"); - } + if (headers == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem building headers list."); HDassert(headers->magic == S3COMMS_HRB_NODE_MAGIC); } - if (FAIL == H5FD_s3comms_hrb_node_set(&headers, "Host", (const char *)handle->purl->host)) { + if (FAIL == H5FD_s3comms_hrb_node_set(&headers, "Host", handle->purl->host)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to set host header") - } - if (headers == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "problem building headers list. " - "(placeholder flags)\n"); - } + if (headers == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem building headers list."); HDassert(headers->magic == S3COMMS_HRB_NODE_MAGIC); request->first_header = headers; /**** COMPUTE AUTHORIZATION ****/ - if (FAIL == /* buffer1 -> canonical request */ - H5FD_s3comms_aws_canonical_request(buffer1, 512, signed_headers, 48, request)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "(placeholder flags)\n"); - } - if (FAIL == /* buffer2->string-to-sign */ - H5FD_s3comms_tostringtosign(buffer2, buffer1, iso8601now, handle->region)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "(placeholder flags)\n"); - } - if (FAIL == /* buffer1 -> signature */ - H5FD_s3comms_HMAC_SHA256(handle->signing_key, SHA256_DIGEST_LENGTH, buffer2, HDstrlen(buffer2), - buffer1)) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "(placeholder flags)\n"); - } + /* buffer1 -> canonical request */ + if (FAIL == H5FD_s3comms_aws_canonical_request(buffer1, 512, signed_headers, 48, request)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "bad canonical request"); + /* buffer2->string-to-sign */ + if (FAIL == H5FD_s3comms_tostringtosign(buffer2, buffer1, iso8601now, handle->region)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "bad string-to-sign"); + /* buffer1 -> signature */ + if (FAIL == H5FD_s3comms_HMAC_SHA256(handle->signing_key, SHA256_DIGEST_LENGTH, buffer2, + HDstrlen(buffer2), buffer1)) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "bad signature"); iso8601now[8] = 0; /* trim to yyyyMMDD */ ret = S3COMMS_FORMAT_CREDENTIAL(buffer2, handle->secret_id, iso8601now, handle->region, "s3"); @@ -1546,23 +1405,16 @@ H5FD_s3comms_s3r_read(s3r_t *handle, haddr_t offset, size_t len, void *dest) ret = HDsnprintf(authorization, 512, "AWS4-HMAC-SHA256 Credential=%s,SignedHeaders=%s,Signature=%s", buffer2, signed_headers, buffer1); - if (ret <= 0 || ret >= 512) { + if (ret <= 0 || ret >= 512) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to format aws4 authorization string"); - } - /* append authorization header to http request buffer - */ - if (H5FD_s3comms_hrb_node_set(&headers, "Authorization", (const char *)authorization) == FAIL) { + /* append authorization header to http request buffer */ + if (H5FD_s3comms_hrb_node_set(&headers, "Authorization", (const char *)authorization) == FAIL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to set Authorization header") - } - if (headers == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "problem building headers list. " - "(placeholder flags)\n"); - } + if (headers == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem building headers list."); - /* update hrb's "first header" pointer - */ + /* update hrb's "first header" pointer */ request->first_header = headers; /**** SET CURLHANDLE HTTP HEADERS FROM GENERATED DATA ****/ @@ -1571,29 +1423,20 @@ H5FD_s3comms_s3r_read(s3r_t *handle, haddr_t offset, size_t len, void *dest) while (node != NULL) { HDassert(node->magic == S3COMMS_HRB_NODE_MAGIC); curlheaders = curl_slist_append(curlheaders, (const char *)node->cat); - if (curlheaders == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "could not append header to curl slist. " - "(placeholder flags)\n"); - } + if (curlheaders == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not append header to curl slist."); node = node->next; } - /* sanity-check - */ - if (curlheaders == NULL) { + /* sanity-check */ + if (curlheaders == NULL) /* above loop was probably never run */ - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "curlheaders was never populated.\n"); - } + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "curlheaders was never populated."); - /* finally, set http headers in curl handle - */ - if (curl_easy_setopt(curlh, CURLOPT_HTTPHEADER, curlheaders) != CURLE_OK) { + /* finally, set http headers in curl handle */ + if (curl_easy_setopt(curlh, CURLOPT_HTTPHEADER, curlheaders) != CURLE_OK) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "error while setting CURL option " - "(CURLOPT_HTTPHEADER). (placeholder flags)"); - } - + "error while setting CURL option (CURLOPT_HTTPHEADER)."); } /* end if should authenticate (info provided) */ /******************* @@ -1609,50 +1452,43 @@ H5FD_s3comms_s3r_read(s3r_t *handle, haddr_t offset, size_t len, void *dest) char curlerrbuf[CURL_ERROR_SIZE]; curlerrbuf[0] = '\0'; - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_ERRORBUFFER, curlerrbuf)) { + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_ERRORBUFFER, curlerrbuf)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem setting error buffer") - } p_status = curl_easy_perform(curlh); if (p_status != CURLE_OK) { - if (CURLE_OK != curl_easy_getinfo(curlh, CURLINFO_RESPONSE_CODE, &httpcode)) { + if (CURLE_OK != curl_easy_getinfo(curlh, CURLINFO_RESPONSE_CODE, &httpcode)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem getting response code") - } HDfprintf(stderr, "CURL ERROR CODE: %d\nHTTP CODE: %d\n", p_status, httpcode); HDfprintf(stderr, "%s\n", curl_easy_strerror(p_status)); - HGOTO_ERROR(H5E_VFL, H5E_CANTOPENFILE, FAIL, "problem while performing request.\n"); + HGOTO_ERROR(H5E_VFL, H5E_CANTOPENFILE, FAIL, "problem while performing request."); } - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_ERRORBUFFER, NULL)) { + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_ERRORBUFFER, NULL)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem unsetting error buffer") - } } /* verbose error reporting */ #else p_status = curl_easy_perform(curlh); - if (p_status != CURLE_OK) { - HGOTO_ERROR(H5E_VFL, H5E_CANTOPENFILE, FAIL, "curl cannot perform request\n") - } + if (p_status != CURLE_OK) + HGOTO_ERROR(H5E_VFL, H5E_CANTOPENFILE, FAIL, "curl cannot perform request") #endif #if S3COMMS_DEBUG if (dest != NULL) { HDfprintf(stderr, "len: %d\n", (int)len); HDfprintf(stderr, "CHECKING FOR BUFFER OVERFLOW\n"); - if (sds == NULL) { + if (sds == NULL) HDfprintf(stderr, "sds is NULL!\n"); - } else { HDfprintf(stderr, "sds: 0x%lx\n", (long long)sds); HDfprintf(stderr, "sds->size: %d\n", (int)sds->size); - if (len > sds->size) { + if (len > sds->size) HDfprintf(stderr, "buffer overwrite\n"); - } } } - else { + else HDfprintf(stderr, "performed on entire file\n"); - } #endif done: @@ -1672,26 +1508,22 @@ done: } if (request != NULL) { while (headers != NULL) - if (FAIL == H5FD_s3comms_hrb_node_set(&headers, headers->name, NULL)) { + if (FAIL == H5FD_s3comms_hrb_node_set(&headers, headers->name, NULL)) HDONE_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cannot release header node") - } HDassert(NULL == headers); - if (FAIL == H5FD_s3comms_hrb_destroy(&request)) { + if (FAIL == H5FD_s3comms_hrb_destroy(&request)) HDONE_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cannot release header request structure") - } HDassert(NULL == request); } if (curlh != NULL) { /* clear any Range */ - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_RANGE, NULL)) { + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_RANGE, NULL)) HDONE_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cannot unset CURLOPT_RANGE") - } /* clear headers */ - if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HTTPHEADER, NULL)) { + if (CURLE_OK != curl_easy_setopt(curlh, CURLOPT_HTTPHEADER, NULL)) HDONE_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cannot unset CURLOPT_HTTPHEADER") - } } FUNC_LEAVE_NOAPI(ret_value); @@ -1727,9 +1559,8 @@ gmnow(void) struct tm *ret_value = NULL; /* Doctor assert, checks against error in time() */ - if ((time_t)(-1) != time(now_ptr)) { - ret_value = gmtime(now_ptr); - } + if ((time_t)(-1) != HDtime(now_ptr)) + ret_value = HDgmtime(now_ptr); HDassert(ret_value != NULL); @@ -1806,77 +1637,65 @@ H5FD_s3comms_aws_canonical_request(char *canonical_request_dest, int _cr_size, c HDfprintf(stdout, "called H5FD_s3comms_aws_canonical_request.\n"); #endif - if (http_request == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "hrb object cannot be null.\n"); - } + if (http_request == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "hrb object cannot be null."); HDassert(http_request->magic == S3COMMS_HRB_MAGIC); - if (canonical_request_dest == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "canonical request destination cannot be null.\n"); - } + if (canonical_request_dest == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "canonical request destination cannot be null."); - if (signed_headers_dest == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "signed headers destination cannot be null.\n"); - } + if (signed_headers_dest == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "signed headers destination cannot be null."); - /* HTTP verb, resource path, and query string lines - */ + /* HTTP verb, resource path, and query string lines */ cr_len = (HDstrlen(http_request->verb) + HDstrlen(http_request->resource) + HDstrlen(query_params) + (size_t)3); /* three newline chars */ - if (cr_len >= cr_size) { + if (cr_len >= cr_size) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "not enough space in canonical request"); - } - ret = HDsnprintf(/* TODO: compiler warning */ - canonical_request_dest, (cr_size - 1), "%s\n%s\n%s\n", http_request->verb, + /* TODO: compiler warning */ + ret = HDsnprintf(canonical_request_dest, (cr_size - 1), "%s\n%s\n%s\n", http_request->verb, http_request->resource, query_params); - if (ret < 0 || (size_t)ret >= cr_size) { + if (ret < 0 || (size_t)ret >= cr_size) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to compose canonical request first line"); - } - /* write in canonical headers, building signed headers concurrently - */ + /* write in canonical headers, building signed headers concurrently */ node = http_request->first_header; /* assumed sorted */ while (node != NULL) { HDassert(node->magic == S3COMMS_HRB_NODE_MAGIC); ret = HDsnprintf(tmpstr, 256, "%s:%s\n", node->lowername, node->value); - if (ret < 0 || ret >= 256) { + if (ret < 0 || ret >= 256) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to concatenate HTTP header %s:%s", node->lowername, node->value); - } cr_len += HDstrlen(tmpstr); - if (cr_len + 1 > cr_size) { + if (cr_len + 1 > cr_size) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "not enough space in canonical request"); - } - strcat(canonical_request_dest, tmpstr); + HDstrcat(canonical_request_dest, tmpstr); ret = HDsnprintf(tmpstr, 256, "%s;", node->lowername); - if (ret < 0 || ret >= 256) { + if (ret < 0 || ret >= 256) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to append semicolon to lowername %s", node->lowername); - } sh_len += HDstrlen(tmpstr); - if (sh_len + 1 > sh_size) { + if (sh_len + 1 > sh_size) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "not enough space in signed headers"); - } - strcat(signed_headers_dest, tmpstr); + HDstrcat(signed_headers_dest, tmpstr); node = node->next; } /* end while node is not NULL */ - /* remove tailing ';' from signed headers sequence - */ + /* remove trailing ';' from signed headers sequence */ signed_headers_dest[HDstrlen(signed_headers_dest) - 1] = '\0'; /* append signed headers and payload hash * NOTE: at present, no HTTP body is handled, per the nature of * requests/range-gets */ - strcat(canonical_request_dest, "\n"); - strcat(canonical_request_dest, signed_headers_dest); - strcat(canonical_request_dest, "\n"); - strcat(canonical_request_dest, EMPTY_SHA256); + HDstrcat(canonical_request_dest, "\n"); + HDstrcat(canonical_request_dest, signed_headers_dest); + HDstrcat(canonical_request_dest, "\n"); + HDstrcat(canonical_request_dest, EMPTY_SHA256); done: FUNC_LEAVE_NOAPI(ret_value); @@ -1925,24 +1744,20 @@ H5FD_s3comms_bytes_to_hex(char *dest, const unsigned char *msg, size_t msg_len, HDfprintf(stdout, "called H5FD_s3comms_bytes_to_hex.\n"); #endif - if (dest == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "hex destination cannot be null.\n") - } - if (msg == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "bytes sequence cannot be null.\n") - } + if (dest == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "hex destination cannot be null.") + if (msg == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "bytes sequence cannot be null.") for (i = 0; i < msg_len; i++) { int chars_written = HDsnprintf(&(dest[i * 2]), 3, /* 'X', 'X', '\n' */ (lowercase == TRUE) ? "%02x" : "%02X", msg[i]); - if (chars_written != 2) { + if (chars_written != 2) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem while writing hex chars for %c", msg[i]); - } } done: FUNC_LEAVE_NOAPI(ret_value); - } /* end H5FD_s3comms_bytes_to_hex() */ /*---------------------------------------------------------------------------- @@ -1975,21 +1790,16 @@ H5FD_s3comms_free_purl(parsed_url_t *purl) if (purl != NULL) { HDassert(purl->magic == S3COMMS_PARSED_URL_MAGIC); - if (purl->scheme != NULL) { + if (purl->scheme != NULL) H5MM_xfree(purl->scheme); - } - if (purl->host != NULL) { + if (purl->host != NULL) H5MM_xfree(purl->host); - } - if (purl->port != NULL) { + if (purl->port != NULL) H5MM_xfree(purl->port); - } - if (purl->path != NULL) { + if (purl->path != NULL) H5MM_xfree(purl->path); - } - if (purl->query != NULL) { + if (purl->query != NULL) H5MM_xfree(purl->query); - } purl->magic += 1ul; H5MM_xfree(purl); } @@ -2041,15 +1851,13 @@ H5FD_s3comms_HMAC_SHA256(const unsigned char *key, size_t key_len, const char *m HDfprintf(stdout, "called H5FD_s3comms_HMAC_SHA256.\n"); #endif - if (dest == NULL) { + if (dest == NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "destination cannot be null."); - } HMAC(EVP_sha256(), key, (int)key_len, (const unsigned char *)msg, msg_len, md, &md_len); - if (H5FD_s3comms_bytes_to_hex(dest, (const unsigned char *)md, (size_t)md_len, true) == FAIL) { + if (H5FD_s3comms_bytes_to_hex(dest, (const unsigned char *)md, (size_t)md_len, true) == FAIL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not convert to hex string."); - } done: FUNC_LEAVE_NOAPI(ret_value); @@ -2127,42 +1935,37 @@ H5FD__s3comms_load_aws_creds_from_file(FILE *file, const char *profile_name, cha int found_setting = 0; char * line_buffer = &(buffer[0]); - FUNC_ENTER_NOAPI_NOINIT + FUNC_ENTER_STATIC #if S3COMMS_DEBUG HDfprintf(stdout, "called load_aws_creds_from_file.\n"); #endif /* format target line for start of profile */ - if (32 < HDsnprintf(profile_line, 32, "[%s]", profile_name)) { + if (32 < HDsnprintf(profile_line, 32, "[%s]", profile_name)) HGOTO_ERROR(H5E_ARGS, H5E_CANTCOPY, FAIL, "unable to format profile label") - } /* look for start of profile */ do { /* clear buffer */ - for (buffer_i = 0; buffer_i < 128; buffer_i++) { + for (buffer_i = 0; buffer_i < 128; buffer_i++) buffer[buffer_i] = 0; - } - line_buffer = fgets(line_buffer, 128, file); - if (line_buffer == NULL) { /* reached end of file */ + line_buffer = HDfgets(line_buffer, 128, file); + if (line_buffer == NULL) /* reached end of file */ goto done; - } - } while (strncmp(line_buffer, profile_line, HDstrlen(profile_line))); + } while (HDstrncmp(line_buffer, profile_line, HDstrlen(profile_line))); /* extract credentials from lines */ do { /* clear buffer */ - for (buffer_i = 0; buffer_i < 128; buffer_i++) { + for (buffer_i = 0; buffer_i < 128; buffer_i++) buffer[buffer_i] = 0; - } /* collect a line from file */ - line_buffer = fgets(line_buffer, 128, file); - if (line_buffer == NULL) { + line_buffer = HDfgets(line_buffer, 128, file); + if (line_buffer == NULL) goto done; /* end of file */ - } /* loop over names to see if line looks like assignment */ for (setting_i = 0; setting_i < setting_count; setting_i++) { @@ -2172,40 +1975,33 @@ H5FD__s3comms_load_aws_creds_from_file(FILE *file, const char *profile_name, cha setting_name = setting_names[setting_i]; setting_name_len = HDstrlen(setting_name); - if (HDsnprintf(line_prefix, 128, "%s=", setting_name) < 0) { + if (HDsnprintf(line_prefix, 128, "%s=", setting_name) < 0) HGOTO_ERROR(H5E_ARGS, H5E_CANTCOPY, FAIL, "unable to format line prefix") - } /* found a matching name? */ if (!HDstrncmp(line_buffer, line_prefix, setting_name_len + 1)) { found_setting = 1; /* skip NULL destination buffer */ - if (setting_pointers[setting_i] == NULL) { + if (setting_pointers[setting_i] == NULL) break; - } /* advance to end of name in string */ do { line_buffer++; } while (*line_buffer != 0 && *line_buffer != '='); - if (*line_buffer == 0 || *(line_buffer + 1) == 0) { + if (*line_buffer == 0 || *(line_buffer + 1) == 0) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "incomplete assignment in file") - } line_buffer++; /* was pointing at '='; advance */ /* copy line buffer into out pointer */ - if (HDstrncpy(setting_pointers[setting_i], (const char *)line_buffer, - HDstrlen(line_buffer)) == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_CANTCOPY, FAIL, "unable to copy line into pointer") - } + HDstrncpy(setting_pointers[setting_i], (const char *)line_buffer, HDstrlen(line_buffer)); /* "trim" tailing whitespace by replacing with null terminator*/ buffer_i = 0; - while (!isspace(setting_pointers[setting_i][buffer_i])) { + while (!HDisspace(setting_pointers[setting_i][buffer_i])) buffer_i++; - } setting_pointers[setting_i][buffer_i] = '\0'; break; /* have read setting; don't compare with others */ @@ -2266,59 +2062,50 @@ H5FD_s3comms_load_aws_profile(const char *profile_name, char *key_id_out, char * #endif #ifdef H5_HAVE_WIN32_API - ret = HDsnprintf(awspath, 117, "%s/.aws/", getenv("USERPROFILE")); + ret = HDsnprintf(awspath, 117, "%s/.aws/", HDgetenv("USERPROFILE")); #else - ret = HDsnprintf(awspath, 117, "%s/.aws/", getenv("HOME")); + ret = HDsnprintf(awspath, 117, "%s/.aws/", HDgetenv("HOME")); #endif - if (ret < 0 || (size_t)ret >= 117) { + if (ret < 0 || (size_t)ret >= 117) HGOTO_ERROR(H5E_ARGS, H5E_CANTCOPY, FAIL, "unable to format home-aws path") - } ret = HDsnprintf(filepath, 128, "%s%s", awspath, "credentials"); - if (ret < 0 || (size_t)ret >= 128) { + if (ret < 0 || (size_t)ret >= 128) HGOTO_ERROR(H5E_ARGS, H5E_CANTCOPY, FAIL, "unable to format credentials path") - } - credfile = fopen(filepath, "r"); + credfile = HDfopen(filepath, "r"); if (credfile != NULL) { if (H5FD__s3comms_load_aws_creds_from_file(credfile, profile_name, key_id_out, secret_access_key_out, - aws_region_out) == FAIL) { + aws_region_out) == FAIL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to load from aws credentials") - } - if (fclose(credfile) == EOF) { + if (HDfclose(credfile) == EOF) HGOTO_ERROR(H5E_FILE, H5E_CANTCLOSEFILE, FAIL, "unable to close credentials file") - } credfile = NULL; } /* end if credential file opened */ ret = HDsnprintf(filepath, 128, "%s%s", awspath, "config"); - if (ret < 0 || (size_t)ret >= 128) { + if (ret < 0 || (size_t)ret >= 128) HGOTO_ERROR(H5E_ARGS, H5E_CANTCOPY, FAIL, "unable to format config path") - } - credfile = fopen(filepath, "r"); + credfile = HDfopen(filepath, "r"); if (credfile != NULL) { if (H5FD__s3comms_load_aws_creds_from_file( credfile, profile_name, (*key_id_out == 0) ? key_id_out : NULL, (*secret_access_key_out == 0) ? secret_access_key_out : NULL, - (*aws_region_out == 0) ? aws_region_out : NULL) == FAIL) { + (*aws_region_out == 0) ? aws_region_out : NULL) == FAIL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unable to load from aws config") - } - if (fclose(credfile) == EOF) { + if (HDfclose(credfile) == EOF) HGOTO_ERROR(H5E_FILE, H5E_CANTCLOSEFILE, FAIL, "unable to close config file") - } credfile = NULL; } /* end if credential file opened */ /* fail if not all three settings were loaded */ - if (*key_id_out == 0 || *secret_access_key_out == 0 || *aws_region_out == 0) { + if (*key_id_out == 0 || *secret_access_key_out == 0 || *aws_region_out == 0) ret_value = FAIL; - } done: - if (credfile != NULL) { - if (fclose(credfile) == EOF) { + if (credfile != NULL) + if (HDfclose(credfile) == EOF) HDONE_ERROR(H5E_ARGS, H5E_ARGS, FAIL, "problem error-closing aws configuration file") - } - } + FUNC_LEAVE_NOAPI(ret_value); } /* end H5FD_s3comms_load_aws_profile() */ @@ -2359,15 +2146,14 @@ H5FD_s3comms_nlowercase(char *dest, const char *s, size_t len) HDfprintf(stdout, "called H5FD_s3comms_nlowercase.\n"); #endif - if (dest == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "destination cannot be null.\n"); - } + if (dest == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "destination cannot be null."); if (len > 0) { - HDmemcpy(dest, s, len); + H5MM_memcpy(dest, s, len); do { len--; - dest[len] = (char)tolower((int)dest[len]); + dest[len] = (char)HDtolower((int)dest[len]); } while (len > 0); } @@ -2423,16 +2209,14 @@ H5FD_s3comms_parse_url(const char *str, parsed_url_t **_purl) HDprintf("called H5FD_s3comms_parse_url.\n"); #endif - if (str == NULL || *str == '\0') { + if (str == NULL || *str == '\0') HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid url string"); - } urllen = (long int)HDstrlen(str); purl = (parsed_url_t *)H5MM_malloc(sizeof(parsed_url_t)); - if (purl == NULL) { + if (purl == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "can't allocate space for parsed_url_t"); - } purl->magic = S3COMMS_PARSED_URL_MAGIC; purl->scheme = NULL; purl->host = NULL; @@ -2445,31 +2229,26 @@ H5FD_s3comms_parse_url(const char *str, parsed_url_t **_purl) ***************/ tmpstr = HDstrchr(curstr, ':'); - if (tmpstr == NULL) { + if (tmpstr == NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid SCHEME construction: probably not URL"); - } len = tmpstr - curstr; HDassert((0 <= len) && (len < urllen)); - /* check for restrictions - */ + /* check for restrictions */ for (i = 0; i < len; i++) { /* scheme = [a-zA-Z+-.]+ (terminated by ":") */ - if (!isalpha(curstr[i]) && '+' != curstr[i] && '-' != curstr[i] && '.' != curstr[i]) { + if (!HDisalpha(curstr[i]) && '+' != curstr[i] && '-' != curstr[i] && '.' != curstr[i]) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid SCHEME construction"); - } } - /* copy lowercased scheme to structure - */ + + /* copy lowercased scheme to structure */ purl->scheme = (char *)H5MM_malloc(sizeof(char) * (size_t)(len + 1)); - if (purl->scheme == NULL) { + if (purl->scheme == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "can't allocate space for SCHEME"); - } - (void)HDstrncpy(purl->scheme, curstr, (size_t)len); + HDstrncpy(purl->scheme, curstr, (size_t)len); purl->scheme[len] = '\0'; - for (i = 0; i < len; i++) { - purl->scheme[i] = (char)tolower(purl->scheme[i]); - } + for (i = 0; i < len; i++) + purl->scheme[i] = (char)HDtolower(purl->scheme[i]); /* Skip "://" */ tmpstr += 3; @@ -2482,36 +2261,31 @@ H5FD_s3comms_parse_url(const char *str, parsed_url_t **_purl) if (*curstr == '[') { /* IPv6 */ while (']' != *tmpstr) { - if (tmpstr == 0) { /* end of string reached! */ + /* end of string reached! */ + if (tmpstr == 0) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "reached end of URL: incomplete IPv6 HOST"); - } tmpstr++; } tmpstr++; } /* end if (IPv6) */ else { while (0 != *tmpstr) { - if (':' == *tmpstr || '/' == *tmpstr || '?' == *tmpstr) { + if (':' == *tmpstr || '/' == *tmpstr || '?' == *tmpstr) break; - } tmpstr++; } } /* end else (IPv4) */ len = tmpstr - curstr; - if (len == 0) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "HOST substring cannot be empty"); - } - else if (len > urllen) { + if (len == 0) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "HOST substring cannot be empty") + else if (len > urllen) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem with length of HOST substring"); - } - /* copy host - */ + /* copy host */ purl->host = (char *)H5MM_malloc(sizeof(char) * (size_t)(len + 1)); - if (purl->host == NULL) { + if (purl->host == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "can't allocate space for HOST"); - } - (void)HDstrncpy(purl->host, curstr, (size_t)len); + HDstrncpy(purl->host, curstr, (size_t)len); purl->host[len] = 0; /************* @@ -2521,29 +2295,22 @@ H5FD_s3comms_parse_url(const char *str, parsed_url_t **_purl) if (':' == *tmpstr) { tmpstr += 1; /* advance past ':' */ curstr = tmpstr; - while ((0 != *tmpstr) && ('/' != *tmpstr) && ('?' != *tmpstr)) { + while ((0 != *tmpstr) && ('/' != *tmpstr) && ('?' != *tmpstr)) tmpstr++; - } len = tmpstr - curstr; - if (len == 0) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "PORT element cannot be empty"); - } - else if (len > urllen) { + if (len == 0) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "PORT element cannot be empty") + else if (len > urllen) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem with length of PORT substring"); - } - for (i = 0; i < len; i++) { - if (!isdigit(curstr[i])) { + for (i = 0; i < len; i++) + if (!HDisdigit(curstr[i])) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "PORT is not a decimal string"); - } - } - /* copy port - */ + /* copy port */ purl->port = (char *)H5MM_malloc(sizeof(char) * (size_t)(len + 1)); - if (purl->port == NULL) { /* cannot malloc */ + if (purl->port == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "can't allocate space for PORT"); - } - (void)HDstrncpy(purl->port, curstr, (size_t)len); + HDstrncpy(purl->port, curstr, (size_t)len); purl->port[len] = 0; } /* end if PORT element */ @@ -2556,21 +2323,17 @@ H5FD_s3comms_parse_url(const char *str, parsed_url_t **_purl) tmpstr += 1; curstr = tmpstr; - /* seek end of PATH - */ - while ((0 != *tmpstr) && ('?' != *tmpstr)) { + /* seek end of PATH */ + while ((0 != *tmpstr) && ('?' != *tmpstr)) tmpstr++; - } len = tmpstr - curstr; - if (len > urllen) { + if (len > urllen) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem with length of PATH substring"); - } if (len > 0) { purl->path = (char *)H5MM_malloc(sizeof(char) * (size_t)(len + 1)); - if (purl->path == NULL) { + if (purl->path == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "can't allocate space for PATH"); - } /* cannot malloc path pointer */ - (void)HDstrncpy(purl->path, curstr, (size_t)len); + HDstrncpy(purl->path, curstr, (size_t)len); purl->path[len] = 0; } } /* end if PATH element */ @@ -2582,21 +2345,17 @@ H5FD_s3comms_parse_url(const char *str, parsed_url_t **_purl) if ('?' == *tmpstr) { tmpstr += 1; curstr = tmpstr; - while (0 != *tmpstr) { + while (0 != *tmpstr) tmpstr++; - } len = tmpstr - curstr; - if (len == 0) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "QUERY cannot be empty"); - } - else if (len > urllen) { + if (len == 0) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "QUERY cannot be empty") + else if (len > urllen) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem with length of QUERY substring"); - } purl->query = (char *)H5MM_malloc(sizeof(char) * (size_t)(len + 1)); - if (purl->query == NULL) { + if (purl->query == NULL) HGOTO_ERROR(H5E_ARGS, H5E_CANTALLOC, FAIL, "can't allocate space for QUERY"); - } /* cannot malloc path pointer */ - (void)HDstrncpy(purl->query, curstr, (size_t)len); + HDstrncpy(purl->query, curstr, (size_t)len); purl->query[len] = 0; } /* end if QUERY exists */ @@ -2604,9 +2363,9 @@ H5FD_s3comms_parse_url(const char *str, parsed_url_t **_purl) ret_value = SUCCEED; done: - if (ret_value == FAIL) { + if (ret_value == FAIL) H5FD_s3comms_free_purl(purl); - } + FUNC_LEAVE_NOAPI(ret_value); } /* end H5FD_s3comms_parse_url() */ @@ -2666,9 +2425,8 @@ H5FD_s3comms_percent_encode_char(char *repr, const unsigned char c, size_t *repr HDfprintf(stdout, "called H5FD_s3comms_percent_encode_char.\n"); #endif - if (repr == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "no destination `repr`.\n") - } + if (repr == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "no destination `repr`.") #if S3COMMS_DEBUG H5FD_s3comms_bytes_to_hex((char *)hex, s, 1, FALSE); @@ -2685,9 +2443,8 @@ H5FD_s3comms_percent_encode_char(char *repr, const unsigned char c, size_t *repr #endif *repr_len = 3; chars_written = HDsnprintf(repr, 4, "%%%02X", c); - if (chars_written < 0) { + if (chars_written < 0) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cannot write char %c", c); - } } /* end if single-byte unicode char */ else { /* multi-byte, multi-percent representation @@ -2737,9 +2494,8 @@ H5FD_s3comms_percent_encode_char(char *repr, const unsigned char c, size_t *repr acc += (stack_size > 3) ? 0x10 : 0; /* 0x00010000 */ stack_size--; chars_written = HDsnprintf(repr, 4, "%%%02X", (unsigned char)(acc + stack[stack_size])); - if (chars_written < 0) { + if (chars_written < 0) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cannot write char %c", c); - } *repr_len += 3; /************************ @@ -2750,9 +2506,8 @@ H5FD_s3comms_percent_encode_char(char *repr, const unsigned char c, size_t *repr for (i = 0; i < stack_size; i++) { chars_written = HDsnprintf(&repr[i * 3 + 3], 4, "%%%02X", (unsigned char)(0x80 + stack[stack_size - 1 - i])); - if (chars_written < 0) { + if (chars_written < 0) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cannot write char %c", c); - } *repr_len += 3; } /* end for each continuation byte */ } /* end else (multi-byte) */ @@ -2818,31 +2573,24 @@ H5FD_s3comms_signing_key(unsigned char *md, const char *secret, const char *regi HDfprintf(stdout, "called H5FD_s3comms_signing_key.\n"); #endif - if (md == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "Destination `md` cannot be NULL.\n") - } - if (secret == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "`secret` cannot be NULL.\n") - } - if (region == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "`region` cannot be NULL.\n") - } - if (iso8601now == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "`iso8601now` cannot be NULL.\n") - } + if (md == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "Destination `md` cannot be NULL.") + if (secret == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "`secret` cannot be NULL.") + if (region == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "`region` cannot be NULL.") + if (iso8601now == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "`iso8601now` cannot be NULL.") AWS4_secret_len = 4 + HDstrlen(secret) + 1; AWS4_secret = (char *)H5MM_malloc(sizeof(char *) * AWS4_secret_len); - if (AWS4_secret == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "Could not allocate space.\n") - } + if (AWS4_secret == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "Could not allocate space.") - /* prepend "AWS4" to start of the secret key - */ + /* prepend "AWS4" to start of the secret key */ ret = HDsnprintf(AWS4_secret, AWS4_secret_len, "%s%s", "AWS4", secret); - if ((size_t)ret != (AWS4_secret_len - 1)) { + if ((size_t)ret != (AWS4_secret_len - 1)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem writing AWS4+secret `%s`", secret); - } /* hash_func, key, len(key), msg, len(msg), digest_dest, digest_len_dest * we know digest length, so ignore via NULL @@ -2917,22 +2665,17 @@ H5FD_s3comms_tostringtosign(char *dest, const char *req, const char *now, const HDfprintf(stdout, "called H5FD_s3comms_tostringtosign.\n"); #endif - if (dest == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "destination buffer cannot be null.\n") - } - if (req == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "canonical request cannot be null.\n") - } - if (now == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "Timestring cannot be NULL.\n") - } - if (region == NULL) { - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "Region cannot be NULL.\n") - } + if (dest == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "destination buffer cannot be null.") + if (req == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "canonical request cannot be null.") + if (now == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "Timestring cannot be NULL.") + if (region == NULL) + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "Region cannot be NULL.") - for (i = 0; i < 128; i++) { + for (i = 0; i < 128; i++) tmp[i] = '\0'; - } for (i = 0; i < SHA256_DIGEST_LENGTH * 2 + 1; i++) { checksum[i] = '\0'; hexsum[i] = '\0'; @@ -2940,31 +2683,28 @@ H5FD_s3comms_tostringtosign(char *dest, const char *req, const char *now, const HDstrncpy(day, now, 8); day[8] = '\0'; ret = HDsnprintf(tmp, 127, "%s/%s/s3/aws4_request", day, region); - if (ret <= 0 || ret >= 127) { + if (ret <= 0 || ret >= 127) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "problem adding day and region to string") - } - HDmemcpy((dest + d), "AWS4-HMAC-SHA256\n", 17); + H5MM_memcpy((dest + d), "AWS4-HMAC-SHA256\n", 17); d = 17; - HDmemcpy((dest + d), now, HDstrlen(now)); + H5MM_memcpy((dest + d), now, HDstrlen(now)); d += HDstrlen(now); dest[d++] = '\n'; - HDmemcpy((dest + d), tmp, HDstrlen(tmp)); + H5MM_memcpy((dest + d), tmp, HDstrlen(tmp)); d += HDstrlen(tmp); dest[d++] = '\n'; SHA256((const unsigned char *)req, HDstrlen(req), checksum); if (H5FD_s3comms_bytes_to_hex(hexsum, (const unsigned char *)checksum, SHA256_DIGEST_LENGTH, true) == - FAIL) { + FAIL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "could not create hex string"); - } - for (i = 0; i < SHA256_DIGEST_LENGTH * 2; i++) { + for (i = 0; i < SHA256_DIGEST_LENGTH * 2; i++) dest[d++] = hexsum[i]; - } dest[d] = '\0'; @@ -3012,18 +2752,16 @@ H5FD_s3comms_trim(char *dest, char *s, size_t s_len, size_t *n_written) HDfprintf(stdout, "called H5FD_s3comms_trim.\n"); #endif - if (dest == NULL) { + if (dest == NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "destination cannot be null.") - } - if (s == NULL) { + if (s == NULL) s_len = 0; - } if (s_len > 0) { /* Find first non-whitespace character from start; * reduce total length per character. */ - while ((s_len > 0) && isspace((unsigned char)s[0]) && s_len > 0) { + while ((s_len > 0) && HDisspace((unsigned char)s[0]) && s_len > 0) { s++; s_len--; } @@ -3035,12 +2773,11 @@ H5FD_s3comms_trim(char *dest, char *s, size_t s_len, size_t *n_written) if (s_len > 0) { do { s_len--; - } while (isspace((unsigned char)s[s_len])); + } while (HDisspace((unsigned char)s[s_len])); s_len++; - /* write output into dest - */ - HDmemcpy(dest, s, s_len); + /* write output into dest */ + H5MM_memcpy(dest, s, s_len); } } @@ -3105,12 +2842,10 @@ H5FD_s3comms_uriencode(char *dest, const char *s, size_t s_len, hbool_t encode_s HDfprintf(stdout, "H5FD_s3comms_uriencode called.\n"); #endif - if (s == NULL) { + if (s == NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "source string cannot be NULL"); - } - if (dest == NULL) { + if (dest == NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "destination cannot be NULL"); - } /* Write characters to destination, converting to percent-encoded * "hex-utf-8" strings if necessary. @@ -3118,10 +2853,9 @@ H5FD_s3comms_uriencode(char *dest, const char *s, size_t s_len, hbool_t encode_s */ for (s_off = 0; s_off < s_len; s_off++) { c = s[s_off]; - if (isalnum(c) || c == '.' || c == '-' || c == '_' || c == '~' || - (c == '/' && encode_slash == FALSE)) { + if (HDisalnum(c) || c == '.' || c == '-' || c == '_' || c == '~' || + (c == '/' && encode_slash == FALSE)) dest[dest_off++] = c; - } else { hex_off = 0; if (H5FD_s3comms_percent_encode_char(hex_buffer, (const unsigned char)c, &hex_len) == FAIL) { @@ -3133,9 +2867,8 @@ H5FD_s3comms_uriencode(char *dest, const char *s, size_t s_len, hbool_t encode_s hex_buffer, (int)s_off, s); } - for (hex_off = 0; hex_off < hex_len; hex_off++) { + for (hex_off = 0; hex_off < hex_len; hex_off++) dest[dest_off++] = hex_buffer[hex_off]; - } } /* end else (not a regular character) */ } /* end for each character */ |