Merging TO_BASE64() and FROM_BASE64() from MySQL-5.6
This commit is contained in:
parent
9cbd53bfb2
commit
e33582d20d
@ -26,11 +26,21 @@ extern "C" {
|
||||
*/
|
||||
int base64_needed_encoded_length(int length_of_data);
|
||||
|
||||
/*
|
||||
Maximum length base64_encode_needed_length() can accept with no overflow.
|
||||
*/
|
||||
int base64_encode_max_arg_length(void);
|
||||
|
||||
/*
|
||||
Calculate how much memory needed for dst of base64_decode()
|
||||
*/
|
||||
int base64_needed_decoded_length(int length_of_encoded_data);
|
||||
|
||||
/*
|
||||
Maximum length base64_decode_needed_length() can accept with no overflow.
|
||||
*/
|
||||
int base64_decode_max_arg_length();
|
||||
|
||||
/*
|
||||
Encode data as a base64 string
|
||||
*/
|
||||
@ -40,7 +50,10 @@ int base64_encode(const void *src, size_t src_len, char *dst);
|
||||
Decode a base64 string into data
|
||||
*/
|
||||
int base64_decode(const char *src, size_t src_len,
|
||||
void *dst, const char **end_ptr);
|
||||
void *dst, const char **end_ptr, int flags);
|
||||
|
||||
/* Allow multuple chunks 'AAA= AA== AA==', binlog uses this */
|
||||
#define MY_BASE64_DECODE_ALLOW_MULTIPLE_CHUNKS 1
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -103,7 +103,8 @@ call mtr.add_suppression("Slave SQL.*Column 1 of table .test.char128_utf8. canno
|
||||
BINLOG '';
|
||||
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
|
||||
BINLOG '123';
|
||||
ERROR HY000: Decoding of base64 string failed
|
||||
BINLOG '-2079193929';
|
||||
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
|
||||
ERROR HY000: Decoding of base64 string failed
|
||||
BINLOG 'xç↓%~∙D╒ƒ╡';
|
||||
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
|
||||
ERROR HY000: Decoding of base64 string failed
|
||||
|
@ -161,8 +161,9 @@ call mtr.add_suppression("Slave SQL.*Column 1 of table .test.char128_utf8. canno
|
||||
--echo #
|
||||
--error ER_SYNTAX_ERROR
|
||||
BINLOG '';
|
||||
--error ER_BASE64_DECODE_ERROR
|
||||
BINLOG '123';
|
||||
--error ER_SYNTAX_ERROR
|
||||
--error ER_BASE64_DECODE_ERROR
|
||||
BINLOG '-2079193929';
|
||||
--error ER_SYNTAX_ERROR
|
||||
--error ER_BASE64_DECODE_ERROR
|
||||
BINLOG 'xç↓%~∙D╒ƒ╡';
|
||||
|
@ -1559,3 +1559,118 @@ drop table t1,t2;
|
||||
--echo # End of 5.5 tests
|
||||
--echo #
|
||||
|
||||
|
||||
--echo #
|
||||
--echo # Start of 5.6 tests
|
||||
--echo #
|
||||
|
||||
--echo #
|
||||
--echo # WL#5510 Functions to_base64 and from_base64
|
||||
--echo #
|
||||
let $1=64;
|
||||
while($1)
|
||||
{
|
||||
dec $1;
|
||||
eval CREATE TABLE t1 AS SELECT TO_BASE64(REPEAT('a',$1)) AS to_base64;
|
||||
SHOW CREATE TABLE t1;
|
||||
SELECT to_base64, LENGTH(to_base64) FROM t1;
|
||||
CREATE TABLE t2 AS SELECT from_base64(to_base64) AS from_base64 FROM t1;
|
||||
SHOW CREATE TABLE t2;
|
||||
SELECT from_base64, LENGTH(from_base64) FROM t2;
|
||||
DROP TABLE t2;
|
||||
DROP TABLE t1;
|
||||
--echo
|
||||
}
|
||||
CREATE TABLE t1 (a VARBINARY(64));
|
||||
INSERT INTO t1 VALUES (0x00), (0x0000), (0x000000), (0x00000000);
|
||||
INSERT INTO t1 VALUES (0x00010203040506070809);
|
||||
SELECT TO_BASE64(a), hex(a) FROM t1 ORDER BY a;
|
||||
DROP TABLE t1;
|
||||
|
||||
--echo #
|
||||
--echo # Test NULL output for NULL input
|
||||
--echo #
|
||||
SELECT TO_BASE64(NULL);
|
||||
SELECT FROM_BASE64(NULL);
|
||||
|
||||
--echo #
|
||||
--echo # RFC4648 test vectors
|
||||
--echo #
|
||||
SELECT @b:= TO_BASE64(''), FROM_BASE64(@b); # ""
|
||||
SELECT @b:= TO_BASE64('f'), FROM_BASE64(@b); # "Zg=="
|
||||
SELECT @b:= TO_BASE64('fo'), FROM_BASE64(@b); # "Zm8="
|
||||
SELECT @b:= TO_BASE64('foo'), FROM_BASE64(@b); # "Zm9v"
|
||||
SELECT @b:= TO_BASE64('foob'), FROM_BASE64(@b); # "Zm9vYg=="
|
||||
SELECT @b:= TO_BASE64('fooba'), FROM_BASE64(@b); # "Zm9vYmE="
|
||||
SELECT @b:= TO_BASE64('foobar'), FROM_BASE64(@b); # "Zm9vYmFy"
|
||||
|
||||
|
||||
--echo #
|
||||
--echo # Invalid characters - return NULL
|
||||
--echo #
|
||||
SELECT hex(FROM_BASE64('#'));
|
||||
SELECT hex(FROM_BASE64('A#'));
|
||||
SELECT hex(FROM_BASE64('AB#'));
|
||||
SELECT hex(FROM_BASE64('ABC#'));
|
||||
SELECT hex(FROM_BASE64('ABCD#'));
|
||||
|
||||
--echo #
|
||||
--echo # "=" is not valid on the first and second positions of a quadruple
|
||||
--echo #
|
||||
SELECT hex(FROM_BASE64('='));
|
||||
SELECT hex(FROM_BASE64('A='));
|
||||
SELECT hex(FROM_BASE64('ABCD='));
|
||||
SELECT hex(FROM_BASE64('ABCDE='));
|
||||
|
||||
--echo #
|
||||
--echo # Incomplete sequences - return NULL
|
||||
--echo #
|
||||
SELECT hex(FROM_BASE64('A'));
|
||||
SELECT hex(FROM_BASE64('AB'));
|
||||
SELECT hex(FROM_BASE64('ABC'));
|
||||
|
||||
--echo #
|
||||
--echo # Unexpected input after pad characters - return NULL
|
||||
--echo #
|
||||
SELECT hex(FROM_BASE64('AAA=x'));
|
||||
SELECT hex(FROM_BASE64('AA==x'));
|
||||
|
||||
|
||||
--echo #
|
||||
--echo # Delimiters are allowed at any position
|
||||
--echo #
|
||||
SELECT hex(FROM_BASE64(' A B C D '));
|
||||
SELECT hex(FROM_BASE64(' A A = = '));
|
||||
SELECT hex(FROM_BASE64(' A A A = '));
|
||||
SELECT hex(FROM_BASE64(' A \n B \r C \t D '));
|
||||
|
||||
--echo #
|
||||
--echo # Testing that to_base64 respects max_allowed_packet
|
||||
--echo #
|
||||
SELECT LENGTH(TO_BASE64(REPEAT('a', @@max_allowed_packet-10)));
|
||||
|
||||
--echo #
|
||||
--echo # Testing base64 with various data types
|
||||
--echo #
|
||||
CREATE TABLE t1 (
|
||||
i1 INT,
|
||||
f1 FLOAT,
|
||||
dc1 DECIMAL(10,5),
|
||||
e1 ENUM('enum11','enum12','enum13'),
|
||||
s1 SET('set1','set2','set3'),
|
||||
t1 TIME,
|
||||
d1 DATE,
|
||||
dt1 DATETIME
|
||||
);
|
||||
INSERT INTO t1 VALUES
|
||||
(-12345, -456.789, 123.45, 'enum13', 'set1,set3',
|
||||
'01:02:03', '2010-01-01', '2011-01-01 02:03:04');
|
||||
SELECT FROM_BASE64(TO_BASE64(i1)) FROM t1;
|
||||
SELECT FROM_BASE64(TO_BASE64(f1)) FROM t1;
|
||||
SELECT FROM_BASE64(TO_BASE64(dc1)) FROM t1;
|
||||
SELECT FROM_BASE64(TO_BASE64(e1)) FROM t1;
|
||||
SELECT FROM_BASE64(TO_BASE64(s1)) FROM t1;
|
||||
SELECT FROM_BASE64(TO_BASE64(t1)) FROM t1;
|
||||
SELECT FROM_BASE64(TO_BASE64(d1)) FROM t1;
|
||||
SELECT FROM_BASE64(TO_BASE64(dt1)) FROM t1;
|
||||
DROP TABLE t1;
|
||||
|
362
mysys/base64.c
362
mysys/base64.c
@ -1,5 +1,5 @@
|
||||
/* Copyright (c) 2003-2008 MySQL AB, 2009 Sun Microsystems, Inc.
|
||||
Use is subject to license terms.
|
||||
/* Copyright (c) 2003, 2010, Oracle and/or its affiliates.
|
||||
Copyright (c) 2013, MariaDB Foundation.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@ -25,6 +25,28 @@ static char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
/**
|
||||
* Maximum length base64_needed_encoded_length()
|
||||
* can handle without signed integer overflow: (x + 2) / 3 * 4
|
||||
*/
|
||||
int
|
||||
base64_encode_max_arg_length()
|
||||
{
|
||||
#if (SIZEOF_INT == 8)
|
||||
/*
|
||||
(6827690988321067803 + 2) / 3 + 4 -> 9223372036854775805 Okey
|
||||
(6827690988321067804 + 2) / 3 + 4 -> -9223372036854775807 Overflow
|
||||
*/
|
||||
return 0x5EC0D4C77B03531BLL; /* 6827690988321067803 */
|
||||
#else
|
||||
/*
|
||||
1589695686 -> 2147483646 (7FFFFFFE)
|
||||
1589695687 -> -2147483645
|
||||
*/
|
||||
return 0x5EC0D4C6; /* 1589695686 */
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
base64_needed_encoded_length(int length_of_data)
|
||||
@ -39,10 +61,24 @@ base64_needed_encoded_length(int length_of_data)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Maximum length supported by base64_decode().
|
||||
*/
|
||||
int
|
||||
base64_decode_max_arg_length()
|
||||
{
|
||||
#if (SIZEOF_INT == 8)
|
||||
return 0x7FFFFFFFFFFFFFFFLL;
|
||||
#else
|
||||
return 0x7FFFFFFF;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
base64_needed_decoded_length(int length_of_encoded_data)
|
||||
{
|
||||
return (int) ceil(length_of_encoded_data * 3 / 4);
|
||||
return (int) ((longlong) length_of_encoded_data + 3) / 4 * 3;
|
||||
}
|
||||
|
||||
|
||||
@ -51,6 +87,11 @@ base64_needed_decoded_length(int length_of_encoded_data)
|
||||
|
||||
Note: We require that dst is pre-allocated to correct size.
|
||||
See base64_needed_encoded_length().
|
||||
|
||||
Note: We add line separators every 76 characters.
|
||||
|
||||
Note: The output string is properly padded with the '=' character,
|
||||
so the length of the output string is always divisable by 4.
|
||||
*/
|
||||
|
||||
int
|
||||
@ -101,130 +142,233 @@ base64_encode(const void *src, size_t src_len, char *dst)
|
||||
}
|
||||
|
||||
|
||||
static inline uint
|
||||
pos(unsigned char c)
|
||||
/*
|
||||
Base64 decoder stream
|
||||
*/
|
||||
typedef struct my_base64_decoder_t
|
||||
{
|
||||
return (uint) (strchr(base64_table, c) - base64_table);
|
||||
}
|
||||
|
||||
|
||||
#define SKIP_SPACE(src, i, size) \
|
||||
{ \
|
||||
while (i < size && my_isspace(&my_charset_latin1, * src)) \
|
||||
{ \
|
||||
i++; \
|
||||
src++; \
|
||||
} \
|
||||
if (i == size) \
|
||||
{ \
|
||||
break; \
|
||||
} \
|
||||
}
|
||||
const char *src; /* Pointer to the current input position */
|
||||
const char *end; /* Pointer to the end of input buffer */
|
||||
uint c; /* Collect bits into this number */
|
||||
int error; /* Error code */
|
||||
uchar state; /* Character number in the current group of 4 */
|
||||
uchar mark; /* Number of padding marks in the current group */
|
||||
} MY_BASE64_DECODER;
|
||||
|
||||
|
||||
/*
|
||||
Decode a base64 string
|
||||
|
||||
SYNOPSIS
|
||||
base64_decode()
|
||||
src Pointer to base64-encoded string
|
||||
len Length of string at 'src'
|
||||
dst Pointer to location where decoded data will be stored
|
||||
end_ptr Pointer to variable that will refer to the character
|
||||
after the end of the encoded data that were decoded. Can
|
||||
be NULL.
|
||||
|
||||
DESCRIPTION
|
||||
|
||||
The base64-encoded data in the range ['src','*end_ptr') will be
|
||||
decoded and stored starting at 'dst'. The decoding will stop
|
||||
after 'len' characters have been read from 'src', or when padding
|
||||
occurs in the base64-encoded data. In either case: if 'end_ptr' is
|
||||
non-null, '*end_ptr' will be set to point to the character after
|
||||
the last read character, even in the presence of error.
|
||||
|
||||
NOTE
|
||||
We require that 'dst' is pre-allocated to correct size.
|
||||
|
||||
SEE ALSO
|
||||
base64_needed_decoded_length().
|
||||
|
||||
RETURN VALUE
|
||||
Number of bytes written at 'dst' or -1 in case of failure
|
||||
Helper table for decoder.
|
||||
-2 means "space character"
|
||||
-1 means "bad character"
|
||||
Non-negative values mean valid base64 encoding character.
|
||||
*/
|
||||
int
|
||||
base64_decode(const char *src_base, size_t len,
|
||||
void *dst, const char **end_ptr)
|
||||
static int8
|
||||
from_base64_table[]=
|
||||
{
|
||||
char b[3];
|
||||
size_t i= 0;
|
||||
char *dst_base= (char *)dst;
|
||||
char const *src= src_base;
|
||||
char *d= dst_base;
|
||||
size_t j;
|
||||
/*00*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-1,-1,
|
||||
/*10*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
||||
/*20*/ -2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* !"#$%&'()*+,-./ */
|
||||
/*30*/ 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 0123456789:;<=>? */
|
||||
/*40*/ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* @ABCDEFGHIJKLMNO */
|
||||
/*50*/ 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* PQRSTUVWXYZ[\]^_ */
|
||||
/*60*/ -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* `abcdefghijklmno */
|
||||
/*70*/ 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* pqrstuvwxyz{|}~ */
|
||||
/*80*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
||||
/*90*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
||||
/*A0*/ -2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
||||
/*B0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
||||
/*C0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
||||
/*D0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
||||
/*E0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
||||
/*F0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
|
||||
};
|
||||
|
||||
while (i < len)
|
||||
|
||||
/**
|
||||
* Skip leading spaces in a base64 encoded stream
|
||||
* and stop on the first non-space character.
|
||||
* decoder->src will point to the first non-space character,
|
||||
* or to the end of the input string.
|
||||
* In case when end-of-input met on unexpected position,
|
||||
* decoder->error is also set to 1.
|
||||
*
|
||||
* See http://en.wikipedia.org/wiki/Base64 for the base64 encoding details
|
||||
*
|
||||
* @param decoder Pointer to MY_BASE64_DECODER
|
||||
*
|
||||
* @return
|
||||
* FALSE on success (there are some more non-space input characters)
|
||||
* TRUE on error (end-of-input found)
|
||||
*/
|
||||
|
||||
static inline my_bool
|
||||
my_base64_decoder_skip_spaces(MY_BASE64_DECODER *decoder)
|
||||
{
|
||||
for ( ; decoder->src < decoder->end; decoder->src++)
|
||||
{
|
||||
unsigned c= 0;
|
||||
size_t mark= 0;
|
||||
if (from_base64_table[(uchar) *decoder->src] != -2)
|
||||
return FALSE;
|
||||
}
|
||||
if (decoder->state > 0)
|
||||
decoder->error= 1; /* Unexpected end-of-input found */
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
SKIP_SPACE(src, i, len);
|
||||
|
||||
c += pos(*src++);
|
||||
c <<= 6;
|
||||
i++;
|
||||
/**
|
||||
* Convert the next character in a base64 encoded stream
|
||||
* to a number in the range [0..63]
|
||||
* and mix it with the previously collected value in decoder->c.
|
||||
*
|
||||
* @param decode base64 decoding stream
|
||||
*
|
||||
* @return
|
||||
* FALSE on success
|
||||
* TRUE on error (invalid base64 character found)
|
||||
*/
|
||||
static inline my_bool
|
||||
my_base64_add(MY_BASE64_DECODER *decoder)
|
||||
{
|
||||
int res;
|
||||
decoder->c <<= 6;
|
||||
if ((res= from_base64_table[(uchar) *decoder->src++]) < 0)
|
||||
return (decoder->error= TRUE);
|
||||
decoder->c+= (uint) res;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
SKIP_SPACE(src, i, len);
|
||||
|
||||
c += pos(*src++);
|
||||
c <<= 6;
|
||||
i++;
|
||||
/**
|
||||
* Get the next character from a base64 encoded stream.
|
||||
* Skip spaces, then scan the next base64 character or a pad character
|
||||
* and collect bits into decoder->c.
|
||||
*
|
||||
* @param decoder Pointer to MY_BASE64_DECODER
|
||||
* @return
|
||||
* FALSE on success (a valid base64 encoding character found)
|
||||
* TRUE on error (unexpected character or unexpected end-of-input found)
|
||||
*/
|
||||
static my_bool
|
||||
my_base64_decoder_getch(MY_BASE64_DECODER *decoder)
|
||||
{
|
||||
if (my_base64_decoder_skip_spaces(decoder))
|
||||
return TRUE; /* End-of-input */
|
||||
|
||||
SKIP_SPACE(src, i, len);
|
||||
|
||||
if (*src != '=')
|
||||
c += pos(*src++);
|
||||
else
|
||||
if (!my_base64_add(decoder)) /* Valid base64 character found */
|
||||
{
|
||||
if (decoder->mark)
|
||||
{
|
||||
src += 2; /* There should be two bytes padding */
|
||||
i= len;
|
||||
mark= 2;
|
||||
c <<= 6;
|
||||
goto end;
|
||||
/* If we have scanned '=' already, then only '=' is valid */
|
||||
DBUG_ASSERT(decoder->state == 3);
|
||||
decoder->error= 1;
|
||||
decoder->src--;
|
||||
return TRUE; /* expected '=', but encoding character found */
|
||||
}
|
||||
c <<= 6;
|
||||
i++;
|
||||
|
||||
SKIP_SPACE(src, i, len);
|
||||
|
||||
if (*src != '=')
|
||||
c += pos(*src++);
|
||||
else
|
||||
{
|
||||
src += 1; /* There should be one byte padding */
|
||||
i= len;
|
||||
mark= 1;
|
||||
goto end;
|
||||
}
|
||||
i++;
|
||||
|
||||
end:
|
||||
b[0]= (c >> 16) & 0xff;
|
||||
b[1]= (c >> 8) & 0xff;
|
||||
b[2]= (c >> 0) & 0xff;
|
||||
|
||||
for (j=0; j<3-mark; j++)
|
||||
*d++= b[j];
|
||||
decoder->state++;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (end_ptr != NULL)
|
||||
*end_ptr= src;
|
||||
/* Process error */
|
||||
switch (decoder->state)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
decoder->src--;
|
||||
return TRUE; /* base64 character expected */
|
||||
break;
|
||||
|
||||
/*
|
||||
The variable 'i' is set to 'len' when padding has been read, so it
|
||||
does not actually reflect the number of bytes read from 'src'.
|
||||
*/
|
||||
return i != len ? -1 : (int) (d - dst_base);
|
||||
case 2:
|
||||
case 3:
|
||||
if (decoder->src[-1] == '=')
|
||||
{
|
||||
decoder->error= 0; /* Not an error - it's a pad character */
|
||||
decoder->mark++;
|
||||
}
|
||||
else
|
||||
{
|
||||
decoder->src--;
|
||||
return TRUE; /* base64 character or '=' expected */
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
DBUG_ASSERT(0);
|
||||
return TRUE; /* Wrong state, should not happen */
|
||||
}
|
||||
|
||||
decoder->state++;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decode a base64 string
|
||||
* The base64-encoded data in the range ['src','*end_ptr') will be
|
||||
* decoded and stored starting at 'dst'. The decoding will stop
|
||||
* after 'len' characters have been read from 'src', or when padding
|
||||
* occurs in the base64-encoded data. In either case: if 'end_ptr' is
|
||||
* non-null, '*end_ptr' will be set to point to the character after
|
||||
* the last read character, even in the presence of error.
|
||||
*
|
||||
* Note: 'dst' must have sufficient space to store the decoded data.
|
||||
* Use base64_needed_decoded_length() to calculate the correct space size.
|
||||
*
|
||||
* Note: we allow spaces and line separators at any position.
|
||||
*
|
||||
* @param src Pointer to base64-encoded string
|
||||
* @param len Length of string at 'src'
|
||||
* @param dst Pointer to location where decoded data will be stored
|
||||
* @param end_ptr Pointer to variable that will refer to the character
|
||||
* after the end of the encoded data that were decoded.
|
||||
* Can be NULL.
|
||||
* @flags flags e.g. allow multiple chunks
|
||||
* @return Number of bytes written at 'dst', or -1 in case of failure
|
||||
*/
|
||||
int
|
||||
base64_decode(const char *src_base, size_t len,
|
||||
void *dst, const char **end_ptr, int flags)
|
||||
{
|
||||
char *d= (char*) dst;
|
||||
MY_BASE64_DECODER decoder;
|
||||
|
||||
decoder.src= src_base;
|
||||
decoder.end= src_base + len;
|
||||
decoder.error= 0;
|
||||
decoder.mark= 0;
|
||||
|
||||
for ( ; ; )
|
||||
{
|
||||
decoder.c= 0;
|
||||
decoder.state= 0;
|
||||
|
||||
if (my_base64_decoder_getch(&decoder) ||
|
||||
my_base64_decoder_getch(&decoder) ||
|
||||
my_base64_decoder_getch(&decoder) ||
|
||||
my_base64_decoder_getch(&decoder))
|
||||
break;
|
||||
|
||||
*d++= (decoder.c >> 16) & 0xff;
|
||||
*d++= (decoder.c >> 8) & 0xff;
|
||||
*d++= (decoder.c >> 0) & 0xff;
|
||||
|
||||
if (decoder.mark)
|
||||
{
|
||||
d-= decoder.mark;
|
||||
if (!(flags & MY_BASE64_DECODE_ALLOW_MULTIPLE_CHUNKS))
|
||||
break;
|
||||
decoder.mark= 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return error if there are more non-space characters */
|
||||
decoder.state= 0;
|
||||
if (!my_base64_decoder_skip_spaces(&decoder))
|
||||
decoder.error= 1;
|
||||
|
||||
if (end_ptr != NULL)
|
||||
*end_ptr= decoder.src;
|
||||
|
||||
return decoder.error ? -1 : (int) (d - (char*) dst);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1151,6 +1151,19 @@ protected:
|
||||
};
|
||||
|
||||
|
||||
class Create_func_from_base64 : public Create_func_arg1
|
||||
{
|
||||
public:
|
||||
virtual Item *create_1_arg(THD *thd, Item *arg1);
|
||||
|
||||
static Create_func_from_base64 s_singleton;
|
||||
|
||||
protected:
|
||||
Create_func_from_base64() {}
|
||||
virtual ~Create_func_from_base64() {}
|
||||
};
|
||||
|
||||
|
||||
class Create_func_from_days : public Create_func_arg1
|
||||
{
|
||||
public:
|
||||
@ -2357,6 +2370,19 @@ protected:
|
||||
};
|
||||
|
||||
|
||||
class Create_func_to_base64 : public Create_func_arg1
|
||||
{
|
||||
public:
|
||||
virtual Item *create_1_arg(THD *thd, Item *arg1);
|
||||
|
||||
static Create_func_to_base64 s_singleton;
|
||||
|
||||
protected:
|
||||
Create_func_to_base64() {}
|
||||
virtual ~Create_func_to_base64() {}
|
||||
};
|
||||
|
||||
|
||||
class Create_func_to_days : public Create_func_arg1
|
||||
{
|
||||
public:
|
||||
@ -3812,6 +3838,16 @@ Create_func_format::create_native(THD *thd, LEX_STRING name,
|
||||
}
|
||||
|
||||
|
||||
Create_func_from_base64 Create_func_from_base64::s_singleton;
|
||||
|
||||
|
||||
Item *
|
||||
Create_func_from_base64::create_1_arg(THD *thd, Item *arg1)
|
||||
{
|
||||
return new (thd->mem_root) Item_func_from_base64(arg1);
|
||||
}
|
||||
|
||||
|
||||
Create_func_found_rows Create_func_found_rows::s_singleton;
|
||||
|
||||
Item*
|
||||
@ -5040,6 +5076,15 @@ Create_func_timediff::create_2_arg(THD *thd, Item *arg1, Item *arg2)
|
||||
}
|
||||
|
||||
|
||||
Create_func_to_base64 Create_func_to_base64::s_singleton;
|
||||
|
||||
Item*
|
||||
Create_func_to_base64::create_1_arg(THD *thd, Item *arg1)
|
||||
{
|
||||
return new (thd->mem_root) Item_func_to_base64(arg1);
|
||||
}
|
||||
|
||||
|
||||
Create_func_to_days Create_func_to_days::s_singleton;
|
||||
|
||||
Item*
|
||||
@ -5393,6 +5438,7 @@ static Native_func_registry func_array[] =
|
||||
{ { C_STRING_WITH_LEN("FLOOR") }, BUILDER(Create_func_floor)},
|
||||
{ { C_STRING_WITH_LEN("FORMAT") }, BUILDER(Create_func_format)},
|
||||
{ { C_STRING_WITH_LEN("FOUND_ROWS") }, BUILDER(Create_func_found_rows)},
|
||||
{ { C_STRING_WITH_LEN("FROM_BASE64") }, BUILDER(Create_func_from_base64)},
|
||||
{ { C_STRING_WITH_LEN("FROM_DAYS") }, BUILDER(Create_func_from_days)},
|
||||
{ { C_STRING_WITH_LEN("FROM_UNIXTIME") }, BUILDER(Create_func_from_unixtime)},
|
||||
{ { C_STRING_WITH_LEN("GEOMCOLLFROMTEXT") }, GEOM_BUILDER(Create_func_geometry_from_text)},
|
||||
@ -5576,6 +5622,7 @@ static Native_func_registry func_array[] =
|
||||
{ { C_STRING_WITH_LEN("TIME_FORMAT") }, BUILDER(Create_func_time_format)},
|
||||
{ { C_STRING_WITH_LEN("TIME_TO_SEC") }, BUILDER(Create_func_time_to_sec)},
|
||||
{ { C_STRING_WITH_LEN("TOUCHES") }, GEOM_BUILDER(Create_func_touches)},
|
||||
{ { C_STRING_WITH_LEN("TO_BASE64") }, BUILDER(Create_func_to_base64)},
|
||||
{ { C_STRING_WITH_LEN("TO_DAYS") }, BUILDER(Create_func_to_days)},
|
||||
{ { C_STRING_WITH_LEN("TO_SECONDS") }, BUILDER(Create_func_to_seconds)},
|
||||
{ { C_STRING_WITH_LEN("UCASE") }, BUILDER(Create_func_ucase)},
|
||||
|
@ -51,6 +51,7 @@
|
||||
#include "password.h" // my_make_scrambled_password,
|
||||
// my_make_scrambled_password_323
|
||||
#include <m_ctype.h>
|
||||
#include <base64.h>
|
||||
#include <my_md5.h>
|
||||
#include "sha1.h"
|
||||
#include "my_aes.h"
|
||||
@ -451,6 +452,107 @@ void Item_func_aes_decrypt::fix_length_and_dec()
|
||||
set_persist_maybe_null(1);
|
||||
}
|
||||
|
||||
|
||||
void Item_func_to_base64::fix_length_and_dec()
|
||||
{
|
||||
maybe_null= args[0]->maybe_null;
|
||||
collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
|
||||
if (args[0]->max_length > (uint) base64_encode_max_arg_length())
|
||||
{
|
||||
maybe_null= 1;
|
||||
fix_char_length_ulonglong((ulonglong) base64_encode_max_arg_length());
|
||||
}
|
||||
else
|
||||
{
|
||||
int length= base64_needed_encoded_length((int) args[0]->max_length);
|
||||
DBUG_ASSERT(length > 0);
|
||||
fix_char_length_ulonglong((ulonglong) length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String *Item_func_to_base64::val_str_ascii(String *str)
|
||||
{
|
||||
String *res= args[0]->val_str(str);
|
||||
bool too_long= false;
|
||||
int length;
|
||||
if (!res ||
|
||||
res->length() > (uint) base64_encode_max_arg_length() ||
|
||||
(too_long=
|
||||
((uint) (length= base64_needed_encoded_length((int) res->length())) >
|
||||
current_thd->variables.max_allowed_packet)) ||
|
||||
tmp_value.alloc((uint) length))
|
||||
{
|
||||
null_value= 1; // NULL input, too long input, or OOM.
|
||||
if (too_long)
|
||||
{
|
||||
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
|
||||
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
|
||||
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
|
||||
current_thd->variables.max_allowed_packet);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
base64_encode(res->ptr(), (int) res->length(), (char*) tmp_value.ptr());
|
||||
DBUG_ASSERT(length > 0);
|
||||
tmp_value.length((uint) length - 1); // Without trailing '\0'
|
||||
null_value= 0;
|
||||
return &tmp_value;
|
||||
}
|
||||
|
||||
|
||||
void Item_func_from_base64::fix_length_and_dec()
|
||||
{
|
||||
if (args[0]->max_length > (uint) base64_decode_max_arg_length())
|
||||
{
|
||||
fix_char_length_ulonglong((ulonglong) base64_decode_max_arg_length());
|
||||
}
|
||||
else
|
||||
{
|
||||
int length= base64_needed_decoded_length((int) args[0]->max_length);
|
||||
fix_char_length_ulonglong((ulonglong) length);
|
||||
}
|
||||
maybe_null= 1; // Can be NULL, e.g. in case of badly formed input string
|
||||
}
|
||||
|
||||
|
||||
String *Item_func_from_base64::val_str(String *str)
|
||||
{
|
||||
String *res= args[0]->val_str_ascii(str);
|
||||
bool too_long= false;
|
||||
int length;
|
||||
const char *end_ptr;
|
||||
|
||||
if (!res ||
|
||||
res->length() > (uint) base64_decode_max_arg_length() ||
|
||||
(too_long=
|
||||
((uint) (length= base64_needed_decoded_length((int) res->length())) >
|
||||
current_thd->variables.max_allowed_packet)) ||
|
||||
tmp_value.alloc((uint) length) ||
|
||||
(length= base64_decode(res->ptr(), (int) res->length(),
|
||||
(char *) tmp_value.ptr(), &end_ptr, 0)) < 0 ||
|
||||
end_ptr < res->ptr() + res->length())
|
||||
{
|
||||
null_value= 1; // NULL input, too long input, OOM, or badly formed input
|
||||
if (too_long)
|
||||
{
|
||||
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
|
||||
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
|
||||
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
|
||||
current_thd->variables.max_allowed_packet);
|
||||
}
|
||||
else if (res && length < 0)
|
||||
{
|
||||
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
|
||||
ER_BAD_BASE64_DATA, ER(ER_BAD_BASE64_DATA),
|
||||
end_ptr - res->ptr());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
tmp_value.length((uint) length);
|
||||
null_value= 0;
|
||||
return &tmp_value;
|
||||
}
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
@ -116,6 +116,27 @@ public:
|
||||
const char *func_name() const { return "sha2"; }
|
||||
};
|
||||
|
||||
class Item_func_to_base64 :public Item_str_ascii_func
|
||||
{
|
||||
String tmp_value;
|
||||
public:
|
||||
Item_func_to_base64(Item *a) :Item_str_ascii_func(a) {}
|
||||
String *val_str_ascii(String *);
|
||||
void fix_length_and_dec();
|
||||
const char *func_name() const { return "to_base64"; }
|
||||
};
|
||||
|
||||
class Item_func_from_base64 :public Item_str_func
|
||||
{
|
||||
String tmp_value;
|
||||
public:
|
||||
Item_func_from_base64(Item *a) :Item_str_func(a) {}
|
||||
String *val_str(String *);
|
||||
void fix_length_and_dec();
|
||||
const char *func_name() const { return "from_base64"; }
|
||||
};
|
||||
|
||||
|
||||
class Item_func_aes_encrypt :public Item_str_func
|
||||
{
|
||||
public:
|
||||
|
@ -7058,3 +7058,5 @@ ER_NO_SUCH_QUERY
|
||||
eng "Unknown query id: %lld"
|
||||
ger "Unbekannte Abfrage-ID: %lld"
|
||||
rus "Неизвестный номер запроса: %lld"
|
||||
ER_BAD_BASE64_DATA
|
||||
eng "Bad base64 data as position %u"
|
||||
|
@ -118,7 +118,8 @@ void mysql_client_binlog_statement(THD* thd)
|
||||
strptr < thd->lex->comment.str + thd->lex->comment.length ; )
|
||||
{
|
||||
char const *endptr= 0;
|
||||
int bytes_decoded= base64_decode(strptr, coded_len, buf, &endptr);
|
||||
int bytes_decoded= base64_decode(strptr, coded_len, buf, &endptr,
|
||||
MY_BASE64_DECODE_ALLOW_MULTIPLE_CHUNKS);
|
||||
|
||||
#ifndef HAVE_valgrind
|
||||
/*
|
||||
|
@ -60,7 +60,7 @@ main(int argc __attribute__((unused)),char *argv[])
|
||||
|
||||
/* Decode */
|
||||
dst= (char *) malloc(base64_needed_decoded_length(strlen(str)));
|
||||
dst_len= base64_decode(str, strlen(str), dst, NULL);
|
||||
dst_len= base64_decode(str, strlen(str), dst, NULL, 0);
|
||||
ok(dst_len == src_len, "Comparing lengths");
|
||||
|
||||
cmp= memcmp(src, dst, src_len);
|
||||
|
Loading…
x
Reference in New Issue
Block a user