#include #include "log.h" #include "regex_table.h" typedef struct { int regId; pcre *re; char *regex; } REGEX_CONTEXT, *PREGEX_CONTEXT; #define SVR_REGEX_STR "^(([1-9]\\d{0,3})|([1-5]\\d{4})|(6[0-4]\\d{3})|(65[0-4]\\d{2})|(655[0-2]" \ "\\d)|(6553[0-5]))$" #define IP_REGEX_STR "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?" \ ")(\\/(\\d|[1-2]\\d|3[0-2]))?$|^([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d" \ "|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([" \ "0-1]\\d|2[0-8]))))?$|^::([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]" \ "?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]" \ "\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}((25[0-5]|" \ "2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]" \ "?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:" \ "){0,2}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?" \ "\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){3}:" \ "([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]" \ "|2[0-4]\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-" \ "fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]" \ "\\d|[01]?\\d\\d?)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1" \ ",4}:){7}[\\da-fA-F]{1,4}(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^:((:[" \ "\\da-fA-F]{1,4}){1,6}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^[\\da-f" \ "A-F]{1,4}:((:[\\da-fA-F]{1,4}){1,5}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8])" \ ")))?$|^([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,4}|:)(\\/([1-9]?\\d|" \ "(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1,4}){1,3" \ "}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-fA-F]{1,4}:){4}((:[" \ "\\da-fA-F]{1,4}){1,2}|:)(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$|^([\\da-" \ "fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})?(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))" \ "?$|^([\\da-fA-F]{1,4}:){6}:(\\/([1-9]?\\d|(1([0-1]\\d|2[0-8]))))?$" #define DT_REGEX_STR "^(?:(?!0000)[0-9]{4}\\/(?:(?:0[1-9]|1[0-2])\\/(?:0[1-9]|1[0-9]|2[0-8])|(" \ "?:0[13-9]|1[0-2])\\/(?:29|30)|(?:0[13578]|1[02])\\/31)|(?:[0-9]{2}(?:0[48" \ "]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)\\/02\\/29" \ ") (20|21|22|23|[0-1][0-9]):[0-5][0-9]$|^(20|21|22|23|[0-1][0-9]):[0-5][0-" \ "9]$" #define TIME_REGEX_STR "^(20|21|22|23|[0-1][0-9]):[0-5][0-9]$" const char *g_regex_tbl[REGEX_MAX] = { SVR_REGEX_STR, IP_REGEX_STR, DT_REGEX_STR, TIME_REGEX_STR, }; static REGEX_CONTEXT g_reg_ctx[REGEX_MAX] = { {REGEX_SVR_PORT, NULL, SVR_REGEX_STR}, {REGEX_IP_ADDR, NULL, IP_REGEX_STR}, {REGEX_DT, NULL, DT_REGEX_STR}, {REGEX_TIME, NULL, TIME_REGEX_STR} }; int pcre_match(int regId, const char *pStr) { pcre *re; pcre_extra *pcreExtra; const char *error; int ret, erroffset; if(regId < 0 || regId > REGEX_MAX || !pStr) { LOG_EX(LOG_Error, "Input params error: %d, %p\n", regId, pStr); return FALSE; } re = pcre_compile(g_regex_tbl[regId], 0, &error, &erroffset, NULL); if(!re) { LOG_EX(LOG_Error, "pcre_compile error at %d: %s, %s\n", erroffset, g_regex_tbl[regId], error); return FALSE; } pcreExtra = pcre_study(re, 0, &error); if(error != NULL) { LOG_EX(LOG_Error, "pcre_study error: %s, %s\n", g_regex_tbl[regId], error); pcre_free(re); return FALSE; } ret = pcre_exec(re, pcreExtra, pStr, strlen(pStr), // length of string 0, // Start looking at this point 0, // OPTIONS NULL, 0); // Length of subStrVec if(ret < 0) { switch(ret) { case PCRE_ERROR_NOMATCH : LOG_EX(LOG_Error, "[%s] String did not match the pattern\n", pStr); break; case PCRE_ERROR_NULL : LOG_EX(LOG_Error, "[%s] Something was null\n", pStr); break; case PCRE_ERROR_BADOPTION : LOG_EX(LOG_Error, "[%s] A bad option was passed\n", pStr); break; case PCRE_ERROR_BADMAGIC : LOG_EX(LOG_Error, "Magic number bad (compiled re corrupt?)\n"); break; case PCRE_ERROR_UNKNOWN_NODE : LOG_EX(LOG_Error, "[%s] Something kooky in the compiled re\n", pStr); break; case PCRE_ERROR_NOMEMORY : LOG_EX(LOG_Error, "[%s] Ran out of memory\n", pStr); break; default : LOG_EX(LOG_Error, "[%s] Unknown error\n", pStr); break; } /* end switch */ } else { ret = TRUE; } pcre_free(re); if(pcreExtra != NULL) { #ifdef PCRE_CONFIG_JIT pcre_free_study(pcreExtra); #else pcre_free(pcreExtra); #endif } return ret; } int pcre_match_str(const char *pRegex, const char *pStr) { pcre *re; pcre_extra *pcreExtra; const char *error; int ret, erroffset; if(!pRegex || !pStr) { LOG_EX(LOG_Error, "Input params error: %p, %p\n", pRegex, pStr); return FALSE; } re = pcre_compile(pRegex, 0, &error, &erroffset, NULL); if(!re) { LOG_EX(LOG_Error, "pcre_compile error at %d: %s, %s\n", erroffset, pRegex, error); return FALSE; } pcreExtra = pcre_study(re, 0, &error); if(error != NULL) { LOG_EX(LOG_Error, "pcre_study error: %s, %s\n", pRegex, error); pcre_free(re); return FALSE; } ret = pcre_exec(re, pcreExtra, pStr, strlen(pStr), // length of string 0, // Start looking at this point 0, // OPTIONS NULL, 0); // Length of subStrVec if(ret < 0) { switch(ret) { case PCRE_ERROR_NOMATCH : LOG_EX(LOG_Error, "[%s] String did not match the pattern\n", pStr); break; case PCRE_ERROR_NULL : LOG_EX(LOG_Error, "[%s] Something was null\n", pStr); break; case PCRE_ERROR_BADOPTION : LOG_EX(LOG_Error, "[%s] A bad option was passed\n", pStr); break; case PCRE_ERROR_BADMAGIC : LOG_EX(LOG_Error, "Magic number bad (compiled re corrupt?)\n"); break; case PCRE_ERROR_UNKNOWN_NODE : LOG_EX(LOG_Error, "[%s] Something kooky in the compiled re\n", pStr); break; case PCRE_ERROR_NOMEMORY : LOG_EX(LOG_Error, "[%s] Ran out of memory\n", pStr); break; default : LOG_EX(LOG_Error, "[%s] Unknown error\n", pStr); break; } /* end switch */ } else { ret = TRUE; } pcre_free(re); if(pcreExtra != NULL) { #ifdef PCRE_CONFIG_JIT pcre_free_study(pcreExtra); #else pcre_free(pcreExtra); #endif } return ret; } #if 0 int regex_match(const char *regex, const char *pStr) { int result = FALSE; regex_t reg; int regexInit; if(!regex || !pStr) { LOG_EX(LOG_Error, "Input params error: %p, %p\n", regex, pStr); return FALSE; } regexInit = regcomp(®, regex, REG_EXTENDED); if(regexInit) { LOG_EX(LOG_Error, "regcomp error\n"); return FALSE; } else { int reti = regexec(®, pStr, 0, NULL, 0); if(REG_NOERROR != reti) { char buf[1024]; memset(buf, 0, 1024); //Error print: match failed! regerror(reti, regexInit, buf, 1024); LOG_EX(LOG_Error, "%s match %s failed(%d): %s\n", regex, pStr, reti, buf); } else { //LOG_EX(LOG_Debug, "%s match %s OK\n", regex, pStr); result = TRUE; } } regfree(®); return result; } #endif