~ubuntu-branches/ubuntu/saucy/mapserver/saucy-security

« back to all changes in this revision

Viewing changes to mapxbase.c

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2011-12-23 14:02:06 UTC
  • mfrom: (26.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111223140206-n3h9t2hsa8hyslmu
Tags: 6.0.1-2
Added missed stuff for libmapscript-perl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/******************************************************************************
2
 
 * $Id: mapxbase.c 7418 2008-02-29 00:02:49Z nsavard $
 
2
 * $Id: mapxbase.c 10772 2010-11-29 18:27:02Z aboudreault $
3
3
 *
4
4
 * Project:  MapServer
5
5
 * Purpose:  .dbf access API.  Derived from shapelib, and relicensed with 
28
28
 * DEALINGS IN THE SOFTWARE.
29
29
 ****************************************************************************/
30
30
 
 
31
#define _FILE_OFFSET_BITS 64
 
32
 
31
33
#include "mapserver.h"
32
34
#include <stdlib.h> /* for atof() and atoi() */
33
35
#include <math.h>
34
36
 
35
 
MS_CVSID("$Id: mapxbase.c 7418 2008-02-29 00:02:49Z nsavard $")
 
37
MS_CVSID("$Id: mapxbase.c 10772 2010-11-29 18:27:02Z aboudreault $")
 
38
 
 
39
/* try to use a large file version of fseek for files up to 4GB (#3514) */
 
40
#if _MSC_VER > 1310
 
41
#  define safe_fseek _fseeki64
 
42
#elif defined(fseeko)
 
43
#  define safe_fseek fseeko
 
44
#else
 
45
#  define safe_fseek fseek
 
46
#endif
36
47
 
37
48
/************************************************************************/
38
49
/*                             SfRealloc()                              */
112
123
static void flushRecord( DBFHandle psDBF )
113
124
 
114
125
{
115
 
    int         nRecordOffset;
 
126
    unsigned int nRecordOffset;
116
127
 
117
128
    if( psDBF->bCurrentRecordModified && psDBF->nCurrentRecord > -1 )
118
129
    {
121
132
        nRecordOffset = psDBF->nRecordLength * psDBF->nCurrentRecord 
122
133
                                                     + psDBF->nHeaderLength;
123
134
 
124
 
        fseek( psDBF->fp, nRecordOffset, 0 );
 
135
        safe_fseek( psDBF->fp, nRecordOffset, 0 );
125
136
        fwrite( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
126
137
    }
127
138
}
150
161
    /*  Ensure the extension is converted to dbf or DBF if it is            */
151
162
    /*  currently .shp or .shx.                                             */
152
163
    /* -------------------------------------------------------------------- */
153
 
    pszDBFFilename = (char *) malloc(strlen(pszFilename)+1);
 
164
    pszDBFFilename = (char *) msSmallMalloc(strlen(pszFilename)+1);
154
165
    strcpy( pszDBFFilename, pszFilename );
155
166
    
156
167
    if( strcmp(pszFilename+strlen(pszFilename)-4,".shp") 
168
179
    /*      Open the file.                                                  */
169
180
    /* -------------------------------------------------------------------- */
170
181
    psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );
 
182
    MS_CHECK_ALLOC(psDBF, sizeof(DBFInfo), NULL);
171
183
    psDBF->fp = fopen( pszDBFFilename, pszAccess );
172
184
    if( psDBF->fp == NULL )
173
185
        return( NULL );
184
196
    /* -------------------------------------------------------------------- */
185
197
    /*  Read Table Header info                                              */
186
198
    /* -------------------------------------------------------------------- */
187
 
    pabyBuf = (uchar *) malloc(500);
 
199
    pabyBuf = (uchar *) msSmallMalloc(500);
188
200
    fread( pabyBuf, 32, 1, psDBF->fp );
189
201
 
190
202
    psDBF->nRecords = nRecords = 
195
207
    
196
208
    psDBF->nFields = nFields = (nHeadLen - 32) / 32;
197
209
 
198
 
    psDBF->pszCurrentRecord = (char *) malloc(nRecLen);
 
210
    psDBF->pszCurrentRecord = (char *) msSmallMalloc(nRecLen);
199
211
 
200
212
    /* -------------------------------------------------------------------- */
201
213
    /*  Read in Field Definitions                                           */
206
218
    fseek( psDBF->fp, 32, 0 );
207
219
    fread( pabyBuf, nHeadLen, 1, psDBF->fp );
208
220
 
209
 
    psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);
210
 
    psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);
