-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocesshtml.c
1249 lines (1049 loc) · 38.8 KB
/
processhtml.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 "YahooEml.h"
#define MAXTAGLEN 31 // max length of a tag to store
#define MAXATTRS 20 // max number of attribute pairs
static char G_CurTagStr[MAXTAGLEN + 1]; // global area to store current tag
static char G_CodeBreaker[MAXTAGLEN + 2]; // tag to end preformatted text
//static char *G_CurAttrTbl[MAXATTRS][2]; // Table of pointers to attributes and values
static char G_EmailBuf[1024]; // Temp storage for href attribute as defined by A tag.
static char G_StyleBuf[1024]; // temp storage for style attribute
//static char G_FontFace[MAXTAGLEN + 1]; // Font face as defined by FONT tag.
static char G_FontColor[MAXTAGLEN + 1]; // Font color as defined by FONT tag.
static char G_FontSize[MAXTAGLEN + 1]; // Font size as defined by FONT tag.
static char G_ImgSrc[256]; // Url to image
static int G_ImgWidth; // Image width as defined by IMG tag.
static int G_ImgHeight; // Image height as defined by IMG tag.
static char G_InPre; // Boolean if in preformatted text
// BBCode enums
// These are the BBcodes that we support and match the string table.
// NOTE: <set> tags and odd numbers, </unset> tags are even numbers,
enum { BB_BOLD=1, BB_END_BOLD,
BB_ITALIC, BB_END_ITALIC,
BB_UNDER, BB_END_UNDER,
BB_STRIKE, BB_END_STRIKE,
BB_SUP, BB_END_SUP,
BB_SUB, BB_END_SUB,
BB_LEFT, BB_END_LEFT,
BB_CENTER, BB_END_CENTER,
BB_RIGHT, BB_END_RIGHT,
BB_QUOTE, BB_END_QUOTE,
BB_LIST, BB_END_LIST,
BB_LIST2, BB_END_LIST2,
BB_LI, BB_END_LI,
BB_MOVE, BB_END_MOVE,
BB_TABLE, BB_END_TABLE,
BB_TR, BB_END_TR,
BB_TD, BB_END_TD,
BB_TH, BB_END_TH,
BB_NEWLINE, BB_END_NEWLINE,
BB_NEWLINE2, BB_END_NEWLINE2,
BB_HR, BB_MAXSIMPLE,
BB_CODE, BB_END_CODE,
BB_SPECIAL, BB_END_SPECIAL,
BB_SPECIAL_A, BB_END_SPECIAL_A,
BB_SPECIAL_IMG, BB_END_SPECIAL_IMG,
BB_SPECIAL_FONT, BB_END_SPECIAL_FONT,
BB_URL, BB_END_URL,
BB_EMAIL, BB_END_EMAIL,
BB_IMG, BB_END_IMG,
BB_IMG2, BB_END_IMG2,
BB_SIZE, BB_END_SIZE,
BB_COLOR, BB_END_COLOR,
BB_MAXCODES };
// BBCode table
static char *BBCodes[] =
{
"",
"[b]", "[/b]",
"[i]", "[/i]",
"[u]", "[/u]",
"[s]", "[/s]",
"[sup]", "[/sup]",
"[sub]", "[/sub]",
"[left]", "[/left]",
"[center]", "[/center]",
"[right]", "[/right]",
"[quote]", "[/quote]",
"[list]", "[/list]",
"[list=1]", "[/list]",
"[li]", "[/li]",
"[move]", "[/move]",
"[table]", "[/table]",
"[tr]", "[/tr]",
"[td]", "[/td]",
"[th]", "[/th]",
"\r\n", "",
"\r\n\r\n", "",
"\r\n---------------\r\n", "endsimple",
"[code]", "[/code]",
"", "", // special and end special
"", "", // special A and end special A
"", "", // Special img and end special img
"", "", // Special font and end special font
"[url=", "[/url]",
"[email=", "[/email]",
"[img]", "[/img]",
"[img=", "[/img]",
"[size=", "[/size]",
"[color=", "[/color]",
NULL
};
// BBCode table
static char *HtmlPrint[] =
{
"",
"<b>", "</b>",
"<i>", "</i>",
"<u>", "</u>",
"<s>", "</s>",
"<sup>", "</sup>",
"<sub>", "</sub>",
"<left>", "</left>",
"<center>", "</center>",
"<right>", "</right>",
"<blockquote>", "</blockquote>",
"<list>", "</list>",
"<list=1>", "</list>",
"<li>", "</li>",
"<marquee>", "</marquee>",
"<table>", "</table>",
"<tr>", "</tr>",
"<td>", "</td>",
"<th>", "</th>",
"<br>", "</br>",
"<p>", "</p>",
"<hr>", "endsimple",
"<pre>", "</pre>",
"", "", // special and end special
"", "", // special A and end special A
"", "", // Special img and end special img
"", "", // Special font and end special font
"<a href=\"%s\">", "</a>", // link
"<a href=\"mailto:=", "</a>", // email
"<img src=\"%s\">", "",
"<img width=%d height=%d src=\"%s\">", "",
"<span style=font size", "</span>",
"<font color=", "</font>",
NULL
};
// end tags that break (stop) attributes - can be any tag
enum { ST_BOLD=0, ST_ITALIC, ST_UNDER, ST_STRIKE,
ST_SIZE, ST_COLOR, /* ST_FACE, */
ST_LEFT, ST_CENTER, ST_RIGHT, ST_MAXSTYLE };
// This array records which tag was used to start the font style.
// since ANY tag, not just the ones supported here, can have a Style
// attribute, we must store the actual tag and not its reference.
static char G_BrkAttrs[ST_MAXSTYLE][MAXTAGLEN + 2]; // ends the current font attribute
// Codes output when a BrkAttr[] is matched.
static int G_BrkCodes[ST_MAXSTYLE] =
{
BB_END_BOLD, BB_END_ITALIC, BB_END_UNDER, BB_END_STRIKE,
BB_END_SIZE, BB_END_COLOR, /* BB_END_FONT, */
BB_END_LEFT, BB_END_CENTER, BB_END_RIGHT
};
typedef struct HTMLREC
{
char *html;
int bbidx;
} HTMLREC;
// This table is the simple one to one html codes.
static HTMLREC HtmlCodes[] =
{
{ "", 0 }, // unused code
// These are simple codes with no parameters
{ "HR", BB_HR },
{ "H1", BB_BOLD },
{ "/H1", BB_END_BOLD },
{ "H2", BB_BOLD },
{ "/H2", BB_END_BOLD },
{ "H3", BB_BOLD },
{ "/H3", BB_END_BOLD },
{ "H4", BB_BOLD },
{ "/H4", BB_END_BOLD },
{ "H5", BB_BOLD },
{ "/H5", BB_END_BOLD },
{ "H6", BB_BOLD },
{ "/H6", BB_END_BOLD },
{ "BLOCKQUOTE", BB_QUOTE },
{ "/BLOCKQUOTE",BB_END_QUOTE },
{ "UL", BB_LIST },
{ "/UL", BB_END_LIST },
{ "OL", BB_LIST2 },
{ "/OL", BB_END_LIST },
{ "LI", BB_LI },
{ "/LI", BB_END_LI },
{ "B", BB_BOLD },
{ "/B", BB_END_BOLD },
{ "BIG", BB_BOLD },
{ "/B", BB_END_BOLD },
{ "U", BB_UNDER },
{ "/U", BB_END_UNDER },
{ "INS", BB_UNDER },
{ "/INS", BB_END_UNDER },
{ "I", BB_ITALIC },
{ "/I", BB_END_ITALIC },
{ "EM", BB_ITALIC },
{ "/EM", BB_END_ITALIC },
{ "STRONG", BB_BOLD },
{ "/STRONG", BB_END_BOLD },
{ "LEFT", BB_LEFT },
{ "/LEFT", BB_END_LEFT },
{ "CENTER", BB_CENTER },
{ "/CENTER", BB_END_CENTER },
{ "RIGHT", BB_RIGHT },
{ "/RIGHT", BB_END_RIGHT },
{ "STRIKE", BB_STRIKE },
{ "/STRIKE", BB_END_STRIKE },
{ "S", BB_STRIKE },
{ "/S", BB_END_STRIKE },
{ "DEL", BB_STRIKE },
{ "/DEL", BB_END_STRIKE },
{ "MARQUEE", BB_MOVE },
{ "/MARQUEE", BB_END_MOVE },
{ "TABLE", BB_TABLE },
{ "/TABLE", BB_END_TABLE },
{ "TR", BB_TR },
{ "/TR", BB_END_TR },
{ "TD", BB_TD },
{ "/TD", BB_END_TD },
{ "TH", BB_TH },
{ "/TH", BB_END_TH },
{ "SUP", BB_SUP },
{ "/SUP", BB_END_SUP },
{ "SUB", BB_SUB },
{ "/SUB", BB_END_SUB },
// These tags have no end tags
{ "BR", BB_NEWLINE },
{ "P", BB_NEWLINE2 },
// Preformatted text areas - content between markers is not modified.
{ "TEXTAREA", BB_CODE }, // text block
{ "/TEXTAREA", BB_END_CODE }, // end text block
{ "PRE", BB_CODE }, // preformatted
{ "/PRE", BB_END_CODE }, // end preformatted
{ "TT", BB_CODE }, // teletype
{ "/TT", BB_END_CODE }, // end teletype
{ "CODE", BB_CODE }, // code
{ "/CODE", BB_END_CODE }, // end code
// These tags require special processing.
{ "A", BB_SPECIAL_A },
{ "/A", BB_END_SPECIAL_A },
{ "IMG", BB_SPECIAL_IMG },
{ "/IMG", BB_END_IMG },
{ "FONT", BB_SPECIAL_FONT },
{ "/FONT", BB_END_SPECIAL_FONT },
{ NULL, NULL }
};
// Strips yahoo CSS junk from email
// Look for "#yiv" followed by at least one digit. If found, then look
// for a "{" then a "}". If that sequence is found, delete it.
void RemoveYinv(char *Buf)
{
char ch, *OutBuf;
char *lptr;
int level;
if (!Buf) return;
OutBuf = Buf;
while ((ch = *Buf++) != 0)
{
if (ch == '#') // possible match
{
if (memcmp(Buf, "yiv", 3) == 0 && isdigit(Buf[3]))
{
Buf += 4;
while (!isspace(*Buf)) Buf++; // skip over "#yiv234234534"
// scan for opening bracket
lptr = strchr(Buf + 4, '{');
if (lptr)
{
// scan to closing bracket
Buf = lptr + 1;
level = 1;
while ((ch = *Buf++) != 0)
{
if (ch == '{')
level++;
else if (ch == '}')
level--;
if (level == 0) break;
}
}
// remove trailing white space
while (isspace(*Buf)) Buf++;
}
else *OutBuf++ = ch;
}
else // regular chars
{
*OutBuf++ = ch;
}
}
*OutBuf = 0; // null terminate
}
/*
TAG CONVERSIONS EQUAL OR SHORTER LENGTH
HTML Tag Converts to:
<BR> or <BR /> Carriage Return
<H1> to <H6> </H1> to </H6> [b][/b]
<BLOCKQUOTE></BLOCKQUOTE> [quote][/quote] [quote="author"]quoted text[/quote]
<LI> [*]
<IMG SRC="pic.jpg"> [img]pic.jpg[/img]
<A HREF="http://webpage.com/">Web Page</A> [url=http://webpage.com]Web Page[/url]
<A HREF="mailto:[email protected]">Email me</A> [[email protected]]Email me[/email]
<BIG></BIG> [b][/b]
<B></B> [b][/b]
<U></U> or <ins></ins> [u][/u]
<I></I> or <EM></EM> [i][/i]
<FONT FACE="arial"></FONT> [font=arial][/font]
<FONT COLOR="red"></FONT> [color=red][/color]
<FONT SIZE="5"></FONT> [size=5][/size]
<STRONG></STRONG> [b][/b]
<TEXTAREA></TEXTAREA> [code][/code] (Note: All code within textarea is not converted.)
<SCRIPT></SCRIPT> (ignored)
<LEFT></LEFT> [left][/left]
<CENTER></CENTER> [center][/center]
<RIGHT></RIGHT> [right][/right]
<STRIKE></STRIKE> [s][/s]
<del></del> [s][/s]
<MARQUEE></MARQUEE> [move][/move]
<HR> [hr]
<TABLE></TABLE> [table][/table]
<TR></TR> [tr][/tr]
<TD></TD> [td][/td]
<TH></TH> [th][/th]
(Space)
<SUP></SUP> [sup][/sup]
<SUB></SUB> [sub][/sub]
TAG CONVERSIONS RESULTING IN GREATER LENGTH
HTML Tag Converts to:
<P> 2 Carriage Returns \r\n\r\n 4 chars
<UL></UL> [list][/list]
<OL></OL> [list=1][/list]
<FONT FACE="arial" COLOR="red" SIZE="5"></FONT> [font=arial][color=red][size=5][/font][/color][/size]
<PRE></PRE> [code][/code]
<TT></TT> [code][/code]
*/
// Check the tag in G_CurTagStr to see if it was
// used to close a style or font attribute
// Writes encoded bytes to OutBuf. Clears G_BrkAttrs[] if used.
// Returns the number of chars written to OutBuf.
int CloseStyle(char *OutBuf)
{
int i, cnt = 0;
if (G_CurTagStr[0] == '/')
{
for (i = 0; i < ST_MAXSTYLE; i++)
{
if (strcmpi(G_CurTagStr + 1, G_BrkAttrs[i]) == 0)
{
G_BrkAttrs[i][0] = 0;
*OutBuf++ = 0xFF;
*OutBuf++ = (char) G_BrkCodes[i];
cnt += 2;
}
}
}
return(cnt);
}
/*
// styles - can appear in any html tag including <HTML>
<span style="font-weight: bold;">bolded text</span> [b][/b]
<span style="font-style: italic;">italicized text</span> [i][/i]
<span style="text-decoration: underline;">underlined text</span> [u][/u]
<span style="text-decoration: line-through;">strikethrough text</span> [s][/s]
<span style="font-size:30">Large Text</span> [size=30]Large Text[/size]
<span style="color:fuchsia;">Text in fuchsia</span> [color=fuchsia]Text in fuchsia[/color]
<span style="color:#FF00FF;">Text in fuchsia</span> [color=#FF00FF]Text in fuchsia[/color]
<p style="font-family:courier;">This is a paragraph.</p>
*/
// HTML Style codes
// A "style" code can be in ANY HTML tag so ALL tags need to be seached.
// General format: <tag style="param:value *[; param2:value][;] "> some text </tag>
static HTMLREC HtmlStyles[ST_MAXSTYLE] =
{
{ "font-weight:bold", BB_BOLD },
{ "font-style:italic", BB_ITALIC },
{ "text-decoration:underline", BB_UNDER },
{ "text-decoration:line-through", BB_STRIKE },
{ "font-size:", BB_SIZE },
{ "color:", BB_COLOR },
// { "font-family:", BB_FONT },
{ "text-align:left", BB_LEFT },
{ "text-align:center", BB_CENTER },
{ "text-align:right", BB_RIGHT }
};
// Parse style string and output corresponding values.
// style syntax: <tagname style="property:value;">
// General format: <tag style="param:value *[; param2:value][;] "> some text </tag>
// If a supported style was detected, write the code to OutBuf, copy the
// appropriate break tag, and return number of bytes written to OutStr.
int ProcessStyle(char *OutBuf)
{
int i, Count;
char *sptr, *dptr;
char ch;
// make sure there is a style and this is not an end tag
if (G_StyleBuf[0] == 0 || G_CurTagStr[0] == '/') return(0);
// remove spaces
sptr = dptr = G_StyleBuf;
while ((ch = *sptr++) != 0) if (!isspace(ch)) *dptr++ = ch;
*dptr = 0;
// parse styles
Count = 0;
sptr = strtok(G_StyleBuf, ";");
while (sptr)
{
for (i = 0; i < ST_MAXSTYLE; i++)
{
char *param;
int LenParam;
param = HtmlStyles[i].html;
LenParam = strlen(param);
if (memicmp(sptr, param, LenParam) == 0)
{
int bbcode;
// found a match - copy break tag
strcpy(G_BrkAttrs[i], G_CurTagStr);
// output code
bbcode = HtmlStyles[i].bbidx;
*OutBuf++ = 0xFF;
*OutBuf++ = (char) bbcode;
Count += 2;
// special case for size wich has a parameter
if (bbcode == BB_SIZE) // add size or color or font
{
int rt;
rt = sprintf(OutBuf, "%x;", NormalizeFontSize(sptr + LenParam));
OutBuf += rt;
Count += rt;
}
// special case for size and color which have a parameter
if (bbcode == BB_COLOR) // add size or color or font
{
int rt;
rt = sprintf(OutBuf, "%x;", NormalizeFontColor(sptr + LenParam));
OutBuf += rt;
Count += rt;
}
break; // for loop
}
}
sptr = strtok(NULL, ";");
}
G_StyleBuf[0] = 0;
return(Count);
}
// Search for HTML Tag
// Tag must be null terminated string with no leading space.
// Return the BB enum value or 0 if not found.
int SearchTag(char *Tag)
{
int i;
if (!Tag) return(0);
for (i = 0; HtmlCodes[i].html; i++)
{
if (strcmpi(HtmlCodes[i].html, Tag) == 0) // found it
return(HtmlCodes[i].bbidx);
}
return(0);
}
// Process email string to make sure its compatable with BBCODE
void ExtractEmail(char *Ebuf)
{
char *ptr, *pdomain;
// enforce a text '@' text email.
ptr = strchr(Ebuf, '@');
if (!ptr)
{
Ebuf[0] = 0;
return;
}
*ptr = 0; // temp
pdomain = ptr + 1;
strrev(Ebuf); // reverse order
ptr = strpbrk(Ebuf, "?&<>()';:");
if (ptr) *ptr = 0;
strrev(Ebuf); // correct order
ptr = strpbrk(pdomain, "?&;:<>()'");
if (ptr) *ptr = 0;
ptr = strchr(Ebuf, 0);
*ptr++ = '@';
strcpy(ptr, pdomain);
}
// Process an URL so that it is a valid link URL
// Returns length of url in chars or 0 if invalid
// Requires the "http://" or "https://" protocol in the string.
// href="http://www.example.com/default.htm" - allowed
// href="https://www.example.com/default.htm" - allowed
// href="www.example.com" - not allowed
// href="default.htm" - not allowed
// href="#top" - not allowed
// ftp://, file: - not allowed
// href="javascript:alert('Hello');" - not allowed
int ExtractUrl(char *Ubuf)
{
if (memicmp(Ubuf, "http://", 7) == 0 || memicmp(Ubuf, "https://", 8) == 0)
{
// good enough
return(strlen(Ubuf));
}
return(0);
}
// Process the HTML tag and its attributes appropriately
// Returns the number of bytes written to outbuf.
int ProcessHtmlTag(char *OutBuf)
{
int rt;
static int G_Special_A_Type = 0;
rt = SearchTag(G_CurTagStr);
if (rt == 0) return(0); // tag not handled here
else if (rt < BB_MAXSIMPLE) // simple tag
{
// with simple tags, we only have top save the value
*OutBuf = 0xFF;
*(OutBuf + 1) = (unsigned char) rt;
return(2); // wrote 2 bytes
}
else if (rt == BB_CODE) // special area that doesn't get processed
{
// Copy all text verbatim until an END_CODE is encountered.
G_InPre = TRUE; // Inside preformatted text
G_CodeBreaker[0] = '/';
strcpy(G_CodeBreaker + 1, G_CurTagStr); // curtagstr is known to be smaller
*OutBuf = 0xFF;
*(OutBuf + 1) = BB_CODE;
return(2); // wrote 2 bytes
}
else if (rt == BB_END_CODE)
{
G_InPre = FALSE;
*OutBuf = 0xFF;
*(OutBuf + 1) = BB_END_CODE;
return(2); // wrote 2 bytes
}
else if (rt == BB_SPECIAL_A) // special processing for anchor tags
{
// could be a link or could be a mailto
// Examine the HREF attricute. If the first chars of the HREF value
// are "mailto:" then its processed as an email.
// Otherwise, its processed as a link.
G_Special_A_Type = 0;
if (memicmp(G_EmailBuf, "mailto:", 7) == 0) // Mailto
{
// The email address is extracted and the following
// code is generated: BB_EMAIL {bare email address} ';'
// which gets translated to [email={addr}]
G_Special_A_Type = BB_END_EMAIL;
ExtractEmail(G_EmailBuf);
return(sprintf(OutBuf, "%c%c%s]", 0xFF, BB_EMAIL, G_EmailBuf));
}
else // Link
{
// Generate: BB_URL {url} ';' which gets translated to: [url={url}]
G_Special_A_Type = BB_END_URL;
ExtractUrl(G_EmailBuf);
return(sprintf(OutBuf, "%c%c%s]", 0xFF, BB_URL, G_EmailBuf));
}
}
else if (rt == BB_END_SPECIAL_A) // special processing for anchor tags
{
// Close tag for link or mailto
if (G_Special_A_Type)
return(sprintf(OutBuf, "%c%c", 0xFF, G_Special_A_Type));
}
else if (rt == BB_SPECIAL_IMG) // special processing for image tags
{
// check paramters for src, Width and Height
// HTML does not normally close an IMG tag, but XHTML and BBcode do
// This generates [img]{src}[/img] or [img={width)x{height}] {src} [/img]
if (!G_ImgSrc[0]) return(0); // No image provided
if (G_ImgWidth && G_ImgHeight) // must have both to count
{
return(sprintf(OutBuf, "%c%c%dx%d]%s%c%c",
0xFF, BB_IMG2, // indicates paramters
G_ImgWidth, G_ImgHeight, G_ImgSrc, 0xFF, BB_END_IMG));
}
else // only the image itself
{
return(sprintf(OutBuf, "%c%c%s%c%c",
0xFF, BB_IMG, // indicates no paramters
G_ImgSrc, 0xFF, BB_END_IMG));
}
}
else if (rt == BB_SPECIAL_FONT) // special processing for FONT tags
{
int len;
// check parameters for color, face and size
// If these parameters are included, Generates code
// [font={face}], [color={color}] and [size={size}]
// The closing "</FONT> is handled by the CloseStyle() function.
len = 0;
// if (G_FontFace[0])
// {
// rt = sprintf(OutBuf, "%c%c%s]", 0xFF, BB_FONT, G_FontFace);
// OutBuf += rt;
// len += rt;
// strcpy(G_BrkAttrs[ST_FACE], "font");
// }
if (G_FontColor[0])
{
rt = sprintf(OutBuf, "%c%c%x;", 0xFF, BB_COLOR, NormalizeFontColor(G_FontColor));
OutBuf += rt;
len += rt;
strcpy(G_BrkAttrs[ST_COLOR], "font");
}
if (G_FontSize[0])
{
rt = sprintf(OutBuf, "%c%c%x;", 0xFF, BB_SIZE, NormalizeFontSize(G_FontSize));
// OutBuf += rt;
len += rt;
strcpy(G_BrkAttrs[ST_SIZE], "font");
}
return(len);
}
return(0);
}
// return a pointer to the next char in string that is not a whitespace char
char *SkipSpaces(char *str)
{
int len;
len = strspn(str, " \r\n\t\f"); // skip whitespace
return(str + len);
}
// Parse hTML tag
// Given a pointer to a tag string starting with '<'.
// Tag string is not null terminated.
// Returns number of chars in buffer that caller should advance. Usually
// this will point to the that after the '>'.
// Copies results to CurTagStr, G_EmailBuf, G_FontFace, G_FontColor,
// G_FontSize, G_ImgWidth and G_ImageHeight.
//
// HTML Tag Syntax * = zero or more, + = 1 or more, [ ] = optional:
// htmltag := '<' tagname ( <attrdef>* | (whitespc)* ) ['/'] '>'
// attrdef := (whitespc)+ AttributeName (whitespc)* [ '=' (whitespc)* <ValueStr> ] (whitespc)*
// ValueStr := ( '"' value '"') | value
int ParseHtmlTag(char *TagStr)
{
char *ptr, *ptr2;
int len;
char ch;
char inquote;
int retval;
char tmpattr[MAXTAGLEN + 2];
// Initialize variables for new tag
memset(G_CurTagStr, 0, MAXTAGLEN + 1);
memset(G_EmailBuf, 0, sizeof(G_EmailBuf));
memset(G_StyleBuf, 0, sizeof(G_StyleBuf));
// memset(G_FontFace, 0, sizeof(G_FontFace));
memset(G_FontColor, 0, sizeof(G_FontColor));
memset(G_FontSize, 0, sizeof(G_FontSize));
memset(G_ImgSrc, 0, sizeof(G_ImgSrc));
G_ImgWidth = 0;
G_ImgHeight = 0;
// Do we really have a tag?
if (!TagStr || *TagStr != '<') return(0); // Not a tag
// Tag or '/' must be first char after '<' or its a syntax error
TagStr++; // point to first char in tag
ptr = TagStr;
if (*ptr == '/') ptr++;
if (!isalpha(*ptr)) return(0);
// Make sure there are no more '<' chars before a '>' char
inquote = 0;
retval = ptr - TagStr + 1; // count chars in tag while we're at it.
while ((ch = *ptr++) != 0)
{
retval++;
if (ch == '"' || ch == '\'')
{
if (inquote == ch) // end quote
{
inquote = 0;
continue;
}
else if (inquote == 0) // start quote
{
inquote = ch;
continue;
}
}
if (inquote) continue; // ignore stuff in quotes
if (ch == '<') return(0); // this should not be here
else if (ch == '>') break; // found end of tag
}
if (ch == 0) return(0); // If we hit the end, then it wasn't a real one.
// When here, we are reasonably certain that this is a valid tag
// and not something like "A < B"
// Get tag
len = strcspn(TagStr, " \r\n\t\f>"); // get len of tag
if (len == 0) return(0); // No tag found
memcpy(G_CurTagStr, TagStr, min(len, MAXTAGLEN)); // copy tag
ptr = SkipSpaces(TagStr + len); // skip trailing white space
// if (*ptr == '/' || *ptr == '>') return(retval); // nothing but a tag
// ptr should now point to first attribute
// get attributes
while (*ptr)
{
// get length of attribute
len = strcspn(ptr, "= \r\n\t\f/>");
if (len == 0) return(retval); // no more attributes found
// copy to tmpstr
memset(tmpattr, 0, sizeof(tmpattr));
memcpy(tmpattr, ptr, min(len, MAXTAGLEN));
ptr += len; // point to char that stopped the strcspn()
if (*ptr != '=') // we haven't found the '=' yet
{
ptr = SkipSpaces(ptr); // skip any preceeding spaces
if (*ptr != '=') continue; // no '=' means no value
}
// If here, ptr is pointing to the equal
ptr = SkipSpaces(ptr + 1); // skip any trailing spaces
// When here, ptr is pointing to the first char in the value
// This could be a dbl-quote, single-quote or a non-space
ch = *ptr;
if (ch == '\'' || ch == '"')
{
ptr++; // point to the first char in quote
ptr2 = strchr(ptr, ch); // get pointer to terminal quote
if (!ptr2 || ptr == ptr2)
break; // missing terminal quote or empty string = no value
len = ptr2 - ptr; // length wiothout quotes
ptr2++; // point to char after closing quote
}
else // unquoted value
{
len = strcspn(ptr, " \r\n\t\f/'`\"=<>");
if (len == 0) return(retval); // syntax error - value can't be null after '='
ptr2 = ptr + len; // point to char that stopped the scan
}
// when here, ptr is start of value, Len is the length, and
// ptr2 is the next char after the value
if (strcmpi(tmpattr, "style") == 0) // is it a style attribute
{
memcpy(G_StyleBuf, ptr, min(sizeof(G_StyleBuf) - 1, len));
}
else if (strcmpi(G_CurTagStr, "font") == 0) // FONT tag
{
if (strcmpi(tmpattr, "color") == 0) // font color
memcpy(G_FontColor, ptr, min(sizeof(G_FontColor) - 1, len));
else if (strcmpi(tmpattr, "size") == 0) // font size
memcpy(G_FontSize, ptr, min(sizeof(G_FontSize) - 1, len));
// else if (strcmpi(tmpattr, "face") == 0) // font face
// memcpy(G_FontFace, ptr, min(sizeof(G_FontFace) - 1, len));
}
else if (strcmpi(G_CurTagStr, "img") == 0) // IMG tag
{
if (strcmpi(tmpattr, "width") == 0) // img width
G_ImgWidth = atoi(ptr);
else if (strcmpi(tmpattr, "height") == 0) // img height
G_ImgHeight = atoi(ptr);
else if (strcmpi(tmpattr, "src") == 0) // img src
memcpy(G_ImgSrc, ptr, min(sizeof(G_ImgSrc) - 1, len));
}
else if (strcmpi(G_CurTagStr, "a") == 0) // Anchor tag
{
if (strcmpi(tmpattr, "href") == 0) // either Mailto or link
memcpy(G_EmailBuf, ptr, min(sizeof(G_EmailBuf) - 1, len));
}
ptr = SkipSpaces(ptr2); // point to start of next attribute
}
return(retval);
}
// Checks to see if tag is to be ignored.
// Modifies Buf if found.
// Returns 0 if start not found, 1 if start and stop found, -1 if start found but not stop.
int IgnoreStuff(char **Buf, char *Start, char *Stop)
{
// Look for start string
if (memicmp(*Buf, Start, strlen(Start)) == 0) // special comment
{
char *ptr;
// ignore everything until we get to stop string
ptr = stristr(*Buf, Stop);
if (ptr)
{
ptr = strchr(ptr, '>'); // move to closing '>'
if (ptr)
{
// Found closing '>', return pointer to next char
*Buf = ptr + 1;
return(1);
}
}
// EOF - move BUF to EOF
*Buf = strchr(*Buf, 0);
return(-1); // EOF error
}
return(0); // no match found
}
// 1. Convert HTML file to BBCode in place. Known tags are replaced with
// 0xFF followed by a code byte. If 0xFF is in the source, then it will
// be followed by another 0xFF.
// 2. Trim white space to a single space. " " is converted to a
// real space, but not trimmed.
// 3. When a '<' is found and another is found before a '>', then
// the text from the first '<' up to the second is treated like regular text.
// 4. If a tag is not syntactly correct, it is treated like regular text.
// When the file is printed, the actual BBCodes are substituted.
int Html2BBCode(char *Buf)
{
char ch, *dptr;
int len;
memset(G_BrkAttrs, 0, sizeof(G_BrkAttrs)); // clear attribute break array
dptr = Buf;
while ((ch = *Buf) != 0)
{
if (ch == (char) 0xFF) // special escape char
{
// check to see if there is room for the escape code
if (dptr - Buf >= 2) // plenty of room
{
*dptr++ = 0xFF;
*dptr++ = 0xFF;
}
else *dptr++ = ' '; // wasn't enough room - discard char
Buf++;
continue;
}
if (ch == '<')
{
// ignore comments
if (IgnoreStuff(&Buf, "<!--", "-->")) continue;
// ignore scripts
if (IgnoreStuff(&Buf, "<script", "</script")) continue;
// ignore head section
if (IgnoreStuff(&Buf, "<head", "</head")) continue;
// ignore head section
if (IgnoreStuff(&Buf, "<!doctype", ">")) continue;
len = ParseHtmlTag(Buf);
if (len)
{
// Found a valid tag if here
/* if (G_InPre) // we are in preformatted text
{
// see if this was the matching terminal tag
if (strcmpi(G_CodeBreaker, G_CurTagStr) == 0)
{
// This is the only tag we process
// Cancel preformatted text
G_InPre = FALSE;
*dptr++ = (char) 0xFF;
*dptr++ = BB_END_CODE;
Buf += len; // point to next char
}
else // This was not an END tag - just copy the '<'
{
*dptr++ = '<'; // not an end tag
Buf++;
}
}
else // not in preformatted text - process tag normally
*/ {
dptr += ProcessHtmlTag(dptr); // process a handled tag
dptr += ProcessStyle(dptr); // process style string