Annotate

util-src/encodings.c @ 415:3cf7deea9971

Added util-src/encodings.c - support for base64, stringprep and idna encodings
author Waqas Hussain <waqas20@gmail.com>
date Wed, 26 Nov 2008 01:45:25 +0500
child 417:d60e63379284
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 /*
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 * xxpath.c
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 * An implementation of a subset of xpath for Lua 5.1
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 * Waqas Hussain <waqas20@gmail.com>
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 * 05 Oct 2008 15:28:15
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 */
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 #include <string.h>
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 #include "lua.h"
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 #include "lauxlib.h"
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 /*const char* hex_tab = "0123456789abcdef";
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 void toHex(const char* in, int length, char* out) {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 int i;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 for (i = 0; i < length; i++) {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 out[i*2] = hex_tab[(in[i] >> 4) & 0xF];
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 out[i*2+1] = hex_tab[(in[i]) & 0xF];
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 //out[i*2] = 0;
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
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 /***************** BASE64 *****************/
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 #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
26
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 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
28 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 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
31 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 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
33 int i;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 char s[4];
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 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
36 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
37 tuple /= 64;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 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
40 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
41 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 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
44 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 size_t l;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 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
47 luaL_Buffer b;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 int n;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 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
50 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
51 switch (l%3)
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 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
54 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
55 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 luaL_pushresult(&b);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 return 1;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 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
61 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 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
63 char s[3];
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 switch (--n)
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 case 3: s[2]=tuple;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 case 2: s[1]=tuple >> 8;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 case 1: s[0]=tuple >> 16;
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 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
71 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 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
74 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 size_t l;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 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
77 luaL_Buffer b;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 int n=0;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 char t[4];
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 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
81 for (;;)
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 int c=*s++;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 switch (c)
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 const char *p;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 default:
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 p=strchr(code,c); if (p==NULL) return 0;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 t[n++]= p-code;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 if (n==4)
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 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
93 n=0;
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 break;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 case '=':
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 switch (n)
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 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
100 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
101 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
102 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 case 0:
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 luaL_pushresult(&b);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 return 1;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 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
107 break;
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 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 return 0;
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
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 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
114 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 { "encode", Lbase64_encode },
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 { "decode", Lbase64_decode },
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 { NULL, NULL }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 };
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 /***************** STRINGPREP *****************/
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 #include <stringprep.h>
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 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
125 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 size_t len;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 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
128 char string[1024];
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 int ret;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 if (len >= 1024) {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 lua_pushnil(L);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 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
133 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 strcpy(string, s);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 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
136 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
137 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
138 return 1;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 } else {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 lua_pushnil(L);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 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
142 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 }
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 #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
146 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
147
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 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
149 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
150 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
151 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
152
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 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
154 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 { "nameprep", Lstringprep_nameprep },
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 { "nodeprep", Lstringprep_nodeprep },
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 { "resourceprep", Lstringprep_resourceprep },
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 { "saslprep", Lstringprep_saslprep },
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 { NULL, NULL }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 };
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 /***************** IDNA *****************/
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 #include <idna.h>
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 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
167 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 size_t len;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 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
170 char* output = NULL;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 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
172 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
173 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
174 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
175 return 1;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 } else {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 lua_pushnil(L);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 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
179 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
180 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 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
184 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 size_t len;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186 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
187 char* output = NULL;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 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
189 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
190 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
191 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
192 return 1;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 } else {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 lua_pushnil(L);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 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
196 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
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
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 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
201 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 { "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
203 { "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
204 { NULL, NULL }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 };
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 /***************** end *****************/
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 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
210 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 { NULL, NULL }
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 };
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 LUALIB_API int luaopen_encodings(lua_State *L)
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 {
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 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
217
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 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
219 lua_newtable(L);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 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
221 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
222
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 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
224 lua_newtable(L);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 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
226 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
227
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 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
229 lua_newtable(L);
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 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
231 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
232
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 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
234 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
235 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
236 return 1;
3cf7deea9971 Added util-src/encodings.c - support for base64, stringprep and idna encodings
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237 }