apr_byte_t oidc_util_regexp_substitute(apr_pool_t *pool, const char *input,
		const char *regexp, const char *replace, char **output,
		char **error_str) {

	const char *errorptr = NULL;
	int erroffset;
	char *substituted = NULL;
	apr_byte_t rc = FALSE;

	pcre *preg = pcre_compile(regexp, 0, &errorptr, &erroffset, NULL);
	if (preg == NULL) {
		*error_str = apr_psprintf(pool,
				"pattern [%s] is not a valid regular expression", regexp);
		goto out;
	}

	if (strlen(input) >= OIDC_PCRE_MAXCAPTURE - 1) {
		*error_str =
				apr_psprintf(pool,
						"string length (%d) is larger than the maximum allowed for pcre_subst (%d)",
						(int) strlen(input), OIDC_PCRE_MAXCAPTURE - 1);
		goto out;
	}

	substituted = pcre_subst(preg, NULL, input, (int) strlen(input), 0, 0,
			replace);
	if (substituted == NULL) {
		*error_str =
				apr_psprintf(pool,
						"unknown error could not match string [%s] using pattern [%s] and replace matches in [%s]",
						input, regexp, replace);
		goto out;
	}

	*output = apr_pstrdup(pool, substituted);
	rc = TRUE;

	out: if (substituted)
		pcre_free(substituted);
	if (preg)
		pcre_free(preg);

	return rc;
}
apr_byte_t oidc_util_regexp_substitute(apr_pool_t *pool, const char *input,
		const char *regexp, const char *replace, char **output,
		char **error_str) {

	const char *errorptr = NULL;
	int erroffset;
	char *substituted = NULL;
	apr_byte_t rc = FALSE;

	pcre *preg = pcre_compile(regexp, 0, &errorptr, &erroffset, NULL);
	if (preg == NULL) {
		*error_str = apr_psprintf(pool,
				"pattern [%s] is not a valid regular expression", regexp);
		goto out;
	}

	if (strlen(input) >= OIDC_PCRE_MAXCAPTURE - 1) {
		*error_str =
				apr_psprintf(pool,
						"string length (%d) is larger than the maximum allowed for pcre_subst (%d)",
						(int) strlen(input), OIDC_PCRE_MAXCAPTURE - 1);
		goto out;
	}

	substituted = pcre_subst(preg, NULL, input, (int) strlen(input), 0, 0,
			replace);
	if (substituted == NULL) {
		*error_str =
				apr_psprintf(pool,
						"unknown error could not match string [%s] using pattern [%s] and replace matches in [%s]",
						input, regexp, replace);
		goto out;
	}

	*output = apr_pstrdup(pool, substituted);
	rc = TRUE;

	out: if (substituted)
		pcre_free(substituted);
	if (preg)
		pcre_free(preg);

	return rc;
}