Fri, 16 Dec 2011 23:21:28 +0000
Initial commit with my changes.
Originally imported from yaml-2.0.tar.gz: http://files.luaforge.net/releases/yaml/yaml/0.2/yaml-0.2.tar.gz
0
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | #include <lua.h> |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | #include <lauxlib.h> |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | #include "b64.h" |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | int frombase64(lua_State *L, const unsigned char *str, unsigned int len) { |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | int d = 0, dlast = 0, phase = 0; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | unsigned char c; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | static int table[256] = { |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */ |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | }; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | luaL_Buffer b; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | luaL_buffinit(L, &b); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | for (; len--; ++str) { |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | d = table[(int)*str]; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | if (d == -1) continue; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | switch(phase) { |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | case 0: |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | ++phase; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | break; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | case 1: |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | c = ((dlast << 2) | ((d & 0x30) >> 4)); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | luaL_addchar(&b, c); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | ++phase; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | break; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | case 2: |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | c = (((dlast & 0xf) << 4) | ((d & 0x3c) >> 2)); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | luaL_addchar(&b, c); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | ++phase; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | break; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | case 3: |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | c = (((dlast & 0x03 ) << 6) | d); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | luaL_addchar(&b, c); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | phase = 0; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | break; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | } |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | dlast = d; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | } |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | luaL_pushresult(&b); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | return 1; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | } |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | static void b64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n) { |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | static const char code[] = |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | unsigned long tuple = c3 + 256UL * (c2 + 256UL * c1); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | int i; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | char s[4]; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | for (i = 0; i < 4; i++) { |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | s[3-i] = code[tuple % 64]; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | tuple /= 64; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | } |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | for (i = n+1; i < 4; i++) s[i] = '='; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | luaL_addlstring(b, s, 4); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | } |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | int tobase64(lua_State *L, int pos) { |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | size_t l; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | const unsigned char *s = (const unsigned char*)luaL_checklstring(L, pos, &l); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | luaL_Buffer b; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | int n; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | luaL_buffinit(L, &b); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | for (n = l / 3; n--; s += 3) |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | b64_encode(&b, s[0], s[1], s[2], 3); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | switch (l % 3) { |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 | case 1: |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | b64_encode(&b, s[0], 0, 0, 1); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | break; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 | case 2: |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 | b64_encode(&b, s[0], s[1], 0, 2); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 | break; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 | } |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 | luaL_pushresult(&b); |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 | return 1; |
3ebc0316f54f
Initial commit with my changes.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 | } |