diff --git a/guacamole/proxy/guacio.c b/guacamole/proxy/guacio.c index 612c0504a..accf41034 100644 --- a/guacamole/proxy/guacio.c +++ b/guacamole/proxy/guacio.c @@ -22,11 +22,37 @@ GUACIO* guac_open(int fd) { } void guac_close(GUACIO* io) { + guac_flush(io); free(io); } +ssize_t guac_write_string(GUACIO* io, const char* str) { -ssize_t __write_base64_triplet(GUACIO* io, int a, int b, int c) { + int fd = io->fd; + char* out_buf = io->out_buf; + + int retval; + + for (; *str != '\0'; str++) { + + out_buf[io->written++] = *str; + + /* Flush when necessary, return on error */ + if (io->written > 8188 /* sizeof(out_buf) - 4 */) { + retval = write(fd, out_buf, io->written); + if (retval < 0) + return retval; + + io->written = 0; + } + + } + + return 0; + +} + +ssize_t __guac_write_base64_triplet(GUACIO* io, int a, int b, int c) { int fd = io->fd; char* out_buf = io->out_buf; @@ -75,7 +101,7 @@ ssize_t __write_base64_triplet(GUACIO* io, int a, int b, int c) { } -ssize_t __write_base64_byte(GUACIO* io, char buf) { +ssize_t __guac_write_base64_byte(GUACIO* io, char buf) { int* ready_buf = io->ready_buf; @@ -85,7 +111,7 @@ ssize_t __write_base64_byte(GUACIO* io, char buf) { /* Flush triplet */ if (io->ready == 3) { - retval = __write_base64_triplet(io, ready_buf[0], ready_buf[1], ready_buf[2]); + retval = __guac_write_base64_triplet(io, ready_buf[0], ready_buf[1], ready_buf[2]); if (retval < 0) return retval; @@ -95,7 +121,7 @@ ssize_t __write_base64_byte(GUACIO* io, char buf) { return 1; } -ssize_t write_base64(GUACIO* io, const void* buf, size_t count) { +ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count) { int retval; @@ -104,7 +130,7 @@ ssize_t write_base64(GUACIO* io, const void* buf, size_t count) { while (char_buf < end) { - retval = __write_base64_byte(io, *(char_buf++)); + retval = __guac_write_base64_byte(io, *(char_buf++)); if (retval < 0) return retval; @@ -114,17 +140,10 @@ ssize_t write_base64(GUACIO* io, const void* buf, size_t count) { } -ssize_t flush_base64(GUACIO* io) { +ssize_t guac_flush(GUACIO* io) { int retval; - /* Flush triplet to output buffer */ - while (io->ready > 0) { - retval = __write_base64_byte(io, -1); - if (retval < 0) - return retval; - } - /* Flush remaining bytes in buffer */ if (io->written > 0) { retval = write(io->fd, io->out_buf, io->written); @@ -138,3 +157,18 @@ ssize_t flush_base64(GUACIO* io) { } +ssize_t guac_flush_base64(GUACIO* io) { + + int retval; + + /* Flush triplet to output buffer */ + while (io->ready > 0) { + retval = __guac_write_base64_byte(io, -1); + if (retval < 0) + return retval; + } + + return 0; + +} + diff --git a/guacamole/proxy/guacio.h b/guacamole/proxy/guacio.h index 1d6cf2d79..7d0343061 100644 --- a/guacamole/proxy/guacio.h +++ b/guacamole/proxy/guacio.h @@ -17,8 +17,10 @@ typedef struct GUACIO { } GUACIO; GUACIO* guac_open(int fd); -ssize_t write_base64(GUACIO* io, const void* buf, size_t count); -ssize_t flush_base64(GUACIO* io); +ssize_t guac_write_string(GUACIO* io, const char* str); +ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count); +ssize_t guac_flush_base64(GUACIO* io); +ssize_t guac_flush(GUACIO* io); void guac_close(GUACIO* io); #endif diff --git a/guacamole/proxy/proxy.c b/guacamole/proxy/proxy.c index 413cac976..0cd03c43d 100644 --- a/guacamole/proxy/proxy.c +++ b/guacamole/proxy/proxy.c @@ -8,7 +8,7 @@ void guac_write_png(png_structp png, png_bytep data, png_size_t length) { - if (write_base64((GUACIO*) png->io_ptr, data, length) < 0) { + if (guac_write_base64((GUACIO*) png->io_ptr, data, length) < 0) { perror("Error writing PNG"); png_error(png, "Error writing PNG"); return; @@ -49,7 +49,7 @@ void proxy(int client_fd) { } - write(io->fd, "name:hello;size:1024,768;", 25); + guac_write_string(io, "name:hello;size:1024,768;"); for (y=0; y<200; y++) { @@ -91,11 +91,11 @@ void proxy(int client_fd) { PNG_FILTER_TYPE_DEFAULT ); - write(io->fd, "png:0,0,", 8); + guac_write_string(io, "png:0,0,"); png_set_rows(png, png_info, png_rows); png_write_png(png, png_info, PNG_TRANSFORM_IDENTITY, NULL); - if (flush_base64(io) < 0) { + if (guac_flush_base64(io) < 0) { perror("Error flushing PNG"); png_error(png, "Error flushing PNG"); return; @@ -103,15 +103,16 @@ void proxy(int client_fd) { png_destroy_write_struct(&png, &png_info); - write(io->fd, ";", 1); + guac_write_string(io, ";"); } - write(io->fd, "error:Test finished.;", 21); + guac_write_string(io, "error:Test finished.;"); /* Free PNG data */ for (y = 0; y<100 /* height */; y++) free(png_rows[y]); free(png_rows); + guac_close(io); }