示例#1
0
int EpsilonDataset::GetNextByte()
{
    if (nFileBufOffset < nFileBufCurSize)
    {
        nFileOff ++;
        return pabyFileBuf[nFileBufOffset ++];
    }

    if (bError || bEOF)
        return -1;

    if (nFileBufCurSize + BUFFER_CHUNK > nFileBufMaxSize)
    {
        GByte* pabyFileBufNew =
            (GByte*)VSIRealloc(pabyFileBuf, nFileBufCurSize + BUFFER_CHUNK);
        if (pabyFileBufNew == NULL)
        {
            bError = TRUE;
            return -1;
        }
        pabyFileBuf = pabyFileBufNew;
        nFileBufMaxSize = nFileBufCurSize + BUFFER_CHUNK;
    }
    int nBytesRead =
        (int)VSIFReadL(pabyFileBuf + nFileBufCurSize, 1, BUFFER_CHUNK, fp);
    nFileBufCurSize += nBytesRead;
    if (nBytesRead < BUFFER_CHUNK)
        bEOF = TRUE;
    if (nBytesRead == 0)
        return -1;

    nFileOff ++;
    return pabyFileBuf[nFileBufOffset ++];
}
示例#2
0
int  OGROSMLayer::AddToArray(OGRFeature* poFeature, int bCheckFeatureThreshold)
{
    if( bCheckFeatureThreshold && nFeatureArraySize > MAX_THRESHOLD)
    {
        if( !bHasWarnedTooManyFeatures )
        {
            CPLError(CE_Failure, CPLE_AppDefined,
                    "Too many features have accumulated in %s layer. "
                    "Use OGR_INTERLEAVED_READING=YES mode",
                    GetName());
        }
        bHasWarnedTooManyFeatures = TRUE;
        return FALSE;
    }

    if (nFeatureArraySize == nFeatureArrayMaxSize)
    {
        nFeatureArrayMaxSize = nFeatureArrayMaxSize + nFeatureArrayMaxSize / 2 + 128;
        CPLDebug("OSM", "For layer %s, new max size is %d", GetName(), nFeatureArrayMaxSize);
        OGRFeature** papoNewFeatures = (OGRFeature**)VSIRealloc(papoFeatures,
                                nFeatureArrayMaxSize * sizeof(OGRFeature*));
        if (papoNewFeatures == NULL)
        {
            CPLError(CE_Failure, CPLE_AppDefined,
                     "For layer %s, cannot resize feature array to %d features",
                     GetName(), nFeatureArrayMaxSize);
            return FALSE;
        }
        papoFeatures = papoNewFeatures;
    }
    papoFeatures[nFeatureArraySize ++] = poFeature;
    
    return TRUE;
}
示例#3
0
static size_t
CPLWriteFct(void *buffer, size_t size, size_t nmemb, void *reqInfo)

{
    CPLHTTPResult *psResult = (CPLHTTPResult *) reqInfo;
    int  nNewSize;

    nNewSize = psResult->nDataLen + nmemb*size + 1;
    if( nNewSize > psResult->nDataAlloc )
    {
        psResult->nDataAlloc = (int) (nNewSize * 1.25 + 100);
        GByte* pabyNewData = (GByte *) VSIRealloc(psResult->pabyData,
                             psResult->nDataAlloc);
        if( pabyNewData == NULL )
        {
            VSIFree(psResult->pabyData);
            psResult->pabyData = NULL;
            psResult->pszErrBuf = CPLStrdup(CPLString().Printf("Out of memory allocating %d bytes for HTTP data buffer.", psResult->nDataAlloc));
            psResult->nDataAlloc = psResult->nDataLen = 0;

            return 0;
        }
        psResult->pabyData = pabyNewData;
    }

    memcpy( psResult->pabyData + psResult->nDataLen, buffer,
            nmemb * size );

    psResult->nDataLen += nmemb * size;
    psResult->pabyData[psResult->nDataLen] = 0;

    return nmemb;
}
示例#4
0
文件: cpl_conv.c 项目: cran/RArcInfo
void * CPLRealloc( void * pData, size_t nNewSize )

{
    void	*pReturn;

    if ( nNewSize == 0 )
    {
        VSIFree(pData);
        return NULL;
    }

    if( pData == NULL )
        pReturn = VSIMalloc( nNewSize );
    else
        pReturn = VSIRealloc( pData, nNewSize );
    
    if( pReturn == NULL )
    {
        CPLError( CE_Fatal, CPLE_OutOfMemory,
                  "CPLRealloc(): Out of memory allocating %d bytes.\n",
                  nNewSize );
    }

    return pReturn;
}
示例#5
0
static size_t CPLHTTPWriteFunc(void *buffer, size_t count, size_t nmemb, void *req) {
    CPLHTTPRequest *psRequest = reinterpret_cast<CPLHTTPRequest *>(req);
    size_t size = count * nmemb;

    if (size == 0) return 0;

    const size_t required_size = psRequest->nDataLen + size + 1;
    if (required_size > psRequest->nDataAlloc) {
        size_t new_size = required_size * 2;
        if (new_size < 512) new_size = 512;
        psRequest->nDataAlloc = new_size;
        GByte * pabyNewData = reinterpret_cast<GByte *>(VSIRealloc(psRequest->pabyData, new_size));
        if (pabyNewData == NULL) {
            VSIFree(psRequest->pabyData);
            psRequest->pabyData = NULL;
            psRequest->pszError = CPLStrdup(CPLString().Printf("Out of memory allocating %u bytes for HTTP data buffer.", static_cast<int>(new_size)));
            psRequest->nDataAlloc = 0;
            psRequest->nDataLen = 0;
            return 0;
        }
        psRequest->pabyData = pabyNewData;
    }
    memcpy(psRequest->pabyData + psRequest->nDataLen, buffer, size);
    psRequest->nDataLen += size;
    psRequest->pabyData[psRequest->nDataLen] = 0;
    return nmemb;
}
示例#6
0
文件: cpl_serv.c 项目: hkaiser/TRiAS
const char *CPLReadLine( FILE * fp )

{
    static char	*pszRLBuffer = NULL;
    static int	nRLBufferSize = 0;
    int		nLength, nReadSoFar = 0;

/* -------------------------------------------------------------------- */
/*      Loop reading chunks of the line till we get to the end of       */
/*      the line.                                                       */
/* -------------------------------------------------------------------- */
    do {
/* -------------------------------------------------------------------- */
/*      Grow the working buffer if we have it nearly full.  Fail out    */
/*      of read line if we can't reallocate it big enough (for          */
/*      instance for a _very large_ file with no newlines).             */
/* -------------------------------------------------------------------- */
        if( nRLBufferSize-nReadSoFar < 128 )
        {
            nRLBufferSize = nRLBufferSize*2 + 128;
            pszRLBuffer = (char *) VSIRealloc(pszRLBuffer, nRLBufferSize);
            if( pszRLBuffer == NULL )
            {
                nRLBufferSize = 0;
                return NULL;
            }
        }

/* -------------------------------------------------------------------- */
/*      Do the actual read.                                             */
/* -------------------------------------------------------------------- */
        if( VSIFGets( pszRLBuffer+nReadSoFar, nRLBufferSize-nReadSoFar, fp )
            == NULL )
            return NULL;

        nReadSoFar = strlen(pszRLBuffer);

    } while( nReadSoFar == nRLBufferSize - 1
             && pszRLBuffer[nRLBufferSize-2] != 13
             && pszRLBuffer[nRLBufferSize-2] != 10 );

/* -------------------------------------------------------------------- */
/*      Clear CR and LF off the end.                                    */
/* -------------------------------------------------------------------- */
    nLength = strlen(pszRLBuffer);
    if( nLength > 0
        && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) )
    {
        pszRLBuffer[--nLength] = '\0';
    }
    
    if( nLength > 0
        && (pszRLBuffer[nLength-1] == 10 || pszRLBuffer[nLength-1] == 13) )
    {
        pszRLBuffer[--nLength] = '\0';
    }

    return( pszRLBuffer );
}
示例#7
0
void VizGeorefSpline2D::grow_points()

