/* {{{ php_pgsql_convert_match
* test field value with regular expression specified.
*/
-static int php_pgsql_convert_match(const char *str, size_t str_len, const char *regex , int icase)
+static int php_pgsql_convert_match(const char *str, size_t str_len, const char *regex , size_t regex_len, int icase)
{
pcre2_code *re;
PCRE2_SIZE err_offset;
options |= PCRE2_CASELESS;
}
- re = pcre2_compile((PCRE2_SPTR)regex, PCRE2_ZERO_TERMINATED, options, &errnumber, &err_offset, php_pcre_cctx());
+ re = pcre2_compile((PCRE2_SPTR)regex, regex_len, options, &errnumber, &err_offset, php_pcre_cctx());
if (NULL == re) {
PCRE2_UCHAR err_msg[256];
pcre2_get_error_message(errnumber, err_msg, sizeof(err_msg));
}
else {
/* FIXME: better regex must be used */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([+-]{0,1}[0-9]+)$", 0) == FAILURE) {
+#define REGEX0 "^([+-]{0,1}[0-9]+)$"
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
}
+#undef REGEX0
}
break;
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
+#define REGEX0 "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$"
+#define REGEX1 "^[+-]{0,1}(inf)(inity){0,1}$"
/* better regex? */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$", 0) == FAILURE) {
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[+-]{0,1}(inf)(inity){0,1}$", 1) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX1, sizeof(REGEX1)-1, 1) == FAILURE) {
err = 1;
} else {
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
else {
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
}
+#undef REGEX0
+#undef REGEX1
}
break;
}
else {
/* better regex? */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[0-9]+$", 0) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[0-9]+$", sizeof("^[0-9]+$")-1, 0) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
+#define REGEX0 "^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\\/[0-9]{1,3})?$"
+#define REGEX1 "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/[0-9]{1,3})?$"
/* The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. See more in the doc.
The regex might still be not perfect, but catches the most of IP variants. We might decide to remove the regex
at all though and let the server side to handle it.*/
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\\/[0-9]{1,3})?$", 0) == FAILURE
- && php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/[0-9]{1,3})?$", 0) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE
+ && php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX1, sizeof(REGEX1)-1, 0) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
+#undef REGEX1
}
break;
} else if (!strcasecmp(Z_STRVAL_P(val), "now()")) {
ZVAL_STRINGL(&new_val, "NOW()", sizeof("NOW()")-1);
} else {
+#define REGEX0 "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})(([ \\t]+|T)(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$"
/* better regex? */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})(([ \\t]+|T)(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$", 1) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
} else {
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
+#define REGEX0 "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$"
/* FIXME: better regex must be used */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$", 1) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
+#define REGEX0 "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1})){0,1}$"
/* FIXME: better regex must be used */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1})){0,1}$", 1) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;
unit markings. For example, '1 12:59:10' is read the same as '1 day 12 hours 59 min 10
sec'.
*/
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val),
- "^(@?[ \\t]+)?("
-
- /* Textual time units and their abbreviations: */
- "(([-+]?[ \\t]+)?"
- "[0-9]+(\\.[0-9]*)?[ \\t]*"
- "(millenniums|millennia|millennium|mil|mils|"
- "centuries|century|cent|c|"
- "decades|decade|dec|decs|"
- "years|year|y|"
- "months|month|mon|"
- "weeks|week|w|"
- "days|day|d|"
- "hours|hour|hr|hrs|h|"
- "minutes|minute|mins|min|m|"
- "seconds|second|secs|sec|s))+|"
-
- /* Textual time units plus (dd)* hh[:mm[:ss]] */
- "((([-+]?[ \\t]+)?"
- "[0-9]+(\\.[0-9]*)?[ \\t]*"
- "(millenniums|millennia|millennium|mil|mils|"
- "centuries|century|cent|c|"
- "decades|decade|dec|decs|"
- "years|year|y|"
- "months|month|mon|"
- "weeks|week|w|"
- "days|day|d))+"
- "([-+]?[ \\t]+"
- "([0-9]+[ \\t]+)+" /* dd */
- "(([0-9]{1,2}:){0,2}[0-9]{0,2})" /* hh:[mm:[ss]] */
- ")?))"
- "([ \\t]+ago)?$",
- 1) == FAILURE) {
+#define REGEX0 \
+ "^(@?[ \\t]+)?(" \
+ /* Textual time units and their abbreviations: */ \
+ "(([-+]?[ \\t]+)?" \
+ "[0-9]+(\\.[0-9]*)?[ \\t]*" \
+ "(millenniums|millennia|millennium|mil|mils|" \
+ "centuries|century|cent|c|" \
+ "decades|decade|dec|decs|" \
+ "years|year|y|" \
+ "months|month|mon|" \
+ "weeks|week|w|" \
+ "days|day|d|" \
+ "hours|hour|hr|hrs|h|" \
+ "minutes|minute|mins|min|m|" \
+ "seconds|second|secs|sec|s))+|" \
+ /* Textual time units plus (dd)* hh[:mm[:ss]] */ \
+ "((([-+]?[ \\t]+)?" \
+ "[0-9]+(\\.[0-9]*)?[ \\t]*" \
+ "(millenniums|millennia|millennium|mil|mils|" \
+ "centuries|century|cent|c|" \
+ "decades|decade|dec|decs|" \
+ "years|year|y|" \
+ "months|month|mon|" \
+ "weeks|week|w|" \
+ "days|day|d))+" \
+ "([-+]?[ \\t]+" \
+ "([0-9]+[ \\t]+)+" /* dd */ \
+ "(([0-9]{1,2}:){0,2}[0-9]{0,2})" /* hh:[mm:[ss]] */ \
+ ")?))" \
+ "([ \\t]+ago)?$"
+
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
}
else {
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$", 1) == FAILURE) {
+#define REGEX0 "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$"
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;