View Source

[modwtcl]
apreq.h
dist/
libapreq1/
mod_wtcl.c
mod_wtcl.h
setupExclude.txt
util/
win.def
win.mak
wt1.1/
WtAppTable.c
WtAppTable.h
WtBasicCmds.c
WtBasicCmds.h
WtClientRequest.c
WtClientRequest.h
WtCollection.c
WtCollection.h
WtCollectionCmds.c
WtCollectionCmds.h
WtContext.c
WtContext.h
WtContextEvents.c
WtContextEvents.h
WtCookie.c
WtCookie.h
WtDbSession.c
WtDbSession.h
WtExecute.c
WtExecute.h
WtHtmlEntities.c
WtHtmlEntities.h
WtInitCmds.c
WtInitCmds.h
WtMtTable.c
WtMtTable.h
WtMultiTable.c
WtMultiTable.h
WtOS.h
WtProcSession.c
WtProcSession.h
WtResponse.c
WtResponse.h
WtServerCmds.c
WtServerCmds.h
WtSession.c
WtSession.h
WtSettings.c
WtSettings.h
WtTable.c
WtTable.h
WtTableCmds.c
WtTableCmds.h
WtTableUtil.c
WtTableUtil.h
WtUpload.c
WtUpload.h
WtUtil.c
WtUtil.h
WtWebErrors.c
WtWebErrors.h
WtWindows.h
File: / archive / modwtcl / WtHtmlEntities.c

Lines Size Modified Created Owner MIME Types
678 9,162 2010/05/09 04:04:34 2011/06/13 15:35:15 BUILTIN\Administrators text/x-csrc

0001
/*
0002
 * Copyright 2001 Alexander Boverman and the original authors.
0003
 * 
0004
 * Licensed under the Apache License, Version 2.0 (the "License");
0005
 * you may not use this file except in compliance with the License.
0006
 * You may obtain a copy of the License at
0007
 * 
0008
 *      http://www.apache.org/licenses/LICENSE-2.0
0009
 * 
0010
 * Unless required by applicable law or agreed to in writing, software
0011
 * distributed under the License is distributed on an "AS IS" BASIS,
0012
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013
 * See the License for the specific language governing permissions and
0014
 * limitations under the License.
0015
 */
