LCOV - code coverage report
Current view: directory - frmts/nitf - rpftocfile.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 267 189 70.8 %
Date: 2011-12-18 Functions: 4 3 75.0 %

       1                 : /******************************************************************************
       2                 :  * $Id: rpftocfile.cpp 20996 2010-10-28 18:38:15Z rouault $
       3                 :  *
       4                 :  * Project:  RPF A.TOC read Library
       5                 :  * Purpose:  Module responsible for opening a RPF TOC file, populating RPFToc
       6                 :  *           structure
       7                 :  * Author:   Even Rouault, even.rouault at mines-paris.org
       8                 :  *
       9                 :  **********************************************************************
      10                 :  * Copyright (c) 2007, Even Rouault
      11                 :  *
      12                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      13                 :  * copy of this software and associated documentation files (the "Software"),
      14                 :  * to deal in the Software without restriction, including without limitation
      15                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      16                 :  * and/or sell copies of the Software, and to permit persons to whom the
      17                 :  * Software is furnished to do so, subject to the following conditions:
      18                 :  *
      19                 :  * The above copyright notice and this permission notice shall be included
      20                 :  * in all copies or substantial portions of the Software.
      21                 :  *
      22                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      23                 :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      24                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      25                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      26                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      27                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      28                 :  * DEALINGS IN THE SOFTWARE.
      29                 :  ****************************************************************************/
      30                 : 
      31                 : /* Portions of code are placed under the following copyright : */
      32                 : /*
      33                 :  ******************************************************************************
      34                 :  * Copyright (C) 1995 Logiciels et Applications Scientifiques (L.A.S.) Inc
      35                 :  * Permission to use, copy, modify and distribute this software and
      36                 :  * its documentation for any purpose and without fee is hereby granted,
      37                 :  * provided that the above copyright notice appear in all copies, that
      38                 :  * both the copyright notice and this permission notice appear in
      39                 :  * supporting documentation, and that the name of L.A.S. Inc not be used 
      40                 :  * in advertising or publicity pertaining to distribution of the software 
      41                 :  * without specific, written prior permission. L.A.S. Inc. makes no
      42                 :  * representations about the suitability of this software for any purpose.
      43                 :  * It is provided "as is" without express or implied warranty.
      44                 :  ******************************************************************************
      45                 :  */
      46                 : 
      47                 : #include "rpftoclib.h"
      48                 : #include "cpl_vsi.h"
      49                 : #include "cpl_conv.h"
      50                 : #include "cpl_string.h"
      51                 : 
      52                 : CPL_CVSID("$Id: rpftocfile.cpp 20996 2010-10-28 18:38:15Z rouault $");
      53                 : 
      54                 : /************************************************************************/
      55                 : /*                        RPFTOCTrim()                                    */
      56                 : /************************************************************************/
      57                 : 
      58              40 : static void RPFTOCTrim(char* str)
      59                 : {
      60              40 :     char* c = str;
      61                 :     int i;
      62              40 :     if (str == NULL || *str == 0)
      63               0 :         return;
      64                 : 
      65              80 :     while(*c == ' ')
      66                 :     {
      67               0 :         c++;
      68                 :     }
      69              40 :     if (c != str)
      70                 :     {
      71               0 :         memmove(str, c, strlen(c)+1);
      72                 :     }
      73                 :     
      74              40 :     i = strlen(str) - 1;
      75              96 :     while (i >= 0 && str[i] == ' ')
      76                 :     {
      77              16 :         str[i] = 0;
      78              16 :         i--;
      79                 :     }
      80                 : }
      81                 : 
      82                 : /************************************************************************/
      83                 : /*                        RPFTOCRead()                                 */
      84                 : /************************************************************************/
      85                 : 
      86                 :  
      87               0 : RPFToc* RPFTOCRead(const char* pszFilename, NITFFile* psFile)
      88                 : {
      89                 :     int nTRESize;
      90                 :     const char* pachTRE = NITFFindTRE( psFile->pachTRE, psFile->nTREBytes, 
      91               0 :                            "RPFHDR", &nTRESize );
      92               0 :     if (pachTRE == NULL)
      93                 :     {
      94                 :         CPLError( CE_Failure, CPLE_NotSupported, 
      95               0 :                   "Invalid TOC file. Can't find RPFHDR." );
      96               0 :         return NULL;
      97                 :     }
      98                 : 
      99               0 :     if (nTRESize != 48)
     100                 :     {
     101                 :         CPLError( CE_Failure, CPLE_NotSupported, 
     102               0 :                   "RPFHDR TRE wrong size." );
     103               0 :         return NULL;
     104                 :     }
     105                 : 
     106               0 :     return  RPFTOCReadFromBuffer(pszFilename, psFile->fp, pachTRE);
     107                 : }
     108                 : 
     109                 : 
     110                 : /* This function is directly inspired by function parse_toc coming from ogdi/driver/rpf/utils.c */
     111                 : 
     112               8 : RPFToc* RPFTOCReadFromBuffer(const char* pszFilename, VSILFILE* fp, const char* tocHeader)
     113                 : {
     114                 :     int i, j;
     115                 :     unsigned int locationSectionPhysicalLocation;
     116                 :     
     117                 :     int nSections;
     118               8 :     unsigned int boundaryRectangleSectionSubHeaderPhysIndex = 0;
     119               8 :     unsigned int boundaryRectangleTablePhysIndex = 0;
     120               8 :     unsigned int frameFileIndexSectionSubHeaderPhysIndex = 0;
     121               8 :     unsigned int frameFileIndexSubsectionPhysIndex = 0;
     122                 :     
     123                 :     unsigned int boundaryRectangleTableOffset;
     124                 :     unsigned short boundaryRectangleCount;
     125                 :     
     126                 :     unsigned int frameIndexTableOffset;
     127                 :     unsigned int nFrameFileIndexRecords;
     128                 :     unsigned short nFrameFilePathnameRecords;
     129                 :     unsigned short frameFileIndexRecordLength;
     130                 : 
     131               8 :     int newBoundaryId = 0;
     132                 : 
     133                 :     RPFToc* toc;
     134                 :     
     135               8 :     tocHeader += 1; /* skip endian */
     136               8 :     tocHeader += 2; /* skip header length */
     137               8 :     tocHeader += 12; /* skip file name : this should be A.TOC (padded) */
     138               8 :     tocHeader += 1; /* skip new  */
     139               8 :     tocHeader += 15; /* skip standard_num  */
     140               8 :     tocHeader += 8; /* skip standard_date  */
     141               8 :     tocHeader += 1; /* skip classification  */
     142               8 :     tocHeader += 2; /* skip country  */
     143               8 :     tocHeader += 2; /* skip release  */
     144                 :     
     145               8 :     memcpy(&locationSectionPhysicalLocation, tocHeader, sizeof(unsigned int));
     146               8 :     CPL_MSBPTR32(&locationSectionPhysicalLocation);
     147                 :     
     148               8 :     if( VSIFSeekL( fp, locationSectionPhysicalLocation, SEEK_SET ) != 0)
     149                 :     {
     150                 :         CPLError( CE_Failure, CPLE_NotSupported, 
     151                 :                   "Invalid TOC file. Unable to seek to locationSectionPhysicalLocation at offset %d.",
     152               0 :                    locationSectionPhysicalLocation );
     153               0 :         return NULL;
     154                 :     }
     155                 :     
     156               8 :     NITFLocation* pasLocations = NITFReadRPFLocationTable(fp, &nSections);
     157                 :     
     158              40 :     for (i = 0; i < nSections; i++)
     159                 :     {
     160              32 :         if (pasLocations[i].nLocId == LID_BoundaryRectangleSectionSubheader)
     161                 :         {
     162               8 :             boundaryRectangleSectionSubHeaderPhysIndex = pasLocations[i].nLocOffset;
     163                 :         }
     164              24 :         else if (pasLocations[i].nLocId == LID_BoundaryRectangleTable)
     165                 :         {
     166               8 :             boundaryRectangleTablePhysIndex = pasLocations[i].nLocOffset;
     167                 :         }
     168              16 :         else if (pasLocations[i].nLocId == LID_FrameFileIndexSectionSubHeader)
     169                 :         {
     170               8 :             frameFileIndexSectionSubHeaderPhysIndex = pasLocations[i].nLocOffset;
     171                 :         }
     172               8 :         else if (pasLocations[i].nLocId == LID_FrameFileIndexSubsection)
     173                 :         {
     174               8 :             frameFileIndexSubsectionPhysIndex = pasLocations[i].nLocOffset;
     175                 :         }
     176                 :     }
     177                 : 
     178               8 :     CPLFree(pasLocations);
     179                 :     
     180               8 :     if (boundaryRectangleSectionSubHeaderPhysIndex == 0)
     181                 :     {
     182                 :         CPLError( CE_Failure, CPLE_NotSupported, 
     183               0 :                   "Invalid TOC file. Can't find LID_BoundaryRectangleSectionSubheader." );
     184               0 :         return NULL;
     185                 :     }
     186               8 :     if (boundaryRectangleTablePhysIndex == 0)
     187                 :     {
     188                 :         CPLError( CE_Failure, CPLE_NotSupported, 
     189               0 :                   "Invalid TOC file. Can't find LID_BoundaryRectangleTable." );
     190               0 :         return NULL;
     191                 :     }
     192               8 :     if (frameFileIndexSectionSubHeaderPhysIndex == 0)
     193                 :     {
     194                 :         CPLError( CE_Failure, CPLE_NotSupported, 
     195               0 :                   "Invalid TOC file. Can't find LID_FrameFileIndexSectionSubHeader." );
     196               0 :         return NULL;
     197                 :     }
     198               8 :     if (frameFileIndexSubsectionPhysIndex == 0)
     199                 :     {
     200                 :         CPLError( CE_Failure, CPLE_NotSupported, 
     201               0 :                   "Invalid TOC file. Can't find LID_FrameFileIndexSubsection." );
     202               0 :         return NULL;
     203                 :     }
     204                 :     
     205               8 :     if( VSIFSeekL( fp, boundaryRectangleSectionSubHeaderPhysIndex, SEEK_SET ) != 0)
     206                 :     {
     207                 :         CPLError( CE_Failure, CPLE_NotSupported, 
     208                 :                   "Invalid TOC file. Unable to seek to boundaryRectangleSectionSubHeaderPhysIndex at offset %d.",
     209               0 :                    boundaryRectangleSectionSubHeaderPhysIndex );
     210               0 :         return NULL;
     211                 :     }
     212                 :     
     213               8 :     VSIFReadL( &boundaryRectangleTableOffset, 1, sizeof(boundaryRectangleTableOffset), fp);
     214               8 :     CPL_MSBPTR32( &boundaryRectangleTableOffset );
     215                 :     
     216               8 :     VSIFReadL( &boundaryRectangleCount, 1, sizeof(boundaryRectangleCount), fp);
     217               8 :     CPL_MSBPTR16( &boundaryRectangleCount );
     218                 :     
     219               8 :     if( VSIFSeekL( fp, boundaryRectangleTablePhysIndex, SEEK_SET ) != 0)
     220                 :     {
     221                 :         CPLError( CE_Failure, CPLE_NotSupported, 
     222                 :                   "Invalid TOC file. Unable to seek to boundaryRectangleTablePhysIndex at offset %d.",
     223               0 :                    boundaryRectangleTablePhysIndex );
     224               0 :         return NULL;
     225                 :     }
     226                 :     
     227               8 :     toc = (RPFToc*)CPLMalloc(sizeof(RPFToc));
     228               8 :     toc->nEntries = boundaryRectangleCount;
     229               8 :     toc->entries = (RPFTocEntry*)CPLMalloc(boundaryRectangleCount * sizeof(RPFTocEntry));
     230               8 :     memset(toc->entries, 0, boundaryRectangleCount * sizeof(RPFTocEntry));
     231                 :     
     232              16 :     for(i=0;i<toc->nEntries;i++)
     233                 :     {
     234               8 :         toc->entries[i].isOverviewOrLegend = 0;
     235                 :         
     236               8 :         VSIFReadL( toc->entries[i].type, 1, 5, fp);
     237               8 :         toc->entries[i].type[5] = 0;
     238               8 :         RPFTOCTrim(toc->entries[i].type);
     239                 :         
     240               8 :         VSIFReadL( toc->entries[i].compression, 1, 5, fp);
     241               8 :         toc->entries[i].compression[5] = 0;
     242               8 :         RPFTOCTrim(toc->entries[i].compression);
     243                 :         
     244               8 :         VSIFReadL( toc->entries[i].scale, 1, 12, fp);
     245               8 :         toc->entries[i].scale[12] = 0;
     246               8 :         RPFTOCTrim(toc->entries[i].scale);
     247              16 :         if (toc->entries[i].scale[0] == '1' &&
     248               8 :             toc->entries[i].scale[1] == ':')
     249                 :         {
     250               8 :             memmove(toc->entries[i].scale,
     251               8 :                     toc->entries[i].scale+2,
     252              24 :                     strlen(toc->entries[i].scale+2)+1);
     253                 :         }
     254                 :         
     255               8 :         VSIFReadL( toc->entries[i].zone, 1, 1, fp);
     256               8 :         toc->entries[i].zone[1] = 0;
     257               8 :         RPFTOCTrim(toc->entries[i].zone);
     258                 :         
     259               8 :         VSIFReadL( toc->entries[i].producer, 1, 5, fp);
     260               8 :         toc->entries[i].producer[5] = 0;
     261               8 :         RPFTOCTrim(toc->entries[i].producer);
     262                 : 
     263               8 :         VSIFReadL( &toc->entries[i].nwLat, 1, sizeof(double), fp);
     264               8 :         CPL_MSBPTR64( &toc->entries[i].nwLat);
     265                 : 
     266               8 :         VSIFReadL( &toc->entries[i].nwLong, 1, sizeof(double), fp);
     267               8 :         CPL_MSBPTR64( &toc->entries[i].nwLong);
     268                 : 
     269               8 :         VSIFReadL( &toc->entries[i].swLat, 1, sizeof(double), fp);
     270               8 :         CPL_MSBPTR64( &toc->entries[i].swLat);
     271                 : 
     272               8 :         VSIFReadL( &toc->entries[i].swLong, 1, sizeof(double), fp);
     273               8 :         CPL_MSBPTR64( &toc->entries[i].swLong);
     274                 : 
     275               8 :         VSIFReadL( &toc->entries[i].neLat, 1, sizeof(double), fp);
     276               8 :         CPL_MSBPTR64( &toc->entries[i].neLat);
     277                 : 
     278               8 :         VSIFReadL( &toc->entries[i].neLong, 1, sizeof(double), fp);
     279               8 :         CPL_MSBPTR64( &toc->entries[i].neLong);
     280                 : 
     281               8 :         VSIFReadL( &toc->entries[i].seLat, 1, sizeof(double), fp);
     282               8 :         CPL_MSBPTR64( &toc->entries[i].seLat);
     283                 :         
     284               8 :         VSIFReadL( &toc->entries[i].seLong, 1, sizeof(double), fp);
     285               8 :         CPL_MSBPTR64( &toc->entries[i].seLong);
     286                 :         
     287               8 :         VSIFReadL( &toc->entries[i].vertResolution, 1, sizeof(double), fp);
     288               8 :         CPL_MSBPTR64( &toc->entries[i].vertResolution);
     289                 :         
     290               8 :         VSIFReadL( &toc->entries[i].horizResolution, 1, sizeof(double), fp);
     291               8 :         CPL_MSBPTR64( &toc->entries[i].horizResolution);
     292                 :         
     293               8 :         VSIFReadL( &toc->entries[i].vertInterval, 1, sizeof(double), fp);
     294               8 :         CPL_MSBPTR64( &toc->entries[i].vertInterval);
     295                 : 
     296               8 :         VSIFReadL( &toc->entries[i].horizInterval, 1, sizeof(double), fp);
     297               8 :         CPL_MSBPTR64( &toc->entries[i].horizInterval);
     298                 :         
     299               8 :         VSIFReadL( &toc->entries[i].nVertFrames, 1, sizeof(int), fp);
     300               8 :         CPL_MSBPTR32( &toc->entries[i].nVertFrames );
     301                 :         
     302               8 :         VSIFReadL( &toc->entries[i].nHorizFrames, 1, sizeof(int), fp);
     303               8 :         CPL_MSBPTR32( &toc->entries[i].nHorizFrames );
     304                 :         
     305               8 :         toc->entries[i].frameEntries = (RPFTocFrameEntry*)
     306               8 :                 VSIMalloc3(toc->entries[i].nVertFrames, toc->entries[i].nHorizFrames, sizeof(RPFTocFrameEntry));
     307               8 :         if (toc->entries[i].frameEntries == NULL)
     308                 :         {
     309                 :             CPLError( CE_Failure, CPLE_OutOfMemory,
     310               0 :                       "RPFTOCReadFromBuffer : Out of memory. Probably due to corrupted TOC file.");
     311               0 :             RPFTOCFree(toc);
     312               0 :             return NULL;
     313                 :         }
     314               8 :         memset(toc->entries[i].frameEntries, 0,
     315              16 :                toc->entries[i].nVertFrames * toc->entries[i].nHorizFrames * sizeof(RPFTocFrameEntry));
     316                 :         
     317                 :         CPLDebug("RPFTOC", "[%d] type=%s, compression=%s, scale=%s, zone=%s, producer=%s, nVertFrames=%d, nHorizFrames=%d",
     318              24 :                  i, toc->entries[i].type, toc->entries[i].compression, toc->entries[i].scale,
     319              32 :                  toc->entries[i].zone, toc->entries[i].producer, toc->entries[i].nVertFrames, toc->entries[i].nHorizFrames);
     320                 :     }
     321                 :     
     322               8 :     if( VSIFSeekL( fp, frameFileIndexSectionSubHeaderPhysIndex, SEEK_SET ) != 0)
     323                 :     {
     324                 :         CPLError( CE_Failure, CPLE_NotSupported, 
     325                 :                   "Invalid TOC file. Unable to seek to frameFileIndexSectionSubHeaderPhysIndex at offset %d.",
     326               0 :                    frameFileIndexSectionSubHeaderPhysIndex );
     327               0 :         RPFTOCFree(toc);
     328               0 :         return NULL;
     329                 :     }
     330                 :     
     331                 :     /* Skip 1 byte security classification */
     332               8 :     VSIFSeekL( fp, 1, SEEK_CUR );
     333                 :     
     334               8 :     VSIFReadL( &frameIndexTableOffset, 1, sizeof(frameIndexTableOffset), fp);
     335               8 :     CPL_MSBPTR32( &frameIndexTableOffset );
     336                 :     
     337               8 :     VSIFReadL( &nFrameFileIndexRecords, 1, sizeof(nFrameFileIndexRecords), fp);
     338               8 :     CPL_MSBPTR32( &nFrameFileIndexRecords );
     339                 :     
     340               8 :     VSIFReadL( &nFrameFilePathnameRecords, 1, sizeof(nFrameFilePathnameRecords), fp);
     341               8 :     CPL_MSBPTR16( &nFrameFilePathnameRecords );
     342                 :     
     343               8 :     VSIFReadL( &frameFileIndexRecordLength, 1, sizeof(frameFileIndexRecordLength), fp);
     344               8 :     CPL_MSBPTR16( &frameFileIndexRecordLength );
     345                 :     
     346              16 :     for (i=0;i<(int)nFrameFileIndexRecords;i++)
     347                 :     {
     348                 :         RPFTocEntry* entry;
     349                 :         RPFTocFrameEntry* frameEntry;
     350                 :         unsigned short boundaryId, frameRow, frameCol;
     351                 :         unsigned int offsetFrameFilePathName;
     352                 :         unsigned short pathLength;
     353                 :         
     354               8 :         if( VSIFSeekL( fp, frameFileIndexSubsectionPhysIndex + frameFileIndexRecordLength * i, SEEK_SET ) != 0)
     355                 :         {
     356                 :             CPLError( CE_Failure, CPLE_NotSupported, 
     357                 :                     "Invalid TOC file. Unable to seek to frameFileIndexSubsectionPhysIndex(%d) at offset %d.",
     358               0 :                      i, frameFileIndexSubsectionPhysIndex + frameFileIndexRecordLength * i);
     359               0 :             RPFTOCFree(toc);
     360               0 :             return NULL;
     361                 :         }
     362                 : 
     363               8 :         VSIFReadL( &boundaryId, 1, sizeof(boundaryId), fp);
     364               8 :         CPL_MSBPTR16( &boundaryId );
     365                 :         
     366               8 :         if (i == 0 && boundaryId == 0)
     367               8 :             newBoundaryId = 1;
     368               8 :         if (newBoundaryId == 0)
     369               0 :             boundaryId--;
     370                 :         
     371               8 :         if (boundaryId >= toc->nEntries)
     372                 :         {
     373                 :             CPLError( CE_Failure, CPLE_NotSupported, 
     374                 :                     "Invalid TOC file. Bad boundary id (%d) for frame file index %d.",
     375               0 :                      boundaryId, i);
     376               0 :             RPFTOCFree(toc);
     377               0 :             return NULL;
     378                 :         }
     379                 :         
     380               8 :         entry = &toc->entries[boundaryId];
     381               8 :         entry->boundaryId = boundaryId;
     382                 :         
     383               8 :         VSIFReadL( &frameRow, 1, sizeof(frameRow), fp);
     384               8 :         CPL_MSBPTR16( &frameRow );
     385                 :         
     386               8 :         VSIFReadL( &frameCol, 1, sizeof(frameCol), fp);
     387               8 :         CPL_MSBPTR16( &frameCol );
     388                 : 
     389                 : 
     390               8 :         if (newBoundaryId == 0)
     391                 :         {
     392               0 :             frameRow--;
     393               0 :             frameCol--;
     394                 :         }
     395                 :         else
     396                 :         {
     397                 :             /* Trick so that frames are numbered north to south */
     398               8 :             frameRow = (unsigned short)((entry->nVertFrames-1) - frameRow);
     399                 :         }
     400                 :    
     401               8 :         if (frameRow >= entry->nVertFrames)
     402                 :         {
     403                 :             CPLError( CE_Failure, CPLE_NotSupported, 
     404                 :                         "Invalid TOC file. Bad row num (%d) for frame file index %d.",
     405               0 :                         frameRow, i);
     406               0 :             RPFTOCFree(toc);
     407               0 :             return NULL;
     408                 :         }
     409                 :         
     410               8 :         if (frameCol >= entry->nHorizFrames)
     411                 :         {
     412                 :             CPLError( CE_Failure, CPLE_NotSupported, 
     413                 :                         "Invalid TOC file. Bad col num (%d) for frame file index %d.",
     414               0 :                         frameCol, i);
     415               0 :             RPFTOCFree(toc);
     416               0 :             return NULL;
     417                 :         }
     418                 : 
     419               8 :         frameEntry = &entry->frameEntries[frameRow * entry->nHorizFrames + frameCol ];
     420               8 :         frameEntry->frameRow = frameRow;
     421               8 :         frameEntry->frameCol = frameCol;
     422                 : 
     423               8 :         if (frameEntry->exists)
     424                 :         {
     425                 :             CPLError( CE_Warning, CPLE_AppDefined, 
     426                 :                       "Frame entry(%d,%d) for frame file index %d was already found.",
     427               0 :                       frameRow, frameCol, i);
     428               0 :             CPLFree(frameEntry->directory);
     429               0 :             frameEntry->directory = NULL;
     430               0 :             CPLFree(frameEntry->fullFilePath);
     431               0 :             frameEntry->fullFilePath = NULL;
     432               0 :             frameEntry->exists = 0;
     433                 :         }
     434                 :         
     435               8 :         VSIFReadL( &offsetFrameFilePathName, 1, sizeof(offsetFrameFilePathName), fp);
     436               8 :         CPL_MSBPTR32( &offsetFrameFilePathName );
     437                 :         
     438                 : 
     439               8 :         VSIFReadL( frameEntry->filename, 1, 12, fp);
     440               8 :         frameEntry->filename[12] = '\0';
     441                 : 
     442                 :         /* Check if the filename is an overview or legend */
     443             104 :         for (j=0;j<12;j++)
     444                 :         {
     445             384 :             if (strcmp(&(frameEntry->filename[j]),".OVR") == 0 ||
     446              96 :                 strcmp(&(frameEntry->filename[j]),".ovr") == 0 ||
     447              96 :                 strcmp(&(frameEntry->filename[j]),".LGD") == 0 ||
     448              96 :                 strcmp(&(frameEntry->filename[j]),".lgd") == 0)
     449                 :             {
     450               0 :                 entry->isOverviewOrLegend = TRUE;
     451               0 :                 break;
     452                 :             }
     453                 :         }
     454                 :         
     455                 :         /* Extract series code */
     456               8 :         if (entry->seriesAbbreviation == NULL)
     457                 :         {
     458               8 :             const NITFSeries* series = NITFGetSeriesInfo(frameEntry->filename);
     459               8 :             if (series)
     460                 :             {
     461               8 :                 entry->seriesAbbreviation = series->abbreviation;
     462               8 :                 entry->seriesName = series->name;
     463                 :             }
     464                 :         }
     465                 : 
     466                 :         /* Get file geo reference */
     467               8 :         VSIFReadL( frameEntry->georef, 1, 6, fp);
     468               8 :         frameEntry->georef[6] = '\0';
     469                 : 
     470                 :         /* Go to start of pathname record */
     471                 :         /* New path_off offset from start of frame file index section of TOC?? */
     472                 :         /* Add pathoffset wrt frame file index table subsection (loc[3]) */
     473               8 :         if( VSIFSeekL( fp, frameFileIndexSubsectionPhysIndex + offsetFrameFilePathName, SEEK_SET ) != 0)
     474                 :         {
     475                 :             CPLError( CE_Failure, CPLE_NotSupported, 
     476                 :                     "Invalid TOC file. Unable to seek to frameFileIndexSubsectionPhysIndex + offsetFrameFilePathName(%d) at offset %d.",
     477               0 :                      i, frameFileIndexSubsectionPhysIndex + offsetFrameFilePathName);
     478               0 :             RPFTOCFree(toc);
     479               0 :             return NULL;
     480                 :         }
     481                 : 
     482               8 :         VSIFReadL( &pathLength, 1, sizeof(pathLength), fp);
     483               8 :         CPL_MSBPTR16( &pathLength );
     484                 :         
     485                 :         /* if nFrameFileIndexRecords == 65535 and pathLength == 65535 for each record,
     486                 :            this leads to 4 GB allocation... Protect against this case */
     487               8 :         if (pathLength > 256)
     488                 :         {
     489                 :             CPLError( CE_Failure, CPLE_NotSupported,
     490               0 :                       "Path length is big : %d. Probably corrupted TOC file.", (int)pathLength);
     491               0 :             RPFTOCFree(toc);
     492               0 :             return NULL;
     493                 :         }
     494                 :         
     495               8 :         frameEntry->directory = (char *)CPLMalloc(pathLength+1);
     496               8 :         VSIFReadL( frameEntry->directory, 1, pathLength, fp);
     497               8 :         frameEntry->directory[pathLength] = 0;
     498               8 :         if (pathLength > 0 && frameEntry->directory[pathLength-1] == '/')
     499               8 :             frameEntry->directory[pathLength-1] = 0;
     500                 :         
     501               8 :         if (frameEntry->directory[0] == '.' && frameEntry->directory[1] == '/')
     502               0 :             memmove(frameEntry->directory, frameEntry->directory+2, strlen(frameEntry->directory+2)+1);
     503                 :         
     504                 :         {
     505               8 :             char* baseDir = CPLStrdup(CPLGetDirname(pszFilename));
     506                 :             VSIStatBufL sStatBuf;
     507                 :             char* subdir;
     508               8 :             if (CPLIsFilenameRelative(frameEntry->directory) == FALSE)
     509               0 :                 subdir = CPLStrdup(frameEntry->directory);
     510              16 :             else if (frameEntry->directory[0] == '.' && frameEntry->directory[1] == 0)
     511               8 :                 subdir = CPLStrdup(baseDir);
     512                 :             else
     513               0 :                 subdir = CPLStrdup(CPLFormFilename(baseDir, frameEntry->directory, NULL));
     514                 : #if !defined(_WIN32) && !defined(_WIN32_CE)
     515               8 :             if( VSIStatL( subdir, &sStatBuf ) != 0 && subdir[strlen(baseDir)] != 0)
     516                 :             {
     517               0 :                 char* c = subdir + strlen(baseDir)+1;
     518               0 :                 while(*c)
     519                 :                 {
     520               0 :                     if (*c >= 'A' && *c <= 'Z')
     521               0 :                         *c += 'a' - 'A';
     522               0 :                     c++;
     523                 :                 }
     524                 :             }
     525                 : #endif
     526                 :             frameEntry->fullFilePath = CPLStrdup(CPLFormFilename(
     527                 :                     subdir,
     528               8 :                     frameEntry->filename, NULL));
     529               8 :             if( VSIStatL( frameEntry->fullFilePath, &sStatBuf ) != 0 )
     530                 :             {
     531                 : #if !defined(_WIN32) && !defined(_WIN32_CE)
     532               0 :                 char* c = frameEntry->fullFilePath + strlen(subdir)+1;
     533               0 :                 while(*c)
     534                 :                 {
     535               0 :                     if (*c >= 'A' && *c <= 'Z')
     536               0 :                         *c += 'a' - 'A';
     537               0 :                     c++;
     538                 :                 }
     539               0 :                 if( VSIStatL( frameEntry->fullFilePath, &sStatBuf ) != 0 )
     540                 : #endif
     541                 :                 {
     542               0 :                     frameEntry->fileExists = 0;
     543                 :                     CPLError( CE_Warning, CPLE_AppDefined, 
     544               0 :                         "File %s does not exist.", frameEntry->fullFilePath );
     545                 :                 }
     546                 : #if !defined(_WIN32) && !defined(_WIN32_CE)
     547                 :                 else
     548                 :                 {
     549               0 :                     frameEntry->fileExists = 1;
     550                 :                 }
     551                 : #endif
     552                 :             }
     553                 :             else
     554                 :             {
     555               8 :                 frameEntry->fileExists = 1;
     556                 :             }
     557               8 :             CPLFree(subdir);
     558               8 :             CPLFree(baseDir);
     559                 :         }
     560                 : 
     561               8 :         CPLDebug("RPFTOC", "Entry %d : %s,%s (%d, %d)", boundaryId, frameEntry->directory, frameEntry->filename, frameRow, frameCol);
     562                 : 
     563               8 :         frameEntry->exists = 1;
     564                 :     }
     565                 :     
     566               8 :     return toc;
     567                 : }
     568                 : 
     569                 : /************************************************************************/
     570                 : /*                        RPFTOCFree()                                 */
     571                 : /************************************************************************/
     572                 : 
     573               8 : void RPFTOCFree(RPFToc*  toc)
     574                 : {
     575                 :     int i, j;
     576               8 :     if (!toc) return;
     577                 : 
     578              16 :     for(i=0;i<toc->nEntries;i++)
     579                 :     {
     580              16 :         for(j=0;j<(int)(toc->entries[i].nVertFrames * toc->entries[i].nHorizFrames); j++)
     581                 :         {
     582               8 :             CPLFree(toc->entries[i].frameEntries[j].fullFilePath);
     583               8 :             CPLFree(toc->entries[i].frameEntries[j].directory);
     584                 :         }
     585               8 :         CPLFree(toc->entries[i].frameEntries);
     586                 :     }
     587                 : 
     588               8 :     CPLFree(toc->entries);
     589               8 :     CPLFree(toc);
     590                 : }

Generated by: LCOV version 1.7