From acc579cc9dd58166b780e65f96cbfa9272d99de1 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 26 Nov 2020 10:15:48 +0000 Subject: [PATCH] force binary mode for connections on win stdin / stdout (for example) are created in text mode by default on win. We need to flip them to binary mode for connection read and write. See https://stackoverflow.com/questions/65014352/pipe-libvips-cli-output-to-stdout-in-windows --- ChangeLog | 1 + libvips/iofuncs/source.c | 13 +++++++++++++ libvips/iofuncs/target.c | 17 +++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8660448b..2f6b12d1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,7 @@ - block zero width or height images from imagemagick load [Koen1999] - check for overflow in gifload height [lovell] - fix msb_first default in ppm load and save [ewelot] +- force binary mode on win for connection read / write [Alreiber] 6/9/20 started 8.10.2 - update magicksave/load profile handling [kelilevi] diff --git a/libvips/iofuncs/source.c b/libvips/iofuncs/source.c index 4989e458..224baeb8 100644 --- a/libvips/iofuncs/source.c +++ b/libvips/iofuncs/source.c @@ -7,6 +7,9 @@ * - add vips_pipe_read_limit_set() * 3/10/20 * - improve behaviour with read and seek on pipes + * 26/11/20 + * - use _setmode() on win to force binary read for previously opened + * descriptors */ /* @@ -62,6 +65,9 @@ #include #include #include +#ifdef OS_WIN32 +#include +#endif /*OS_WIN32*/ #include #include @@ -300,6 +306,13 @@ vips_source_build( VipsObject *object ) if( vips_object_argument_isset( object, "descriptor" ) ) { connection->descriptor = dup( connection->descriptor ); connection->close_descriptor = connection->descriptor; + +#ifdef OS_WIN32 + /* Windows will create eg. stdin and stdout in text mode. + * We always read in binary mode. + */ + _setmode( connection->descriptor, _O_BINARY ); +#endif /*OS_WIN32*/ } if( vips_object_argument_isset( object, "blob" ) ) { diff --git a/libvips/iofuncs/target.c b/libvips/iofuncs/target.c index 1ca37835..01b3339c 100644 --- a/libvips/iofuncs/target.c +++ b/libvips/iofuncs/target.c @@ -2,6 +2,10 @@ * 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 */ /* @@ -51,6 +55,9 @@ #include #include #include +#ifdef OS_WIN32 +#include +#endif /*OS_WIN32*/ #include #include @@ -135,10 +142,16 @@ vips_target_build( VipsObject *object ) else if( vips_object_argument_isset( object, "descriptor" ) ) { connection->descriptor = dup( connection->descriptor ); connection->close_descriptor = connection->descriptor; + +#ifdef OS_WIN32 + /* Windows will create eg. stdin and stdout in text mode. + * We always write in binary mode. + */ + _setmode( connection->descriptor, _O_BINARY ); +#endif /*OS_WIN32*/ } - else if( target->memory ) { + else if( target->memory ) target->memory_buffer = g_byte_array_new(); - } return( 0 ); }