1 : /******************************************************************************
2 : * $Id: ogr_expat.cpp 18832 2010-02-14 12:57:50Z 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 18832 2010-02-14 12:57:50Z rouault $");
36 :
37 : #define OGR_EXPAT_MAX_ALLOWED_ALLOC 10000000
38 :
39 : /************************************************************************/
40 : /* OGRExpatMalloc() */
41 : /************************************************************************/
42 :
43 46008 : static void* OGRExpatMalloc(size_t size)
44 : {
45 46008 : if (size < OGR_EXPAT_MAX_ALLOWED_ALLOC)
46 46008 : 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 330 : static void* OGRExpatRealloc(void *ptr, size_t size)
60 : {
61 330 : if (size < OGR_EXPAT_MAX_ALLOWED_ALLOC)
62 330 : 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 : /* OGRExpatUnknownEncodingHandler() */
74 : /************************************************************************/
75 :
76 0 : static int OGRExpatUnknownEncodingHandler (void *unused_encodingHandlerData,
77 : const XML_Char *name,
78 : XML_Encoding *info)
79 : {
80 0 : if (!(EQUAL(name, "WINDOWS-1252")))
81 0 : return XML_STATUS_ERROR;
82 :
83 : /* Map CP1252 bytes to Unicode values */
84 : int i;
85 0 : for(i=0;i<0x80;i++)
86 0 : info->map[i] = i;
87 :
88 0 : info->map[0x80] = 0x20AC;
89 0 : info->map[0x81] = -1;
90 0 : info->map[0x82] = 0x201A;
91 0 : info->map[0x83] = 0x0192;
92 0 : info->map[0x84] = 0x201E;
93 0 : info->map[0x85] = 0x2026;
94 0 : info->map[0x86] = 0x2020;
95 0 : info->map[0x87] = 0x2021;
96 0 : info->map[0x88] = 0x02C6;
97 0 : info->map[0x89] = 0x2030;
98 0 : info->map[0x8A] = 0x0160;
99 0 : info->map[0x8B] = 0x2039;
100 0 : info->map[0x8C] = 0x0152;
101 0 : info->map[0x8D] = -1;
102 0 : info->map[0x8E] = 0x017D;
103 0 : info->map[0x8F] = -1;
104 0 : info->map[0x90] = -1;
105 0 : info->map[0x91] = 0x2018;
106 0 : info->map[0x92] = 0x2019;
107 0 : info->map[0x93] = 0x201C;
108 0 : info->map[0x94] = 0x201D;
109 0 : info->map[0x95] = 0x2022;
110 0 : info->map[0x96] = 0x2013;
111 0 : info->map[0x97] = 0x2014;
112 0 : info->map[0x98] = 0x02DC;
113 0 : info->map[0x99] = 0x2122;
114 0 : info->map[0x9A] = 0x0161;
115 0 : info->map[0x9B] = 0x203A;
116 0 : info->map[0x9C] = 0x0153;
117 0 : info->map[0x9D] = -1;
118 0 : info->map[0x9E] = 0x017E;
119 0 : info->map[0x9F] = 0x0178;
120 :
121 0 : for(i=0xA0;i<=0xFF;i++)
122 0 : info->map[i] = i;
123 :
124 0 : info->data = NULL;
125 0 : info->convert = NULL;
126 0 : info->release = NULL;
127 :
128 0 : return XML_STATUS_OK;
129 : }
130 :
131 : /************************************************************************/
132 : /* OGRCreateExpatXMLParser() */
133 : /************************************************************************/
134 :
135 1510 : XML_Parser OGRCreateExpatXMLParser()
136 : {
137 : XML_Memory_Handling_Suite memsuite;
138 1510 : memsuite.malloc_fcn = OGRExpatMalloc;
139 1510 : memsuite.realloc_fcn = OGRExpatRealloc;
140 1510 : memsuite.free_fcn = free;
141 1510 : XML_Parser hParser = XML_ParserCreate_MM(NULL, &memsuite, NULL);
142 :
143 : XML_SetUnknownEncodingHandler(hParser,
144 : OGRExpatUnknownEncodingHandler,
145 1510 : NULL);
146 :
147 1510 : return hParser;
148 : }
149 :
150 : #endif
|