connection.h 14.5 KB
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 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
/* A byte source/sink .. it can be a pipe, socket, or perhaps a node.js stream.
 *
 * J.Cupitt, 19/6/14
 */

/*

    This file is part of VIPS.

    VIPS is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

#ifndef VIPS_CONNECTION_H
#define VIPS_CONNECTION_H

#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/

#define VIPS_TYPE_CONNECTION (vips_connection_get_type())
#define VIPS_CONNECTION( obj ) \
	(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
	VIPS_TYPE_CONNECTION, VipsConnection ))
#define VIPS_CONNECTION_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_CAST( (klass), \
	VIPS_TYPE_CONNECTION, VipsConnectionClass))
#define VIPS_IS_CONNECTION( obj ) \
	(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_CONNECTION ))
#define VIPS_IS_CONNECTION_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_CONNECTION ))
#define VIPS_CONNECTION_GET_CLASS( obj ) \
	(G_TYPE_INSTANCE_GET_CLASS( (obj), \
	VIPS_TYPE_CONNECTION, VipsConnectionClass ))

/* Communicate with something like a socket or pipe. 
 */
typedef struct _VipsConnection {
	VipsObject parent_object;

	/*< private >*/

	/* Read/write this fd if connected to a system pipe/socket. Override
	 * ::read() and ::write() to do something else.
	 */
	int descriptor;	

	/* A descriptor we close with vips_tracked_close().
	 */
	int tracked_descriptor;	

	/* A descriptor we close with close().
	 */
	int close_descriptor;	

	/* If descriptor is a file, the filename we opened. Handy for error
	 * messages. 
	 */
	char *filename; 

} VipsConnection;

typedef struct _VipsConnectionClass {
	VipsObjectClass parent_class;

} VipsConnectionClass;

GType vips_connection_get_type( void );

const char *vips_connection_filename( VipsConnection *connection );
const char *vips_connection_nick( VipsConnection *connection );

void vips_pipe_read_limit_set( gint64 limit );

#define VIPS_TYPE_SOURCE (vips_source_get_type())
#define VIPS_SOURCE( obj ) \
	(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
	VIPS_TYPE_SOURCE, VipsSource ))
#define VIPS_SOURCE_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_CAST( (klass), \
	VIPS_TYPE_SOURCE, VipsSourceClass))
#define VIPS_IS_SOURCE( obj ) \
	(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_SOURCE ))
#define VIPS_IS_SOURCE_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_SOURCE ))
#define VIPS_SOURCE_GET_CLASS( obj ) \
	(G_TYPE_INSTANCE_GET_CLASS( (obj), \
	VIPS_TYPE_SOURCE, VipsSourceClass ))

/* Read from something like a socket, file or memory area and present the data
 * with a unified seek / read / map interface.
 *
 * During the header phase, we save data from unseekable sources in a buffer
 * so readers can rewind and read again. We don't buffer data during the
 * decode stage.
 */
typedef struct _VipsSource {
	VipsConnection parent_object;

	/* We have two phases: 
	 *
	 * During the header phase, we save bytes read from the input (if this
	 * is an unseekable source) so that we can rewind and try again, if
	 * necessary.
	 *
	 * Once we reach decode phase, we no longer support rewind and the
	 * buffer of saved data is discarded.
	 */
	gboolean decode;

	/* TRUE if this input is something like a pipe. These don't support
	 * seek or map -- all you can do is read() bytes sequentially.
	 *
	 * If you attempt to map or get the size of a pipe-style input, it'll 
	 * get read entirely into memory. Seeks will cause read up to the seek
	 * point.
	 */
	gboolean have_tested_seek;
	gboolean is_pipe;

	/* The current read point and length.
	 *
	 * length is -1 for is_pipe sources.
	 *
	 * off_t can be 32 bits on some platforms, so make sure we have a 
	 * full 64.
	 */
	gint64 read_position;
	gint64 length;

	/*< private >*/

	/* For sources where we have the whole image in memory (from a memory
	 * buffer, from mmaping the file, from reading the pipe into memory), 
	 * a pointer to the start.
	 */
	const void *data;

	/* For is_pipe sources, save data read during header phase here. If 
	 * we rewind and try again, serve data from this until it runs out.
	 *
	 * If we need to force the whole pipe into memory, read everything to
	 * this and put a copy of the pointer in data.
	 */
	GByteArray *header_bytes;

	/* Save the first few bytes here for file type sniffing.
	 */
	GByteArray *sniff;

	/* For a memory source, the blob we read from. 
	 */
	VipsBlob *blob;

	/* If we mmaped the file, what we need to unmmap on finalize.
	 */
	void *mmap_baseaddr;
	size_t mmap_length;

} VipsSource;

