Software /
code /
prosody
Annotate
util-src/encodings.c @ 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.
author | Tobias Markmann <tm@ayena.de> |
---|---|
date | Fri, 28 Nov 2008 18:36:04 +0100 |
parent | 441:4089b62b510c |
child | 474:666e22848890 |
rev | line source |
---|---|
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 /* |
417 | 2 * encodings.c |
3 * 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
|
4 */ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 |
441
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
6 // 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
|
7 #define _CRT_SECURE_NO_DEPRECATE |
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
8 |
415
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 #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
|
10 #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
|
11 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 #include "lua.h" |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 #include "lauxlib.h" |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 /***************** BASE64 *****************/ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 #define uint unsigned int |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 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
|
20 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 static void base64_encode(luaL_Buffer *b, uint c1, uint c2, uint c3, int n) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 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
|
25 int i; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 char s[4]; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 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
|
28 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
|
29 tuple /= 64; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 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
|
32 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
|
33 } |
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 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
|
36 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 size_t l; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 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
|
39 luaL_Buffer b; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 int n; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 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
|
42 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
|
43 switch (l%3) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 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
|
46 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
|
47 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 luaL_pushresult(&b); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 } |
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 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
|
53 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 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
|
55 char s[3]; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 switch (--n) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 { |
441
4089b62b510c
Minor changes to C files (to prevent compiler warnings)
Waqas Hussain <waqas20@gmail.com>
parents:
432
diff
changeset
|
58 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
|
59 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
|
60 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
|
61 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 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
|
63 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 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
|
66 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 size_t l; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 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
|
69 luaL_Buffer b; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 int n=0; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 char t[4]; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 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
|
73 for (;;) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 int c=*s++; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 switch (c) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 const char *p; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 default: |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 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
|
81 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
|
82 if (n==4) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 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
|
85 n=0; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 break; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 case '=': |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 switch (n) |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 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
|
92 case 2: base64_decode(&b,t[0],t[1],0,0,2); break; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 case 3: base64_decode(&b,t[0],t[1],t[2],0,3); break; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 case 0: |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 luaL_pushresult(&b); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 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
|
99 break; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 } |
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 return 0; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 } |
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 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
|
106 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 { "encode", Lbase64_encode }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 { "decode", Lbase64_decode }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 { NULL, NULL } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 }; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 /***************** STRINGPREP *****************/ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 #include <stringprep.h> |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 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
|
117 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 size_t len; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 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
|
120 char string[1024]; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 int ret; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 if (len >= 1024) { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 lua_pushnil(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 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
|
125 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 strcpy(string, s); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 } else { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 lua_pushnil(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 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
|
134 } |
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 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 #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
|
138 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
|
139 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 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
|
141 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
|
142 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
|
143 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
|
144 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 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
|
146 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 { "nameprep", Lstringprep_nameprep }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 { "nodeprep", Lstringprep_nodeprep }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 { "resourceprep", Lstringprep_resourceprep }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 { "saslprep", Lstringprep_saslprep }, |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 { NULL, NULL } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 }; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 /***************** IDNA *****************/ |
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 #include <idna.h> |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 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
|
159 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 size_t len; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 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
|
162 char* output = NULL; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 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
|
164 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
|
165 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
|
166 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
|
167 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 } else { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 lua_pushnil(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 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
|
171 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
|
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 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
175 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
|
176 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 size_t len; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
178 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
|
179 char* output = NULL; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 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
|
181 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
|
182 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
|
183 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
|
184 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 } else { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 lua_pushnil(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 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
|
188 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
|
189 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 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
|
193 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 { "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
|
195 { "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
|
196 { NULL, NULL } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 }; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 /***************** end *****************/ |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 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
|
202 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 { NULL, NULL } |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
204 }; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 |
432
fb19a5c14d44
Fix for hashes.c and encodings.c to rename luaopen for util_*
Matthew Wild <mwild1@gmail.com>
parents:
417
diff
changeset
|
206 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
|
207 { |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 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
|
209 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 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
|
211 lua_newtable(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 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
|
213 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
|
214 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 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
|
216 lua_newtable(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 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
|
218 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
|
219 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 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
|
221 lua_newtable(L); |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 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
|
223 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
|
224 |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 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
|
226 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
|
227 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
|
228 return 1; |
3cf7deea9971
Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
229 } |