Advertisement
slovacus

base64 mysql with tables temporary

Mar 19th, 2012
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.86 KB | None | 0 0
  1. -- --------------------------------------------------------------------------------
  2. -- Routine DDL
  3. -- Note: comments before and after the routine body will not be stored by the server
  4. -- --------------------------------------------------------------------------------
  5. DELIMITER $$
  6.  
  7. CREATE DEFINER=`root`@`localhost` FUNCTION `BASE64_ENCODE`(INPUT BLOB) RETURNS BLOB
  8.     DETERMINISTIC
  9.     SQL SECURITY INVOKER
  10. BEGIN
  11.         DECLARE ret BLOB DEFAULT '';
  12.         DECLARE done TINYINT DEFAULT 0;
  13.  
  14.         IF INPUT IS NULL THEN
  15.                 RETURN NULL;
  16.         END IF;
  17.  
  18. CREATE TEMPORARY TABLE base64_data (c CHAR(1) BINARY, val TINYINT)ENGINE=MEMORY;
  19.         INSERT INTO base64_data VALUES
  20.                 ('A',0), ('B',1), ('C',2), ('D',3), ('E',4), ('F',5), ('G',6), ('H',7), ('I',8), ('J',9),
  21.                 ('K',10), ('L',11), ('M',12), ('N',13), ('O',14), ('P',15), ('Q',16), ('R',17), ('S',18), ('T',19),
  22.                 ('U',20), ('V',21), ('W',22), ('X',23), ('Y',24), ('Z',25), ('a',26), ('b',27), ('c',28), ('d',29),
  23.                 ('e',30), ('f',31), ('g',32), ('h',33), ('i',34), ('j',35), ('k',36), ('l',37), ('m',38), ('n',39),
  24.                 ('o',40), ('p',41), ('q',42), ('r',43), ('s',44), ('t',45), ('u',46), ('v',47), ('w',48), ('x',49),
  25.                 ('y',50), ('z',51), ('0',52), ('1',53), ('2',54), ('3',55), ('4',56), ('5',57), ('6',58), ('7',59),
  26.                 ('8',60), ('9',61), ('+',62), ('/',63), ('=',0) ;
  27.  
  28.  
  29. each_block:
  30.         WHILE NOT done DO BEGIN
  31.                 DECLARE accum_value BIGINT UNSIGNED DEFAULT 0;
  32.                 DECLARE in_count TINYINT DEFAULT 0;
  33.                 DECLARE out_count TINYINT;
  34.  
  35. each_input_char:
  36.                 WHILE in_count < 3 DO BEGIN
  37.                         DECLARE first_char CHAR(1);
  38.        
  39.                         IF LENGTH(INPUT) = 0 THEN
  40.                                 SET done = 1;
  41.                                 SET accum_value = accum_value << (8 * (3 - in_count));
  42.                                 LEAVE each_input_char;
  43.                         END IF;
  44.        
  45.                         SET first_char = SUBSTRING(INPUT,1,1);
  46.                         SET INPUT = SUBSTRING(INPUT,2);
  47.        
  48.                         SET accum_value = (accum_value << 8) + ASCII(first_char);
  49.  
  50.                         SET in_count = in_count + 1;
  51.                 END; END WHILE;
  52.  
  53.                 -- We've now accumulated 24 bits; deaccumulate into base64 characters
  54.  
  55.                 -- We have to work from the left, so use the third byte position and shift left
  56.                 CASE
  57.                         WHEN in_count = 3 THEN SET out_count = 4;
  58.                         WHEN in_count = 2 THEN SET out_count = 3;
  59.                         WHEN in_count = 1 THEN SET out_count = 2;
  60.                         ELSE RETURN ret;
  61.                 END CASE;
  62.  
  63.                 WHILE out_count > 0 DO BEGIN
  64.                         BEGIN
  65.                                 DECLARE out_char CHAR(1);
  66.                                 DECLARE base64_getval CURSOR FOR SELECT c FROM base64_data WHERE val = (accum_value >> 18);
  67.  
  68.                                 OPEN base64_getval;
  69.                                 FETCH base64_getval INTO out_char;
  70.                                 CLOSE base64_getval;
  71.  
  72.                                 SET ret = CONCAT(ret,out_char);
  73.                                 SET out_count = out_count - 1;
  74.                                 SET accum_value = accum_value << 6 & 0xffffff;
  75.                         END;
  76.                 END; END WHILE;
  77.  
  78.                 CASE
  79.                         WHEN in_count = 2 THEN SET ret = CONCAT(ret,'=');
  80.                         WHEN in_count = 1 THEN SET ret = CONCAT(ret,'==');
  81.                         ELSE BEGIN END;
  82.                 END CASE;
  83.        
  84.         END; END WHILE;
  85.  
  86.  DROP TEMPORARY TABLE IF EXISTS base64_data;
  87.  
  88.         RETURN ret;
  89.  
  90. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement