Coverage Report

Created: 2026-08-11 08:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/gtiff/libgeotiff/geo_tiffp.c
Line
Count
Source
1
/**********************************************************************
2
 *
3
 *  geo_tiffp.c  Private TIFF interface module for GEOTIFF
4
 *
5
 *    This module implements the interface between the GEOTIFF
6
 *    tag parser and the TIFF i/o module. The current setup
7
 *    relies on the "libtiff" code, but if you use your own
8
 *    TIFF reader software, you may replace the module implementations
9
 *    here with your own calls. No "libtiff" dependencies occur
10
 *    anywhere else in this code.
11
 *
12
 * copyright (c) 1995   Niles D. Ritter
13
 *
14
 * Permission granted to use this software, so long as this copyright
15
 * notice accompanies any products derived therefrom.
16
 *
17
 **********************************************************************/
18
19
#include "geotiff.h"    /* public GTIFF interface */
20
21
#include "geo_tiffp.h"  /* Private TIFF interface */
22
#include "geo_keyp.h"   /* Private GTIFF interface */
23
24
/* tiff size array global */
25
gsize_t _gtiff_size[] = { 0, 1, 2, 4, 8, 1, 4, 8, 1, 2, 4, 1 };
26
27
static int        _GTIFGetField (tiff_t *tif, pinfo_t tag, int *count, void *value );
28
static int        _GTIFSetField (tiff_t *tif, pinfo_t tag, int  count, void *value );
29
static tagtype_t  _GTIFTagType  (tiff_t *tif, pinfo_t tag);
30
31
/*
32
 * Set up default TIFF handlers.
33
 */
34
void _GTIFSetDefaultTIFF(TIFFMethod *method)
35
277k
{
36
277k
  if (!method) return;
37
38
277k
  method->get = _GTIFGetField;
39
277k
  method->set = _GTIFSetField;
40
277k
  method->type = _GTIFTagType;
41
277k
}
42
43
gdata_t _GTIFcalloc(gsize_t size)
44
1.74M
{
45
1.74M
    gdata_t data=(gdata_t)_TIFFmalloc((tsize_t)size);
46
1.74M
  if (data) _TIFFmemset((tdata_t)data,0,(tsize_t)size);
47
1.74M
  return data;
48
1.74M
}
49
50
gdata_t _GTIFrealloc(gdata_t ptr, gsize_t size)
51
160k
{
52
160k
    return _TIFFrealloc((tdata_t)ptr, (tsize_t) size);
53
160k
}
54
55
void _GTIFmemcpy(gdata_t out,gdata_t in,gsize_t size)
56
644k
{
57
644k
  _TIFFmemcpy((tdata_t)out,(tdata_t)in,(tsize_t)size);
58
644k
}
59
60
void _GTIFFree(gdata_t data)
61
1.74M
{
62
1.74M
  if (data) _TIFFfree((tdata_t)data);
63
1.74M
}
64
65
66
67
/* returns the value of TIFF tag <tag>, or if
68
 * the value is an array, returns an allocated buffer
69
 * containing the values. Allocate a copy of the actual
70
 * buffer, sized up for updating.
71
 */
72
static int _GTIFGetField (tiff_t *tif, pinfo_t tag, int *count, void *val )
73
827k
{
74
827k
  int status;
75
827k
  unsigned short scount=0;
76
827k
  char *tmp;
77
827k
  const gsize_t size = _gtiff_size[_GTIFTagType (tif,tag)];
78
79
827k
  if (_GTIFTagType(tif,  tag) == TYPE_ASCII)
80
274k
  {
81
274k
    status = TIFFGetField((TIFF *)tif,tag,&tmp);
82
274k
    if (!status) return status;
83
133k
    scount = (unsigned short) (strlen(tmp)+1);
84
133k
  }
85
552k
  else status = TIFFGetField((TIFF *)tif,tag,&scount,&tmp);
86
686k
  if (!status) return status;
87
88
293k
  *count = scount;
89
90
293k
  char *value = (char *)_GTIFcalloc( (scount+MAX_VALUES)*size);
91
293k
  if (!value) return 0;
92
93
293k
  _TIFFmemcpy( value, tmp,  size * scount);
94
95
293k
  *(char **)val = value;
96
293k
  return status;
97
293k
}
98
99
/*
100
 * Set a GeoTIFF TIFF field.
101
 */
102
static int _GTIFSetField (tiff_t *tif, pinfo_t tag, int count, void *value )
103
605
{
104
605
  const unsigned short scount = (unsigned short) count;
105
106
605
  int status;
107
  /* libtiff ASCII uses null-delimiter */
108
605
  if (_GTIFTagType(tif,  tag) == TYPE_ASCII)
109
203
    status = TIFFSetField((TIFF *)tif,tag,value);
110
402
  else
111
402
    status = TIFFSetField((TIFF *)tif,tag,scount,value);
112
605
  return status;
113
605
}
114
115
116
/*
117
 *  This routine is supposed to return the TagType of the <tag>
118
 *  TIFF tag. Unfortunately, "libtiff" does not provide this
119
 *  service by default, so we just have to "know" what type of tags
120
 *  we've got, and how many. We only define the ones Geotiff
121
 *  uses here, and others return UNKNOWN. The "tif" parameter
122
 *  is provided for those TIFF implementations that provide
123
 *  for tag-type queries.
124
 */
125
static tagtype_t  _GTIFTagType  (tiff_t *tif, pinfo_t tag)
126
2.71M
{
127
2.71M
  (void) tif; /* dummy reference */
128
129
2.71M
  tagtype_t ttype;
130
2.71M
  switch (tag)
131
2.71M
  {
132
779k
    case GTIFF_ASCIIPARAMS:    ttype=TYPE_ASCII; break;
133
9
    case GTIFF_PIXELSCALE:
134
13
    case GTIFF_TRANSMATRIX:
135
36
    case GTIFF_TIEPOINTS:
136
652k
    case GTIFF_DOUBLEPARAMS:   ttype=TYPE_DOUBLE; break;
137
1.28M
    case GTIFF_GEOKEYDIRECTORY: ttype=TYPE_SHORT; break;
138
5.25k
    default: ttype = TYPE_UNKNOWN;
139
2.71M
  }
140
141
2.71M
  return ttype;
142
2.71M
}