summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mnogosearch.org>2013-09-23 18:58:33 +0400
committerAlexander Barkov <bar@mnogosearch.org>2013-09-23 18:58:33 +0400
commite33582d20d2a9f215dc4d0effa55886bbabdce3d (patch)
treeeebea43211dfaefb954c40af5b72afe97019b572 /sql
parent9cbd53bfb2e72376080a3951185e4780b0519718 (diff)
downloadmariadb-git-e33582d20d2a9f215dc4d0effa55886bbabdce3d.tar.gz
Merging TO_BASE64() and FROM_BASE64() from MySQL-5.6
Diffstat (limited to 'sql')
-rw-r--r--sql/item_create.cc47
-rw-r--r--sql/item_strfunc.cc102
-rw-r--r--sql/item_strfunc.h21
-rw-r--r--sql/share/errmsg-utf8.txt2
-rw-r--r--sql/sql_binlog.cc3
5 files changed, 174 insertions, 1 deletions
diff --git a/sql/item_create.cc b/sql/item_create.cc
index 3d0a2f58eb7..9842f88e904 100644
--- a/sql/item_create.cc
+++ b/sql/item_create.cc
@@ -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)},
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 6e47cc3c49e..2d2b231fdfe 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -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;
+}
///////////////////////////////////////////////////////////////////////////////
diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h
index 86e1aa70a6b..5c85c74ad52 100644
--- a/sql/item_strfunc.h
+++ b/sql/item_strfunc.h
@@ -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:
diff --git a/sql/share/errmsg-utf8.txt b/sql/share/errmsg-utf8.txt
index 75004bba351..05693693bc9 100644
--- a/sql/share/errmsg-utf8.txt
+++ b/sql/share/errmsg-utf8.txt
@@ -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"
diff --git a/sql/sql_binlog.cc b/sql/sql_binlog.cc
index 3bb5deab406..a148838dd1f 100644
--- a/sql/sql_binlog.cc
+++ b/sql/sql_binlog.cc
@@ -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
/*