From 312375057bfd9786129752b0fdd6a0f309855206 Mon Sep 17 00:00:00 2001 From: "Philip.Hazel" Date: Tue, 25 Nov 2014 17:35:01 +0000 Subject: [PATCH] Change callouts to pass the user data as a separate argument. --- doc/pcre2api.3 | 4 ++-- doc/pcre2callout.3 | 26 +++++++++++--------------- src/pcre2.h.in | 3 +-- src/pcre2_context.c | 2 +- src/pcre2_dfa_match.c | 8 ++++---- src/pcre2_intmodedep.h | 6 +++--- src/pcre2_jit_compile.c | 5 ++--- src/pcre2_match.c | 8 ++++---- src/pcre2test.c | 22 +++++++++++----------- 9 files changed, 39 insertions(+), 45 deletions(-) diff --git a/doc/pcre2api.3 b/doc/pcre2api.3 index 2c0d053..28df12b 100644 --- a/doc/pcre2api.3 +++ b/doc/pcre2api.3 @@ -114,7 +114,7 @@ document for an overview of all the PCRE2 documentation. .B void pcre2_match_context_free(pcre2_match_context *\fImcontext\fP); .sp .B int pcre2_set_callout(pcre2_match_context *\fImcontext\fP, -.B " int (*\fIcallout_function\fP)(pcre2_callout_block *)," +.B " int (*\fIcallout_function\fP)(pcre2_callout_block *, void *)," .B " void *\fIcallout_data\fP);" .sp .B int pcre2_set_match_limit(pcre2_match_context *\fImcontext\fP, @@ -645,7 +645,7 @@ PCRE2_ERROR_BADDATA if invalid data is detected. .sp .nf .B int pcre2_set_callout(pcre2_match_context *\fImcontext\fP, -.B " int (*\fIcallout_function\fP)(pcre2_callout_block *)," +.B " int (*\fIcallout_function\fP)(pcre2_callout_block *, void *)," .B " void *\fIcallout_data\fP);" .fi .sp diff --git a/doc/pcre2callout.3 b/doc/pcre2callout.3 index 3b126c8..4e83305 100644 --- a/doc/pcre2callout.3 +++ b/doc/pcre2callout.3 @@ -1,4 +1,4 @@ -.TH PCRE2CALLOUT 3 "23 November 2014" "PCRE2 10.00" +.TH PCRE2CALLOUT 3 "25 November 2014" "PCRE2 10.00" .SH NAME PCRE2 - Perl-compatible regular expressions (revised API) .SH SYNOPSIS @@ -7,7 +7,7 @@ PCRE2 - Perl-compatible regular expressions (revised API) .B #include .PP .SM -.B int (*pcre2_callout)(pcre2_callout_block *); +.B int (*pcre2_callout)(pcre2_callout_block *, void *); . .SH DESCRIPTION .rs @@ -119,14 +119,19 @@ callouts such as the example above are obeyed. .sp During matching, when PCRE2 reaches a callout point, if an external function is set in the match context, it is called. This applies to both normal and DFA -matching. The only argument to the callout function is a pointer to a -\fBpcre2_callout\fP block. This structure contains the following fields: +matching. The first argument to the callout function is a pointer to a +\fBpcre2_callout\fP block. The second argument is the void * callout data that +was supplied when the callout was set up by calling \fBpcre2_set_callout()\fP +(see the +.\" HREF +\fBpcre2api\fP +.\" +documentation). The callout block structure contains the following fields: .sp uint32_t \fIversion\fP; uint32_t \fIcallout_number\fP; uint32_t \fIcapture_top\fP; uint32_t \fIcapture_last\fP; - void *\fIcallout_data\fP; PCRE2_SIZE *\fIoffset_vector\fP; PCRE2_SPTR \fImark\fP; PCRE2_SPTR \fIsubject\fP; @@ -177,15 +182,6 @@ outside the recursion, as do the values of all captured substrings. If no substrings have been captured, the value of \fIcapture_last\fP is 0. This is always the case for the DFA matching functions. .P -The \fIcallout_data\fP field contains a value that is passed to a matching -function specifically so that it can be passed back in callouts. It is set in -the match context when the callout is set up by calling -\fBpcre2_set_callout()\fP (see the -.\" HREF -\fBpcre2api\fP -.\" -documentation). -.P The \fIpattern_position\fP field contains the offset to the next item to be matched in the pattern string. .P @@ -236,6 +232,6 @@ Cambridge, England. .rs .sp .nf -Last updated: 23 November 2014 +Last updated: 25 November 2014 Copyright (c) 1997-2014 University of Cambridge. .fi diff --git a/src/pcre2.h.in b/src/pcre2.h.in index 04643c1..0993297 100644 --- a/src/pcre2.h.in +++ b/src/pcre2.h.in @@ -324,7 +324,6 @@ typedef struct pcre2_callout_block { \ uint32_t callout_number; /* Number compiled into pattern */ \ uint32_t capture_top; /* Max current capture */ \ uint32_t capture_last; /* Most recently closed capture */ \ - void *callout_data; /* Data passed in with the call */ \ PCRE2_SIZE *offset_vector; /* The offset vector */ \ PCRE2_SPTR mark; /* Pointer to current mark or NULL */ \ PCRE2_SPTR subject; /* The subject being matched */ \ @@ -378,7 +377,7 @@ PCRE2_EXP_DECL \ pcre2_match_context *pcre2_match_context_create(pcre2_general_context *); \ PCRE2_EXP_DECL void pcre2_match_context_free(pcre2_match_context *); \ PCRE2_EXP_DECL int pcre2_set_callout(pcre2_match_context *, \ - int (*)(pcre2_callout_block *), void *); \ + int (*)(pcre2_callout_block *, void *), void *); \ PCRE2_EXP_DECL int pcre2_set_match_limit(pcre2_match_context *, \ uint32_t); \ PCRE2_EXP_DECL int pcre2_set_recursion_limit(pcre2_match_context *, \ diff --git a/src/pcre2_context.c b/src/pcre2_context.c index 9c0b324..7300c1f 100644 --- a/src/pcre2_context.c +++ b/src/pcre2_context.c @@ -331,7 +331,7 @@ return 0; PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION pcre2_set_callout(pcre2_match_context *mcontext, - int (*callout)(pcre2_callout_block *), void *callout_data) + int (*callout)(pcre2_callout_block *, void *), void *callout_data) { mcontext->callout = callout; mcontext->callout_data = callout_data; diff --git a/src/pcre2_dfa_match.c b/src/pcre2_dfa_match.c index 4267e9e..40d8744 100644 --- a/src/pcre2_dfa_match.c +++ b/src/pcre2_dfa_match.c @@ -2615,7 +2615,6 @@ for (;;) cb.callout_number = code[LINK_SIZE+2]; cb.capture_top = 1; cb.capture_last = 0; - cb.callout_data = mb->callout_data; cb.offset_vector = offsets; cb.mark = NULL; /* No (*MARK) support */ cb.subject = start_subject; @@ -2624,7 +2623,8 @@ for (;;) cb.current_position = (PCRE2_SIZE)(ptr - start_subject); cb.pattern_position = GET(code, LINK_SIZE + 3); cb.next_item_length = GET(code, 3 + 2*LINK_SIZE); - if ((rrc = (mb->callout)(&cb)) < 0) return rrc; /* Abandon */ + if ((rrc = (mb->callout)(&cb, mb->callout_data)) < 0) + return rrc; /* Abandon */ } if (rrc > 0) break; /* Fail this thread */ code += PRIV(OP_lengths)[OP_CALLOUT]; /* Skip callout data */ @@ -2962,7 +2962,6 @@ for (;;) cb.callout_number = code[1]; cb.capture_top = 1; cb.capture_last = 0; - cb.callout_data = mb->callout_data; cb.offset_vector = offsets; cb.mark = NULL; /* No (*MARK) support */ cb.subject = start_subject; @@ -2971,7 +2970,8 @@ for (;;) cb.current_position = (PCRE2_SIZE)(ptr - start_subject); cb.pattern_position = GET(code, 2); cb.next_item_length = GET(code, 2 + LINK_SIZE); - if ((rrc = (mb->callout)(&cb)) < 0) return rrc; /* Abandon */ + if ((rrc = (mb->callout)(&cb, mb->callout_data)) < 0) + return rrc; /* Abandon */ } if (rrc == 0) { ADD_ACTIVE(state_offset + PRIV(OP_lengths)[OP_CALLOUT], 0); } diff --git a/src/pcre2_intmodedep.h b/src/pcre2_intmodedep.h index 75e0099..72771b7 100644 --- a/src/pcre2_intmodedep.h +++ b/src/pcre2_intmodedep.h @@ -577,7 +577,7 @@ typedef struct pcre2_real_match_context { pcre2_jit_callback jit_callback; void *jit_callback_data; #endif - int (*callout)(pcre2_callout_block *); + int (*callout)(pcre2_callout_block *, void *); void *callout_data; uint32_t match_limit; uint32_t recursion_limit; @@ -784,7 +784,7 @@ typedef struct match_block { recursion_info *recursive; /* Linked list of recursion data */ ovecsave_frame *ovecsave_chain; /* Linked list of free ovecsave blocks */ void *callout_data; /* To pass back to callouts */ - int (*callout)(pcre2_callout_block *); /* Callout function or NULL */ + int (*callout)(pcre2_callout_block *,void *); /* Callout function or NULL */ #ifdef HEAP_MATCH_RECURSE void *match_frames_base; /* For remembering malloc'd frames */ #endif @@ -809,7 +809,7 @@ typedef struct dfa_match_block { PCRE2_UCHAR nl[4]; /* Newline string when fixed */ uint16_t bsr_convention; /* \R interpretation */ void *callout_data; /* To pass back to callouts */ - int (*callout)(pcre2_callout_block *); /* Callout function or NULL */ + int (*callout)(pcre2_callout_block *,void *); /* Callout function or NULL */ dfa_recursion_info *recursive; /* Linked list of recursion data */ } dfa_match_block; diff --git a/src/pcre2_jit_compile.c b/src/pcre2_jit_compile.c index 50808a3..fc1137f 100644 --- a/src/pcre2_jit_compile.c +++ b/src/pcre2_jit_compile.c @@ -178,7 +178,7 @@ typedef struct jit_arguments { pcre2_match_data *match_data; PCRE2_SPTR startchar_ptr; PCRE2_UCHAR *mark_ptr; - int (*callout)(pcre2_callout_block *); + int (*callout)(pcre2_callout_block *, void *); void *callout_data; /* Everything else after. */ sljit_ui limit_match; @@ -6303,7 +6303,6 @@ if (arguments->callout == NULL) return 0; callout_block->version = 0; -callout_block->callout_data = arguments->callout_data; /* Offsets in subject. */ callout_block->subject_length = arguments->end - arguments->begin; @@ -6325,7 +6324,7 @@ for (i = 2; i < oveccount; i += 2) callout_block->capture_top = (callout_block->capture_top >> 1) + 1; ovector[0] = PCRE2_UNSET; ovector[1] = PCRE2_UNSET; -return (arguments->callout)(callout_block); +return (arguments->callout)(callout_block, arguments->callout_data); } /* Aligning to 8 byte. */ diff --git a/src/pcre2_match.c b/src/pcre2_match.c index 346a796..2053415 100644 --- a/src/pcre2_match.c +++ b/src/pcre2_match.c @@ -1311,7 +1311,6 @@ for (;;) cb.callout_number = ecode[1]; cb.capture_top = offset_top/2; cb.capture_last = mb->capture_last & CAPLMASK; - cb.callout_data = mb->callout_data; cb.offset_vector = mb->ovector; cb.mark = mb->nomatch_mark; cb.subject = mb->start_subject; @@ -1320,7 +1319,8 @@ for (;;) cb.current_position = (PCRE2_SIZE)(eptr - mb->start_subject); cb.pattern_position = GET(ecode, 2); cb.next_item_length = GET(ecode, 2 + LINK_SIZE); - if ((rrc = mb->callout(&cb)) > 0) RRETURN(MATCH_NOMATCH); + if ((rrc = mb->callout(&cb, mb->callout_data)) > 0) + RRETURN(MATCH_NOMATCH); if (rrc < 0) RRETURN(rrc); } @@ -1715,7 +1715,6 @@ for (;;) cb.callout_number = ecode[1]; cb.capture_top = offset_top/2; cb.capture_last = mb->capture_last & CAPLMASK; - cb.callout_data = mb->callout_data; cb.offset_vector = mb->ovector; cb.mark = mb->nomatch_mark; cb.subject = mb->start_subject; @@ -1724,7 +1723,8 @@ for (;;) cb.current_position = (PCRE2_SIZE)(eptr - mb->start_subject); cb.pattern_position = GET(ecode, 2); cb.next_item_length = GET(ecode, 2 + LINK_SIZE); - if ((rrc = mb->callout(&cb)) > 0) RRETURN(MATCH_NOMATCH); + if ((rrc = mb->callout(&cb, mb->callout_data)) > 0) + RRETURN(MATCH_NOMATCH); if (rrc < 0) RRETURN(rrc); } ecode += 2 + 2*LINK_SIZE; diff --git a/src/pcre2test.c b/src/pcre2test.c index 40e8e5e..f585310 100644 --- a/src/pcre2test.c +++ b/src/pcre2test.c @@ -929,11 +929,11 @@ are supported. */ #define PCRE2_SET_CALLOUT(a,b,c) \ if (test_mode == PCRE8_MODE) \ - pcre2_set_callout_8(G(a,8),(int (*)(pcre2_callout_block_8 *))b,c); \ + pcre2_set_callout_8(G(a,8),(int (*)(pcre2_callout_block_8 *, void *))b,c); \ else if (test_mode == PCRE16_MODE) \ - pcre2_set_callout_16(G(a,16),(int (*)(pcre2_callout_block_16 *))b,c); \ + pcre2_set_callout_16(G(a,16),(int (*)(pcre2_callout_block_16 *, void *))b,c); \ else \ - pcre2_set_callout_32(G(a,32),(int (*)(pcre2_callout_block_32 *))b,c); + pcre2_set_callout_32(G(a,32),(int (*)(pcre2_callout_block_32 *, void *))b,c); #define PCRE2_SET_CHARACTER_TABLES(a,b) \ if (test_mode == PCRE8_MODE) \ @@ -1304,10 +1304,10 @@ the three different cases. */ #define PCRE2_SET_CALLOUT(a,b,c) \ if (test_mode == G(G(PCRE,BITONE),_MODE)) \ G(pcre2_set_callout_,BITONE)(G(a,BITONE), \ - (int (*)(G(pcre2_callout_block_,BITONE) *))b,c); \ + (int (*)(G(pcre2_callout_block_,BITONE) *, void *))b,c); \ else \ G(pcre2_set_callout_,BITTWO)(G(a,BITTWO), \ - (int (*)(G(pcre2_callout_block_,BITTWO) *))b,c); + (int (*)(G(pcre2_callout_block_,BITTWO) *, void *))b,c); #define PCRE2_SET_CHARACTER_TABLES(a,b) \ if (test_mode == G(G(PCRE,BITONE),_MODE)) \ @@ -1510,7 +1510,7 @@ the three different cases. */ #define PCRE2_PATTERN_INFO(a,b,c,d) a = pcre2_pattern_info_8(G(b,8),c,d) #define PCRE2_PRINTINT(a) pcre2_printint_8(compiled_code8,outfile,a) #define PCRE2_SET_CALLOUT(a,b,c) \ - pcre2_set_callout_8(G(a,8),(int (*)(pcre2_callout_block_8 *))b,c) + pcre2_set_callout_8(G(a,8),(int (*)(pcre2_callout_block_8 *, void *))b,c) #define PCRE2_SET_CHARACTER_TABLES(a,b) pcre2_set_character_tables_8(G(a,8),b) #define PCRE2_SET_COMPILE_RECURSION_GUARD(a,b) \ pcre2_set_compile_recursion_guard_8(G(a,8),b) @@ -1591,7 +1591,7 @@ the three different cases. */ #define PCRE2_PATTERN_INFO(a,b,c,d) a = pcre2_pattern_info_16(G(b,16),c,d) #define PCRE2_PRINTINT(a) pcre2_printint_16(compiled_code16,outfile,a) #define PCRE2_SET_CALLOUT(a,b,c) \ - pcre2_set_callout_16(G(a,16),(int (*)(pcre2_callout_block_16 *))b,c); + pcre2_set_callout_16(G(a,16),(int (*)(pcre2_callout_block_16 *, void *))b,c); #define PCRE2_SET_CHARACTER_TABLES(a,b) pcre2_set_character_tables_16(G(a,16),b) #define PCRE2_SET_COMPILE_RECURSION_GUARD(a,b) \ pcre2_set_compile_recursion_guard_16(G(a,16),b) @@ -1672,7 +1672,7 @@ the three different cases. */ #define PCRE2_PATTERN_INFO(a,b,c,d) a = pcre2_pattern_info_32(G(b,32),c,d) #define PCRE2_PRINTINT(a) pcre2_printint_32(compiled_code32,outfile,a) #define PCRE2_SET_CALLOUT(a,b,c) \ - pcre2_set_callout_32(G(a,32),(int (*)(pcre2_callout_block_32 *))b,c); + pcre2_set_callout_32(G(a,32),(int (*)(pcre2_callout_block_32 *, void *))b,c); #define PCRE2_SET_CHARACTER_TABLES(a,b) pcre2_set_character_tables_32(G(a,32),b) #define PCRE2_SET_COMPILE_RECURSION_GUARD(a,b) \ pcre2_set_compile_recursion_guard_32(G(a,32),b) @@ -4126,7 +4126,7 @@ Return: */ static int -callout_function(pcre2_callout_block_8 *cb) +callout_function(pcre2_callout_block_8 *cb, void *callout_data_ptr) { uint32_t i, pre_start, post_start, subject_length; BOOL utf = (FLD(compiled_code, overall_options) & PCRE2_UTF) != 0; @@ -4216,9 +4216,9 @@ if (cb->mark != last_callout_mark) last_callout_mark = cb->mark; } -if (cb->callout_data != NULL) +if (callout_data_ptr != NULL) { - int callout_data = *((int32_t *)(cb->callout_data)); + int callout_data = *((int32_t *)callout_data_ptr); if (callout_data != 0) { fprintf(outfile, "Callout data = %d\n", callout_data);