<div dir="ltr">Hello,<div><br></div><div>One of the reasons I used libunistring for this detection is that for pretty much all of the code fragments I found online for doing this in a "simple" way I saw people pointing out flaws in those algorithms.  Can you confirm that this code doesn't have any of those flaws and is guaranteed to work in all cases (has this implementation been stubbed out and tested fully by someone here)?</div>
<div><br></div><div>What new systems does libunistring not work on?  I have only ever had problems with it on older OS versions which didn't contain it at all.</div><div><br></div><div>Does this really improve performance?  Only a tiny, tiny, subset of libunistring is used.  As a result it doesn't really matter if libunistring in general is slow, just whether or not the one function used from libunistring is slow.</div>
<div><br></div><div>It also looks like the code style (particularly indentation) of that patch doesn't really match the style from that file.  If that really is the case it is probably worth aligning the patch to the style in that file before applying and committing it.</div>
<div><br></div><div>Regards,</div><div><br></div><div>Peter</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 20 December 2013 11:29, Timo Teras <span dir="ltr"><<a href="mailto:timo.teras@iki.fi" target="_blank">timo.teras@iki.fi</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
Yes - it is MIT license in the webpage. Might be a good thing to<br>
mention that in the created header file too.<br>
<span class="HOEnZb"><font color="#888888"><br>
- Timo<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On Fri, 20 Dec 2013 12:08:10 +0100<br>
Daniel-Constantin Mierla <<a href="mailto:miconda@gmail.com">miconda@gmail.com</a>> wrote:<br>
<br>
> getting rid of libunistring is good, indeed, thanks.<br>
><br>
> One thing that has to be cared with the websocket module is the need<br>
> to link against libssl, so if the new included code is gpl we need to<br>
> have an exception for it to comply with official debian packaging<br>
> requirements.<br>
><br>
> The page lists a different license than gpl, it seems to be MIT,<br>
> which I guess is all fine, being compatible with GPL and allowing<br>
> linking with libssl. But I wanted to highlight in case anyone else<br>
> has different opinion/more details.<br>
><br>
> Cheers,<br>
> Daniel<br>
><br>
><br>
> On 20/12/13 11:47, Timo Teräs wrote:<br>
> > libunistring is old, slow, messy code and uncompilable on new<br>
> > systems. remove the sole user of it, and replace it with inline<br>
> > utf8 decoder implementation from<br>
> > <a href="http://bjoern.hoehrmann.de/utf-8/decoder/dfa/" target="_blank">http://bjoern.hoehrmann.de/utf-8/decoder/dfa/</a>.<br>
> ><br>
> > improves performance and portability as libunistring is not needed.<br>
> > ---<br>
> > @Peter, Hugh: Would it be ok for me to push this?<br>
> ><br>
> >   modules/websocket/Makefile                |  2 +-<br>
> >   modules/websocket/README                  |  1 -<br>
> >   modules/websocket/doc/websocket_admin.xml |  3 --<br>
> >   modules/websocket/utf8_decode.h           | 52<br>
> > +++++++++++++++++++++++++++++++<br>
> > modules/websocket/ws_frame.c              |  4 +-- 5 files changed,<br>
> > 55 insertions(+), 7 deletions(-) create mode 100644<br>
> > modules/websocket/utf8_decode.h<br>
> ><br>
> > diff --git a/modules/websocket/Makefile b/modules/websocket/Makefile<br>
> > index bb7c809..c686a82 100644<br>
> > --- a/modules/websocket/Makefile<br>
> > +++ b/modules/websocket/Makefile<br>
> > @@ -27,7 +27,7 @@ else<br>
> >     #       E.g.: make TLS_HOOKS=1 TLS_EXTRA_LIBS="-lz -lkrb5"<br>
> >   endif<br>
> ><br>
> > -LIBS+= $(TLS_EXTRA_LIBS) -lunistring<br>
> > +LIBS+= $(TLS_EXTRA_LIBS)<br>
> ><br>
> >   # Static linking, if you'd like to use TLS and WEBSOCKET at the<br>
> > same time #<br>
> > diff --git a/modules/websocket/README b/modules/websocket/README<br>
> > index 49d8693..bdba3e4 100644<br>
> > --- a/modules/websocket/README<br>
> > +++ b/modules/websocket/README<br>
> > @@ -316,7 +316,6 @@ onreply_route[WS_REPLY] {<br>
> >      The following libraries must be installed before running<br>
> > Kamailio with this module loaded:<br>
> >        * OpenSSL.<br>
> > -     * GNU libunistring.<br>
> ><br>
> >   4. Parameters<br>
> ><br>
> > diff --git a/modules/websocket/doc/websocket_admin.xml<br>
> > b/modules/websocket/doc/websocket_admin.xml index fa7d300..e40dc09<br>
> > 100644 --- a/modules/websocket/doc/websocket_admin.xml<br>
> > +++ b/modules/websocket/doc/websocket_admin.xml<br>
> > @@ -262,9 +262,6 @@ onreply_route[WS_REPLY] {<br>
> >             <listitem><br>
> >             <para><emphasis>OpenSSL</emphasis>.</para><br>
> >             </listitem><br>
> > -           <listitem><br>
> > -           <para><emphasis>GNU libunistring</emphasis>.</para><br>
> > -           </listitem><br>
> >             </itemizedlist><br>
> >             </para><br>
> >     </section><br>
> > diff --git a/modules/websocket/utf8_decode.h<br>
> > b/modules/websocket/utf8_decode.h new file mode 100644<br>
> > index 0000000..b274fe7<br>
> > --- /dev/null<br>
> > +++ b/modules/websocket/utf8_decode.h<br>
> > @@ -0,0 +1,52 @@<br>
> > +#include <stdint.h><br>
> > +#include <stddef.h><br>
> > +<br>
> > +// Copyright (c) 2008-2010 Bjoern Hoehrmann <<a href="mailto:bjoern@hoehrmann.de">bjoern@hoehrmann.de</a>><br>
> > +// See <a href="http://bjoern.hoehrmann.de/utf-8/decoder/dfa/" target="_blank">http://bjoern.hoehrmann.de/utf-8/decoder/dfa/</a> for details.<br>
> > +<br>
> > +#define UTF8_ACCEPT 0<br>
> > +#define UTF8_REJECT 12<br>
> > +<br>
> > +static const uint8_t utf8d[] = {<br>
> > +  // The first part of the table maps bytes to character classes<br>
> > that<br>
> > +  // to reduce the size of the transition table and create<br>
> > bitmasks.<br>
> > +   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,<br>
> > 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,<br>
> > +   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,<br>
> > 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,<br>
> > +   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,<br>
> > 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,<br>
> > +   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,<br>
> > 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,<br>
> > +   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,<br>
> > 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,<br>
> > +   7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,<br>
> > 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,<br>
> > +   8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,<br>
> > 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,<br>
> > +  10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3,<br>
> > 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, +<br>
> > +  // The second part is a transition table that maps a combination<br>
> > +  // of a state of the automaton and a character class to a state.<br>
> > +   0,12,24,36,60,96,84,12,12,12,48,72,<br>
> > 12,12,12,12,12,12,12,12,12,12,12,12,<br>
> > +  12, 0,12,12,12,12,12, 0,12, 0,12,12,<br>
> > 12,24,12,12,12,12,12,24,12,24,12,12,<br>
> > +  12,12,12,12,12,12,12,24,12,12,12,12,<br>
> > 12,24,12,12,12,12,12,12,12,24,12,12,<br>
> > +  12,12,12,12,12,12,12,36,12,36,12,12,<br>
> > 12,36,12,12,12,12,12,36,12,36,12,12,<br>
> > +  12,36,12,12,12,12,12,12,12,12,12,12,<br>
> > +};<br>
> > +<br>
> > +static inline uint32_t decode(uint32_t* state, uint32_t* codep,<br>
> > uint32_t byte) +{<br>
> > +  uint32_t type = utf8d[byte];<br>
> > +<br>
> > +  *codep = (*state != UTF8_ACCEPT) ?<br>
> > +    (byte & 0x3fu) | (*codep << 6) :<br>
> > +    (0xff >> type) & (byte);<br>
> > +<br>
> > +  *state = utf8d[256 + *state + type];<br>
> > +  return *state;<br>
> > +}<br>
> > +<br>
> > +static inline int IsUTF8(uint8_t* s, size_t len)<br>
> > +{<br>
> > +  uint32_t codepoint, state = 0;<br>
> > +<br>
> > +  while (len--)<br>
> > +    decode(&state, &codepoint, *s++);<br>
> > +<br>
> > +  return state == UTF8_ACCEPT;<br>
> > +}<br>
> > +<br>
> > diff --git a/modules/websocket/ws_frame.c<br>
> > b/modules/websocket/ws_frame.c index a3a4cef..3562437 100644<br>
> > --- a/modules/websocket/ws_frame.c<br>
> > +++ b/modules/websocket/ws_frame.c<br>
> > @@ -22,7 +22,7 @@<br>
> >    */<br>
> ><br>
> >   #include <limits.h><br>
> > -#include <unistr.h><br>
> > +#include "utf8_decode.h"<br>
> >   #include "../../events.h"<br>
> >   #include "../../receive.h"<br>
> >   #include "../../stats.h"<br>
> > @@ -695,7 +695,7 @@ int ws_frame_transmit(void *data)<br>
> >     frame.fin = 1;<br>
> >     /* Can't be sure whether this message is UTF-8 or not so<br>
> > check to see if it "might" be UTF-8 and send as binary if it<br>
> > definitely isn't */<br>
> > -   frame.opcode = (u8_check((uint8_t *) wsev->buf, wsev->len)<br>
> > == NULL) ?<br>
> > +   frame.opcode = IsUTF8((uint8_t *) wsev->buf, wsev->len) ?<br>
> >                             OPCODE_TEXT_FRAME :<br>
> > OPCODE_BINARY_FRAME; frame.payload_len = wsev->len;<br>
> >     frame.payload_data = wsev->buf;<br>
><br>
><br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr"><div><font face="courier new, monospace">Peter Dunkley</font></div><div><font face="courier new, monospace">Technical Director</font></div>
<div><font face="courier new, monospace">Crocodile RCS Ltd</font></div></div>
</div>