⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcretest.c

📁 php-4.4.7学习linux时下载的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/**************************************************             PCRE testing program               **************************************************//* This program was hacked up as a tester for PCRE. I really should havewritten it more tidily in the first place. Will I ever learn? It has grown andbeen extended and consequently is now rather, er, *very* untidy in places.-----------------------------------------------------------------------------Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:    * Redistributions of source code must retain the above copyright notice,      this list of conditions and the following disclaimer.    * Redistributions in binary form must reproduce the above copyright      notice, this list of conditions and the following disclaimer in the      documentation and/or other materials provided with the distribution.    * Neither the name of the University of Cambridge nor the names of its      contributors may be used to endorse or promote products derived from      this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSEARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BELIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OFSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESSINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER INCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF SUCH DAMAGE.-----------------------------------------------------------------------------*/#include <ctype.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <time.h>#include <locale.h>#include <errno.h>/* A number of things vary for Windows builds. Originally, pcretest opened itsinput and output without "b"; then I was told that "b" was needed in someenvironments, so it was added for release 5.0 to both the input and output. (Itmakes no difference on Unix-like systems.) Later I was told that it is wrongfor the input on Windows. I've now abstracted the modes into two macros thatare set here, to make it easier to fiddle with them, and removed "b" from theinput mode under Windows. */#if defined(_WIN32) || defined(WIN32)#include <io.h>                /* For _setmode() */#include <fcntl.h>             /* For _O_BINARY */#define INPUT_MODE   "r"#define OUTPUT_MODE  "wb"#else#include <sys/time.h>          /* These two includes are needed */#include <sys/resource.h>      /* for setrlimit(). */#define INPUT_MODE   "rb"#define OUTPUT_MODE  "wb"#endif#define PCRE_SPY        /* For Win32 build, import data, not export *//* We include pcre_internal.h because we need the internal info for displayingthe results of pcre_study() and we also need to know about the internalmacros, structures, and other internal data values; pcretest has "insideinformation" compared to a program that strictly follows the PCRE API. */#include "pcre_internal.h"/* We need access to the data tables that PCRE uses. So as not to have to keeptwo copies, we include the source file here, changing the names of the externalsymbols to prevent clashes. */#define _pcre_utf8_table1      utf8_table1#define _pcre_utf8_table1_size utf8_table1_size#define _pcre_utf8_table2      utf8_table2#define _pcre_utf8_table3      utf8_table3#define _pcre_utf8_table4      utf8_table4#define _pcre_utt              utt#define _pcre_utt_size         utt_size#define _pcre_OP_lengths       OP_lengths#include "pcre_tables.c"/* We also need the pcre_printint() function for printing out compiledpatterns. This function is in a separate file so that it can be included inpcre_compile.c when that module is compiled with debugging enabled.The definition of the macro PRINTABLE, which determines whether to print anoutput character as-is or as a hex value when showing compiled patterns, iscontained in this file. We uses it here also, in cases when the locale has notbeen explicitly changed, so as to get consistent output from systems thatdiffer in their output from isprint() even in the "C" locale. */#include "pcre_printint.src"#define PRINTHEX(c) (locale_set? isprint(c) : PRINTABLE(c))/* It is possible to compile this test program without including support fortesting the POSIX interface, though this is not available via the standardMakefile. */#if !defined NOPOSIX#include "pcreposix.h"#endif/* It is also possible, for the benefit of the version imported into Exim, tobuild pcretest without support for UTF8 (define NOUTF8), without the interfaceto the DFA matcher (NODFA), and without the doublecheck of the old "info"function (define NOINFOCHECK). *//* Other parameters */#ifndef CLOCKS_PER_SEC#ifdef CLK_TCK#define CLOCKS_PER_SEC CLK_TCK#else#define CLOCKS_PER_SEC 100#endif#endif/* This is the default loop count for timing. */#define LOOPREPEAT 500000/* Static variables */static FILE *outfile;static int log_store = 0;static int callout_count;static int callout_extra;static int callout_fail_count;static int callout_fail_id;static int first_callout;static int locale_set = 0;static int show_malloc;static int use_utf8;static size_t gotten_store;/* The buffers grow automatically if very long input lines are encountered. */static int buffer_size = 50000;static uschar *buffer = NULL;static uschar *dbuffer = NULL;static uschar *pbuffer = NULL;/**************************************************        Read or extend an input line            **************************************************//* Input lines are read into buffer, but both patterns and data lines can becontinued over multiple input lines. In addition, if the buffer fills up, wewant to automatically expand it so as to be able to handle extremely largelines that are needed for certain stress tests. When the input buffer isexpanded, the other two buffers must also be expanded likewise, and thecontents of pbuffer, which are a copy of the input for callouts, must bepreserved (for when expansion happens for a data line). This is not the mostoptimal way of handling this, but hey, this is just a test program!Arguments:  f            the file to read  start        where in buffer to start (this *must* be within buffer)Returns:       pointer to the start of new data               could be a copy of start, or could be moved               NULL if no data read and EOF reached*/static uschar *extend_inputline(FILE *f, uschar *start){uschar *here = start;for (;;)  {  int rlen = buffer_size - (here - buffer);  if (rlen > 1000)    {    int dlen;    if (fgets((char *)here, rlen,  f) == NULL)      return (here == start)? NULL : start;    dlen = (int)strlen((char *)here);    if (dlen > 0 && here[dlen - 1] == '\n') return start;    here += dlen;    }  else    {    int new_buffer_size = 2*buffer_size;    uschar *new_buffer = (unsigned char *)malloc(new_buffer_size);    uschar *new_dbuffer = (unsigned char *)malloc(new_buffer_size);    uschar *new_pbuffer = (unsigned char *)malloc(new_buffer_size);    if (new_buffer == NULL || new_dbuffer == NULL || new_pbuffer == NULL)      {      fprintf(stderr, "pcretest: malloc(%d) failed\n", new_buffer_size);      exit(1);      }    memcpy(new_buffer, buffer, buffer_size);    memcpy(new_pbuffer, pbuffer, buffer_size);    buffer_size = new_buffer_size;    start = new_buffer + (start - buffer);    here = new_buffer + (here - buffer);    free(buffer);    free(dbuffer);    free(pbuffer);    buffer = new_buffer;    dbuffer = new_dbuffer;    pbuffer = new_pbuffer;    }  }return NULL;  /* Control never gets here */}/**************************************************          Read number from string               **************************************************//* We don't use strtoul() because SunOS4 doesn't have it. Rather than messaround with conditional compilation, just do the job by hand. It is only usedfor unpicking arguments, so just keep it simple.Arguments:  str           string to be converted  endptr        where to put the end pointerReturns:        the unsigned long*/static intget_value(unsigned char *str, unsigned char **endptr){int result = 0;while(*str != 0 && isspace(*str)) str++;while (isdigit(*str)) result = result * 10 + (int)(*str++ - '0');*endptr = str;return(result);}/**************************************************            Convert UTF-8 string to value       **************************************************//* This function takes one or more bytes that represents a UTF-8 character,and returns the value of the character.Argument:  utf8bytes   a pointer to the byte vector  vptr        a pointer to an int to receive the valueReturns:      >  0 => the number of bytes consumed              -6 to 0 => malformed UTF-8 character at offset = (-return)*/#if !defined NOUTF8static intutf82ord(unsigned char *utf8bytes, int *vptr){int c = *utf8bytes++;int d = c;int i, j, s;for (i = -1; i < 6; i++)               /* i is number of additional bytes */  {  if ((d & 0x80) == 0) break;  d <<= 1;  }if (i == -1) { *vptr = c; return 1; }  /* ascii character */if (i == 0 || i == 6) return 0;        /* invalid UTF-8 *//* i now has a value in the range 1-5 */s = 6*i;d = (c & utf8_table3[i]) << s;for (j = 0; j < i; j++)  {  c = *utf8bytes++;  if ((c & 0xc0) != 0x80) return -(j+1);  s -= 6;  d |= (c & 0x3f) << s;  }/* Check that encoding was the correct unique one */for (j = 0; j < utf8_table1_size; j++)  if (d <= utf8_table1[j]) break;if (j != i) return -(i+1);/* Valid value */*vptr = d;return i+1;}#endif/**************************************************       Convert character value to UTF-8         **************************************************//* This function takes an integer value in the range 0 - 0x7fffffffand encodes it as a UTF-8 character in 0 to 6 bytes.Arguments:  cvalue     the character value  utf8bytes  pointer to buffer for result - at least 6 bytes longReturns:     number of characters placed in the buffer*/#if !defined NOUTF8static intord2utf8(int cvalue, uschar *utf8bytes){register int i, j;for (i = 0; i < utf8_table1_size; i++)  if (cvalue <= utf8_table1[i]) break;utf8bytes += i;for (j = i; j > 0; j--) { *utf8bytes-- = 0x80 | (cvalue & 0x3f); cvalue >>= 6; }*utf8bytes = utf8_table2[i] | cvalue;return i + 1;}#endif/**************************************************             Print character string             **************************************************//* Character string printing function. Must handle UTF-8 strings in utf8mode. Yields number of characters printed. If handed a NULL file, just countschars without printing. */static int pchars(unsigned char *p, int length, FILE *f){int c = 0;int yield = 0;while (length-- > 0)  {#if !defined NOUTF8  if (use_utf8)    {    int rc = utf82ord(p, &c);    if (rc > 0 && rc <= length + 1)   /* Mustn't run over the end */      {      length -= rc - 1;      p += rc;      if (PRINTHEX(c))        {        if (f != NULL) fprintf(f, "%c", c);        yield++;        }      else        {        int n = 4;        if (f != NULL) fprintf(f, "\\x{%02x}", c);        yield += (n <= 0x000000ff)? 2 :                 (n <= 0x00000fff)? 3 :                 (n <= 0x0000ffff)? 4 :                 (n <= 0x000fffff)? 5 : 6;        }      continue;      }    }#endif   /* Not UTF-8, or malformed UTF-8  */  c = *p++;  if (PRINTHEX(c))    {    if (f != NULL) fprintf(f, "%c", c);    yield++;    }  else    {    if (f != NULL) fprintf(f, "\\x%02x", c);    yield += 4;    }  }return yield;}/**************************************************              Callout function                  **************************************************//* Called from PCRE as a result of the (?C) item. We print out where we are inthe match. Yield zero unless more callouts than the fail count, or the calloutdata is not zero. */static int callout(pcre_callout_block *cb){FILE *f = (first_callout | callout_extra)? outfile : NULL;int i, pre_start, post_start, subject_length;if (callout_extra)  {  fprintf(f, "Callout %d: last capture = %d\n",    cb->callout_number, cb->capture_last);  for (i = 0; i < cb->capture_top * 2; i += 2)    {    if (cb->offset_vector[i] < 0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -