1 : /******************************************************************************
2 : * $Id: tif_vsi.c 11957 2007-08-24 21:46:34Z warmerdam $
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) 2000, 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 "tiffiop.h"
36 : #include "cpl_vsi.h"
37 :
38 : static tsize_t
39 447 : _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
40 : {
41 447 : return VSIFReadL( buf, 1, size, (FILE *) fd );
42 : }
43 :
44 : static tsize_t
45 216 : _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
46 : {
47 216 : return VSIFWriteL( buf, 1, size, (FILE *) fd );
48 : }
49 :
50 : static toff_t
51 584 : _tiffSeekProc(thandle_t fd, toff_t off, int whence)
52 : {
53 584 : if( VSIFSeekL( (FILE *) fd, off, whence ) == 0 )
54 584 : return (toff_t) VSIFTellL( (FILE *) fd );
55 : else
56 0 : return (toff_t) -1;
57 : }
58 :
59 : static int
60 32 : _tiffCloseProc(thandle_t fd)
61 : {
62 32 : return VSIFCloseL( (FILE *) fd );
63 : }
64 :
65 : static toff_t
66 0 : _tiffSizeProc(thandle_t fd)
67 : {
68 : vsi_l_offset old_off;
69 : toff_t file_size;
70 :
71 0 : old_off = VSIFTellL( (FILE *) fd );
72 0 : VSIFSeekL( (FILE *) fd, 0, SEEK_END );
73 :
74 0 : file_size = (toff_t) VSIFTellL( (FILE *) fd );
75 0 : VSIFSeekL( (FILE *) fd, old_off, SEEK_SET );
76 :
77 0 : return file_size;
78 : }
79 :
80 : static int
81 0 : _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
82 : {
83 : (void) fd; (void) pbase; (void) psize;
84 0 : return (0);
85 : }
86 :
87 : static void
88 0 : _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
89 : {
90 : (void) fd; (void) base; (void) size;
91 0 : }
92 :
93 : /*
94 : * Open a TIFF file descriptor for read/writing.
95 : */
96 : TIFF*
97 0 : TIFFFdOpen(int fd, const char* name, const char* mode)
98 : {
99 0 : return NULL;
100 : }
101 :
102 : /*
103 : * Open a TIFF file for read/writing.
104 : */
105 : TIFF*
106 32 : TIFFOpen(const char* name, const char* mode)
107 : {
108 : static const char module[] = "TIFFOpen";
109 : int i, a_out;
110 : char access[32];
111 : FILE *fp;
112 : TIFF *tif;
113 :
114 32 : a_out = 0;
115 32 : access[0] = '\0';
116 102 : for( i = 0; mode[i] != '\0'; i++ )
117 : {
118 184 : if( mode[i] == 'r'
119 70 : || mode[i] == 'w'
120 38 : || mode[i] == '+'
121 6 : || mode[i] == 'a' )
122 : {
123 64 : access[a_out++] = mode[i];
124 64 : access[a_out] = '\0';
125 : }
126 : }
127 :
128 32 : strcat( access, "b" );
129 :
130 32 : fp = VSIFOpenL( name, access );
131 32 : if (fp == NULL) {
132 0 : if( errno >= 0 )
133 0 : TIFFError(module,"%s: %s", name, VSIStrerror( errno ) );
134 : else
135 0 : TIFFError(module, "%s: Cannot open", name);
136 0 : return ((TIFF *)0);
137 : }
138 :
139 32 : tif = TIFFClientOpen(name, mode,
140 : (thandle_t) fp,
141 : _tiffReadProc, _tiffWriteProc,
142 : _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
143 : _tiffMapProc, _tiffUnmapProc);
144 :
145 32 : if( tif != NULL )
146 32 : tif->tif_fd = 0;
147 : else
148 0 : VSIFCloseL( fp );
149 :
150 32 : return tif;
151 : }
152 :
153 : void*
154 40538 : _TIFFmalloc(tsize_t s)
155 : {
156 40538 : return VSIMalloc((size_t) s);
157 : }
158 :
159 : void
160 125872 : _TIFFfree(tdata_t p)
161 : {
162 125872 : VSIFree( p );
163 125872 : }
164 :
165 : void*
166 129284 : _TIFFrealloc(tdata_t p, tsize_t s)
167 : {
168 129284 : return VSIRealloc( p, s );
169 : }
170 :
171 : void
172 33901 : _TIFFmemset(void* p, int v, tmsize_t c)
173 : {
174 33901 : memset(p, v, (size_t) c);
175 33901 : }
176 :
177 : void
178 177273 : _TIFFmemcpy(void* d, const void* s, tmsize_t c)
179 : {
180 177273 : memcpy(d, s, (size_t) c);
181 177273 : }
182 :
183 : int
184 0 : _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
185 : {
186 0 : return (memcmp(p1, p2, (size_t) c));
187 : }
188 :
189 : static void
190 0 : unixWarningHandler(const char* module, const char* fmt, va_list ap)
191 : {
192 0 : if (module != NULL)
193 0 : fprintf(stderr, "%s: ", module);
194 0 : fprintf(stderr, "Warning, ");
195 0 : vfprintf(stderr, fmt, ap);
196 0 : fprintf(stderr, ".\n");
197 0 : }
198 : TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
199 :
200 : static void
201 0 : unixErrorHandler(const char* module, const char* fmt, va_list ap)
202 : {
203 0 : if (module != NULL)
204 0 : fprintf(stderr, "%s: ", module);
205 0 : vfprintf(stderr, fmt, ap);
206 0 : fprintf(stderr, ".\n");
207 0 : }
208 : TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
|