1 : /******************************************************************************
2 : * $Id: tifvsi.cpp 24411 2012-05-13 20:16:08Z rouault $
3 : *
4 : * Project: GeoTIFF Driver
5 : * Purpose: Implement system hook functions for libtiff on top of CPL/VSI,
6 : * including > 2GB support. Based on tif_unix.c from libtiff
7 : * distribution.
8 : * Author: Frank Warmerdam, warmerdam@pobox.com
9 : *
10 : ******************************************************************************
11 : * Copyright (c) 2005, Frank Warmerdam, warmerdam@pobox.com
12 : *
13 : * Permission is hereby granted, free of charge, to any person obtaining a
14 : * copy of this software and associated documentation files (the "Software"),
15 : * to deal in the Software without restriction, including without limitation
16 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 : * and/or sell copies of the Software, and to permit persons to whom the
18 : * Software is furnished to do so, subject to the following conditions:
19 : *
20 : * The above copyright notice and this permission notice shall be included
21 : * in all copies or substantial portions of the Software.
22 : *
23 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 : * DEALINGS IN THE SOFTWARE.
30 : ****************************************************************************/
31 :
32 : /*
33 : * TIFF Library UNIX-specific Routines.
34 : */
35 : #include "cpl_vsi.h"
36 : #include "tifvsi.h"
37 :
38 : #include <errno.h>
39 :
40 : // We avoid including xtiffio.h since it drags in the libgeotiff version
41 : // of the VSI functions.
42 :
43 : #ifdef RENAME_INTERNAL_LIBGEOTIFF_SYMBOLS
44 : #include "gdal_libgeotiff_symbol_rename.h"
45 : #endif
46 :
47 : CPL_C_START
48 : extern TIFF CPL_DLL * XTIFFClientOpen(const char* name, const char* mode,
49 : thandle_t thehandle,
50 : TIFFReadWriteProc, TIFFReadWriteProc,
51 : TIFFSeekProc, TIFFCloseProc,
52 : TIFFSizeProc,
53 : TIFFMapFileProc, TIFFUnmapFileProc);
54 : CPL_C_END
55 :
56 : static tsize_t
57 118225 : _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
58 : {
59 118225 : return VSIFReadL( buf, 1, size, (VSILFILE *) fd );
60 : }
61 :
62 : static tsize_t
63 120889 : _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
64 : {
65 120889 : tsize_t nRet = VSIFWriteL( buf, 1, size, (VSILFILE *) fd );
66 120889 : if (nRet < size)
67 : {
68 0 : TIFFErrorExt( fd, "_tiffWriteProc", "%s", VSIStrerror( errno ) );
69 : }
70 120889 : return nRet;
71 : }
72 :
73 : static toff_t
74 190190 : _tiffSeekProc(thandle_t fd, toff_t off, int whence)
75 : {
76 190190 : if( VSIFSeekL( (VSILFILE *) fd, off, whence ) == 0 )
77 190190 : return (toff_t) VSIFTellL( (VSILFILE *) fd );
78 : else
79 : {
80 0 : TIFFErrorExt( fd, "_tiffSeekProc", "%s", VSIStrerror( errno ) );
81 0 : return (toff_t) -1;
82 : }
83 : }
84 :
85 : static int
86 8249 : _tiffCloseProc(thandle_t fd)
87 : {
88 8249 : return VSIFCloseL( (VSILFILE *) fd );
89 : }
90 :
91 : static toff_t
92 9912 : _tiffSizeProc(thandle_t fd)
93 : {
94 : vsi_l_offset old_off;
95 : toff_t file_size;
96 :
97 9912 : old_off = VSIFTellL( (VSILFILE *) fd );
98 9912 : VSIFSeekL( (VSILFILE *) fd, 0, SEEK_END );
99 :
100 9912 : file_size = (toff_t) VSIFTellL( (VSILFILE *) fd );
101 9912 : VSIFSeekL( (VSILFILE *) fd, old_off, SEEK_SET );
102 :
103 9912 : return file_size;
104 : }
105 :
106 : static int
107 6248 : _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
108 : {
109 : (void) fd; (void) pbase; (void) psize;
110 6248 : return (0);
111 : }
112 :
113 : static void
114 0 : _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
115 : {
116 : (void) fd; (void) base; (void) size;
117 0 : }
118 :
119 : /*
120 : * Open a TIFF file for read/writing.
121 : */
122 8267 : TIFF* VSI_TIFFOpen(const char* name, const char* mode)
123 : {
124 : static const char module[] = "TIFFOpen";
125 : int i, a_out;
126 : char access[32];
127 : VSILFILE *fp;
128 : TIFF *tif;
129 :
130 8267 : a_out = 0;
131 8267 : access[0] = '\0';
132 22533 : for( i = 0; mode[i] != '\0'; i++ )
133 : {
134 31603 : if( mode[i] == 'r'
135 7326 : || mode[i] == 'w'
136 5999 : || mode[i] == '+'
137 4012 : || mode[i] == 'a' )
138 : {
139 10254 : access[a_out++] = mode[i];
140 10254 : access[a_out] = '\0';
141 : }
142 : }
143 :
144 8267 : strcat( access, "b" );
145 :
146 8267 : fp = VSIFOpenL( name, access );
147 8267 : if (fp == NULL) {
148 18 : if( errno >= 0 )
149 18 : TIFFError(module,"%s: %s", name, VSIStrerror( errno ) );
150 : else
151 0 : TIFFError(module, "%s: Cannot open", name);
152 18 : return ((TIFF *)0);
153 : }
154 :
155 : tif = XTIFFClientOpen(name, mode,
156 : (thandle_t) fp,
157 : _tiffReadProc, _tiffWriteProc,
158 : _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
159 8249 : _tiffMapProc, _tiffUnmapProc);
160 :
161 8249 : if( tif == NULL )
162 0 : VSIFCloseL( fp );
163 :
164 8249 : return tif;
165 : }
|