211
 
    psDBF->panFieldDecimals = (int *) malloc(sizeof(int) * nFields);
212
 
    psDBF->pachFieldType = (char *) malloc(sizeof(char) * nFields);
 
221
    psDBF->panFieldOffset = (int *) msSmallMalloc(sizeof(int) * nFields);
 
222
    psDBF->panFieldSize = (int *) msSmallMalloc(sizeof(int) * nFields);
 
223
    psDBF->panFieldDecimals = (int *) msSmallMalloc(sizeof(int) * nFields);
 
224
    psDBF->pachFieldType = (char *) msSmallMalloc(sizeof(char) * nFields);
213
225
 
214
226
    for( iField = 0; iField < nFields; iField++ )
215
227
    {
327
339
    /*  Create the info structure.                                          */
328
340
    /* -------------------------------------------------------------------- */
329
341
    psDBF = (DBFHandle) malloc(sizeof(DBFInfo));
 
342
    if (psDBF == NULL)
 
343
    {
 
344
        msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", "msDBFCreate()",
 
345
                   __FILE__, __LINE__, sizeof(DBFInfo));
 
346
        fclose(fp);
 
347
        return NULL;
 
348
    }
330
349
 
331
350
    psDBF->fp = fp;
332
351
    psDBF->nRecords = 0;
487
506
static char *msDBFReadAttribute(DBFHandle psDBF, int hEntity, int iField )
488
507
 
489
508
{
490
 
    int         nRecordOffset, i;
 
509
    int         i;
 
510
    unsigned int nRecordOffset;
491
511
    uchar       *pabyRec;
492
512
    char        *pReturnField = NULL;
493
 
 
 
513
    
494
514
    /* -------------------------------------------------------------------- */
495
515
    /*  Is the request valid?                                               */
496
516
    /* -------------------------------------------------------------------- */
515
535
 
516
536
        nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;
517
537
 
518
 
        fseek( psDBF->fp, nRecordOffset, 0 );
 
538
        safe_fseek( psDBF->fp, nRecordOffset, 0 );
519
539
        fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
520
540
 
521
541
        psDBF->nCurrentRecord = hEntity;
672
692
/************************************************************************/
673
693
static int msDBFWriteAttribute(DBFHandle psDBF, int hEntity, int iField, void * pValue )
674
694
{
675
 
  int           nRecordOffset, i, j;
 
695
  unsigned int          nRecordOffset;
 
696
  int  i, j;
676
697
  uchar *pabyRec;
677
698
  char  szSField[40], szFormat[12];
678
699
  
709
730
      
710
731
      nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;
711
732
      
712
 
      fseek( psDBF->fp, nRecordOffset, 0 );
 
733
      safe_fseek( psDBF->fp, nRecordOffset, 0 );
713
734
      fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
714
735
 
715
736
      psDBF->nCurrentRecord = hEntity;
725
746
  case 'N':
726
747
  case 'F':
727
748
    if( psDBF->panFieldDecimals[iField] == 0 ) {
728
 
      sprintf( szFormat, "%%%dd", psDBF->panFieldSize[iField] );
729
 
      sprintf(szSField, szFormat, (int) *((double *) pValue) );
 
749
      snprintf( szFormat, sizeof(szFormat), "%%%dd", psDBF->panFieldSize[iField] );
 
750
      snprintf(szSField, sizeof(szSField), szFormat, (int) *((double *) pValue) );
730
751
      if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
731
752
        szSField[psDBF->panFieldSize[iField]] = '\0';
732
753
      strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), szSField, strlen(szSField) );
733
754
    } else {
734
 
      sprintf( szFormat, "%%%d.%df", psDBF->panFieldSize[iField], psDBF->panFieldDecimals[iField] );
735
 
      sprintf(szSField, szFormat, *((double *) pValue) );
 
755
      snprintf( szFormat, sizeof(szFormat), "%%%d.%df", psDBF->panFieldSize[iField], psDBF->panFieldDecimals[iField] );
 
756
      snprintf(szSField, sizeof(szSField), szFormat, *((double *) pValue) );
736
757
      if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
737
758
        szSField[psDBF->panFieldSize[iField]] = '\0';
738
759
      strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),  szSField, strlen(szSField) );
