File: Quote.xs

package info (click to toggle)
libxml-quote-perl 1.02-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 84 kB
  • sloc: perl: 259; makefile: 3
file content (486 lines) | stat: -rw-r--r-- 8,569 bytes parent folder | download | duplicates (4)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* $Version: release/perl/base/XML-Quote/Quote.xs,v 1.12 2003/01/31 09:16:58 godegisel Exp $ */

#ifdef __cplusplus
extern "C" {
#endif

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#ifdef __cplusplus
}
#endif

#define XQ_DEBUG	0

#ifdef SVf_UTF8
#define XML_Util_UTF8
#endif

//////////////////////////////////////////////////////////////////////////////

// '"'==0x22	"	5
// '&'==0x26	&	4
// '\''==0x27	'	5
// '<'==0x3C	&lt;	3
// '>'==0x3E	&gt;	3

static STRLEN XQ_quote_add[] = {
// 0x00 - 0x0F
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
// 0x10 - 0x1F
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
// 0x20 - 0x2F
0,0,5,0,0,0,4,5,0,0,0,0,0,0,0,0,
// 0x30 - 0x3F
0,0,0,0,0,0,0,0,0,0,0,0,3,0,3,0,
};

static STRLEN XQ_quote_add_min[] = {
// 0x00 - 0x0F
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
// 0x10 - 0x1F
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
// 0x20 - 0x2F
0,0,5,0,0,0,4,0,0,0,0,0,0,0,0,0,
// 0x30 - 0x3F
0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,
};

//////////////////////////////////////////////////////////////////////////////

static SV *
xml_quote(SV * srcSV)	{
	SV      * dstSV;
	unsigned char	* src, * src2;
	unsigned char	* dst;
	unsigned char c;
	STRLEN  src_len, src_len2, dst_len, offset;

	src=SvPV(srcSV, src_len);	//length without trailing \0

#if XQ_DEBUG
	warn("source=%s\n",src);
	warn("src=%p\n",src);
#endif

	dst_len=src_len;
	src2=src;
	src_len2=src_len;

#if XQ_DEBUG
	warn("source length=%i\n",src_len);
#endif

	// calculate target string length
	while(src_len2--)	{
		c=*src2++;
		if(c < 0x3F && (offset=XQ_quote_add[c]) )	{
#if XQ_DEBUG
			warn("add offset=%i [symbol=%i]\n",offset,c);
#endif
			dst_len+=offset;
		}
/* table lookup is faster (or not?...)
		if(c > 0x3E || c < 0x22)	{	// || < 0x22
			continue;
		}
		if('&'==c)		{	// 0x26
   			// &amp;
			dst_len+=4;
   		}else if('"'==c)	{	// 0x22
   			// &quot;
			dst_len+=5;
   		}else if('\''==c)	{	// 0x27
			// &apos;
   			dst_len+=5;
		}else if('<'==c)	{	// 0x3C
   			// &lt;
			dst_len+=3;
   		}else if('>'==c)	{	// 0x3E
			// &gt;
   			dst_len+=3;
		}//if
*/
	}//while

        if(dst_len == src_len)	{
#if XQ_DEBUG
		warn("nothing to quote\n");
#endif
        	// nothing to quote
		dstSV=newSVpv(src, dst_len);
#ifdef XML_Util_UTF8
   		if(SvUTF8(srcSV))
   			SvUTF8_on(dstSV);
#endif
		return dstSV;
        }

#if XQ_DEBUG
	warn("dest length=%i\n",dst_len);
#endif

   	dstSV=newSV(dst_len);
	SvCUR_set(dstSV, dst_len);
	SvPOK_on(dstSV);
#ifdef XML_Util_UTF8
   	if(SvUTF8(srcSV))
		SvUTF8_on(dstSV);
#endif

   	dst=SvPVX(dstSV);

   	while(src_len--)	{	// \0 also copied
		c=*src++;
#if XQ_DEBUG
		warn("src_len=%i symbol=%c src=%lx\n",src_len,c,src);
#endif
//		if(c > 0x3E || c < 0x22)	{
		if(c > 0x3E || ! XQ_quote_add[c])	{
			*dst++=c;
			continue;
		}
/*	it's overkill
#ifdef XML_Util_UTF8
		// really 'dst_len' has other semantic (i.e. 'sym_len')
		// I just reuse it ;)
		dst_len=PL_utf8skip[c];	//dst_len=UTF8SKIP(c);
		if(dst_len > 1)	{
   			*dst++=c;
			while(--dst_len)	{
				*dst++=*src++;
				src_len--;
			}
			continue;
		}
#endif
*/
		*dst++='&';
		if('&'==c)		{	// 0x26
   			// &amp;
			*dst++='a'; *dst++='m'; *dst++='p';
		}else if('>'==c)	{	// 0x3E
   			// &gt;
   			*dst++='g'; *dst++='t';
		}else if('<'==c)	{	// 0x3C
   			// &lt;
   			*dst++='l'; *dst++='t';
		}else if('"'==c)	{	// 0x22
   			// &quot;
			*dst++='q'; *dst++='u'; *dst++='o'; *dst++='t';
//		}else if('\''==c)	{	// 0x27
		}else	{
   			// &apos;
   			*dst++='a'; *dst++='p'; *dst++='o'; *dst++='s';
   		}//if
		*dst++=';';
	}//while

	return dstSV;
}
//////////////////////////////////////////////////////////////////////////////

