diff --git a/src/pcre2_config.c b/src/pcre2_config.c index 86bc7fd..e5d2447 100644 --- a/src/pcre2_config.c +++ b/src/pcre2_config.c @@ -150,11 +150,7 @@ switch (what) #else const char *v = "Unicode not supported"; #endif - PCRE2_UCHAR *t = (PCRE2_UCHAR *)where; - if (strlen(v) >= BYTES2CU(length) - 1) return PCRE2_ERROR_BADLENGTH; - while (*v != 0) *t++ = *v++; - *t = 0; - return t - (PCRE2_UCHAR *)where; + return PRIV(strcpy_c8)((PCRE2_UCHAR *)where, BYTES2CU(length), v); } break; @@ -187,14 +183,10 @@ switch (what) case PCRE2_CONFIG_VERSION: { - PCRE2_UCHAR *t = (PCRE2_UCHAR *)where; const char *v = (XSTRING(Z PCRE2_PRERELEASE)[1] == 0)? XSTRING(PCRE2_MAJOR.PCRE2_MINOR PCRE2_DATE) : XSTRING(PCRE2_MAJOR.PCRE2_MINOR) XSTRING(PCRE2_PRERELEASE PCRE2_DATE); - if (strlen(v) >= BYTES2CU(length) - 1) return PCRE2_ERROR_BADLENGTH; - while (*v != 0) *t++ = *v++; - *t = 0; - return t - (PCRE2_UCHAR *)where; + return PRIV(strcpy_c8)((PCRE2_UCHAR *)where, BYTES2CU(length), v); } } diff --git a/src/pcre2_internal.h b/src/pcre2_internal.h index e54c5c4..bfea30c 100644 --- a/src/pcre2_internal.h +++ b/src/pcre2_internal.h @@ -1865,13 +1865,14 @@ is available. */ #define _pcre2_jit_get_size PCRE2_SUFFIX(_pcre2_jit_get_size_) #define _pcre2_memctl_malloc PCRE2_SUFFIX(_pcre2_memctl_malloc_) #define _pcre2_ord2utf PCRE2_SUFFIX(_pcre2_ord2utf_) -#define _pcre2_strcmp PCRE2_SUFFIX(_pcre_strcmp_) -#define _pcre2_strcmp_c8 PCRE2_SUFFIX(_pcre_strcmp_c8_) -#define _pcre2_strlen PCRE2_SUFFIX(_pcre_strlen_) -#define _pcre2_strncmp PCRE2_SUFFIX(_pcre_strncmp_) -#define _pcre2_strncmp_c8 PCRE2_SUFFIX(_pcre_strncmp_c8_) -#define _pcre2_study PCRE2_SUFFIX(_pcre_study_) -#define _pcre2_valid_utf PCRE2_SUFFIX(_pcre_valid_utf_) +#define _pcre2_strcmp PCRE2_SUFFIX(_pcre2_strcmp_) +#define _pcre2_strcmp_c8 PCRE2_SUFFIX(_pcre2_strcmp_c8_) +#define _pcre2_strcpy_c8 PCRE2_SUFFIX(_pcre2_strcpy_c8_) +#define _pcre2_strlen PCRE2_SUFFIX(_pcre2_strlen_) +#define _pcre2_strncmp PCRE2_SUFFIX(_pcre2_strncmp_) +#define _pcre2_strncmp_c8 PCRE2_SUFFIX(_pcre2_strncmp_c8_) +#define _pcre2_study PCRE2_SUFFIX(_pcre2_study_) +#define _pcre2_valid_utf PCRE2_SUFFIX(_pcre2_valid_utf_) #define _pcre2_was_newline PCRE2_SUFFIX(_pcre2_was_newline_) #define _pcre2_xclass PCRE2_SUFFIX(_pcre2_xclass_) @@ -1885,6 +1886,7 @@ extern void *_pcre2_memctl_malloc(size_t, pcre2_memctl *); extern unsigned int _pcre2_ord2utf(uint32_t, PCRE2_UCHAR *); extern int _pcre2_strcmp(PCRE2_SPTR, PCRE2_SPTR); extern int _pcre2_strcmp_c8(PCRE2_SPTR, const char *); +extern int _pcre2_strcpy_c8(PCRE2_UCHAR *, size_t, const char *); extern int _pcre2_strlen(PCRE2_SPTR); extern int _pcre2_strncmp(PCRE2_SPTR, PCRE2_SPTR, size_t); extern int _pcre2_strncmp_c8(PCRE2_SPTR, const char *, size_t); diff --git a/src/pcre2_string_utils.c b/src/pcre2_string_utils.c index 8b0a45d..1a6864b 100644 --- a/src/pcre2_string_utils.c +++ b/src/pcre2_string_utils.c @@ -160,7 +160,6 @@ return 0; } - /************************************************* * Find the length of a PCRE2 string * *************************************************/ @@ -178,4 +177,28 @@ while (*str++ != 0) c++; return c; } + +/************************************************* +* Copy 8-bit 0-terminated string to PCRE2 string * +*************************************************/ + +/* Arguments: + str1 buffer to receive the string + length length of buffer in code units + str2 8-bit string to be copied + +Returns: the number of code units used (excluding trailing zero) + PCRE2_ERROR_BADLENGTH (a negative number) if buffer is too small +*/ + +int +PRIV(strcpy_c8)(PCRE2_UCHAR *str1, size_t length, const char *str2) +{ +PCRE2_UCHAR *t = str1; +if (strlen(str2) >= length) return PCRE2_ERROR_BADLENGTH; +while (*str2 != 0) *t++ = *str2++; +*t = 0; +return t - str1; +} + /* End of pcre2_string_utils.c */