{
    int new_max = _max_nof_points*2 + 2 + 3;
    int i;

    x = (double *) VSIRealloc( x, sizeof(double) * new_max );
    y = (double *) VSIRealloc( y, sizeof(double) * new_max );
    u = (double *) VSIRealloc( u, sizeof(double) * new_max );
    unused = (int *) VSIRealloc( unused, sizeof(int) * new_max );
    index = (int *) VSIRealloc( index, sizeof(int) * new_max );
    for( i = 0; i < VIZGEOREF_MAX_VARS; i++ )
    {
        rhs[i] = (double *) 
            VSIRealloc( rhs[i], sizeof(double) * new_max );
        coef[i] = (double *) 
            VSIRealloc( coef[i], sizeof(double) * new_max );
        if( _max_nof_points == 0 )
        {
            memset(rhs[i], 0, 3 * sizeof(double));
            memset(coef[i], 0, 3 * sizeof(double));
        }
    }

    _max_nof_points = new_max - 3;
}
示例#8
0
bool VSIMemFile::SetLength( vsi_l_offset nNewLength )

{
/* -------------------------------------------------------------------- */
/*      Grow underlying array if needed.                                */
/* -------------------------------------------------------------------- */
    if( nNewLength > nAllocLength )
    {
        /* If we don't own the buffer, we cannot reallocate it because */
        /* the return address might be different from the one passed by */
        /* the caller. Hence, the caller would not be able to free */
        /* the buffer... */
        if( !bOwnData )
        {
            CPLError(CE_Failure, CPLE_NotSupported,
                     "Cannot extended in-memory file whose ownership was not transferred");
            return false;
        }

        GByte *pabyNewData;
        const vsi_l_offset nNewAlloc = (nNewLength + nNewLength / 10) + 5000;
        if( (vsi_l_offset)(size_t)nNewAlloc != nNewAlloc )
            pabyNewData = NULL;
        else
            pabyNewData = (GByte *) VSIRealloc(pabyData, (size_t)nNewAlloc);
        if( pabyNewData == NULL )
        {
            CPLError(CE_Failure, CPLE_OutOfMemory,
                     "Cannot extend in-memory file to " CPL_FRMT_GUIB " bytes due to out-of-memory situation",
                     nNewAlloc);
            return false;
        }

        /* Clear the new allocated part of the buffer */
        memset(pabyNewData + nAllocLength, 0, 
               (size_t) (nNewAlloc - nAllocLength));

        pabyData = pabyNewData;
        nAllocLength = nNewAlloc;
    }

    nLength = nNewLength;
    time(&mTime);

    return true;
}
static int VSICurlStreamingHandleWriteFuncForHeader(void *buffer, size_t count, size_t nmemb, void *req)
{
    WriteFuncStruct* psStruct = (WriteFuncStruct*) req;
    size_t nSize = count * nmemb;

    char* pNewBuffer = (char*) VSIRealloc(psStruct->pBuffer,
                                          psStruct->nSize + nSize + 1);
    if (pNewBuffer)
    {
        psStruct->pBuffer = pNewBuffer;
        memcpy(psStruct->pBuffer + psStruct->nSize, buffer, nSize);
        psStruct->pBuffer[psStruct->nSize + nSize] = '\0';
        if (psStruct->bIsHTTP && psStruct->bIsInHeader)
        {
            char* pszLine = psStruct->pBuffer + psStruct->nSize;
            if (EQUALN(pszLine, "HTTP/1.0 ", 9) ||
                EQUALN(pszLine, "HTTP/1.1 ", 9))
                psStruct->nHTTPCode = atoi(pszLine + 9);

            if (pszLine[0] == '\r' || pszLine[0] == '\n')
            {
                if (psStruct->bDownloadHeaderOnly)
                {
                    /* If moved permanently/temporarily, go on. Otherwise stop now*/
                    if (!(psStruct->nHTTPCode == 301 || psStruct->nHTTPCode == 302))
                        return 0;
                }
                else
                {
                    psStruct->bIsInHeader = FALSE;
                }
            }
        }
        psStruct->nSize += nSize;
        return nmemb;
    }
    else
    {
        return 0;
    }
}
示例#10
0
OGRErr GMLHandler::dataHandlerAttribute(const char *data, int nLen)

{
    int nIter = 0;

    if( m_bInCurField )
    {
        // Ignore white space
        if (m_nCurFieldLen == 0)
        {
            while (nIter < nLen)
            {
                char ch = data[nIter];
                if( !(ch == ' ' || ch == 10 || ch == 13 || ch == '\t') )
                    break;
                nIter ++;
            }
        }

        int nCharsLen = nLen - nIter;

        if (m_nCurFieldLen + nCharsLen + 1 > m_nCurFieldAlloc)
        {
            m_nCurFieldAlloc = m_nCurFieldAlloc * 4 / 3 + nCharsLen + 1;
            char *pszNewCurField = (char *)
                VSIRealloc( m_pszCurField, m_nCurFieldAlloc );
            if (pszNewCurField == NULL)
            {
                return OGRERR_NOT_ENOUGH_MEMORY;
            }
            m_pszCurField = pszNewCurField;
        }
        memcpy( m_pszCurField + m_nCurFieldLen, data + nIter, nCharsLen);
        m_nCurFieldLen += nCharsLen;
        m_pszCurField[m_nCurFieldLen] = '\0';
    }

    return OGRERR_NONE;
}
示例#11
0
OGRErr GMLHandler::dataHandlerGeometry(const char *data, int nLen)

{
    int nIter = 0;

    // Ignore white space
    if (m_nGeomLen == 0)
    {
        while (nIter < nLen)
        {
            char ch = data[nIter];
            if( !(ch == ' ' || ch == 10 || ch == 13 || ch == '\t') )
                break;
            nIter ++;
        }
    }

    int nCharsLen = nLen - nIter;
    if (nCharsLen)
    {
        if( m_nGeomLen + nCharsLen + 1 > m_nGeomAlloc )
        {
            m_nGeomAlloc = m_nGeomAlloc * 4 / 3 + nCharsLen + 1;
            char* pszNewGeometry = (char *)
                VSIRealloc( m_pszGeometry, m_nGeomAlloc);
            if (pszNewGeometry == NULL)
            {
                return OGRERR_NOT_ENOUGH_MEMORY;
            }
            m_pszGeometry = pszNewGeometry;
        }
        memcpy( m_pszGeometry+m_nGeomLen, data + nIter, nCharsLen);
        m_nGeomLen += nCharsLen;
        m_pszGeometry[m_nGeomLen] = '\0';
    }

    return OGRERR_NONE;
}
示例#12
0
OGRErr OGRMemLayer::ISetFeature( OGRFeature *poFeature )

{
    if (!bUpdatable)
        return OGRERR_FAILURE;

    if( poFeature == NULL )
        return OGRERR_FAILURE;

    if( poFeature->GetFID() == OGRNullFID )
    {
        while( iNextCreateFID < nMaxFeatureCount 
               && papoFeatures[iNextCreateFID] != NULL )
            iNextCreateFID++;
        poFeature->SetFID( iNextCreateFID++ );
    }
    else if ( poFeature->GetFID() < OGRNullFID )
    {
        CPLError(CE_Failure, CPLE_NotSupported,
                 "negative FID are not supported");
        return OGRERR_FAILURE;
    }

    if( poFeature->GetFID() >= nMaxFeatureCount )
    {
        GIntBig nNewCount = MAX(2*nMaxFeatureCount+10, poFeature->GetFID() + 1 );
        if( (GIntBig)(size_t)(sizeof(OGRFeature *) * nNewCount) !=
                                (GIntBig)sizeof(OGRFeature *) * nNewCount )
        {
            CPLError(CE_Failure, CPLE_OutOfMemory,
                     "Cannot allocate array of " CPL_FRMT_GIB " elements", nNewCount);
            return OGRERR_FAILURE;
        }

        OGRFeature** papoNewFeatures = (OGRFeature **) 
            VSIRealloc( papoFeatures, (size_t)(sizeof(OGRFeature *) * nNewCount) );
        if (papoNewFeatures == NULL)
        {
            CPLError(CE_Failure, CPLE_OutOfMemory,
                     "Cannot allocate array of " CPL_FRMT_GIB " elements", nNewCount);
            return OGRERR_FAILURE;
        }
        papoFeatures = papoNewFeatures;
        memset( papoFeatures + nMaxFeatureCount, 0, 
                sizeof(OGRFeature *) * (size_t)(nNewCount - nMaxFeatureCount) );
        nMaxFeatureCount = nNewCount;
    }

    if( papoFeatures[poFeature->GetFID()] != NULL )
    {
        delete papoFeatures[poFeature->GetFID()];
        papoFeatures[poFeature->GetFID()] = NULL;
        nFeatureCount--;
    }

    papoFeatures[poFeature->GetFID()] = poFeature->Clone();
    int i;
    for(i = 0; i < poFeatureDefn->GetGeomFieldCount(); i ++)
    {
        OGRGeometry* poGeom = papoFeatures[poFeature->GetFID()]->GetGeomFieldRef(i);
        if( poGeom != NULL && poGeom->getSpatialReference() == NULL )
        {
            poGeom->assignSpatialReference(
                poFeatureDefn->GetGeomFieldDefn(i)->GetSpatialRef());
        }
    }
    nFeatureCount++;

    return OGRERR_NONE;
}
示例#13
0
OGRErr GMLHandler::startElement(const char *pszName, void* attr )

