Module: sip-router Branch: master Commit: f2dc27ced23a03241045607f394ca2d6834e90f8 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=f2dc27ce...
Author: Timo Teräs timo.teras@iki.fi Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Thu Jul 17 23:40:10 2014 +0200
websocket: option to compile the module without libunistring
- patch discussed on sr-dev - dcm: aded define wrapper to have this as compile time option and included the MIT license from the code site
---
modules/websocket/Makefile | 10 ++++- modules/websocket/utf8_decode.h | 82 +++++++++++++++++++++++++++++++++++++++ modules/websocket/ws_frame.c | 11 +++++ 3 files changed, 102 insertions(+), 1 deletions(-)
diff --git a/modules/websocket/Makefile b/modules/websocket/Makefile index bb7c809..b28f0ab 100644 --- a/modules/websocket/Makefile +++ b/modules/websocket/Makefile @@ -7,6 +7,8 @@ include ../../Makefile.defs auto_gen= NAME=websocket.so
+EMBEDDED_UTF8_DECODE ?= 0 + ifeq ($(CROSS_COMPILE),) SSL_BUILDER=$(shell \ if pkg-config --exists libssl; then \ @@ -27,7 +29,13 @@ else # E.g.: make TLS_HOOKS=1 TLS_EXTRA_LIBS="-lz -lkrb5" endif
-LIBS+= $(TLS_EXTRA_LIBS) -lunistring +LIBS+= $(TLS_EXTRA_LIBS) + +ifeq ($(EMBEDDED_UTF8_DECODE),0) + LIBS+= -lunistring +else + DEFS += -DEMBEDDED_UTF8_DECODE +endif
# Static linking, if you'd like to use TLS and WEBSOCKET at the same time # diff --git a/modules/websocket/utf8_decode.h b/modules/websocket/utf8_decode.h new file mode 100644 index 0000000..bb2d4bf --- /dev/null +++ b/modules/websocket/utf8_decode.h @@ -0,0 +1,82 @@ +/** + * Copyright (c) 2008-2010 Bjoern Hoehrmann bjoern@hoehrmann.de + * + * MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. + */ + +#ifdef EMBEDDED_UTF8_DECODE + +#ifndef _UTF8_DECODE_H_ +#define _UTF8_DECODE_H_ + +#include <stdint.h> +#include <stddef.h> + +#define UTF8_ACCEPT 0 +#define UTF8_REJECT 12 + +static const uint8_t utf8d[] = { + // The first part of the table maps bytes to character classes that + // to reduce the size of the transition table and create bitmasks. + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, + + // The second part is a transition table that maps a combination + // of a state of the automaton and a character class to a state. + 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12, + 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12, + 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12, + 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12, + 12,36,12,12,12,12,12,12,12,12,12,12, +}; + +static inline uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte) +{ + uint32_t type = utf8d[byte]; + + *codep = (*state != UTF8_ACCEPT) ? + (byte & 0x3fu) | (*codep << 6) : + (0xff >> type) & (byte); + + *state = utf8d[256 + *state + type]; + return *state; +} + +static inline int IsUTF8(uint8_t* s, size_t len) +{ + uint32_t codepoint, state = 0; + + while (len--) + decode(&state, &codepoint, *s++); + + return state == UTF8_ACCEPT; +} + +#endif + +#endif diff --git a/modules/websocket/ws_frame.c b/modules/websocket/ws_frame.c index 2f1a248..07f6204 100644 --- a/modules/websocket/ws_frame.c +++ b/modules/websocket/ws_frame.c @@ -27,7 +27,13 @@ */
#include <limits.h> + +#ifdef EMBEDDED_UTF8_DECODE +#include "utf8_decode.h" +#else #include <unistr.h> +#endif + #include "../../events.h" #include "../../receive.h" #include "../../stats.h" @@ -726,8 +732,13 @@ int ws_frame_transmit(void *data) frame.fin = 1; /* Can't be sure whether this message is UTF-8 or not so check to see if it "might" be UTF-8 and send as binary if it definitely isn't */ +#ifdef EMBEDDED_UTF8_DECODE + frame.opcode = IsUTF8((uint8_t *) wsev->buf, wsev->len) ? + OPCODE_TEXT_FRAME : OPCODE_BINARY_FRAME; +#else frame.opcode = (u8_check((uint8_t *) wsev->buf, wsev->len) == NULL) ? OPCODE_TEXT_FRAME : OPCODE_BINARY_FRAME; +#endif frame.payload_len = wsev->len; frame.payload_data = wsev->buf; frame.wsc = wsconn_get(wsev->id);