Annotate

util-src/encodings.c @ 12261:f7946c8e502f

util.format: Fix typo in comment [codespell]
author Kim Alvefur <zash@zash.se>
date Fri, 04 Feb 2022 16:40:38 +0100
parent 10921:6eb5d2bb11af
child 12575:1f6f05a98fcd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2572
diff changeset
1 /* Prosody IM
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2572
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2572
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
4 -- Copyright (C) 1994-2015 Lua.org, PUC-Rio.
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
5 --
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
7 -- COPYING file in the source package for more information.
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
8 --
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
9 */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
10
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
11 /*
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
12 * encodings.c
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
13 * Lua library for base64, stringprep and idna encodings
520
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
14 */
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
15
3965
4ae4b2c0e99d util.encodings: Switch comment styles to build ok as ANSI C
Matthew Wild <mwild1@gmail.com>
parents: 3769
diff changeset
16 /* Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way */
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
17 #define _CRT_SECURE_NO_DEPRECATE
520
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
18
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
19 #include <string.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
20 #include <stdlib.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
21 #include "lua.h"
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
22 #include "lauxlib.h"
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
23
6789
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6643
diff changeset
24 #if (LUA_VERSION_NUM == 501)
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6643
diff changeset
25 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
6413
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6412
diff changeset
26 #endif
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
27 #if (LUA_VERSION_NUM < 504)
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
28 #define luaL_pushfail lua_pushnil
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
29 #endif
6413
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6412
diff changeset
30
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
31 /***************** BASE64 *****************/
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
32
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
33 static const char code[] =
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
34 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
35
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
36 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n) {
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
37 unsigned long tuple = c3 + 256UL * (c2 + 256UL * c1);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
38 int i;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
39 char s[4];
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
40
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
41 for(i = 0; i < 4; i++) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
42 s[3 - i] = code[tuple % 64];
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
43 tuple /= 64;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
44 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
45
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
46 for(i = n + 1; i < 4; i++) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
47 s[i] = '=';
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
48 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
49
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
50 luaL_addlstring(b, s, 4);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
51 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
52
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
53 static int Lbase64_encode(lua_State *L) { /** encode(s) */
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
54 size_t l;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
55 const unsigned char *s = (const unsigned char *)luaL_checklstring(L, 1, &l);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
56 luaL_Buffer b;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
57 int n;
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
58 luaL_buffinit(L, &b);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
59
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
60 for(n = l / 3; n--; s += 3) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
61 base64_encode(&b, s[0], s[1], s[2], 3);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
62 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
63
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
64 switch(l % 3) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
65 case 1:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
66 base64_encode(&b, s[0], 0, 0, 1);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
67 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
68
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
69 case 2:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
70 base64_encode(&b, s[0], s[1], 0, 2);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
71 break;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
72 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
73
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
74 luaL_pushresult(&b);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
75 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
76 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
77
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
78 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n) {
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
79 unsigned long tuple = c4 + 64L * (c3 + 64L * (c2 + 64L * c1));
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
80 char s[3];
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
81
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
82 switch(--n) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
83 case 3:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
84 s[2] = (char) tuple;
9153
26c5a4a14905 encodings: Explicitly say that base64 decoding falls through in a switch, fixes a warning in gcc 7+.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8291
diff changeset
85 /* Falls through. */
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
86
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
87 case 2:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
88 s[1] = (char)(tuple >> 8);
9153
26c5a4a14905 encodings: Explicitly say that base64 decoding falls through in a switch, fixes a warning in gcc 7+.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8291
diff changeset
89 /* Falls through. */
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
90
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
91 case 1:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
92 s[0] = (char)(tuple >> 16);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
93 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
94
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
95 luaL_addlstring(b, s, n);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
96 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
97
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
98 static int Lbase64_decode(lua_State *L) { /** decode(s) */
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
99 size_t l;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
100 const char *s = luaL_checklstring(L, 1, &l);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
101 luaL_Buffer b;
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
102 int n = 0;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
103 char t[4];
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
104 luaL_buffinit(L, &b);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
105
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
106 for(;;) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
107 int c = *s++;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
108
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
109 switch(c) {
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
110 const char *p;
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
111
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
112 default:
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
113 p = strchr(code, c);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
114
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
115 if(p == NULL) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
116 return 0;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
117 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
118
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
119 t[n++] = (char)(p - code);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
120
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
121 if(n == 4) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
122 base64_decode(&b, t[0], t[1], t[2], t[3], 4);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
123 n = 0;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
124 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
125
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
126 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
127
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
128 case '=':
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
129
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
130 switch(n) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
131 case 1:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
132 base64_decode(&b, t[0], 0, 0, 0, 1);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
133 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
134
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
135 case 2:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
136 base64_decode(&b, t[0], t[1], 0, 0, 2);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
137 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
138
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
139 case 3:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
140 base64_decode(&b, t[0], t[1], t[2], 0, 3);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
141 break;
601
6cb908ef01c8 Fixed util.encodings.base64.decode to not truncate results when encountering an '=' before the end of the given input.
Waqas Hussain <waqas20@gmail.com>
parents: 520
diff changeset
142 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
143
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
144 n = 0;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
145 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
146
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
147 case 0:
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
148 luaL_pushresult(&b);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
149 return 1;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
150
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
151 case '\n':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
152 case '\r':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
153 case '\t':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
154 case ' ':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
155 case '\f':
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
156 case '\b':
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
157 break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
158 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
159 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
160 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
161
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
162 static const luaL_Reg Reg_base64[] = {
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
163 { "encode", Lbase64_encode },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
164 { "decode", Lbase64_decode },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
165 { NULL, NULL }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
166 };
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
167
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
168 /******************* UTF-8 ********************/
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
169
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
170 /*
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
171 * Adapted from Lua 5.3
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
172 * Needed because libidn does not validate that input is valid UTF-8
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
173 */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
174
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
175 #define MAXUNICODE 0x10FFFF
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
176
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
177 /*
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
178 * Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
179 */
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
180 static const char *utf8_decode(const char *o, int *val) {
7998
604beb13596a util.encodings: Make limit lookup table a const (from Lua f2a813ae)
Kim Alvefur <zash@zash.se>
parents: 7889
diff changeset
181 static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
182 const unsigned char *s = (const unsigned char *)o;
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
183 unsigned int c = s[0];
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
184 unsigned int res = 0; /* final result */
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
185
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
186 if(c < 0x80) { /* ascii? */
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
187 res = c;
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
188 } else {
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
189 int count = 0; /* to count number of continuation bytes */
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
190
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
191 while(c & 0x40) { /* still have continuation bytes? */
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
192 int cc = s[++count]; /* read next byte */
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
193
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
194 if((cc & 0xC0) != 0x80) { /* not a continuation byte? */
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
195 return NULL; /* invalid byte sequence */
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
196 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
197
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
198 res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
199 c <<= 1; /* to test next bit */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
200 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
201
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
202 res |= ((c & 0x7F) << (count * 5)); /* add first byte */
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
203
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
204 if(count > 3 || res > MAXUNICODE || res <= limits[count] || (0xd800 <= res && res <= 0xdfff)) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
205 return NULL; /* invalid byte sequence */
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
206 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
207
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
208 s += count; /* skip continuation bytes read */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
209 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
210
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
211 if(val) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
212 *val = res;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
213 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
214
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
215 return (const char *)s + 1; /* +1 to include first byte */
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
216 }
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
217
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
218 /*
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
219 * Check that a string is valid UTF-8
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
220 * Returns NULL if not
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
221 */
10478
5e9a1a75f8a7 util.encodings: Don’t export unneeded symbols
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10371
diff changeset
222 static const char *check_utf8(lua_State *L, int idx, size_t *l) {
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
223 size_t pos, len;
8291
fba1ea67f5c0 util.encodings: Use the 'idx' argument correctly [-Wunused-parameter]
Kim Alvefur <zash@zash.se>
parents: 7998
diff changeset
224 const char *s = luaL_checklstring(L, idx, &len);
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
225 pos = 0;
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
226
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
227 while(pos <= len) {
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
228 const char *s1 = utf8_decode(s + pos, NULL);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
229
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
230 if(s1 == NULL) { /* conversion error? */
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
231 return NULL;
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
232 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
233
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
234 pos = s1 - s;
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
235 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
236
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
237 if(l != NULL) {
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
238 *l = len;
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
239 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
240
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
241 return s;
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
242 }
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
243
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
244 static int Lutf8_valid(lua_State *L) {
6592
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
245 lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL);
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
246 return 1;
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
247 }
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
248
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
249 static int Lutf8_length(lua_State *L) {
6592
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
250 size_t len;
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
251
6592
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
252 if(!check_utf8(L, 1, &len)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
253 luaL_pushfail(L);
6592
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
254 lua_pushliteral(L, "invalid utf8");
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
255 return 2;
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
256 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
257
6592
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
258 lua_pushinteger(L, len);
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
259 return 1;
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
260 }
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
261
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
262 static const luaL_Reg Reg_utf8[] = {
6592
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
263 { "valid", Lutf8_valid },
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
264 { "length", Lutf8_length },
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
265 { NULL, NULL }
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
266 };
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
267
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
268 /***************** STRINGPREP *****************/
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
269 #ifdef USE_STRINGPREP_ICU
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
270
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
271 #include <unicode/usprep.h>
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
272 #include <unicode/ustring.h>
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
273 #include <unicode/utrace.h>
9976
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
274 #include <unicode/uspoof.h>
10261
010c67532ed0 util.encodings: Switch ICU binding to IDNA2008 (fixes #533, #1301)
Kim Alvefur <zash@zash.se>
parents: 10006
diff changeset
275 #include <unicode/uidna.h>
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
276
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
277 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
278 size_t input_len;
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
279 int32_t unprepped_len, prepped_len, output_len;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
280 const char *input;
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
281 char output[1024];
10357
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
282 int flags = USPREP_ALLOW_UNASSIGNED;
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
283
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
284 UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
285 UChar prepped[1024];
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
286
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
287 UErrorCode err = U_ZERO_ERROR;
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
288
10371
228338120009 util.encodings: Don't ignore non-strings passed to stringprep functions
Kim Alvefur <zash@zash.se>
parents: 10359
diff changeset
289 input = luaL_checklstring(L, 1, &input_len);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
290
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
291 if(input_len >= 1024) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
292 luaL_pushfail(L);
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
293 return 1;
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
294 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
295
10357
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
296 /* strict */
10359
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
297 if(!lua_isnoneornil(L, 2)) {
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
298 luaL_checktype(L, 2, LUA_TBOOLEAN);
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
299 if(lua_toboolean(L, 2)) {
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
300 flags = 0;
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
301 }
10357
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
302 }
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
303
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
304 u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
305
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
306 if(U_FAILURE(err)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
307 luaL_pushfail(L);
4273
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
308 return 1;
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
309 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
310
10357
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
311 prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, flags, NULL, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
312
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
313 if(U_FAILURE(err)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
314 luaL_pushfail(L);
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
315 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
316 } else {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
317 u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
318
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
319 if(U_SUCCESS(err) && output_len < 1024) {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
320 lua_pushlstring(L, output, output_len);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
321 } else {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
322 luaL_pushfail(L);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
323 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
324
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
325 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
326 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
327 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
328
10478
5e9a1a75f8a7 util.encodings: Don’t export unneeded symbols
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10371
diff changeset
329 static UStringPrepProfile *icu_nameprep;
5e9a1a75f8a7 util.encodings: Don’t export unneeded symbols
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10371
diff changeset
330 static UStringPrepProfile *icu_nodeprep;
5e9a1a75f8a7 util.encodings: Don’t export unneeded symbols
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10371
diff changeset
331 static UStringPrepProfile *icu_resourceprep;
5e9a1a75f8a7 util.encodings: Don’t export unneeded symbols
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10371
diff changeset
332 static UStringPrepProfile *icu_saslprep;
5e9a1a75f8a7 util.encodings: Don’t export unneeded symbols
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10371
diff changeset
333 static USpoofChecker *icu_spoofcheck;
5e9a1a75f8a7 util.encodings: Don’t export unneeded symbols
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10371
diff changeset
334 static UIDNA *icu_idna2008;
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
335
9979
b06f6ff878ee util.encodings: Add compat with ICU before version 58
Kim Alvefur <zash@zash.se>
parents: 9976
diff changeset
336 #if (U_ICU_VERSION_MAJOR_NUM < 58)
b06f6ff878ee util.encodings: Add compat with ICU before version 58
Kim Alvefur <zash@zash.se>
parents: 9976
diff changeset
337 /* COMPAT */
b06f6ff878ee util.encodings: Add compat with ICU before version 58
Kim Alvefur <zash@zash.se>
parents: 9976
diff changeset
338 #define USPOOF_CONFUSABLE (USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_WHOLE_SCRIPT_CONFUSABLE)
b06f6ff878ee util.encodings: Add compat with ICU before version 58
Kim Alvefur <zash@zash.se>
parents: 9976
diff changeset
339 #endif
b06f6ff878ee util.encodings: Add compat with ICU before version 58
Kim Alvefur <zash@zash.se>
parents: 9976
diff changeset
340
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
341 /* initialize global ICU stringprep profiles */
10478
5e9a1a75f8a7 util.encodings: Don’t export unneeded symbols
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10371
diff changeset
342 static void init_icu(void) {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
343 UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
344 utrace_setLevel(UTRACE_VERBOSE);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
345 icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
346 icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
347 icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
348 icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
9976
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
349 icu_spoofcheck = uspoof_open(&err);
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
350 uspoof_setChecks(icu_spoofcheck, USPOOF_CONFUSABLE, &err);
10262
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
351 int options = UIDNA_DEFAULT;
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
352 #if 0
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
353 /* COMPAT with future Unicode versions */
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
354 options |= UIDNA_ALLOW_UNASSIGNED;
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
355 #endif
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
356 #if 1
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
357 /* Forbid eg labels starting with _ */
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
358 options |= UIDNA_USE_STD3_RULES;
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
359 #endif
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
360 #if 0
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
361 /* TODO determine if we need this */
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
362 options |= UIDNA_CHECK_BIDI;
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
363 #endif
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
364 #if 0
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
365 /* UTS46 makes it sound like these are the responsibility of registrars */
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
366 options |= UIDNA_CHECK_CONTEXTJ;
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
367 options |= UIDNA_CHECK_CONTEXTO;
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
368 #endif
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
369 #if 0
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
370 /* This disables COMPAT with IDNA 2003 */
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
371 options |= UIDNA_NONTRANSITIONAL_TO_ASCII;
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
372 options |= UIDNA_NONTRANSITIONAL_TO_UNICODE;
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
373 #endif
b1209bc15cd1 util.encodings: Spell out all IDNA 2008 options ICU has
Kim Alvefur <zash@zash.se>
parents: 10261
diff changeset
374 icu_idna2008 = uidna_openUTS46(options, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
375
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
376 if(U_FAILURE(err)) {
10479
d362934437eb util.encodings: Remove redundant cast
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10478
diff changeset
377 fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName(err));
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
378 }
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
379 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
380
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
381 #define MAKE_PREP_FUNC(myFunc, prep) \
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
382 static int myFunc(lua_State *L) { return icu_stringprep_prep(L, prep); }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
383
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
384 MAKE_PREP_FUNC(Lstringprep_nameprep, icu_nameprep) /** stringprep.nameprep(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
385 MAKE_PREP_FUNC(Lstringprep_nodeprep, icu_nodeprep) /** stringprep.nodeprep(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
386 MAKE_PREP_FUNC(Lstringprep_resourceprep, icu_resourceprep) /** stringprep.resourceprep(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
387 MAKE_PREP_FUNC(Lstringprep_saslprep, icu_saslprep) /** stringprep.saslprep(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
388
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
389 static const luaL_Reg Reg_stringprep[] = {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
390 { "nameprep", Lstringprep_nameprep },
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
391 { "nodeprep", Lstringprep_nodeprep },
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
392 { "resourceprep", Lstringprep_resourceprep },
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
393 { "saslprep", Lstringprep_saslprep },
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
394 { NULL, NULL }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
395 };
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
396 #else /* USE_STRINGPREP_ICU */
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
397
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
398 /****************** libidn ********************/
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
399
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
400 #include <stringprep.h>
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
401
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
402 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) {
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
403 size_t len;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
404 const char *s;
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
405 char string[1024];
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
406 int ret;
10357
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
407 Stringprep_profile_flags flags = 0;
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
408
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
409 s = check_utf8(L, 1, &len);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
410
10357
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
411 /* strict */
10359
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
412 if(!lua_isnoneornil(L, 2)) {
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
413 luaL_checktype(L, 2, LUA_TBOOLEAN);
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
414 if(lua_toboolean(L, 2)) {
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
415 flags = STRINGPREP_NO_UNASSIGNED;
4ef785f45aa2 util.encodings: Strictly verify that the 'strict' *prep argument is a boolean
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
416 }
10357
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
417 }
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
418
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
419 if(s == NULL || len >= 1024 || len != strlen(s)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
420 luaL_pushfail(L);
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
421 return 1; /* TODO return error message */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
422 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
423
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
424 strcpy(string, s);
10357
72e6d0d7ff9b util.encodings: Optional strict flag to stringprep
Kim Alvefur <zash@zash.se>
parents: 10262
diff changeset
425 ret = stringprep(string, 1024, flags, profile);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
426
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
427 if(ret == STRINGPREP_OK) {
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
428 lua_pushstring(L, string);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
429 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
430 } else {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
431 luaL_pushfail(L);
3965
4ae4b2c0e99d util.encodings: Switch comment styles to build ok as ANSI C
Matthew Wild <mwild1@gmail.com>
parents: 3769
diff changeset
432 return 1; /* TODO return error message */
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
433 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
434 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
435
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
436 #define MAKE_PREP_FUNC(myFunc, prep) \
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
437 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
438
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
439 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
440 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
441 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
442 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
443
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
444 static const luaL_Reg Reg_stringprep[] = {
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
445 { "nameprep", Lstringprep_nameprep },
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
446 { "nodeprep", Lstringprep_nodeprep },
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
447 { "resourceprep", Lstringprep_resourceprep },
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
448 { "saslprep", Lstringprep_saslprep },
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
449 { NULL, NULL }
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
450 };
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
451 #endif
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
452
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
453 /***************** IDNA *****************/
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
454 #ifdef USE_STRINGPREP_ICU
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
455 #include <unicode/ustdio.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
456 #include <unicode/uidna.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
457 /* IDNA2003 or IDNA2008 ? ? ? */
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
458 static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
459 size_t len;
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
460 int32_t ulen, dest_len, output_len;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
461 const char *s = luaL_checklstring(L, 1, &len);
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
462 UChar ustr[1024];
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
463 UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
464 UChar dest[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
465 char output[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
466
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
467 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
468
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
469 if(U_FAILURE(err)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
470 luaL_pushfail(L);
4273
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
471 return 1;
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
472 }
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
473
10261
010c67532ed0 util.encodings: Switch ICU binding to IDNA2008 (fixes #533, #1301)
Kim Alvefur <zash@zash.se>
parents: 10006
diff changeset
474 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
010c67532ed0 util.encodings: Switch ICU binding to IDNA2008 (fixes #533, #1301)
Kim Alvefur <zash@zash.se>
parents: 10006
diff changeset
475 dest_len = uidna_nameToASCII(icu_idna2008, ustr, ulen, dest, 256, &info, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
476
10261
010c67532ed0 util.encodings: Switch ICU binding to IDNA2008 (fixes #533, #1301)
Kim Alvefur <zash@zash.se>
parents: 10006
diff changeset
477 if(U_FAILURE(err) || info.errors) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
478 luaL_pushfail(L);
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
479 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
480 } else {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
481 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
482
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
483 if(U_SUCCESS(err) && output_len < 1024) {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
484 lua_pushlstring(L, output, output_len);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
485 } else {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
486 luaL_pushfail(L);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
487 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
488
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
489 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
490 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
491 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
492
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
493 static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
494 size_t len;
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
495 int32_t ulen, dest_len, output_len;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
496 const char *s = luaL_checklstring(L, 1, &len);
4271
18d888c8c12d util.encodings: Fix idna.to_unicode
Paul Aurich <paul@darkrain42.org>
parents: 3965
diff changeset
497 UChar ustr[1024];
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
498 UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
499 UChar dest[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
500 char output[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
501
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
502 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
503
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
504 if(U_FAILURE(err)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
505 luaL_pushfail(L);
4273
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
506 return 1;
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
507 }
7f789266b741 util.encodings: Check return values before proceeding
Paul Aurich <paul@darkrain42.org>
parents: 4272
diff changeset
508
10261
010c67532ed0 util.encodings: Switch ICU binding to IDNA2008 (fixes #533, #1301)
Kim Alvefur <zash@zash.se>
parents: 10006
diff changeset
509 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
010c67532ed0 util.encodings: Switch ICU binding to IDNA2008 (fixes #533, #1301)
Kim Alvefur <zash@zash.se>
parents: 10006
diff changeset
510 dest_len = uidna_nameToUnicode(icu_idna2008, ustr, ulen, dest, 1024, &info, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
511
10261
010c67532ed0 util.encodings: Switch ICU binding to IDNA2008 (fixes #533, #1301)
Kim Alvefur <zash@zash.se>
parents: 10006
diff changeset
512 if(U_FAILURE(err) || info.errors) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
513 luaL_pushfail(L);
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
514 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
515 } else {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
516 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
517
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
518 if(U_SUCCESS(err) && output_len < 1024) {
3769
9338d0785277 util-src/Makefile, util-src/encodings.c{,pp}: Port ICU code to C, rename encodings.cpp back to .c and amend the Makefile accordingly
Matthew Wild <mwild1@gmail.com>
parents: 3764
diff changeset
519 lua_pushlstring(L, output, output_len);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
520 } else {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
521 luaL_pushfail(L);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
522 }
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
523
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
524 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
525 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
526 }
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
527
9976
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
528 static int Lskeleton(lua_State *L) {
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
529 size_t len;
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
530 int32_t ulen, dest_len, output_len;
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
531 const char *s = luaL_checklstring(L, 1, &len);
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
532 UErrorCode err = U_ZERO_ERROR;
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
533 UChar ustr[1024];
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
534 UChar dest[1024];
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
535 char output[1024];
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
536
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
537 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
538
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
539 if(U_FAILURE(err)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
540 luaL_pushfail(L);
9976
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
541 return 1;
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
542 }
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
543
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
544 dest_len = uspoof_getSkeleton(icu_spoofcheck, 0, ustr, ulen, dest, 1024, &err);
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
545
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
546 if(U_FAILURE(err)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
547 luaL_pushfail(L);
9976
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
548 return 1;
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
549 }
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
550
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
551 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
552
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
553 if(U_SUCCESS(err)) {
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
554 lua_pushlstring(L, output, output_len);
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
555 return 1;
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
556 }
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
557
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
558 luaL_pushfail(L);
9976
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
559 return 1;
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
560 }
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
561
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
562 #else /* USE_STRINGPREP_ICU */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
563 /****************** libidn ********************/
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
564
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
565 #include <idna.h>
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
566 #include <idn-free.h>
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
567
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
568 static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
569 size_t len;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
570 const char *s = check_utf8(L, 1, &len);
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
571 char *output = NULL;
6643
127b9f0c6135 util.encodings: Move declarations to top of function [pedantic]
Kim Alvefur <zash@zash.se>
parents: 6615
diff changeset
572 int ret;
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
573
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
574 if(s == NULL || len != strlen(s)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
575 luaL_pushfail(L);
6591
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
576 return 1; /* TODO return error message */
fe3018a2f187 util.encodings: Perform validation of UTF-8 strings before passing to libidn (Based on code from the utf8 library in Lua 5.3)
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
577 }
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
578
6643
127b9f0c6135 util.encodings: Move declarations to top of function [pedantic]
Kim Alvefur <zash@zash.se>
parents: 6615
diff changeset
579 ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
580
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
581 if(ret == IDNA_SUCCESS) {
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
582 lua_pushstring(L, output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
583 idn_free(output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
584 return 1;
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
585 } else {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
586 luaL_pushfail(L);
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
587 idn_free(output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
588 return 1; /* TODO return error message */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
589 }
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
590 }
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
591
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
592 static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
593 size_t len;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
594 const char *s = luaL_checklstring(L, 1, &len);
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
595 char *output = NULL;
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
596 int ret = idna_to_unicode_8z8z(s, &output, 0);
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
597
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
598 if(ret == IDNA_SUCCESS) {
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
599 lua_pushstring(L, output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
600 idn_free(output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
601 return 1;
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
602 } else {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10479
diff changeset
603 luaL_pushfail(L);
4272
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
604 idn_free(output);
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
605 return 1; /* TODO return error message */
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
606 }
0a4ce2086a88 util.encodings: Swap code order ("ifndef" bugs me)
Paul Aurich <paul@darkrain42.org>
parents: 4271
diff changeset
607 }
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
608 #endif
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
609
6615
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6608
diff changeset
610 static const luaL_Reg Reg_idna[] = {
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
611 { "to_ascii", Lidna_to_ascii },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
612 { "to_unicode", Lidna_to_unicode },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
613 { NULL, NULL }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
614 };
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
615
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
616 /***************** end *****************/
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
617
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
618 LUALIB_API int luaopen_util_encodings(lua_State *L) {
7818
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 6789
diff changeset
619 #if (LUA_VERSION_NUM > 501)
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 6789
diff changeset
620 luaL_checkversion(L);
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 6789
diff changeset
621 #endif
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
622 #ifdef USE_STRINGPREP_ICU
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
623 init_icu();
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
624 #endif
6411
6c8f6364bc48 util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents: 4302
diff changeset
625 lua_newtable(L);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
626
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
627 lua_newtable(L);
6789
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6643
diff changeset
628 luaL_setfuncs(L, Reg_base64, 0);
6412
0e94f89d0e62 util-src/*.c: Use the more concise lua_setfield
Kim Alvefur <zash@zash.se>
parents: 6411
diff changeset
629 lua_setfield(L, -2, "base64");
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
630
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
631 lua_newtable(L);
6789
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6643
diff changeset
632 luaL_setfuncs(L, Reg_stringprep, 0);
6412
0e94f89d0e62 util-src/*.c: Use the more concise lua_setfield
Kim Alvefur <zash@zash.se>
parents: 6411
diff changeset
633 lua_setfield(L, -2, "stringprep");
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
634
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
635 lua_newtable(L);
6789
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6643
diff changeset
636 luaL_setfuncs(L, Reg_idna, 0);
6412
0e94f89d0e62 util-src/*.c: Use the more concise lua_setfield
Kim Alvefur <zash@zash.se>
parents: 6411
diff changeset
637 lua_setfield(L, -2, "idna");
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
638
6592
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
639 lua_newtable(L);
6789
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6643
diff changeset
640 luaL_setfuncs(L, Reg_utf8, 0);
6604
478308ee29dd Merge 0.9->0.10 again
Kim Alvefur <zash@zash.se>
parents: 6413 6592
diff changeset
641 lua_setfield(L, -2, "utf8");
6592
141afe8a167b util.encodings: Expose UTF-8 validation and length checking functions
Kim Alvefur <zash@zash.se>
parents: 6591
diff changeset
642
9976
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
643 #ifdef USE_STRINGPREP_ICU
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
644 lua_newtable(L);
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
645 lua_pushcfunction(L, Lskeleton);
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
646 lua_setfield(L, -2, "skeleton");
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
647 lua_setfield(L, -2, "confusable");
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
648 #endif
0e2f663d0714 util.encodings: Add binding to confusables skeleton function in ICU
Kim Alvefur <zash@zash.se>
parents: 9973
diff changeset
649
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
650 lua_pushliteral(L, "-3.14");
6412
0e94f89d0e62 util-src/*.c: Use the more concise lua_setfield
Kim Alvefur <zash@zash.se>
parents: 6411
diff changeset
651 lua_setfield(L, -2, "version");
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
652 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
653 }