-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.c
1503 lines (1320 loc) · 33.8 KB
/
util.c
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ctype.h>
#include <time.h>
#include <apr.h>
#include <apr_strings.h>
#include <apr_file_io.h>
#include <apr_date.h>
#include <apr_sha1.h>
#include <httpd.h>
#include <libxml/entities.h>
#include <libxml/HTMLparser.h>
#include "private.h"
#include "buffer.h"
#include "db.h"
#include "req.h"
#include "wiki.h"
#include "util.h"
static const char UTF8length[256] = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,6,6
};
static const char basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/**
* RSR note: This is the UTF8ToHTML function out of libxml2 with a fix that
* properly parses UTF-8 documents containing characters for which there
* are no named entity values in HTML (e.g. Chinese characters). Instead,
* these are simply converted to numerical entity values. A patch was
* submitted and accepted but we'll have to include the code here if we
* want to work with libxml2 v2.6.27 and earlier. Once all major distros
* include a new version we can pull this and start relying on libxml2.
*
* UTF8ToHtml:
* @out: a pointer to an array of bytes to store the result
* @outlen: the length of @out
* @in: a pointer to an array of UTF-8 chars
* @inlen: the length of @in
*
* Take a block of UTF-8 chars in and try to convert it to an ASCII
* plus HTML entities block of chars out.
*
* Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
* The value of @inlen after return is the number of octets consumed
* as the return value is positive, else unpredictable.
* The value of @outlen after return is the number of octets consumed.
*/
static int
virgule_UTF8ToHtml(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen) {
const unsigned char* processed = in;
const unsigned char* outend;
const unsigned char* outstart = out;
const unsigned char* instart = in;
const unsigned char* inend;
unsigned int c, d;
int trailing;
if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1);
if (in == NULL) {
/*
* initialization nothing to do
*/
*outlen = 0;
*inlen = 0;
return(0);
}
inend = in + (*inlen);
outend = out + (*outlen);
while (in < inend) {
d = *in++;
if (d < 0x80) { c= d; trailing= 0; }
else if (d < 0xC0) {
/* trailing byte in leading position */
*outlen = out - outstart;
*inlen = processed - instart;
return(-2);
} else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
else {
/* no chance for this in Ascii */
*outlen = out - outstart;
*inlen = processed - instart;
return(-2);
}
if (inend - in < trailing) {
break;
}
for ( ; trailing; trailing--) {
if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
break;
c <<= 6;
c |= d & 0x3F;
}
/* assertion: c is a single UTF-4 value */
if (c < 0x80) {
if (out + 1 >= outend)
break;
*out++ = c;
} else {
int len;
const htmlEntityDesc * ent;
const char *cp;
char nbuf[16];
/*
* Try to lookup a predefined HTML entity for it
*/
ent = htmlEntityValueLookup(c);
if (ent == NULL) {
snprintf(nbuf, sizeof(nbuf), "#%u", c);
cp = nbuf;
}
else
cp = ent->name;
len = strlen(cp);
if (out + 2 + len >= outend)
break;
*out++ = '&';
memcpy(out, cp, len);
out += len;
*out++ = ';';
}
processed = in;
}
*outlen = out - outstart;
*inlen = processed - instart;
return(0);
}
static char *
b64enc (apr_pool_t *p, const char *data, int size)
{
char *result;
int result_blocks;
int i;
result_blocks = ((size + 2) / 3);
result = apr_pcalloc (p, 4 * result_blocks + 1);
for (i = 0; i < result_blocks; i++)
{
int rem = size - i * 3;
int d1 = data[i * 3] & 0xff;
int d2 = rem > 1 ? data[i * 3 + 1] & 0xff : 0;
int d3 = rem > 2 ? data[i * 3 + 2] & 0xff : 0;
result[i * 4] = basis_64[(d1 >> 2) & 0x3f];
result[i * 4 + 1] = basis_64[((d1 & 0x03) << 4) | ((d2 & 0xf0) >> 4)];
result[i * 4 + 2] = rem > 1 ? basis_64[((d2 & 0x0f) << 2) |
((d3 & 0xc0) >> 6)] : '=';
result[i * 4 + 3] = rem > 2 ? basis_64[d3 & 0x3f] : '=';
}
return result;
}
/**
* virgule_rand_cookie: Create a new, random cookie.
* @p: pool in which to allocate.
*
* Creates a new, base-64 encoded random cookie. The cookie has 120
* bits of entropy, which should be enough for even the paranoid, and
* also aligns nicely in base64.
*
* Return value: The random cookie.
**/
char *
virgule_rand_cookie (apr_pool_t *p)
{
apr_file_t *fd;
apr_size_t bytes_read;
char buf[15];
if (apr_file_open (&fd, "/dev/random", APR_READ,
APR_UREAD|APR_UWRITE|APR_GWRITE|APR_WREAD, p) != APR_SUCCESS)
return NULL;
bytes_read = sizeof(buf);
apr_file_read (fd, buf, &bytes_read);
apr_file_close (fd);
if (bytes_read < sizeof(buf))
{
return NULL;
}
return b64enc (p, buf, sizeof(buf));
}
const char *
virgule_match_prefix (const char *url, const char *prefix)
{
int len;
len = strlen (prefix);
if (!strncmp (url, prefix, len))
return url + len;
else
return NULL;
}
/**
* escape_noniso_char: Provide a plausible replacement for non-iso characters.
* @c: Character to escape.
*
* If a character is not an ISO printable, provide a replacement. Otherwise,
* return NULL.
*
* I was very tempted to name this routine "defenestrate_char", as a pun
* on "escape windows char". Cooler heads prevailed.
*
* Reference: http://czyborra.com/charsets/codepages.html#CP1252
*
* Return value: Replacement string.
**/
static const char *
escape_noniso_char (char c)
{
int u = c & 0xff;
if ((u >= 0x20 && u <= 0x80) ||
u >= 0xa0)
return NULL;
switch (u)
{
case 0x80:
return "[Euro]";
case 0x82:
return ",";
case 0x83:
return "f";
case 0x84:
return ",,";
case 0x85:
return "...";
case 0x86:
return "[dagger]";
case 0x87:
return "[dbldagger]";
case 0x88:
return "^";
case 0x89:
return "%0";
case 0x8A:
return "S";
case 0x8B:
return "<";
case 0x8C:
return "OE";
case 0x8E:
return "Z";
case 0x91:
return "`";
case 0x92:
return "'";
case 0x93:
return "``";
case 0x94:
return "''";
case 0x95:
return "*";
case 0x96:
return "-";
case 0x97:
return "--";
case 0x98:
return "~";
case 0x99:
return "[TM]";
case 0x9A:
return "s";
case 0x9B:
return ">";
case 0x9C:
return "oe";
case 0x9E:
return "z";
case 0x9F:
return "Y";
default:
return "";
}
}
static void
nice_text_cat (char *buf, int *p_j, const char *src, int size)
{
if (buf) memcpy (buf + *p_j, src, size);
*p_j += size;
}
static int
nice_text_helper (const char *raw, char *buf)
{
int i;
int j;
int nl_state = 0;
const char *replacement;
j = 0;
for (i = 0; raw[i]; i++)
{
char c = raw[i];
if (c == '\n')
{
nice_text_cat (buf, &j, "\n", 1);
if (nl_state == 3)
nl_state = 1;
else if (nl_state == 1)
nl_state = 2;
}
else if (c != '\r')
{
if (nl_state == 2)
nice_text_cat (buf, &j, "<p> ", 4);
nl_state = 3;
if (c == '&')
nice_text_cat (buf, &j, "&", 5);
else if (c == '<')
nice_text_cat (buf, &j, "<", 4);
else if (c == '>')
nice_text_cat (buf, &j, ">", 4);
else if ((replacement = escape_noniso_char (c)) != NULL)
nice_text_cat (buf, &j, replacement, strlen (replacement));
else
{
if (buf) buf[j] = c;
j++;
}
}
}
return j;
}
/**
* virgule_nice_UTF8: Convert raw UTF8 into nice HTML. Outlength is probably
* overkill. It assumes every input character is converted to a 6 byte
* entity in the output buffer.
* @raw: Raw UTF8
*
* Return value: HTML formatted text
**/
char *
virgule_nice_utf8 (apr_pool_t *p, const char *utf8)
{
int inlen = strlen(utf8);
int outlen = inlen * 6;
char *out = NULL;
if (utf8 == NULL)
return NULL;
out = apr_palloc (p, outlen + 1);
if(out == NULL)
return NULL;
memset(out,0,outlen);
if(virgule_UTF8ToHtml ((unsigned char *)out,&outlen,(unsigned char *)utf8,&inlen) == 0)
{
out[outlen] = 0;
return out;
}
return NULL;
}
/**
* nice_text: Convert raw text into nice HTML.
* @raw: Raw text.
*
* Return value: HTML formatted text.
**/
char *
virgule_nice_text (apr_pool_t *p, const char *raw)
{
char *result;
int size;
if (raw == NULL)
return NULL;
size = nice_text_helper (raw, NULL);
result = apr_palloc (p, size + 1);
nice_text_helper (raw, result);
result[size] = '\0';
return result;
}
static char *
nice_person_link (VirguleReq *vr, const char *name)
{
apr_pool_t *p = vr->r->pool;
int i;
for (i = 0; name[i]; i++)
if (!isalnum (name[i]))
return apr_psprintf (p, "<person>%s</person>", name);
return apr_psprintf (p, "<a href=\"%s/person/%s/\">%s</a>",
vr->prefix, ap_os_escape_path(p, name, 1), name);
}
static char *
nice_proj_link (VirguleReq *vr, const char *proj)
{
apr_pool_t *p = vr->r->pool;
if (strchr (proj, '/') || proj[0] == '.' || strlen (proj) > 30)
return apr_psprintf (p, "<proj>%s</proj>", proj);
return apr_psprintf (p, "<a href=\"%s/proj/%s/\">%s</a>",
vr->prefix, ap_os_escape_path (p, proj, 1), proj);
}
/**
* The passed string will be modified to make it a legal CSS1 class name.
* CSS1 class names MUST start with an [A-Za-z]. Subsequent characters MUST
* be [A-Za-z0-9] or '-'. All illegal characters must be escaped using a
* prefix of '\' (e.g. 2foo becomes \2foo and foo_bar becomes foo\_bar).
*/
char *
virgule_force_legal_css_name (VirguleReq *vr, const char *name)
{
int inlen = strlen(name);
int outlen = inlen * 3;
int i = 0;
int o = 0;
char *cssname = NULL;
if (name == NULL)
return NULL;
cssname = apr_palloc (vr->r->pool, outlen + 1);
if(cssname == NULL)
return NULL;
memset(cssname,0,outlen);
if (!isalpha(name[0]))
{
cssname[o++] = '\\';
cssname[o++] = name[0];
}
else
cssname[o++] = name[0];
for (i = 1; name[i]; i++,o++)
{
if (name[i] == ' ')
{
cssname[o++] = '\\';
cssname[o++] = '2';
cssname[o] = '0';
}
else if (!isalnum(name[i]) && !name[i] != '-')
{
cssname[o++] = '\\';
cssname[o] = name[i];
}
else
cssname[o] = name[i];
}
cssname[o] = 0;
return cssname;
}
struct _Topic {
char *desc;
char *url;
};
/**
* add_topic - Allocates a Topic structures during loading
* of the site configuration. This information must survive across
* multiple requests so it uses the thread private pool.
*/
const Topic *
virgule_add_topic (VirguleReq *vr, const char *desc, const char *url)
{
Topic *topic;
topic = apr_palloc (vr->priv->pool, sizeof(Topic));
topic->desc = apr_pstrdup (vr->priv->pool, desc);
topic->url = apr_pstrdup (vr->priv->pool, url);
return topic;
}
struct _NavOption {
char *label;
char *url;
};
/**
* add_nav_option - Allocates a NavOption structures during loading
* of the site configuration. This information must survive across
* multiple requests so it uses the thread private pool.
*/
const NavOption *
virgule_add_nav_option (VirguleReq *vr, const char *label, const char *url)
{
NavOption *option;
option = apr_palloc (vr->priv->pool, sizeof(NavOption));
option->label = apr_pstrdup (vr->priv->pool, label);
option->url = apr_pstrdup (vr->priv->pool, url);
return option;
}
struct _AllowedTag {
char *tagname;
int empty;
char **allowed_attributes;
char *(*handler) (VirguleReq *vr, const char *str);
};
static AllowedTag special_allowed_tags[] = {
{ "person", 0, NULL, nice_person_link },
{ "proj", 0, NULL, nice_proj_link },
{ "project", 0, NULL, nice_proj_link },
{ "wiki", 0, NULL, virgule_wiki_link },
{ "youtube", 0, NULL, virgule_youtube_link }
};
/**
* add_allowed_tag - Allocates an AllowedTag structures during loading
* of the site configuration. This information must survive across
* multiple requests so it uses the thread private pool.
*/
const AllowedTag *
virgule_add_allowed_tag (VirguleReq *vr, const char *tagname, int can_be_empty,
char **allowed_attributes)
{
AllowedTag *tag;
int i, n = sizeof (special_allowed_tags) / sizeof (special_allowed_tags[0]);
/* special tags are handled specially */
for (i = 0; i < n; i++)
if (!strcmp(special_allowed_tags[i].tagname, tagname))
return &special_allowed_tags[i];
tag = apr_palloc (vr->priv->pool, sizeof(AllowedTag));
tag->tagname = apr_pstrdup (vr->priv->pool, tagname);
tag->empty = can_be_empty;
tag->allowed_attributes = allowed_attributes;
tag->handler = NULL;
return tag;
}
int
virgule_render_acceptable_html (VirguleReq *vr)
{
const AllowedTag **tag;
virgule_buffer_printf (vr->b, "<p> The following <a href=\"%s/html.html\">HTML</a> "
"is accepted: ", vr->prefix);
for (tag = vr->priv->allowed_tags; *tag; tag++)
virgule_buffer_printf (vr->b, "<%s> ", (*tag)->tagname);
virgule_buffer_puts (vr->b, "</p>\n");
return 0;
}
/**
* match_tag: Match the html source against the tag.
* @src: Pointer to html source, right after the '<'.
* @tag: Tag name, in lower case.
*
* Return value: pointer to next token after tag if matched, NULL
* otherwise.
**/
static const char *
match_tag (const char *src, const char *tag)
{
int i;
char c;
for (i = 0; isalpha (c = src[i]); i++)
if (tolower (c) != tag[i])
return NULL;
if (tag[i] != 0)
return NULL;
while (isspace (src[i])) i++;
return src + i;
}
static const char *
find_end_tag (const char *str, const char *tag, const char **after)
{
int i;
for (i = 0; str[i]; i++)
{
if (str[i] == '<')
{
const char *ptr;
if (str[i + 1] != '/')
return NULL;
/* Allow </> close tag syntax. */
if (str[i + 2] == '>')
ptr = str + i + 2;
else
ptr = match_tag (str + i + 2, tag);
if (ptr == NULL) return NULL;
if (ptr[0] != '>')
return NULL;
*after = ptr + 1;
return str + i;
}
}
return NULL;
}
/**
* virgule_user_is_special: Test whether or not the current user, if any, is
* on the list of special (admin) users. Returns TRUE if the user is special.
*/
int
virgule_user_is_special (VirguleReq *vr, const char *user)
{
const char **u = NULL;
if (user)
{
for (u = vr->priv->special_users; *u; u++)
if (!strcmp (user, *u))
break;
if (*u)
return TRUE;
}
return FALSE;
}
/**
* virgule_add_nofollow: Add a nofollow relationship attribute
* to any anchor tags found in the string.
*/
char *
virgule_add_nofollow (VirguleReq *vr, const char *raw)
{
const char *rel = " rel=\"nofollow\"";
const char *c = NULL;
char *out = NULL;
char *tmp = NULL;
int i = 0;
/* count the anchor tags */
for(c=raw;*c;c++)
{
if(*c == '<' && (*(c+1) == 'a' || *(c+1) == 'A'))
{
i++;
c++;
}
}
if(i == 0)
return (char *)raw;
/* allocate a new buffer or fail silently */
out = apr_palloc (vr->priv->pool, (apr_size_t)((i*15)+strlen(raw)+1));
if(out == NULL)
return (char *)raw;
/* add the nofollow relations */
for(tmp=out,c=raw;*c;c++)
{
if(*c == '<' && (*(c+1) == 'a' || *(c+1) == 'A'))
{
while(*c && *c!='>')
*tmp++ = *c++;
if(*c == '>')
{
memcpy(tmp,rel,15);
tmp+=15;
}
*tmp++ = *c;
}
else
*tmp++ = *c;
}
*tmp=0;
return out;
}
/**
* virgule_strip_a: Remove any anchor tags found in the string
*/
char *
virgule_strip_a (VirguleReq *vr, const char *raw)
{
/* strip anchor tags */
char *clean = apr_pstrdup (vr->r->pool, raw);
char *tmp1 = (char *)raw;
char *tmp2 = clean;
while(*tmp1!=0)
{
if(strncasecmp(tmp1,"<a",2)==0)
{
while(*tmp1!=0&&*tmp1!='>')
tmp1++;
if(*tmp1!=0)
tmp1++;
continue;
}
if(strncasecmp(tmp1,"</a>",4)==0)
{
tmp1+=4;
continue;
}
*tmp2 = *tmp1;
tmp1++;
tmp2++;
}
*tmp2=0;
return clean;
}
static void
nice_tag(Buffer *b, const AllowedTag *tag, const char *raw, int len)
{
int i, end, tag_len;
tag_len = strlen (tag->tagname);
virgule_buffer_write (b, raw, tag_len + 1);
for (i = 1 + tag_len; i < len - 1; i = end)
{
for (end = i; isspace (raw[end]); end++)
;
i = end;
if (i < len - 1)
{
int in_quote = 0;
for (end = i; end < len - 1; end++) {
if (!in_quote && isspace (raw[end]))
break;
if (raw[end] == '"')
in_quote = !in_quote;
}
/* ignore attributes with unterminated quotes */
if (!in_quote)
{
char **attrib = NULL;
if (tag->allowed_attributes)
{
for (attrib = tag->allowed_attributes; *attrib; attrib++)
{
if (match_tag(raw + i, *attrib))
break;
}
}
/* ignore invalid attributes */
if (*attrib)
{
virgule_buffer_puts(b, " ");
virgule_buffer_write(b, raw + i, end - i);
}
}
}
}
virgule_buffer_puts (b, ">");
}
/**
* nice_htext: Convert raw html'ish text into nice HTML.
* @raw: Raw text.
* @p_error: Where error message is to be stored.
*
* Return value: HTML formatted text.
**/
char *
virgule_nice_htext (VirguleReq *vr, const char *utf8, char **p_error)
{
apr_pool_t *p = vr->r->pool;
Buffer *b = virgule_buffer_new (p);
apr_array_header_t *tag_stack;
int i, end;
char c;
int nl_state = 0;
*p_error = NULL;
const char *raw = virgule_nice_utf8 (vr->r->pool, utf8);
#if 0
/* revert to old nicetext behavior */
return virgule_nice_text (p, raw);
#endif
tag_stack = apr_array_make (p, 16, sizeof (char *));
for (i = 0; raw[i]; i = end)
{
for (end = i;
(c = raw[end]) &&
c != '\n' && c != '&' && c != '<' && c != '>' && !(c & 0x80);
end++);
if (end > i + 1 || raw[i] != '\r')
{
if (nl_state == 2)
virgule_buffer_puts (b, "<p> ");
nl_state = 3;
}
if (end > i)
virgule_buffer_write (b, raw + i, end - i);
i = end;
if (c == '&')
{
end++;
if (raw[end] == '#')
{
/* numeric entity */
if (isdigit (raw[end + 1]))
{
/* decimal character reference */
end += 2;
while (isdigit (raw[end])) end++;
}
#if 0
/* apparently, the ģ syntax is not valid HTML,
even though it is XML. */
else if (raw[end + 1] == 'x')
{
/* hexadecimal character reference */
if (isxdigit (raw[end + 2]))
{
end += 3;
while (isxdigit (raw[end])) end++;
}
}
#endif
}
else
{
/* entity reference */
while (isalpha (raw[end])) end++;
}
if (end > i + 1 && raw[end] == ';')
{
end++;
virgule_buffer_write (b, raw + i, end - i);
continue;
}
end = i + 1;
virgule_buffer_puts (b, "&");
}
else if (c == '<')
{
const AllowedTag **tag;
const char *tail = NULL; /* to avoid uninitialized warning */
end++;
if (raw[end] == '/')
{
char *tos;
int tos_idx;
/* just skip closing tag for empty elements */
for (tag = vr->priv->allowed_tags; *tag; tag++)
if ((*tag)->empty &&
(tail = match_tag (raw + end + 1, (*tag)->tagname)) != NULL)
break;
if (*tag)
{
end = tail - raw + 1;
continue;
}
/* pop tag stack if closing tag matches */
tos_idx = tag_stack->nelts - 1;
if (tos_idx >= 0)
{
tos = ((char **)(tag_stack->elts))[tos_idx];
/* Allow </> syntax to close tags. */
if (raw[end + 1] == '>')
tail = raw + end + 1;
else
tail = match_tag (raw + end + 1, tos);
if (tail != NULL && *tail == '>')
{
virgule_buffer_printf (b, "</%s>", tos);
tag_stack->nelts--;
end = tail - raw + 1;
continue;
}
}
while(*tag)
tag++;
}
else
{
for (tag = vr->priv->allowed_tags; *tag; tag++)
{
tail = match_tag (raw + end, (*tag)->tagname);
if (tail != NULL)
break;
}
}
if (*tag)
{
/* todo: handle quotes */
while ((c = raw[end]) && c != '>')
end++;
if (c == '>')
{
end++;
if ((*tag)->handler != NULL)
{
char *body;
int body_size;
const char *body_end;
const char *after;
body_end = find_end_tag (raw + end,
(*tag)->tagname,
&after);
if (body_end != NULL)
{
body_size = body_end - (raw + end);
body = apr_palloc (p, body_size + 1);
memcpy (body, raw + end, body_size);
body[body_size] = 0;
#if 1
virgule_buffer_puts (b, (*tag)->handler (vr, body));
#else
virgule_buffer_printf (b, "[body = %s, %d]", body, body_size);
#endif
end = after - raw;
continue;
}
else
{
#if 0
virgule_buffer_printf (b, "[body_end = NULL]");
#endif
}
}
else
{
nice_tag(b, *tag, raw + i, end - i);
if (!(*tag)->empty)
{
char **p_stack;
p_stack = (char **)apr_array_push (tag_stack);
*p_stack = (*tag)->tagname;
}
continue;
}
}
end = i + 1;
}
/* tag not matched, escape the html */
virgule_buffer_puts (b, "<");