Software /
code /
prosody
Annotate
util-src/encodings.c @ 618:69de678792d6
Pass HTTP request object to callback
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 12 Dec 2008 04:30:35 +0000 |
parent | 601:6cb908ef01c8 |
child | 766:433a5226267f |
rev | line source |
---|---|
520 | 1 /* Prosody IM v0.1 |
2 -- Copyright (C) 2008 Matthew Wild | |
3 -- Copyright (C) 2008 Waqas Hussain | |
4 -- | |
5 -- This program is free software; you can redistribute it and/or | |
6 -- modify it under the terms of the GNU General Public License | |
7 -- as published by the Free Software Foundation; either version 2 | |
8 -- of the License, or (at your option) any later version. | |
9 -- | |
10 -- This program is distributed in the hope that it will be useful, | |
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 -- GNU General Public License for more details. | |
14 -- | |
15 -- You should have received a copy of the GNU General Public License | |
16 -- along with this program; if not, write to the Free Software | |
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
18 */ | |
19 | |
20 | |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 /* |
417 | 22 * encodings.c |
23 * Lua library for base64, stringprep and idna encodings | |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 |
441
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
26 // Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way |
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
27 #define _CRT_SECURE_NO_DEPRECATE |
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
28 |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 #include <string.h> |
473
22b0e654c4cf
To use free you should include stdlib.h rather than malloc.h according to man page of free() and various C reference websites.
Tobias Markmann <tm@ayena.de>
parents:
441
diff
changeset
|
30 #include <stdlib.h> |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 #include "lua.h" |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 #include "lauxlib.h" |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 /***************** BASE64 *****************/ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 static const char code[]= |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 |
474
666e22848890
Use unsigned int instead of uint because uint is already in use on OS X.
Tobias Markmann <tm@ayena.de>
parents:
473
diff
changeset
|
40 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n) |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 unsigned long tuple=c3+256UL*(c2+256UL*c1); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 int i; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 char s[4]; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 for (i=0; i<4; i++) { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 s[3-i] = code[tuple % 64]; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 tuple /= 64; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 for (i=n+1; i<4; i++) s[i]='='; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 luaL_addlstring(b,s,4); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 static int Lbase64_encode(lua_State *L) /** encode(s) */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 size_t l; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 luaL_Buffer b; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 int n; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 luaL_buffinit(L,&b); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 switch (l%3) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 case 1: base64_encode(&b,s[0],0,0,1); break; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 case 2: base64_encode(&b,s[0],s[1],0,2); break; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 luaL_pushresult(&b); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1)); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 char s[3]; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 switch (--n) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 { |
441
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
76 case 3: s[2]=(char) tuple; |
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
77 case 2: s[1]=(char) (tuple >> 8); |
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
78 case 1: s[0]=(char) (tuple >> 16); |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 luaL_addlstring(b,s,n); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 static int Lbase64_decode(lua_State *L) /** decode(s) */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 size_t l; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 const char *s=luaL_checklstring(L,1,&l); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 luaL_Buffer b; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 int n=0; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 char t[4]; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 luaL_buffinit(L,&b); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 for (;;) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 { |
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
|
93 int c=*s++; |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 switch (c) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 const char *p; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 default: |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 p=strchr(code,c); if (p==NULL) return 0; |
441
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
99 t[n++]= (char) (p-code); |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 if (n==4) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 base64_decode(&b,t[0],t[1],t[2],t[3],4); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 n=0; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 break; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 case '=': |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 switch (n) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 case 1: base64_decode(&b,t[0],0,0,0,1); break; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 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
|
111 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
|
112 } |
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
|
113 n=0; |
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
|
114 break; |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 case 0: |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 luaL_pushresult(&b); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 case '\n': case '\r': case '\t': case ' ': case '\f': case '\b': |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 break; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 return 0; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 static const luaL_Reg Reg_base64[] = |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 { "encode", Lbase64_encode }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 { "decode", Lbase64_decode }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 { NULL, NULL } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 }; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 /***************** STRINGPREP *****************/ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 #include <stringprep.h> |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 size_t len; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 const char *s = luaL_checklstring(L, 1, &len); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 char string[1024]; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 int ret; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 if (len >= 1024) { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 lua_pushnil(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 return 1; // TODO return error message |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 strcpy(string, s); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 ret = stringprep(string, 1024, 0, profile); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 if (ret == STRINGPREP_OK) { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 lua_pushstring(L, string); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 } else { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 lua_pushnil(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 return 1; // TODO return error message |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 #define MAKE_PREP_FUNC(myFunc, prep) \ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 static const luaL_Reg Reg_stringprep[] = |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 { "nameprep", Lstringprep_nameprep }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 { "nodeprep", Lstringprep_nodeprep }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 { "resourceprep", Lstringprep_resourceprep }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 { "saslprep", Lstringprep_saslprep }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 { NULL, NULL } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
172 }; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 /***************** IDNA *****************/ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
175 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 #include <idna.h> |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
178 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 size_t len; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 const char *s = luaL_checklstring(L, 1, &len); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 char* output = NULL; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 int ret = idna_to_ascii_8z(s, &output, 0); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 if (ret == IDNA_SUCCESS) { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 lua_pushstring(L, output); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 if (output) free(output); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 } else { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 lua_pushnil(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 if (output) free(output); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 return 1; // TODO return error message |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 size_t len; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 const char *s = luaL_checklstring(L, 1, &len); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 char* output = NULL; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 int ret = idna_to_unicode_8z8z(s, &output, 0); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 if (ret == IDNA_SUCCESS) { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 lua_pushstring(L, output); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 if (output) free(output); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
204 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 } else { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
206 lua_pushnil(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
207 if (output) free(output); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 return 1; // TODO return error message |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 static const luaL_Reg Reg_idna[] = |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
213 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 { "to_ascii", Lidna_to_ascii }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 { "to_unicode", Lidna_to_unicode }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
216 { NULL, NULL } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 }; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 /***************** end *****************/ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 static const luaL_Reg Reg[] = |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
223 { NULL, NULL } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
224 }; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 |
432
fb19a5c14d44
Fix for hashes.c and encodings.c to rename luaopen for util_*
Matthew Wild <mwild1@gmail.com>
parents:
417
diff
changeset
|
226 LUALIB_API int luaopen_util_encodings(lua_State *L) |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 luaL_register(L, "encodings", Reg); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
229 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
230 lua_pushliteral(L, "base64"); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
231 lua_newtable(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
232 luaL_register(L, NULL, Reg_base64); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
233 lua_settable(L,-3); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
234 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
235 lua_pushliteral(L, "stringprep"); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
236 lua_newtable(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
237 luaL_register(L, NULL, Reg_stringprep); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
238 lua_settable(L,-3); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
239 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
240 lua_pushliteral(L, "idna"); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
241 lua_newtable(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
242 luaL_register(L, NULL, Reg_idna); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
243 lua_settable(L,-3); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
244 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
245 lua_pushliteral(L, "version"); /** version */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
246 lua_pushliteral(L, "-3.14"); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
247 lua_settable(L,-3); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
248 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
249 } |