338 int bLazySHXLoading =
FALSE;
339 int nLenWithoutExtension;
346 if( strcmp(pszAccess,
"rb+") == 0 || strcmp(pszAccess,
"r+b") == 0
347 || strcmp(pszAccess,
"r+") == 0 )
358 #if !defined(bBigEndian)
360 if( *((
uchar *) &i) == 1 )
380 memcpy(pszFullname, pszLayer, nLenWithoutExtension);
381 memcpy(pszFullname + nLenWithoutExtension,
".shp", 5);
385 memcpy(pszFullname + nLenWithoutExtension,
".SHP", 5);
391 size_t nMessageLen = strlen(pszFullname)*2+256;
393 pszFullname[nLenWithoutExtension] = 0;
394 snprintf( pszMessage, nMessageLen,
"Unable to open %s.shp or %s.SHP.",
395 pszFullname, pszFullname );
396 psHooks->
Error( pszMessage );
405 memcpy(pszFullname + nLenWithoutExtension,
".shx", 5);
409 memcpy(pszFullname + nLenWithoutExtension,
".SHX", 5);
415 size_t nMessageLen = strlen(pszFullname)*2+256;
417 pszFullname[nLenWithoutExtension] = 0;
418 snprintf( pszMessage, nMessageLen,
"Unable to open %s.shx or %s.SHX. "
419 "Set SHAPE_RESTORE_SHX config option to YES to restore or "
420 "create it.", pszFullname, pszFullname );
421 psHooks->
Error( pszMessage );
438 psSHP->
sHooks.
Error(
".shp file is unreadable, or corrupt." );
448 (pabyBuf[26]<<8)|pabyBuf[27];
460 || pabyBuf[2] != 0x27
461 || (pabyBuf[3] != 0x0a && pabyBuf[3] != 0x0d) )
463 psSHP->
sHooks.
Error(
".shx file is unreadable, or corrupt." );
472 psSHP->
nRecords = pabyBuf[27]|(pabyBuf[26]<<8)|(pabyBuf[25]<<16)|
473 ((pabyBuf[24] & 0x7F)<<24);
480 char szErrorMsg[200];
482 snprintf( szErrorMsg,
sizeof(szErrorMsg),
483 "Record count in .shp header is %d, which seems\n"
484 "unreasonable. Assuming header is corrupt.",
486 szErrorMsg[
sizeof(szErrorMsg)-1] =
'\0';
498 if( psSHP->
nRecords >= 1024 * 1024 )
503 if( nFileSize > 100 &&
515 memcpy( &dValue, pabyBuf+36, 8 );
519 memcpy( &dValue, pabyBuf+44, 8 );
523 memcpy( &dValue, pabyBuf+52, 8 );
527 memcpy( &dValue, pabyBuf+60, 8 );
531 memcpy( &dValue, pabyBuf+68, 8 );
535 memcpy( &dValue, pabyBuf+76, 8 );
539 memcpy( &dValue, pabyBuf+84, 8 );
543 memcpy( &dValue, pabyBuf+92, 8 );
558 if( bLazySHXLoading )
567 char szErrorMsg[200];
569 snprintf( szErrorMsg,
sizeof(szErrorMsg),
570 "Not enough memory to allocate requested memory (nRecords=%d).\n"
571 "Probably broken SHP file",
573 szErrorMsg[
sizeof(szErrorMsg)-1] =
'\0';
579 if (pabyBuf)
free( pabyBuf );
584 if( bLazySHXLoading )
595 char szErrorMsg[200];
597 snprintf( szErrorMsg,
sizeof(szErrorMsg),
598 "Failed to read all values for %d records in .shx file: %s.",
600 szErrorMsg[
sizeof(szErrorMsg)-1] =
'\0';
615 if (strcmp(pszAccess,
"rb") == 0)
621 for( i = 0; i < psSHP->
nRecords; i++ )
623 unsigned int nOffset, nLength;
625 memcpy( &nOffset, pabyBuf + i * 8, 4 );
628 memcpy( &nLength, pabyBuf + i * 8 + 4, 4 );
634 snprintf(
str,
sizeof(
str),
635 "Invalid offset for entity %d", i);
636 str[
sizeof(
str)-1] =
'\0';
643 if( nLength >
STATIC_CAST(
unsigned int, INT_MAX / 2 - 4) )
646 snprintf(
str,
sizeof(
str),
647 "Invalid length for entity %d", i);
648 str[
sizeof(
str)-1] =
'\0';
static void SwapWord(int length, void *wordP)
static int SHPGetLenWithoutExtension(const char *pszBasename)
void SHPAPI_CALL SHPClose(SHPHandle psSHP)
#define STATIC_CAST(type, x)
void(* Error)(const char *message)
SAFile(* FOpen)(const char *filename, const char *access)
SAOffset(* FTell)(SAFile file)
int(* FClose)(SAFile file)
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
unsigned int * panRecSize
unsigned int * panRecOffset