From 9fe7132fe3c7f5fb85ed01c8ab16dc12f755450a Mon Sep 17 00:00:00 2001 From: Dana Robinson <43805+derobins@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:22:21 -0700 Subject: [PATCH] Remove dead H5FD_s3comms_percent_encode_char() (#4591) --- src/H5FDs3comms.c | 118 ---------------------------------------------- src/H5FDs3comms.h | 2 - test/s3comms.c | 72 ---------------------------- 3 files changed, 192 deletions(-) diff --git a/src/H5FDs3comms.c b/src/H5FDs3comms.c index d0577fa5e78..4246263a551 100644 --- a/src/H5FDs3comms.c +++ b/src/H5FDs3comms.c @@ -2110,124 +2110,6 @@ H5FD_s3comms_parse_url(const char *str, parsed_url_t **_purl) FUNC_LEAVE_NOAPI(ret_value) } /* end H5FD_s3comms_parse_url() */ -/*---------------------------------------------------------------------------- - * - * Function: H5FD_s3comms_percent_encode_char() - * - * Purpose: - * - * "Percent-encode" utf-8 character `c`, e.g., - * '$' -> "%24" - * '¢' -> "%C2%A2" - * - * `c` cannot be null. - * - * Does not (currently) accept multi-byte characters... - * limit to (?) u+00ff, well below upper bound for two-byte utf-8 encoding - * (u+0080..u+07ff). - * - * Writes output to `repr`. - * `repr` cannot be null. - * Assumes adequate space i `repr`... - * >>> char[4] or [7] for most characters, - * >>> [13] as theoretical maximum. - * - * Representation `repr` is null-terminated. - * - * Stores length of representation (without null terminator) at pointer - * `repr_len`. - * - * Return : SUCCEED/FAIL - * - * - SUCCESS: `SUCCEED` - * - percent-encoded representation written to `repr` - * - 'repr' is null-terminated - * - FAILURE: `FAIL` - * - `c` or `repr` was NULL - * - *---------------------------------------------------------------------------- - */ -herr_t -H5FD_s3comms_percent_encode_char(char *repr, const unsigned char c, size_t *repr_len) -{ - unsigned int i = 0; - int chars_written = 0; - herr_t ret_value = SUCCEED; - - FUNC_ENTER_NOAPI_NOINIT - - if (repr == NULL) - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "no destination `repr`."); - - if (c <= (unsigned char)0x7f) { - /* character represented in a single "byte" - * and single percent-code - */ - *repr_len = 3; - chars_written = snprintf(repr, 4, "%%%02X", c); - 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 - */ - unsigned int acc = 0; /* byte accumulator */ - unsigned int k = 0; /* uint character representation */ - unsigned int stack_size = 0; - unsigned char stack[4] = {0, 0, 0, 0}; - - stack_size = 0; - k = (unsigned int)c; - *repr_len = 0; - do { - /* push number onto stack in six-bit slices - */ - acc = k; - acc >>= 6; /* cull least */ - acc <<= 6; /* six bits */ - stack[stack_size++] = (unsigned char)(k - acc); - k = acc >> 6; - } while (k > 0); - - /* `stack` now has two to four six-bit 'numbers' to be put into - * UTF-8 byte fields. - */ - - /**************** - * leading byte * - ****************/ - - /* prepend 11[1[1]]0 to first byte */ - /* 110xxxxx, 1110xxxx, or 11110xxx */ - acc = 0xC0; /* 0x11000000 */ - acc += (stack_size > 2) ? 0x20 : 0; /* 0x00100000 */ - acc += (stack_size > 3) ? 0x10 : 0; /* 0x00010000 */ - stack_size--; - chars_written = snprintf(repr, 4, "%%%02X", (unsigned char)(acc + stack[stack_size])); - if (chars_written < 0) - HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cannot write char %c", c); - *repr_len += 3; - - /************************ - * continuation byte(s) * - ************************/ - - /* 10xxxxxx */ - for (i = 0; i < stack_size; i++) { - chars_written = - snprintf(&repr[i * 3 + 3], 4, "%%%02X", (unsigned char)(0x80 + stack[stack_size - 1 - i])); - 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) */ - - *(repr + *repr_len) = '\0'; - -done: - FUNC_LEAVE_NOAPI(ret_value) -} /* H5FD_s3comms_percent_encode_char */ - /*---------------------------------------------------------------------------- * * Function: H5FD_s3comms_signing_key() diff --git a/src/H5FDs3comms.h b/src/H5FDs3comms.h index a0c72c7129a..62855e3d952 100644 --- a/src/H5FDs3comms.h +++ b/src/H5FDs3comms.h @@ -500,8 +500,6 @@ H5_DLL herr_t H5FD_s3comms_load_aws_profile(const char *name, char *key_id_out, 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); - H5_DLL herr_t H5FD_s3comms_signing_key(unsigned char *md, const char *secret, const char *region, const char *iso8601now); diff --git a/test/s3comms.c b/test/s3comms.c index 0dcff97cc65..ab447f869d9 100644 --- a/test/s3comms.c +++ b/test/s3comms.c @@ -1546,77 +1546,6 @@ test_parse_url(void) } /* end test_parse_url() */ -/*--------------------------------------------------------------------------- - * - * Function: test_percent_encode_char() - * - * Purpose: - * - * Define and verify behavior of `H5FD_s3comms_percent_encode_char()` - * - * Return: - * - * Success: 0 - * Failure: -1 - * - *--------------------------------------------------------------------------- - */ -static herr_t -test_percent_encode_char(void) -{ - /************************* - * test-local structures * - *************************/ - - struct testcase { - const char c; - const char *exp; - size_t exp_len; - }; - - /************************ - * test-local variables * - ************************/ - - struct testcase cases[] = { - {'$', "%24", 3}, /* u+0024 dollar sign */ - {' ', "%20", 3}, /* u+0020 space */ - {'^', "%5E", 3}, /* u+0094 carat */ - {'/', "%2F", 3}, /* u+002f solidus (forward slash) */ - /* {??, "%C5%8C", 6},*/ /* u+014c Latin Capital Letter O with Macron */ - /* Not included because it is multibyte "wide" character that poses */ - /* issues both in the underlying function and in being written in */ - /* this file. */ - /* {'¢', "%C2%A2", 6}, */ /* u+00a2 cent sign */ - /* above works, but complains about wide character overflow */ - /* Elide for now, until it is determined (a) unnecessary or */ - /* (b) requiring signature change to accommodate wide characters */ - {'\0', "%00", 3}, /* u+0000 null */ - }; - char dest[13]; - size_t dest_len = 0; - int i = 0; - int n_cases = 5; - - TESTING("percent encode characters"); - - for (i = 0; i < n_cases; i++) { - JSVERIFY(SUCCEED, H5FD_s3comms_percent_encode_char(dest, (const unsigned char)cases[i].c, &dest_len), - NULL) - JSVERIFY(cases[i].exp_len, dest_len, NULL) - JSVERIFY(0, strncmp(dest, cases[i].exp, dest_len), NULL) - JSVERIFY_STR(cases[i].exp, dest, NULL) - } - - JSVERIFY(FAIL, H5FD_s3comms_percent_encode_char(NULL, (const unsigned char)'^', &dest_len), NULL) - - PASSED(); - return 0; - -error: - return -1; -} /* end test_percent_encode_char() */ - /*--------------------------------------------------------------------------- * Function: test_s3r_get_filesize() *--------------------------------------------------------------------------- @@ -2237,7 +2166,6 @@ main(void) /* tests ordered roughly by dependence */ nerrors += test_macro_format_credential() < 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; nerrors += test_signing_key() < 0 ? 1 : 0;