summaryrefslogtreecommitdiff
blob: 7f640a9c0a4a745bcc0942c534bfc1c07190b501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * Copyright (c) 1988-1996 Sam Leffler
 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */

/*
 * TIFF Library UNIX-specific Routines.
 */
#include "tiffiop.h"
#include <iostream>

#ifndef __VMS
using namespace std;
#endif

/*
  ISO C++ uses a 'std::streamsize' type to define counts.  This makes
  it similar to, (but perhaps not the same as) size_t.

  The std::ios::pos_type is used to represent stream positions as used
  by tellg(), tellp(), seekg(), and seekp().  This makes it similar to
  (but perhaps not the same as) 'off_t'.  The std::ios::streampos type
  is used for character streams, but is documented to not be an
  integral type anymore, so it should *not* be assigned to an integral
  type.

  The std::ios::off_type is used to specify relative offsets needed by
  the variants of seekg() and seekp() which accept a relative offset
  argument.

  Useful prototype knowledge:

  Obtain read position
    ios::pos_type basic_istream::tellg()

  Set read position
    basic_istream& basic_istream::seekg(ios::pos_type)
    basic_istream& basic_istream::seekg(ios::off_type, ios_base::seekdir)

  Read data
    basic_istream& istream::read(char *str, streamsize count)

  Number of characters read in last unformatted read
    streamsize istream::gcount();

  Obtain write position
    ios::pos_type basic_ostream::tellp()

  Set write position
    basic_ostream& basic_ostream::seekp(ios::pos_type)
    basic_ostream& basic_ostream::seekp(ios::off_type, ios_base::seekdir)

  Write data
    basic_ostream& ostream::write(const char *str, streamsize count)
*/

struct tiffis_data;
struct tiffos_data;

