1 : /******************************************************************************
2 : * $Id: tifvsi.cpp 17674 2009-09-24 13:40:55Z chaitanya $
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 "tiffio.h"
37 :
38 : // We avoid including xtiffio.h since it drags in the libgeotiff version
39 : // of the VSI functions.
40 :
41 : CPL_C_START
42 : extern TIFF CPL_DLL * XTIFFClientOpen(const char* name, const char* mode,
43 : thandle_t thehandle,
44 : TIFFReadWriteProc, TIFFReadWriteProc,
45 : TIFFSeekProc, TIFFCloseProc,
46 : TIFFSizeProc,
47 : TIFFMapFileProc, TIFFUnmapFileProc);
48 : CPL_C_END
49 :
50 : static tsize_t
51 57973 : _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
52 : {
53 57973 : return VSIFReadL( buf, 1, size, (FILE *) fd );
54 : }
55 :
56 : static tsize_t
57 82772 : _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
58 : {
59 82772 : return VSIFWriteL( buf, 1, size, (FILE *) fd );
60 : }
61 :
62 : static toff_t
63 113096 : _tiffSeekProc(thandle_t fd, toff_t off, int whence)
64 : {
65 113096 : if( VSIFSeekL( (FILE *) fd, off, whence ) == 0 )
66 113096 : return (toff_t) VSIFTellL( (FILE *) fd );
67 : else
68 0 : return (toff_t) -1;
69 : }
70 :
71 : static int
72 3706 : _tiffCloseProc(thandle_t fd)
73 : {
74 3706 : return VSIFCloseL( (FILE *) fd );
75 : }
76 :
77 : static toff_t
78 3037 : _tiffSizeProc(thandle_t fd)
79 : {
80 : vsi_l_offset old_off;
81 : toff_t file_size;
82 :
83 3037 : old_off = VSIFTellL( (FILE *) fd );
84 3037 : VSIFSeekL( (FILE *) fd, 0, SEEK_END );
85 :
86 3037 : file_size = (toff_t) VSIFTellL( (FILE *) fd );
87 3037 : VSIFSeekL( (FILE *) fd, old_off, SEEK_SET );
88 :
89 3037 : return file_size;
90 : }
91 :
92 : static int
93 2655 : _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
94 : {
95 : (void) fd; (void) pbase; (void) psize;
96 2655 : return (0);
97 : }
98 :
99 : static void
100 0 : _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
101 : {
102 : (void) fd; (void) base; (void) size;
103 0 : }
104 :
105 : /*
106 : * Open a TIFF file for read/writing.
107 : */
108 3707 : TIFF* VSI_TIFFOpen(const char* name, const char* mode)
109 : {
110 : static const char module[] = "TIFFOpen";
111 : int i, a_out;
112 : char access[32];
113 : FILE *fp;
114 : TIFF *tif;
115 :
116 3707 : a_out = 0;
117 3707 : access[0] = '\0';
118 8487 : for( i = 0; mode[i] != '\0'; i++ )
119 : {
120 7785 : if( mode[i] == 'r'
121 1894 : || mode[i] == 'w'
122 1073 : || mode[i] == '+'
123 38 : || mode[i] == 'a' )
124 : {
125 4742 : access[a_out++] = mode[i];
126 4742 : access[a_out] = '\0';
127 : }
128 : }
129 :
130 3707 : strcat( access, "b" );
131 :
132 3707 : fp = VSIFOpenL( name, access );
133 3707 : if (fp == NULL) {
134 0 : if( errno >= 0 )
135 0 : TIFFError(module,"%s: %s", name, VSIStrerror( errno ) );
136 : else
137 0 : TIFFError(module, "%s: Cannot open", name);
138 0 : return ((TIFF *)0);
139 : }
140 :
141 : tif = XTIFFClientOpen(name, mode,
142 : (thandle_t) fp,
143 : _tiffReadProc, _tiffWriteProc,
144 : _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
145 3707 : _tiffMapProc, _tiffUnmapProc);
146 :
147 3707 : if( tif == NULL )
148 0 : VSIFCloseL( fp );
149 :
150 3707 : return tif;
151 : }
|