{
    GMLReadState *poState = m_poReader->GetState();

    int nLNLenBytes = strlen(pszName);

/* -------------------------------------------------------------------- */
/*      If we are in the midst of collecting a feature attribute        */
/*      value, then this must be a complex attribute which we don't     */
/*      try to collect for now, so just terminate the field             */
/*      collection.                                                     */
/* -------------------------------------------------------------------- */
    if( m_pszCurField != NULL )
    {
        CPLFree( m_pszCurField );
        m_pszCurField = NULL;
    }

/* -------------------------------------------------------------------- */
/*      If we are collecting geometry, or if we determine this is a     */
/*      geometry element then append to the geometry info.              */
/* -------------------------------------------------------------------- */
    if( m_pszGeometry != NULL 
        || IsGeometryElement( pszName ) )
    {
        /* should save attributes too! */

        if( m_pszGeometry == NULL )
            m_nGeometryDepth = poState->m_nPathLength;
        
        char* pszAttributes = GetAttributes(attr);

        if( m_nGeomLen + nLNLenBytes + 4 + strlen( pszAttributes ) >
            m_nGeomAlloc )
        {
            m_nGeomAlloc = (int) (m_nGeomAlloc * 1.3 + nLNLenBytes + 1000 +
                                  strlen( pszAttributes ));
            char* pszNewGeometry = (char *) 
                VSIRealloc( m_pszGeometry, m_nGeomAlloc);
            if (pszNewGeometry == NULL)
            {
                CPLFree(pszAttributes);
                return CE_Failure;
            }
            m_pszGeometry = pszNewGeometry;
        }

        strcpy( m_pszGeometry+m_nGeomLen++, "<" );
        strcpy( m_pszGeometry+m_nGeomLen, pszName );
        m_nGeomLen += nLNLenBytes;
        /* saving attributes */
        strcat( m_pszGeometry + m_nGeomLen, pszAttributes );
        m_nGeomLen += strlen( pszAttributes );
        CPLFree(pszAttributes);
        strcat( m_pszGeometry + (m_nGeomLen++), ">" );
    }
    
/* -------------------------------------------------------------------- */
/*      Is it a feature?  If so push a whole new state, and return.     */
/* -------------------------------------------------------------------- */
    else if( m_poReader->IsFeatureElement( pszName ) )
    {
        char* pszFID = GetFID(attr);

        m_poReader->PushFeature( pszName, pszFID);

        CPLFree(pszFID);

        m_nDepthFeature = m_nDepth;
        m_nDepth ++;

        return CE_None;
    }

/* -------------------------------------------------------------------- */
/*      If it is (or at least potentially is) a simple attribute,       */
/*      then start collecting it.                                       */
/* -------------------------------------------------------------------- */
    else if( m_poReader->IsAttributeElement( pszName ) )
    {
        CPLFree( m_pszCurField );
        m_pszCurField = CPLStrdup("");
    }

/* -------------------------------------------------------------------- */
/*      Push the element onto the current state's path.                 */
/* -------------------------------------------------------------------- */
    poState->PushPath( pszName );

    m_nDepth ++;

    return CE_None;
}
示例#14
0
OGRErr GMLHandler::endElement(const char* pszName )

{
    m_nDepth --;

    GMLReadState *poState = m_poReader->GetState();

    int nLNLenBytes = strlen(pszName);

/* -------------------------------------------------------------------- */
/*      Is this closing off an attribute value?  We assume so if        */
/*      we are collecting an attribute value and got to this point.     */
/*      We don't bother validating that the closing tag matches the     */
/*      opening tag.                                                    */
/* -------------------------------------------------------------------- */
    if( m_pszCurField != NULL )
    {
        CPLAssert( poState->m_poFeature != NULL );
        
        m_poReader->SetFeatureProperty( pszName, m_pszCurField );
        CPLFree( m_pszCurField );
        m_pszCurField = NULL;
    }

/* -------------------------------------------------------------------- */
/*      If we are collecting Geometry than store it, and consider if    */
/*      this is the end of the geometry.                                */
/* -------------------------------------------------------------------- */
    if( m_pszGeometry != NULL )
    {
        /* should save attributes too! */

        if( m_nGeomLen + nLNLenBytes + 4 > m_nGeomAlloc )
        {
            m_nGeomAlloc = (int) (m_nGeomAlloc * 1.3 + nLNLenBytes + 1000);
            char* pszNewGeometry = (char *) 
                VSIRealloc( m_pszGeometry, m_nGeomAlloc);
            if (pszNewGeometry == NULL)
            {
                return CE_Failure;
            }
            m_pszGeometry = pszNewGeometry;
        }

        strcat( m_pszGeometry+m_nGeomLen, "</" );
        strcpy( m_pszGeometry+m_nGeomLen+2, pszName );
        strcat( m_pszGeometry+m_nGeomLen+nLNLenBytes+2, ">" );
        m_nGeomLen += nLNLenBytes + 3;

        if( poState->m_nPathLength == m_nGeometryDepth+1 )
        {
            if( poState->m_poFeature != NULL )
                poState->m_poFeature->SetGeometryDirectly( m_pszGeometry );
            else
                CPLFree( m_pszGeometry );

            m_pszGeometry = NULL;
            m_nGeomAlloc = m_nGeomLen = 0;
        }
    }

/* -------------------------------------------------------------------- */
/*      If we are collecting a feature, and this element tag matches    */
/*      element name for the class, then we have finished the           */
/*      feature, and we pop the feature read state.                     */
/* -------------------------------------------------------------------- */
    if( m_nDepth == m_nDepthFeature && poState->m_poFeature != NULL
        && strcmp(pszName,
                 poState->m_poFeature->GetClass()->GetElementName()) == 0 )
    {
        m_poReader->PopState();
    }

/* -------------------------------------------------------------------- */
/*      Otherwise, we just pop the element off the local read states    */
/*      element stack.                                                  */
/* -------------------------------------------------------------------- */
    else
    {
        if( strcmp(pszName,poState->GetLastComponent()) == 0 )
            poState->PopPath();
        else
        {
            CPLAssert( FALSE );
        }
    }

    return CE_None;
}
示例#15
0
void*
_TIFFrealloc(tdata_t p, tsize_t s)
{
    return VSIRealloc( p, s );
}
示例#16
0
int VSIIngestFile( VSILFILE* fp,
                   const char* pszFilename,
                   GByte** ppabyRet,
                   vsi_l_offset* pnSize,
                   GIntBig nMaxSize)
{
    vsi_l_offset nDataLen = 0;
    int bFreeFP = FALSE;

    if( fp == NULL && pszFilename == NULL )
        return FALSE;
    if( ppabyRet == NULL )
        return FALSE;

    *ppabyRet = NULL;
    if( pnSize != NULL )
        *pnSize = 0;

    if( NULL == fp )
    {
        fp = VSIFOpenL( pszFilename, "rb" );
        if( NULL == fp )
        {
            CPLError( CE_Failure, CPLE_FileIO,
                      "Cannot open file '%s'", pszFilename );
            return FALSE;
        }
        bFreeFP = TRUE;
    }
    else
        VSIFSeekL(fp, 0, SEEK_SET);

    if( pszFilename == NULL ||
        strcmp(pszFilename, "/vsistdin/") == 0 )
    {
        vsi_l_offset nDataAlloc = 0;
        VSIFSeekL( fp, 0, SEEK_SET );
        while(TRUE)
        {
            if( nDataLen + 8192 + 1 > nDataAlloc )
            {
                nDataAlloc = (nDataAlloc * 4) / 3 + 8192 + 1;
                if( nDataAlloc > (vsi_l_offset)(size_t)nDataAlloc )
                {
                    CPLError( CE_Failure, CPLE_AppDefined,
                              "Input file too large to be opened" );
                    VSIFree( *ppabyRet );
                    *ppabyRet = NULL;
                    if( bFreeFP )
                        VSIFCloseL( fp );
                    return FALSE;
                }
                GByte* pabyNew = (GByte*)VSIRealloc(*ppabyRet, (size_t)nDataAlloc);
                if( pabyNew == NULL )
                {
                    CPLError( CE_Failure, CPLE_OutOfMemory,
                              "Cannot allocated " CPL_FRMT_GIB " bytes",
                              nDataAlloc );
                    VSIFree( *ppabyRet );
                    *ppabyRet = NULL;
                    if( bFreeFP )
                        VSIFCloseL( fp );
                    return FALSE;
                }
                *ppabyRet = pabyNew;
            }
            int nRead = (int)VSIFReadL( *ppabyRet + nDataLen, 1, 8192, fp );
            nDataLen += nRead;

            if ( nMaxSize >= 0 && nDataLen > (vsi_l_offset)nMaxSize )
            {
                CPLError( CE_Failure, CPLE_AppDefined,
                              "Input file too large to be opened" );
                VSIFree( *ppabyRet );
                *ppabyRet = NULL;
                if( pnSize != NULL )
                    *pnSize = 0;
                if( bFreeFP )
                    VSIFCloseL( fp );
                return FALSE;
            }

            if( pnSize != NULL )
                *pnSize += nRead;
            (*ppabyRet)[nDataLen] = '\0';
            if( nRead == 0 )
                break;
        }
    }
    else
    {
        VSIFSeekL( fp, 0, SEEK_END );
        nDataLen = VSIFTellL( fp );

        // With "large" VSI I/O API we can read data chunks larger than VSIMalloc
        // could allocate. Catch it here.
        if ( nDataLen > (vsi_l_offset)(size_t)nDataLen ||
             (nMaxSize >= 0 && nDataLen > (vsi_l_offset)nMaxSize) )
        {
            CPLError( CE_Failure, CPLE_AppDefined,
                      "Input file too large to be opened" );
            if( bFreeFP )
                VSIFCloseL( fp );
            return FALSE;
        }

        VSIFSeekL( fp, 0, SEEK_SET );

        *ppabyRet = (GByte*)VSIMalloc((size_t)(nDataLen + 1));
        if( NULL == *ppabyRet )
        {
            CPLError( CE_Failure, CPLE_OutOfMemory,
                      "Cannot allocated " CPL_FRMT_GIB " bytes",
                      nDataLen + 1 );
            if( bFreeFP )
                VSIFCloseL( fp );
            return FALSE;
        }

        (*ppabyRet)[nDataLen] = '\0';
        if( ( nDataLen != VSIFReadL( *ppabyRet, 1, (size_t)nDataLen, fp ) ) )
        {
            CPLError( CE_Failure, CPLE_FileIO,
                      "Cannot read " CPL_FRMT_GIB " bytes",
                      nDataLen );
            VSIFree( *ppabyRet );
            *ppabyRet = NULL;
            if( bFreeFP )
                VSIFCloseL( fp );
            return FALSE;
        }
        if( pnSize != NULL )
            *pnSize = nDataLen;
    }
    if( bFreeFP )
        VSIFCloseL( fp );
    return TRUE;
}
示例#17
0
NTFRecord::NTFRecord( FILE * fp )

