Base64 output of PNGs.

This commit is contained in:
Michael Jumper
2010-09-05 13:02:57 -07:00
parent a87ba2b76b
commit 2bc18b630f
5 changed files with 161 additions and 14 deletions

View File

@@ -25,8 +25,6 @@ import java.net.Socket;
import java.io.InputStream; import java.io.InputStream;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.Reader;
import java.io.InputStreamReader;
import net.sourceforge.guacamole.instruction.Instruction; import net.sourceforge.guacamole.instruction.Instruction;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
@@ -36,13 +34,13 @@ import net.sourceforge.guacamole.event.PointerEvent;
public class GuacamoleClient extends Client { public class GuacamoleClient extends Client {
private Socket sock; private Socket sock;
private Reader input; private InputStream input;
public GuacamoleClient(String hostname, int port) throws GuacamoleException { public GuacamoleClient(String hostname, int port) throws GuacamoleException {
try { try {
sock = new Socket(InetAddress.getByName(hostname), port); sock = new Socket(InetAddress.getByName(hostname), port);
input = new InputStreamReader(new BufferedInputStream(sock.getInputStream())); input = new BufferedInputStream(sock.getInputStream());
} }
catch (IOException e) { catch (IOException e) {
throw new GuacamoleException(e); throw new GuacamoleException(e);
@@ -78,7 +76,7 @@ public class GuacamoleClient extends Client {
try { try {
// While we're blocking, or input is available // While we're blocking, or input is available
while (blocking || input.ready()) { while (blocking || input.available() > 0) {
int readChar = input.read(); int readChar = input.read();
if (readChar == -1) if (readChar == -1)

View File

@@ -3,12 +3,15 @@
all: guacd all: guacd
guacd: daemon.o proxy.o guacd: daemon.o proxy.o base64.o
$(CC) daemon.o proxy.o -l png -o guacd $(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 $(CC) -c -ansi -pedantic proxy.c
base64.o: base64.c base64.h
$(CC) -c -ansi -pedantic base64.c
daemon.o: daemon.c proxy.h daemon.o: daemon.c proxy.h
$(CC) -c -ansi -pedantic daemon.c $(CC) -c -ansi -pedantic daemon.c

123
guacamole/proxy/base64.c Normal file
View File

@@ -0,0 +1,123 @@
#include <unistd.h>
#include <stdio.h>
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;
}

6
guacamole/proxy/base64.h Normal file
View File

@@ -0,0 +1,6 @@
#include <unistd.h>
ssize_t write_base64(int fd, const void* buf, size_t count);
ssize_t flush_base64(int fd);

View File

@@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <png.h> #include <png.h>
#include "base64.h"
#include "proxy.h" #include "proxy.h"
struct guac_write_info { 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; client_fd = ((struct guac_write_info*) png->io_ptr)->client_fd;
/* TODO: */ if (write_base64(client_fd, data, length) < 0) {
/* write_base64(client_fd, data, length); */ perror("Error writing PNG");
png_error(png, "Error writing PNG");
return;
}
} }
void guac_write_flush(png_structp png) { void guac_write_flush(png_structp png) {
printf("HERE!\n");
} }
void proxy(int client_fd) { 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 */ /* Free PNG data */
for (y = 0; y<100 /* height */; y++) for (y = 0; y<100 /* height */; y++)