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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
|
/* A byte source/sink .. it can be a pipe, file descriptor, memory area,
* socket, node.js stream, etc.
*
* J.Cupitt, 19/6/14
*
* 26/11/20
* - use _setmode() on win to force binary write for previously opened
* descriptors
*/
/*
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
*/
/*
#define VIPS_DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /*HAVE_UNISTD_H*/
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <vips/vips.h>
#ifdef G_OS_WIN32
#include <io.h>
#endif /*G_OS_WIN32*/
#include <vips/debug.h>
#include <vips/internal.h>
/* libtiff needs to be able to seek and read back output files, unfortunately,
* so we must open read-write.
*/
#define MODE_READWRITE CLOEXEC (BINARYIZE (O_RDWR | O_CREAT | O_TRUNC))
G_DEFINE_TYPE( VipsTarget, vips_target, VIPS_TYPE_CONNECTION );
static void
vips_target_finalize( GObject *gobject )
{
VipsTarget *target = VIPS_TARGET( gobject );
VIPS_DEBUG_MSG( "vips_target_finalize:\n" );
if( target->memory_buffer ) {
g_string_free( target->memory_buffer, TRUE );
target->memory_buffer = NULL;
}
if( target->blob ) {
vips_area_unref( VIPS_AREA( target->blob ) );
target->blob = NULL;
}
if( target->delete_on_close &&
target->delete_on_close_filename )
g_unlink( target->delete_on_close_filename );
VIPS_FREE( target->delete_on_close_filename );
G_OBJECT_CLASS( vips_target_parent_class )->finalize( gobject );
}
static int
vips_target_build( VipsObject *object )
{
VipsConnection *connection = VIPS_CONNECTION( object );
VipsTarget *target = VIPS_TARGET( object );
VIPS_DEBUG_MSG( "vips_target_build: %p\n", connection );
if( VIPS_OBJECT_CLASS( vips_target_parent_class )->build( object ) )
return( -1 );
if( vips_object_argument_isset( object, "filename" ) &&
vips_object_argument_isset( object, "descriptor" ) ) {
vips_error( vips_connection_nick( connection ),
"%s", _( "don't set 'filename' and 'descriptor'" ) );
return( -1 );
}
if( connection->filename ) {
const char *filename = connection->filename;
int fd;
/* 0644 is rw user, r group and other.
*/
if( (fd = vips_tracked_open( filename,
MODE_READWRITE, 0644 )) == -1 ) {
vips_error_system( errno,
vips_connection_nick( connection ),
"%s", _( "unable to open for write" ) );
return( -1 );
}
connection->tracked_descriptor = fd;
connection->descriptor = fd;
}
else if( vips_object_argument_isset( object, "descriptor" ) ) {
connection->descriptor = dup( connection->descriptor );
connection->close_descriptor = connection->descriptor;
#ifdef G_OS_WIN32
/* Windows will create eg. stdin and stdout in text mode.
* We always write in binary mode.
*/
_setmode( connection->descriptor, _O_BINARY );
#endif /*G_OS_WIN32*/
}
else if( target->memory )
target->memory_buffer =
g_string_sized_new( VIPS_TARGET_BUFFER_SIZE );
return( 0 );
}
static gint64
vips_target_write_real( VipsTarget *target, const void *data, size_t length )
{
VipsConnection *connection = VIPS_CONNECTION( target );
gint64 result;
VIPS_DEBUG_MSG( "vips_target_write_real: %zd bytes\n", length );
if( target->memory_buffer ) {
VIPS_DEBUG_MSG( "vips_target_write_real: to position %zd\n",
target->position );
g_string_overwrite_len( target->memory_buffer, target->position,
data, length );
target->position += length;
result = length;
}
else
result = write( connection->descriptor, data, length );
return( result );
}
static off_t
vips_target_seek_real( VipsTarget *target, off_t offset, int whence )
{
VipsConnection *connection = VIPS_CONNECTION( target );
const char *nick = vips_connection_nick( connection );
off_t new_position;
VIPS_DEBUG_MSG( "vips_target_seek_real: offset = %ld, whence = %d\n",
offset, whence );
if( target->memory_buffer ) {
switch( whence ) {
case SEEK_SET:
new_position = offset;
break;
case SEEK_CUR:
new_position = target->position + offset;
break;
case SEEK_END:
new_position = target->memory_buffer->len + offset;
break;
default:
vips_error( nick, "%s", _( "bad 'whence'" ) );
return( -1 );
}
if( new_position > target->memory_buffer->len )
g_string_set_size( target->memory_buffer,
new_position );
target->position = new_position;
}
else
new_position = lseek( connection->descriptor, offset, whence );
return( new_position );
}
static gint64
vips_target_read_real( VipsTarget *target, void *data, size_t length )
{
gint64 bytes_read;
VIPS_DEBUG_MSG( "vips_target_read_real: %zd bytes\n", length );
if( target->memory_buffer ) {
bytes_read = VIPS_MIN( length,
target->memory_buffer->len - target->position );
VIPS_DEBUG_MSG( " %zd bytes from memory\n", bytes_read );
memcpy( data,
target->memory_buffer->str +
target->position,
bytes_read );
target->position += bytes_read;
}
else {
VipsConnection *connection = VIPS_CONNECTION( target );
int fd = connection->descriptor;
do {
bytes_read = read( fd, data, length );
} while( bytes_read < 0 && errno == EINTR );
}
VIPS_DEBUG_MSG( " read %zd bytes\n", bytes_read );
return( bytes_read );
}
static int
vips_target_end_real( VipsTarget *target )
{
VIPS_DEBUG_MSG( "vips_target_finish_real:\n" );
return( 0 );
}
static void
vips_target_finish_real( VipsTarget *target )
{
VIPS_DEBUG_MSG( "vips_target_finish_real:\n" );
}
static void
vips_target_class_init( VipsTargetClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = VIPS_OBJECT_CLASS( class );
gobject_class->finalize = vips_target_finalize;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "target";
object_class->description = _( "Target" );
object_class->build = vips_target_build;
class->write = vips_target_write_real;
class->read = vips_target_read_real;
class->seek = vips_target_seek_real;
class->end = vips_target_end_real;
class->finish = vips_target_finish_real;
VIPS_ARG_BOOL( class, "memory", 3,
_( "Memory" ),
_( "File descriptor should output to memory" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsTarget, memory ),
FALSE );
/* SET_ALWAYS means that blob is set by C and the obj system is not
* involved in creation or destruction. It can be read at any time.
*/
VIPS_ARG_BOXED( class, "blob", 4,
_( "Blob" ),
_( "Blob to save to" ),
VIPS_ARGUMENT_SET_ALWAYS,
G_STRUCT_OFFSET( VipsTarget, blob ),
VIPS_TYPE_BLOB );
}
static void
vips_target_init( VipsTarget *target )
{
target->blob = vips_blob_new( NULL, NULL, 0 );
target->write_point = 0;
}
/**
* vips_target_new_to_descriptor:
* @descriptor: write to this file descriptor
*
* Create a target attached to a file descriptor.
* @descriptor is kept open until the target is finalized.
*
* See also: vips_target_new_to_file().
*
* Returns: a new target.
*/
VipsTarget *
vips_target_new_to_descriptor( int descriptor )
{
VipsTarget *target;
VIPS_DEBUG_MSG( "vips_target_new_to_descriptor: %d\n",
descriptor );
target = VIPS_TARGET( g_object_new( VIPS_TYPE_TARGET,
"descriptor", descriptor,
NULL ) );
if( vips_object_build( VIPS_OBJECT( target ) ) ) {
VIPS_UNREF( target );
return( NULL );
}
return( target );
}
/**
* vips_target_new_to_file:
* @filename: write to this file
*
* Create a target attached to a file.
*
* Returns: a new target.
*/
VipsTarget *
vips_target_new_to_file( const char *filename )
{
VipsTarget *target;
VIPS_DEBUG_MSG( "vips_target_new_to_file: %s\n",
filename );
target = VIPS_TARGET( g_object_new( VIPS_TYPE_TARGET,
"filename", filename,
NULL ) );
if( vips_object_build( VIPS_OBJECT( target ) ) ) {
VIPS_UNREF( target );
return( NULL );
}
return( target );
}
/**
* vips_target_new_to_memory:
*
* Create a target which will write to a memory area. Read from @blob to get
* memory.
*
* See also: vips_target_new_to_file().
*
* Returns: a new #VipsConnection
*/
VipsTarget *
vips_target_new_to_memory( void )
{
VipsTarget *target;
VIPS_DEBUG_MSG( "vips_target_new_to_memory:\n" );
target = VIPS_TARGET( g_object_new( VIPS_TYPE_TARGET,
"memory", TRUE,
NULL ) );
if( vips_object_build( VIPS_OBJECT( target ) ) ) {
VIPS_UNREF( target );
return( NULL );
}
return( target );
}
/**
* vips_target_new_temp:
* @based_on: base the temporary target on this target
*
* Create a temporary target -- either a temporary file on disc, or an area in
* memory, depending on what sort of target @based_on is.
*
* See also: vips_target_new_to_file().
*
* Returns: a new target.
*/
VipsTarget *
vips_target_new_temp( VipsTarget *based_on )
{
VipsTarget *target;
VIPS_DEBUG_MSG( "vips_target_new_temp: %p\n", based_on );
if( vips_connection_filename( VIPS_CONNECTION( based_on ) ) ) {
int descriptor;
char *filename;
if( !(filename = vips__temp_name( "%s.target" )) )
return( NULL );
if( (descriptor =
vips__open_image_write( filename, TRUE )) < 0 ) {
g_free( filename );
return( NULL );
}
if( !(target = vips_target_new_to_descriptor( descriptor )) ) {
g_free( filename );
vips_tracked_close( descriptor );
return( NULL );
}
vips_tracked_close( descriptor );
target->delete_on_close = TRUE;
target->delete_on_close_filename = filename;
}
else
target = vips_target_new_to_memory();
return( target );
}
static int
vips_target_write_unbuffered( VipsTarget *target,
const void *data, size_t length )
{
VipsTargetClass *class = VIPS_TARGET_GET_CLASS( target );
VIPS_DEBUG_MSG( "vips_target_write_unbuffered:\n" );
if( target->ended )
return( 0 );
while( length > 0 ) {
gint64 bytes_written;
bytes_written = class->write( target, data, length );
/* n == 0 isn't strictly an error, but we treat it as
* one to make sure we don't get stuck in this loop.
*/
if( bytes_written <= 0 ) {
vips_error_system( errno,
vips_connection_nick(
VIPS_CONNECTION( target ) ),
"%s", _( "write error" ) );
return( -1 );
}
length -= bytes_written;
data += bytes_written;
}
return( 0 );
}
static int
vips_target_flush( VipsTarget *target )
{
g_assert( target->write_point >= 0 );
g_assert( target->write_point <= VIPS_TARGET_BUFFER_SIZE );
VIPS_DEBUG_MSG( "vips_target_flush:\n" );
if( target->write_point > 0 ) {
if( vips_target_write_unbuffered( target,
target->output_buffer, target->write_point ) )
return( -1 );
target->write_point = 0;
}
return( 0 );
}
/**
* vips_target_write:
* @target: target to operate on
* @buffer: bytes to write
* @length: length of @buffer in bytes
*
* Write @length bytes from @buffer to the output.
*
* Returns: 0 on success, -1 on error.
*/
int
vips_target_write( VipsTarget *target, const void *buffer, size_t length )
{
VIPS_DEBUG_MSG( "vips_target_write: %zd bytes\n", length );
if( length > VIPS_TARGET_BUFFER_SIZE - target->write_point &&
vips_target_flush( target ) )
return( -1 );
if( length > VIPS_TARGET_BUFFER_SIZE - target->write_point ) {
/* Still too large? Do an unbuffered write.
*/
if( vips_target_write_unbuffered( target, buffer, length ) )
return( -1 );
}
else {
memcpy( target->output_buffer + target->write_point,
buffer, length );
target->write_point += length;
}
return( 0 );
}
/**
* vips_target_read:
* @target: target to operate on
* @buffer: store bytes here
* @length: length of @buffer in bytes
*
* Read up to @length bytes from @target and store the bytes in @buffer.
* Return the number of bytes actually read. If all bytes have been read from
* the file, return 0.
*
* Arguments exactly as read(2).
*
* Reading froma target sounds weird, but libtiff needs this for
* multi-page writes. This method will fail for targets like pipes.
*
* Returns: the number of bytes read, 0 on end of file, -1 on error.
*/
gint64
vips_target_read( VipsTarget *target, void *buffer, size_t length )
{
VipsTargetClass *class = VIPS_TARGET_GET_CLASS( target );
VIPS_DEBUG_MSG( "vips_target_read: %zd bytes\n", length );
if( vips_target_flush( target ) )
return( -1 );
return( class->read( target, buffer, length ) );
}
/**
* vips_target_seek:
* @target: target to operate on
* @position: position to seek to
* @whence: seek relative to beginning, offset, or end
*
* Seek the target. This behaves exactly as lseek(2).
*
* Seeking a target sounds weird, but libtiff needs this. This method will
* fail for targets like pipes.
*
* Returns: the new seek position, -1 on error.
*/
off_t
vips_target_seek( VipsTarget *target, off_t position, int whence )
{
VipsTargetClass *class = VIPS_TARGET_GET_CLASS( target );
off_t new_position;
VIPS_DEBUG_MSG( "vips_target_seek: pos = %ld, whence = %d\n",
position, whence );
if( vips_target_flush( target ) )
return( -1 );
new_position = class->seek( target, position, whence );
VIPS_DEBUG_MSG( "vips_target_seek: new_position = %ld\n",
new_position );
return( new_position );
}
/**
* vips_target_end:
* @target: target to operate on
* @buffer: bytes to write
* @length: length of @buffer in bytes
*
* Call this at the end of write to make the target do any cleaning up. You
* can call it many times.
*
* After a target has been finished, further writes will do nothing.
*
* Returns: 0 on success, -1 on error.
*/
int
vips_target_end( VipsTarget *target )
{
VipsTargetClass *class = VIPS_TARGET_GET_CLASS( target );
VIPS_DEBUG_MSG( "vips_target_end:\n" );
if( target->ended )
return( 0 );
if( vips_target_flush( target ) )
return( -1 );
/* Move the target buffer into the blob so it can be read out.
*/
if( target->memory_buffer ) {
const char *data;
size_t length;
length = target->memory_buffer->len;
data = g_string_free( target->memory_buffer, FALSE );
target->memory_buffer = NULL;
vips_blob_set( target->blob,
(VipsCallbackFn) vips_area_free_cb, data, length );
}
else {
if( class->end( target ) )
return( -1 );
}
target->ended = TRUE;
return( 0 );
}
/**
* vips_target_finish:
* @target: target to operate on
* @buffer: bytes to write
* @length: length of @buffer in bytes
*
* Deprecated in favour of vips_target_end().
*/
void
vips_target_finish( VipsTarget *target )
{
(void) vips_target_end( target );
}
/**
* vips_target_steal:
* @target: target to operate on
* @length: return number of bytes of data
*
* Memory targets only (see vips_target_new_to_memory()). Steal all data
* written to the target so far, and finish it.
*
* You must free the returned pointer with g_free().
*
* The data is NOT automatically null-terminated. vips_target_putc() a '\0'
* before calling this to get a null-terminated string.
*
* Returns: (array length=length) (element-type guint8) (transfer full): the
* data
*/
unsigned char *
vips_target_steal( VipsTarget *target, size_t *length )
{
const char *data;
(void) vips_target_flush( target );
if( !target->memory_buffer ||
target->ended ) {
if( length )
*length = target->memory_buffer->len;
return( NULL );
}
if( length )
*length = target->memory_buffer->len;
data = g_string_free( target->memory_buffer, FALSE );
target->memory_buffer = NULL;
/* We must have a valid byte array or end will fail.
*/
target->memory_buffer = g_string_sized_new( 0 );
if( vips_target_end( target ) )
return( NULL );
return( (unsigned char *) data );
}
/**
* vips_target_steal_text:
* @target: target to operate on
*
* As vips_target_steal_text(), but return a null-terminated string.
*
* Returns: (transfer full): target contents as a null-terminated string.
*/
char *
vips_target_steal_text( VipsTarget *target )
{
vips_target_putc( target, '\0' );
return( (char *) vips_target_steal( target, NULL ) );
}
/**
* vips_target_putc:
* @target: target to operate on
* @ch: character to write
*
* Write a single character @ch to @target. See the macro VIPS_TARGET_PUTC()
* for a faster way to do this.
*
* Returns: 0 on success, -1 on error.
*/
int
vips_target_putc( VipsTarget *target, int ch )
{
VIPS_DEBUG_MSG( "vips_target_putc: %d\n", ch );
if( target->write_point >= VIPS_TARGET_BUFFER_SIZE &&
vips_target_flush( target ) )
return( -1 );
target->output_buffer[target->write_point++] = ch;
return( 0 );
}
/**
* vips_target_writes:
* @target: target to operate on
* @str: string to write
*
* Write a null-terminated string to @target.
*
* Returns: 0 on success, and -1 on error.
*/
int
vips_target_writes( VipsTarget *target, const char *str )
{
return( vips_target_write( target,
(unsigned char *) str, strlen( str ) ) );
}
/**
* vips_target_writef:
* @target: target to operate on
* @fmt: <function>printf()</function>-style format string
* @...: arguments to format string
*
* Format the string and write to @target.
*
* Returns: 0 on success, and -1 on error.
*/
int
vips_target_writef( VipsTarget *target, const char *fmt, ... )
{
va_list ap;
char *line;
int result;
va_start( ap, fmt );
line = g_strdup_vprintf( fmt, ap );
va_end( ap );
result = vips_target_writes( target, line );
g_free( line );
return( result );
}
/**
* vips_target_write_amp:
* @target: target to operate on
* @str: string to write
*
* Write @str to @target, but escape stuff that xml hates in text. Our
* argument string is utf-8.
*
* XML rules:
*
* - We must escape &<>
* - Don't escape \n, \t, \r
* - Do escape the other ASCII codes.
*
* Returns: 0 on success, -1 on error.
*/
int
vips_target_write_amp( VipsTarget *target, const char *str )
{
const char *p;
for( p = str; *p; p++ )
if( *p < 32 &&
*p != '\n' &&
*p != '\t' &&
*p != '\r' ) {
/* You'd think we could output "%x;", but xml
* 1.0 parsers barf on that. xml 1.1 allows this, but
* there are almost no parsers.
*
* U+2400 onwards are unicode glyphs for the ASCII
* control characters, so we can use them -- thanks
* electroly.
*/
if( vips_target_writef( target,
"&#x%04x;", 0x2400 + *p ) )
return( -1 );
}
else if( *p == '<' ) {
if( vips_target_writes( target, "<" ) )
return( -1 );
}
else if( *p == '>' ) {
if( vips_target_writes( target, ">" ) )
return( -1 );
}
else if( *p == '&' ) {
if( vips_target_writes( target, "&" ) )
return( -1 );
}
else {
if( VIPS_TARGET_PUTC( target, *p ) )
return( -1 );
}
return( 0 );
}
|