From f931c2aed2d9b69f2847551d312ad21a901ad688 Mon Sep 17 00:00:00 2001 From: Dana Robinson <43805+derobins@users.noreply.github.com> Date: Wed, 19 Jun 2024 12:24:13 -0700 Subject: [PATCH] Removed unused code from H5FDs3comms.c (#4588) * H5FD_s3comms_nlowercase() * H5FD_s3comms_trim() * H5FD_s3comms_uriencode() --- src/H5FDs3comms.c | 199 ------------------------------- src/H5FDs3comms.h | 8 -- test/s3comms.c | 297 ---------------------------------------------- 3 files changed, 504 deletions(-) diff --git a/src/H5FDs3comms.c b/src/H5FDs3comms.c index 9b0d786dfe7..26691318e60 100644 --- a/src/H5FDs3comms.c +++ b/src/H5FDs3comms.c @@ -1967,51 +1967,6 @@ H5FD_s3comms_load_aws_profile(const char *profile_name, char *key_id_out, char * FUNC_LEAVE_NOAPI(ret_value) } /* end H5FD_s3comms_load_aws_profile() */ -/*---------------------------------------------------------------------------- - * - * Function: H5FD_s3comms_nlowercase() - * - * Purpose: - * - * From string starting at `s`, write `len` characters to `dest`, - * converting all to lowercase. - * - * Behavior is undefined if `s` is NULL or `len` overruns the allocated - * space of either `s` or `dest`. - * - * Provided as convenience. - * - * Return: - * - * - SUCCESS: `SUCCEED` - * - upon completion, `dest` is populated - * - FAILURE: `FAIL` - * - `dest == NULL` - * - *---------------------------------------------------------------------------- - */ -herr_t -H5FD_s3comms_nlowercase(char *dest, const char *s, size_t len) -{ - herr_t ret_value = SUCCEED; - - FUNC_ENTER_NOAPI_NOINIT - - if (dest == NULL) - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "destination cannot be null."); - - if (len > 0) { - H5MM_memcpy(dest, s, len); - do { - len--; - dest[len] = (char)tolower((int)dest[len]); - } while (len > 0); - } - -done: - FUNC_LEAVE_NOAPI(ret_value) -} /* end H5FD_s3comms_nlowercase() */ - /*---------------------------------------------------------------------------- * * Function: H5FD_s3comms_parse_url() @@ -2511,158 +2466,4 @@ H5FD_s3comms_tostringtosign(char *dest, const char *req, const char *now, const FUNC_LEAVE_NOAPI(ret_value) } /* end H5ros3_tostringtosign() */ -/*---------------------------------------------------------------------------- - * - * Function: H5FD_s3comms_trim() - * - * Purpose: - * - * Remove all whitespace characters from start and end of a string `s` - * of length `s_len`, writing trimmed string copy to `dest`. - * Stores number of characters remaining at `n_written`. - * - * Destination for trimmed copy `dest` cannot be null. - * `dest` must have adequate space allocated for trimmed copy. - * If inadequate space, behavior is undefined, possibly resulting - * in segfault or overwrite of other data. - * - * If `s` is NULL or all whitespace, `dest` is untouched and `n_written` - * is set to 0. - * - * Return: - * - * - SUCCESS: `SUCCEED` - * - FAILURE: `FAIL` - * - `dest == NULL` - * - *---------------------------------------------------------------------------- - */ -herr_t -H5FD_s3comms_trim(char *dest, char *s, size_t s_len, size_t *n_written) -{ - herr_t ret_value = SUCCEED; - - FUNC_ENTER_NOAPI_NOINIT - - if (dest == NULL) - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "destination cannot be 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++; - s_len--; - } - - /* Find first non-whitespace character from tail; - * reduce length per-character. - * If length is 0 already, there is no non-whitespace character. - */ - if (s_len > 0) { - do { - s_len--; - } while (isspace((unsigned char)s[s_len])); - s_len++; - - /* write output into dest */ - H5MM_memcpy(dest, s, s_len); - } - } - - *n_written = s_len; - -done: - FUNC_LEAVE_NOAPI(ret_value) -} /* end H5FD_s3comms_trim() */ - -/*---------------------------------------------------------------------------- - * - * Function: H5FD_s3comms_uriencode() - * - * Purpose: - * - * URIencode (percent-encode) every byte except "[a-zA-Z0-9]-._~". - * - * For each character in source string `_s` from `s[0]` to `s[s_len-1]`, - * writes to `dest` either the raw character or its percent-encoded - * equivalent. - * - * See `H5FD_s3comms_bytes_to_hex` for information on percent-encoding. - * - * Space (' ') character encoded as "%20" (not "+") - * - * Forward-slash ('/') encoded as "%2F" only when `encode_slash == true`. - * - * Records number of characters written at `n_written`. - * - * Assumes that `dest` has been allocated with enough space. - * - * Neither `dest` nor `s` can be NULL. - * - * `s_len == 0` will have no effect. - * - * Return: - * - * - SUCCESS: `SUCCEED` - * - FAILURE: `FAIL` - * - source strings `s` or destination `dest` are NULL - * - error while attempting to percent-encode a character - * - *---------------------------------------------------------------------------- - */ -herr_t -H5FD_s3comms_uriencode(char *dest, const char *s, size_t s_len, bool encode_slash, size_t *n_written) -{ - char c = 0; - size_t dest_off = 0; - char hex_buffer[13]; - size_t hex_off = 0; - size_t hex_len = 0; - herr_t ret_value = SUCCEED; - size_t s_off = 0; - - FUNC_ENTER_NOAPI_NOINIT - - if (s == NULL) - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "source string cannot be 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. - * e.g., '$' -> "%24" - */ - 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)) - dest[dest_off++] = c; - else { - hex_off = 0; - if (H5FD_s3comms_percent_encode_char(hex_buffer, (const unsigned char)c, &hex_len) == FAIL) { - hex_buffer[0] = c; - hex_buffer[1] = 0; - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, - "unable to percent-encode character \'%s\' " - "at %d in \"%s\"", - hex_buffer, (int)s_off, s); - } - - 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 */ - - if (dest_off < s_len) - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "buffer overflow"); - - *n_written = dest_off; - -done: - FUNC_LEAVE_NOAPI(ret_value) -} /* H5FD_s3comms_uriencode */ - #endif /* H5_HAVE_ROS3_VFD */ diff --git a/src/H5FDs3comms.h b/src/H5FDs3comms.h index 120a71a9c85..d523928efc6 100644 --- a/src/H5FDs3comms.h +++ b/src/H5FDs3comms.h @@ -533,8 +533,6 @@ H5_DLL herr_t H5FD_s3comms_HMAC_SHA256(const unsigned char *key, size_t key_len, H5_DLL herr_t H5FD_s3comms_load_aws_profile(const char *name, char *key_id_out, char *secret_access_key_out, char *aws_region_out); -H5_DLL herr_t H5FD_s3comms_nlowercase(char *dest, const char *s, size_t len); - H5_DLL herr_t H5FD_s3comms_parse_url(const char *str, parsed_url_t **purl); H5_DLL herr_t H5FD_s3comms_percent_encode_char(char *repr, const unsigned char c, size_t *repr_len); @@ -544,12 +542,6 @@ H5_DLL herr_t H5FD_s3comms_signing_key(unsigned char *md, const char *secret, co H5_DLL herr_t H5FD_s3comms_tostringtosign(char *dest, const char *req_str, const char *now, const char *region); - -H5_DLL herr_t H5FD_s3comms_trim(char *dest, char *s, size_t s_len, size_t *n_written); - -H5_DLL herr_t H5FD_s3comms_uriencode(char *dest, const char *s, size_t s_len, bool encode_slash, - size_t *n_written); - #ifdef __cplusplus } #endif diff --git a/test/s3comms.c b/test/s3comms.c index 40f163e8ef2..e08da8e99b1 100644 --- a/test/s3comms.c +++ b/test/s3comms.c @@ -1255,80 +1255,6 @@ test_HMAC_SHA256(void) } /* end test_HMAC_SHA256() */ -/*---------------------------------------------------------------------------- - * - * Function: test_nlowercase() - * - * Purpose: - * - * Define and verify behavior of `H5FD_s3comms_nlowercase()` - * - *---------------------------------------------------------------------------- - */ -static herr_t -test_nlowercase(void) -{ - /************************* - * test-local structures * - *************************/ - - struct testcase { - const char *in; - size_t len; - const char *exp; - }; - - /************************ - * test-local variables * - ************************/ - - /* any character after in exp on or after exp[len] is undefined. - * in this test, kept as the null character for simplicity. - */ - struct testcase cases[] = { - { - "HALlEluJAh", - 6, - "hallel", - }, - { - "all\0 lower", - 10, - "all\0 lower", - }, - { - "to meeeeeee", - 0, - "", - }, - }; - char *dest = NULL; - int i = 0; - int n_cases = 3; - - TESTING("nlowercase"); - - for (i = 0; i < n_cases; i++) { - dest = (char *)malloc(sizeof(char) * 16); - - JSVERIFY(SUCCEED, H5FD_s3comms_nlowercase(dest, cases[i].in, cases[i].len), cases[i].in) - if (cases[i].len > 0) { - JSVERIFY(0, strncmp(dest, cases[i].exp, cases[i].len), NULL) - } - free(dest); - } /* end for each testcase */ - - JSVERIFY(FAIL, H5FD_s3comms_nlowercase(NULL, cases[0].in, cases[0].len), "null destination should fail") - - PASSED(); - return 0; - -error: - free(dest); - return -1; - -} /* end test_nlowercase() */ - /*--------------------------------------------------------------------------- * * Function: test_parse_url() @@ -2252,226 +2178,6 @@ test_tostringtosign(void) } /* end test_tostringtosign() */ -/*---------------------------------------------------------------------------- - * - * Function: test_trim() - * - * Purpose: - * - * Define and verify behavior of `H5FD_s3comms_trim()`. - * - *---------------------------------------------------------------------------- - */ -static herr_t -test_trim(void) -{ - /************************* - * test-local structures * - *************************/ - - struct testcase { - const char *in; - size_t in_len; - const char *exp; - size_t exp_len; - }; - - /************************ - * test-local variables * - ************************/ - - struct testcase cases[] = { - { - "block string", - 12, - "block string", - 12, - }, - { - " \n\r \t", - 6, - "", - 0, - }, - { - " \twhite b4", - 10, - "white b4", - 8, - }, - { - "white after\r\n ", - 15, - "white after", - 11, - }, - { - " on\nends\t", - 9, - "on\nends", - 7, - }, - }; - char dest[32]; - size_t dest_len = 0; - int i = 0; - int n_cases = 5; - char *str = NULL; - - TESTING("s3comms trim"); - - for (i = 0; i < n_cases; i++) { - assert(str == NULL); - str = (char *)malloc(sizeof(char) * cases[i].in_len); - assert(str != NULL); - strncpy(str, cases[i].in, cases[i].in_len); - - JSVERIFY(SUCCEED, H5FD_s3comms_trim(dest, str, cases[i].in_len, &dest_len), NULL) - JSVERIFY(cases[i].exp_len, dest_len, cases[i].in) - if (dest_len > 0) { - JSVERIFY(0, strncmp(cases[i].exp, dest, dest_len), cases[i].exp) - } - free(str); - str = NULL; - } /* end for each testcase */ - - JSVERIFY(SUCCEED, H5FD_s3comms_trim(dest, NULL, 3, &dest_len), - "should not fail when trimming a null string"); - JSVERIFY(0, dest_len, "trimming NULL string writes 0 characters") - - assert(str == NULL); - str = (char *)malloc(sizeof(char *) * 11); - assert(str != NULL); - memcpy(str, "some text ", 11); /* string with null terminator */ - JSVERIFY(FAIL, H5FD_s3comms_trim(NULL, str, 10, &dest_len), "destination for trim cannot be NULL"); - free(str); - str = NULL; - - PASSED(); - return 0; - -error: - if (str != NULL) { - free(str); - } - return -1; - -} /* end test_trim() */ - -/*---------------------------------------------------------------------------- - * - * Function: test_uriencode() - * - * Purpose: - * - * Define and verify behavior of `H5FD_s3comms_uriencode()`. - * - *---------------------------------------------------------------------------- - */ -static herr_t -test_uriencode(void) -{ - /************************* - * test-local structures * - *************************/ - - struct testcase { - const char *str; - size_t s_len; - bool encode_slash; - const char *expected; - }; - - /************************ - * test-local variables * - ************************/ - - struct testcase cases[] = {{ - "/path/to/resource.jpg", - 21, - false, - "/path/to/resource.jpg", - }, - { - "/path/to/resource.jpg", - 21, - true, - "%2Fpath%2Fto%2Fresource.jpg", - }, - { - "string got_spaa ces", - 20, - true, - "string%20got_spaa%20%20ces", - }, - { - "sp ac~es/and-sl ash.encoded", - 27, - true, - "sp%20ac~es%2Fand-sl%20ash.encoded", - }, - { - "sp ac~es/and-sl ash.unencoded", - 29, - false, - "sp%20ac~es/and-sl%20ash.unencoded", - }, - { - "/path/to/resource.txt", - 0, - false, - "", - - }}; - char *dest = NULL; - size_t dest_written = 0; - int i = 0; - int ncases = 6; - size_t str_len = 0; - - TESTING("s3comms uriencode"); - - for (i = 0; i < ncases; i++) { - str_len = cases[i].s_len; - dest = (char *)malloc(sizeof(char) * str_len * 3 + 1); - FAIL_IF(dest == NULL) - - JSVERIFY(SUCCEED, - H5FD_s3comms_uriencode(dest, cases[i].str, str_len, cases[i].encode_slash, &dest_written), - NULL); - JSVERIFY(strlen(cases[i].expected), dest_written, NULL) - JSVERIFY(0, strncmp(dest, cases[i].expected, dest_written), cases[i].expected); - - free(dest); - dest = NULL; - } /* end for each testcase */ - - /*************** - * ERROR CASES * - ***************/ - - dest = (char *)malloc(sizeof(char) * 15); - assert(dest != NULL); - - JSVERIFY(FAIL, H5FD_s3comms_uriencode(NULL, "word$", 5, false, &dest_written), - "destination cannot be NULL"); - JSVERIFY(FAIL, H5FD_s3comms_uriencode(dest, NULL, 5, false, &dest_written), - "source string cannot be NULL"); - - free(dest); - dest = NULL; - - PASSED(); - return 0; - -error: - if (dest != NULL) { - free(dest); - } - return -1; - -} /* end test_uriencode() */ - #endif /* H5_HAVE_ROS3_VFD */ /*------------------------------------------------------------------------- @@ -2532,9 +2238,6 @@ main(void) /* tests ordered roughly by dependence */ nerrors += test_macro_format_credential() < 0 ? 1 : 0; - nerrors += test_trim() < 0 ? 1 : 0; - nerrors += test_nlowercase() < 0 ? 1 : 0; - nerrors += test_uriencode() < 0 ? 1 : 0; nerrors += test_percent_encode_char() < 0 ? 1 : 0; nerrors += test_bytes_to_hex() < 0 ? 1 : 0; nerrors += test_HMAC_SHA256() < 0 ? 1 : 0;