typedef struct _VipsSourceClass {
	VipsConnectionClass parent_class;

	/* Subclasses can define these to implement other source methods.
	 */

	/* Read from the source into the supplied buffer, args exactly as
	 * read(2). Set errno on error.
	 *
	 * We must return gint64, since ssize_t is often defined as unsigned
	 * on Windows.
	 */
	gint64 (*read)( VipsSource *, void *, size_t );

	/* Seek to a certain position, args exactly as lseek(2). Set errno on
	 * error.
	 *
	 * Unseekable sources should always return -1. VipsSource will then
	 * seek by _read()ing bytes into memory as required.
	 *
	 * We have to use int64 rather than off_t, since we must work on
	 * Windows, where off_t can be 32-bits.
	 */
	gint64 (*seek)( VipsSource *, gint64, int );

} VipsSourceClass;

GType vips_source_get_type( void );

VipsSource *vips_source_new_from_descriptor( int descriptor );
VipsSource *vips_source_new_from_file( const char *filename );
VipsSource *vips_source_new_from_blob( VipsBlob *blob );
VipsSource *vips_source_new_from_memory( const void *data, size_t size );
VipsSource *vips_source_new_from_options( const char *options );

void vips_source_minimise( VipsSource *source );
int vips_source_unminimise( VipsSource *source );
int vips_source_decode( VipsSource *source );
gint64 vips_source_read( VipsSource *source, void *data, size_t length );
gboolean vips_source_is_mappable( VipsSource *source );
const void *vips_source_map( VipsSource *source, size_t *length );
VipsBlob *vips_source_map_blob( VipsSource *source );
gint64 vips_source_seek( VipsSource *source, gint64 offset, int whence );
int vips_source_rewind( VipsSource *source );
gint64 vips_source_sniff_at_most( VipsSource *source, 
	unsigned char **data, size_t length );
unsigned char *vips_source_sniff( VipsSource *source, size_t length );
gint64 vips_source_length( VipsSource *source ); 

#define VIPS_TYPE_SOURCE_CUSTOM (vips_source_custom_get_type())
#define VIPS_SOURCE_CUSTOM( obj ) \
	(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
	VIPS_TYPE_SOURCE_CUSTOM, VipsSourceCustom ))
#define VIPS_SOURCE_CUSTOM_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_CAST( (klass), \
	VIPS_TYPE_SOURCE_CUSTOM, VipsSourceCustomClass))
#define VIPS_IS_SOURCE_CUSTOM( obj ) \
	(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_SOURCE_CUSTOM ))
#define VIPS_IS_SOURCE_CUSTOM_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_SOURCE_CUSTOM ))
#define VIPS_SOURCE_CUSTOM_GET_CLASS( obj ) \
	(G_TYPE_INSTANCE_GET_CLASS( (obj), \
	VIPS_TYPE_SOURCE_CUSTOM, VipsSourceCustomClass ))

/* Subclass of source_custom with signals for handlers. This is supposed to be
 * useful for language bindings.
 */
typedef struct _VipsSourceCustom {
	VipsSource parent_object;

} VipsSourceCustom;

typedef struct _VipsSourceCustomClass {
	VipsSourceClass parent_class;

	/* The action signals clients can use to implement read and seek.
	 * We must use gint64 everywhere since there's no G_TYPE_SIZE.
	 */

	gint64 (*read)( VipsSourceCustom *, void *, gint64 );
	gint64 (*seek)( VipsSourceCustom *, gint64, int );

} VipsSourceCustomClass;

