Skip to content
Browse files

1.1 update

*	modified:   from_php.c
*	new file:   ghfcns.c
*	modified:   ghmysql.c
*	modified:   lib_mysqludf_preg_capture.c
*	modified:   lib_mysqludf_preg_check.c
*	modified:   lib_mysqludf_preg_position.c
*	modified:   lib_mysqludf_preg_replace.c
*	modified:   lib_mysqludf_preg_rlike.c
*	modified:   preg.c
*
  • Loading branch information...
1 parent 9518db3 commit 460af3bf9ec7bd2652670be0601b72c80c061b83 raw committed
Showing with 136 additions and 46 deletions.
  1. +17 −8 from_php.c
  2. +49 −0 ghfcns.c
  3. +1 −33 ghmysql.c
  4. +10 −0 lib_mysqludf_preg_capture.c
  5. +8 −0 lib_mysqludf_preg_check.c
  6. +9 −0 lib_mysqludf_preg_position.c
  7. +27 −2 lib_mysqludf_preg_replace.c
  8. +10 −1 lib_mysqludf_preg_rlike.c
  9. +5 −2 preg.c
View
25 from_php.c
@@ -51,8 +51,17 @@ POSSIBILITY OF SUCH DAMAGE.
* with a mysql udf.
*/
-#include "ghmysql.h"
-#include "preg.h"
+#include "pcre.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+//#include "ghmysql.h"
+//#include "preg.h"
+
+#include "ghfcns.h"
+
#undef HAVE_SETLOCALE // R.A.W
@@ -104,7 +113,7 @@ pcre *compileRegex( char *regex , int regex_len , char *msg , int msglen )
char *p, *pp;
char *pattern;
int do_study = 0;
- int poptions = 0;
+ //int poptions = 0;
unsigned const char *tables = NULL;
char buf[ 1024 ] ;
@@ -122,7 +131,7 @@ pcre *compileRegex( char *regex , int regex_len , char *msg , int msglen )
/* Try to lookup the cached regex entry, and if successful, just pass
back the compiled pattern, otherwise go on and compile it. */
- regex_len = strlen(regex);
+ //regex_len = strlen(regex);
//R.A.W.
#if 0
@@ -397,8 +406,8 @@ char *pregReplace(pcre *re , pcre_extra *extra ,
int size_offsets; /* Size of the offsets array */
int new_len; /* Length of needed storage */
int alloc_len; /* Actual allocated length */
- int eval_result_len=0; /* Length of the eval'ed or
- function-returned string */
+ //int eval_result_len=0; /* Length of the eval'ed or
+ //function-returned string */
int match_len; /* Length of the current match */
int backref; /* Backreference number */
int eval; /* If the replacement string should be eval'ed */
@@ -413,13 +422,13 @@ char *pregReplace(pcre *re , pcre_extra *extra ,
*match, /* The current match */
*piece, /* The current piece of subject */
*replace_end=NULL, /* End of replacement string */
- *eval_result, /* Result of eval or custom function */
walk_last; /* Last walked character */
+ //*eval_result, /* Result of eval or custom function */
int rc;
if (extra == NULL) {
// R.A.W.
- memset( &extra , 0 , sizeof( extra ) ) ;
+ memset( &extra_data , 0 , sizeof( extra_data ) ) ;
extra_data.flags = PCRE_EXTRA_MATCH_LIMIT | PCRE_EXTRA_MATCH_LIMIT_RECURSION;
extra = &extra_data;
}
View
49 ghfcns.c
@@ -0,0 +1,49 @@
+/*
+ * ghfcns.c
+ * eMailGanizer
+ *
+ * Created by Rich Waters on 9/3/10.
+ * Copyright 2010 GoodHumans. All rights reserved.
+ *
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "ghfcns.h"
+
+/**
+ * @fn char *ghstrndup( char *s , int l )
+ *
+ * @brief safe strndup
+ *
+ * @param s - bytes to copy
+ * @param l - number of bytes to copy
+ *
+ * @return ptr - to newly allocate memory
+ * @return NULL - if memory not available
+ *
+ * @details - This function copies the given number of bytes from the given
+ * buffer into a newly allocated memory area. It then adds a '\\0' to the
+ * end. This function shouldn't be necessary, but since many call rely on
+ * null-termination instead of lengths, this creates things that can be
+ * passed to those functions safely.
+ */
+char *ghstrndup( char *s , int l )
+{
+ char *colval = NULL ;
+
+ colval = malloc( l + 1 ) ;
+ if( !colval )
+ {
+ fprintf( stderr , "Not enough memory: %d\n" , l ) ;
+ return NULL ;
+ }
+ memcpy( colval , s , l ) ;
+ colval[ l ] = '\0';
+
+ return colval ;
+}
+
+
View
34 ghmysql.c
@@ -25,41 +25,9 @@
*
*/
#include "ghmysql.h"
+#include "ghfcns.h"
-/**
- * @fn char *ghstrndup( char *s , int l )
- *
- * @brief safe strndup
- *
- * @param s - bytes to copy
- * @param l - number of bytes to copy
- *
- * @return ptr - to newly allocate memory
- * @return NULL - if memory not available
- *
- * @details - This function copies the given number of bytes from the given
- * buffer into a newly allocated memory area. It then adds a '\\0' to the
- * end. This function shouldn't be necessary, but since many call rely on
- * null-termination instead of lengths, this creates things that can be
- * passed to those functions safely.
- */
-char *ghstrndup( char *s , int l )
-{
- char *colval = NULL ;
-
- colval = malloc( l + 1 ) ;
- if( !colval )
- {
- fprintf( stderr , "Not enough memory: %d\n" , l ) ;
- return NULL ;
- }
- memcpy( colval , s , l ) ;
- colval[ l ] = '\0';
-
- return colval ;
-}
-
/**
* @fn char *ghargdup( UDF_ARGS *args,int i )
View
10 lib_mysqludf_preg_capture.c
@@ -221,6 +221,16 @@ char *preg_capture(UDF_INIT *initid , UDF_ARGS *args, char *result,
*ptr->return_buffer = '\0'; /* clear return value */
*length = 0 ; /* just to be safe */
+#ifndef GH_1_0_NULL_HANDLING
+ if( ghargIsNullConstant( args , 0 ) || ghargIsNullConstant( args , 1 )
+ || ghargIsNullConstant( args , 2 ) )
+ {
+ //fprintf( stderr , "null subject. Returning NULL\n" ) ;
+ *is_null = 1 ;
+ return NULL ;
+ }
+#endif
+
// compile the regex if necessary
if( ptr->constant_pattern )
re = ptr->re ;
View
8 lib_mysqludf_preg_check.c
@@ -162,6 +162,14 @@ longlong preg_check( UDF_INIT *initid , UDF_ARGS *args, char *is_null,
pcre *re ; /* the compiled regex */
+#ifndef GH_1_0_NULL_HANDLING
+ if( ghargIsNullConstant( args , 0 ) )
+ {
+ *is_null = 1 ;
+ return NULL ;
+ }
+#endif
+
ptr = (struct preg_s *) initid->ptr ;
if( args->args[0] && args->lengths[0] )
{
View
9 lib_mysqludf_preg_position.c
@@ -205,6 +205,15 @@ longlong preg_position( UDF_INIT *initid, UDF_ARGS *args, char *is_null,
*error = 0 ; /* default to no error */
*ptr->return_buffer = '\0'; /* clear return value */
+#ifndef GH_1_0_NULL_HANDLING
+ if( ghargIsNullConstant( args , 0 ) || ghargIsNullConstant( args , 1 )
+ || ghargIsNullConstant( args , 2 ) )
+ {
+ *is_null = 1 ;
+ return NULL ;
+ }
+#endif
+
// compile the regex if necessary
if( ptr->constant_pattern )
re = ptr->re ;
View
29 lib_mysqludf_preg_replace.c
@@ -168,7 +168,6 @@ my_bool preg_replace_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
return 0;
}
-
/**
* @fn char *preg_replace( UDF_INIT *initid , UDF_ARGS *args, char *result,
* unsigned long *length, char *is_null, char *error )
@@ -216,6 +215,14 @@ char *preg_replace( UDF_INIT *initid , UDF_ARGS *args, char *result,
*is_null = 0 ;
*error = 0 ; /* default to no error */
+#ifndef GH_1_0_NULL_HANDLING
+ if( ghargIsNullConstant( args , 2 ) || ghargIsNullConstant( args , 0 ) )
+ {
+ *is_null = 1 ;
+ return NULL ;
+ }
+#endif
+
if( ptr->constant_pattern )
{
re = ptr->re ;
@@ -231,6 +238,13 @@ char *preg_replace( UDF_INIT *initid , UDF_ARGS *args, char *result,
}
}
+ int nullReplacement ;
+ nullReplacement = 0 ;
+ if( ghargIsNullConstant( args , 1 ) )
+ {
+ nullReplacement = 1 ;
+ }
+
replacement = ghargdups( args , 1 , &repl_len ) ;
if( !replacement )
{
@@ -262,11 +276,22 @@ char *preg_replace( UDF_INIT *initid , UDF_ARGS *args, char *result,
limit = -1 ;
}
+ fprintf( stderr, "REPLA %s\n",replacement ) ;
+
s = pregReplace( re , NULL , subject, subject_len , replacement ,
repl_len , 0 , &s_len , limit , &count ,
msg , sizeof(msg) ) ;
- result = pregMoveToReturnValues( initid ,length,is_null , error,s,s_len );
+#ifndef GH_1_0_NULL_HANDLING
+ if( nullReplacement && s && subject && strcmp( s , subject ) ) {
+ result = NULL ;
+ *is_null = 1 ;
+ }
+ else
+#endif
+ {
+ result = pregMoveToReturnValues( initid ,length,is_null , error,s,s_len );
+ }
free( subject );
free( replacement ) ;
View
11 lib_mysqludf_preg_rlike.c
@@ -166,8 +166,17 @@ longlong preg_rlike( UDF_INIT *initid , UDF_ARGS *args, char *is_null,
int rc ;
pcre *re ; /* the compiled regex */
+#ifndef GH_1_0_NULL_HANDLING
+ if( ghargIsNullConstant( args , 0 ) || ghargIsNullConstant( args , 1 ) )
+ {
+ *is_null = 1 ;
+ return NULL ;
+ }
+#endif
+
ptr = (struct preg_s *) initid->ptr ;
- if( args->args[1] && args->lengths[1] )
+ // Need to leave out the length check here because some patterns can return true against an empty string
+ if( args->args[1] /*&& args->lengths[1]*/ )
{
if( ptr->constant_pattern )
{
View
7 preg.c
@@ -240,7 +240,8 @@ char *pregSkipToOccurence( pcre *re , char *subject , int subject_len ,
ex_subject = subject ;
// Skip over the 1st N occurences
- while( occurence-- && subject_offset < subject_len ) {
+
+ while( occurence-- && subject_offset <= subject_len ) {
// Run the regex and find the groupnum if possible
*rc = pcre_exec(re, NULL, subject + subject_offset ,
@@ -345,9 +346,11 @@ my_bool pregInit(UDF_INIT *initid, UDF_ARGS *args, char *message)
if( ghargIsNullConstant( args , 0 ) )
{
- strcpy( message, "NULL pattern" ) ;
ptr->constant_pattern = 1 ;
+#ifdef GH_1_0_NULL_HANDLING
+ strcpy( message, "NULL pattern" ) ;
return 1 ;
+#endif
}

0 comments on commit 460af3b

Please sign in to comment.
Something went wrong with that request. Please try again.