828
849
    return(NULL);
829
850
  }
830
851
 
831
 
  if((items = (char **)malloc(sizeof(char *)*nFields)) == NULL) {
832
 
    msSetError(MS_MEMERR, NULL, "msGetDBFItems()");
833
 
    return(NULL);
834
 
  }
 
852
  items = (char **)malloc(sizeof(char *)*nFields);
 
853
  MS_CHECK_ALLOC(items, sizeof(char *)*nFields, NULL);
835
854
 
836
855
  for(i=0;i<nFields;i++) {
837
856
    msDBFGetFieldInfo(dbffile, i, fName, NULL, NULL);
838
 
    items[i] = strdup(fName);
 
857
    items[i] = msStrdup(fName);
839
858
  }
840
859
 
841
860
  return(items);
854
873
    return(NULL);
855
874
  }
856
875
 
857
 
  if((values = (char **)malloc(sizeof(char *)*nFields)) == NULL) {
858
 
    msSetError(MS_MEMERR, NULL, "msGetAllDBFValues()");
859
 
    return(NULL);
860
 
  }
 
876
  values = (char **)malloc(sizeof(char *)*nFields);
 
877
  MS_CHECK_ALLOC(values, sizeof(char *)*nFields, NULL);
861
878
 
862
879
  for(i=0;i<nFields;i++)
863
 
    values[i] = strdup(msDBFReadStringAttribute(dbffile, record, i));
 
880
    values[i] = msStrdup(msDBFReadStringAttribute(dbffile, record, i));
864
881
 
865
882
  return(values);
866
883
}
870
887
  int *itemindexes=NULL, i;
871
888
 
872
889
  if(numitems == 0) return(NULL);
873
 
 
 
890
  
874
891
  itemindexes = (int *)malloc(sizeof(int)*numitems);
875
 
  if(!itemindexes) {
876
 
    msSetError(MS_MEMERR, NULL, "msGetItemIndexes()");
877
 
    return(NULL);
878
 
  }
879
 
 
 
892
  MS_CHECK_ALLOC(itemindexes, sizeof(int)*numitems, NULL);
 
893
  
880
894
  for(i=0;i<numitems;i++) {
881
895
    itemindexes[i] = msDBFGetItemIndex(dbffile, items[i]);
882
896
    if(itemindexes[i] == -1) { 
896
910
 
897
911
  if(numitems == 0) return(NULL);
898
912
 
899
 
  if((values = (char **)malloc(sizeof(char *)*numitems)) == NULL) {
900
 
    msSetError(MS_MEMERR, NULL, "msGetSomeDBFValues()");
901
 
    return(NULL);
902
 
  }
 
913
  values = (char **)malloc(sizeof(char *)*numitems);
 
914
  MS_CHECK_ALLOC(values, sizeof(char *)*numitems, NULL);
903
915
 
904
916
  for(i=0;i<numitems;i++) {
905
917
    value = msDBFReadStringAttribute(dbffile, record, itemindexes[i]);
906
918
    if (value == NULL)
907
919
      return NULL; /* Error already reported by msDBFReadStringAttribute() */
908
 
    values[i] = strdup(value);
 
920
    values[i] = msStrdup(value);
909
921
  }
910
922
 
911
923
  return(values);