aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/PCRE.cc
blob: afa6e954b43717c79fb1ac6cb3f4b42a61ab644a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365


#include <qpdf/PCRE.hh>
#include <qpdf/QUtil.hh>

#include <iostream>

PCRE::Exception::Exception(std::string const& message)
{
    this->setMessage("PCRE error: " + message);
}

PCRE::NoBackref::NoBackref() :
    Exception("no match")
{
}

PCRE::Match::Match(int nbackrefs, char const* subject)
{
    this->init(-1, nbackrefs, subject);
}

PCRE::Match::~Match()
{
    this->destroy();
}

PCRE::Match::Match(Match const& rhs)
{
    this->copy(rhs);
}

PCRE::Match&
PCRE::Match::operator=(Match const& rhs)
{
    if (this != &rhs)
    {
	this->destroy();
	this->copy(rhs);
    }
    return *this;
}

void
PCRE::Match::init(int nmatches, int nbackrefs, char const* subject)
{
    this->nmatches = nmatches;
    this->nbackrefs = nbackrefs;
    this->subject = subject;
    this->ovecsize = 3 * (1 + nbackrefs);
    this->ovector = 0;
    if (this->ovecsize)
    {
	this->ovector = new int[this->ovecsize];
    }
}

void
PCRE::Match::copy(Match const& rhs)
{
    this->init(rhs.nmatches, rhs.nbackrefs, rhs.subject);
    int i;
    for (i = 0; i < this->ovecsize; ++i)
    {
	this->ovector[i] = rhs.ovector[i];
    }
}

void
PCRE::Match::destroy()
{
    delete [] this->ovector;
}

PCRE::Match::operator bool()
{
    return (this->nmatches >= 0);
}


std::string
PCRE::Match::getMatch(int n, int flags)
    throw(QEXC::General, Exception)
{
    // This method used to be implemented in terms of
    // pcre_get_substring, but that function gives you an empty string
    // for an unmatched backreference that is in range.

    int offset;
    int length;
    try
    {
	getOffsetLength(n, offset, length);
    }
    catch (NoBackref&)
    {
	if (flags & gm_no_substring_returns_empty)
	{
	    return "";
	}
	else
	{
	    throw;
	}
    }

    return std::string(this->subject).substr(offset, length);
}

void
PCRE::Match::getOffsetLength(int n, int& offset, int& length) throw(Exception)
{
    if ((this->nmatches < 0) ||
	(n > this->nmatches - 1) ||
	(this->ovector[n * 2] == -1))
    {
	throw NoBackref();
    }
    offset = this->ovector[n * 2];
    length = this->ovector[n * 2 + 1] - offset;
}


int
PCRE::Match::getOffset(int n) throw(Exception)
{
    int offset;
    int length;
    this->getOffsetLength(n, offset, length);
    return offset;
}


int
PCRE::Match::getLength(int n) throw(Exception)
{
    int offset;
    int length;
    this->getOffsetLength(n, offset, length);
    return length;
}


int
PCRE::Match::nMatches() const
{
    return this->nmatches;
}

PCRE::PCRE(char const* pattern, int options) throw (Exception)
{
    char const *errptr;
    int erroffset;
    this->code = pcre_compile(pattern, options, &errptr, &erroffset, 0);
    if (this->code)
    {
	this->nbackrefs = pcre_info(this->code, 0, 0);
    }
    else
    {
	std::string message = (std::string("compilation of ") + pattern +
			  " failed at offset " +
			  QUtil::int_to_string(erroffset) + ": " +
			  errptr);
	throw Exception(message);
    }
}

PCRE::~PCRE()
{
    pcre_free(this->code);
}

PCRE::Match
PCRE::match(char const* subject, int options, int startoffset, int size)
    throw (QEXC::General, Exception)
{
    if (size == -1)
    {
	size = strlen(subject);
    }

    Match result(this->nbackrefs, subject);
    int status = pcre_exec(this->code, 0, subject, size,
			   startoffset, options,
			   result.ovector, result.ovecsize);
    if (status >= 0)
    {
	result.nmatches = status;
    }
    else
    {
	std::string message;

	switch (status)
	{
	  case PCRE_ERROR_NOMATCH:
	    break;

	  case PCRE_ERROR_BADOPTION:
	    message = "bad option passed to PCRE::match()";
	    throw Exception(message);
	    break;

	  case PCRE_ERROR_NOMEMORY:
	    message = "insufficient memory";
	    throw Exception(message);
	    break;

	  case PCRE_ERROR_NULL:
	  case PCRE_ERROR_BADMAGIC:
	  case PCRE_ERROR_UNKNOWN_NODE:
	  default:
	    message = "pcre_exec returned " + QUtil::int_to_string(status);
	    throw QEXC::Internal(message);
	}
    }

    return result;
}

