Skip to content
shpopen.c 111 KiB
Newer Older
/******************************************************************************
 * $Id: shpopen.c,v 1.76 2017-09-10 10:11:36 erouault Exp $
 *
 * Project:  Shapelib
 * Purpose:  Implementation of core Shapefile read/write functions.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 1999, 2001, Frank Warmerdam
 * Copyright (c) 2011-2013, Even Rouault <even dot rouault at mines-paris dot org>
 *
 * This software is available under the following "MIT Style" license,
 * or at the option of the licensee under the LGPL (see COPYING).  This
 * option is discussed in more detail in shapelib.html.
 *
 * --
 *
 * 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.
 ******************************************************************************
 *
 * $Log: shpopen.c,v $
 * Revision 1.76  2017-09-10 10:11:36  erouault
 * * shpopen.c: resync with GDAL copy. Make sure to zero terminate all
 * error messages. And fix regression regarding re-writing the last shape
 * of a file (https://trac.osgeo.org/gdal/ticket/7031)
 *
 * Revision 1.75  2016-12-05 12:44:05  erouault
 * * Major overhaul of Makefile build system to use autoconf/automake.
 *
 * * Warning fixes in contrib/
 *
 * Revision 1.74  2016-12-04 15:30:15  erouault
 * * shpopen.c, dbfopen.c, shptree.c, shapefil.h: resync with
 * GDAL Shapefile driver. Mostly cleanups. SHPObject and DBFInfo
 * structures extended with new members. New functions:
 * DBFSetLastModifiedDate, SHPOpenLLEx, SHPRestoreSHX,
 * SHPSetFastModeReadObject
 *
 * * sbnsearch.c: new file to implement original ESRI .sbn spatial
 * index reading. (no write support). New functions:
 * SBNOpenDiskTree, SBNCloseDiskTree, SBNSearchDiskTree,
 * SBNSearchDiskTreeInteger, SBNSearchFreeIds
 *
 * * Makefile, makefile.vc, CMakeLists.txt, shapelib.def: updates
 * with new file and symbols.
 *
 * * commit: helper script to cvs commit
 *
 * Revision 1.73  2012-01-24 22:33:01  fwarmerdam
 * fix memory leak on failure to open .shp (gdal #4410)
 *
 * Revision 1.72  2011-12-11 22:45:28  fwarmerdam
 * fix failure return from SHPOpenLL.
 *
 * Revision 1.71  2011-09-15 03:33:58  fwarmerdam
 * fix missing cast (#2344)
 *
 * Revision 1.70  2011-07-24 05:59:25  fwarmerdam
 * minimize use of CPLError in favor of SAHooks.Error()
 *
 * Revision 1.69  2011-07-24 03:24:22  fwarmerdam
 * fix memory leaks in error cases creating shapefiles (#2061)
 *
 * Revision 1.68  2010-08-27 23:42:52  fwarmerdam
 * add SHPAPI_CALL attribute in code
 *
 * Revision 1.67  2010-07-01 08:15:48  fwarmerdam
 * do not error out on an object with zero vertices
 *
 * Revision 1.66  2010-07-01 07:58:57  fwarmerdam
 * minor cleanup of error handling
 *
 * Revision 1.65  2010-07-01 07:27:13  fwarmerdam
 * white space formatting adjustments
 *
 * Revision 1.64  2010-01-28 11:34:34  fwarmerdam
 * handle the shape file length limits more gracefully (#3236)
 *
 * Revision 1.63  2010-01-28 04:04:40  fwarmerdam
 * improve numerical accuracy of SHPRewind() algs (gdal #3363)
 *
 * Revision 1.62  2010-01-17 05:34:13  fwarmerdam
 * Remove asserts on x/y being null (#2148).
 *
 * Revision 1.61  2010-01-16 05:07:42  fwarmerdam
 * allow 0/nulls in shpcreateobject (#2148)
 *
 * Revision 1.60  2009-09-17 20:50:02  bram
 * on Win32, define snprintf as alias to _snprintf
 *
 * Revision 1.59  2008-03-14 05:25:31  fwarmerdam
 * Correct crash on buggy geometries (gdal #2218)
 *
 * Revision 1.58  2008/01/08 23:28:26  bram
 * on line 2095, use a float instead of a double to avoid a compiler warning
 *
 * Revision 1.57  2007/12/06 07:00:25  fwarmerdam
 * dbfopen now using SAHooks for fileio
 *
 * Revision 1.56  2007/12/04 20:37:56  fwarmerdam
 * preliminary implementation of hooks api for io and errors
 *
 * Revision 1.55  2007/11/21 22:39:56  fwarmerdam
 * close shx file in readonly mode (GDAL #1956)
 *
 * Revision 1.54  2007/11/15 00:12:47  mloskot
 * Backported recent changes from GDAL (Ticket #1415) to Shapelib.
 *
 * Revision 1.53  2007/11/14 22:31:08  fwarmerdam
 * checks after mallocs to detect for corrupted/voluntary broken shapefiles.
 * http://trac.osgeo.org/gdal/ticket/1991
 *
 * Revision 1.52  2007/06/21 15:58:33  fwarmerdam
 * fix for SHPRewindObject when rings touch at one vertex (gdal #976)
 *
 * Revision 1.51  2006/09/04 15:24:01  fwarmerdam
 * Fixed up log message for 1.49.
 *
 * Revision 1.50  2006/09/04 15:21:39  fwarmerdam
 * fix of last fix
 *
 * Revision 1.49  2006/09/04 15:21:00  fwarmerdam
 * MLoskot: Added stronger test of Shapefile reading failures, e.g. truncated
 * files.  The problem was discovered by Tim Sutton and reported here
 *   https://svn.qgis.org/trac/ticket/200
 *
 * Revision 1.48  2006/01/26 15:07:32  fwarmerdam
 * add bMeasureIsUsed flag from Craig Bruce: Bug 1249
 *
 * Revision 1.47  2006/01/04 20:07:23  fwarmerdam
 * In SHPWriteObject() make sure that the record length is updated
 * when rewriting an existing record.
 *
 * Revision 1.46  2005/02/11 17:17:46  fwarmerdam
 * added panPartStart[0] validation
 *
 * Revision 1.45  2004/09/26 20:09:48  fwarmerdam
 * const correctness changes
 *
 * Revision 1.44  2003/12/29 00:18:39  fwarmerdam
 * added error checking for failed IO and optional CPL error reporting
 *
 * Revision 1.43  2003/12/01 16:20:08  warmerda
 * be careful of zero vertex shapes
 *
 * Revision 1.42  2003/12/01 14:58:27  warmerda
 * added degenerate object check in SHPRewindObject()
 *
 * Revision 1.41  2003/07/08 15:22:43  warmerda
 * avoid warning
 *
 * Revision 1.40  2003/04/21 18:30:37  warmerda
 * added header write/update public methods
 *
 * Revision 1.39  2002/08/26 06:46:56  warmerda
 * avoid c++ comments
 *
 * Revision 1.38  2002/05/07 16:43:39  warmerda
 * Removed debugging printf()
 *
 * Revision 1.37  2002/04/10 17:35:22  warmerda
 * fixed bug in ring reversal code
 *
 * Revision 1.36  2002/04/10 16:59:54  warmerda
 * added SHPRewindObject
 *
 * Revision 1.35  2001/12/07 15:10:44  warmerda
 * fix if .shx fails to open
 *
 * Revision 1.34  2001/11/01 16:29:55  warmerda
 * move pabyRec into SHPInfo for thread safety
 *
 * Revision 1.33  2001/07/03 12:18:15  warmerda
 * Improved cleanup if SHX not found, provided by Riccardo Cohen.
 *
 * Revision 1.32  2001/06/22 01:58:07  warmerda
 * be more careful about establishing initial bounds in face of NULL shapes
 *
 * Revision 1.31  2001/05/31 19:35:29  warmerda
 * added support for writing null shapes
 *
 * Revision 1.30  2001/05/28 12:46:29  warmerda
 * Add some checking on reasonableness of record count when opening.
 *
Loading
Loading full blame...