0016
0017
#include <tcl.h>
0018
#include "WtOS.h"
0019
#include "WtHtmlEntities.h"
0020
#include "WtContext.h"
0021
0022
WtHtmlMaps wtHtmlMaps = {0, NULL, 10000, NULL, NULL};
0023
0024
int wtHtmlEntityChars[] = {
0025
  34,
0026
  38,
0027
  39,
0028
  60,
0029
  62,
0030
  160,
0031
  161,
0032
  162,
0033
  163,
0034
  164,
0035
  165,
0036
  166,
0037
  167,
0038
  168,
0039
  169,
0040
  170,
0041
  171,
0042
  172,
0043
  173,
0044
  174,
0045
  175,
0046
  176,
0047
  177,
0048
  178,
0049
  179,
0050
  180,
0051
  181,
0052
  182,
0053
  183,
0054
  184,
0055
  185,
0056
  186,
0057
  187,
0058
  188,
0059
  189,
0060
  190,
0061
  191,
0062
  192,
0063
  193,
0064
  194,
0065
  195,
0066
  196,
0067
  197,
0068
  198,
0069
  199,
0070
  200,
0071
  201,
0072
  202,
0073
  203,
0074
  204,
0075
  205,
0076
  206,
0077
  207,
0078
  208,
0079
  209,
0080
  210,
0081
  211,
0082
  212,
0083
  213,
0084
  214,
0085
  215,
0086
  216,
0087
  217,
0088
  218,
0089
  219,
0090
  220,
0091
  221,
0092
  222,
0093
  223,
0094
  224,
0095
  225,
0096
  226,
0097
  227,
0098
  228,
0099
  229,
0100
  230,
0101
  231,
0102
  232,
0103
  233,
0104
  234,
0105
  235,
0106
  236,
0107
  237,
0108
  238,
0109
  239,
0110
  240,
0111
  241,
0112
  242,
0113
  243,
0114
  244,
0115
  245,
0116
  246,
0117
  247,
0118
  248,
0119
  249,
0120
  250,
0121
  251,
0122
  252,
0123
  253,
0124
  254,
0125
  255,
0126
  338,
0127
  339,
0128
  352,
0129
  353,
0130
  376,
0131
  402,
0132
  710,
0133
  732,
0134
  913,
0135
  914,
0136
  915,
0137
  916,
0138
  917,
0139
  918,
0140
  919,
0141
  920,
0142
  921,
0143
  922,
0144
  923,
0145
  924,
0146
  925,
0147
  926,
0148
  927,
0149
  928,
0150
  929,
0151
  931,
0152
  932,
0153
  933,
0154
  934,
0155
  935,
0156
  936,
0157
  937,
0158
  945,
0159
  946,
0160
  947,
0161
  948,
0162
  949,
0163
  950,
0164
  951,
0165
  952,
0166
  953,
0167
  954,
0168
  955,
0169
  956,
0170
  957,
0171
  958,
0172
  959,
0173
  960,
0174
  961,
0175
  962,
0176
  963,
0177
  964,
0178
  965,
0179
  966,
0180
  967,
0181
  968,
0182
  969,
0183
  977,
0184
  978,
0185
  982,
0186
  8194,
0187
  8195,
0188
  8201,
0189
  8204,
0190
  8205,
0191
  8206,
0192
  8207,
0193
  8211,
0194
  8212,
0195
  8216,
0196
  8217,
0197
  8218,
0198
  8220,
0199
  8221,
0200
  8222,
0201
  8224,
0202
  8225,
0203
  8226,
0204
  8230,
0205
  8240,
0206
  8242,
0207
  8243,
0208
  8249,
0209
  8250,
0210
  8254,
0211
  8260,
0212
  8364,
0213
  8465,
0214
  8472,
0215
  8476,
0216
  8482,
0217
  8501,
0218
  8592,
0219
  8593,
0220
  8594,
0221
  8595,
0222
  8596,
0223
  8629,
0224
  8656,
0225
  8657,
0226
  8658,
0227
  8659,
0228
  8660,
0229
  8704,
0230
  8706,
0231
  8707,
0232
  8709,
0233
  8711,
0234
  8712,
0235
  8713,
0236
  8715,
0237
  8719,
0238
  8721,
0239
  8722,
0240
  8727,
0241
  8730,
0242
  8733,
0243
  8734,
0244
  8736,
0245
  8743,
0246
  8744,
0247
  8745,
0248
  8746,
0249
  8747,
0250
  8756,
0251
  8764,
0252
  8773,
0253
  8776,
0254
  8800,
0255
  8801,
0256
  8804,
0257
  8805,
0258
  8834,
0259
  8835,
0260
  8836,
0261
  8838,
0262
  8839,
0263
  8853,
0264
  8855,
0265
  8869,
0266
  8901,
0267
  8968,
0268
  8969,
0269
  8970,
0270
  8971,
0271
  9001,
0272
  9002,
0273
  9674,
0274
  9824,
0275
  9827,
0276
  9829,
0277
  9830
0278
};
0279
0280
const char *wtHtmlEntityStrings[] = {
0281
  "quot",
0282
  "amp",
0283
  "apos",
0284
  "lt",
0285
  "gt",
0286
  "nbsp",
0287
  "iexcl",
0288
  "cent",
0289
  "pound",
0290
  "curren",
0291
  "yen",
0292
  "brvbar",
0293
  "sect",
0294
  "uml",
0295
  "copy",
0296
  "ordf",
0297
  "laquo",
0298
  "not",
0299
  "shy",
0300
  "reg",
0301
  "macr",
0302
  "deg",
0303
  "plusmn",
0304
  "sup2",
0305
  "sup3",
0306
  "acute",
0307
  "micro",
0308
  "para",
0309
  "middot",
0310
  "cedil",
0311
  "sup1",
0312
  "ordm",
0313
  "raquo",
0314
  "frac14",
0315
  "frac12",
0316
  "frac34",
0317
  "iquest",
0318
  "Agrave",
0319
  "Aacute",
0320
  "Acirc",
0321
  "Atilde",
0322
  "Auml",
0323
  "Aring",
0324
  "AElig",
0325
  "Ccedil",
0326
  "Egrave",
0327
  "Eacute",
0328
  "Ecirc",
0329
  "Euml",
0330
  "Igrave",
0331
  "Iacute",
0332
  "Icirc",
0333
  "Iuml",
0334
  "ETH",
0335
  "Ntilde",
0336
  "Ograve",
0337
  "Oacute",
0338
  "Ocirc",
0339
  "Otilde",
0340
  "Ouml",
0341
  "times",
0342
  "Oslash",
0343
  "Ugrave",
0344
  "Uacute",
0345
  "Ucirc",
0346
  "Uuml",
0347
  "Yacute",
0348
  "THORN",
0349
  "szlig",
0350
  "agrave",
0351
  "aacute",
0352
  "acirc",
0353
  "atilde",
0354
  "auml",
0355
  "aring",
0356
  "aelig",
0357
  "ccedil",
0358
  "egrave",
0359
  "eacute",
0360
  "ecirc",
0361
  "euml",
0362
  "igrave",
0363
  "iacute",
0364
  "icirc",
0365
  "iuml",
0366
  "eth",
0367
  "ntilde",
0368
  "ograve",
0369
  "oacute",
0370
  "ocirc",
0371
  "otilde",
0372
  "ouml",
0373
  "divide",
0374
  "oslash",
0375
  "ugrave",
0376
  "uacute",
0377
  "ucirc",
0378
  "uuml",
0379
  "yacute",
0380
  "thorn",
0381
  "yuml",
0382
  "OElig",
0383
  "oelig",
0384
  "Scaron",
0385
  "scaron",
0386
  "Yuml",
0387
  "fnof",
0388
  "circ",
0389
  "tilde",
0390
  "Alpha",
0391
  "Beta",
0392
  "Gamma",
0393
  "Delta",
0394
  "Epsilon",
0395
  "Zeta",
0396
  "Eta",
0397
  "Theta",
0398
  "Iota",
0399
  "Kappa",
0400
  "Lambda",
0401
  "Mu",
0402
  "Nu",
0403
  "Xi",
0404
  "Omicron",
0405
  "Pi",
0406
  "Rho",
0407
  "Sigma",
0408
  "Tau",
0409
  "Upsilon",
0410
  "Phi",
0411
  "Chi",
0412
  "Psi",
0413
  "Omega",
0414
  "alpha",
0415
  "beta",
0416
  "gamma",
0417
  "delta",
0418
  "epsilon",
0419
  "zeta",
0420
  "eta",
0421
  "theta",
0422
  "iota",
0423
  "kappa",
0424
  "lambda",
0425
  "mu",
0426
  "nu",
0427
  "xi",
0428
  "omicron",
0429
  "pi",
0430
  "rho",
0431
  "sigmaf",
0432
  "sigma",
0433
  "tau",
0434
  "upsilon",
0435
  "phi",
0436
  "chi",
0437
  "psi",
0438
  "omega",
0439
  "thetasym",
0440
  "upsih",
0441
  "piv",
0442
  "ensp",
0443
  "emsp",
0444
  "thinsp",
0445
  "zwnj",
0446
  "zwj",
0447
  "lrm",
0448
  "rlm",
0449
  "ndash",
0450
  "mdash",
0451
  "lsquo",
0452
  "rsquo",
0453
  "sbquo",
0454
  "ldquo",
0455
  "rdquo",
0456
  "bdquo",
0457
  "dagger",
0458
  "Dagger",
0459
  "bull",
0460
  "hellip",
0461
  "permil",
0462
  "prime",
0463
  "Prime",
0464
  "lsaquo",
0465
  "rsaquo",
0466
  "oline",
0467
  "frasl",
0468
  "euro",
0469
  "image",
0470
  "weierp",
0471
  "real",
0472
  "trade",
0473
  "alefsym",
0474
  "larr",
0475
  "uarr",
0476
  "rarr",
0477
  "darr",
0478
  "harr",
0479
  "crarr",
0480
  "lArr",
0481
  "uArr",
0482
  "rArr",
0483
  "dArr",
0484
  "hArr",
0485
  "forall",
0486
  "part",
0487
  "exist",
0488
  "empty",
0489
  "nabla",
0490
  "isin",
0491
  "notin",
0492
  "ni",
0493
  "prod",
0494
  "sum",
0495
  "minus",
0496
  "lowast",
0497
  "radic",
0498
  "prop",
0499
  "infin",
0500
  "ang",
0501
  "and",
0502
  "or",
0503
  "cap",
0504
  "cup",
0505
  "int",
0506
  "there4",
0507
  "sim",
0508
  "cong",
0509
  "asymp",
0510
  "ne",
0511
  "equiv",
0512
  "le",
0513
  "ge",
0514
  "sub",
0515
  "sup",
0516
  "nsub",
0517
  "sube",
0518
  "supe",
0519
  "oplus",
0520
  "otimes",
0521
  "perp",
0522
  "sdot",
0523
  "lceil",
0524
  "rceil",
0525
  "lfloor",
0526
  "rfloor",
0527
  "lang",
0528
  "rang",
0529
  "loz",
0530
  "spades",
0531
  "clubs",
0532
  "hearts",
0533
  "diams"
0534
};
0535
0536
void WtInitHtmlEntities()
0537
{
0538
  if (!wtHtmlMaps.initialized) {
0539
    Tcl_MutexLock(&wtHtmlMaps.htmlLock);
0540
    if (!wtHtmlMaps.initialized) {
0541
      WtDoInitHtmlEntities();
0542
      wtHtmlMaps.initialized = 1;
0543
    }
0544
    Tcl_MutexUnlock(&wtHtmlMaps.htmlLock);
0545
  }
0546
}
0547
0548
void WtDoInitHtmlEntities()
0549
{
0550
  int i, created;
0551
  Tcl_HashEntry *entry;
0552
  char str[256];
0553
0554
  /* Chars to entities */
0555
0556
  wtHtmlMaps.charsToEnts = (char **)ckalloc(sizeof(char *) *
0557
    wtHtmlMaps.maxHtmlChar);
0558
  memset(wtHtmlMaps.charsToEnts, '\0',
0559
    sizeof(char *) * wtHtmlMaps.maxHtmlChar);
0560
0561
  for (i = 0; i < (sizeof(wtHtmlEntityChars) / sizeof(int)); i++) {
0562
    wtHtmlMaps.charsToEnts[wtHtmlEntityChars[i]] =
0563
      str_strdup(wtHtmlEntityStrings[i]);
0564
  }
0565
0566
  for (i = 0; i < 256; i++) {
0567
    if (!wtHtmlMaps.charsToEnts[i]) {
0568
      snprintf(str, sizeof(str), "&#%d;", i);
0569
      wtHtmlMaps.charsToEnts[i] = str_strdup(str);
0570
    }
0571
  }
0572
0573
  /* Entities to chars */
0574
0575
  wtHtmlMaps.entsToChars =
0576
    (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable));