extern "C" {

	static tmsize_t _tiffosReadProc(thandle_t, void*, tmsize_t);
	static tmsize_t _tiffisReadProc(thandle_t fd, void* buf, tmsize_t size);
	static tmsize_t _tiffosWriteProc(thandle_t fd, void* buf, tmsize_t size);
	static tmsize_t _tiffisWriteProc(thandle_t, void*, tmsize_t);
	static uint64   _tiffosSeekProc(thandle_t fd, uint64 off, int whence);
	static uint64   _tiffisSeekProc(thandle_t fd, uint64 off, int whence);
	static uint64   _tiffosSizeProc(thandle_t fd);
	static uint64   _tiffisSizeProc(thandle_t fd);
	static int      _tiffosCloseProc(thandle_t fd);
	static int      _tiffisCloseProc(thandle_t fd);
	static int 	_tiffDummyMapProc(thandle_t , void** base, toff_t* size );
	static void     _tiffDummyUnmapProc(thandle_t , void* base, toff_t size );
	static TIFF*    _tiffStreamOpen(const char* name, const char* mode, void *fd);

struct tiffis_data
{
	istream	*stream;
        ios::pos_type start_pos;
};

struct tiffos_data
{
	ostream	*stream;
	ios::pos_type start_pos;
};

static tmsize_t
_tiffosReadProc(thandle_t, void*, tmsize_t)
{
        return 0;
}

static tmsize_t
_tiffisReadProc(thandle_t fd, void* buf, tmsize_t size)
{
        tiffis_data	*data = reinterpret_cast<tiffis_data *>(fd);

        // Verify that type does not overflow.
        streamsize request_size = size;
        if (static_cast<tmsize_t>(request_size) != size)
          return static_cast<tmsize_t>(-1);

        data->stream->read((char *) buf, request_size);

        return static_cast<tmsize_t>(data->stream->gcount());
}

static tmsize_t
_tiffosWriteProc(thandle_t fd, void* buf, tmsize_t size)
{
	tiffos_data	*data = reinterpret_cast<tiffos_data *>(fd);
	ostream		*os = data->stream;
	ios::pos_type	pos = os->tellp();

        // Verify that type does not overflow.
        streamsize request_size = size;
        if (static_cast<tmsize_t>(request_size) != size)
          return static_cast<tmsize_t>(-1);

	os->write(reinterpret_cast<const char *>(buf), request_size);

	return static_cast<tmsize_t>(os->tellp() - pos);
}

static tmsize_t
_tiffisWriteProc(thandle_t, void*, tmsize_t)
{
	return 0;
}

static uint64
_tiffosSeekProc(thandle_t fd, uint64 off, int whence)
{
	tiffos_data	*data = reinterpret_cast<tiffos_data *>(fd);
	ostream		*os = data->stream;

	// if the stream has already failed, don't do anything
	if( os->fail() )
		return static_cast<uint64>(-1);

	switch(whence) {
	case SEEK_SET:
		{
			// Compute 64-bit offset
			uint64 new_offset = static_cast<uint64>(data->start_pos) + off;

			// Verify that value does not overflow
			ios::off_type offset = static_cast<ios::off_type>(new_offset);
			if (static_cast<uint64>(offset) != new_offset)
				return static_cast<uint64>(-1);
			
			os->seekp(offset, ios::beg);
		break;
		}
	case SEEK_CUR:
		{
			// Verify that value does not overflow
			ios::off_type offset = static_cast<ios::off_type>(off);
			if (static_cast<uint64>(offset) != off)
				return static_cast<uint64>(-1);

			os->seekp(offset, ios::cur);
			break;
		}
	case SEEK_END:
		{
			// Verify that value does not overflow
			ios::off_type offset = static_cast<ios::off_type>(off);
			if (static_cast<uint64>(offset) != off)
				return static_cast<uint64>(-1);

			os->seekp(offset, ios::end);
			break;
		}
	}

	// Attempt to workaround problems with seeking past the end of the
	// stream.  ofstream doesn't have a problem with this but
	// ostrstream/ostringstream does. In that situation, add intermediate
	// '\0' characters.
	if( os->fail() ) {
#ifdef __VMS
		int		old_state;
#else
		ios::iostate	old_state;
#endif
		ios::pos_type	origin;

		old_state = os->rdstate();
		// reset the fail bit or else tellp() won't work below
		os->clear(os->rdstate() & ~ios::failbit);
		switch( whence ) {
			case SEEK_SET:
                        default:
				origin = data->start_pos;
				break;
			case SEEK_CUR:
				origin = os->tellp();
				break;
			case SEEK_END:
				os->seekp(0, ios::end);
				origin = os->tellp();
				break;
		}
		// restore original stream state
		os->clear(old_state);	

		// only do something if desired seek position is valid
		if( (static_cast<uint64>(origin) + off) > static_cast<uint64>(data->start_pos) ) {
			uint64	num_fill;

			// clear the fail bit 
			os->clear(os->rdstate() & ~ios::failbit);

			// extend the stream to the expected size
			os->seekp(0, ios::end);
			num_fill = (static_cast<uint64>(origin)) + off - os->tellp();
			for( uint64 i = 0; i < num_fill; i++ )
				os->put('\0');

			// retry the seek
			os->seekp(static_cast<ios::off_type>(static_cast<uint64>(origin) + off), ios::beg);
		}
	}

	return static_cast<uint64>(os->tellp());
}

static uint64
_tiffisSeekProc(thandle_t fd, uint64 off, int whence)
{
	tiffis_data	*data = reinterpret_cast<tiffis_data *>(fd);

	switch(whence) {
	case SEEK_SET:
		{
			// Compute 64-bit offset
			uint64 new_offset = static_cast<uint64>(data->start_pos) + off;
			
			// Verify that value does not overflow
			ios::off_type offset = static_cast<ios::off_type>(new_offset);
			if (static_cast<uint64>(offset) != new_offset)
				return static_cast<uint64>(-1);

			data->stream->seekg(offset, ios::beg);
			break;
		}
	case SEEK_CUR:
		{
			// Verify that value does not overflow
			ios::off_type offset = static_cast<ios::off_type>(off);
			if (static_cast<uint64>(offset) != off)
				return static_cast<uint64>(-1);

			data->stream->seekg(offset, ios::cur);
			break;
		}
	case SEEK_END:
		{
			// Verify that value does not overflow
			ios::off_type offset = static_cast<ios::off_type>(off);
			if (static_cast<uint64>(offset) != off)
				return static_cast<uint64>(-1);

			data->stream->seekg(offset, ios::end);
			break;
		}
	}

	return (uint64) (data->stream->tellg() - data->start_pos);
}

static uint64
_tiffosSizeProc(thandle_t fd)
{
	tiffos_data	*data = reinterpret_cast<tiffos_data *>(fd);
	ostream		*os = data->stream;
	ios::pos_type	pos = os->tellp();
	ios::pos_type	len;

	os->seekp(0, ios::end);
	len = os->tellp();
	os->seekp(pos);

	return (uint64) len;
}

static uint64
_tiffisSizeProc(thandle_t fd)
{
	tiffis_data	*data = reinterpret_cast<tiffis_data *>(fd);
	ios::pos_type	pos = data->stream->tellg();
	ios::pos_type	len;

	data->stream->seekg(0, ios::end);
	len = data->stream->tellg();
	data->stream->seekg(pos);

	return (uint64) len;
}

static int
_tiffosCloseProc(thandle_t fd)
{
	// Our stream was not allocated by us, so it shouldn't be closed by us.
	delete reinterpret_cast<tiffos_data *>(fd);
	return 0;
}

static int
_tiffisCloseProc(thandle_t fd)
{
	// Our stream was not allocated by us, so it shouldn't be closed by us.
	delete reinterpret_cast<tiffis_data *>(fd);
	return 0;
}

static int
_tiffDummyMapProc(thandle_t , void** base, toff_t* size )
{
	(void) base;
	(void) size;
	return (0);
}

static void
_tiffDummyUnmapProc(thandle_t , void* base, toff_t size )
{
	(void) base;
	(void) size;
}

/*
 * Open a TIFF file descriptor for read/writing.
 */
static TIFF*
_tiffStreamOpen(const char* name, const char* mode, void *fd)
{
	TIFF*	tif;

	if( strchr(mode, 'w') ) {
		tiffos_data	*data = new tiffos_data;
		data->stream = reinterpret_cast<ostream *>(fd);
		data->start_pos = data->stream->tellp();

		// Open for writing.
		tif = TIFFClientOpen(name, mode,
				reinterpret_cast<thandle_t>(data),
				_tiffosReadProc,
                                _tiffosWriteProc,
				_tiffosSeekProc,
                                _tiffosCloseProc,
				_tiffosSizeProc,
				_tiffDummyMapProc,
                                _tiffDummyUnmapProc);
		if (!tif) {
			delete data;
		}
	} else {
		tiffis_data	*data = new tiffis_data;
		data->stream = reinterpret_cast<istream *>(fd);
		data->start_pos = data->stream->tellg();
		// Open for reading.
		tif = TIFFClientOpen(name, mode,
				reinterpret_cast<thandle_t>(data),
				_tiffisReadProc,
                                _tiffisWriteProc,
				_tiffisSeekProc,
                                _tiffisCloseProc,
				_tiffisSizeProc,
				_tiffDummyMapProc,
                                _tiffDummyUnmapProc);
		if (!tif) {
			delete data;
		}
	}

	return (tif);
}

} /* extern "C" */

TIFF*
TIFFStreamOpen(const char* name, ostream *os)
{
	// If os is either a ostrstream or ostringstream, and has no data
	// written to it yet, then tellp() will return -1 which will break us.
	// We workaround this by writing out a dummy character and
	// then seek back to the beginning.
	if( !os->fail() && static_cast<int>(os->tellp()) < 0 ) {
		*os << '\0';
		os->seekp(0);
	}

	// NB: We don't support mapped files with streams so add 'm'
	return _tiffStreamOpen(name, "wm", os);
}

TIFF*
TIFFStreamOpen(const char* name, istream *is)
{
	// NB: We don't support mapped files with streams so add 'm'
	return _tiffStreamOpen(name, "rm", is);
}

/* vim: set ts=8 sts=8 sw=8 noet: */
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 8
 * fill-column: 78
 * End:
 */