static SV *
xml_quote_min(SV * srcSV)	{
	SV      * dstSV;
	unsigned char	* src, * src2;
	unsigned char	* dst;
	unsigned char c;
	STRLEN  src_len, src_len2, dst_len, offset;

	src=SvPV(srcSV, src_len);	//length without trailing \0

#if XQ_DEBUG
	warn("source=%s\n",src);
	warn("src=%p\n",src);
#endif

	dst_len=src_len;
	src2=src;
	src_len2=src_len;

#if XQ_DEBUG
	warn("source length=%i\n",src_len);
#endif

	// calculate target string length
	while(src_len2--)	{
		c=*src2++;
		if(c < 0x3D && (offset=XQ_quote_add_min[c]) )	{
#if XQ_DEBUG
			warn("add offset=%i [symbol=%i]\n",offset,c);
#endif
			dst_len+=offset;
		}
	}//while

        if(dst_len == src_len)	{
#if XQ_DEBUG
		warn("nothing to quote\n");
#endif
        	// nothing to quote
		dstSV=newSVpv(src, dst_len);
#ifdef XML_Util_UTF8
   		if(SvUTF8(srcSV))
   			SvUTF8_on(dstSV);
#endif
		return dstSV;
        }

#if XQ_DEBUG
	warn("dest length=%i\n",dst_len);
#endif

   	dstSV=newSV(dst_len);
	SvCUR_set(dstSV, dst_len);
	SvPOK_on(dstSV);
#ifdef XML_Util_UTF8
   	if(SvUTF8(srcSV))
		SvUTF8_on(dstSV);
#endif

   	dst=SvPVX(dstSV);

   	while(src_len--)	{	// \0 also copied
		c=*src++;
#if XQ_DEBUG
		warn("src_len=%i symbol=%c src=%lx\n",src_len,c,src);
#endif
		if(c > 0x3C || ! XQ_quote_add_min[c])	{
			*dst++=c;
			continue;
		}

		*dst++='&';
		if('&'==c)		{	// 0x26
   			// &amp;
			*dst++='a'; *dst++='m'; *dst++='p';
		}else if('<'==c)	{	// 0x3C
   			// &lt;
   			*dst++='l'; *dst++='t';
//		}else if('"'==c)	{	// 0x22
		}else	{
   			// &quot;
			*dst++='q'; *dst++='u'; *dst++='o'; *dst++='t';
   		}//if
		*dst++=';';
	}//while

	return dstSV;
}
//////////////////////////////////////////////////////////////////////////////
static SV *
xml_dequote(SV * srcSV)	{
	SV      * dstSV;
	unsigned char	* src, *src2;
	unsigned char	* dst;
	unsigned char c,c1,c2,c3,c4;
	STRLEN  src_len, src_len2, dst_len;

	src=SvPV(srcSV, src_len);	//length without trailing \0
//	warn("src_len=%i",src_len);
	src2=src;
	src_len2=src_len;
	dst_len=src_len;

	// calculate dequoted string length
	while(src_len >=3)	{
		c=*src++;
		src_len--;

		if('&'!=c)	{
			continue;
		}
		/*
		&amp;
		&quot;
		&apos;
		&lt;
		&gt;
		*/
		c=*src;
		c1=*(src+1);
		c2=*(src+2);
		if(c2==';' && c1=='t' && (c=='l' || c=='g'))	{
			dst_len-=3;
			src+=3;
   			src_len-=3;
			continue;
		}

		if(src_len >= 4)	{
			c3=*(src+3);
		}else	{
			continue;
		}

		if(c=='a' && c1=='m' && c2=='p' && c3==';')	{
			dst_len-=4;
			src+=4;
   			src_len-=4;
			continue;
		}

		if(src_len >= 5)	{
			c4=*(src+4);
		}else	{
			continue;
		}

		if(c4==';'
		&& (
			(c=='q' && c1=='u' && c2=='o' && c3=='t')
			||
			(c=='a' && c1=='p' && c2=='o' && c3=='s')
		   )
		)  {
			dst_len-=5;
			src+=5;
			src_len-=5;
			continue;
		}//if
	}//while

        if(dst_len == src_len2)	{
        	// nothing to dequote
		dstSV=newSVpv(src2, dst_len);
#ifdef XML_Util_UTF8
   		if(SvUTF8(srcSV))
   			SvUTF8_on(dstSV);
#endif
//		warn("nothing to dequote");
		return dstSV;
        }

//   	dstSV=newSVpv("", dst_len);
   	dstSV=newSV(dst_len);
	SvCUR_set(dstSV, dst_len);
	SvPOK_on(dstSV);
#ifdef XML_Util_UTF8
   	if(SvUTF8(srcSV))
		SvUTF8_on(dstSV);
#endif
	dst=SvPVX(dstSV);

   	while(src_len2>=3)	{	// 3 is min length of quoted symbol
		c=*src2++;
		src_len2--;
		if('&'!=c)	{
			*dst++=c;
			continue;
		}
		c=*src2;
		c1=*(src2+1);
		c2=*(src2+2);

		// 1. test len=3: &lt; &gt;
		if(c1=='t' && c2==';')	{
			if(c=='l')	{
				*dst++='<';
				src2+=3;
				src_len2-=3;
				continue;
			}else if(c=='g')	{
				*dst++='>';
			}else	{
				*dst++='&';
				continue;
			}
   			src2+=3;
   			src_len2-=3;
   			continue;
		}//if lt | gt

		
		// 2. test len=4: &amp;
		if(src_len2 >= 4)	{
			c3=*(src2+3);
		}else	{
			*dst++='&';
			continue;
		}

		if(c=='a' && c1=='m' && c2=='p' && c3==';')	{
			*dst++='&';
			src2+=4;
   			src_len2-=4;
			continue;
		}

		// 3. test len=5: &quot; &apos;
		if(src_len2 >= 5)	{
			c4=*(src2+4);
		}else	{
			*dst++='&';
			continue;
		}

		if(c4==';')	{
			if(c=='q' && c1=='u' && c2=='o' && c3=='t')	{
				*dst++='"';
			}else if(c=='a' && c1=='p' && c2=='o' && c3=='s') {
				*dst++='\'';
			}else	{
				*dst++='&';
				continue;
			}
			src2+=5;
   			src_len2-=5;
			continue;
		}//if ;

		*dst++='&';
	}//while


   	while(src_len2-- > 0)	{	// also copy trailing \0
		*dst++=*src2++;
	}

	return dstSV;
}
//////////////////////////////////////////////////////////////////////////////

MODULE = XML::Quote		PACKAGE = XML::Quote


SV *
xml_quote(string)
   SV *string
   INIT:
	if(!SvOK(string))	{
		XSRETURN_UNDEF;
	}
   CODE:
	RETVAL = xml_quote(string);
   OUTPUT:
	RETVAL


SV *
xml_quote_min(string)
   SV *string
   INIT:
	if(!SvOK(string))	{
		XSRETURN_UNDEF;
	}
   CODE:
	RETVAL = xml_quote_min(string);
   OUTPUT:
	RETVAL


SV *
xml_dequote(string)
   SV *string
   INIT:
	if(!SvOK(string))	{
		XSRETURN_UNDEF;
	}
   CODE:
	RETVAL = xml_dequote(string);
   OUTPUT:
	RETVAL