GType vips_source_custom_get_type( void );
VipsSourceCustom *vips_source_custom_new( void );

/* A GInputStream that wraps a VipsSource. This lets us eg. 
 * hook librsvg up to libvips using their GInputStream interface.
 */

#define VIPS_TYPE_G_INPUT_STREAM (vips_g_input_stream_get_type())
#define VIPS_G_INPUT_STREAM( obj ) \
	(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
	VIPS_TYPE_G_INPUT_STREAM, VipsGInputStream ))
#define VIPS_G_INPUT_STREAM_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_CAST( (klass), \
	VIPS_TYPE_G_INPUT_STREAM, VipsGInputStreamClass))
#define VIPS_IS_G_INPUT_STREAM( obj ) \
	(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_G_INPUT_STREAM ))
#define VIPS_IS_G_INPUT_STREAM_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_G_INPUT_STREAM ))
#define VIPS_G_INPUT_STREAM_GET_CLASS( obj ) \
	(G_TYPE_INSTANCE_GET_CLASS( (obj), \
	VIPS_TYPE_G_INPUT_STREAM, VipsGInputStreamClass ))

typedef struct _VipsGInputStream {
	GInputStream parent_instance;

	/*< private >*/

	/* The VipsSource we wrap.
	 */
	VipsSource *source;

} VipsGInputStream;

typedef struct _VipsGInputStreamClass {
	GInputStreamClass parent_class;

} VipsGInputStreamClass;

GInputStream *vips_g_input_stream_new_from_source( VipsSource *source );

/* A VipsSource that wraps a GInputStream. This lets us eg. load PNGs from 
 * GFile objects.
 */

#define VIPS_TYPE_SOURCE_G_INPUT_STREAM (vips_source_g_input_stream_get_type())
#define VIPS_SOURCE_G_INPUT_STREAM( obj ) \
	(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
	VIPS_TYPE_SOURCE_G_INPUT_STREAM, VipsSourceGInputStream ))
#define VIPS_SOURCE_G_INPUT_STREAM_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_CAST( (klass), \
	VIPS_TYPE_SOURCE_G_INPUT_STREAM, VipsSourceGInputStreamClass))
#define VIPS_IS_SOURCE_G_INPUT_STREAM( obj ) \
	(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_SOURCE_G_INPUT_STREAM ))
#define VIPS_IS_SOURCE_G_INPUT_STREAM_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_SOURCE_G_INPUT_STREAM ))
#define VIPS_SOURCE_G_INPUT_STREAM_GET_CLASS( obj ) \
	(G_TYPE_INSTANCE_GET_CLASS( (obj), \
	VIPS_TYPE_SOURCE_G_INPUT_STREAM, VipsSourceGInputStreamClass ))

typedef struct _VipsSourceGInputStream {
	VipsSource parent_instance;

	/*< private >*/

	/* The GInputStream we wrap.
	 */
	GInputStream *stream;

	GSeekable *seekable;
	GFileInfo *info;

} VipsSourceGInputStream;

typedef struct _VipsSourceGInputStreamClass {
	VipsSourceClass parent_class;

} VipsSourceGInputStreamClass;

VipsSourceGInputStream *vips_source_g_input_stream_new( GInputStream *stream );

#define VIPS_TYPE_TARGET (vips_target_get_type())
#define VIPS_TARGET( obj ) \
	(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
	VIPS_TYPE_TARGET, VipsTarget ))
#define VIPS_TARGET_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_CAST( (klass), \
	VIPS_TYPE_TARGET, VipsTargetClass))
#define VIPS_IS_TARGET( obj ) \
	(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_TARGET ))
#define VIPS_IS_TARGET_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_TARGET ))
#define VIPS_TARGET_GET_CLASS( obj ) \
	(G_TYPE_INSTANCE_GET_CLASS( (obj), \
	VIPS_TYPE_TARGET, VipsTargetClass ))

/* PNG writes in 8kb chunks, so we need to be a little larger than that.
 */
#define VIPS_TARGET_BUFFER_SIZE (8500)