{
    nType = 99;
    nLength = 0;
    pszData = NULL;

    if( fp == NULL )
        return;

/* ==================================================================== */
/*      Read lines untill we get to one without a continuation mark.    */
/* ==================================================================== */
    char      szLine[MAX_RECORD_LEN+3];
    int       nNewLength;

    do { 
        nNewLength = ReadPhysicalLine( fp, szLine );
        if( nNewLength == -1 || nNewLength == -2 )
            break;

        while( nNewLength > 0 && szLine[nNewLength-1] == ' ' )
               szLine[--nNewLength] = '\0';

        if( nNewLength < 2 || szLine[nNewLength-1] != '%' )
        {
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "Corrupt NTF record, missing end '%%'." );
            CPLFree( pszData );
            pszData = NULL;
            break;
        }

        if( pszData == NULL )
        {
            nLength = nNewLength - 2;
            pszData = (char *) VSIMalloc(nLength+1);
            if (pszData == NULL)
            {
                CPLError( CE_Failure, CPLE_OutOfMemory, "Out of memory");
                return;
            }
            memcpy( pszData, szLine, nLength );
            pszData[nLength] = '\0';
        }
        else
        {
            if( !EQUALN(szLine,"00",2) || nNewLength < 4 )
            {
                CPLError( CE_Failure, CPLE_AppDefined, "Invalid line");
                VSIFree(pszData);
                pszData = NULL;
                return;
            }

            char* pszNewData = (char *) VSIRealloc(pszData,nLength+(nNewLength-4)+1);
            if (pszNewData == NULL)
            {
                CPLError( CE_Failure, CPLE_OutOfMemory, "Out of memory");
                VSIFree(pszData);
                pszData = NULL;
                return;
            }

            pszData = pszNewData;
            memcpy( pszData+nLength, szLine+2, nNewLength-4 );
            nLength += nNewLength-4;
            pszData[nLength] = '\0';
        }
    } while( szLine[nNewLength-2] == '1' );

/* -------------------------------------------------------------------- */
/*      Figure out the record type.                                     */
/* -------------------------------------------------------------------- */
    if( pszData != NULL )
    {
        char      szType[3];

        strncpy( szType, pszData, 2 );
        szType[2] = '\0';

        nType = atoi(szType);
    }
}
示例#18
0
OGRErr GMLHandler::startElement(const char *pszName, void* attr )

