PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ if() [2/9]

if ( bBigEndian  )

Definition at line 1658 of file shpopen.c.

1669  {
1670  int32 nPoints, nParts;
1671  int32 i, nOffset;
1672 
1673  if ( 40 + 8 + 4 > nEntitySize )
1674  {
1675  snprintf(szErrorMsg, sizeof(szErrorMsg),
1676  "Corrupted .shp file : shape %d : nEntitySize = %d",
1677  hEntity, nEntitySize);
1678  psSHP->sHooks.Error( szErrorMsg );
1680  return NULL;
1681  }
1682 /* -------------------------------------------------------------------- */
1683 /* Get the X/Y bounds. */
1684 /* -------------------------------------------------------------------- */
1685  memcpy( &(psShape->dfXMin), psSHP->pabyRec + 8 + 4, 8 );
1686  memcpy( &(psShape->dfYMin), psSHP->pabyRec + 8 + 12, 8 );
1687  memcpy( &(psShape->dfXMax), psSHP->pabyRec + 8 + 20, 8 );
1688  memcpy( &(psShape->dfYMax), psSHP->pabyRec + 8 + 28, 8 );
1689 
1690  if( bBigEndian ) SwapWord( 8, &(psShape->dfXMin) );
1691  if( bBigEndian ) SwapWord( 8, &(psShape->dfYMin) );
1692  if( bBigEndian ) SwapWord( 8, &(psShape->dfXMax) );
1693  if( bBigEndian ) SwapWord( 8, &(psShape->dfYMax) );
1694 
1695 /* -------------------------------------------------------------------- */
1696 /* Extract part/point count, and build vertex and part arrays */
1697 /* to proper size. */
1698 /* -------------------------------------------------------------------- */
1699  memcpy( &nPoints, psSHP->pabyRec + 40 + 8, 4 );
1700  memcpy( &nParts, psSHP->pabyRec + 36 + 8, 4 );
1701 
1702  if( bBigEndian ) SwapWord( 4, &nPoints );
1703  if( bBigEndian ) SwapWord( 4, &nParts );
1704 
1705  if (nPoints > 50 * 1000 * 1000 || nParts > 10 * 1000 * 1000)
1706  {
1707  snprintf(szErrorMsg, sizeof(szErrorMsg),
1708  "Corrupted .shp file : shape %d, nPoints=%d, nParts=%d.",
1709  hEntity, nPoints, nParts);
1710  psSHP->sHooks.Error( szErrorMsg );
1712  return NULL;
1713  }
1714 
1715  /* With the previous checks on nPoints and nParts, */
1716  /* we should not overflow here and after */
1717  /* since 50 M * (16 + 8 + 8) = 1 600 MB */
1718  nRequiredSize = 44 + 8 + 4 * nParts + 16 * nPoints;
1719  if ( psShape->nSHPType == SHPT_POLYGONZ
1720  || psShape->nSHPType == SHPT_ARCZ
1721  || psShape->nSHPType == SHPT_MULTIPATCH )
1722  {
1723  nRequiredSize += 16 + 8 * nPoints;
1724  }
1725  if( psShape->nSHPType == SHPT_MULTIPATCH )
1726  {
1727  nRequiredSize += 4 * nParts;
1728  }
1729  if (nRequiredSize > nEntitySize)
1730  {
1731  snprintf(szErrorMsg, sizeof(szErrorMsg),
1732  "Corrupted .shp file : shape %d, nPoints=%d, nParts=%d, nEntitySize=%d.",
1733  hEntity, nPoints, nParts, nEntitySize);
1734  psSHP->sHooks.Error( szErrorMsg );
1736  return NULL;
1737  }
1738 
1739  psShape->nVertices = nPoints;
1740  psShape->padfX = (double *) calloc(nPoints,sizeof(double));
1741  psShape->padfY = (double *) calloc(nPoints,sizeof(double));
1742  psShape->padfZ = (double *) calloc(nPoints,sizeof(double));
1743  psShape->padfM = (double *) calloc(nPoints,sizeof(double));
1744 
1745  psShape->nParts = nParts;
1746  psShape->panPartStart = (int *) calloc(nParts,sizeof(int));
1747  psShape->panPartType = (int *) calloc(nParts,sizeof(int));
1748 
1749  if (psShape->padfX == NULL ||
1750  psShape->padfY == NULL ||
1751  psShape->padfZ == NULL ||
1752  psShape->padfM == NULL ||
1753  psShape->panPartStart == NULL ||
1754  psShape->panPartType == NULL)
1755  {
1756  snprintf(szErrorMsg, sizeof(szErrorMsg),
1757  "Not enough memory to allocate requested memory (nPoints=%d, nParts=%d) for shape %d. "
1758  "Probably broken SHP file", hEntity, nPoints, nParts );
1759  psSHP->sHooks.Error( szErrorMsg );
1761  return NULL;
1762  }
1763 
1764  for( i = 0; i < nParts; i++ )
1765  psShape->panPartType[i] = SHPP_RING;
1766 
1767 /* -------------------------------------------------------------------- */
1768 /* Copy out the part array from the record. */
1769 /* -------------------------------------------------------------------- */
1770  memcpy( psShape->panPartStart, psSHP->pabyRec + 44 + 8, 4 * nParts );
1771  for( i = 0; i < nParts; i++ )
1772  {
1773  if( bBigEndian ) SwapWord( 4, psShape->panPartStart+i );
1774 
1775  /* We check that the offset is inside the vertex array */
1776  if (psShape->panPartStart[i] < 0
1777  || (psShape->panPartStart[i] >= psShape->nVertices
1778  && psShape->nVertices > 0) )
1779  {
1780  snprintf(szErrorMsg, sizeof(szErrorMsg),
1781  "Corrupted .shp file : shape %d : panPartStart[%d] = %d, nVertices = %d",
1782  hEntity, i, psShape->panPartStart[i], psShape->nVertices);
1783  psSHP->sHooks.Error( szErrorMsg );
1785  return NULL;
1786  }
1787  if (i > 0 && psShape->panPartStart[i] <= psShape->panPartStart[i-1])
1788  {
1789  snprintf(szErrorMsg, sizeof(szErrorMsg),
1790  "Corrupted .shp file : shape %d : panPartStart[%d] = %d, panPartStart[%d] = %d",
1791  hEntity, i, psShape->panPartStart[i], i - 1, psShape->panPartStart[i - 1]);
1792  psSHP->sHooks.Error( szErrorMsg );
1794  return NULL;
1795  }
1796  }
1797 
1798  nOffset = 44 + 8 + 4*nParts;
1799 
1800 /* -------------------------------------------------------------------- */
1801 /* If this is a multipatch, we will also have parts types. */
1802 /* -------------------------------------------------------------------- */
1803  if( psShape->nSHPType == SHPT_MULTIPATCH )
1804  {
1805  memcpy( psShape->panPartType, psSHP->pabyRec + nOffset, 4*nParts );
1806  for( i = 0; i < nParts; i++ )
1807  {
1808  if( bBigEndian ) SwapWord( 4, psShape->panPartType+i );
1809  }
1810 
1811  nOffset += 4*nParts;
1812  }
1813 
1814 /* -------------------------------------------------------------------- */
1815 /* Copy out the vertices from the record. */
1816 /* -------------------------------------------------------------------- */
1817  for( i = 0; i < nPoints; i++ )
1818  {
1819  memcpy(psShape->padfX + i,
1820  psSHP->pabyRec + nOffset + i * 16,
1821  8 );
1822 
1823  memcpy(psShape->padfY + i,
1824  psSHP->pabyRec + nOffset + i * 16 + 8,
1825  8 );
1826 
1827  if( bBigEndian ) SwapWord( 8, psShape->padfX + i );
1828  if( bBigEndian ) SwapWord( 8, psShape->padfY + i );
1829  }
1830 
1831  nOffset += 16*nPoints;
1832 
1833 /* -------------------------------------------------------------------- */
1834 /* If we have a Z coordinate, collect that now. */
1835 /* -------------------------------------------------------------------- */
1836  if( psShape->nSHPType == SHPT_POLYGONZ
1837  || psShape->nSHPType == SHPT_ARCZ
1838  || psShape->nSHPType == SHPT_MULTIPATCH )
1839  {
1840  memcpy( &(psShape->dfZMin), psSHP->pabyRec + nOffset, 8 );
1841  memcpy( &(psShape->dfZMax), psSHP->pabyRec + nOffset + 8, 8 );
1842 
1843  if( bBigEndian ) SwapWord( 8, &(psShape->dfZMin) );
1844  if( bBigEndian ) SwapWord( 8, &(psShape->dfZMax) );
1845 
1846  for( i = 0; i < nPoints; i++ )
1847  {
1848  memcpy( psShape->padfZ + i,
1849  psSHP->pabyRec + nOffset + 16 + i*8, 8 );
1850  if( bBigEndian ) SwapWord( 8, psShape->padfZ + i );
1851  }
1852 
1853  nOffset += 16 + 8*nPoints;
1854  }
1855 
1856 /* -------------------------------------------------------------------- */
1857 /* If we have a M measure value, then read it now. We assume */
1858 /* that the measure can be present for any shape if the size is */
1859 /* big enough, but really it will only occur for the Z shapes */
1860 /* (options), and the M shapes. */
1861 /* -------------------------------------------------------------------- */
1862  if( nEntitySize >= nOffset + 16 + 8*nPoints )
1863  {
1864  memcpy( &(psShape->dfMMin), psSHP->pabyRec + nOffset, 8 );
1865  memcpy( &(psShape->dfMMax), psSHP->pabyRec + nOffset + 8, 8 );
1866 
1867  if( bBigEndian ) SwapWord( 8, &(psShape->dfMMin) );
1868  if( bBigEndian ) SwapWord( 8, &(psShape->dfMMax) );
1869 
1870  for( i = 0; i < nPoints; i++ )
1871  {
1872  memcpy( psShape->padfM + i,
1873  psSHP->pabyRec + nOffset + 16 + i*8, 8 );
1874  if( bBigEndian ) SwapWord( 8, psShape->padfM + i );
1875  }
1876  psShape->bMeasureIsUsed = TRUE;
1877  }
1878  }
#define SHPT_ARCZ
Definition: shapefil.h:312
#define SHPT_MULTIPATCH
Definition: shapefil.h:319
#define SHPP_RING
Definition: shapefil.h:332
#define SHPT_POLYGONZ
Definition: shapefil.h:313
static int bBigEndian
Definition: shpopen.c:293
unsigned int int32
Definition: shpopen.c:273
nEntitySize
Definition: shpopen.c:1579
static void SwapWord(int length, void *wordP)
Definition: shpopen.c:302
psShape
Definition: shpopen.c:1643
#define TRUE
Definition: shpopen.c:278
void SHPAPI_CALL SHPDestroyObject(SHPObject *psShape)
Definition: shpopen.c:2182