Diffstat (limited to 'backend/php/src/plugins/base64_install.sql') (more/less context) (ignore whitespace changes)
-rw-r--r-- | backend/php/src/plugins/base64_install.sql | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/backend/php/src/plugins/base64_install.sql b/backend/php/src/plugins/base64_install.sql new file mode 100644 index 0000000..40401d6 --- a/dev/null +++ b/backend/php/src/plugins/base64_install.sql | |||
@@ -0,0 +1,172 @@ | |||
1 | -- base64.sql - MySQL base64 encoding/decoding functions | ||
2 | -- Copyright (C) 2006 Ian Gulliver | ||
3 | -- | ||
4 | -- This program is free software; you can redistribute it and/or modify | ||
5 | -- it under the terms of version 2 of the GNU General Public License as | ||
6 | -- published by the Free Software Foundation. | ||
7 | -- | ||
8 | -- This program is distributed in the hope that it will be useful, | ||
9 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | -- GNU General Public License for more details. | ||
12 | -- | ||
13 | -- You should have received a copy of the GNU General Public License | ||
14 | -- along with this program; if not, write to the Free Software | ||
15 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
16 | |||
17 | |||
18 | DROP TABLE IF EXISTS base64_data | | ||
19 | CREATE TABLE base64_data (c CHAR(1) BINARY, val TINYINT) | | ||
20 | INSERT INTO base64_data VALUES | ||
21 | ('A',0), ('B',1), ('C',2), ('D',3), ('E',4), ('F',5), ('G',6), ('H',7), ('I',8), ('J',9), | ||
22 | ('K',10), ('L',11), ('M',12), ('N',13), ('O',14), ('P',15), ('Q',16), ('R',17), ('S',18), ('T',19), | ||
23 | ('U',20), ('V',21), ('W',22), ('X',23), ('Y',24), ('Z',25), ('a',26), ('b',27), ('c',28), ('d',29), | ||
24 | ('e',30), ('f',31), ('g',32), ('h',33), ('i',34), ('j',35), ('k',36), ('l',37), ('m',38), ('n',39), | ||
25 | ('o',40), ('p',41), ('q',42), ('r',43), ('s',44), ('t',45), ('u',46), ('v',47), ('w',48), ('x',49), | ||
26 | ('y',50), ('z',51), ('0',52), ('1',53), ('2',54), ('3',55), ('4',56), ('5',57), ('6',58), ('7',59), | ||
27 | ('8',60), ('9',61), ('+',62), ('/',63), ('=',0) | | ||
28 | |||
29 | |||
30 | DROP FUNCTION IF EXISTS BASE64_DECODE | | ||
31 | CREATE FUNCTION BASE64_DECODE (input BLOB) | ||
32 | RETURNS BLOB | ||
33 | CONTAINS SQL | ||
34 | DETERMINISTIC | ||
35 | SQL SECURITY INVOKER | ||
36 | BEGIN | ||
37 | DECLARE ret BLOB DEFAULT ''; | ||
38 | DECLARE done TINYINT DEFAULT 0; | ||
39 | |||
40 | IF input IS NULL THEN | ||
41 | RETURN NULL; | ||
42 | END IF; | ||
43 | |||
44 | each_block: | ||
45 | WHILE NOT done DO BEGIN | ||
46 | DECLARE accum_value BIGINT UNSIGNED DEFAULT 0; | ||
47 | DECLARE in_count TINYINT DEFAULT 0; | ||
48 | DECLARE out_count TINYINT DEFAULT 3; | ||
49 | |||
50 | each_input_char: | ||
51 | WHILE in_count < 4 DO BEGIN | ||
52 | DECLARE first_char CHAR(1); | ||
53 | |||
54 | IF LENGTH(input) = 0 THEN | ||
55 | RETURN ret; | ||
56 | END IF; | ||
57 | |||
58 | SET first_char = SUBSTRING(input,1,1); | ||
59 | SET input = SUBSTRING(input,2); | ||
60 | |||
61 | BEGIN | ||
62 | DECLARE tempval TINYINT UNSIGNED; | ||
63 | DECLARE error TINYINT DEFAULT 0; | ||
64 | DECLARE base64_getval CURSOR FOR SELECT val FROM base64_data WHERE c = first_char; | ||
65 | DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET error = 1; | ||
66 | |||
67 | OPEN base64_getval; | ||
68 | FETCH base64_getval INTO tempval; | ||
69 | CLOSE base64_getval; | ||
70 | |||
71 | IF error THEN | ||
72 | ITERATE each_input_char; | ||
73 | END IF; | ||
74 | |||
75 | SET accum_value = (accum_value << 6) + tempval; | ||
76 | END; | ||
77 | |||
78 | SET in_count = in_count + 1; | ||
79 | |||
80 | IF first_char = '=' THEN | ||
81 | SET done = 1; | ||
82 | SET out_count = out_count - 1; | ||
83 | END IF; | ||
84 | END; END WHILE; | ||
85 | |||
86 | -- We've now accumulated 24 bits; deaccumulate into bytes | ||
87 | |||
88 | -- We have to work from the left, so use the third byte position and shift left | ||
89 | WHILE out_count > 0 DO BEGIN | ||
90 | SET ret = CONCAT(ret,CHAR((accum_value & 0xff0000) >> 16)); | ||
91 | SET out_count = out_count - 1; | ||
92 | SET accum_value = (accum_value << 8) & 0xffffff; | ||
93 | END; END WHILE; | ||
94 | |||
95 | END; END WHILE; | ||
96 | |||
97 | RETURN ret; | ||
98 | END | | ||
99 | |||
100 | DROP FUNCTION IF EXISTS BASE64_ENCODE | | ||
101 | CREATE FUNCTION BASE64_ENCODE (input BLOB) | ||
102 | RETURNS BLOB | ||
103 | CONTAINS SQL | ||
104 | DETERMINISTIC | ||
105 | SQL SECURITY INVOKER | ||
106 | BEGIN | ||
107 | DECLARE ret BLOB DEFAULT ''; | ||
108 | DECLARE done TINYINT DEFAULT 0; | ||
109 | |||
110 | IF input IS NULL THEN | ||
111 | RETURN NULL; | ||
112 | END IF; | ||
113 | |||
114 | each_block: | ||
115 | WHILE NOT done DO BEGIN | ||
116 | DECLARE accum_value BIGINT UNSIGNED DEFAULT 0; | ||
117 | DECLARE in_count TINYINT DEFAULT 0; | ||
118 | DECLARE out_count TINYINT; | ||
119 | |||
120 | each_input_char: | ||
121 | WHILE in_count < 3 DO BEGIN | ||
122 | DECLARE first_char CHAR(1); | ||
123 | |||
124 | IF LENGTH(input) = 0 THEN | ||
125 | SET done = 1; | ||
126 | SET accum_value = accum_value << (8 * (3 - in_count)); | ||
127 | LEAVE each_input_char; | ||
128 | END IF; | ||
129 | |||
130 | SET first_char = SUBSTRING(input,1,1); | ||
131 | SET input = SUBSTRING(input,2); | ||
132 | |||
133 | SET accum_value = (accum_value << 8) + ASCII(first_char); | ||
134 | |||
135 | SET in_count = in_count + 1; | ||
136 | END; END WHILE; | ||
137 | |||
138 | -- We've now accumulated 24 bits; deaccumulate into base64 characters | ||
139 | |||
140 | -- We have to work from the left, so use the third byte position and shift left | ||
141 | CASE | ||
142 | WHEN in_count = 3 THEN SET out_count = 4; | ||
143 | WHEN in_count = 2 THEN SET out_count = 3; | ||
144 | WHEN in_count = 1 THEN SET out_count = 2; | ||
145 | ELSE RETURN ret; | ||
146 | END CASE; | ||
147 | |||
148 | WHILE out_count > 0 DO BEGIN | ||
149 | BEGIN | ||
150 | DECLARE out_char CHAR(1); | ||
151 | DECLARE base64_getval CURSOR FOR SELECT c FROM base64_data WHERE val = (accum_value >> 18); | ||
152 | |||
153 | OPEN base64_getval; | ||
154 | FETCH base64_getval INTO out_char; | ||
155 | CLOSE base64_getval; | ||
156 | |||
157 | SET ret = CONCAT(ret,out_char); | ||
158 | SET out_count = out_count - 1; | ||
159 | SET accum_value = accum_value << 6 & 0xffffff; | ||
160 | END; | ||
161 | END; END WHILE; | ||
162 | |||
163 | CASE | ||
164 | WHEN in_count = 2 THEN SET ret = CONCAT(ret,'='); | ||
165 | WHEN in_count = 1 THEN SET ret = CONCAT(ret,'=='); | ||
166 | ELSE BEGIN END; | ||
167 | END CASE; | ||
168 | |||
169 | END; END WHILE; | ||
170 | |||
171 | RETURN ret; | ||
172 | END | | ||