/gdal/ogr/ogr_expat.cpp

https://github.com/aashish24/gdal-cmake · C++ · 150 lines · 89 code · 20 blank · 41 comment · 5 complexity · a487ef51688554b434d29a861091fab2 MD5 · raw file

  1. /******************************************************************************
  2. * $Id$
  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. #ifdef HAVE_EXPAT
  30. #include "ogr_expat.h"
  31. #include "cpl_error.h"
  32. CPL_CVSID("$Id$");
  33. #define OGR_EXPAT_MAX_ALLOWED_ALLOC 10000000
  34. /************************************************************************/
  35. /* OGRExpatMalloc() */
  36. /************************************************************************/
  37. static void* OGRExpatMalloc(size_t size)
  38. {
  39. if (size < OGR_EXPAT_MAX_ALLOWED_ALLOC)
  40. return malloc(size);
  41. else
  42. {
  43. CPLError(CE_Failure, CPLE_OutOfMemory,
  44. "Expat tried to malloc %d bytes. File probably corrupted", (int)size);
  45. return NULL;
  46. }
  47. }
  48. /************************************************************************/
  49. /* OGRExpatRealloc() */
  50. /************************************************************************/
  51. static void* OGRExpatRealloc(void *ptr, size_t size)
  52. {
  53. if (size < OGR_EXPAT_MAX_ALLOWED_ALLOC)
  54. return realloc(ptr, size);
  55. else
  56. {
  57. CPLError(CE_Failure, CPLE_OutOfMemory,
  58. "Expat tried to realloc %d bytes. File probably corrupted", (int)size);
  59. free(ptr);
  60. return NULL;
  61. }
  62. }
  63. /************************************************************************/
  64. /* OGRExpatUnknownEncodingHandler() */
  65. /************************************************************************/
  66. static int OGRExpatUnknownEncodingHandler (void *unused_encodingHandlerData,
  67. const XML_Char *name,
  68. XML_Encoding *info)
  69. {
  70. if (!(EQUAL(name, "WINDOWS-1252")))
  71. return XML_STATUS_ERROR;
  72. /* Map CP1252 bytes to Unicode values */
  73. int i;
  74. for(i=0;i<0x80;i++)
  75. info->map[i] = i;
  76. info->map[0x80] = 0x20AC;
  77. info->map[0x81] = -1;
  78. info->map[0x82] = 0x201A;
  79. info->map[0x83] = 0x0192;
  80. info->map[0x84] = 0x201E;
  81. info->map[0x85] = 0x2026;
  82. info->map[0x86] = 0x2020;
  83. info->map[0x87] = 0x2021;
  84. info->map[0x88] = 0x02C6;
  85. info->map[0x89] = 0x2030;
  86. info->map[0x8A] = 0x0160;
  87. info->map[0x8B] = 0x2039;
  88. info->map[0x8C] = 0x0152;
  89. info->map[0x8D] = -1;
  90. info->map[0x8E] = 0x017D;
  91. info->map[0x8F] = -1;
  92. info->map[0x90] = -1;
  93. info->map[0x91] = 0x2018;
  94. info->map[0x92] = 0x2019;
  95. info->map[0x93] = 0x201C;
  96. info->map[0x94] = 0x201D;
  97. info->map[0x95] = 0x2022;
  98. info->map[0x96] = 0x2013;
  99. info->map[0x97] = 0x2014;
  100. info->map[0x98] = 0x02DC;
  101. info->map[0x99] = 0x2122;
  102. info->map[0x9A] = 0x0161;
  103. info->map[0x9B] = 0x203A;
  104. info->map[0x9C] = 0x0153;
  105. info->map[0x9D] = -1;
  106. info->map[0x9E] = 0x017E;
  107. info->map[0x9F] = 0x0178;
  108. for(i=0xA0;i<=0xFF;i++)
  109. info->map[i] = i;
  110. info->data = NULL;
  111. info->convert = NULL;
  112. info->release = NULL;
  113. return XML_STATUS_OK;
  114. }
  115. /************************************************************************/
  116. /* OGRCreateExpatXMLParser() */
  117. /************************************************************************/
  118. XML_Parser OGRCreateExpatXMLParser()
  119. {
  120. XML_Memory_Handling_Suite memsuite;
  121. memsuite.malloc_fcn = OGRExpatMalloc;
  122. memsuite.realloc_fcn = OGRExpatRealloc;
  123. memsuite.free_fcn = free;
  124. XML_Parser hParser = XML_ParserCreate_MM(NULL, &memsuite, NULL);
  125. XML_SetUnknownEncodingHandler(hParser,
  126. OGRExpatUnknownEncodingHandler,
  127. NULL);
  128. return hParser;
  129. }
  130. #endif