1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or
5  *  (at your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *  GNU General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  *  Jabber
17  *  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
18  */
19 
NS(XmlGetUtf8InternalEncoding)20 const ENCODING *NS(XmlGetUtf8InternalEncoding)()
21 {
22     return &ns(internal_utf8_encoding).enc;
23 }
24 
NS(XmlGetUtf16InternalEncoding)25 const ENCODING *NS(XmlGetUtf16InternalEncoding)()
26 {
27 #if XML_BYTE_ORDER == 12
28     return &ns(internal_little2_encoding).enc;
29 #elif XML_BYTE_ORDER == 21
30 return &ns(internal_big2_encoding).enc;
31 #else
32 const short n = 1;
33     return *(const char *)&n ? &ns(internal_little2_encoding).enc : &ns(internal_big2_encoding).enc;
34 #endif
35 }
36 
37 static
38 const ENCODING *NS(encodings)[] = {
39     &ns(latin1_encoding).enc,
40     &ns(ascii_encoding).enc,
41     &ns(utf8_encoding).enc,
42     &ns(big2_encoding).enc,
43     &ns(big2_encoding).enc,
44     &ns(little2_encoding).enc,
45     &ns(utf8_encoding).enc /* NO_ENC */
46 };
47 
48 static
NS(initScanProlog)49 int NS(initScanProlog)(const ENCODING *enc, const char *ptr, const char *end,
50                        const char **nextTokPtr)
51 {
52     return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_PROLOG_STATE, ptr, end, nextTokPtr);
53 }
54 
55 static
NS(initScanContent)56 int NS(initScanContent)(const ENCODING *enc, const char *ptr, const char *end,
57                         const char **nextTokPtr)
58 {
59     return initScan(NS(encodings), (const INIT_ENCODING *)enc, XML_CONTENT_STATE, ptr, end, nextTokPtr);
60 }
61 
NS(XmlInitEncoding)62 int NS(XmlInitEncoding)(INIT_ENCODING *p, const ENCODING **encPtr, const char *name)
63 {
64     int i = getEncodingIndex(name);
65     if (i == UNKNOWN_ENC)
66         return 0;
67     INIT_ENC_INDEX(p) = (char)i;
68     p->initEnc.scanners[XML_PROLOG_STATE] = NS(initScanProlog);
69     p->initEnc.scanners[XML_CONTENT_STATE] = NS(initScanContent);
70     p->initEnc.updatePosition = initUpdatePosition;
71     p->encPtr = encPtr;
72     *encPtr = &(p->initEnc);
73     return 1;
74 }
75 
76 static
NS(findEncoding)77 const ENCODING *NS(findEncoding)(const ENCODING *enc, const char *ptr, const char *end)
78 {
79 #define ENCODING_MAX 128
80     char buf[ENCODING_MAX];
81     char *p = buf;
82     int i;
83     XmlUtf8Convert(enc, &ptr, end, &p, p + ENCODING_MAX - 1);
84     if (ptr != end)
85         return 0;
86     *p = 0;
87     if (streqci(buf, "UTF-16") && enc->minBytesPerChar == 2)
88         return enc;
89     i = getEncodingIndex(buf);
90     if (i == UNKNOWN_ENC)
91         return 0;
92     return NS(encodings)[i];
93 }
94 
NS(XmlParseXmlDecl)95 int NS(XmlParseXmlDecl)(int isGeneralTextEntity,
96                         const ENCODING *enc,
97                         const char *ptr,
98                         const char *end,
99                         const char **badPtr,
100                         const char **versionPtr,
101                         const char **encodingName,
102                         const ENCODING **encoding,
103                         int *standalone)
104 {
105     return doParseXmlDecl(NS(findEncoding),
106                           isGeneralTextEntity,
107                           enc,
108                           ptr,
109                           end,
110                           badPtr,
111                           versionPtr,
112                           encodingName,
113                           encoding,
114                           standalone);
115 }
116