1 : /******************************************************************************
2 : * $Id: ogr_expat.cpp 24982 2012-09-26 19:41:33Z rouault $
3 : *
4 : * Project: OGR
5 : * Purpose: Convenience function for parsing with Expat library
6 : * Author: Even Rouault, even dot rouault at mines dash paris dot org
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2009, Even Rouault
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifdef HAVE_EXPAT
31 :
32 : #include "ogr_expat.h"
33 : #include "cpl_error.h"
34 :
35 : CPL_CVSID("$Id: ogr_expat.cpp 24982 2012-09-26 19:41:33Z rouault $");
36 :
37 : #define OGR_EXPAT_MAX_ALLOWED_ALLOC 10000000
38 :
39 : /************************************************************************/
40 : /* OGRExpatMalloc() */
41 : /************************************************************************/
42 :
43 27424 : static void* OGRExpatMalloc(size_t size)
44 : {
45 27424 : if (size < OGR_EXPAT_MAX_ALLOWED_ALLOC)
46 27424 : return malloc(size);
47 : else
48 : {
49 : CPLError(CE_Failure, CPLE_OutOfMemory,
50 0 : "Expat tried to malloc %d bytes. File probably corrupted", (int)size);
51 0 : return NULL;
52 : }
53 : }
54 :
55 : /************************************************************************/
56 : /* OGRExpatRealloc() */
57 : /************************************************************************/
58 :
59 198 : static void* OGRExpatRealloc(void *ptr, size_t size)
60 : {
61 198 : if (size < OGR_EXPAT_MAX_ALLOWED_ALLOC)
62 198 : return realloc(ptr, size);
63 : else
64 : {
65 : CPLError(CE_Failure, CPLE_OutOfMemory,
66 0 : "Expat tried to realloc %d bytes. File probably corrupted", (int)size);
67 0 : free(ptr);
68 0 : return NULL;
69 : }
70 : }
71 :
72 : /************************************************************************/
73 : /* FillWINDOWS1252() */
74 : /************************************************************************/
75 :
76 0 : static void FillWINDOWS1252(XML_Encoding *info)
77 : {
78 : /* Map CP1252 bytes to Unicode values */
79 : int i;
80 0 : for(i=0;i<0x80;i++)
81 0 : info->map[i] = i;
82 :
83 0 : info->map[0x80] = 0x20AC;
84 0 : info->map[0x81] = -1;
85 0 : info->map[0x82] = 0x201A;
86 0 : info->map[0x83] = 0x0192;
87 0 : info->map[0x84] = 0x201E;
88 0 : info->map[0x85] = 0x2026;
89 0 : info->map[0x86] = 0x2020;
90 0 : info->map[0x87] = 0x2021;
91 0 : info->map[0x88] = 0x02C6;
92 0 : info->map[0x89] = 0x2030;
93 0 : info->map[0x8A] = 0x0160;
94 0 : info->map[0x8B] = 0x2039;
95 0 : info->map[0x8C] = 0x0152;
96 0 : info->map[0x8D] = -1;
97 0 : info->map[0x8E] = 0x017D;
98 0 : info->map[0x8F] = -1;
99 0 : info->map[0x90] = -1;
100 0 : info->map[0x91] = 0x2018;
101 0 : info->map[0x92] = 0x2019;
102 0 : info->map[0x93] = 0x201C;
103 0 : info->map[0x94] = 0x201D;
104 0 : info->map[0x95] = 0x2022;
105 0 : info->map[0x96] = 0x2013;
106 0 : info->map[0x97] = 0x2014;
107 0 : info->map[0x98] = 0x02DC;
108 0 : info->map[0x99] = 0x2122;
109 0 : info->map[0x9A] = 0x0161;
110 0 : info->map[0x9B] = 0x203A;
111 0 : info->map[0x9C] = 0x0153;
112 0 : info->map[0x9D] = -1;
113 0 : info->map[0x9E] = 0x017E;
114 0 : info->map[0x9F] = 0x0178;
115 :
116 0 : for(i=0xA0;i<=0xFF;i++)
117 0 : info->map[i] = i;
118 0 : }
119 :
120 : /************************************************************************/
121 : /* FillISO885915() */
122 : /************************************************************************/
123 :
124 0 : static void FillISO885915(XML_Encoding *info)
125 : {
126 : /* Map ISO-8859-15 bytes to Unicode values */
127 : /* Generated by generate_encoding_table.c */
128 : int i;
129 0 : for(i = 0x00; i < 0xA4; i++)
130 0 : info->map[i] = i;
131 0 : info->map[0xA4] = 0x20AC;
132 0 : info->map[0xA5] = 0xA5;
133 0 : info->map[0xA6] = 0x0160;
134 0 : info->map[0xA7] = 0xA7;
135 0 : info->map[0xA8] = 0x0161;
136 0 : for(i = 0xA9; i < 0xB4; i++)
137 0 : info->map[i] = i;
138 0 : info->map[0xB4] = 0x017D;
139 0 : for(i = 0xB5; i < 0xB8; i++)
140 0 : info->map[i] = i;
141 0 : info->map[0xB8] = 0x017E;
142 0 : for(i = 0xB9; i < 0xBC; i++)
143 0 : info->map[i] = i;
144 0 : info->map[0xBC] = 0x0152;
145 0 : info->map[0xBD] = 0x0153;
146 0 : info->map[0xBE] = 0x0178;
147 0 : for(i = 0xBF; i < 0x100; i++)
148 0 : info->map[i] = i;
149 0 : }
150 :
151 : /************************************************************************/
152 : /* OGRExpatUnknownEncodingHandler() */
153 : /************************************************************************/
154 :
155 0 : static int OGRExpatUnknownEncodingHandler (void *unused_encodingHandlerData,
156 : const XML_Char *name,
157 : XML_Encoding *info)
158 : {
159 0 : if( EQUAL(name, "WINDOWS-1252") )
160 0 : FillWINDOWS1252(info);
161 0 : else if( EQUAL(name, "ISO-8859-15") )
162 0 : FillISO885915(info);
163 : else
164 0 : return XML_STATUS_ERROR;
165 :
166 0 : info->data = NULL;
167 0 : info->convert = NULL;
168 0 : info->release = NULL;
169 :
170 0 : return XML_STATUS_OK;
171 : }
172 :
173 : /************************************************************************/
174 : /* OGRCreateExpatXMLParser() */
175 : /************************************************************************/
176 :
177 920 : XML_Parser OGRCreateExpatXMLParser()
178 : {
179 : XML_Memory_Handling_Suite memsuite;
180 920 : memsuite.malloc_fcn = OGRExpatMalloc;
181 920 : memsuite.realloc_fcn = OGRExpatRealloc;
182 920 : memsuite.free_fcn = free;
183 920 : XML_Parser hParser = XML_ParserCreate_MM(NULL, &memsuite, NULL);
184 :
185 : XML_SetUnknownEncodingHandler(hParser,
186 : OGRExpatUnknownEncodingHandler,
187 920 : NULL);
188 :
189 920 : return hParser;
190 : }
191 :
192 : #endif
|