/src/gdal/frmts/gtiff/libtiff/tif_vsi.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /******************************************************************************  | 
2  |  |  *  | 
3  |  |  * Project:  GeoTIFF Driver  | 
4  |  |  * Purpose:  Implement system hook functions for libtiff on top of CPL/VSI,  | 
5  |  |  *           including > 2GB support.  Based on tif_unix.c from libtiff  | 
6  |  |  *           distribution.  | 
7  |  |  * Author:   Frank Warmerdam, warmerdam@pobox.com  | 
8  |  |  *  | 
9  |  |  ******************************************************************************  | 
10  |  |  * Copyright (c) 2000, Frank Warmerdam, warmerdam@pobox.com  | 
11  |  |  *  | 
12  |  |  * SPDX-License-Identifier: MIT  | 
13  |  |  ****************************************************************************/  | 
14  |  |  | 
15  |  | /*  | 
16  |  |  * TIFF Library UNIX-specific Routines.  | 
17  |  |  */  | 
18  |  | #include "tiffiop.h"  | 
19  |  | #include "cpl_vsi.h"  | 
20  |  |  | 
21  | 0  | CPL_INLINE static void CPL_IGNORE_RET_VAL_INT(CPL_UNUSED int unused) {} | 
22  |  |  | 
23  |  | static tsize_t  | 
24  |  | _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)  | 
25  | 0  | { | 
26  | 0  |     return VSIFReadL( buf, 1, size, (VSILFILE *) fd );  | 
27  | 0  | }  | 
28  |  |  | 
29  |  | static tsize_t  | 
30  |  | _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)  | 
31  | 0  | { | 
32  | 0  |     return VSIFWriteL( buf, 1, size, (VSILFILE *) fd );  | 
33  | 0  | }  | 
34  |  |  | 
35  |  | static toff_t  | 
36  |  | _tiffSeekProc(thandle_t fd, toff_t off, int whence)  | 
37  | 0  | { | 
38  | 0  |     if( VSIFSeekL( (VSILFILE *) fd, off, whence ) == 0 )  | 
39  | 0  |         return (toff_t) VSIFTellL( (VSILFILE *) fd );  | 
40  | 0  |     else  | 
41  | 0  |         return (toff_t) -1;  | 
42  | 0  | }  | 
43  |  |  | 
44  |  | static int  | 
45  |  | _tiffCloseProc(thandle_t fd)  | 
46  | 0  | { | 
47  | 0  |     return VSIFCloseL( (VSILFILE *) fd );  | 
48  | 0  | }  | 
49  |  |  | 
50  |  | static toff_t  | 
51  |  | _tiffSizeProc(thandle_t fd)  | 
52  | 0  | { | 
53  | 0  |     vsi_l_offset  old_off;  | 
54  | 0  |     toff_t        file_size;  | 
55  |  | 
  | 
56  | 0  |     old_off = VSIFTellL( (VSILFILE *) fd );  | 
57  | 0  |     CPL_IGNORE_RET_VAL_INT(VSIFSeekL( (VSILFILE *) fd, 0, SEEK_END ));  | 
58  |  | 
  | 
59  | 0  |     file_size = (toff_t) VSIFTellL( (VSILFILE *) fd );  | 
60  | 0  |     CPL_IGNORE_RET_VAL_INT(VSIFSeekL( (VSILFILE *) fd, old_off, SEEK_SET ));  | 
61  |  | 
  | 
62  | 0  |     return file_size;  | 
63  | 0  | }  | 
64  |  |  | 
65  |  | static int  | 
66  |  | _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)  | 
67  | 0  | { | 
68  | 0  |   (void) fd; (void) pbase; (void) psize;  | 
69  | 0  |   return (0);  | 
70  | 0  | }  | 
71  |  |  | 
72  |  | static void  | 
73  |  | _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)  | 
74  | 0  | { | 
75  | 0  |   (void) fd; (void) base; (void) size;  | 
76  | 0  | }  | 
77  |  |  | 
78  |  | /*  | 
79  |  |  * Open a TIFF file descriptor for read/writing.  | 
80  |  |  */  | 
81  |  | TIFF*  | 
82  |  | TIFFFdOpen(CPL_UNUSED int fd, CPL_UNUSED const char* name, CPL_UNUSED const char* mode)  | 
83  | 0  | { | 
84  | 0  |   return NULL;  | 
85  | 0  | }  | 
86  |  |  | 
87  |  | /*  | 
88  |  |  * Open a TIFF file for read/writing.  | 
89  |  |  */  | 
90  |  | TIFF*  | 
91  |  | TIFFOpen(const char* name, const char* mode)  | 
92  | 0  | { | 
93  | 0  |   static const char module[] = "TIFFOpen";  | 
94  | 0  |   int           i, a_out;  | 
95  | 0  |         char          szAccess[32];  | 
96  | 0  |         VSILFILE          *fp;  | 
97  | 0  |         TIFF          *tif;  | 
98  | 0  |         char         *pszAccess = szAccess;  | 
99  |  | 
  | 
100  | 0  |         a_out = 0;  | 
101  | 0  |         pszAccess[0] = '\0';  | 
102  | 0  |         for( i = 0; mode[i] != '\0'; i++ )  | 
103  | 0  |         { | 
104  | 0  |             if( mode[i] == 'r'  | 
105  | 0  |                 || mode[i] == 'w'  | 
106  | 0  |                 || mode[i] == '+'  | 
107  | 0  |                 || mode[i] == 'a' )  | 
108  | 0  |             { | 
109  | 0  |                 szAccess[a_out++] = mode[i];  | 
110  | 0  |                 szAccess[a_out] = '\0';  | 
111  | 0  |             }  | 
112  | 0  |         }  | 
113  |  | 
  | 
114  | 0  |         strcat( szAccess, "b" );  | 
115  |  | 
  | 
116  | 0  |         fp = VSIFOpenL( name, szAccess );  | 
117  | 0  |   if (fp == NULL) { | 
118  | 0  |             if( errno >= 0 )  | 
119  | 0  |                 TIFFError(module,"%s: %s", name, VSIStrerror( errno ) );  | 
120  | 0  |             else  | 
121  | 0  |     TIFFError(module, "%s: Cannot open", name);  | 
122  | 0  |             return ((TIFF *)0);  | 
123  | 0  |   }  | 
124  |  |  | 
125  | 0  |   tif = TIFFClientOpen(name, mode,  | 
126  | 0  |       (thandle_t) fp,  | 
127  | 0  |       _tiffReadProc, _tiffWriteProc,  | 
128  | 0  |       _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,  | 
129  | 0  |       _tiffMapProc, _tiffUnmapProc);  | 
130  |  | 
  | 
131  | 0  |         if( tif != NULL )  | 
132  | 0  |             tif->tif_fd = 0;  | 
133  | 0  |         else  | 
134  | 0  |             CPL_IGNORE_RET_VAL_INT(VSIFCloseL( fp ));  | 
135  |  |           | 
136  | 0  |   return tif;  | 
137  | 0  | }  | 
138  |  |  | 
139  |  | void*  | 
140  |  | _TIFFmalloc(tsize_t s)  | 
141  | 0  | { | 
142  | 0  |     return VSIMalloc((size_t) s);  | 
143  | 0  | }  | 
144  |  |  | 
145  |  | void* _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)  | 
146  | 0  | { | 
147  | 0  |     if( nmemb == 0 || siz == 0 )  | 
148  | 0  |         return ((void *) NULL);  | 
149  |  |  | 
150  | 0  |     return VSICalloc((size_t) nmemb, (size_t)siz);  | 
151  | 0  | }  | 
152  |  |  | 
153  |  | void  | 
154  |  | _TIFFfree(tdata_t p)  | 
155  | 0  | { | 
156  | 0  |     VSIFree( p );  | 
157  | 0  | }  | 
158  |  |  | 
159  |  | void*  | 
160  |  | _TIFFrealloc(tdata_t p, tsize_t s)  | 
161  | 0  | { | 
162  | 0  |     return VSIRealloc( p, s );  | 
163  | 0  | }  | 
164  |  |  | 
165  |  | void  | 
166  |  | _TIFFmemset(void* p, int v, tmsize_t c)  | 
167  | 0  | { | 
168  | 0  |   memset(p, v, (size_t) c);  | 
169  | 0  | }  | 
170  |  |  | 
171  |  | void  | 
172  |  | _TIFFmemcpy(void* d, const void* s, tmsize_t c)  | 
173  | 0  | { | 
174  | 0  |   memcpy(d, s, (size_t) c);  | 
175  | 0  | }  | 
176  |  |  | 
177  |  | int  | 
178  |  | _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)  | 
179  | 0  | { | 
180  | 0  |   return (memcmp(p1, p2, (size_t) c));  | 
181  | 0  | }  | 
182  |  |  | 
183  |  | static void  | 
184  |  | unixWarningHandler(const char* module, const char* fmt, va_list ap)  | 
185  | 0  | { | 
186  | 0  |   if (module != NULL)  | 
187  | 0  |     fprintf(stderr, "%s: ", module);  | 
188  | 0  |   fprintf(stderr, "Warning, ");  | 
189  | 0  |   vfprintf(stderr, fmt, ap);  | 
190  | 0  |   fprintf(stderr, ".\n");  | 
191  | 0  | }  | 
192  |  | TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;  | 
193  |  |  | 
194  |  | static void  | 
195  |  | unixErrorHandler(const char* module, const char* fmt, va_list ap)  | 
196  | 0  | { | 
197  | 0  |   if (module != NULL)  | 
198  | 0  |     fprintf(stderr, "%s: ", module);  | 
199  | 0  |   vfprintf(stderr, fmt, ap);  | 
200  | 0  |   fprintf(stderr, ".\n");  | 
201  | 0  | }  | 
202  |  | TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;  |