FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Modules/expat/xmltok.c at 3.9 · python-emscripten/cpython · GitHub
python-emscripten
cpython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Modules
/
expat
/
xmltok.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
1672 lines (1499 loc) · 52.7 KB
Breadcrumbs
cpython
/
Modules
/
expat
/
xmltok.c
Copy path
File metadata and controls
1672 lines (1499 loc) · 52.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
__ __ _
___\ \/ /_ __ __ _| |_
/ _ \\ /| '_ \ / _` | __|
| __// \| |_) | (_| | |_
\___/_/\_\ .__/ \__,_|\__|
|_| XML parser
Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
Copyright (c) 2000-2017 Expat development team
Licensed under the MIT license:
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifdef
_WIN32
# include
"winconfig.h"
#else
# ifdef
HAVE_EXPAT_CONFIG_H
# include
<expat_config.h>
# endif
#endif
/* ndef _WIN32 */
#include
<stddef.h>
#include
<string.h>
/* memcpy */
#if
defined(
_MSC_VER
)
&&
(
_MSC_VER
<=
1700
)
/* for vs2012/11.0/1700 and earlier Visual Studio compilers */
# define
bool
int
# define
false
0
# define
true
1
#else
# include
<stdbool.h>
#endif
#include
"expat_external.h"
#include
"internal.h"
#include
"xmltok.h"
#include
"nametab.h"
#ifdef
XML_DTD
# define
IGNORE_SECTION_TOK_VTABLE
, PREFIX(ignoreSectionTok)
#else
# define
IGNORE_SECTION_TOK_VTABLE
/* as nothing */
#endif
#define
VTABLE1
\
{PREFIX(prologTok), PREFIX(contentTok), \
PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE}, \
{PREFIX(attributeValueTok), PREFIX(entityValueTok)}, \
PREFIX(nameMatchesAscii), PREFIX(nameLength), PREFIX(skipS), \
PREFIX(getAtts), PREFIX(charRefNumber), PREFIX(predefinedEntityName), \
PREFIX(updatePosition), PREFIX(isPublicId)
#define
VTABLE
VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
#define
UCS2_GET_NAMING
(
pages
,
hi
,
lo
) \
(namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1u << ((lo)&0x1F)))
/* A 2 byte UTF-8 representation splits the characters 11 bits between
the bottom 5 and 6 bits of the bytes. We need 8 bits to index into
pages, 3 bits to add to that index and 5 bits to generate the mask.
*/
#define
UTF8_GET_NAMING2
(
pages
,
byte
) \
(namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
+ ((((byte)[0]) & 3) << 1) + ((((byte)[1]) >> 5) & 1)] \
& (1u << (((byte)[1]) & 0x1F)))
/* A 3 byte UTF-8 representation splits the characters 16 bits between
the bottom 4, 6 and 6 bits of the bytes. We need 8 bits to index
into pages, 3 bits to add to that index and 5 bits to generate the
mask.
*/
#define
UTF8_GET_NAMING3
(
pages
,
byte
) \
(namingBitmap \
[((pages)[((((byte)[0]) & 0xF) << 4) + ((((byte)[1]) >> 2) & 0xF)] \
<< 3) \
+ ((((byte)[1]) & 3) << 1) + ((((byte)[2]) >> 5) & 1)] \
& (1u << (((byte)[2]) & 0x1F)))
#define
UTF8_GET_NAMING
(
pages
,
p
,
n
) \
((n) == 2 \
? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
: ((n) == 3 ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) : 0))
/* Detection of invalid UTF-8 sequences is based on Table 3.1B
of Unicode 3.2: http://www.unicode.org/unicode/reports/tr28/
with the additional restriction of not allowing the Unicode
code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
Implementation details:
(A & 0x80) == 0 means A < 0x80
and
(A & 0xC0) == 0xC0 means A > 0xBF
*/
#define
UTF8_INVALID2
(
p
) \
((*p) < 0xC2 || ((p)[1] & 0x80) == 0 || ((p)[1] & 0xC0) == 0xC0)
#define
UTF8_INVALID3
(
p
) \
(((p)[2] & 0x80) == 0 \
|| ((*p) == 0xEF && (p)[1] == 0xBF ? (p)[2] > 0xBD \
: ((p)[2] & 0xC0) == 0xC0) \
|| ((*p) == 0xE0 \
? (p)[1] < 0xA0 || ((p)[1] & 0xC0) == 0xC0 \
: ((p)[1] & 0x80) == 0 \
|| ((*p) == 0xED ? (p)[1] > 0x9F : ((p)[1] & 0xC0) == 0xC0)))
#define
UTF8_INVALID4
(
p
) \
(((p)[3] & 0x80) == 0 || ((p)[3] & 0xC0) == 0xC0 || ((p)[2] & 0x80) == 0 \
|| ((p)[2] & 0xC0) == 0xC0 \
|| ((*p) == 0xF0 \
? (p)[1] < 0x90 || ((p)[1] & 0xC0) == 0xC0 \
: ((p)[1] & 0x80) == 0 \
|| ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0)))
static
int
PTRFASTCALL
isNever
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
UNUSED_P
(
p
);
return
0
;
}
static
int
PTRFASTCALL
utf8_isName2
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
UTF8_GET_NAMING2
(
namePages
, (
const
unsigned
char
*
)
p
);
}
static
int
PTRFASTCALL
utf8_isName3
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
UTF8_GET_NAMING3
(
namePages
, (
const
unsigned
char
*
)
p
);
}
#define
utf8_isName4
isNever
static
int
PTRFASTCALL
utf8_isNmstrt2
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
UTF8_GET_NAMING2
(
nmstrtPages
, (
const
unsigned
char
*
)
p
);
}
static
int
PTRFASTCALL
utf8_isNmstrt3
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
UTF8_GET_NAMING3
(
nmstrtPages
, (
const
unsigned
char
*
)
p
);
}
#define
utf8_isNmstrt4
isNever
static
int
PTRFASTCALL
utf8_isInvalid2
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
UTF8_INVALID2
((
const
unsigned
char
*
)
p
);
}
static
int
PTRFASTCALL
utf8_isInvalid3
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
UTF8_INVALID3
((
const
unsigned
char
*
)
p
);
}
static
int
PTRFASTCALL
utf8_isInvalid4
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
UTF8_INVALID4
((
const
unsigned
char
*
)
p
);
}
struct
normal_encoding
{
ENCODING
enc
;
unsigned
char
type
[
256
];
#ifdef
XML_MIN_SIZE
int
(
PTRFASTCALL
*
byteType
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isNameMin
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isNmstrtMin
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
byteToAscii
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRCALL
*
charMatches
)(
const
ENCODING
*
,
const
char
*
,
int
);
#endif
/* XML_MIN_SIZE */
int
(
PTRFASTCALL
*
isName2
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isName3
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isName4
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isNmstrt2
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isNmstrt3
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isNmstrt4
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isInvalid2
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isInvalid3
)(
const
ENCODING
*
,
const
char
*
);
int
(
PTRFASTCALL
*
isInvalid4
)(
const
ENCODING
*
,
const
char
*
);
};
#define
AS_NORMAL_ENCODING
(
enc
) ((const struct normal_encoding *)(enc))
#ifdef
XML_MIN_SIZE
# define
STANDARD_VTABLE
(
E
) \
E##byteType, E##isNameMin, E##isNmstrtMin, E##byteToAscii, E##charMatches,
#else
# define
STANDARD_VTABLE
(
E
)
/* as nothing */
#endif
#define
NORMAL_VTABLE
(
E
) \
E##isName2, E##isName3, E##isName4, E##isNmstrt2, E##isNmstrt3, \
E##isNmstrt4, E##isInvalid2, E##isInvalid3, E##isInvalid4
#define
NULL_VTABLE
\
/* isName2 */
NULL,
/* isName3 */
NULL
,
/* isName4 */
NULL, \
/* isNmstrt2 */
NULL,
/* isNmstrt3 */
NULL,
/* isNmstrt4 */
NULL, \
/* isInvalid2 */
NULL,
/* isInvalid3 */
NULL,
/* isInvalid4 */
NULL
static
int
FASTCALL
checkCharRefNumber
(
int
);
#include
"xmltok_impl.h"
#include
"ascii.h"
#ifdef
XML_MIN_SIZE
# define
sb_isNameMin
isNever
# define
sb_isNmstrtMin
isNever
#endif
#ifdef
XML_MIN_SIZE
# define
MINBPC
(
enc
) ((enc)->minBytesPerChar)
#else
/* minimum bytes per character */
# define
MINBPC
(
enc
) 1
#endif
#define
SB_BYTE_TYPE
(
enc
,
p
) \
(((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
#ifdef
XML_MIN_SIZE
static
int
PTRFASTCALL
sb_byteType
(
const
ENCODING
*
enc
,
const
char
*
p
) {
return
SB_BYTE_TYPE
(
enc
,
p
);
}
# define
BYTE_TYPE
(
enc
,
p
) (AS_NORMAL_ENCODING(enc)->byteType(enc, p))
#else
# define
BYTE_TYPE
(
enc
,
p
) SB_BYTE_TYPE(enc, p)
#endif
#ifdef
XML_MIN_SIZE
# define
BYTE_TO_ASCII
(
enc
,
p
) (AS_NORMAL_ENCODING(enc)->byteToAscii(enc, p))
static
int
PTRFASTCALL
sb_byteToAscii
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
*
p
;
}
#else
# define
BYTE_TO_ASCII
(
enc
,
p
) (*(p))
#endif
#define
IS_NAME_CHAR
(
enc
,
p
,
n
) (AS_NORMAL_ENCODING(enc)->isName##n(enc, p))
#define
IS_NMSTRT_CHAR
(
enc
,
p
,
n
) (AS_NORMAL_ENCODING(enc)->isNmstrt##n(enc, p))
#define
IS_INVALID_CHAR
(
enc
,
p
,
n
) \
(AS_NORMAL_ENCODING(enc)->isInvalid##n(enc, p))
#ifdef
XML_MIN_SIZE
# define
IS_NAME_CHAR_MINBPC
(
enc
,
p
) \
(AS_NORMAL_ENCODING(enc)->isNameMin(enc, p))
# define
IS_NMSTRT_CHAR_MINBPC
(
enc
,
p
) \
(AS_NORMAL_ENCODING(enc)->isNmstrtMin(enc, p))
#else
# define
IS_NAME_CHAR_MINBPC
(
enc
,
p
) (0)
# define
IS_NMSTRT_CHAR_MINBPC
(
enc
,
p
) (0)
#endif
#ifdef
XML_MIN_SIZE
# define
CHAR_MATCHES
(
enc
,
p
,
c
) \
(AS_NORMAL_ENCODING(enc)->charMatches(enc, p, c))
static
int
PTRCALL
sb_charMatches
(
const
ENCODING
*
enc
,
const
char
*
p
,
int
c
) {
UNUSED_P
(
enc
);
return
*
p
==
c
;
}
#else
/* c is an ASCII character */
# define
CHAR_MATCHES
(
enc
,
p
,
c
) (*(p) == c)
#endif
#define
PREFIX
(
ident
) normal_##ident
#define
XML_TOK_IMPL_C
#include
"xmltok_impl.c"
#undef
XML_TOK_IMPL_C
#undef
MINBPC
#undef
BYTE_TYPE
#undef
BYTE_TO_ASCII
#undef
CHAR_MATCHES
#undef
IS_NAME_CHAR
#undef
IS_NAME_CHAR_MINBPC
#undef
IS_NMSTRT_CHAR
#undef
IS_NMSTRT_CHAR_MINBPC
#undef
IS_INVALID_CHAR
enum
{
/* UTF8_cvalN is value of masked first byte of N byte sequence */
UTF8_cval1
=
0x00
,
UTF8_cval2
=
0xc0
,
UTF8_cval3
=
0xe0
,
UTF8_cval4
=
0xf0
};
void
_INTERNAL_trim_to_complete_utf8_characters
(
const
char
*
from
,
const
char
*
*
fromLimRef
) {
const
char
*
fromLim
=
*
fromLimRef
;
size_t
walked
=
0
;
for
(;
fromLim
>
from
;
fromLim
--
,
walked
++
) {
const
unsigned
char
prev
=
(
unsigned
char
)
fromLim
[
-1
];
if
((
prev
&
0xf8u
)
==
0xf0u
) {
/* 4-byte character, lead by 0b11110xxx byte */
if
(
walked
+
1
>=
4
) {
fromLim
+=
4
-
1
;
break
;
}
else
{
walked
=
0
;
}
}
else
if
((
prev
&
0xf0u
)
==
0xe0u
) {
/* 3-byte character, lead by 0b1110xxxx byte */
if
(
walked
+
1
>=
3
) {
fromLim
+=
3
-
1
;
break
;
}
else
{
walked
=
0
;
}
}
else
if
((
prev
&
0xe0u
)
==
0xc0u
) {
/* 2-byte character, lead by 0b110xxxxx byte */
if
(
walked
+
1
>=
2
) {
fromLim
+=
2
-
1
;
break
;
}
else
{
walked
=
0
;
}
}
else
if
((
prev
&
0x80u
)
==
0x00u
) {
/* 1-byte character, matching 0b0xxxxxxx */
break
;
}
}
*
fromLimRef
=
fromLim
;
}
static
enum
XML_Convert_Result
PTRCALL
utf8_toUtf8
(
const
ENCODING
*
enc
,
const
char
*
*
fromP
,
const
char
*
fromLim
,
char
*
*
toP
,
const
char
*
toLim
) {
bool
input_incomplete
=
false;
bool
output_exhausted
=
false;
/* Avoid copying partial characters (due to limited space). */
const
ptrdiff_t
bytesAvailable
=
fromLim
-
*
fromP
;
const
ptrdiff_t
bytesStorable
=
toLim
-
*
toP
;
UNUSED_P
(
enc
);
if
(
bytesAvailable
>
bytesStorable
) {
fromLim
=
*
fromP
+
bytesStorable
;
output_exhausted
=
true;
}
/* Avoid copying partial characters (from incomplete input). */
{
const
char
*
const
fromLimBefore
=
fromLim
;
_INTERNAL_trim_to_complete_utf8_characters
(
*
fromP
,
&
fromLim
);
if
(
fromLim
<
fromLimBefore
) {
input_incomplete
=
true;
}
}
{
const
ptrdiff_t
bytesToCopy
=
fromLim
-
*
fromP
;
memcpy
(
*
toP
,
*
fromP
,
bytesToCopy
);
*
fromP
+=
bytesToCopy
;
*
toP
+=
bytesToCopy
;
}
if
(
output_exhausted
)
/* needs to go first */
return
XML_CONVERT_OUTPUT_EXHAUSTED
;
else
if
(
input_incomplete
)
return
XML_CONVERT_INPUT_INCOMPLETE
;
else
return
XML_CONVERT_COMPLETED
;
}
static
enum
XML_Convert_Result
PTRCALL
utf8_toUtf16
(
const
ENCODING
*
enc
,
const
char
*
*
fromP
,
const
char
*
fromLim
,
unsigned short
*
*
toP
,
const
unsigned short
*
toLim
) {
enum
XML_Convert_Result
res
=
XML_CONVERT_COMPLETED
;
unsigned short
*
to
=
*
toP
;
const
char
*
from
=
*
fromP
;
while
(
from
<
fromLim
&&
to
<
toLim
) {
switch
(((
struct
normal_encoding
*
)
enc
)
->
type
[(
unsigned
char
)
*
from
]) {
case
BT_LEAD2
:
if
(
fromLim
-
from
<
2
) {
res
=
XML_CONVERT_INPUT_INCOMPLETE
;
goto
after
;
}
*
to
++
=
(
unsigned short
)(((
from
[
0
]
&
0x1f
) <<
6
) | (
from
[
1
]
&
0x3f
));
from
+=
2
;
break
;
case
BT_LEAD3
:
if
(
fromLim
-
from
<
3
) {
res
=
XML_CONVERT_INPUT_INCOMPLETE
;
goto
after
;
}
*
to
++
=
(
unsigned short
)(((
from
[
0
]
&
0xf
) <<
12
) | ((
from
[
1
]
&
0x3f
) <<
6
)
| (
from
[
2
]
&
0x3f
));
from
+=
3
;
break
;
case
BT_LEAD4
: {
unsigned long
n
;
if
(
toLim
-
to
<
2
) {
res
=
XML_CONVERT_OUTPUT_EXHAUSTED
;
goto
after
;
}
if
(
fromLim
-
from
<
4
) {
res
=
XML_CONVERT_INPUT_INCOMPLETE
;
goto
after
;
}
n
=
((
from
[
0
]
&
0x7
) <<
18
) | ((
from
[
1
]
&
0x3f
) <<
12
)
| ((
from
[
2
]
&
0x3f
) <<
6
) | (
from
[
3
]
&
0x3f
);
n
-=
0x10000
;
to
[
0
]
=
(
unsigned short
)((
n
>>
10
) |
0xD800
);
to
[
1
]
=
(
unsigned short
)((
n
&
0x3FF
) |
0xDC00
);
to
+=
2
;
from
+=
4
;
}
break
;
default
:
*
to
++
=
*
from
++
;
break
;
}
}
if
(
from
<
fromLim
)
res
=
XML_CONVERT_OUTPUT_EXHAUSTED
;
after
:
*
fromP
=
from
;
*
toP
=
to
;
return
res
;
}
#ifdef
XML_NS
static
const
struct
normal_encoding
utf8_encoding_ns
=
{{
VTABLE1
,
utf8_toUtf8
,
utf8_toUtf16
,
1
,
1
,
0
},
{
# include
"asciitab.h"
# include
"utf8tab.h"
},
STANDARD_VTABLE
(
sb_
)
NORMAL_VTABLE
(
utf8_
)};
#endif
static
const
struct
normal_encoding
utf8_encoding
=
{{
VTABLE1
,
utf8_toUtf8
,
utf8_toUtf16
,
1
,
1
,
0
},
{
#define
BT_COLON
BT_NMSTRT
#include
"asciitab.h"
#undef
BT_COLON
#include
"utf8tab.h"
},
STANDARD_VTABLE
(
sb_
)
NORMAL_VTABLE
(
utf8_
)};
#ifdef
XML_NS
static
const
struct
normal_encoding
internal_utf8_encoding_ns
=
{{
VTABLE1
,
utf8_toUtf8
,
utf8_toUtf16
,
1
,
1
,
0
},
{
# include
"iasciitab.h"
# include
"utf8tab.h"
},
STANDARD_VTABLE
(
sb_
)
NORMAL_VTABLE
(
utf8_
)};
#endif
static
const
struct
normal_encoding
internal_utf8_encoding
=
{{
VTABLE1
,
utf8_toUtf8
,
utf8_toUtf16
,
1
,
1
,
0
},
{
#define
BT_COLON
BT_NMSTRT
#include
"iasciitab.h"
#undef
BT_COLON
#include
"utf8tab.h"
},
STANDARD_VTABLE
(
sb_
)
NORMAL_VTABLE
(
utf8_
)};
static
enum
XML_Convert_Result
PTRCALL
latin1_toUtf8
(
const
ENCODING
*
enc
,
const
char
*
*
fromP
,
const
char
*
fromLim
,
char
*
*
toP
,
const
char
*
toLim
) {
UNUSED_P
(
enc
);
for
(;;) {
unsigned
char
c
;
if
(
*
fromP
==
fromLim
)
return
XML_CONVERT_COMPLETED
;
c
=
(
unsigned
char
)
*
*
fromP
;
if
(
c
&
0x80
) {
if
(
toLim
-
*
toP
<
2
)
return
XML_CONVERT_OUTPUT_EXHAUSTED
;
*
(
*
toP
)
++
=
(
char
)((
c
>>
6
) |
UTF8_cval2
);
*
(
*
toP
)
++
=
(
char
)((
c
&
0x3f
) |
0x80
);
(
*
fromP
)
++
;
}
else
{
if
(
*
toP
==
toLim
)
return
XML_CONVERT_OUTPUT_EXHAUSTED
;
*
(
*
toP
)
++
=
*
(
*
fromP
)
++
;
}
}
}
static
enum
XML_Convert_Result
PTRCALL
latin1_toUtf16
(
const
ENCODING
*
enc
,
const
char
*
*
fromP
,
const
char
*
fromLim
,
unsigned short
*
*
toP
,
const
unsigned short
*
toLim
) {
UNUSED_P
(
enc
);
while
(
*
fromP
<
fromLim
&&
*
toP
<
toLim
)
*
(
*
toP
)
++
=
(
unsigned
char
)
*
(
*
fromP
)
++
;
if
((
*
toP
==
toLim
)
&&
(
*
fromP
<
fromLim
))
return
XML_CONVERT_OUTPUT_EXHAUSTED
;
else
return
XML_CONVERT_COMPLETED
;
}
#ifdef
XML_NS
static
const
struct
normal_encoding
latin1_encoding_ns
=
{{
VTABLE1
,
latin1_toUtf8
,
latin1_toUtf16
,
1
,
0
,
0
},
{
# include
"asciitab.h"
# include
"latin1tab.h"
},
STANDARD_VTABLE
(
sb_
)
NULL_VTABLE
};
#endif
static
const
struct
normal_encoding
latin1_encoding
=
{{
VTABLE1
,
latin1_toUtf8
,
latin1_toUtf16
,
1
,
0
,
0
},
{
#define
BT_COLON
BT_NMSTRT
#include
"asciitab.h"
#undef
BT_COLON
#include
"latin1tab.h"
},
STANDARD_VTABLE
(
sb_
)
NULL_VTABLE
};
static
enum
XML_Convert_Result
PTRCALL
ascii_toUtf8
(
const
ENCODING
*
enc
,
const
char
*
*
fromP
,
const
char
*
fromLim
,
char
*
*
toP
,
const
char
*
toLim
) {
UNUSED_P
(
enc
);
while
(
*
fromP
<
fromLim
&&
*
toP
<
toLim
)
*
(
*
toP
)
++
=
*
(
*
fromP
)
++
;
if
((
*
toP
==
toLim
)
&&
(
*
fromP
<
fromLim
))
return
XML_CONVERT_OUTPUT_EXHAUSTED
;
else
return
XML_CONVERT_COMPLETED
;
}
#ifdef
XML_NS
static
const
struct
normal_encoding
ascii_encoding_ns
=
{{
VTABLE1
,
ascii_toUtf8
,
latin1_toUtf16
,
1
,
1
,
0
},
{
# include
"asciitab.h"
/* BT_NONXML == 0 */
},
STANDARD_VTABLE
(
sb_
)
NULL_VTABLE
};
#endif
static
const
struct
normal_encoding
ascii_encoding
=
{{
VTABLE1
,
ascii_toUtf8
,
latin1_toUtf16
,
1
,
1
,
0
},
{
#define
BT_COLON
BT_NMSTRT
#include
"asciitab.h"
#undef
BT_COLON
/* BT_NONXML == 0 */
},
STANDARD_VTABLE
(
sb_
)
NULL_VTABLE
};
static
int
PTRFASTCALL
unicode_byte_type
(
char
hi
,
char
lo
) {
switch
((
unsigned
char
)
hi
) {
/* 0xD800–0xDBFF first 16-bit code unit or high surrogate (W1) */
case
0xD8
:
case
0xD9
:
case
0xDA
:
case
0xDB
:
return
BT_LEAD4
;
/* 0xDC00–0xDFFF second 16-bit code unit or low surrogate (W2) */
case
0xDC
:
case
0xDD
:
case
0xDE
:
case
0xDF
:
return
BT_TRAIL
;
case
0xFF
:
switch
((
unsigned
char
)
lo
) {
case
0xFF
:
/* noncharacter-FFFF */
case
0xFE
:
/* noncharacter-FFFE */
return
BT_NONXML
;
}
break
;
}
return
BT_NONASCII
;
}
#define
DEFINE_UTF16_TO_UTF8
(
E
) \
static enum XML_Convert_Result PTRCALL E##toUtf8( \
const ENCODING *enc, const char **fromP, const char *fromLim, \
char **toP, const char *toLim) { \
const char *from = *fromP; \
UNUSED_P(enc); \
fromLim = from + (((fromLim - from) >> 1) << 1);
/* shrink to even */
\
for
(;
from
<
fromLim
;
from
+=
2
) { \
int
plane
; \
unsigned
char
lo2
; \
unsigned
char
lo
=
GET_LO
(
from
); \
unsigned
char
hi
=
GET_HI
(
from
); \
switch
(
hi
) { \
case
0
: \
if
(
lo
<
0x80
) { \
if
(
*
toP
==
toLim
) { \
*
fromP
=
from
; \
return
XML_CONVERT_OUTPUT_EXHAUSTED
; \
} \
*
(
*
toP
)
++
=
lo
; \
break
; \
} \
/* fall through */
\
case
0x1
: \
case
0x2
: \
case
0x3
: \
case
0x4
: \
case
0x5
: \
case
0x6
: \
case
0x7
: \
if
(
toLim
-
*
toP
<
2
) { \
*
fromP
=
from
; \
return
XML_CONVERT_OUTPUT_EXHAUSTED
; \
} \
*
(
*
toP
)
++
=
((
lo
>>
6
) | (
hi
<<
2
) |
UTF8_cval2
); \
*
(
*
toP
)
++
=
((
lo
&
0x3f
) |
0x80
); \
break
; \
default
: \
if
(
toLim
-
*
toP
<
3
) { \
*
fromP
=
from
; \
return
XML_CONVERT_OUTPUT_EXHAUSTED
; \
} \
/* 16 bits divided 4, 6, 6 amongst 3 bytes */
\
*
(
*
toP
)
++
=
((
hi
>>
4
) |
UTF8_cval3
); \
*
(
*
toP
)
++
=
(((
hi
&
0xf
) <<
2
) | (
lo
>>
6
) |
0x80
); \
*
(
*
toP
)
++
=
((
lo
&
0x3f
) |
0x80
); \
break
; \
case
0xD8
: \
case
0xD9
: \
case
0xDA
: \
case
0xDB
: \
if
(
toLim
-
*
toP
<
4
) { \
*
fromP
=
from
; \
return
XML_CONVERT_OUTPUT_EXHAUSTED
; \
} \
if
(
fromLim
-
from
<
4
) { \
*
fromP
=
from
; \
return
XML_CONVERT_INPUT_INCOMPLETE
; \
} \
plane
=
(((
hi
&
0x3
) <<
2
) | ((
lo
>>
6
)
&
0x3
))
+
1
; \
*
(
*
toP
)
++
=
(
char
)((
plane
>>
2
) |
UTF8_cval4
); \
*
(
*
toP
)
++
=
(((
lo
>>
2
)
&
0xF
) | ((
plane
&
0x3
) <<
4
) |
0x80
); \
from
+=
2
; \
lo2
=
GET_LO
(
from
); \
*
(
*
toP
)
++
=
(((
lo
&
0x3
) <<
4
) | ((
GET_HI
(
from
)
&
0x3
) <<
2
) \
| (
lo2
>>
6
) |
0x80
); \
*
(
*
toP
)
++
=
((
lo2
&
0x3f
) |
0x80
); \
break
; \
} \
} \
*
fromP
=
from
; \
if
(
from
<
fromLim
) \
return
XML_CONVERT_INPUT_INCOMPLETE
; \
else
\
return
XML_CONVERT_COMPLETED
; \
}
#define
DEFINE_UTF16_TO_UTF16
(
E
) \
static enum XML_Convert_Result PTRCALL E##toUtf16( \
const ENCODING *enc, const char **fromP, const char *fromLim, \
unsigned short **toP, const unsigned short *toLim) { \
enum XML_Convert_Result res = XML_CONVERT_COMPLETED; \
UNUSED_P(enc); \
fromLim = *fromP + (((fromLim - *fromP) >> 1) << 1);
/* shrink to even */
\
/* Avoid copying first half only of surrogate */
\
if
(
fromLim
-
*
fromP
>
((
toLim
-
*
toP
) <<
1
) \
&&
(
GET_HI
(
fromLim
-
2
)
&
0xF8
)
==
0xD8
) { \
fromLim
-=
2
; \
res
=
XML_CONVERT_INPUT_INCOMPLETE
; \
} \
for
(;
*
fromP
<
fromLim
&&
*
toP
<
toLim
;
*
fromP
+=
2
) \
*
(
*
toP
)
++
=
(
GET_HI
(
*
fromP
) <<
8
) |
GET_LO
(
*
fromP
); \
if
((
*
toP
==
toLim
)
&&
(
*
fromP
<
fromLim
)) \
return
XML_CONVERT_OUTPUT_EXHAUSTED
; \
else
\
return
res
; \
}
#define
SET2
(
ptr
,
ch
) (((ptr)[0] = ((ch)&0xff)), ((ptr)[1] = ((ch) >> 8)))
#define
GET_LO
(
ptr
) ((unsigned char)(ptr)[0])
#define
GET_HI
(
ptr
) ((unsigned char)(ptr)[1])
DEFINE_UTF16_TO_UTF8
(
little2_
)
DEFINE_UTF16_TO_UTF16
(
little2_
)
#undef
SET2
#undef
GET_LO
#undef
GET_HI
#define
SET2
(
ptr
,
ch
) (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch)&0xFF)))
#define
GET_LO
(
ptr
) ((unsigned char)(ptr)[1])
#define
GET_HI
(
ptr
) ((unsigned char)(ptr)[0])
DEFINE_UTF16_TO_UTF8
(
big2_
)
DEFINE_UTF16_TO_UTF16
(
big2_
)
#undef
SET2
#undef
GET_LO
#undef
GET_HI
#define
LITTLE2_BYTE_TYPE
(
enc
,
p
) \
((p)[1] == 0 ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
: unicode_byte_type((p)[1], (p)[0]))
#define
LITTLE2_BYTE_TO_ASCII
(
p
) ((p)[1] == 0 ? (p)[0] : -1)
#define
LITTLE2_CHAR_MATCHES
(
p
,
c
) ((p)[1] == 0 && (p)[0] == c)
#define
LITTLE2_IS_NAME_CHAR_MINBPC
(
p
) \
UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
#define
LITTLE2_IS_NMSTRT_CHAR_MINBPC
(
p
) \
UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
#ifdef
XML_MIN_SIZE
static
int
PTRFASTCALL
little2_byteType
(
const
ENCODING
*
enc
,
const
char
*
p
) {
return
LITTLE2_BYTE_TYPE
(
enc
,
p
);
}
static
int
PTRFASTCALL
little2_byteToAscii
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
LITTLE2_BYTE_TO_ASCII
(
p
);
}
static
int
PTRCALL
little2_charMatches
(
const
ENCODING
*
enc
,
const
char
*
p
,
int
c
) {
UNUSED_P
(
enc
);
return
LITTLE2_CHAR_MATCHES
(
p
,
c
);
}
static
int
PTRFASTCALL
little2_isNameMin
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
LITTLE2_IS_NAME_CHAR_MINBPC
(
p
);
}
static
int
PTRFASTCALL
little2_isNmstrtMin
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
LITTLE2_IS_NMSTRT_CHAR_MINBPC
(
p
);
}
# undef
VTABLE
# define
VTABLE
VTABLE1, little2_toUtf8, little2_toUtf16
#else
/* not XML_MIN_SIZE */
# undef
PREFIX
# define
PREFIX
(
ident
) little2_##ident
# define
MINBPC
(
enc
) 2
/* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
# define
BYTE_TYPE
(
enc
,
p
) LITTLE2_BYTE_TYPE(enc, p)
# define
BYTE_TO_ASCII
(
enc
,
p
) LITTLE2_BYTE_TO_ASCII(p)
# define
CHAR_MATCHES
(
enc
,
p
,
c
) LITTLE2_CHAR_MATCHES(p, c)
# define
IS_NAME_CHAR
(
enc
,
p
,
n
) 0
# define
IS_NAME_CHAR_MINBPC
(
enc
,
p
) LITTLE2_IS_NAME_CHAR_MINBPC(p)
# define
IS_NMSTRT_CHAR
(
enc
,
p
,
n
) (0)
# define
IS_NMSTRT_CHAR_MINBPC
(
enc
,
p
) LITTLE2_IS_NMSTRT_CHAR_MINBPC(p)
# define
XML_TOK_IMPL_C
# include
"xmltok_impl.c"
# undef
XML_TOK_IMPL_C
# undef
MINBPC
# undef
BYTE_TYPE
# undef
BYTE_TO_ASCII
# undef
CHAR_MATCHES
# undef
IS_NAME_CHAR
# undef
IS_NAME_CHAR_MINBPC
# undef
IS_NMSTRT_CHAR
# undef
IS_NMSTRT_CHAR_MINBPC
# undef
IS_INVALID_CHAR
#endif
/* not XML_MIN_SIZE */
#ifdef
XML_NS
static
const
struct
normal_encoding
little2_encoding_ns
=
{{
VTABLE
,
2
,
0
,
# if
BYTEORDER
==
1234
1
# else
0
# endif
},
{
# include
"asciitab.h"
# include
"latin1tab.h"
},
STANDARD_VTABLE
(
little2_
)
NULL_VTABLE
};
#endif
static
const
struct
normal_encoding
little2_encoding
=
{{
VTABLE
,
2
,
0
,
#if
BYTEORDER
==
1234
1
#else
0
#endif
},
{
#define
BT_COLON
BT_NMSTRT
#include
"asciitab.h"
#undef
BT_COLON
#include
"latin1tab.h"
},
STANDARD_VTABLE
(
little2_
)
NULL_VTABLE
};
#if
BYTEORDER
!=
4321
# ifdef
XML_NS
static
const
struct
normal_encoding
internal_little2_encoding_ns
=
{{
VTABLE
,
2
,
0
,
1
},
{
# include
"iasciitab.h"
# include
"latin1tab.h"
},
STANDARD_VTABLE
(
little2_
)
NULL_VTABLE
};
# endif
static
const
struct
normal_encoding
internal_little2_encoding
=
{{
VTABLE
,
2
,
0
,
1
},
{
# define
BT_COLON
BT_NMSTRT
# include
"iasciitab.h"
# undef
BT_COLON
# include
"latin1tab.h"
},
STANDARD_VTABLE
(
little2_
)
NULL_VTABLE
};
#endif
#define
BIG2_BYTE_TYPE
(
enc
,
p
) \
((p)[0] == 0 \
? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
: unicode_byte_type((p)[0], (p)[1]))
#define
BIG2_BYTE_TO_ASCII
(
p
) ((p)[0] == 0 ? (p)[1] : -1)
#define
BIG2_CHAR_MATCHES
(
p
,
c
) ((p)[0] == 0 && (p)[1] == c)
#define
BIG2_IS_NAME_CHAR_MINBPC
(
p
) \
UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
#define
BIG2_IS_NMSTRT_CHAR_MINBPC
(
p
) \
UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
#ifdef
XML_MIN_SIZE
static
int
PTRFASTCALL
big2_byteType
(
const
ENCODING
*
enc
,
const
char
*
p
) {
return
BIG2_BYTE_TYPE
(
enc
,
p
);
}
static
int
PTRFASTCALL
big2_byteToAscii
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
BIG2_BYTE_TO_ASCII
(
p
);
}
static
int
PTRCALL
big2_charMatches
(
const
ENCODING
*
enc
,
const
char
*
p
,
int
c
) {
UNUSED_P
(
enc
);
return
BIG2_CHAR_MATCHES
(
p
,
c
);
}
static
int
PTRFASTCALL
big2_isNameMin
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
BIG2_IS_NAME_CHAR_MINBPC
(
p
);
}
static
int
PTRFASTCALL
big2_isNmstrtMin
(
const
ENCODING
*
enc
,
const
char
*
p
) {
UNUSED_P
(
enc
);
return
BIG2_IS_NMSTRT_CHAR_MINBPC
(
p
);
}
# undef
VTABLE
# define
VTABLE
VTABLE1, big2_toUtf8, big2_toUtf16
#else
/* not XML_MIN_SIZE */
# undef
PREFIX
# define
PREFIX
(
ident
) big2_##ident
# define
MINBPC
(
enc
) 2
/* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
# define
BYTE_TYPE
(
enc
,
p
) BIG2_BYTE_TYPE(enc, p)
# define
BYTE_TO_ASCII
(
enc
,
p
) BIG2_BYTE_TO_ASCII(p)
# define
CHAR_MATCHES
(
enc
,
p
,
c
) BIG2_CHAR_MATCHES(p, c)
# define
IS_NAME_CHAR
(
enc
,
p
,
n
) 0
# define
IS_NAME_CHAR_MINBPC
(
enc
,
p
) BIG2_IS_NAME_CHAR_MINBPC(p)
# define
IS_NMSTRT_CHAR
(
enc
,
p
,
n
) (0)
# define
IS_NMSTRT_CHAR_MINBPC
(
enc
,
p
) BIG2_IS_NMSTRT_CHAR_MINBPC(p)
# define
XML_TOK_IMPL_C
# include
"xmltok_impl.c"
# undef
XML_TOK_IMPL_C
# undef
MINBPC
# undef
BYTE_TYPE
# undef
BYTE_TO_ASCII
# undef
CHAR_MATCHES
# undef
IS_NAME_CHAR
# undef
IS_NAME_CHAR_MINBPC
# undef
IS_NMSTRT_CHAR
# undef
IS_NMSTRT_CHAR_MINBPC
# undef
IS_INVALID_CHAR
#endif
/* not XML_MIN_SIZE */
#ifdef
XML_NS
static
const
struct
normal_encoding
big2_encoding_ns
=
{{
VTABLE
,
2
,
0
,
# if
BYTEORDER
==
4321
1
# else
0
# endif
},
{
# include
"asciitab.h"
# include
"latin1tab.h"
},
STANDARD_VTABLE
(
big2_
)
NULL_VTABLE
};
#endif
static
const
struct
normal_encoding
big2_encoding
=
{{
VTABLE
,
2
,
0
,
#if
BYTEORDER
==
4321
1
#else
0
#endif
},
{
#define
BT_COLON
BT_NMSTRT
#include
"asciitab.h"
#undef
BT_COLON
#include
"latin1tab.h"
},
STANDARD_VTABLE
(
big2_
)
NULL_VTABLE
};
#if
BYTEORDER
!=
1234
# ifdef
XML_NS
static
const
struct
normal_encoding
internal_big2_encoding_ns
=
{{
VTABLE
,
2
,
0
,
1
},
{
# include
"iasciitab.h"
# include
"latin1tab.h"
},
STANDARD_VTABLE
(
big2_
)
NULL_VTABLE
};
# endif
static
const
struct
normal_encoding
internal_big2_encoding
=
{{
VTABLE
,
2
,
0
,
1
},
{
# define
BT_COLON
BT_NMSTRT
# include
"iasciitab.h"
# undef
BT_COLON
# include
"latin1tab.h"
},
STANDARD_VTABLE
(
big2_
)
NULL_VTABLE
};
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL