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 )
387 memcpy(pszFullname, pszLayer, nLenWithoutExtension);
388 memcpy(pszFullname + nLenWithoutExtension,
".shp", 5);
392 memcpy(pszFullname + nLenWithoutExtension,
".SHP", 5);
398 size_t nMessageLen = strlen(pszFullname)*2+256;
400 pszFullname[nLenWithoutExtension] = 0;
401 snprintf( pszMessage, nMessageLen,
"Unable to open %s.shp or %s.SHP.",
402 pszFullname, pszFullname );
403 psHooks->
Error( pszMessage );
412 memcpy(pszFullname + nLenWithoutExtension,
".shx", 5);
416 memcpy(pszFullname + nLenWithoutExtension,
".SHX", 5);
422 size_t nMessageLen = strlen(pszFullname)*2+256;
424 pszFullname[nLenWithoutExtension] = 0;
425 snprintf( pszMessage, nMessageLen,
"Unable to open %s.shx or %s.SHX. "
426 "Set SHAPE_RESTORE_SHX config option to YES to restore or "
427 "create it.", pszFullname, pszFullname );
428 psHooks->
Error( pszMessage );
445 psSHP->
sHooks.
Error(
".shp file is unreadable, or corrupt." );
455 (pabyBuf[26]<<8)|pabyBuf[27];
467 || pabyBuf[2] != 0x27
468 || (pabyBuf[3] != 0x0a && pabyBuf[3] != 0x0d) )
470 psSHP->
sHooks.
Error(
".shx file is unreadable, or corrupt." );
479 psSHP->
nRecords = pabyBuf[27]|(pabyBuf[26]<<8)|(pabyBuf[25]<<16)|
480 ((pabyBuf[24] & 0x7F)<<24);
487 char szErrorMsg[200];
489 snprintf( szErrorMsg,
sizeof(szErrorMsg),
490 "Record count in .shp header is %d, which seems\n"
491 "unreasonable. Assuming header is corrupt.",
493 szErrorMsg[
sizeof(szErrorMsg)-1] =
'\0';
505 if( psSHP->
nRecords >= 1024 * 1024 )
510 if( nFileSize > 100 &&
522 memcpy( &dValue, pabyBuf+36, 8 );
526 memcpy( &dValue, pabyBuf+44, 8 );
530 memcpy( &dValue, pabyBuf+52, 8 );
534 memcpy( &dValue, pabyBuf+60, 8 );
538 memcpy( &dValue, pabyBuf+68, 8 );
542 memcpy( &dValue, pabyBuf+76, 8 );
546 memcpy( &dValue, pabyBuf+84, 8 );
550 memcpy( &dValue, pabyBuf+92, 8 );
565 if( bLazySHXLoading )
574 char szErrorMsg[200];
576 snprintf( szErrorMsg,
sizeof(szErrorMsg),
577 "Not enough memory to allocate requested memory (nRecords=%d).\n"
578 "Probably broken SHP file",
580 szErrorMsg[
sizeof(szErrorMsg)-1] =
'\0';
586 if (pabyBuf)
free( pabyBuf );
591 if( bLazySHXLoading )
602 char szErrorMsg[200];
604 snprintf( szErrorMsg,
sizeof(szErrorMsg),
605 "Failed to read all values for %d records in .shx file: %s.",
607 szErrorMsg[
sizeof(szErrorMsg)-1] =
'\0';
622 if (strcmp(pszAccess,
"rb") == 0)
628 for( i = 0; i < psSHP->
nRecords; i++ )
630 unsigned int nOffset, nLength;
632 memcpy( &nOffset, pabyBuf + i * 8, 4 );
635 memcpy( &nLength, pabyBuf + i * 8 + 4, 4 );
641 snprintf(
str,
sizeof(
str),
642 "Invalid offset for entity %d", i);
643 str[
sizeof(
str)-1] =
'\0';
650 if( nLength >
STATIC_CAST(
unsigned int, INT_MAX / 2 - 4) )
653 snprintf(
str,
sizeof(
str),
654 "Invalid length for entity %d", i);
655 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