0577
  Tcl_InitHashTable(wtHtmlMaps.entsToChars, TCL_STRING_KEYS);
0578
0579
  for (i = 0; i < (sizeof(wtHtmlEntityStrings) / sizeof(char *)); i++) {
0580
    entry = Tcl_CreateHashEntry(wtHtmlMaps.entsToChars,
0581
      wtHtmlEntityStrings[i], &created);
0582
    Tcl_SetHashValue(entry, wtHtmlEntityChars[i]);
0583
  }
0584
}
0585
0586
void WtDeleteHtmlEntities()
0587
{
0588
  if (wtHtmlMaps.initialized) {
0589
    Tcl_MutexLock(&wtHtmlMaps.htmlLock);
0590
    if (wtHtmlMaps.initialized) {
0591
      WtDoDeleteHtmlEntities();
0592
      wtHtmlMaps.initialized = 0;
0593
    }
0594
    Tcl_MutexUnlock(&wtHtmlMaps.htmlLock);
0595
  }
0596
}
0597
0598
void WtDoDeleteHtmlEntities()
0599
{
0600
  int i;
0601
  for (i = 0; i < wtHtmlMaps.maxHtmlChar; i++) {
0602
    if (wtHtmlMaps.charsToEnts[i]) {
0603
      free(wtHtmlMaps.charsToEnts[i]);
0604
    }
0605
  }
0606
  ckfree((char *)wtHtmlMaps.charsToEnts);
0607
0608
  Tcl_DeleteHashTable(wtHtmlMaps.entsToChars);
0609
  ckfree((char *)wtHtmlMaps.entsToChars);
0610
  wtHtmlMaps.entsToChars = NULL;
0611
}
0612
0613
/* Code bits for HTML encoding taken from PHP source */
0614
0615
Tcl_Obj *WtEncodeHtml(const char *utfStr, int options)
0616
{
0617
  Tcl_UniChar uniCh;
0618
  const char *p, *q, *ent;
0619
  char buf[256];
0620
  Tcl_Obj *dest;
0621
0622
  WtInitHtmlEntities();
0623
0624
  p = utfStr;
0625
  dest = Tcl_NewStringObj(NULL, 0);
0626
0627
  for (;;) {
0628
    Tcl_UtfToUniChar(p, &uniCh);
0629
    if (uniCh == 0) {
0630
      break;
0631
    }
0632
0633
    q = Tcl_UtfNext(p);
0634
0635
    if (uniCh == '<' || uniCh == '>' || uniCh == '&') {
0636
      if (options & WT_ENT_SPECIAL_CHARS || options & WT_ENT_DEFAULT ||
0637
          options & WT_ENT_ALL) {
0638
        Tcl_AppendStringsToObj(dest, "&",
0639
          wtHtmlMaps.charsToEnts[uniCh], ";", NULL);
0640
      } else {
0641
        Tcl_AppendToObj(dest, p, q - p);
0642
      }
0643
    } else if (uniCh == '"') {
0644
      if (options & WT_ENT_DOUBLE_QUOTE || options & WT_ENT_DEFAULT ||
0645
          options & WT_ENT_ALL) {
0646
        Tcl_AppendStringsToObj(dest, "&", wtHtmlMaps.charsToEnts[uniCh],
0647
          ";", NULL);
0648
      } else {
0649
        Tcl_AppendToObj(dest, p, q - p);
0650
      }
0651
    } else if (uniCh == '\'') {
0652
      if (options & WT_ENT_SINGLE_QUOTE || options & WT_ENT_DEFAULT ||
0653
          options & WT_ENT_ALL) {
0654
        Tcl_AppendStringsToObj(dest, "&", wtHtmlMaps.charsToEnts[uniCh],
0655
          ";", NULL);
0656
      } else {
0657
        Tcl_AppendToObj(dest, p, q - p);
0658
      }
0659
    } else if (uniCh < wtHtmlMaps.maxHtmlChar &&
0660
        (ent = wtHtmlMaps.charsToEnts[uniCh])) {
0661
      if (options & WT_ENT_NAMED_ENTITIES || options & WT_ENT_ALL) {
0662
        Tcl_AppendStringsToObj(dest, "&",
0663
          wtHtmlMaps.charsToEnts[uniCh], ";", NULL);
0664
      } else {
0665
        Tcl_AppendToObj(dest, p, q - p);
0666
      }
0667
    } else if (options & WT_ENT_UNKNOWN_CHARS || options & WT_ENT_ALL) {
0668
      snprintf(buf, sizeof(buf), "&#x%X;", uniCh);
0669
      Tcl_AppendToObj(dest, buf, -1);
0670
    } else {
0671
      Tcl_AppendToObj(dest, p, q - p);
0672
    }
0673
0674
    p = q;
0675
  }
0676
0677
  return dest;
0678
}