{
    GMLReadState *poState = m_poReader->GetState();

    if (m_bIgnoreFeature && m_nDepth >= m_nDepthFeature)
    {
        m_nDepth ++;
        return CE_None;
    }

    if ( m_nDepth == 0 )
    {
        if (strcmp(pszName, "CityModel") == 0 )
        {
            m_bIsCityGML = TRUE;
        }
        else if (strcmp(pszName, "AIXMBasicMessage") == 0)
        {
            m_bIsAIXM = m_bReportHref = TRUE;
        }
    }
    else if ( m_nDepth == 2 && m_bInBoundedBy )
    {
        if ( strcmp(pszName, "Envelope") == 0 )
        {
            char* pszGlobalSRSName = GetAttributeValue(attr, "srsName");
            if (pszGlobalSRSName != NULL &&
                strncmp(pszGlobalSRSName, "EPSG:", 5) == 0 &&
                CSLTestBoolean(CPLGetConfigOption("GML_CONSIDER_EPSG_AS_URN", "NO")))
            {
                char* pszNew = CPLStrdup(CPLSPrintf("urn:ogc:def:crs:EPSG::%s", pszGlobalSRSName+5));
                CPLFree(pszGlobalSRSName);
                pszGlobalSRSName = pszNew;
            }
            m_poReader->SetGlobalSRSName(pszGlobalSRSName);
            CPLFree(pszGlobalSRSName);
        }
    }

/* -------------------------------------------------------------------- */
/*      If we are in the midst of collecting a feature attribute        */
/*      value, then this must be a complex attribute which we don't     */
/*      try to collect for now, so just terminate the field             */
/*      collection.                                                     */
/* -------------------------------------------------------------------- */
    if( m_pszCurField != NULL )
    {
        CPLFree( m_pszCurField );
        m_pszCurField = NULL;
    }

    if ( m_bInCityGMLGenericAttr )
    {
        if( strcmp(pszName, "value") == 0 )
        {
            CPLFree( m_pszCurField );
            m_pszCurField = CPLStrdup("");
        }
    }

/* -------------------------------------------------------------------- */
/*      If we are collecting geometry, or if we determine this is a     */
/*      geometry element then append to the geometry info.              */
/* -------------------------------------------------------------------- */
    else if( poState->m_poFeature != NULL &&
             (m_pszGeometry != NULL
              || IsGeometryElement( pszName )
              || (m_bIsAIXM && strcmp( pszName, "ElevatedPoint") == 0)) )
    {
        int bReadGeometry;

        if( m_pszGeometry == NULL )
        {
            /* If the <GeometryElementPath> is defined in the .gfs, use it */
            /* to read the appropriate geometry element */
            const char* pszGeometryElement = (poState->m_poFeature) ?
                    poState->m_poFeature->GetClass()->GetGeometryElement() : NULL;
            if (pszGeometryElement != NULL)
                bReadGeometry = strcmp(poState->m_pszPath, pszGeometryElement) == 0;
            else
            {
                /* AIXM special case: for RouteSegment, we only want to read Curve geometries */
                /* not 'start' and 'end' geometries */
                if (m_bIsAIXM &&
                    strcmp(poState->m_poFeature->GetClass()->GetName(), "RouteSegment") == 0)
                    bReadGeometry = strcmp( pszName, "Curve") == 0;
                else
                    bReadGeometry = TRUE;
            }
            CPLAssert(m_nGeometryDepth == 0);
            m_nGeometryDepth = m_nDepth;
        }
        else
            bReadGeometry = TRUE;

        if (bReadGeometry)
        {
            char* pszAttributes = GetAttributes(attr);
            size_t nLNLenBytes = strlen(pszName);

            /* Some CityGML lack a srsDimension="3" in posList, such as in */
            /* http://www.citygml.org/fileadmin/count.php?f=fileadmin%2Fcitygml%2Fdocs%2FFrankfurt_Street_Setting_LOD3.zip */
            /* So we have to add it manually */
            if (m_bIsCityGML && strcmp(pszName, "posList") == 0 &&
                strstr(pszAttributes, "srsDimension") == NULL)
            {
                CPLFree(pszAttributes);
                pszAttributes = CPLStrdup(" srsDimension=\"3\"");
            }

            if( m_nGeomLen + nLNLenBytes + 4 + strlen( pszAttributes ) >
                m_nGeomAlloc )
            {
                m_nGeomAlloc = (size_t) (m_nGeomAlloc * 1.3 + nLNLenBytes + 1000 +
                                    strlen( pszAttributes ));
                char* pszNewGeometry = (char *)
                    VSIRealloc( m_pszGeometry, m_nGeomAlloc);
                if (pszNewGeometry == NULL)
                {
                    CPLFree(pszAttributes);
                    return CE_Failure;
                }
                m_pszGeometry = pszNewGeometry;
            }

            strcpy( m_pszGeometry+m_nGeomLen++, "<" );
            strcpy( m_pszGeometry+m_nGeomLen, pszName );
            m_nGeomLen += nLNLenBytes;
            /* saving attributes */
            strcat( m_pszGeometry + m_nGeomLen, pszAttributes );
            m_nGeomLen += strlen( pszAttributes );
            CPLFree(pszAttributes);
            strcat( m_pszGeometry + (m_nGeomLen++), ">" );
        }
    }

    else if (m_nGeometryDepth != 0 && m_nDepth > m_nGeometryDepth)
    {
        ;
    }

    else if( m_bInBoundedBy)
    {
        ;
    }

/* -------------------------------------------------------------------- */
/*      Is it a feature?  If so push a whole new state, and return.     */
/* -------------------------------------------------------------------- */
    else if( m_nDepthFeature == 0 &&
             m_poReader->IsFeatureElement( pszName ) )
    {
        const char* pszFilteredClassName = m_poReader->GetFilteredClassName();
        if ( pszFilteredClassName != NULL &&
             strcmp(pszName, pszFilteredClassName) != 0 )
        {
            m_bIgnoreFeature = TRUE;
            m_nDepthFeature = m_nDepth;
            m_nDepth ++;

            return CE_None;
        }

        m_bIgnoreFeature = FALSE;

        char* pszFID = GetFID(attr);

        m_poReader->PushFeature( pszName, pszFID);

        CPLFree(pszFID);

        m_nDepthFeature = m_nDepth;
        m_nDepth ++;

        return CE_None;
    }

    else if( strcmp(pszName, "boundedBy") == 0 )
    {
        m_bInBoundedBy = TRUE;
        m_inBoundedByDepth = m_nDepth;
    }

/* -------------------------------------------------------------------- */
/*      Is it a CityGML generic attribute ?                             */
/* -------------------------------------------------------------------- */
    else if( m_poReader->IsCityGMLGenericAttributeElement( pszName, attr ) )
    {
        m_bInCityGMLGenericAttr = TRUE;
        CPLFree(m_pszCityGMLGenericAttrName);
        m_pszCityGMLGenericAttrName = GetAttributeValue(attr, "name");
        m_inCityGMLGenericAttrDepth = m_nDepth;
    }

/* -------------------------------------------------------------------- */
/*      If it is (or at least potentially is) a simple attribute,       */
/*      then start collecting it.                                       */
/* -------------------------------------------------------------------- */
    else if( m_poReader->IsAttributeElement( pszName ) )
    {
        CPLFree( m_pszCurField );
        m_pszCurField = CPLStrdup("");
        if (m_bReportHref)
        {
            CPLFree(m_pszHref);
            m_pszHref = GetAttributeValue(attr, "xlink:href");
        }
        CPLFree(m_pszUom);
        m_pszUom = GetAttributeValue(attr, "uom");
        CPLFree(m_pszValue);
        m_pszValue = GetAttributeValue(attr, "value");
    }
    else if( m_bReportHref && m_poReader->IsAttributeElement( CPLSPrintf("%s_href", pszName ) ) )
    {
        CPLFree( m_pszCurField );
        m_pszCurField = CPLStrdup("");
        CPLFree(m_pszHref);
        m_pszHref = GetAttributeValue(attr, "xlink:href");
    }

/* -------------------------------------------------------------------- */
/*      Push the element onto the current state's path.                 */
/* -------------------------------------------------------------------- */
    poState->PushPath( pszName );

    m_nDepth ++;

    return CE_None;
}
示例#19
0
NITFDES *NITFDESAccess( NITFFile *psFile, int iSegment )