void
PCRE::test(int n)
{
    try
    {
	if (n == 1)
	{
	    static char const* utf8 = "abπdefq";
	    PCRE u1("^([[:alpha:]]+)");
	    PCRE u2("^([\\p{L}]+)", PCRE_UTF8);
	    PCRE::Match m1 = u1.match(utf8);
	    if (m1)
	    {
		std::cout << "no utf8: " << m1.getMatch(1) << std::endl;
	    }
	    PCRE::Match m2 = u2.match(utf8);
	    if (m2)
	    {
		std::cout << "utf8: " << m2.getMatch(1) << std::endl;
	    }
	    return;
	}

	try
	{
	    PCRE pcre1("a**");
	}
	catch (Exception& e)
	{
	    std::cout << e.unparse() << std::endl;
	}

	PCRE pcre2("^([^\\s:]*)\\s*:\\s*(.*?)\\s*$");
	PCRE::Match m2 = pcre2.match("key: value one two three ");
	if (m2)
	{
	    std::cout << m2.nMatches() << std::endl;
	    std::cout << m2.getMatch(0) << std::endl;
	    std::cout << m2.getOffset(0) << std::endl;
	    std::cout << m2.getLength(0) << std::endl;
	    std::cout << m2.getMatch(1) << std::endl;
	    std::cout << m2.getOffset(1) << std::endl;
	    std::cout << m2.getLength(1) << std::endl;
	    std::cout << m2.getMatch(2) << std::endl;
	    std::cout << m2.getOffset(2) << std::endl;
	    std::cout << m2.getLength(2) << std::endl;
	    try
	    {
		std::cout << m2.getMatch(3) << std::endl;
	    }
	    catch (Exception& e)
	    {
		std::cout << e.unparse() << std::endl;
	    }
	    try
	    {
		std::cout << m2.getOffset(3) << std::endl;
	    }
	    catch (Exception& e)
	    {
		std::cout << e.unparse() << std::endl;
	    }
	}
	PCRE pcre3("^(a+)(b+)?$");
	PCRE::Match m3 = pcre3.match("aaa");
	try
	{
	    if (m3)
	    {
		std::cout << m3.nMatches() << std::endl;
		std::cout << m3.getMatch(0) << std::endl;
		std::cout << m3.getMatch(1) << std::endl;
		std::cout << "-"
			  << m3.getMatch(
			      2, Match::gm_no_substring_returns_empty)
			  << "-" << std::endl;
		std::cout << "hello" << std::endl;
		std::cout << m3.getMatch(2) << std::endl;
		std::cout << "can't see this" << std::endl;
	    }
	}
	catch (Exception& e)
	{
	    std::cout << e.unparse() << std::endl;
	}

	// backref: 1   2 3        4      5
	PCRE pcre4("^((?:(a(b)?)(?:,(c))?)|(c))?$");
	static char const* candidates[] = {
	    "qqqcqqq",		// no match
	    "ab,c",		// backrefs: 0, 1, 2, 3, 4
	    "ab",		// backrefs: 0, 1, 2, 3
	    "a",		// backrefs: 0, 1, 2
	    "a,c",		// backrefs: 0, 1, 2, 4
	    "c",		// backrefs: 0, 1, 5
	    "",			// backrefs: 0
	    0
	};
	for (char const** p = candidates; *p; ++p)
	{
	    PCRE::Match m(pcre4.match(*p));
	    if (m)
	    {
		int nmatches = m.nMatches();
		for (int i = 0; i < nmatches; ++i)
		{
		    std::cout << *p << ": " << i << ": ";
		    try
		    {
			std::string match = m.getMatch(i);
			std::cout << match;
		    }
		    catch (NoBackref&)
		    {
			std::cout << "no backref (getMatch)";
		    }
		    std::cout << std::endl;

		    std::cout << *p << ": " << i << ": ";
		    try
		    {
			int offset;
			int length;
			m.getOffsetLength(i, offset, length);
			std::cout << offset << ", " << length;
		    }
		    catch (NoBackref&)
		    {
			std::cout << "no backref (getOffsetLength)";
		    }
		    std:: cout << std::endl;
		}
	    }
	    else
	    {
		std::cout << *p << ": no match" << std::endl;
	    }
	}
    }
    catch (QEXC::General& e)
    {
	std::cout << "unexpected exception: " << e.unparse() << std::endl;
    }
}