From 2bc18b630f28909e0736264861db12473bfdfd72 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 5 Sep 2010 13:02:57 -0700 Subject: [PATCH] Base64 output of PNGs. --- .../guacamole/GuacamoleClient.java | 8 +- guacamole/proxy/Makefile | 9 +- guacamole/proxy/base64.c | 123 ++++++++++++++++++ guacamole/proxy/base64.h | 6 + guacamole/proxy/proxy.c | 29 ++++- 5 files changed, 161 insertions(+), 14 deletions(-) create mode 100644 guacamole/proxy/base64.c create mode 100644 guacamole/proxy/base64.h diff --git a/guacamole/client/src/net/sourceforge/guacamole/GuacamoleClient.java b/guacamole/client/src/net/sourceforge/guacamole/GuacamoleClient.java index 4980ac5c2..2aa7e563b 100644 --- a/guacamole/client/src/net/sourceforge/guacamole/GuacamoleClient.java +++ b/guacamole/client/src/net/sourceforge/guacamole/GuacamoleClient.java @@ -25,8 +25,6 @@ import java.net.Socket; import java.io.InputStream; import java.io.BufferedInputStream; -import java.io.Reader; -import java.io.InputStreamReader; import net.sourceforge.guacamole.instruction.Instruction; import net.sourceforge.guacamole.GuacamoleException; @@ -36,13 +34,13 @@ import net.sourceforge.guacamole.event.PointerEvent; public class GuacamoleClient extends Client { private Socket sock; - private Reader input; + private InputStream input; public GuacamoleClient(String hostname, int port) throws GuacamoleException { try { sock = new Socket(InetAddress.getByName(hostname), port); - input = new InputStreamReader(new BufferedInputStream(sock.getInputStream())); + input = new BufferedInputStream(sock.getInputStream()); } catch (IOException e) { throw new GuacamoleException(e); @@ -78,7 +76,7 @@ public class GuacamoleClient extends Client { try { // While we're blocking, or input is available - while (blocking || input.ready()) { + while (blocking || input.available() > 0) { int readChar = input.read(); if (readChar == -1) diff --git a/guacamole/proxy/Makefile b/guacamole/proxy/Makefile index cd7122b4f..021ba5b9f 100644 --- a/guacamole/proxy/Makefile +++ b/guacamole/proxy/Makefile @@ -3,12 +3,15 @@ all: guacd -guacd: daemon.o proxy.o - $(CC) daemon.o proxy.o -l png -o guacd +guacd: daemon.o proxy.o base64.o + $(CC) daemon.o proxy.o base64.o -l png -o guacd -proxy.o: proxy.c proxy.h +proxy.o: proxy.c proxy.h base64.h $(CC) -c -ansi -pedantic proxy.c +base64.o: base64.c base64.h + $(CC) -c -ansi -pedantic base64.c + daemon.o: daemon.c proxy.h $(CC) -c -ansi -pedantic daemon.c diff --git a/guacamole/proxy/base64.c b/guacamole/proxy/base64.c new file mode 100644 index 000000000..7be69ad81 --- /dev/null +++ b/guacamole/proxy/base64.c @@ -0,0 +1,123 @@ + +#include +#include + +char characters[64] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', +}; + +int ready_buf[3]; +int ready = 0; + +int written = 0; +char out_buf[8192]; + +ssize_t __write_base64_triplet(int fd, int a, int b, int c) { + + int retval; + + /* Byte 1 */ + out_buf[written++] = characters[(a & 0xFC) >> 2]; /* [AAAAAA]AABBBB BBBBCC CCCCCC */ + + if (b >= 0) { + out_buf[written++] = characters[((a & 0x03) << 4) | ((b & 0xF0) >> 4)]; /* AAAAAA[AABBBB]BBBBCC CCCCCC */ + + if (c >= 0) { + out_buf[written++] = characters[((b & 0x0F) << 2) | ((c & 0xC0) >> 6)]; /* AAAAAA AABBBB[BBBBCC]CCCCCC */ + out_buf[written++] = characters[c & 0x3F]; /* AAAAAA AABBBB BBBBCC[CCCCCC] */ + } + else { + out_buf[written++] = characters[((b & 0x0F) << 2)]; /* AAAAAA AABBBB[BBBB--]------ */ + out_buf[written++] = '='; /* AAAAAA AABBBB BBBB--[------] */ + } + } + else { + out_buf[written++] = characters[((a & 0x03) << 4)]; /* AAAAAA[AA----]------ ------ */ + out_buf[written++] = '='; /* AAAAAA AA----[------]------ */ + out_buf[written++] = '='; /* AAAAAA AA---- ------[------] */ + } + + /* At this point, 4 bytes have been written */ + + /* Flush when necessary, return on error */ + if (written > 8188 /* sizeof(out_buf) - 4 */) { + retval = write(fd, out_buf, written); + if (retval < 0) + return retval; + + written = 0; + } + + if (b < 0) + return 1; + + if (c < 0) + return 2; + + return 3; + +} + +ssize_t __write_base64_byte(int fd, char buf) { + + int retval; + + ready_buf[ready++] = buf & 0xFF; + + /* Flush triplet */ + if (ready == 3) { + retval = __write_base64_triplet(fd, ready_buf[0], ready_buf[1], ready_buf[2]); + if (retval < 0) + return retval; + + ready = 0; + } + + return 1; +} + +ssize_t write_base64(int fd, const char* buf, size_t count) { + + int retval; + + const char* end = buf + count; + + while (buf < end) { + + retval = __write_base64_byte(fd, *(buf++)); + if (retval < 0) + return retval; + + } + + return count; + +} + +ssize_t flush_base64(int fd) { + + int retval; + + /* Flush triplet to output buffer */ + while (ready > 0) { + retval = __write_base64_byte(fd, -1); + if (retval < 0) + return retval; + } + + /* Flush remaining bytes in buffer */ + if (written > 0) { + retval = write(fd, out_buf, written); + if (retval < 0) + return retval; + + written = 0; + } + + return 0; + +} + diff --git a/guacamole/proxy/base64.h b/guacamole/proxy/base64.h new file mode 100644 index 000000000..0f23461c0 --- /dev/null +++ b/guacamole/proxy/base64.h @@ -0,0 +1,6 @@ + +#include + +ssize_t write_base64(int fd, const void* buf, size_t count); +ssize_t flush_base64(int fd); + diff --git a/guacamole/proxy/proxy.c b/guacamole/proxy/proxy.c index f3834fa2a..c69d1e9d9 100644 --- a/guacamole/proxy/proxy.c +++ b/guacamole/proxy/proxy.c @@ -2,6 +2,7 @@ #include #include +#include "base64.h" #include "proxy.h" struct guac_write_info { @@ -14,13 +15,15 @@ void guac_write_png(png_structp png, png_bytep data, png_size_t length) { client_fd = ((struct guac_write_info*) png->io_ptr)->client_fd; - /* TODO: */ - /* write_base64(client_fd, data, length); */ + if (write_base64(client_fd, data, length) < 0) { + perror("Error writing PNG"); + png_error(png, "Error writing PNG"); + return; + } } void guac_write_flush(png_structp png) { - printf("HERE!\n"); } void proxy(int client_fd) { @@ -90,10 +93,24 @@ void proxy(int client_fd) { } - png_set_rows(png, png_info, png_rows); - png_write_png(png, png_info, PNG_TRANSFORM_IDENTITY, NULL); - write(client_fd, "name:hello;size:1024,768;error:Test finished.;", 46); + write(client_fd, "name:hello;size:1024,768;", 25); + + /*for (y=0; y<20; y++) {*/ + write(client_fd, "png:0,0,", 8); + png_set_rows(png, png_info, png_rows); + png_write_png(png, png_info, PNG_TRANSFORM_IDENTITY, NULL); + + if (flush_base64(client_fd) < 0) { + perror("Error flushing PNG"); + png_error(png, "Error flushing PNG"); + return; + } + + write(client_fd, ";", 1); + /*}*/ + + write(client_fd, "error:Test finished.;", 21); /* Free PNG data */ for (y = 0; y<100 /* height */; y++)