/* Output to something like a socket, pipe or memory area. 
 */
typedef struct _VipsTarget {
	VipsConnection parent_object;

	/*< private >*/

	/* This target should write to memory.
	 */
	gboolean memory;

	/* The target has been finished and can no longer be written.
	 */
	gboolean finished;

	/* Write memory output here.
	 */
	GByteArray *memory_buffer;

	/* And return memory via this blob.
	 */
	VipsBlob *blob;

	/* Buffer small writes here. write_point is the index of the next
	 * character to write.
	 */
	unsigned char output_buffer[VIPS_TARGET_BUFFER_SIZE];
	int write_point;

} VipsTarget;

typedef struct _VipsTargetClass {
	VipsConnectionClass parent_class;

	/* Write to output. Args exactly as write(2).
	 *
	 * We must return gint64, since ssize_t is often defined as unsigned
	 * on Windows.
	 */
	gint64 (*write)( VipsTarget *, const void *, size_t );

	/* Output has been generated, so do any clearing up,
	 * eg. copy the bytes we saved in memory to the target blob.
	 */
	void (*finish)( VipsTarget * );

} VipsTargetClass;

GType vips_target_get_type( void );

VipsTarget *vips_target_new_to_descriptor( int descriptor );
VipsTarget *vips_target_new_to_file( const char *filename );
VipsTarget *vips_target_new_to_memory( void );
int vips_target_write( VipsTarget *target, const void *data, size_t length );
void vips_target_finish( VipsTarget *target );
unsigned char *vips_target_steal( VipsTarget *target, size_t *length );
char *vips_target_steal_text( VipsTarget *target );

int vips_target_putc( VipsTarget *target, int ch );
#define VIPS_TARGET_PUTC( S, C ) ( \
	(S)->write_point < VIPS_TARGET_BUFFER_SIZE ? \
	((S)->output_buffer[(S)->write_point++] = (C), 0) : \
	vips_target_putc( (S), (C) ) \
)
int vips_target_writes( VipsTarget *target, const char *str );
int vips_target_writef( VipsTarget *target, const char *fmt, ... )
	__attribute__((format(printf, 2, 3)));
int vips_target_write_amp( VipsTarget *target, const char *str );

#define VIPS_TYPE_TARGET_CUSTOM (vips_target_custom_get_type())
#define VIPS_TARGET_CUSTOM( obj ) \
	(G_TYPE_CHECK_INSTANCE_CAST( (obj), \
	VIPS_TYPE_TARGET_CUSTOM, VipsTargetCustom ))
#define VIPS_TARGET_CUSTOM_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_CAST( (klass), \
	VIPS_TYPE_TARGET_CUSTOM, VipsTargetCustomClass))
#define VIPS_IS_TARGET_CUSTOM( obj ) \
	(G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_TARGET_CUSTOM ))
#define VIPS_IS_TARGET_CUSTOM_CLASS( klass ) \
	(G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_TARGET_CUSTOM ))
#define VIPS_TARGET_CUSTOM_GET_CLASS( obj ) \
	(G_TYPE_INSTANCE_GET_CLASS( (obj), \
	VIPS_TYPE_TARGET_CUSTOM, VipsTargetCustomClass ))

#define VIPS_TARGET_CUSTOM_BUFFER_SIZE (4096)

/* Output to something like a socket, pipe or memory area. 
 */
typedef struct _VipsTargetCustom {
	VipsTarget parent_object;

} VipsTargetCustom;

typedef struct _VipsTargetCustomClass {
	VipsTargetClass parent_class;

	/* The action signals clients can use to implement write and finish.
	 * We must use gint64 everywhere since there's no G_TYPE_SIZE.
	 */

	gint64 (*write)( VipsTargetCustom *, const void *, gint64 );
	void (*finish)( VipsTargetCustom * );

} VipsTargetCustomClass;

GType vips_target_custom_get_type( void );
VipsTargetCustom *vips_target_custom_new( void );

#ifdef __cplusplus
}
#endif /*__cplusplus*/

#endif /*VIPS_CONNECTION_H*/