{
    NITFDES   *psDES;
    char      *pachHeader;
    NITFSegmentInfo *psSegInfo;
    char       szDESID[26];
    int        nOffset;
    int        bHasDESOFLW;
    int        nDESSHL;

/* -------------------------------------------------------------------- */
/*      Verify segment, and return existing DES accessor if there       */
/*      is one.                                                         */
/* -------------------------------------------------------------------- */
    if( iSegment < 0 || iSegment >= psFile->nSegmentCount )
        return NULL;

    psSegInfo = psFile->pasSegmentInfo + iSegment;

    if( !EQUAL(psSegInfo->szSegmentType,"DE") )
        return NULL;

    if( psSegInfo->hAccess != NULL )
        return (NITFDES *) psSegInfo->hAccess;

/* -------------------------------------------------------------------- */
/*      Read the DES subheader.                                         */
/* -------------------------------------------------------------------- */
    if (psSegInfo->nSegmentHeaderSize < 200)
    {
        CPLError(CE_Failure, CPLE_AppDefined,
                    "DES header too small");
        return NULL;
    }

    pachHeader = (char*) VSI_MALLOC_VERBOSE(psSegInfo->nSegmentHeaderSize);
    if (pachHeader == NULL)
    {
        return NULL;
    }

retry:
    if( VSIFSeekL( psFile->fp, psSegInfo->nSegmentHeaderStart,
                  SEEK_SET ) != 0
        || VSIFReadL( pachHeader, 1, psSegInfo->nSegmentHeaderSize,
                     psFile->fp ) != psSegInfo->nSegmentHeaderSize )
    {
        CPLError( CE_Failure, CPLE_FileIO,
                  "Failed to read %u byte DES subheader from " CPL_FRMT_GUIB ".",
                  psSegInfo->nSegmentHeaderSize,
                  psSegInfo->nSegmentHeaderStart );
        CPLFree(pachHeader);
        return NULL;
    }

    if (!STARTS_WITH_CI(pachHeader, "DE"))
    {
        if (STARTS_WITH_CI(pachHeader + 4, "DERegistered"))
        {
            /* BAO_46_Ed1/rpf/conc/concz10/000fz010.ona and cie are buggy */
            CPLDebug("NITF", "Patching nSegmentHeaderStart and nSegmentStart for DE segment %d", iSegment);
            psSegInfo->nSegmentHeaderStart += 4;
            psSegInfo->nSegmentStart += 4;
            goto retry;
        }

        CPLError(CE_Failure, CPLE_AppDefined,
                 "Invalid segment prefix for DE segment %d", iSegment);

        CPLFree(pachHeader);
        return NULL;
    }

/* -------------------------------------------------------------------- */
/*      Initialize DES object.                                          */
/* -------------------------------------------------------------------- */
    psDES = (NITFDES *) CPLCalloc(sizeof(NITFDES),1);

    psDES->psFile = psFile;
    psDES->iSegment = iSegment;
    psDES->pachHeader = pachHeader;

    psSegInfo->hAccess = psDES;

/* -------------------------------------------------------------------- */
/*      Collect a variety of information as metadata.                   */
/* -------------------------------------------------------------------- */
#define GetMD( length, name )              \
    do { NITFExtractMetadata( &(psDES->papszMetadata), pachHeader,    \
                         nOffset, length,                        \
                         "NITF_" #name ); \
    nOffset += length; } while(0)

    nOffset = 2;
    GetMD( 25, DESID  );
    GetMD(  2, DESVER );
    GetMD(  1, DECLAS );
    GetMD(  2, DESCLSY );
    GetMD( 11, DESCODE );
    GetMD(  2, DESCTLH );
    GetMD( 20, DESREL  );
    GetMD(  2, DESDCTP );
    GetMD(  8, DESDCDT );
    GetMD(  4, DESDCXM );
    GetMD(  1, DESDG   );
    GetMD(  8, DESDGDT );
    GetMD( 43, DESCLTX );
    GetMD(  1, DESCATP );
    GetMD( 40, DESCAUT );
    GetMD(  1, DESCRSN );
    GetMD(  8, DESSRDT );
    GetMD( 15, DESCTLN );

    /* Load DESID */
    NITFGetField( szDESID, pachHeader, 2, 25);

    /* For NITF < 02.10, we cannot rely on DESID=TRE_OVERFLOW to detect */
    /* if DESOFLW and DESITEM are present. So if the next 4 bytes are non */
    /* numeric, we'll assume that DESOFLW is there */
    bHasDESOFLW = STARTS_WITH_CI(szDESID, "TRE_OVERFLOW") ||
       (!((pachHeader[nOffset+0] >= '0' && pachHeader[nOffset+0] <= '9') &&
          (pachHeader[nOffset+1] >= '0' && pachHeader[nOffset+1] <= '9') &&
          (pachHeader[nOffset+2] >= '0' && pachHeader[nOffset+2] <= '9') &&
          (pachHeader[nOffset+3] >= '0' && pachHeader[nOffset+3] <= '9')));

    if (bHasDESOFLW)
    {
        if ((int)psSegInfo->nSegmentHeaderSize < nOffset + 6 + 3 )
        {
            CPLError(CE_Failure, CPLE_AppDefined,
                        "DES header too small");
            NITFDESDeaccess(psDES);
            return NULL;
        }
        GetMD(  6, DESOFLW );
        GetMD(  3, DESITEM );
    }

    if ((int)psSegInfo->nSegmentHeaderSize < nOffset + 4 )
    {
        CPLError(CE_Failure, CPLE_AppDefined,
                    "DES header too small");
        NITFDESDeaccess(psDES);
        return NULL;
    }

    GetMD( 4, DESSHL );
    nDESSHL = atoi(CSLFetchNameValue( psDES->papszMetadata, "NITF_DESSHL" ) );

    if (nDESSHL < 0)
    {
        CPLError(CE_Failure, CPLE_AppDefined,
                    "Invalid value for DESSHL");
        NITFDESDeaccess(psDES);
        return NULL;
    }
    if ( (int)psSegInfo->nSegmentHeaderSize < nOffset + nDESSHL)
    {
        CPLError(CE_Failure, CPLE_AppDefined,
                    "DES header too small");
        NITFDESDeaccess(psDES);
        return NULL;
    }

    if (STARTS_WITH_CI(szDESID, "CSSHPA DES"))
    {
        if ( nDESSHL != 62 && nDESSHL != 80)
        {
            CPLError(CE_Failure, CPLE_AppDefined,
                     "Invalid DESSHL for CSSHPA DES");
            NITFDESDeaccess(psDES);
            return NULL;
        }

        GetMD( 25, SHAPE_USE );
        GetMD( 10, SHAPE_CLASS );
        if (nDESSHL == 80)
            GetMD( 18, CC_SOURCE );
        GetMD(  3, SHAPE1_NAME );
        GetMD(  6, SHAPE1_START );
        GetMD(  3, SHAPE2_NAME );
        GetMD(  6, SHAPE2_START );
        GetMD(  3, SHAPE3_NAME );
        GetMD(  6, SHAPE3_START );
    }
    else if (STARTS_WITH_CI(szDESID, "XML_DATA_CONTENT"))
    {
        /* TODO : handle nDESSHL = 0005 and 0283 */
        if (nDESSHL >= 5)
        {
            GetMD( 5, DESCRC );
            if (nDESSHL >= 283)
            {
                GetMD( 8, DESSHFT );
                GetMD( 20, DESSHDT );
                GetMD( 40, DESSHRP );
                GetMD( 60, DESSHSI );
                GetMD( 10, DESSHSV );
                GetMD( 20, DESSHSD );
                GetMD( 120, DESSHTN );
                if (nDESSHL >= 773)
                {
                    GetMD( 125, DESSHLPG );
                    GetMD( 25, DESSHLPT );
                    GetMD( 20, DESSHLI );
                    GetMD( 120, DESSHLIN );
                    GetMD( 200, DESSHABS );
                }
            }
        }
    }
    else if (STARTS_WITH_CI(szDESID, "CSATTA DES") && nDESSHL == 52)
    {
        GetMD( 12, ATT_TYPE );
        GetMD( 14, DT_ATT );
        GetMD( 8, DATE_ATT );
        GetMD( 13, T0_ATT );
        GetMD( 5, NUM_ATT );
    }
    else if (nDESSHL > 0)
        GetMD(  nDESSHL, DESSHF );

    if ((int)psSegInfo->nSegmentHeaderSize > nOffset)
    {
        char* pszEscapedDESDATA =
                CPLEscapeString( pachHeader + nOffset,
                                 (int)psSegInfo->nSegmentHeaderSize - nOffset,
                                 CPLES_BackslashQuotable );
        psDES->papszMetadata = CSLSetNameValue( psDES->papszMetadata,
                                                "NITF_DESDATA",
                                                pszEscapedDESDATA );
        CPLFree(pszEscapedDESDATA);
    }
    else
    {

#define TEN_MEGABYTES 10485760

        if (psSegInfo->nSegmentSize > TEN_MEGABYTES)
        {
            const char* pszOffset = CPLSPrintf(CPL_FRMT_GUIB, psFile->pasSegmentInfo[iSegment].nSegmentStart);
            const char* pszSize = CPLSPrintf(CPL_FRMT_GUIB, psFile->pasSegmentInfo[iSegment].nSegmentSize);

            psDES->papszMetadata = CSLSetNameValue( psDES->papszMetadata,
                                                    "NITF_DESDATA_OFFSET",
                                                    pszOffset );
            psDES->papszMetadata = CSLSetNameValue( psDES->papszMetadata,
                                                    "NITF_DESDATA_LENGTH",
                                                    pszSize);
        }
        else
        {
            char* pachData = (char*)VSI_MALLOC_VERBOSE((size_t)psSegInfo->nSegmentSize);
            if (pachData == NULL )
            {
                /* nothing */
            }
            else if( VSIFSeekL( psFile->fp, psSegInfo->nSegmentStart,
                        SEEK_SET ) != 0
                || VSIFReadL( pachData, 1, (size_t)psSegInfo->nSegmentSize,
                            psFile->fp ) != psSegInfo->nSegmentSize )
            {
                CPLDebug("NITF",
                        "Failed to read " CPL_FRMT_GUIB" bytes DES data from " CPL_FRMT_GUIB ".",
                        psSegInfo->nSegmentSize,
                        psSegInfo->nSegmentStart );
            }
            else
            {
                char* pszEscapedDESDATA =
                        CPLEscapeString( pachData,
                                        (int)psSegInfo->nSegmentSize,
                                        CPLES_BackslashQuotable );
                psDES->papszMetadata = CSLSetNameValue( psDES->papszMetadata,
                                                        "NITF_DESDATA",
                                                        pszEscapedDESDATA );
                CPLFree(pszEscapedDESDATA);
            }
            CPLFree(pachData);
        }

#ifdef notdef
        /* Disabled because might generate a huge amount of elements */
        if (STARTS_WITH_CI(szDESID, "CSATTA DES"))
        {
            int nNumAtt = atoi(CSLFetchNameValueDef(psDES->papszMetadata, "NITF_NUM_ATT", "0"));
            if (nNumAtt * 8 * 4 == psSegInfo->nSegmentSize)
            {
                int nMDSize = CSLCount(psDES->papszMetadata);
                char** papszMD = (char**)VSIRealloc(psDES->papszMetadata, (nMDSize + nNumAtt * 4 + 1) * sizeof(char*));
                if (papszMD)
                {
                    int i, j;
                    const GByte* pachDataIter = pachData;

                    psDES->papszMetadata = papszMD;
                    for(i=0;i<nNumAtt;i++)
                    {
                        char szAttrNameValue[64+1+256+1];
                        double dfVal;
                        for(j=0;j<4;j++)
                        {
                            memcpy(&dfVal, pachDataIter, 8);
                            CPL_MSBPTR64(&dfVal);
                            pachDataIter += 8;
                            CPLsprintf(szAttrNameValue, "NITF_ATT_Q%d_%d=%.16g", j+1, i, dfVal);
                            papszMD[nMDSize + i * 4 + j] = CPLStrdup(szAttrNameValue);
                        }
                    }
                    papszMD[nMDSize + nNumAtt * 4] = NULL;
                }
            }
        }
#endif

    }

    return psDES;
}
示例#20
0
OGRErr GMLHandler::endElement(const char* pszName )

{
    m_nDepth --;

    if (m_bIgnoreFeature && m_nDepth >= m_nDepthFeature)
    {
        if (m_nDepth == m_nDepthFeature)
        {
             m_bIgnoreFeature = FALSE;
             m_nDepthFeature = 0;
        }
        return CE_None;
    }

    GMLReadState *poState = m_poReader->GetState();

    if( m_bInBoundedBy && strcmp(pszName, "boundedBy") == 0 &&
        m_inBoundedByDepth == m_nDepth)
    {
        m_bInBoundedBy = FALSE;
    }

    else if( m_bInCityGMLGenericAttr )
    {
        if( m_pszCityGMLGenericAttrName != NULL && m_pszCurField != NULL )
        {
            CPLAssert( poState->m_poFeature != NULL );

            m_poReader->SetFeatureProperty( m_pszCityGMLGenericAttrName, m_pszCurField );
            CPLFree( m_pszCurField );
            m_pszCurField = NULL;
            CPLFree(m_pszCityGMLGenericAttrName);
            m_pszCityGMLGenericAttrName = NULL;
        }

        if( m_inCityGMLGenericAttrDepth == m_nDepth )
        {
            m_bInCityGMLGenericAttr = FALSE;
        }
    }

/* -------------------------------------------------------------------- */
/*      Is this closing off an attribute value?  We assume so if        */
/*      we are collecting an attribute value and got to this point.     */
/*      We don't bother validating that the closing tag matches the     */
/*      opening tag.                                                    */
/* -------------------------------------------------------------------- */
    else if( m_pszCurField != NULL )
    {
        CPLAssert( poState->m_poFeature != NULL );

        if ( m_pszHref != NULL && (m_pszCurField == NULL || EQUAL(m_pszCurField, "")))
        {
            CPLString osPropNameHref = CPLSPrintf("%s_href", poState->m_pszPath);
            m_poReader->SetFeatureProperty( osPropNameHref, m_pszHref );
        }
        else
        {
            if (EQUAL(m_pszCurField, "") && m_pszValue != NULL)
                m_poReader->SetFeatureProperty( poState->m_pszPath, m_pszValue );
            else
                m_poReader->SetFeatureProperty( poState->m_pszPath, m_pszCurField );

            if (m_pszHref != NULL)
            {
                CPLString osPropNameHref = CPLSPrintf("%s_href", poState->m_pszPath);
                m_poReader->SetFeatureProperty( osPropNameHref, m_pszHref );
            }
        }
        if (m_pszUom != NULL)
        {
            CPLString osPropNameUom = CPLSPrintf("%s_uom", poState->m_pszPath);
            m_poReader->SetFeatureProperty( osPropNameUom, m_pszUom );
        }

        CPLFree( m_pszCurField );
        m_pszCurField = NULL;
        CPLFree( m_pszHref );
        m_pszHref = NULL;
        CPLFree( m_pszUom );
        m_pszUom = NULL;
        CPLFree( m_pszValue );
        m_pszValue = NULL;
    }

/* -------------------------------------------------------------------- */
/*      If we are collecting Geometry than store it, and consider if    */
/*      this is the end of the geometry.                                */
/* -------------------------------------------------------------------- */
    if( m_pszGeometry != NULL )
    {
        /* should save attributes too! */

        size_t nLNLenBytes = strlen(pszName);

        if( m_nGeomLen + nLNLenBytes + 4 > m_nGeomAlloc )
        {
            m_nGeomAlloc = (size_t) (m_nGeomAlloc * 1.3 + nLNLenBytes + 1000);
            char* pszNewGeometry = (char *)
                VSIRealloc( m_pszGeometry, m_nGeomAlloc);
            if (pszNewGeometry == NULL)
            {
                return CE_Failure;
            }
            m_pszGeometry = pszNewGeometry;
        }

        strcat( m_pszGeometry+m_nGeomLen, "</" );
        strcpy( m_pszGeometry+m_nGeomLen+2, pszName );
        strcat( m_pszGeometry+m_nGeomLen+nLNLenBytes+2, ">" );
        m_nGeomLen += nLNLenBytes + 3;

        if( m_nDepth == m_nGeometryDepth )
        {
            if( poState->m_poFeature != NULL )
            {
                /* AIXM ElevatedPoint. We want to parse this */
                /* a bit specially because ElevatedPoint is aixm: stuff and */
                /* the srsDimension of the <gml:pos> can be set to TRUE although */
                /* they are only 2 coordinates in practice */
                if ( m_bIsAIXM && strcmp(pszName, "ElevatedPoint") == 0 )
                {
                    CPLXMLNode *psGML = CPLParseXMLString( m_pszGeometry );
                    if (psGML)
                    {
                        const char* pszElevation =
                            CPLGetXMLValue( psGML, "elevation", NULL );
                        if (pszElevation)
                        {
                            m_poReader->SetFeatureProperty( "elevation",
                                                            pszElevation );
                            const char* pszElevationUnit =
                                CPLGetXMLValue( psGML, "elevation.uom", NULL );
                            if (pszElevationUnit)
                            {
                                m_poReader->SetFeatureProperty( "elevation_uom",
                                                             pszElevationUnit );
                            }
                        }

                        const char* pszGeoidUndulation =
                            CPLGetXMLValue( psGML, "geoidUndulation", NULL );
                        if (pszGeoidUndulation)
                        {
                            m_poReader->SetFeatureProperty( "geoidUndulation",
                                                            pszGeoidUndulation );
                            const char* pszGeoidUndulationUnit =
                                CPLGetXMLValue( psGML, "geoidUndulation.uom", NULL );
                            if (pszGeoidUndulationUnit)
                            {
                                m_poReader->SetFeatureProperty( "geoidUndulation_uom",
                                                             pszGeoidUndulationUnit );
                            }
                        }

                        const char* pszPos =
                                        CPLGetXMLValue( psGML, "pos", NULL );
                        const char* pszCoordinates =
                                  CPLGetXMLValue( psGML, "coordinates", NULL );
                        if (pszPos != NULL)
                        {
                            char* pszNewGeometry = CPLStrdup(CPLSPrintf(
                                "<gml:Point><gml:pos>%s</gml:pos></gml:Point>",
                                                                      pszPos));
                            CPLFree(m_pszGeometry);
                            m_pszGeometry = pszNewGeometry;
                        }
                        else if (pszCoordinates != NULL)
                        {
                            char* pszNewGeometry = CPLStrdup(CPLSPrintf(
                                "<gml:Point><gml:coordinates>%s</gml:coordinates></gml:Point>",
                                                              pszCoordinates));
                            CPLFree(m_pszGeometry);
                            m_pszGeometry = pszNewGeometry;
                        }
                        else
                        {
                            CPLFree(m_pszGeometry);
                            m_pszGeometry = NULL;
                        }
                        CPLDestroyXMLNode( psGML );
                    }
                    else
                    {
                        CPLFree(m_pszGeometry);
                        m_pszGeometry = NULL;
                    }
                }
                if (m_pszGeometry)
                {
                    if (m_poReader->FetchAllGeometries())
                        poState->m_poFeature->AddGeometry( m_pszGeometry );
                    else
                        poState->m_poFeature->SetGeometryDirectly( m_pszGeometry );
                }
            }
            else
                CPLFree( m_pszGeometry );

            m_pszGeometry = NULL;
            m_nGeomAlloc = m_nGeomLen = 0;
        }
    }

    if ( m_nGeometryDepth != 0 && m_nDepth == m_nGeometryDepth )
        m_nGeometryDepth = 0;

/* -------------------------------------------------------------------- */
/*      If we are collecting a feature, and this element tag matches    */
/*      element name for the class, then we have finished the           */
/*      feature, and we pop the feature read state.                     */
/* -------------------------------------------------------------------- */
    if( m_nDepth == m_nDepthFeature && poState->m_poFeature != NULL
        && strcmp(pszName,
                 poState->m_poFeature->GetClass()->GetElementName()) == 0 )
    {
        m_nDepthFeature = 0;
        m_poReader->PopState();
    }

/* -------------------------------------------------------------------- */
/*      Otherwise, we just pop the element off the local read states    */
/*      element stack.                                                  */
/* -------------------------------------------------------------------- */
    else
    {
        if( strcmp(pszName,poState->GetLastComponent()) == 0 )
            poState->PopPath();
        else
        {
            CPLAssert( FALSE );
        }
    }

    return CE_None;
}
int EpsilonDataset::ScanBlocks(int* pnBands)
{
    int bRet = FALSE;

    int nExpectedX = 0;
    int nExpectedY = 0;

    int nTileW = -1;
    int nTileH = -1;

    *pnBands = 0;

    bRegularTiling = TRUE;

    eps_block_header hdr;
    while(TRUE)
    {
        Seek(nStartBlockFileOff + nBlockDataSize);

        if (!GetNextBlockData())
        {
            break;
        }

        /* Ignore rasterlite wavelet header */
        int nRasterliteWaveletHeaderLen = strlen(RASTERLITE_WAVELET_HEADER);
        if (nBlockDataSize >= nRasterliteWaveletHeaderLen &&
                memcmp(pabyBlockData, RASTERLITE_WAVELET_HEADER,
                       nRasterliteWaveletHeaderLen) == 0)
        {
            continue;
        }

        /* Stop at rasterlite wavelet footer */
        int nRasterlineWaveletFooterLen = strlen(RASTERLITE_WAVELET_FOOTER);
        if (nBlockDataSize >= nRasterlineWaveletFooterLen &&
                memcmp(pabyBlockData, RASTERLITE_WAVELET_FOOTER,
                       nRasterlineWaveletFooterLen) == 0)
        {
            break;
        }

        if (eps_read_block_header (pabyBlockData,
                                   nBlockDataSize, &hdr) != EPS_OK)
        {
            CPLError(CE_Warning, CPLE_AppDefined, "cannot read block header");
            continue;
        }

        if (hdr.chk_flag == EPS_BAD_CRC ||
                hdr.crc_flag == EPS_BAD_CRC)
        {
            CPLError(CE_Warning, CPLE_AppDefined, "bad CRC");
            continue;
        }

        int W = GET_FIELD(hdr, W);
        int H = GET_FIELD(hdr, H);
        int x = GET_FIELD(hdr, x);
        int y = GET_FIELD(hdr, y);
        int w = GET_FIELD(hdr, w);
        int h = GET_FIELD(hdr, h);

        //CPLDebug("EPSILON", "W=%d,H=%d,x=%d,y=%d,w=%d,h=%d,offset=" CPL_FRMT_GUIB,
        //                    W, H, x, y, w, h, nStartBlockFileOff);

        int nNewBands = (hdr.block_type == EPS_GRAYSCALE_BLOCK) ? 1 : 3;
        if (nRasterXSize == 0)
        {
            if (W <= 0 || H <= 0)
            {
                break;
            }

            bRet = TRUE;
            nRasterXSize = W;
            nRasterYSize = H;
            *pnBands = nNewBands;
        }

        if (nRasterXSize != W || nRasterYSize != H || *pnBands != nNewBands ||
                x < 0 || y < 0 || x + w > W || y + h > H)
        {
            CPLError(CE_Failure, CPLE_AppDefined, "Bad block characteristics");
            bRet = FALSE;
            break;
        }

        nBlocks++;
        pasBlocks = (BlockDesc*)VSIRealloc(pasBlocks, sizeof(BlockDesc) * nBlocks);
        pasBlocks[nBlocks-1].x = x;
        pasBlocks[nBlocks-1].y = y;
        pasBlocks[nBlocks-1].w = w;
        pasBlocks[nBlocks-1].h = h;
        pasBlocks[nBlocks-1].offset = nStartBlockFileOff;

        if (bRegularTiling)
        {
            if (nTileW < 0)
            {
                nTileW = w;
                nTileH = h;
            }

            if (w > nTileW || h > nTileH)
                bRegularTiling = FALSE;

            if (x != nExpectedX)
                bRegularTiling = FALSE;

            if (y != nExpectedY || nTileH != h)
            {
                if (y + h != H)
                    bRegularTiling = FALSE;
            }

            if (nTileW != w)
            {
                if (x + w != W)
                    bRegularTiling = FALSE;
                else
                {
                    nExpectedX = 0;
                    nExpectedY += nTileW;
                }
            }
            else
                nExpectedX += nTileW;

            //if (!bRegularTiling)
            //    CPLDebug("EPSILON", "not regular tiling!");
        }
    }

    return bRet;
}
示例#22
0
static CPLString GIFCollectXMPMetadata(VSILFILE* fp)

{
    CPLString osXMP;

    /* Save current position to avoid disturbing GIF stream decoding */
    vsi_l_offset nCurOffset = VSIFTellL(fp);

    char abyBuffer[2048+1];

    VSIFSeekL( fp, 0, SEEK_SET );

    /* Loop over file */

    int iStartSearchOffset = 1024;
    while(true)
    {
        int nRead = static_cast<int>(VSIFReadL( abyBuffer + 1024, 1, 1024, fp ));
        if (nRead <= 0)
            break;
        abyBuffer[1024 + nRead] = 0;

        int iFoundOffset = -1;
        for(int i=iStartSearchOffset;i<1024+nRead - 14;i++)
        {
            if (memcmp(abyBuffer + i, "\x21\xff\x0bXMP DataXMP", 14) == 0)
            {
                iFoundOffset = i + 14;
                break;
            }
        }

        iStartSearchOffset = 0;

        if (iFoundOffset >= 0)
        {
            int nSize = 1024 + nRead - iFoundOffset;
            char* pszXMP = (char*)VSIMalloc(nSize + 1);
            if (pszXMP == nullptr)
                break;

            pszXMP[nSize] = 0;
            memcpy(pszXMP, abyBuffer + iFoundOffset, nSize);

            /* Read from file until we find a NUL character */
            int nLen = (int)strlen(pszXMP);
            while(nLen == nSize)
            {
                char* pszNewXMP = (char*)VSIRealloc(pszXMP, nSize + 1024 + 1);
                if (pszNewXMP == nullptr)
                    break;
                pszXMP = pszNewXMP;

                nRead = static_cast<int>(VSIFReadL( pszXMP + nSize, 1, 1024, fp ));
                if (nRead <= 0)
                    break;

                pszXMP[nSize + nRead] = 0;
                nLen += (int)strlen(pszXMP + nSize);
                nSize += nRead;
            }

            if (nLen > 256 && pszXMP[nLen - 1] == '\x01' &&
                pszXMP[nLen - 2] == '\x02' && pszXMP[nLen - 255] == '\xff' &&
                pszXMP[nLen - 256] == '\x01')
            {
                pszXMP[nLen - 256] = 0;

                osXMP = pszXMP;
            }

            VSIFree(pszXMP);

            break;
        }

        if (nRead != 1024)
            break;

        memcpy(abyBuffer, abyBuffer + 1024, 1024);
    }

    VSIFSeekL( fp, nCurOffset, SEEK_SET );

    return osXMP;
}
示例#23
0
OGRErr GMLHandler::dataHandler(const char *data, int nLen)

{
    int nIter = 0;

    if( m_pszCurField != NULL )
    {
        int     nCurFieldLength = strlen(m_pszCurField);

        // Ignore white space
        if (nCurFieldLength == 0)
        {
            while (nIter < nLen &&
                   ( data[nIter] == ' ' || data[nIter] == 10 || data[nIter]== 13 || data[nIter] == '\t') )
                nIter ++;
        }

        size_t nCharsLen = nLen - nIter;

        char *pszNewCurField = (char *)
            VSIRealloc( m_pszCurField,
                        nCurFieldLength+ nCharsLen +1 );
        if (pszNewCurField == NULL)
        {
            return CE_Failure;
        }
        m_pszCurField = pszNewCurField;
        memcpy( m_pszCurField + nCurFieldLength, data + nIter, nCharsLen);
        nCurFieldLength += nCharsLen;

        m_pszCurField[nCurFieldLength] = '\0';
    }
    else if( m_pszGeometry != NULL )
    {
        // Ignore white space
        if (m_nGeomLen == 0)
        {
            while (nIter < nLen &&
                   ( data[nIter] == ' ' || data[nIter] == 10 || data[nIter]== 13 || data[nIter] == '\t') )
                nIter ++;
        }

        size_t nCharsLen = nLen - nIter;

        if( m_nGeomLen + nCharsLen + 4 > m_nGeomAlloc )
        {
            m_nGeomAlloc = (size_t) (m_nGeomAlloc * 1.3 + nCharsLen + 1000);
            char* pszNewGeometry = (char *)
                VSIRealloc( m_pszGeometry, m_nGeomAlloc);
            if (pszNewGeometry == NULL)
            {
                return CE_Failure;
            }
            m_pszGeometry = pszNewGeometry;
        }

        memcpy( m_pszGeometry+m_nGeomLen, data + nIter, nCharsLen);
        m_nGeomLen += nCharsLen;
        m_pszGeometry[m_nGeomLen] = '\0';
    }

    return CE_None;
}