util-src/encodings.cpp

Tue, 14 Dec 2010 15:03:37 +0100

author
Tobias Markmann <tm@ayena.de>
date
Tue, 14 Dec 2010 15:03:37 +0100
changeset 3762
f02bac902a1e
parent 2923
util-src/encodings.c@b7049746bd29
child 3764
323169f229fa
permissions
-rw-r--r--

util.encodings: Support for ICU for IDNA operations.

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
520
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
4 --
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
5 -- 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
6 -- 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
7 --
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 * encodings.c
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
12 * Lua library for base64, stringprep and idna encodings
520
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
13 */
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
14
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
15 // Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
16 #define _CRT_SECURE_NO_DEPRECATE
520
e96ac4bb6dd8 and the C files too
Matthew Wild <mwild1@gmail.com>
parents: 474
diff changeset
17
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
18 extern "C" {
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"
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
23 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
24 /***************** BASE64 *****************/
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
25
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
26 #define LUALIB_API extern "C"
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
27
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
28 static const char code[]=
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
29 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
30
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
31 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
32 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
33 unsigned long tuple=c3+256UL*(c2+256UL*c1);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
34 int i;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
35 char s[4];
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
36 for (i=0; i<4; i++) {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
37 s[3-i] = code[tuple % 64];
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
38 tuple /= 64;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
39 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
40 for (i=n+1; i<4; i++) s[i]='=';
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
41 luaL_addlstring(b,s,4);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
42 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
43
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
44 static int Lbase64_encode(lua_State *L) /** encode(s) */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
45 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
46 size_t l;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
47 const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
48 luaL_Buffer b;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
49 int n;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
50 luaL_buffinit(L,&b);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
51 for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
52 switch (l%3)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
53 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
54 case 1: base64_encode(&b,s[0],0,0,1); break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
55 case 2: base64_encode(&b,s[0],s[1],0,2); break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
56 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
57 luaL_pushresult(&b);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
58 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
59 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
60
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
61 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
62 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
63 unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
64 char s[3];
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
65 switch (--n)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
66 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
67 case 3: s[2]=(char) tuple;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
68 case 2: s[1]=(char) (tuple >> 8);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
69 case 1: s[0]=(char) (tuple >> 16);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
70 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
71 luaL_addlstring(b,s,n);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
72 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
73
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
74 static int Lbase64_decode(lua_State *L) /** decode(s) */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
75 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
76 size_t l;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
77 const char *s=luaL_checklstring(L,1,&l);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
78 luaL_Buffer b;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
79 int n=0;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
80 char t[4];
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
81 luaL_buffinit(L,&b);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
82 for (;;)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
83 {
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
84 int c=*s++;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
85 switch (c)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
86 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
87 const char *p;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
88 default:
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
89 p=strchr(code,c); if (p==NULL) return 0;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
90 t[n++]= (char) (p-code);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
91 if (n==4)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
92 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
93 base64_decode(&b,t[0],t[1],t[2],t[3],4);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
94 n=0;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
95 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
96 break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
97 case '=':
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
98 switch (n)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
99 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
100 case 1: base64_decode(&b,t[0],0,0,0,1); break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
101 case 2: base64_decode(&b,t[0],t[1],0,0,2); 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
102 case 3: base64_decode(&b,t[0],t[1],t[2],0,3); break;
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
103 }
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
104 n=0;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
105 break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
106 case 0:
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
107 luaL_pushresult(&b);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
108 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
109 case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
110 break;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
111 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
112 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
113 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
114
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
115 static const luaL_Reg Reg_base64[] =
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
116 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
117 { "encode", Lbase64_encode },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
118 { "decode", Lbase64_decode },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
119 { NULL, NULL }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
120 };
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
121
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
122 /***************** STRINGPREP *****************/
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
123 #ifndef USE_STRINGPREP_ICU
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
124 /****************** libidn ********************/
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
125 #include <stringprep.h>
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
126
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
127 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
128 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
129 size_t len;
1854
7e055cc6bc90 util.encodings: Fixed: Last change was not ANSI C compatible.
Waqas Hussain <waqas20@gmail.com>
parents: 1826
diff changeset
130 const char *s;
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
131 char string[1024];
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
132 int ret;
1844
a4a8fe2a560c util.encodings: Don't throw an error but return nil when passed nil or a non-string value
Matthew Wild <mwild1@gmail.com>
parents: 1826
diff changeset
133 if(!lua_isstring(L, 1)) {
a4a8fe2a560c util.encodings: Don't throw an error but return nil when passed nil or a non-string value
Matthew Wild <mwild1@gmail.com>
parents: 1826
diff changeset
134 lua_pushnil(L);
a4a8fe2a560c util.encodings: Don't throw an error but return nil when passed nil or a non-string value
Matthew Wild <mwild1@gmail.com>
parents: 1826
diff changeset
135 return 1;
a4a8fe2a560c util.encodings: Don't throw an error but return nil when passed nil or a non-string value
Matthew Wild <mwild1@gmail.com>
parents: 1826
diff changeset
136 }
1854
7e055cc6bc90 util.encodings: Fixed: Last change was not ANSI C compatible.
Waqas Hussain <waqas20@gmail.com>
parents: 1826
diff changeset
137 s = lua_tolstring(L, 1, &len);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
138 if (len >= 1024) {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
139 lua_pushnil(L);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
140 return 1; // TODO return error message
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
141 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
142 strcpy(string, s);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
143 ret = stringprep(string, 1024, 0, profile);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
144 if (ret == STRINGPREP_OK) {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
145 lua_pushstring(L, string);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
146 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
147 } else {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
148 lua_pushnil(L);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
149 return 1; // TODO return error message
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
150 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
151 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
152
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
153 #define MAKE_PREP_FUNC(myFunc, prep) \
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
154 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
155
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
156 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
157 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
158 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
159 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
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 static const luaL_Reg Reg_stringprep[] =
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
162 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
163 { "nameprep", Lstringprep_nameprep },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
164 { "nodeprep", Lstringprep_nodeprep },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
165 { "resourceprep", Lstringprep_resourceprep },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
166 { "saslprep", Lstringprep_saslprep },
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
167 { NULL, NULL }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
168 };
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
169
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
170 #else
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
171 #include <unicode/usprep.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
172 //#include <unicode/stringpiece.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
173 #include <unicode/unistr.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
174 #include <unicode/utrace.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
175
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
176 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
177 {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
178 size_t len;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
179 const char *s;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
180 UnicodeString ustr;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
181 UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
182 UChar dest[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
183 char output[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
184 if(!lua_isstring(L, 1)) {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
185 lua_pushnil(L);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
186 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
187 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
188 s = lua_tolstring(L, 1, &len);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
189 if (len >= 1024) {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
190 lua_pushnil(L);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
191 return 1; // TODO return error message
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
192 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
193 ustr = UnicodeString::fromUTF8(s);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
194 len = usprep_prepare(profile, ustr.getBuffer(), ustr.length(), dest, 1024, 0, NULL, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
195 if (U_FAILURE(err)) {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
196 lua_pushnil(L);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
197 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
198 } else {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
199 CheckedArrayByteSink output_sink(output, 1024);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
200 UnicodeString dest_str(TRUE, dest, len);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
201 dest_str.toUTF8(output_sink);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
202 lua_pushstring(L, output);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
203 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
204 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
205 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
206
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
207 UStringPrepProfile *icu_nameprep;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
208 UStringPrepProfile *icu_nodeprep;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
209 UStringPrepProfile *icu_resourceprep;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
210 UStringPrepProfile *icu_saslprep;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
211
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
212 /* initialize global ICU stringprep profiles */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
213 void init_icu()
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
214 {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
215 UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
216 utrace_setLevel(UTRACE_VERBOSE);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
217 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
218 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
219 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
220 icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
221 if (U_FAILURE(err)) fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
222 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
223
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
224 #define MAKE_PREP_FUNC(myFunc, prep) \
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
225 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
226
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
227 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
228 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
229 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
230 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
231
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
232 static const luaL_Reg Reg_stringprep[] =
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
233 {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
234 { "nameprep", Lstringprep_nameprep },
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
235 { "nodeprep", Lstringprep_nodeprep },
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
236 { "resourceprep", Lstringprep_resourceprep },
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
237 { "saslprep", Lstringprep_saslprep },
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
238 { NULL, NULL }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
239 };
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
240 #endif
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
241
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
242 /***************** IDNA *****************/
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
243 #ifndef USE_STRINGPREP_ICU
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
244 /****************** libidn ********************/
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
245 #include <idna.h>
1855
63b5e7ec6840 util.encodings: Included idn-free.h, which explicitly declares the idn_free function.
Waqas Hussain <waqas20@gmail.com>
parents: 1854
diff changeset
246 #include <idn-free.h>
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
247 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
248 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
249 size_t len;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
250 const char *s = luaL_checklstring(L, 1, &len);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
251 char* output = NULL;
2572
0584e157f073 util.encodings: Use STD3 ASCII rules for idna.to_ascii.
Waqas Hussain <waqas20@gmail.com>
parents: 1860
diff changeset
252 int ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
253 if (ret == IDNA_SUCCESS) {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
254 lua_pushstring(L, output);
1826
3d0db768be2f util.encodings: Fixed an issue with cross-module memory deallocation (crashes on some windows versions).
Waqas Hussain <waqas20@gmail.com>
parents: 894
diff changeset
255 idn_free(output);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
256 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
257 } else {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
258 lua_pushnil(L);
1826
3d0db768be2f util.encodings: Fixed an issue with cross-module memory deallocation (crashes on some windows versions).
Waqas Hussain <waqas20@gmail.com>
parents: 894
diff changeset
259 idn_free(output);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
260 return 1; // TODO return error message
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
261 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
262 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
263
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
264 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
265 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
266 size_t len;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
267 const char *s = luaL_checklstring(L, 1, &len);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
268 char* output = NULL;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
269 int ret = idna_to_unicode_8z8z(s, &output, 0);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
270 if (ret == IDNA_SUCCESS) {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
271 lua_pushstring(L, output);
1826
3d0db768be2f util.encodings: Fixed an issue with cross-module memory deallocation (crashes on some windows versions).
Waqas Hussain <waqas20@gmail.com>
parents: 894
diff changeset
272 idn_free(output);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
273 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
274 } else {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
275 lua_pushnil(L);
1826
3d0db768be2f util.encodings: Fixed an issue with cross-module memory deallocation (crashes on some windows versions).
Waqas Hussain <waqas20@gmail.com>
parents: 894
diff changeset
276 idn_free(output);
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
277 return 1; // TODO return error message
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
278 }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
279 }
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
280 #else
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
281 #include <unicode/ustdio.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
282 #include <unicode/uidna.h>
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
283 /* IDNA2003 or IDNA2008 ? ? ? */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
284 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
285 {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
286 size_t len;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
287 int32_t out_len;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
288 const char *s = luaL_checklstring(L, 1, &len);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
289 UnicodeString ustr;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
290 UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
291 UChar dest[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
292 char output[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
293
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
294 ustr = UnicodeString::fromUTF8(s);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
295 out_len = uidna_IDNToASCII(ustr.getBuffer(), ustr.length(), dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
296 if (U_FAILURE(err)) {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
297 lua_pushnil(L);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
298 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
299 } else {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
300 CheckedArrayByteSink output_sink(output, 1024);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
301 UnicodeString dest_str(TRUE, dest, out_len);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
302 dest_str.toUTF8(output_sink);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
303 lua_pushstring(L, output);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
304 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
305 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
306 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
307
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
308 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
309 {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
310 size_t len;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
311 int32_t out_len;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
312 const char *s = luaL_checklstring(L, 1, &len);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
313 UnicodeString ustr;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
314 UErrorCode err = U_ZERO_ERROR;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
315 UChar dest[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
316 char output[1024];
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
317
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
318 ustr = UnicodeString::fromUTF8(s);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
319 out_len = uidna_IDNToUnicode(ustr.getBuffer(), ustr.length(), dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
320 if (U_FAILURE(err)) {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
321 lua_pushnil(L);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
322 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
323 } else {
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
324 CheckedArrayByteSink output_sink(output, 1024);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
325 UnicodeString dest_str(TRUE, dest, out_len);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
326 dest_str.toUTF8(output_sink);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
327 lua_pushstring(L, output);
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
328 return 1;
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
329 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
330 }
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
331 #endif
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
332
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
333 static const luaL_Reg Reg_idna[] =
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
334 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
335 { "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
336 { "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
337 { NULL, NULL }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
338 };
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
339
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
340 /***************** end *****************/
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
341
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
342 static const luaL_Reg Reg[] =
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
343 {
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
344 { NULL, NULL }
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
345 };
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
346
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
347 LUALIB_API int luaopen_util_encodings(lua_State *L)
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
348 {
3762
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
349 #ifdef USE_STRINGPREP_ICU
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
350 init_icu();
f02bac902a1e util.encodings: Support for ICU for IDNA operations.
Tobias Markmann <tm@ayena.de>
parents: 2923
diff changeset
351 #endif
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
352 luaL_register(L, "encodings", Reg);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
353
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
354 lua_pushliteral(L, "base64");
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
355 lua_newtable(L);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
356 luaL_register(L, NULL, Reg_base64);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
357 lua_settable(L,-3);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
358
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
359 lua_pushliteral(L, "stringprep");
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
360 lua_newtable(L);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
361 luaL_register(L, NULL, Reg_stringprep);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
362 lua_settable(L,-3);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
363
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
364 lua_pushliteral(L, "idna");
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
365 lua_newtable(L);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
366 luaL_register(L, NULL, Reg_idna);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
367 lua_settable(L,-3);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
368
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
369 lua_pushliteral(L, "version"); /** version */
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
370 lua_pushliteral(L, "-3.14");
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
371 lua_settable(L,-3);
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
372 return 1;
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 601
diff changeset
373 }

mercurial