Commit 2c45d08d authored by Gernot Tenchio's avatar Gernot Tenchio Committed by Joel Martin

websocket: Use a single buffer for both, encoding and decoding

parent 3eec9765
...@@ -71,8 +71,7 @@ typedef int (*wsEncodeFunc)(rfbClientPtr cl, const char *src, int len, char **ds ...@@ -71,8 +71,7 @@ typedef int (*wsEncodeFunc)(rfbClientPtr cl, const char *src, int len, char **ds
typedef int (*wsDecodeFunc)(rfbClientPtr cl, char *dst, int len); typedef int (*wsDecodeFunc)(rfbClientPtr cl, char *dst, int len);
typedef struct ws_ctx_s { typedef struct ws_ctx_s {
char encodeBuf[B64LEN(UPDATE_BUF_SIZE) + WSHLENMAX]; /* base64 + maximum frame header length */ char codeBuf[B64LEN(UPDATE_BUF_SIZE) + WSHLENMAX]; /* base64 + maximum frame header length */
char decodeBuf[8192]; /* TODO: what makes sense? */
char readbuf[8192]; char readbuf[8192];
int readbufstart; int readbufstart;
int readbuflen; int readbuflen;
...@@ -172,7 +171,7 @@ webSocketsGenSha1Key(char * target, int size, char *key) ...@@ -172,7 +171,7 @@ webSocketsGenSha1Key(char * target, int size, char *key)
int len; int len;
SHA1Context sha; SHA1Context sha;
uint8_t digest[SHA1HashSize]; uint8_t digest[SHA1HashSize];
if (size < B64LEN(SHA1HashSize) + 1) { if (size < B64LEN(SHA1HashSize) + 1) {
rfbErr("webSocketsGenSha1Key: not enough space in target\n"); rfbErr("webSocketsGenSha1Key: not enough space in target\n");
target[0] = '\0'; target[0] = '\0';
...@@ -490,15 +489,15 @@ webSocketsEncodeHixie(rfbClientPtr cl, const char *src, int len, char **dst) ...@@ -490,15 +489,15 @@ webSocketsEncodeHixie(rfbClientPtr cl, const char *src, int len, char **dst)
int sz = 0; int sz = 0;
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx; ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
wsctx->encodeBuf[sz++] = '\x00'; wsctx->codeBuf[sz++] = '\x00';
len = __b64_ntop((unsigned char *)src, len, wsctx->encodeBuf+sz, sizeof(wsctx->encodeBuf) - (sz + 1)); len = __b64_ntop((unsigned char *)src, len, wsctx->codeBuf+sz, sizeof(wsctx->codeBuf) - (sz + 1));
if (len < 0) { if (len < 0) {
return len; return len;
} }
sz += len; sz += len;
wsctx->encodeBuf[sz++] = '\xff'; wsctx->codeBuf[sz++] = '\xff';
*dst = wsctx->encodeBuf; *dst = wsctx->codeBuf;
return sz; return sz;
} }
...@@ -536,7 +535,7 @@ webSocketsDecodeHixie(rfbClientPtr cl, char *dst, int len) ...@@ -536,7 +535,7 @@ webSocketsDecodeHixie(rfbClientPtr cl, char *dst, int len)
char *buf, *end = NULL; char *buf, *end = NULL;
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx; ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
buf = wsctx->decodeBuf; buf = wsctx->codeBuf;
n = ws_peek(cl, buf, len*2+2); n = ws_peek(cl, buf, len*2+2);
...@@ -653,8 +652,8 @@ webSocketsDecodeHybi(rfbClientPtr cl, char *dst, int len) ...@@ -653,8 +652,8 @@ webSocketsDecodeHybi(rfbClientPtr cl, char *dst, int len)
goto spor; goto spor;
} }
buf = wsctx->decodeBuf; buf = wsctx->codeBuf;
header = (ws_header_t *)wsctx->decodeBuf; header = (ws_header_t *)wsctx->codeBuf;
if (-1 == (ret = ws_peek(cl, buf, B64LEN(len) + WSHLENMAX))) { if (-1 == (ret = ws_peek(cl, buf, B64LEN(len) + WSHLENMAX))) {
rfbErr("%s: peek; %m\n", __func__); rfbErr("%s: peek; %m\n", __func__);
...@@ -730,11 +729,11 @@ webSocketsDecodeHybi(rfbClientPtr cl, char *dst, int len) ...@@ -730,11 +729,11 @@ webSocketsDecodeHybi(rfbClientPtr cl, char *dst, int len)
errno = ECONNRESET; errno = ECONNRESET;
break; break;
case WS_OPCODE_TEXT_FRAME: case WS_OPCODE_TEXT_FRAME:
if (-1 == (flength = __b64_pton(payload, (unsigned char *)wsctx->decodeBuf, sizeof(wsctx->decodeBuf)))) { if (-1 == (flength = __b64_pton(payload, (unsigned char *)wsctx->codeBuf, sizeof(wsctx->codeBuf)))) {
rfbErr("%s: Base64 decode error; %m\n", __func__); rfbErr("%s: Base64 decode error; %m\n", __func__);
break; break;
} }
payload = wsctx->decodeBuf; payload = wsctx->codeBuf;
/* fall through */ /* fall through */
case WS_OPCODE_BINARY_FRAME: case WS_OPCODE_BINARY_FRAME:
if (flength > len) { if (flength > len) {
...@@ -778,7 +777,7 @@ webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst) ...@@ -778,7 +777,7 @@ webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst)
return 0; return 0;
} }
header = (ws_header_t *)wsctx->encodeBuf; header = (ws_header_t *)wsctx->codeBuf;
if (wsctx->base64) { if (wsctx->base64) {
opcode = WS_OPCODE_TEXT_FRAME; opcode = WS_OPCODE_TEXT_FRAME;
...@@ -803,7 +802,7 @@ webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst) ...@@ -803,7 +802,7 @@ webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst)
} }
if (wsctx->base64) { if (wsctx->base64) {
if (-1 == (ret = __b64_ntop((unsigned char *)src, len, wsctx->encodeBuf + sz, sizeof(wsctx->encodeBuf) - sz))) { if (-1 == (ret = __b64_ntop((unsigned char *)src, len, wsctx->codeBuf + sz, sizeof(wsctx->codeBuf) - sz))) {
rfbErr("%s: Base 64 encode failed\n", __func__); rfbErr("%s: Base 64 encode failed\n", __func__);
} else { } else {
if (ret != blen) if (ret != blen)
...@@ -811,11 +810,11 @@ webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst) ...@@ -811,11 +810,11 @@ webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst)
ret += sz; ret += sz;
} }
} else { } else {
memcpy(wsctx->encodeBuf + sz, src, len); memcpy(wsctx->codeBuf + sz, src, len);
ret = sz + len; ret = sz + len;
} }
*dst = wsctx->encodeBuf; *dst = wsctx->codeBuf;
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment