summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README-DEV31
-rw-r--r--TODO6
-rw-r--r--libmysqlclient/.gitignore6
-rw-r--r--libmysqlclient/README-DEV32
-rw-r--r--libmysqlclient/build/root.build5
-rw-r--r--libmysqlclient/buildfile2
-rw-r--r--libmysqlclient/manifest21
-rw-r--r--libmysqlclient/mysql/.gitattributes7
-rw-r--r--libmysqlclient/mysql/buildfile16
-rw-r--r--libmysqlclient/mysql/my_config.h49
l---------libmysqlclient/mysql/sql-common1
l---------libmysqlclient/mysql/sql-common/client.cc1
l---------libmysqlclient/mysql/sql-common/client_authentication.cc1
l---------libmysqlclient/mysql/sql-common/client_plugin.cc1
l---------libmysqlclient/mysql/sql-common/get_password.cc1
l---------libmysqlclient/mysql/sql-common/my_path_permissions.cc1
l---------libmysqlclient/mysql/sql-common/net_serv.cc1
-rw-r--r--libmysqlclient/mysql/sql-common/sql_string.cc1254
l---------libmysqlclient/mysql/sql-common/sql_string.cc.orig1
l---------libmysqlclient/mysql/strings1
l---------libmysqlclient/mysql/strings/README1
l---------libmysqlclient/mysql/strings/conf_to_src.cc1
l---------libmysqlclient/mysql/strings/ctype-big5.cc1
l---------libmysqlclient/mysql/strings/ctype-bin.cc1
l---------libmysqlclient/mysql/strings/ctype-cp932.cc1
-rw-r--r--libmysqlclient/mysql/strings/ctype-czech.cc699
l---------libmysqlclient/mysql/strings/ctype-czech.cc.orig1
l---------libmysqlclient/mysql/strings/ctype-euc_kr.cc1
l---------libmysqlclient/mysql/strings/ctype-eucjpms.cc1
l---------libmysqlclient/mysql/strings/ctype-extra.cc1
l---------libmysqlclient/mysql/strings/ctype-gb18030.cc1
l---------libmysqlclient/mysql/strings/ctype-gb2312.cc1
l---------libmysqlclient/mysql/strings/ctype-gbk.cc1
l---------libmysqlclient/mysql/strings/ctype-latin1.cc1
l---------libmysqlclient/mysql/strings/ctype-mb.cc1
l---------libmysqlclient/mysql/strings/ctype-simple.cc1
l---------libmysqlclient/mysql/strings/ctype-sjis.cc1
l---------libmysqlclient/mysql/strings/ctype-tis620.cc1
l---------libmysqlclient/mysql/strings/ctype-uca.cc1
l---------libmysqlclient/mysql/strings/ctype-ucs2.cc1
l---------libmysqlclient/mysql/strings/ctype-ujis.cc1
l---------libmysqlclient/mysql/strings/ctype-utf8.cc1
l---------libmysqlclient/mysql/strings/ctype-win1250ch.cc1
l---------libmysqlclient/mysql/strings/ctype.cc1
-rw-r--r--libmysqlclient/mysql/strings/decimal.cc2482
l---------libmysqlclient/mysql/strings/decimal.cc.orig1
l---------libmysqlclient/mysql/strings/do_ctype.cc1
l---------libmysqlclient/mysql/strings/dtoa.cc1
l---------libmysqlclient/mysql/strings/dump_map.cc1
l---------libmysqlclient/mysql/strings/int2str.cc1
l---------libmysqlclient/mysql/strings/longlong2str.cc1
l---------libmysqlclient/mysql/strings/mb_wc.h1
l---------libmysqlclient/mysql/strings/my_strchr.cc1
l---------libmysqlclient/mysql/strings/my_strtoll10.cc1
l---------libmysqlclient/mysql/strings/str2int.cc1
l---------libmysqlclient/mysql/strings/str_alloc.cc1
l---------libmysqlclient/mysql/strings/str_uca_type.h1
l---------libmysqlclient/mysql/strings/strcont.cc1
l---------libmysqlclient/mysql/strings/strmake.cc1
l---------libmysqlclient/mysql/strings/strxmov.cc1
l---------libmysqlclient/mysql/strings/strxnmov.cc1
l---------libmysqlclient/mysql/strings/t_ctype.h1
l---------libmysqlclient/mysql/strings/uca-dump.cc1
l---------libmysqlclient/mysql/strings/uca9-dump.cc1
l---------libmysqlclient/mysql/strings/uca900_data.h1
l---------libmysqlclient/mysql/strings/uca900_ja_data.h1
l---------libmysqlclient/mysql/strings/uca_data.h1
l---------libmysqlclient/mysql/strings/uctypedump.cc1
l---------libmysqlclient/mysql/strings/utr11-dump.cc1
l---------libmysqlclient/mysql/strings/xml.cc1
l---------libmysqlclient/mysql/version.h.in1
-rw-r--r--libmysqlclient/tests/basic/driver.c3
-rw-r--r--mysql-client/.gitignore25
-rw-r--r--mysql-client/INSTALL7
l---------mysql-client/LICENSE1
-rw-r--r--mysql-client/README21
-rw-r--r--mysql-client/README-DEV15
-rw-r--r--mysql-client/build/.gitignore3
-rw-r--r--mysql-client/build/bootstrap.build10
-rw-r--r--mysql-client/build/export.build9
-rw-r--r--mysql-client/build/root.build25
-rw-r--r--mysql-client/buildfile9
-rw-r--r--mysql-client/manifest35
-rw-r--r--mysql-client/mysql/.gitattributes2
-rw-r--r--mysql-client/mysql/.gitignore1
-rw-r--r--mysql-client/mysql/buildfile93
l---------mysql-client/mysql/client/client_priv.h1
l---------mysql-client/mysql/client/completion_hash.cc1
l---------mysql-client/mysql/client/completion_hash.h1
l---------mysql-client/mysql/client/my_readline.h1
l---------mysql-client/mysql/client/mysql.cc1
l---------mysql-client/mysql/client/pattern_matcher.cc1
l---------mysql-client/mysql/client/pattern_matcher.h1
l---------mysql-client/mysql/client/readline.cc1
-rw-r--r--mysql-client/mysql/config.h11
-rw-r--r--mysql-client/mysql/downstream/mysql_version.h5
-rw-r--r--mysql-client/mysql/downstream/readline.c87
-rw-r--r--mysql-client/mysql/downstream/readline.h59
l---------mysql-client/mysql/include1
l---------mysql-client/mysql/libbinlogevents1
l---------mysql-client/mysql/my_config.h1
l---------mysql-client/mysql/mysqld_error.h1
-rw-r--r--mysql-client/tests/.gitignore4
-rw-r--r--mysql-client/tests/build/.gitignore3
-rw-r--r--mysql-client/tests/build/bootstrap.build8
-rw-r--r--mysql-client/tests/build/root.build15
-rw-r--r--mysql-client/tests/buildfile4
-rw-r--r--mysql-client/tests/help.testscript12
-rw-r--r--mysql-client/tests/version.testscript8
-rw-r--r--packages.manifest2
-rw-r--r--repositories.manifest4
111 files changed, 5112 insertions, 49 deletions
diff --git a/README-DEV b/README-DEV
index 8cf054c..d2e51de 100644
--- a/README-DEV
+++ b/README-DEV
@@ -3,7 +3,8 @@ understanding will be useful when upgrading to a new upstream version.
The upstream package contains the MySQL server, libmysqlclient library and a
number of client/administrative utilities (mysql, mysqlcheck, etc). Currently,
-we only package libmysqlclient (see libmysqlclient/README-DEV for details).
+we only package libmysqlclient and mysql utility (see the respective
+README-DEV files for details).
We add the upstream package as a git submodule and symlink the required files
and subdirectories into the build2 package subdirectories. Then, when required,
@@ -29,7 +30,7 @@ however, that at the time of this writing the latest packaged versions are
different: 5.7.26 on Debian and 8.0.15 on Fedora. These files can be obtained
as follows:
-$ wget https://download.fedoraproject.org/pub/fedora/linux/development/rawhide/Everything/source/tree/Packages/c/community-mysql-8.0.15-1.fc30.src.rpm
+$ wget https://kojipkgs.fedoraproject.org/packages/community-mysql/8.0.15/1.fc30/src/community-mysql-8.0.15-1.fc30.src.rpm
$ rpm2cpio community-mysql-8.0.15-1.fc30.src.rpm | cpio -civ '*.spec'
#$ wget http://deb.debian.org/debian/pool/main/m/mysql-5.7/mysql-5.7_5.7.26-1.debian.tar.xz
@@ -38,12 +39,17 @@ $ rpm2cpio community-mysql-8.0.15-1.fc30.src.rpm | cpio -civ '*.spec'
As a side note, on Debian and Fedora the source, library, and headers are
packaged as follows:
- src library headers
-Debian/Ubuntu: mysql-5.7 libmysqlclient20 libmysqlclient-dev
-Fedora/RHEL: community-mysql community-mysql-libs community-mysql-devel
+ src library headers client
+Debian/Ubuntu: mysql-8.0 libmysqlclient21 libmysqlclient-dev mysql-client-core-8.0
+Fedora: community-mysql community-mysql-libs community-mysql-devel community-mysql
+RHEL mysql mysql-libs mysql-devel mysql
-Search for the Debian and Fedora packages at https://packages.debian.org/search
-and https://apps.fedoraproject.org/packages/.
+Search for the Debian, Fedora, and RedHat packages at
+https://packages.debian.org/search,
+https://apps.fedoraproject.org/packages/, and
+https://access.redhat.com/downloads/content/package-browser (or per
+repository at
+https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/package_manifest/).
Instead of using MySQL 5.7.26 Debian's rules for MySQL 8.0.15 we are
packaging, let's use upstream/packaging/deb-in/rules.in. That's not ideal but
@@ -67,6 +73,14 @@ external dependencies that are not packaged for build2 we end up with:
See the configuration options description at
https://dev.mysql.com/doc/refman/8.0/en/source-configuration-options.html
+Note that the mysql utility cannot be built on POSIX without the system
+Readline or the system or bundled Editline (libedit). None of these libraries
+is currently packaged and so we can only use the bundled libedit library.
+However, the bundled libedit implementation requires ncurses library, which is
+not packaged either. Thus, we bundle our own reduced libedit implementation
+based on the libisocline library (see mysql-client/mysql/downstream/readline.h
+for details).
+
Normally, when packaging a cmake-based project, we try to deduce the source
file and compilation/linking option sets analyzing the root and
feature/component/platform-specific CMakeLists.txt and .cmake files. In
@@ -81,7 +95,8 @@ On POSIX:
$ mkdir build
$ cd build
$ cmake -DDOWNLOAD_BOOST=ON -DWITH_BOOST=../boost -DWITHOUT_SERVER=ON \
- -DENABLED_LOCAL_INFILE=ON -DWITH_ZLIB=system -DWITH_SSL=system ..
+ -DENABLED_LOCAL_INFILE=ON -DWITH_ZLIB=system -DWITH_SSL=system \
+ -DWITH_EDITLINE=system ..
$ make VERBOSE=1 >build.log 2>&1
diff --git a/TODO b/TODO
new file mode 100644
index 0000000..abba7e5
--- /dev/null
+++ b/TODO
@@ -0,0 +1,6 @@
+On the revision:
+
+On the release:
+
+ Note that here we record items that require bumping build2 version
+ requirement to the latest released version of the toolchain.
diff --git a/libmysqlclient/.gitignore b/libmysqlclient/.gitignore
index cece09c..1c363a0 100644
--- a/libmysqlclient/.gitignore
+++ b/libmysqlclient/.gitignore
@@ -3,10 +3,16 @@
*.d
*.t
*.i
+*.i.*
*.ii
+*.ii.*
*.o
*.obj
+*.gcm
+*.pcm
+*.ifc
*.so
+*.dylib
*.dll
*.a
*.lib
diff --git a/libmysqlclient/README-DEV b/libmysqlclient/README-DEV
index f83016d..b69995c 100644
--- a/libmysqlclient/README-DEV
+++ b/libmysqlclient/README-DEV
@@ -6,17 +6,36 @@ Symlink the required upstream components and provide our own implementations
for auto-generated headers:
$ ln -s ../upstream/LICENSE
+$ ln -s ../../upstream/{vio,sql,mysys,mysys_ssl,libbinlogevents,libmysql} mysql
+$ ln -s ../../upstream/include mysql/mysql
+$ ln -s mysql/mysql_version.h.in mysql/version.h.in
-$ ln -s ../../upstream/{strings,vio,sql-common,sql,mysys,mysys_ssl,libbinlogevents,libmysql} \
- mysql
+Also make sure all source files are UTF-8-encoded:
-$ ln -s ../../upstream/include mysql/mysql
+$ mkdir -p mysql/strings
+$ pushd mysql/strings
+$ ln -s ../../../upstream/strings/{README,*.{cc,h}} .
+$ mv ctype-czech.cc ctype-czech.cc.orig
+$ iconv -f ISO-8859-2 -t UTF-8 ctype-czech.cc.orig >ctype-czech.cc
+$ mv decimal.cc decimal.cc.orig
+$ iconv -f UTF-8 -t UTF-8 -c decimal.cc.orig >decimal.cc
+$ popd
+
+Also add missing <limits> include to sql-common/sql_string.cc:
+
+$ mkdir -p mysql/sql-common
+$ pushd mysql/sql-common
+$ ln -s ../../../upstream/sql-common/*.{cc,h} .
+$ mv sql_string.cc sql_string.cc.orig
+$ cp sql_string.cc.orig sql_string.cc
+# Edit sql_string.cc, adding missing <limits> include.
+$ popd
Note that we unable to generate mysql_version.h directly from the template as
it is included as "mysql_version.h" in upstream's source code, which makes
impossible using the header-generating machinery. That's why we create
mysql/mysql_version.h that includes <mysql/version.h> that we auto-generate
-from upstream's mysql/mysql_version.h.in.
+from upstream's mysql_version.h.in.
$ ln -s libbinlogevents/binlog_config.h.cmake mysql/binlog_config.h.cmake.orig
@@ -47,7 +66,8 @@ to, grepping for its usages, for example:
grep -e CPU_LEVEL1_DCACHE_LINESIZE `find -L . -name '*.c*' -o -name '*.h*'`
-And it may become obvious that the macro is not used in libmysqlclient.
+And it may become obvious that the macro is not used in libmysqlclient and
+mysql-client.
Re-creating mysql/my_config.h from scratch every time we upgrade to a new
upstream version would be a real pain. Instead we can only (un)define the
@@ -56,7 +76,7 @@ macro sets:
$ for m in `cat mysql/{config,binlog_config}.h.cmake.orig | \
sed -n 's/.*#\s*\(define\|cmakedefine\)\s\{1,\}\([_a-zA-Z0-9]\{1,\}\)\(\s.*\)\{0,1\}$/\2/p' | sort -u`; do
- if grep -q -e "\b$m\b" `find -L . -name '*.h' -a ! -name my_config.h -a ! -name config.h -o -name '*.c' -o -name '*.cc' -a ! -name mysqld.cc -o -name '*.cpp' -o -name '*.hpp'`; then
+ if grep -q -e "\b$m\b" `find -L . ../mysql-client -name '*.h' -a ! -name my_config.h -a ! -name config.h -o -name '*.c' -o -name '*.cc' -a ! -name mysqld.cc -o -name '*.cpp' -o -name '*.hpp'`; then
echo "$m"
fi
done >used-macros
diff --git a/libmysqlclient/build/root.build b/libmysqlclient/build/root.build
index 9c5eb0b..077c13f 100644
--- a/libmysqlclient/build/root.build
+++ b/libmysqlclient/build/root.build
@@ -8,7 +8,10 @@ using c
h{*}: extension = h
c{*}: extension = c
-cxx.std = latest
+# Note that the implementation uses C++14 internally, with some used
+# constructs being deprecated/removed from the later versions of the standard.
+#
+cxx.std = 14
using cxx
diff --git a/libmysqlclient/buildfile b/libmysqlclient/buildfile
index 53e3e39..fc7acee 100644
--- a/libmysqlclient/buildfile
+++ b/libmysqlclient/buildfile
@@ -1,7 +1,7 @@
# file : buildfile
# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
-./: {*/ -build/} doc{LICENSE INSTALL README} manifest
+./: {*/ -build/} doc{INSTALL README} legal{LICENSE} manifest
# Don't install tests or the INSTALL file.
#
diff --git a/libmysqlclient/manifest b/libmysqlclient/manifest
index 51e1943..e3ed48f 100644
--- a/libmysqlclient/manifest
+++ b/libmysqlclient/manifest
@@ -3,11 +3,11 @@ name: libmysqlclient
# Note: remember to update doc-url below!
#
-version: 8.0.15+9
+version: 8.0.15+15
project: mysql
summary: MySQL C API client library
-license: GPLv2 with Universal FOSS Exception
+license: other: GPL-2.0-only with MySQL Universal FOSS Exception
topics: C, MySQL, SQL, relational database
description-file: README
url: https://www.mysql.com
@@ -18,10 +18,17 @@ email: mysql@lists.mysql.com ; Mailing list.
package-email: packaging@build2.org ; Mailing list.
build-error-email: builds@build2.org
builds: all
+builds: -wasm
builds: -( +windows &gcc ) ; MinGW GCC is not supported.
builds: -static ; Implementation uses C++ and requires special linking steps.
-depends: * build2 >= 0.12.0
-depends: * bpkg >= 0.12.0
-depends: libz >= 1.2.1100
-depends: libcrypto >= 1.1.1
-depends: libssl >= 1.1.1
+depends: * build2 >= 0.15.0
+depends: * bpkg >= 0.15.0
+depends: libz ^1.2.1100
+depends: libcrypto ^1.1.1
+depends: libssl ^1.1.1
+
+# System package mapping.
+#
+fedora-name: community-mysql-devel
+rhel-name: mysql-devel
+centos-name: mysql-devel
diff --git a/libmysqlclient/mysql/.gitattributes b/libmysqlclient/mysql/.gitattributes
new file mode 100644
index 0000000..0cfb4b7
--- /dev/null
+++ b/libmysqlclient/mysql/.gitattributes
@@ -0,0 +1,7 @@
+libbinlogevents symlink=dir
+libmysql symlink=dir
+mysql symlink=dir
+mysys symlink=dir
+mysys_ssl symlink=dir
+sql symlink=dir
+vio symlink=dir
diff --git a/libmysqlclient/mysql/buildfile b/libmysqlclient/mysql/buildfile
index cea8d4c..37a627a 100644
--- a/libmysqlclient/mysql/buildfile
+++ b/libmysqlclient/mysql/buildfile
@@ -37,9 +37,7 @@ lib{mysqlclient}: {h c }{* -version -config} \
sql/auth/{ hxx }{* } \
sql/auth/{ cxx}{password sha2_password_common} \
sql-common/{ hxx cxx}{* } \
- libmysql/{ hxx cxx}{* -*test*} \
- { def}{libmysql_exports } \
- $imp_libs
+ libmysql/{ hxx cxx}{* -*test*}
lib{mysqlclient}: mysys/cxx{posix_timers}: include = $linux
lib{mysqlclient}: mysys/cxx{kqueue_timers}: include = ($bsd || $macos)
@@ -48,6 +46,10 @@ lib{mysqlclient}: mysys/{ cxx}{$mysys_win32} \
vio/{ cxx}{$vio_win32 } \
libmysql/authentication_win/{hxx cxx}{* }: include = $windows
+lib{mysqlclient}: $imp_libs
+
+libs{mysqlclient}: def{libmysql_exports}: include = $windows
+
# Makes sense to distribute READMEs for the bundled libraries. Note that their
# licenses are incorporated into the root LICENSE file.
#
@@ -65,7 +67,7 @@ lib{mysqlclient}: strings/file{README}
# Fedora/RHEL: /var/lib/mysql/mysql.sock
# Source package: /tmp/mysql.sock
#
-h{version}: mysql/in{mysql_version} $src_root/manifest
+h{version}: in{version} $src_root/manifest
{
dist = true
clean = ($src_root != $out_root)
@@ -229,14 +231,14 @@ switch $tclass, $tsys
#
cc.loptions += -Wl,--no-undefined
- cc.libs += -ldl -lpthread -lm -lrt
+ cc.libs += -ldl -lm -lrt -pthread
}
case 'bsd'
- cc.libs += -lexecinfo -lpthread -lm
+ cc.libs += -lexecinfo -lm -pthread
default
- cc.libs += -ldl -lpthread -lm
+ cc.libs += -ldl -lm -pthread
}
# Export options.
diff --git a/libmysqlclient/mysql/my_config.h b/libmysqlclient/mysql/my_config.h
index e13079b..7914fcf 100644
--- a/libmysqlclient/mysql/my_config.h
+++ b/libmysqlclient/mysql/my_config.h
@@ -17,14 +17,15 @@
*/
/*
- * Auto-generated. Defines the following configuration macros:
+ * Includes auto-generated version.h. Defines the following configuration
+ * macros:
#define PROTOCOL_VERSION
* Keep all the macros listed (in this exact form) for the change tracking
* (see README-DEV).
*/
-#include <mysql/version.h> // PROTOCOL_VERSION, MYSQL_SERVER_VERSION
+#include <mysql/mysql_version.h> // PROTOCOL_VERSION, MYSQL_SERVER_VERSION
/*
* Auto-generated. Defines macros that depend on the version and build2
@@ -108,15 +109,24 @@
# define SIZEOF_CHARP SIZEOF_VOIDP
#endif
-#define HAVE_STRUCT_TIMESPEC
-
/*
* Hard to even find any records of these types.
*/
-#undef HAVE_UINT
#undef HAVE_ULONG
/*
+ * GNU libc added strlcpy() and strlcat() in version 2.38 (in anticipation
+ * of their addition to POSIX).
+ */
+#if defined(__FreeBSD__) || \
+ defined(__APPLE__) || \
+ (defined(__GLIBC__) && \
+ defined(__GLIBC_MINOR__) && \
+ (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 38))
+# define HAVE_STRLCAT 1
+#endif
+
+/*
* Specific for Linux.
*/
#ifdef __linux__
@@ -168,7 +178,6 @@
#if defined(__FreeBSD__) || defined(__linux__)
# define HAVE_CLOCK_GETTIME 1
# define HAVE_CUSERID 1
-# define HAVE_SIGEV_THREAD_ID 1
# define HAVE_POSIX_FALLOCATE 1
#endif
@@ -181,7 +190,6 @@
# define HAVE_KQUEUE_TIMERS 1
# define HAVE_SOCKADDR_IN_SIN_LEN 1
# define HAVE_SOCKADDR_IN6_SIN6_LEN 1
-# define HAVE_STRLCAT 1
#endif
/*
@@ -210,8 +218,6 @@
# define HAVE_GETPWUID 1
# define HAVE_GETRLIMIT 1
# define HAVE_GETRUSAGE 1
-# define HAVE_READLINK 1
-# define HAVE_REALPATH 1
# define HAVE_STPCPY 1
# define HAVE_STPNCPY 1
# define HAVE_NL_LANGINFO 1
@@ -223,7 +229,6 @@
# define TIME_WITH_SYS_TIME 1
# define HAVE_ARPA_INET_H 1
# define HAVE_NETINET_IN_H 1
-# define HAVE_CHOWN 1
# define HAVE_FCHMOD 1
# define HAVE_MLOCKALL 1
# define HAVE_SYS_WAIT_H 1
@@ -247,6 +252,18 @@
# define HAVE_BUILTIN_STPCPY 1
/*
+ * Libedit.
+ */
+# define USE_LIBEDIT_INTERFACE 1
+# define USE_NEW_EDITLINE_INTERFACE 1
+# define HAVE_HIST_ENTRY 1
+# undef HAVE_TERM_H
+
+# define GWINSZ_IN_SYS_IOCTL 1
+# define HAVE_INDEX 1
+# define HAVE_INITGROUPS 1
+
+/*
* Specific for Windows.
*/
#else
@@ -254,6 +271,7 @@
# define HAVE_TELL 1
# define NO_FCNTL_NONBLOCK 1
# define DEFAULT_TMPDIR ""
+# define FN_NO_CASE_SENSE 1
#endif
#ifdef _WIN32
@@ -267,10 +285,11 @@
/*
* Common for all supported OSes/compilers.
*/
-#define HAVE_STRNLEN 1
-#define MAX_INDEXES 64U
-#define HAVE_SYS_TYPES_H 1
-#define STACK_DIRECTION -1
+#define MAX_INDEXES 64U
+#define HAVE_SYS_TYPES_H 1
+#define STACK_DIRECTION -1
+#define CPU_LEVEL1_DCACHE_LINESIZE 64
+#define DEFAULT_SECURE_FILE_PRIV_DIR "NULL"
/*
* Default character set and collation.
@@ -307,8 +326,8 @@
#undef HAVE_PRINTSTACK
#undef HAVE_GETHRTIME
#undef HAVE_GETPASSPHRASE
-#undef HAVE_SIGEV_PORT
#undef HAVE_SOLARIS_ATOMIC
+#undef LINUX_ALPINE
/*
* The upstream package undefines it for all supported compilers.
diff --git a/libmysqlclient/mysql/sql-common b/libmysqlclient/mysql/sql-common
deleted file mode 120000
index 422c9c7..0000000
--- a/libmysqlclient/mysql/sql-common
+++ /dev/null
@@ -1 +0,0 @@
-../../upstream/sql-common \ No newline at end of file
diff --git a/libmysqlclient/mysql/sql-common/client.cc b/libmysqlclient/mysql/sql-common/client.cc
new file mode 120000
index 0000000..df85e94
--- /dev/null
+++ b/libmysqlclient/mysql/sql-common/client.cc
@@ -0,0 +1 @@
+../../../upstream/sql-common/client.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/sql-common/client_authentication.cc b/libmysqlclient/mysql/sql-common/client_authentication.cc
new file mode 120000
index 0000000..90e7670
--- /dev/null
+++ b/libmysqlclient/mysql/sql-common/client_authentication.cc
@@ -0,0 +1 @@
+../../../upstream/sql-common/client_authentication.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/sql-common/client_plugin.cc b/libmysqlclient/mysql/sql-common/client_plugin.cc
new file mode 120000
index 0000000..f80c44e
--- /dev/null
+++ b/libmysqlclient/mysql/sql-common/client_plugin.cc
@@ -0,0 +1 @@
+../../../upstream/sql-common/client_plugin.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/sql-common/get_password.cc b/libmysqlclient/mysql/sql-common/get_password.cc
new file mode 120000
index 0000000..934324b
--- /dev/null
+++ b/libmysqlclient/mysql/sql-common/get_password.cc
@@ -0,0 +1 @@
+../../../upstream/sql-common/get_password.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/sql-common/my_path_permissions.cc b/libmysqlclient/mysql/sql-common/my_path_permissions.cc
new file mode 120000
index 0000000..f0cc1d4
--- /dev/null
+++ b/libmysqlclient/mysql/sql-common/my_path_permissions.cc
@@ -0,0 +1 @@
+../../../upstream/sql-common/my_path_permissions.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/sql-common/net_serv.cc b/libmysqlclient/mysql/sql-common/net_serv.cc
new file mode 120000
index 0000000..0a6d83c
--- /dev/null
+++ b/libmysqlclient/mysql/sql-common/net_serv.cc
@@ -0,0 +1 @@
+../../../upstream/sql-common/net_serv.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/sql-common/sql_string.cc b/libmysqlclient/mysql/sql-common/sql_string.cc
new file mode 100644
index 0000000..8efa97a
--- /dev/null
+++ b/libmysqlclient/mysql/sql-common/sql_string.cc
@@ -0,0 +1,1254 @@
+/* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License, version 2.0,
+ as published by the Free Software Foundation.
+
+ This program is also distributed with certain software (including
+ but not limited to OpenSSL) that is licensed under separate terms,
+ as designated in a particular file or component or in included license
+ documentation. The authors of MySQL hereby grant you an additional
+ permission to link the program and your derivative works with the
+ separately licensed software that they have included with MySQL.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License, version 2.0, for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+
+#include "sql_string.h"
+
+#include <limits>
+#include <algorithm>
+
+#include "my_dbug.h"
+#include "my_macros.h"
+#include "my_pointer_arithmetic.h"
+#include "my_sys.h"
+#include "mysql_com.h" // MAX_BIGINT_WIDTH
+
+using std::max;
+using std::min;
+
+#ifdef MYSQL_SERVER
+PSI_memory_key key_memory_String_value;
+#endif
+
+/*****************************************************************************
+** String functions
+*****************************************************************************/
+
+bool String::real_alloc(size_t length) {
+ size_t arg_length = ALIGN_SIZE(length + 1);
+ DBUG_ASSERT(arg_length > length);
+ if (arg_length <= length) return true; /* Overflow */
+ m_length = 0;
+ if (m_alloced_length < arg_length) {
+ mem_free();
+ if (!(m_ptr = static_cast<char *>(
+ my_malloc(STRING_PSI_MEMORY_KEY, arg_length, MYF(MY_WME)))))
+ return true;
+ m_alloced_length = static_cast<uint32>(arg_length);
+ m_is_alloced = true;
+ }
+ m_ptr[0] = 0;
+ return false;
+}
+
+/**
+ Allocates a new buffer on the heap for this String.
+
+ - If the String's internal buffer is privately owned and heap allocated,
+ one of the following is performed.
+
+ - If the requested length is greater than what fits in the buffer, a new
+ buffer is allocated, data moved and the old buffer freed.
+
+ - If the requested length is less or equal to what fits in the buffer, a
+ null character is inserted at the appropriate position.
+
+ - If the String does not keep a private buffer on the heap:
+
+ - If the requested length is greater than what fits in the buffer, or
+ force_on_heap is true, a new buffer is allocated, data is copied.
+ - If the requested length is less or equal to what fits in the buffer,
+ and force_on_heap is false, a null character is inserted at the
+ appropriate position.
+
+ For C compatibility, the new string buffer is null terminated.
+
+ @param alloc_length The requested string size in characters, excluding any
+ null terminator.
+ @param force_on_heap If the caller wants String's 'str' buffer to be on the
+ heap in all cases.
+
+ @retval false Either the copy operation is complete or, if the size of the
+ new buffer is smaller than the currently allocated buffer (if one exists),
+ no allocation occured.
+
+ @retval true An error occured when attempting to allocate memory or memory
+ allocation length exceeded allowed limit (4GB) for String Class.
+*/
+bool String::mem_realloc(size_t alloc_length, bool force_on_heap) {
+ size_t len = ALIGN_SIZE(alloc_length + 1);
+ DBUG_ASSERT(len > alloc_length);
+ if (len <= alloc_length) return true; /* Overflow */
+
+ if (force_on_heap && !m_is_alloced) {
+ /*
+ Caller wants bytes on the heap, and the currently available bytes are
+ not; they are thus irrelevant:
+ */
+ m_alloced_length = 0;
+ }
+
+ if (m_alloced_length < len) { // Available bytes are not enough
+ // Signal an error if len exceeds uint32 max on 64-bit word platform.
+#if defined(__WORDSIZE) && (__WORDSIZE == 64)
+ if (len > std::numeric_limits<uint32>::max()) return true;
+#endif
+ char *new_ptr;
+ if (m_is_alloced) {
+ if (!(new_ptr = static_cast<char *>(
+ my_realloc(STRING_PSI_MEMORY_KEY, m_ptr, len, MYF(MY_WME)))))
+ return true; // Signal error
+ } else if ((new_ptr = static_cast<char *>(
+ my_malloc(STRING_PSI_MEMORY_KEY, len, MYF(MY_WME))))) {
+ if (m_length > len - 1) m_length = 0;
+ if (m_length > 0) memcpy(new_ptr, m_ptr, m_length);
+ new_ptr[m_length] = 0;
+ m_is_alloced = true;
+ } else
+ return true; // Signal error
+ m_ptr = new_ptr;
+ m_alloced_length = static_cast<uint32>(len);
+ }
+ m_ptr[alloc_length] = 0; // This make other funcs shorter
+ return false;
+}
+
+/*
+ Helper function for @see mem_realloc_exp.
+ */
+inline size_t String::next_realloc_exp_size(size_t sz) {
+ const size_t len = ALIGN_SIZE(sz + 1);
+ const size_t ret =
+ (m_is_alloced && m_alloced_length < len) ? sz + (m_length / 4) : sz;
+ return ret;
+}
+
+/**
+ This function is used by the various append() member functions, to ensure
+ that append() has amortized constant cost. Once we have started to allocate
+ buffer on the heap, we increase the buffer size exponentially, rather
+ than linearly.
+
+ @param alloc_length The requested string size in characters, excluding any
+ null terminator.
+
+ @retval false Either the copy operation is complete or, if the size of the
+ new buffer is smaller than the currently allocated buffer (if one exists),
+ no allocation occured.
+
+ @retval true An error occured when attempting to allocate memory.
+
+ @see mem_realloc.
+ */
+bool String::mem_realloc_exp(size_t alloc_length) {
+ if (mem_realloc(next_realloc_exp_size(alloc_length))) return true;
+ m_ptr[alloc_length] = '\0';
+ return false;
+}
+
+bool String::set_int(longlong num, bool unsigned_flag, const CHARSET_INFO *cs) {
+ uint l = 20 * cs->mbmaxlen + 1;
+ int base = unsigned_flag ? 10 : -10;
+
+ if (alloc(l)) return true;
+ m_length = (uint32)(cs->cset->longlong10_to_str)(cs, m_ptr, l, base, num);
+ m_charset = cs;
+ return false;
+}
+
+bool String::set_real(double num, uint decimals, const CHARSET_INFO *cs) {
+ char buff[FLOATING_POINT_BUFFER];
+ uint dummy_errors;
+ size_t len;
+
+ m_charset = cs;
+ if (decimals >= NOT_FIXED_DEC) {
+ len = my_gcvt(num, MY_GCVT_ARG_DOUBLE, static_cast<int>(sizeof(buff)) - 1,
+ buff, NULL);
+ return copy(buff, len, &my_charset_latin1, cs, &dummy_errors);
+ }
+ len = my_fcvt(num, decimals, buff, NULL);
+ return copy(buff, len, &my_charset_latin1, cs, &dummy_errors);
+}
+
+bool String::copy() {
+ if (!m_is_alloced) {
+ m_alloced_length = 0; // Force realloc
+ return mem_realloc(m_length);
+ }
+ return false;
+}
+
+/**
+ Copies the internal buffer from str. If this String has a private heap
+ allocated buffer where new data does not fit, a new buffer is allocated
+ before copying and the old buffer freed. Character set information is also
+ copied.
+
+ If str is the same as this and str doesn't own its buffer, a
+ new buffer is allocated and it's owned by str.
+
+ @param str The string whose internal buffer is to be copied.
+
+ @retval false Success.
+ @retval true Memory allocation failed.
+*/
+bool String::copy(const String &str) {
+ /*
+ If &str == this and it owns the buffer, this operation is a no-op, so skip
+ the meaningless copy. Otherwise if we do, we will read freed memory at
+ the memmove call below.
+ */
+ if (&str == this && str.is_alloced()) return false;
+
+ /*
+ If a String s doesn't own its buffer, here we should allocate
+ a new buffer owned by s and copy the contents there. But alloc()
+ will change this->m_ptr and this->m_length, and if this == &str, this
+ will also change str->m_ptr and str->m_length, so we need to save
+ these values first.
+ */
+ const size_t str_length = str.m_length;
+ const char *str_ptr = str.m_ptr;
+ if (alloc(str.m_length)) return true;
+ m_length = str_length;
+ if (m_length > 0) memmove(m_ptr, str_ptr, m_length); // May be overlapping
+ m_ptr[m_length] = 0;
+ m_charset = str.m_charset;
+ return false;
+}
+
+bool String::copy(const char *str, size_t arg_length, const CHARSET_INFO *cs) {
+ if (alloc(arg_length)) return true;
+ if ((m_length = arg_length)) memcpy(m_ptr, str, arg_length);
+ m_ptr[arg_length] = 0;
+ m_charset = cs;
+ return false;
+}
+
+/*
+ Checks that the source string can be just copied to the destination string
+ without conversion.
+
+ SYNPOSIS
+
+ needs_conversion()
+ arg_length Length of string to copy.
+ from_cs Character set to copy from
+ to_cs Character set to copy to
+ uint32 *offset Returns number of unaligned characters.
+
+ RETURN
+ 0 No conversion needed
+ 1 Either character set conversion or adding leading zeros
+ (e.g. for UCS-2) must be done
+
+ NOTE
+ to_cs may be NULL for "no conversion" if the system variable
+ character_set_results is NULL.
+*/
+
+bool String::needs_conversion(size_t arg_length, const CHARSET_INFO *from_cs,
+ const CHARSET_INFO *to_cs, size_t *offset) {
+ *offset = 0;
+ if (!to_cs || (to_cs == &my_charset_bin) || (to_cs == from_cs) ||
+ my_charset_same(from_cs, to_cs) ||
+ ((from_cs == &my_charset_bin) &&
+ (!(*offset = (arg_length % to_cs->mbminlen)))))
+ return false;
+ return true;
+}
+
+/*
+ Checks that the source string can just be copied to the destination string
+ without conversion.
+ Unlike needs_conversion it will require conversion on incoming binary data
+ to ensure the data are verified for vailidity first.
+
+ @param arg_length Length of string to copy.
+ @param from_cs Character set to copy from
+ @param to_cs Character set to copy to
+
+ @return conversion needed
+*/
+bool String::needs_conversion_on_storage(size_t arg_length,
+ const CHARSET_INFO *cs_from,
+ const CHARSET_INFO *cs_to) {
+ size_t offset;
+ return (needs_conversion(arg_length, cs_from, cs_to, &offset) ||
+ /* force conversion when storing a binary string */
+ (cs_from == &my_charset_bin &&
+ /* into a non-binary destination */
+ cs_to != &my_charset_bin &&
+ /* and any of the following is true :*/
+ (
+ /* it's a variable length encoding */
+ cs_to->mbminlen != cs_to->mbmaxlen ||
+ /* longer than 2 bytes : neither 1 byte nor ucs2 */
+ cs_to->mbminlen > 2 ||
+ /* and is not a multiple of the char byte size */
+ 0 != (arg_length % cs_to->mbmaxlen))));
+}
+
+/*
+ Copy a multi-byte character sets with adding leading zeros.
+
+ SYNOPSIS
+
+ copy_aligned()
+ str String to copy
+ arg_length Length of string. This should NOT be dividable with
+ cs->mbminlen.
+ offset arg_length % cs->mb_minlength
+ cs Character set for 'str'
+
+ NOTES
+ For real multi-byte, ascii incompatible charactser sets,
+ like UCS-2, add leading zeros if we have an incomplete character.
+ Thus,
+ SELECT _ucs2 0xAA
+ will automatically be converted into
+ SELECT _ucs2 0x00AA
+
+ RETURN
+ 0 ok
+ 1 error
+*/
+
+bool String::copy_aligned(const char *str, size_t arg_length, size_t offset,
+ const CHARSET_INFO *cs) {
+ /* How many bytes are in incomplete character */
+ offset = cs->mbminlen - offset; /* How many zeros we should prepend */
+ DBUG_ASSERT(offset && offset != cs->mbminlen);
+
+ size_t aligned_length = arg_length + offset;
+ if (alloc(aligned_length)) return true;
+
+ /*
+ Note, this is only safe for big-endian UCS-2.
+ If we add little-endian UCS-2 sometimes, this code
+ will be more complicated. But it's OK for now.
+ */
+ memset(m_ptr, 0, offset);
+ memcpy(m_ptr + offset, str, arg_length);
+ m_ptr[aligned_length] = 0;
+ /* m_length is always >= 0 as arg_length is != 0 */
+ m_length = aligned_length;
+ m_charset = cs;
+ return false;
+}
+
+bool String::set_or_copy_aligned(const char *str, size_t arg_length,
+ const CHARSET_INFO *cs) {
+ /* How many bytes are in incomplete character */
+ size_t offset = (arg_length % cs->mbminlen);
+
+ if (!offset) /* All characters are complete, just copy */
+ {
+ set(str, arg_length, cs);
+ return false;
+ }
+ return copy_aligned(str, arg_length, offset, cs);
+}
+
+/**
+ Copies the character data into this String, with optional character set
+ conversion.
+
+ @return
+ false ok
+ true Could not allocate result buffer
+
+*/
+
+bool String::copy(const char *str, size_t arg_length,
+ const CHARSET_INFO *from_cs, const CHARSET_INFO *to_cs,
+ uint *errors) {
+ size_t offset;
+
+ DBUG_ASSERT(!str || str != m_ptr);
+
+ if (!needs_conversion(arg_length, from_cs, to_cs, &offset)) {
+ *errors = 0;
+ return copy(str, arg_length, to_cs);
+ }
+ if ((from_cs == &my_charset_bin) && offset) {
+ *errors = 0;
+ return copy_aligned(str, arg_length, offset, to_cs);
+ }
+ size_t new_length = to_cs->mbmaxlen * arg_length;
+ if (alloc(new_length)) return true;
+ m_length = copy_and_convert(m_ptr, new_length, to_cs, str, arg_length,
+ from_cs, errors);
+ m_charset = to_cs;
+ return false;
+}
+
+/*
+ Set a string to the value of a latin1-string, keeping the original charset
+
+ SYNOPSIS
+ copy_or_set()
+ str String of a simple charset (latin1)
+ arg_length Length of string
+
+ IMPLEMENTATION
+ If string object is of a simple character set, set it to point to the
+ given string.
+ If not, make a copy and convert it to the new character set.
+
+ RETURN
+ 0 ok
+ 1 Could not allocate result buffer
+
+*/
+
+bool String::set_ascii(const char *str, size_t arg_length) {
+ if (m_charset->mbminlen == 1) {
+ set(str, arg_length, m_charset);
+ return 0;
+ }
+ uint dummy_errors;
+ return copy(str, arg_length, &my_charset_latin1, m_charset, &dummy_errors);
+}
+
+/* This is used by mysql.cc */
+
+bool String::fill(size_t max_length, char fill_char) {
+ if (m_length > max_length)
+ m_ptr[m_length = max_length] = 0;
+ else {
+ if (mem_realloc(max_length)) return true;
+ memset(m_ptr + m_length, fill_char, max_length - m_length);
+ m_length = max_length;
+ }
+ return false;
+}
+
+void String::strip_sp() {
+ while (m_length && my_isspace(m_charset, m_ptr[m_length - 1])) m_length--;
+}
+
+bool String::append(const String &s) {
+ if (s.length()) {
+ DBUG_ASSERT(!this->uses_buffer_owned_by(&s));
+ DBUG_ASSERT(!s.uses_buffer_owned_by(this));
+
+ if (mem_realloc_exp((m_length + s.length()))) return true;
+ memcpy(m_ptr + m_length, s.ptr(), s.length());
+ m_length += s.length();
+ }
+ return false;
+}
+
+/*
+ Append an ASCII string to the a string of the current character set
+*/
+
+bool String::append(const char *s, size_t arg_length) {
+ if (!arg_length) return false;
+
+ /*
+ For an ASCII incompatible string, e.g. UCS-2, we need to convert
+ */
+ if (m_charset->mbminlen > 1) {
+ size_t add_length = arg_length * m_charset->mbmaxlen;
+ uint dummy_errors;
+ if (mem_realloc(m_length + add_length)) return true;
+ m_length += copy_and_convert(m_ptr + m_length, add_length, m_charset, s,
+ arg_length, &my_charset_latin1, &dummy_errors);
+ return false;
+ }
+
+ /*
+ For an ASCII compatinble string we can just append.
+ */
+ if (mem_realloc_exp(m_length + arg_length)) return true;
+ memcpy(m_ptr + m_length, s, arg_length);
+ m_length += arg_length;
+ return false;
+}
+
+/*
+ Append a 0-terminated ASCII string
+*/
+
+bool String::append(const char *s) { return append(s, (uint)strlen(s)); }
+
+/**
+ Append an unsigned longlong to the string.
+*/
+bool String::append_ulonglong(ulonglong val) {
+ if (mem_realloc_exp(m_length + MAX_BIGINT_WIDTH + 2)) return true;
+ char *end = longlong10_to_str(val, m_ptr + m_length, 10);
+ m_length = end - m_ptr;
+ return false;
+}
+
+/**
+ Append a signed longlong to the string.
+*/
+bool String::append_longlong(longlong val) {
+ if (mem_realloc_exp(m_length + MAX_BIGINT_WIDTH + 2))
+ return true; /* purecov: inspected */
+ char *end = longlong10_to_str(val, m_ptr + m_length, -10);
+ m_length = end - m_ptr;
+ return false;
+}
+
+/*
+ Append a string in the given charset to the string
+ with character set recoding
+*/
+
+bool String::append(const char *s, size_t arg_length, const CHARSET_INFO *cs) {
+ size_t offset;
+
+ if (needs_conversion(arg_length, cs, m_charset, &offset)) {
+ size_t add_length;
+ if ((cs == &my_charset_bin) && offset) {
+ DBUG_ASSERT(m_charset->mbminlen > offset);
+ offset = m_charset->mbminlen - offset; // How many characters to pad
+ add_length = arg_length + offset;
+ if (mem_realloc_exp(m_length + add_length)) return true;
+ memset(m_ptr + m_length, 0, offset);
+ memcpy(m_ptr + m_length + offset, s, arg_length);
+ m_length += add_length;
+ return false;
+ }
+
+ add_length = arg_length / cs->mbminlen * m_charset->mbmaxlen;
+ uint dummy_errors;
+ if (mem_realloc_exp(m_length + add_length)) return true;
+ m_length += copy_and_convert(m_ptr + m_length, add_length, m_charset, s,
+ arg_length, cs, &dummy_errors);
+ } else {
+ if (mem_realloc_exp(m_length + arg_length)) return true;
+ memcpy(m_ptr + m_length, s, arg_length);
+ m_length += arg_length;
+ }
+ return false;
+}
+
+bool String::append(IO_CACHE *file, size_t arg_length) {
+ if (mem_realloc(m_length + arg_length)) return true;
+ if (my_b_read(file, reinterpret_cast<uchar *>(m_ptr) + m_length,
+ arg_length)) {
+ shrink(m_length);
+ return true;
+ }
+ m_length += arg_length;
+ return false;
+}
+
+/**
+ Append a parenthesized number to String.
+ Used in various pieces of SHOW related code.
+
+ @param nr Number
+ @param radix Radix, optional parameter, 10 by default.
+*/
+bool String::append_parenthesized(long nr, int radix) {
+ char buff[64], *end;
+ buff[0] = '(';
+ end = int10_to_str(nr, buff + 1, radix);
+ *end++ = ')';
+ return append(buff, (uint)(end - buff));
+}
+
+bool String::append_with_prefill(const char *s, size_t arg_length,
+ size_t full_length, char fill_char) {
+ size_t t_length = arg_length > full_length ? arg_length : full_length;
+
+ if (mem_realloc(m_length + t_length)) return true;
+ if (full_length > arg_length) {
+ t_length = full_length - arg_length;
+ memset(m_ptr + m_length, fill_char, t_length);
+ m_length = m_length + t_length;
+ }
+ append(s, arg_length);
+ return false;
+}
+
+size_t String::numchars() const {
+ return m_charset->cset->numchars(m_charset, m_ptr, m_ptr + m_length);
+}
+
+size_t String::charpos(size_t i, size_t offset) const {
+ if (i <= 0) return i;
+ return m_charset->cset->charpos(m_charset, m_ptr + offset, m_ptr + m_length,
+ i);
+}
+
+int String::strstr(const String &s, size_t offset) const {
+ if (s.length() + offset <= m_length) {
+ if (!s.length()) return ((int)offset); // Empty string is always found
+
+ const char *str = m_ptr + offset;
+ const char *search = s.ptr();
+ const char *end = m_ptr + m_length - s.length() + 1;
+ const char *search_end = s.ptr() + s.length();
+ skip:
+ while (str != end) {
+ if (*str++ == *search) {
+ const char *i = str;
+ const char *j = search + 1;
+ while (j != search_end)
+ if (*i++ != *j++) goto skip;
+ return (int)(str - m_ptr) - 1;
+ }
+ }
+ }
+ return -1;
+}
+
+/*
+** Search string from end. Offset is offset to the end of string
+*/
+
+int String::strrstr(const String &s, size_t offset) const {
+ if (s.length() <= offset && offset <= m_length) {
+ if (!s.length())
+ return static_cast<int>(offset); // Empty string is always found
+ const char *str = m_ptr + offset - 1;
+ const char *search = s.ptr() + s.length() - 1;
+
+ const char *end = m_ptr + s.length() - 2;
+ const char *search_end = s.ptr() - 1;
+ skip:
+ while (str != end) {
+ if (*str-- == *search) {
+ const char *i = str;
+ const char *j = search - 1;
+ while (j != search_end)
+ if (*i-- != *j--) goto skip;
+ return (int)(i - m_ptr) + 1;
+ }
+ }
+ }
+ return -1;
+}
+
+String String::substr(int offset, int count) const {
+ int original_count = this->numchars();
+ if (offset > original_count) {
+ offset = original_count;
+ }
+ if (offset + count > original_count) {
+ count = original_count - offset;
+ }
+ size_t bytes_offset = this->charpos(offset);
+
+ return String(this->m_ptr + bytes_offset,
+ this->charpos(offset + count) - bytes_offset, this->m_charset);
+}
+
+/*
+ Replace substring with string
+ If wrong parameter or not enough memory, do nothing
+*/
+
+bool String::replace(size_t offset, size_t arg_length, const String &to) {
+ return replace(offset, arg_length, to.ptr(), to.length());
+}
+
+bool String::replace(size_t offset, size_t arg_length, const char *to,
+ size_t to_length) {
+ long diff = static_cast<long>(to_length) - static_cast<long>(arg_length);
+ if (offset + arg_length <= m_length) {
+ if (diff < 0) {
+ if (to_length) memcpy(m_ptr + offset, to, to_length);
+ memmove(m_ptr + offset + to_length, m_ptr + offset + arg_length,
+ m_length - offset - arg_length);
+ } else {
+ if (diff) {
+ if (mem_realloc(m_length + diff)) return true;
+ memmove(m_ptr + offset + to_length, m_ptr + offset + arg_length,
+ m_length - offset - arg_length);
+ }
+ if (to_length) memcpy(m_ptr + offset, to, to_length);
+ }
+ m_length += diff;
+ }
+ return false;
+}
+
+// added by Holyfoot for "geometry" needs
+int String::reserve(size_t space_needed, size_t grow_by) {
+ if (m_alloced_length < m_length + space_needed) {
+ if (mem_realloc(m_alloced_length + max(space_needed, grow_by) - 1))
+ return true;
+ }
+ return false;
+}
+
+void qs_append(const char *str_in, size_t len, String *str) {
+ memcpy(&((*str)[str->length()]), str_in, len + 1);
+ str->length(str->length() + len);
+}
+
+void qs_append(double d, size_t len, String *str) {
+ char *buff = &((*str)[str->length()]);
+ int written = my_gcvt(d, MY_GCVT_ARG_DOUBLE, len, buff, NULL);
+ str->length(str->length() + written);
+}
+
+void qs_append(int i, String *str) {
+ char *buff = &((*str)[str->length()]);
+ char *end = int10_to_str(i, buff, -10);
+ str->length(str->length() + (int)(end - buff));
+}
+
+void qs_append(uint i, String *str) {
+ char *buff = &((*str)[str->length()]);
+ char *end = int10_to_str(i, buff, 10);
+ str->length(str->length() + (int)(end - buff));
+}
+
+/*
+ Compare strings according to collation, without end space.
+
+ SYNOPSIS
+ sortcmp()
+ s First string
+ t Second string
+ cs Collation
+
+ NOTE:
+ Normally this is case sensitive comparison
+
+ RETURN
+ < 0 s < t
+ 0 s == t
+ > 0 s > t
+*/
+
+int sortcmp(const String *s, const String *t, const CHARSET_INFO *cs) {
+ return cs->coll->strnncollsp(cs, (uchar *)s->ptr(), s->length(),
+ (uchar *)t->ptr(), t->length());
+}
+
+/*
+ Compare strings byte by byte. End spaces are also compared.
+
+ SYNOPSIS
+ stringcmp()
+ s First string
+ t Second string
+
+ NOTE:
+ Strings are compared as a stream of uchars
+
+ RETURN
+ < 0 s < t
+ 0 s == t
+ > 0 s > t
+*/
+
+int stringcmp(const String *s, const String *t) {
+ size_t s_len = s->length();
+ size_t t_len = t->length();
+ size_t len = min(s_len, t_len);
+ int cmp = (len == 0) ? 0 : memcmp(s->ptr(), t->ptr(), len);
+ return (cmp) ? cmp : static_cast<int>(s_len) - static_cast<int>(t_len);
+}
+
+/**
+ Makes a copy of a String's buffer unless it's already heap-allocated.
+
+ If the buffer ('str') of 'from' is on the heap, this function returns
+ 'from', possibly re-allocated to be at least from_length bytes long.
+ It is also the case if from==to or to==NULL.
+ Otherwise, this function makes and returns a copy of "from" into "to"; the
+ buffer of "to" is heap-allocated; a pre-condition is that \c from->str and
+ \c to->str must point to non-overlapping buffers.
+ The logic behind this complex design, is that a caller, typically a
+ val_str() function, sometimes has an input String ('from') which buffer it
+ wants to modify; but this String's buffer may or not be heap-allocated; if
+ it's not heap-allocated it is possibly in static storage or belongs to an
+ outer context, and thus should not be modified; in that case the caller
+ wants a heap-allocated copy which it can freely modify.
+
+ @param to destination string
+ @param from source string
+ @param from_length destination string will hold at least from_length bytes.
+ */
+
+String *copy_if_not_alloced(String *to, String *from, size_t from_length) {
+ if (from->m_is_alloced && from->m_alloced_length >= from_length) return from;
+ if ((from->m_is_alloced && (from->m_alloced_length != 0)) || !to ||
+ from == to) {
+ (void)from->mem_realloc(from_length, true /* force heap allocation */);
+ return from;
+ }
+ if (to->mem_realloc(from_length, true)) return from; // Actually an error
+
+ // from and to should not be overlapping
+ DBUG_ASSERT(!to->uses_buffer_owned_by(from));
+ DBUG_ASSERT(!from->uses_buffer_owned_by(to));
+
+ if ((to->m_length = min(from->m_length, from_length)))
+ memcpy(to->m_ptr, from->m_ptr, to->m_length);
+ to->m_charset = from->m_charset;
+ return to;
+}
+
+/****************************************************************************
+ Help functions
+****************************************************************************/
+
+/*
+ copy a string,
+ with optional character set conversion,
+ with optional left padding (for binary -> UCS2 conversion)
+
+ SYNOPSIS
+ well_formed_copy_nchars()
+ to Store result here
+ to_length Maxinum length of "to" string
+ to_cs Character set of "to" string
+ from Copy from here
+ from_length Length of from string
+ from_cs From character set
+ nchars Copy not more that nchars characters
+ well_formed_error_pos Return position when "from" is not well formed
+ or NULL otherwise.
+ cannot_convert_error_pos Return position where a not convertable
+ character met, or NULL otherwise.
+ from_end_pos Return position where scanning of "from"
+ string stopped.
+ NOTES
+
+ RETURN
+ length of bytes copied to 'to'
+*/
+
+size_t well_formed_copy_nchars(const CHARSET_INFO *to_cs, char *to,
+ size_t to_length, const CHARSET_INFO *from_cs,
+ const char *from, size_t from_length,
+ size_t nchars,
+ const char **well_formed_error_pos,
+ const char **cannot_convert_error_pos,
+ const char **from_end_pos) {
+ size_t res;
+
+ if ((to_cs == &my_charset_bin) || (from_cs == &my_charset_bin) ||
+ (to_cs == from_cs) || my_charset_same(from_cs, to_cs)) {
+ if (to_length < to_cs->mbminlen || !nchars) {
+ *from_end_pos = from;
+ *cannot_convert_error_pos = NULL;
+ *well_formed_error_pos = NULL;
+ return 0;
+ }
+
+ if (to_cs == &my_charset_bin) {
+ res = min(min(nchars, to_length), from_length);
+ memmove(to, from, res);
+ *from_end_pos = from + res;
+ *well_formed_error_pos = NULL;
+ *cannot_convert_error_pos = NULL;
+ } else {
+ int well_formed_error;
+ uint from_offset;
+
+ if ((from_offset = (from_length % to_cs->mbminlen)) &&
+ (from_cs == &my_charset_bin)) {
+ /*
+ Copying from BINARY to UCS2 needs to prepend zeros sometimes:
+ INSERT INTO t1 (ucs2_column) VALUES (0x01);
+ 0x01 -> 0x0001
+ */
+ uint pad_length = to_cs->mbminlen - from_offset;
+ memset(to, 0, pad_length);
+ memmove(to + pad_length, from, from_offset);
+ /*
+ In some cases left zero-padding can create an incorrect character.
+ For example:
+ INSERT INTO t1 (utf32_column) VALUES (0x110000);
+ We'll pad the value to 0x00110000, which is a wrong UTF32 sequence!
+ The valid characters range is limited to 0x00000000..0x0010FFFF.
+
+ Make sure we didn't pad to an incorrect character.
+ */
+ if (to_cs->cset->well_formed_len(to_cs, to, to + to_cs->mbminlen, 1,
+ &well_formed_error) !=
+ to_cs->mbminlen) {
+ *from_end_pos = *well_formed_error_pos = from;
+ *cannot_convert_error_pos = NULL;
+ return 0;
+ }
+ nchars--;
+ from += from_offset;
+ from_length -= from_offset;
+ to += to_cs->mbminlen;
+ to_length -= to_cs->mbminlen;
+ }
+
+ size_t min_length = min(from_length, to_length);
+ /*
+ If we operate on a multi-byte fixed-width character set, make
+ sure the string wasn't truncated in the middle of a character.
+ If so, truncate to a character boundary.
+ */
+ if (to_cs->mbmaxlen > 1 && to_cs->mbmaxlen == to_cs->mbminlen)
+ min_length -= min_length % to_cs->mbmaxlen;
+
+ res = to_cs->cset->well_formed_len(to_cs, from, from + min_length, nchars,
+ &well_formed_error);
+ if (res > 0) memmove(to, from, res);
+ *from_end_pos = from + res;
+
+ /*
+ If we are operating on a multi-byte variable-width character set and
+ "well_formed_error" is set to true, it means the string is not
+ well-formed (either partially or completely). But, in case of a
+ well-formed string whose string length is too long for the destination
+ buffer (i.e to_length), there is a possibility that the string is
+ truncated in the middle of a character, which would break its sequence.
+ This could lead to mistakenly rejecting the string as malformed.
+
+ To resolve this, we check if the full string contains a valid character
+ immediately after the returned end point. If it does, the string was
+ just truncated; if it doesn't, it was actually malformed.
+
+ For example: Consider a well-formed string of 300 bytes, which contained
+ a given three-byte code point at str[254..256]. Furthermore, suppose the
+ destination buffer is 255 bytes long. In this case, well_formed_len()
+ would return 254, so we would need to check the bytes str[254..257] to
+ see if they contain a well-formed character. This second invocation of
+ well_formed_len() would return 2, which takes us across the truncation
+ point, confirming that the problem was indeed truncation and not a
+ malformed code point.
+ */
+ if (well_formed_error && to_cs->mbmaxlen > 1 &&
+ res > min_length - to_cs->mbmaxlen) {
+ const char *from_end = from + min(res + to_cs->mbmaxlen, from_length);
+
+ size_t extra = to_cs->cset->well_formed_len(
+ to_cs, *from_end_pos, from_end, 1, &well_formed_error);
+
+ well_formed_error = (res + extra < min_length);
+ }
+
+ *well_formed_error_pos = well_formed_error ? *from_end_pos : nullptr;
+ *cannot_convert_error_pos = nullptr;
+ if (from_offset) res += to_cs->mbminlen;
+ }
+ } else {
+ int cnvres;
+ my_wc_t wc;
+ my_charset_conv_mb_wc mb_wc = from_cs->cset->mb_wc;
+ my_charset_conv_wc_mb wc_mb = to_cs->cset->wc_mb;
+ const uchar *from_end = (const uchar *)from + from_length;
+ uchar *to_end = (uchar *)to + to_length;
+ char *to_start = to;
+ *well_formed_error_pos = NULL;
+ *cannot_convert_error_pos = NULL;
+
+ for (; nchars; nchars--) {
+ const char *from_prev = from;
+ if ((cnvres = (*mb_wc)(from_cs, &wc, (uchar *)from, from_end)) > 0)
+ from += cnvres;
+ else if (cnvres == MY_CS_ILSEQ) {
+ if (!*well_formed_error_pos) *well_formed_error_pos = from;
+ from++;
+ wc = '?';
+ } else if (cnvres > MY_CS_TOOSMALL) {
+ /*
+ A correct multibyte sequence detected
+ But it doesn't have Unicode mapping.
+ */
+ if (!*cannot_convert_error_pos) *cannot_convert_error_pos = from;
+ from += (-cnvres);
+ wc = '?';
+ } else
+ break; // Not enough characters
+
+ outp:
+ if ((cnvres = (*wc_mb)(to_cs, wc, (uchar *)to, to_end)) > 0)
+ to += cnvres;
+ else if (cnvres == MY_CS_ILUNI && wc != '?') {
+ if (!*cannot_convert_error_pos) *cannot_convert_error_pos = from_prev;
+ wc = '?';
+ goto outp;
+ } else {
+ from = from_prev;
+ break;
+ }
+ }
+ *from_end_pos = from;
+ res = to - to_start;
+ }
+ return res;
+}
+
+void String::print(String *str) const {
+ char *st = m_ptr;
+ char *end = st + m_length;
+
+ if (str->reserve(m_length)) return;
+
+ for (; st < end; st++) {
+ uchar c = *st;
+ switch (c) {
+ case '\\':
+ str->append(STRING_WITH_LEN("\\\\"));
+ break;
+ case '\0':
+ str->append(STRING_WITH_LEN("\\0"));
+ break;
+ case '\'':
+ str->append(STRING_WITH_LEN("\\'"));
+ break;
+ case '\n':
+ str->append(STRING_WITH_LEN("\\n"));
+ break;
+ case '\r':
+ str->append(STRING_WITH_LEN("\\r"));
+ break;
+ case '\032': // Ctrl-Z
+ str->append(STRING_WITH_LEN("\\Z"));
+ break;
+ default:
+ str->append(c);
+ }
+ }
+}
+
+/*
+ Exchange state of this object and argument.
+
+ SYNOPSIS
+ String::swap()
+
+ RETURN
+ Target string will contain state of this object and vice versa.
+*/
+
+void String::swap(String &s) noexcept {
+ using std::swap;
+ swap(m_ptr, s.m_ptr);
+ swap(m_length, s.m_length);
+ swap(m_alloced_length, s.m_alloced_length);
+ swap(m_is_alloced, s.m_is_alloced);
+ swap(m_charset, s.m_charset);
+}
+
+char *String::dup(MEM_ROOT *root) const {
+ return strmake_root(root, m_ptr, m_length);
+}
+
+/**
+ Convert string to printable ASCII string
+
+ @details This function converts input string "from" replacing non-ASCII bytes
+ with hexadecimal sequences ("\xXX") optionally appending "..." to the end of
+ the resulting string.
+ This function used in the ER_TRUNCATED_WRONG_VALUE_FOR_FIELD error messages,
+ e.g. when a string cannot be converted to a result charset.
+
+
+ @param to output buffer
+ @param to_len size of the output buffer (8 bytes or greater)
+ @param from input string
+ @param from_len size of the input string
+ @param from_cs input charset
+ @param nbytes maximal number of bytes to convert (from_len if 0)
+
+ @return number of bytes in the output string
+*/
+
+size_t convert_to_printable(char *to, size_t to_len, const char *from,
+ size_t from_len, const CHARSET_INFO *from_cs,
+ size_t nbytes /*= 0*/) {
+ /* needs at least 8 bytes for '\xXX...' and zero byte */
+ DBUG_ASSERT(to_len >= 8);
+
+ char *t = to;
+ char *t_end = to + to_len - 1; // '- 1' is for the '\0' at the end
+ const char *f = from;
+ const char *f_end = from + (nbytes ? min(from_len, nbytes) : from_len);
+ char *dots = to; // last safe place to append '...'
+
+ if (!f || t == t_end) return 0;
+
+ for (; t < t_end && f < f_end; f++) {
+ /*
+ If the source string is ASCII compatible (mbminlen==1)
+ and the source character is in ASCII printable range (0x20..0x7F),
+ then display the character as is.
+
+ Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
+ or the source character is not in the printable range,
+ then print the character using HEX notation.
+ */
+ if (((unsigned char)*f) >= 0x20 && ((unsigned char)*f) <= 0x7F &&
+ from_cs->mbminlen == 1) {
+ *t++ = *f;
+ } else {
+ if (t_end - t < 4) // \xXX
+ break;
+ *t++ = '\\';
+ *t++ = 'x';
+ *t++ = _dig_vec_upper[((unsigned char)*f) >> 4];
+ *t++ = _dig_vec_upper[((unsigned char)*f) & 0x0F];
+ }
+ if (t_end - t >= 3) // '...'
+ dots = t;
+ }
+ if (f < from + from_len)
+ memcpy(dots, STRING_WITH_LEN("...\0"));
+ else
+ *t = '\0';
+ return t - to;
+}
+
+/**
+ Convert a buffer to printable HEX encoded string
+ For eg: ABCDEF1234
+
+
+ @param to output buffer
+ @param to_len size of the output buffer (from_len*2 + 1 or greater)
+ @param from input buffer
+ @param from_len size of the input buffer
+
+ @return number of bytes in the output string
+*/
+size_t bin_to_hex_str(char *to, size_t to_len, char *from, size_t from_len) {
+ char *out;
+ char *in;
+ size_t i;
+
+ if (to_len < ((from_len * 2) + 1)) return 0;
+
+ out = to;
+ in = from;
+
+ for (i = 0; i < from_len; i++, in++) {
+ *out++ = _dig_vec_upper[((unsigned char)*in) >> 4];
+ *out++ = _dig_vec_upper[((unsigned char)*in) & 0xF];
+ }
+
+ *out = '\0';
+
+ return out - to;
+}
+
+/**
+ Check if an input byte sequence is a valid character string of a given charset
+
+ @param cs The input character set.
+ @param str The input byte sequence to validate.
+ @param length A byte length of the str.
+ @param [out] valid_length A byte length of a valid prefix of the str.
+ @param [out] length_error True in the case of a character length error:
+ some byte[s] in the input is not a valid
+ prefix for a character, i.e. the byte length
+ of that invalid character is undefined.
+
+ @retval true if the whole input byte sequence is a valid character string.
+ The length_error output parameter is undefined.
+
+ @return
+ if the whole input byte sequence is a valid character string
+ then
+ return false
+ else
+ if the length of some character in the input is undefined (MY_CS_ILSEQ)
+ or the last character is truncated (MY_CS_TOOSMALL)
+ then
+ *length_error= true; // fatal error!
+ else
+ *length_error= false; // non-fatal error: there is no wide character
+ // encoding for some input character
+ return true
+*/
+bool validate_string(const CHARSET_INFO *cs, const char *str, size_t length,
+ size_t *valid_length, bool *length_error) {
+ if (cs->mbmaxlen > 1) {
+ int well_formed_error;
+ *valid_length = cs->cset->well_formed_len(cs, str, str + length, length,
+ &well_formed_error);
+ *length_error = well_formed_error;
+ return well_formed_error;
+ }
+
+ /*
+ well_formed_len() is not functional on single-byte character sets,
+ so use mb_wc() instead:
+ */
+ *length_error = false;
+
+ const uchar *from = reinterpret_cast<const uchar *>(str);
+ const uchar *from_end = from + length;
+ my_charset_conv_mb_wc mb_wc = cs->cset->mb_wc;
+
+ while (from < from_end) {
+ my_wc_t wc;
+ int cnvres = (*mb_wc)(cs, &wc, (uchar *)from, from_end);
+ if (cnvres <= 0) {
+ *valid_length = from - reinterpret_cast<const uchar *>(str);
+ return true;
+ }
+ from += cnvres;
+ }
+ *valid_length = length;
+ return false;
+}
+
+/**
+ Appends from_str to to_str, escaping certain characters.
+
+ @param [in,out] to_str The destination string.
+ @param [in] from_str The source string.
+
+ @return false on success, true on error.
+*/
+
+bool append_escaped(String *to_str, const String *from_str) {
+ if (to_str->mem_realloc(to_str->length() + from_str->length()))
+ return true; // OOM
+
+ const char *from = from_str->ptr();
+ const char *end = from + from_str->length();
+ for (; from < end; from++) {
+ char c = *from;
+ switch (c) {
+ case '\0':
+ c = '0';
+ break;
+ case '\032':
+ c = 'Z';
+ break;
+ case '\\':
+ case '\'':
+ break;
+ default:
+ goto normal_character;
+ }
+ if (to_str->append('\\')) return true; // OOM
+
+ normal_character:
+ if (to_str->append(c)) return true; // OOM
+ }
+ return false;
+}
diff --git a/libmysqlclient/mysql/sql-common/sql_string.cc.orig b/libmysqlclient/mysql/sql-common/sql_string.cc.orig
new file mode 120000
index 0000000..eca6a47
--- /dev/null
+++ b/libmysqlclient/mysql/sql-common/sql_string.cc.orig
@@ -0,0 +1 @@
+../../../upstream/sql-common/sql_string.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings b/libmysqlclient/mysql/strings
deleted file mode 120000
index 20be740..0000000
--- a/libmysqlclient/mysql/strings
+++ /dev/null
@@ -1 +0,0 @@
-../../upstream/strings \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/README b/libmysqlclient/mysql/strings/README
new file mode 120000
index 0000000..73c31e0
--- /dev/null
+++ b/libmysqlclient/mysql/strings/README
@@ -0,0 +1 @@
+../../../upstream/strings/README \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/conf_to_src.cc b/libmysqlclient/mysql/strings/conf_to_src.cc
new file mode 120000
index 0000000..7725bda
--- /dev/null
+++ b/libmysqlclient/mysql/strings/conf_to_src.cc
@@ -0,0 +1 @@
+../../../upstream/strings/conf_to_src.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-big5.cc b/libmysqlclient/mysql/strings/ctype-big5.cc
new file mode 120000
index 0000000..433d411
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-big5.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-big5.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-bin.cc b/libmysqlclient/mysql/strings/ctype-bin.cc
new file mode 120000
index 0000000..8c950a1
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-bin.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-bin.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-cp932.cc b/libmysqlclient/mysql/strings/ctype-cp932.cc
new file mode 120000
index 0000000..831921d
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-cp932.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-cp932.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-czech.cc b/libmysqlclient/mysql/strings/ctype-czech.cc
new file mode 100644
index 0000000..1440466
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-czech.cc
@@ -0,0 +1,699 @@
+/* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License, version 2.0,
+ as published by the Free Software Foundation.
+
+ This program is also distributed with certain software (including
+ but not limited to OpenSSL) that is licensed under separate terms,
+ as designated in a particular file or component or in included license
+ documentation. The authors of MySQL hereby grant you an additional
+ permission to link the program and your derivative works with the
+ separately licensed software that they have included with MySQL.
+
+ Without limiting anything contained in the foregoing, this file,
+ which is part of C Driver for MySQL (Connector/C), is also subject to the
+ Universal FOSS Exception, version 1.0, a copy of which can be found at
+ http://oss.oracle.com/licenses/universal-foss-exception.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License, version 2.0, for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+
+/* File strings/ctype-czech.c for MySQL.
+
+ This file implements the Czech sorting for the MySQL database
+ server (www.mysql.com). Due to some complicated rules the
+ Czech language has for sorting strings, a more complex
+ solution was needed than the one-to-one conversion table. To
+ note a few, here is an example of a Czech sorting sequence:
+
+ co < hlaska < hláska < hlava < chlapec < krtek
+
+ It because some of the rules are: double char 'ch' is sorted
+ between 'h' and 'i'. Accented character 'á' (a with acute) is
+ sorted after 'a' and before 'b', but only if the word is
+ otherwise the same. However, because 's' is sorted before 'v'
+ in hlava, the accentness of 'á' is overridden. There are many
+ more rules.
+
+ This file defines functions my_strxfrm and my_strcoll for
+ C-like zero terminated strings and my_strnxfrm and my_strnncoll
+ for strings where the length comes as an parameter. Also
+ defined here you will find function my_like_range that returns
+ index range strings for LIKE expression and the
+ MY_STRXFRM_MULTIPLY set to value 4 -- this is the ratio the
+ strings grows during my_strxfrm. The algorithm has four
+ passes, that's why we need four times more space for expanded
+ string.
+
+ This file also contains the ISO-Latin-2 definitions of
+ characters.
+
+ Author: (c) 1997--1998 Jan Pazdziora, adelton@fi.muni.cz
+ Jan Pazdziora has a shared copyright for this code
+
+ The original of this file can also be found at
+ http://www.fi.muni.cz/~adelton/l10n/
+
+ Bug reports and suggestions are always welcome.
+*/
+
+/*
+ * This comment is parsed by configure to create ctype.c,
+ * so don't change it unless you know what you are doing.
+ *
+ * .configure. strxfrm_multiply_czech=4
+ */
+
+#include <string.h>
+#include <sys/types.h>
+
+#include "m_ctype.h"
+#include "my_compiler.h"
+#include "my_inttypes.h"
+
+/*
+ These are four tables for four passes of the algorithm. Please see
+ below for what are the "special values"
+*/
+
+static const unsigned char literal0[] =
+ "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\002\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\043\044\045\046\047\050"
+ "\051\052\053\054\000\000\000\000\000\000\000\003\004\377\007\010\011\012"
+ "\013\015\016\017\020\022\023\024\025\026\027\031\033\034\035\036\037\040"
+ "\041\000\000\000\000\000\000\003\004\377\007\010\011\012\013\015\016\017"
+ "\020\022\023\024\025\026\027\031\033\034\035\036\037\040\041\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\003"
+ "\000\021\000\020\032\000\000\032\032\033\042\000\042\042\000\003\000\021"
+ "\000\020\032\000\000\032\032\033\042\000\042\042\027\003\003\003\003\020"
+ "\006\006\006\010\010\010\010\015\015\007\007\023\023\024\024\024\024\000"
+ "\030\034\034\034\034\040\033\000\027\003\003\003\003\020\006\006\006\010"
+ "\010\010\010\015\015\007\007\023\023\024\024\024\024\000\030\034\034\034"
+ "\034\040\033\000";
+static const unsigned char literal1[] =
+ "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\002\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\106\107\110\111\112\113"
+ "\114\115\116\117\000\000\000\000\000\000\000\003\011\377\016\021\026\027"
+ "\030\032\035\036\037\043\044\047\054\055\056\061\065\070\075\076\077\100"
+ "\102\000\000\000\000\000\000\003\011\377\016\021\026\027\030\032\035\036"
+ "\037\043\044\047\054\055\056\061\065\070\075\076\077\100\102\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\010"
+ "\000\042\000\041\063\000\000\062\064\066\104\000\103\105\000\010\000\042"
+ "\000\041\063\000\000\062\064\066\104\000\103\105\057\004\005\007\006\040"
+ "\014\015\013\022\025\024\023\033\034\017\020\046\045\050\051\053\052\000"
+ "\060\072\071\074\073\101\067\000\057\004\005\007\006\040\014\015\013\022"
+ "\025\024\023\033\034\017\020\046\045\050\051\053\052\000\060\072\071\074"
+ "\073\101\067\000";
+static const unsigned char literal2[] =
+ "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\002\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\212\213\214\215\216\217"
+ "\220\221\222\223\000\000\000\000\000\000\000\004\020\377\032\040\052\054"
+ "\056\063\071\073\075\105\107\115\127\131\133\141\151\157\171\173\175\177"
+ "\203\000\000\000\000\000\000\003\017\377\031\037\051\053\055\062\070\072"
+ "\074\104\106\114\126\130\132\140\150\156\170\172\174\176\202\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
+ "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\016"
+ "\000\103\000\101\145\000\000\143\147\153\207\000\205\211\000\015\000\102"
+ "\000\100\144\000\000\142\146\152\206\000\204\210\135\006\010\014\012\077"
+ "\026\030\024\042\050\046\044\065\067\034\036\113\111\117\121\125\123\000"
+ "\137\163\161\167\165\201\155\000\134\005\007\013\011\076\025\027\023\041"
+ "\047\045\043\064\066\033\035\112\110\116\120\124\122\000\136\162\160\166"
+ "\164\200\154\000";
+static const unsigned char literal3[] =
+ "\264\265\266\267\270\271\272\273\274\002\276\277\300\301\302\303\304\305"
+ "\306\307\310\311\312\313\314\315\316\317\320\321\322\323\002\230\232\253"
+ "\324\252\251\234\240\241\261\260\225\262\224\235\212\213\214\215\216\217"
+ "\220\221\222\223\231\226\244\257\245\227\250\004\020\377\032\040\052\054"
+ "\056\063\071\073\075\105\107\115\127\131\133\141\151\157\171\173\175\177"
+ "\203\242\237\243\254\255\233\003\017\377\031\037\051\053\055\062\070\072"
+ "\074\104\106\114\126\130\132\140\150\156\170\172\174\176\202\246\236\247"
+ "\256\325\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303"
+ "\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\326\016"
+ "\327\103\330\101\145\331\332\143\147\153\207\333\205\211\334\015\335\102"
+ "\336\100\144\337\340\142\146\152\206\341\204\210\135\006\010\014\012\077"
+ "\026\030\024\042\050\046\044\065\067\034\036\113\111\117\121\125\123\263"
+ "\137\163\161\167\165\201\155\342\134\005\007\013\011\076\025\027\023\041"
+ "\047\045\043\064\066\033\035\112\110\116\120\124\122\343\136\162\160\166"
+ "\164\200\154\344";
+
+static const unsigned char *CZ_SORT_TABLE[] = {literal0, literal1, literal2,
+ literal3};
+
+/*
+ These define the valuse for the double chars that need to be
+ sorted as they were single characters -- in Czech these are
+ 'ch', 'Ch' and 'CH'.
+*/
+
+namespace {
+
+struct wordvalue {
+ const char *word;
+ uchar *outvalue;
+};
+
+} // namespace
+
+static struct wordvalue doubles[] = {
+ {"ch", (uchar *)"\014\031\057\057"}, {"Ch", (uchar *)"\014\031\060\060"},
+ {"CH", (uchar *)"\014\031\061\061"}, {"c", (uchar *)"\005\012\021\021"},
+ {"C", (uchar *)"\005\012\022\022"},
+};
+
+/*
+ Unformal description of the algorithm:
+
+ We walk the string left to right.
+
+ The end of the string is either passed as parameter, or is
+ *p == 0. This is hidden in the IS_END macro.
+
+ In the first two passes, we compare word by word. So we make
+ first and second pass on the first word, first and second pass
+ on the second word, etc. If we come to the end of the string
+ during the first pass, we need to jump to the last word of the
+ second pass.
+
+ End of pass is marked with value 1 on the output.
+
+ For each character, we read it's value from the table.
+
+ If the value is ignore (0), we go straight to the next character.
+
+ If the value is space/end of word (2) and we are in the first
+ or second pass, we skip all characters having value 0 -- 2 and
+ switch the passwd.
+
+ If it's the compose character (255), we check if the double
+ exists behind it, find its value.
+
+ We append 0 to the end.
+---
+ Neformální popis algoritmu:
+
+ Procházíme řetězec zleva doprava.
+
+ Konec řetězce je předán buď jako parametr, nebo je to *p == 0.
+ Toto je ošetřeno makrem IS_END.
+
+ Pokud jsme došli na konec řetězce při průchodu 0, nejdeme na
+ začátek, ale na uloženou pozici, protože první a druhý průchod
+ běží současně.
+
+ Konec vstupu (průchodu) označíme na výstupu hodnotou 1.
+
+ Pro každý znak řetězce načteme hodnotu z třídící tabulky.
+
+ Jde-li o hodnotu ignorovat (0), skočíme ihned na další znak..
+
+ Jde-li o hodnotu konec slova (2) a je to průchod 0 nebo 1,
+ přeskočíme všechny další 0 -- 2 a prohodíme průchody.
+
+ Jde-li o kompozitní znak (255), otestujeme, zda následuje
+ správný do dvojice, dohledáme správnou hodnotu.
+
+ Na konci připojíme znak 0
+ */
+
+#define ADD_TO_RESULT(dest, len, totlen, value) \
+ { \
+ if ((totlen) < (len)) { \
+ dest[totlen++] = value; \
+ } \
+ }
+#define IS_END(p, src, len) (((char *)p - (char *)src) >= (len))
+
+#define NEXT_CMP_VALUE(src, p, store, pass, value, len) \
+ while (1) { \
+ if (IS_END(p, src, len)) { \
+ /* when we are at the end of string */ \
+ /* return either 0 for end of string */ \
+ /* or 1 for end of pass */ \
+ value = 0; \
+ if (pass != 3) { \
+ p = (pass++ == 0) ? store : src; \
+ value = 1; \
+ } \
+ break; \
+ } \
+ /* not at end of string */ \
+ value = CZ_SORT_TABLE[pass][*p]; \
+ if (value == 0) { \
+ p++; \
+ continue; \
+ } /* ignore value */ \
+ if (value == 2) /* space */ \
+ { \
+ const uchar *tmp; \
+ const uchar *runner = ++p; \
+ while (!(IS_END(runner, src, len)) && \
+ (CZ_SORT_TABLE[pass][*runner] == 2)) \
+ runner++; /* skip all spaces */ \
+ if (IS_END(runner, src, len)) p = runner; \
+ if ((pass <= 2) && !(IS_END(runner, src, len))) p = runner; \
+ if (IS_END(p, src, len)) continue; \
+ /* we switch passes */ \
+ if (pass > 1) break; \
+ tmp = p; \
+ pass = 1 - pass; \
+ p = store; \
+ store = tmp; \
+ break; \
+ } \
+ if (value == 255) { \
+ int i; \
+ for (i = 0; i < (int)sizeof(doubles); i++) { \
+ const char *pattern = doubles[i].word; \
+ const char *q = (const char *)p; \
+ int j = 0; \
+ while (pattern[j]) { \
+ if (IS_END(q, src, len) || (*q != pattern[j])) break; \
+ j++; \
+ q++; \
+ } \
+ if (!(pattern[j])) { \
+ value = (int)(doubles[i].outvalue[pass]); \
+ p = (const uchar *)q - 1; \
+ break; \
+ } \
+ } \
+ } \
+ p++; \
+ break; \
+ }
+
+/*
+ Function strnncoll, actually strcoll, with Czech sorting, which expect
+ the length of the strings being specified
+*/
+
+extern "C" {
+static int my_strnncoll_czech(const CHARSET_INFO *cs MY_ATTRIBUTE((unused)),
+ const uchar *s1, size_t len1, const uchar *s2,
+ size_t len2, bool s2_is_prefix) {
+ int v1, v2;
+ const uchar *p1, *p2, *store1, *store2;
+ int pass1 = 0, pass2 = 0;
+
+ if (s2_is_prefix && len1 > len2) len1 = len2;
+
+ p1 = s1;
+ p2 = s2;
+ store1 = s1;
+ store2 = s2;
+
+ do {
+ int diff;
+ NEXT_CMP_VALUE(s1, p1, store1, pass1, v1, (int)len1);
+ NEXT_CMP_VALUE(s2, p2, store2, pass2, v2, (int)len2);
+ if ((diff = v1 - v2)) return diff;
+ } while (v1);
+ return 0;
+}
+
+/*
+ TODO: Fix this one to compare strings as they are done in ctype-simple1
+*/
+
+static int my_strnncollsp_czech(const CHARSET_INFO *cs, const uchar *s,
+ size_t slen, const uchar *t, size_t tlen) {
+ for (; slen && s[slen - 1] == ' '; slen--)
+ ;
+ for (; tlen && t[tlen - 1] == ' '; tlen--)
+ ;
+ return my_strnncoll_czech(cs, s, slen, t, tlen, 0);
+}
+
+/*
+ Returns the number of bytes required for strnxfrm().
+*/
+static size_t my_strnxfrmlen_czech(
+ const CHARSET_INFO *cs MY_ATTRIBUTE((unused)), size_t len) {
+ return len * 4 + 4;
+}
+
+/*
+ Function strnxfrm, actually strxfrm, with Czech sorting, which expect
+ the length of the strings being specified
+*/
+
+static size_t my_strnxfrm_czech(const CHARSET_INFO *cs MY_ATTRIBUTE((unused)),
+ uchar *dest, size_t len,
+ uint nweights_arg MY_ATTRIBUTE((unused)),
+ const uchar *src, size_t srclen, uint flags) {
+ int value;
+ const uchar *p, *store;
+ int pass = 0;
+ size_t totlen = 0;
+ p = src;
+ store = src;
+
+ if (!(flags & 0x0F)) /* All levels by default */
+ flags |= 0x0F;
+
+ do {
+ int add = (1 << pass) & flags; /* If this level is needed */
+ NEXT_CMP_VALUE(src, p, store, pass, value, (int)srclen);
+ if (add) ADD_TO_RESULT(dest, len, totlen, value);
+ } while (value);
+ if ((flags & MY_STRXFRM_PAD_TO_MAXLEN) && len > totlen) {
+ memset(dest + totlen, ' ', len - totlen);
+ totlen = len;
+ }
+ return totlen;
+}
+} // extern "C"
+
+#undef IS_END
+
+/*
+ Neformální popis algoritmu:
+
+ procházíme řetězec zleva doprava
+ konec řetězce poznáme podle *p == 0
+ pokud jsme došli na konec řetězce při průchodu 0, nejdeme na
+ začátek, ale na uloženou pozici, protože první a druhý
+ průchod běží současně
+ konec vstupu (průchodu) označíme na výstupu hodnotou 1
+
+ načteme hodnotu z třídící tabulky
+ jde-li o hodnotu ignorovat (0), skočíme na další průchod
+ jde-li o hodnotu konec slova (2) a je to průchod 0 nebo 1,
+ přeskočíme všechny další 0 -- 2 a prohodíme
+ průchody
+ jde-li o kompozitní znak (255), otestujeme, zda následuje
+ správný do dvojice, dohledáme správnou hodnotu
+
+ na konci připojíme znak 0
+ */
+
+/*
+** Calculate min_str and max_str that ranges a LIKE string.
+** Arguments:
+** ptr Pointer to LIKE string.
+** ptr_length Length of LIKE string.
+** escape Escape character in LIKE. (Normally '\').
+** All escape characters should be removed from min_str and max_str
+** res_length Length of min_str and max_str.
+** min_str Smallest case sensitive string that ranges LIKE.
+** Should be space padded to res_length.
+** max_str Largest case sensitive string that ranges LIKE.
+** Normally padded with the biggest character sort value.
+**
+** The function should return 0 if ok and 1 if the LIKE string can't be
+** optimized !
+*/
+
+#define min_sort_char ' '
+#define max_sort_char '9'
+
+extern "C" {
+static bool my_like_range_czech(const CHARSET_INFO *cs, const char *ptr,
+ size_t ptr_length, char escape, char w_one,
+ char w_many, size_t res_length, char *min_str,
+ char *max_str, size_t *min_length,
+ size_t *max_length) {
+ uchar value;
+ const char *end = ptr + ptr_length;
+ char *min_org = min_str;
+ char *min_end = min_str + res_length;
+
+ for (; ptr != end && min_str != min_end; ptr++) {
+ if (*ptr == w_one) /* '_' in SQL */
+ {
+ break;
+ }
+ if (*ptr == w_many) /* '%' in SQL */
+ {
+ break;
+ }
+
+ if (*ptr == escape && ptr + 1 != end) {
+ ptr++;
+ } /* Skip escape */
+
+ value = CZ_SORT_TABLE[0][(int)(uchar)*ptr];
+
+ if (value == 0) /* Ignore in the first pass */
+ {
+ continue;
+ }
+ if (value <= 2) /* End of pass or end of string */
+ {
+ break;
+ }
+ if (value == 255) /* Double char too compicated */
+ {
+ break;
+ }
+
+ *min_str++ = *max_str++ = *ptr;
+ }
+
+ if (cs->state & MY_CS_BINSORT)
+ *min_length = (size_t)(min_str - min_org);
+ else {
+ /* 'a\0\0... is the smallest possible string */
+ *min_length = res_length;
+ }
+ /* a\ff\ff... is the biggest possible string */
+ *max_length = res_length;
+
+ while (min_str != min_end) {
+ *min_str++ = min_sort_char; /* Because of key compression */
+ *max_str++ = max_sort_char;
+ }
+ return 0;
+}
+} // extern "C"
+
+/*
+ * File generated by cset
+ * (C) Abandoned 1997 Zarko Mocnik <zarko.mocnik@dem.si>
+ *
+ * definition table reworked by Jaromir Dolecek <dolecek@ics.muni.cz>
+ */
+
+static const uchar ctype_czech[257] = {
+ 0, 32, 32, 32, 32, 32, 32, 32, 32, 32, 40, 40, 40, 40, 40, 32,
+ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
+ 32, 72, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+ 16, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 16, 16, 16, 16, 16,
+ 16, 16, 129, 129, 129, 129, 129, 129, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 16, 16, 16, 16,
+ 16, 16, 130, 130, 130, 130, 130, 130, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 16, 16, 16, 16,
+ 32, 32, 32, 32, 32, 32, 32, 32, 32, 40, 40, 40, 40, 40, 32, 32,
+ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
+ 72, 1, 16, 1, 16, 1, 1, 16, 0, 0, 1, 1, 1, 1, 16, 1,
+ 1, 16, 2, 16, 2, 16, 2, 2, 16, 16, 2, 2, 2, 2, 16, 2,
+ 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 16, 1, 1, 1, 1, 1, 1, 16, 1, 1, 1, 1, 1, 1, 1,
+ 16, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 16, 2, 2, 2, 2, 2, 2, 2,
+ 16,
+};
+
+static const uchar to_lower_czech[] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
+ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
+ 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
+ 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
+ 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
+ 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
+ 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
+ 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
+ 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 177, 161, 179, 163, 181,
+ 182, 166, 167, 168, 185, 186, 187, 188, 173, 190, 191, 176, 177, 178, 179,
+ 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 224, 225, 226,
+ 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 208, 241,
+ 242, 243, 244, 245, 246, 215, 248, 249, 250, 251, 252, 253, 254, 223, 224,
+ 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
+ 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254,
+ 255,
+};
+
+static const uchar to_upper_czech[] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
+ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
+ 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
+ 90, 91, 92, 93, 94, 95, 96, 65, 66, 67, 68, 69, 70, 71, 72,
+ 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
+ 88, 89, 90, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
+ 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
+ 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
+ 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 160, 178, 162,
+ 180, 164, 165, 183, 184, 169, 170, 171, 172, 189, 174, 175, 192, 193, 194,
+ 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
+ 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 192,
+ 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
+ 240, 209, 210, 211, 212, 213, 214, 247, 216, 217, 218, 219, 220, 221, 222,
+ 255,
+};
+
+static const uchar sort_order_czech[] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
+ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
+ 60, 61, 62, 63, 64, 65, 71, 72, 76, 78, 83, 84, 85, 86, 90,
+ 91, 92, 96, 97, 100, 105, 106, 107, 110, 114, 117, 122, 123, 124, 125,
+ 127, 131, 132, 133, 134, 135, 136, 65, 71, 72, 76, 78, 83, 84, 85,
+ 86, 90, 91, 92, 96, 97, 100, 105, 106, 107, 110, 114, 117, 122, 123,
+ 124, 125, 127, 137, 138, 139, 140, 0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
+ 23, 24, 25, 26, 27, 28, 29, 30, 31, 255, 66, 255, 93, 255, 94,
+ 111, 255, 255, 255, 112, 113, 115, 128, 255, 129, 130, 255, 66, 255, 93,
+ 255, 94, 111, 255, 255, 112, 113, 115, 128, 255, 129, 130, 108, 67, 68,
+ 69, 70, 95, 73, 75, 74, 79, 81, 82, 80, 89, 87, 77, 255, 98,
+ 99, 101, 102, 103, 104, 255, 109, 119, 118, 120, 121, 126, 116, 255, 108,
+ 67, 68, 69, 70, 95, 73, 75, 74, 79, 81, 82, 80, 89, 88, 77,
+ 255, 98, 99, 101, 102, 103, 104, 255, 109, 119, 118, 120, 121, 126, 116,
+ 255,
+};
+
+static uint16 tab_8859_2_uni[256] = {
+ 0, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008,
+ 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x0010, 0x0011,
+ 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A,
+ 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, 0x0020, 0x0021, 0x0022, 0x0023,
+ 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C,
+ 0x002D, 0x002E, 0x002F, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
+ 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E,
+ 0x003F, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
+ 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050,
+ 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,
+ 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, 0x0060, 0x0061, 0x0062,
+ 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B,
+ 0x006C, 0x006D, 0x006E, 0x006F, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074,
+ 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D,
+ 0x007E, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0x00A0, 0x0104,
+ 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, 0x00A8, 0x0160, 0x015E,
+ 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, 0x00B0, 0x0105, 0x02DB, 0x0142,
+ 0x00B4, 0x013E, 0x015B, 0x02C7, 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A,
+ 0x02DD, 0x017E, 0x017C, 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139,
+ 0x0106, 0x00C7, 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE,
+ 0x010E, 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7,
+ 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155,
+ 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D, 0x00E9,
+ 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, 0x0111, 0x0144, 0x0148,
+ 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F, 0x00FA, 0x0171,
+ 0x00FC, 0x00FD, 0x0163, 0x02D9};
+
+/* 0000-00FD , 254 chars */
+static const uchar tab_uni_8859_2_plane00[] = {
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
+ 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+ 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
+ 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
+ 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
+ 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53,
+ 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,
+ 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
+ 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0xA0, 0x00, 0x00, 0x00, 0xA4, 0x00, 0x00, 0xA7,
+ 0xA8, 0x00, 0x00, 0x00, 0x00, 0xAD, 0x00, 0x00, 0xB0, 0x00, 0x00, 0x00,
+ 0xB4, 0x00, 0x00, 0x00, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0xC1, 0xC2, 0x00, 0xC4, 0x00, 0x00, 0xC7, 0x00, 0xC9, 0x00, 0xCB,
+ 0x00, 0xCD, 0xCE, 0x00, 0x00, 0x00, 0x00, 0xD3, 0xD4, 0x00, 0xD6, 0xD7,
+ 0x00, 0x00, 0xDA, 0x00, 0xDC, 0xDD, 0x00, 0xDF, 0x00, 0xE1, 0xE2, 0x00,
+ 0xE4, 0x00, 0x00, 0xE7, 0x00, 0xE9, 0x00, 0xEB, 0x00, 0xED, 0xEE, 0x00,
+ 0x00, 0x00, 0x00, 0xF3, 0xF4, 0x00, 0xF6, 0xF7, 0x00, 0x00, 0xFA, 0x00,
+ 0xFC, 0xFD};
+
+/* 0102-017E , 125 chars */
+static const uchar tab_uni_8859_2_plane01[] = {
+ 0xC3, 0xE3, 0xA1, 0xB1, 0xC6, 0xE6, 0x00, 0x00, 0x00, 0x00, 0xC8, 0xE8,
+ 0xCF, 0xEF, 0xD0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCA, 0xEA,
+ 0xCC, 0xEC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC5, 0xE5, 0x00, 0x00, 0xA5,
+ 0xB5, 0x00, 0x00, 0xA3, 0xB3, 0xD1, 0xF1, 0x00, 0x00, 0xD2, 0xF2, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD5, 0xF5, 0x00, 0x00, 0xC0, 0xE0,
+ 0x00, 0x00, 0xD8, 0xF8, 0xA6, 0xB6, 0x00, 0x00, 0xAA, 0xBA, 0xA9, 0xB9,
+ 0xDE, 0xFE, 0xAB, 0xBB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xD9, 0xF9, 0xDB, 0xFB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAC,
+ 0xBC, 0xAF, 0xBF, 0xAE, 0xBE};
+
+/* 02C7-02DD , 23 chars */
+static const uchar tab_uni_8859_2_plane02[] = {
+ 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0xA2, 0xFF, 0x00, 0xB2, 0x00, 0xBD};
+
+static MY_UNI_IDX idx_uni_8859_2[] = {{0x0000, 0x00FD, tab_uni_8859_2_plane00},
+ {0x0102, 0x017E, tab_uni_8859_2_plane01},
+ {0x02C7, 0x02DD, tab_uni_8859_2_plane02},
+ {0, 0, NULL}};
+
+static MY_COLLATION_HANDLER my_collation_latin2_czech_ci_handler = {
+ nullptr, /* init */
+ nullptr,
+ my_strnncoll_czech,
+ my_strnncollsp_czech,
+ my_strnxfrm_czech,
+ my_strnxfrmlen_czech,
+ my_like_range_czech,
+ my_wildcmp_bin,
+ my_strcasecmp_8bit,
+ my_instr_simple,
+ my_hash_sort_simple,
+ my_propagate_simple};
+
+CHARSET_INFO my_charset_latin2_czech_ci = {
+ 2,
+ 0,
+ 0, /* number */
+ MY_CS_COMPILED | MY_CS_STRNXFRM | MY_CS_CSSORT, /* state */
+ "latin2", /* cs name */
+ "latin2_czech_cs", /* name */
+ "", /* comment */
+ NULL, /* tailoring */
+ NULL, /* coll_param */
+ ctype_czech,
+ to_lower_czech,
+ to_upper_czech,
+ sort_order_czech,
+ NULL, /* uca */
+ tab_8859_2_uni, /* tab_to_uni */
+ idx_uni_8859_2, /* tab_from_uni */
+ &my_unicase_default, /* caseinfo */
+ NULL, /* state_map */
+ NULL, /* ident_map */
+ 4, /* strxfrm_multiply */
+ 1, /* caseup_multiply */
+ 1, /* casedn_multiply */
+ 1, /* mbminlen */
+ 1, /* mbmaxlen */
+ 1, /* mbmaxlenlen */
+ 0, /* min_sort_char */
+ 0, /* max_sort_char */
+ ' ', /* pad char */
+ 0, /* escape_with_backslash_is_dangerous */
+ 4, /* levels_for_compare */
+ &my_charset_8bit_handler,
+ &my_collation_latin2_czech_ci_handler,
+ PAD_SPACE};
diff --git a/libmysqlclient/mysql/strings/ctype-czech.cc.orig b/libmysqlclient/mysql/strings/ctype-czech.cc.orig
new file mode 120000
index 0000000..0103cf9
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-czech.cc.orig
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-czech.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-euc_kr.cc b/libmysqlclient/mysql/strings/ctype-euc_kr.cc
new file mode 120000
index 0000000..58db6b2
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-euc_kr.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-euc_kr.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-eucjpms.cc b/libmysqlclient/mysql/strings/ctype-eucjpms.cc
new file mode 120000
index 0000000..70a1fb9
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-eucjpms.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-eucjpms.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-extra.cc b/libmysqlclient/mysql/strings/ctype-extra.cc
new file mode 120000
index 0000000..427be70
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-extra.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-extra.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-gb18030.cc b/libmysqlclient/mysql/strings/ctype-gb18030.cc
new file mode 120000
index 0000000..600a5f1
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-gb18030.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-gb18030.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-gb2312.cc b/libmysqlclient/mysql/strings/ctype-gb2312.cc
new file mode 120000
index 0000000..33252d0
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-gb2312.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-gb2312.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-gbk.cc b/libmysqlclient/mysql/strings/ctype-gbk.cc
new file mode 120000
index 0000000..5fd3798
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-gbk.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-gbk.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-latin1.cc b/libmysqlclient/mysql/strings/ctype-latin1.cc
new file mode 120000
index 0000000..d60fba2
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-latin1.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-latin1.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-mb.cc b/libmysqlclient/mysql/strings/ctype-mb.cc
new file mode 120000
index 0000000..85191b0
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-mb.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-mb.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-simple.cc b/libmysqlclient/mysql/strings/ctype-simple.cc
new file mode 120000
index 0000000..dbed3d9
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-simple.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-simple.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-sjis.cc b/libmysqlclient/mysql/strings/ctype-sjis.cc
new file mode 120000
index 0000000..2de1db9
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-sjis.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-sjis.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-tis620.cc b/libmysqlclient/mysql/strings/ctype-tis620.cc
new file mode 120000
index 0000000..9e8432a
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-tis620.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-tis620.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-uca.cc b/libmysqlclient/mysql/strings/ctype-uca.cc
new file mode 120000
index 0000000..13e98fc
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-uca.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-uca.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-ucs2.cc b/libmysqlclient/mysql/strings/ctype-ucs2.cc
new file mode 120000
index 0000000..6d77535
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-ucs2.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-ucs2.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-ujis.cc b/libmysqlclient/mysql/strings/ctype-ujis.cc
new file mode 120000
index 0000000..5bb5658
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-ujis.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-ujis.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-utf8.cc b/libmysqlclient/mysql/strings/ctype-utf8.cc
new file mode 120000
index 0000000..1701dd5
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-utf8.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-utf8.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype-win1250ch.cc b/libmysqlclient/mysql/strings/ctype-win1250ch.cc
new file mode 120000
index 0000000..756e33e
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype-win1250ch.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype-win1250ch.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/ctype.cc b/libmysqlclient/mysql/strings/ctype.cc
new file mode 120000
index 0000000..92ee673
--- /dev/null
+++ b/libmysqlclient/mysql/strings/ctype.cc
@@ -0,0 +1 @@
+../../../upstream/strings/ctype.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/decimal.cc b/libmysqlclient/mysql/strings/decimal.cc
new file mode 100644
index 0000000..05a8503
--- /dev/null
+++ b/libmysqlclient/mysql/strings/decimal.cc
@@ -0,0 +1,2482 @@
+/* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License, version 2.0,
+ as published by the Free Software Foundation.
+
+ This program is also distributed with certain software (including
+ but not limited to OpenSSL) that is licensed under separate terms,
+ as designated in a particular file or component or in included license
+ documentation. The authors of MySQL hereby grant you an additional
+ permission to link the program and your derivative works with the
+ separately licensed software that they have included with MySQL.
+
+ Without limiting anything contained in the foregoing, this file,
+ which is part of C Driver for MySQL (Connector/C), is also subject to the
+ Universal FOSS Exception, version 1.0, a copy of which can be found at
+ http://oss.oracle.com/licenses/universal-foss-exception.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License, version 2.0, for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+
+/*
+=======================================================================
+ NOTE: this library implements SQL standard "exact numeric" type
+ and is not at all generic, but rather intentinally crippled to
+ follow the standard :)
+=======================================================================
+ Quoting the standard
+ (SQL:2003, Part 2 Foundations, aka ISO/IEC 9075-2:2003)
+
+4.4.2 Characteristics of numbers, page 27:
+
+ An exact numeric type has a precision P and a scale S. P is a positive
+ integer that determines the number of significant digits in a
+ particular radix R, where R is either 2 or 10. S is a non-negative
+ integer. Every value of an exact numeric type of scale S is of the
+ form n*10^{-S}, where n is an integer such that -R^P <= n <= R^P.
+
+ [...]
+
+ If an assignment of some number would result in a loss of its most
+ significant digit, an exception condition is raised. If least
+ significant digits are lost, implementation-defined rounding or
+ truncating occurs, with no exception condition being raised.
+
+ [...]
+
+ Whenever an exact or approximate numeric value is assigned to an exact
+ numeric value site, an approximation of its value that preserves
+ leading significant digits after rounding or truncating is represented
+ in the declared type of the target. The value is converted to have the
+ precision and scale of the target. The choice of whether to truncate
+ or round is implementation-defined.
+
+ [...]
+
+ All numeric values between the smallest and the largest value,
+ inclusive, in a given exact numeric type have an approximation
+ obtained by rounding or truncation for that type; it is
+ implementation-defined which other numeric values have such
+ approximations.
+
+5.3 <literal>, page 143
+
+ <exact numeric literal> ::=
+ <unsigned integer> [ <period> [ <unsigned integer> ] ]
+ | <period> <unsigned integer>
+
+6.1 <data type>, page 165:
+
+ 19) The <scale> of an <exact numeric type> shall not be greater than
+ the <precision> of the <exact numeric type>.
+
+ 20) For the <exact numeric type>s DECIMAL and NUMERIC:
+
+ a) The maximum value of <precision> is implementation-defined.
+ <precision> shall not be greater than this value.
+ b) The maximum value of <scale> is implementation-defined. <scale>
+ shall not be greater than this maximum value.
+
+ 21) NUMERIC specifies the data type exact numeric, with the decimal
+ precision and scale specified by the <precision> and <scale>.
+
+ 22) DECIMAL specifies the data type exact numeric, with the decimal
+ scale specified by the <scale> and the implementation-defined
+ decimal precision equal to or greater than the value of the
+ specified <precision>.
+
+6.26 <numeric value expression>, page 241:
+
+ 1) If the declared type of both operands of a dyadic arithmetic
+ operator is exact numeric, then the declared type of the result is
+ an implementation-defined exact numeric type, with precision and
+ scale determined as follows:
+
+ a) Let S1 and S2 be the scale of the first and second operands
+ respectively.
+ b) The precision of the result of addition and subtraction is
+ implementation-defined, and the scale is the maximum of S1 and S2.
+ c) The precision of the result of multiplication is
+ implementation-defined, and the scale is S1 + S2.
+ d) The precision and scale of the result of division are
+ implementation-defined.
+*/
+
+#include "decimal.h"
+
+#include <limits.h>
+#include <math.h>
+#include <string.h>
+#include <algorithm>
+
+#include "m_ctype.h"
+#include "m_string.h"
+#include "my_byteorder.h"
+#include "my_compiler.h"
+#include "my_dbug.h"
+#include "my_sys.h" /* for my_alloca */
+#include "myisampack.h"
+
+/*
+ Internally decimal numbers are stored base 10^9 (see DIG_BASE below)
+ So one variable of type decimal_digit_t is limited:
+
+ 0 < decimal_digit <= DIG_MAX < DIG_BASE
+
+ in the decimal_t:
+
+ intg is the number of *decimal* digits (NOT number of decimal_digit_t's !)
+ before the point
+ frac - number of decimal digits after the point
+ buf is an array of decimal_digit_t's
+ len is the length of buf (length of allocated space) in decimal_digit_t's,
+ not in bytes
+*/
+typedef decimal_digit_t dec1;
+typedef longlong dec2;
+
+#define DIG_PER_DEC1 9
+#define DIG_MASK 100000000
+#define DIG_BASE 1000000000
+#define DIG_MAX (DIG_BASE - 1)
+#define ROUND_UP(X) (((X) + DIG_PER_DEC1 - 1) / DIG_PER_DEC1)
+static const dec1 powers10[DIG_PER_DEC1 + 1] = {
+ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
+static const int dig2bytes[DIG_PER_DEC1 + 1] = {0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
+static const dec1 frac_max[DIG_PER_DEC1 - 1] = {900000000, 990000000, 999000000,
+ 999900000, 999990000, 999999000,
+ 999999900, 999999990};
+
+static inline dec1 div_by_pow10(dec1 x, int p) {
+ /*
+ GCC can optimize division by a constant to a multiplication and some
+ shifts, which is faster than dividing by a variable, even taking into
+ account the extra cost of the switch. It is also (empirically on a Skylake)
+ faster than storing the magic multiplier constants in a table and doing it
+ ourselves. However, since the code is much bigger, we only use this in
+ a few select places.
+
+ Note the use of unsigned, which is faster for this specific operation.
+ */
+ DBUG_ASSERT(x >= 0);
+ switch (p) {
+ case 0:
+ return static_cast<uint32_t>(x) / 1;
+ case 1:
+ return static_cast<uint32_t>(x) / 10;
+ case 2:
+ return static_cast<uint32_t>(x) / 100;
+ case 3:
+ return static_cast<uint32_t>(x) / 1000;
+ case 4:
+ return static_cast<uint32_t>(x) / 10000;
+ case 5:
+ return static_cast<uint32_t>(x) / 100000;
+ case 6:
+ return static_cast<uint32_t>(x) / 1000000;
+ case 7:
+ return static_cast<uint32_t>(x) / 10000000;
+ case 8:
+ return static_cast<uint32_t>(x) / 100000000;
+ default:
+ DBUG_ASSERT(false);
+ return x / powers10[p];
+ }
+}
+
+static inline dec1 mod_by_pow10(dec1 x, int p) {
+ // See div_by_pow10 for rationale.
+ DBUG_ASSERT(x >= 0);
+ switch (p) {
+ case 1:
+ return static_cast<uint32_t>(x) % 10;
+ case 2:
+ return static_cast<uint32_t>(x) % 100;
+ case 3:
+ return static_cast<uint32_t>(x) % 1000;
+ case 4:
+ return static_cast<uint32_t>(x) % 10000;
+ case 5:
+ return static_cast<uint32_t>(x) % 100000;
+ case 6:
+ return static_cast<uint32_t>(x) % 1000000;
+ case 7:
+ return static_cast<uint32_t>(x) % 10000000;
+ case 8:
+ return static_cast<uint32_t>(x) % 100000000;
+ default:
+ DBUG_ASSERT(false);
+ return x % powers10[p];
+ }
+}
+
+#define sanity(d) DBUG_ASSERT((d)->len > 0)
+
+#define FIX_INTG_FRAC_ERROR(len, intg1, frac1, error) \
+ do { \
+ if (unlikely(intg1 + frac1 > (len))) { \
+ if (unlikely(intg1 > (len))) { \
+ intg1 = (len); \
+ frac1 = 0; \
+ error = E_DEC_OVERFLOW; \
+ } else { \
+ frac1 = (len)-intg1; \
+ error = E_DEC_TRUNCATED; \
+ } \
+ } else \
+ error = E_DEC_OK; \
+ } while (0)
+
+#define ADD(to, from1, from2, carry) /* assume carry <= 1 */ \
+ do { \
+ dec1 a = (from1) + (from2) + (carry); \
+ DBUG_ASSERT((carry) <= 1); \
+ if (((carry) = a >= DIG_BASE)) /* no division here! */ \
+ a -= DIG_BASE; \
+ (to) = a; \
+ } while (0)
+
+#define ADD2(to, from1, from2, carry) \
+ do { \
+ dec2 a = ((dec2)(from1)) + (from2) + (carry); \
+ if (((carry) = a >= DIG_BASE)) a -= DIG_BASE; \
+ if (unlikely(a >= DIG_BASE)) { \
+ a -= DIG_BASE; \
+ carry++; \
+ } \
+ (to) = (dec1)a; \
+ } while (0)
+
+#define SUB(to, from1, from2, carry) /* to=from1-from2 */ \
+ do { \
+ dec1 a = (from1) - (from2) - (carry); \
+ if (((carry) = a < 0)) a += DIG_BASE; \
+ (to) = a; \
+ } while (0)
+
+#define SUB2(to, from1, from2, carry) /* to=from1-from2 */ \
+ do { \
+ dec1 a = (from1) - (from2) - (carry); \
+ if (((carry) = a < 0)) a += DIG_BASE; \
+ if (unlikely(a < 0)) { \
+ a += DIG_BASE; \
+ carry++; \
+ } \
+ (to) = a; \
+ } while (0)
+
+ALWAYS_INLINE static int decimal_bin_size_inline(int precision, int scale);
+
+/*
+ This is a direct loop unrolling of code that used to look like this:
+ for (; *buf_beg < powers10[i--]; start++) ;
+
+ @param i start index
+ @param val value to compare against list of powers of 10
+
+ @retval Number of leading zeroes that can be removed from fraction.
+
+ @note Why unroll? To get rid of lots of compiler warnings [-Warray-bounds]
+ Nice bonus: unrolled code is significantly faster.
+ */
+static inline int count_leading_zeroes(int i, dec1 val) {
+ int ret = 0;
+ switch (i) {
+ /* @note Intentional fallthrough in all case labels */
+ case 9:
+ if (val >= 1000000000) break;
+ ++ret; // Fall through.
+ case 8:
+ if (val >= 100000000) break;
+ ++ret; // Fall through.
+ case 7:
+ if (val >= 10000000) break;
+ ++ret; // Fall through.
+ case 6:
+ if (val >= 1000000) break;
+ ++ret; // Fall through.
+ case 5:
+ if (val >= 100000) break;
+ ++ret; // Fall through.
+ case 4:
+ if (val >= 10000) break;
+ ++ret; // Fall through.
+ case 3:
+ if (val >= 1000) break;
+ ++ret; // Fall through.
+ case 2:
+ if (val >= 100) break;
+ ++ret; // Fall through.
+ case 1:
+ if (val >= 10) break;
+ ++ret; // Fall through.
+ case 0:
+ if (val >= 1) break;
+ ++ret; // Fall through.
+ default: { DBUG_ASSERT(false); }
+ }
+ return ret;
+}
+
+/*
+ This is a direct loop unrolling of code that used to look like this:
+ for (; *buf_end % powers10[i++] == 0; stop--) ;
+
+ @param i start index
+ @param val value to compare against list of powers of 10
+
+ @retval Number of trailing zeroes that can be removed from fraction.
+
+ @note Why unroll? To get rid of lots of compiler warnings [-Warray-bounds]
+ Nice bonus: unrolled code is significantly faster.
+ */
+static inline int count_trailing_zeroes(int i, dec1 val) {
+ DBUG_ASSERT(val >= 0);
+ uint32_t uval = val;
+
+ int ret = 0;
+ switch (i) {
+ /* @note Intentional fallthrough in all case labels */
+ case 0:
+ if ((uval % 1) != 0) break;
+ ++ret; // Fall through.
+ case 1:
+ if ((uval % 10) != 0) break;
+ ++ret; // Fall through.
+ case 2:
+ if ((uval % 100) != 0) break;
+ ++ret; // Fall through.
+ case 3:
+ if ((uval % 1000) != 0) break;
+ ++ret; // Fall through.
+ case 4:
+ if ((uval % 10000) != 0) break;
+ ++ret; // Fall through.
+ case 5:
+ if ((uval % 100000) != 0) break;
+ ++ret; // Fall through.
+ case 6:
+ if ((uval % 1000000) != 0) break;
+ ++ret; // Fall through.
+ case 7:
+ if ((uval % 10000000) != 0) break;
+ ++ret; // Fall through.
+ case 8:
+ if ((uval % 100000000) != 0) break;
+ ++ret; // Fall through.
+ case 9:
+ if ((uval % 1000000000) != 0) break;
+ ++ret; // Fall through.
+ default: { DBUG_ASSERT(false); }
+ }
+ return ret;
+}
+
+/*
+ Get maximum value for given precision and scale
+
+ SYNOPSIS
+ max_decimal()
+ precision/scale - see decimal_bin_size() below
+ to - decimal where where the result will be stored
+ to->buf and to->len must be set.
+*/
+
+void max_decimal(int precision, int frac, decimal_t *to) {
+ int intpart;
+ dec1 *buf = to->buf;
+ DBUG_ASSERT(precision && precision >= frac);
+
+ to->sign = 0;
+ if ((intpart = to->intg = (precision - frac))) {
+ int firstdigits = intpart % DIG_PER_DEC1;
+ if (firstdigits) *buf++ = powers10[firstdigits] - 1; /* get 9 99 999 ... */
+ for (intpart /= DIG_PER_DEC1; intpart; intpart--) *buf++ = DIG_MAX;
+ }
+
+ if ((to->frac = frac)) {
+ int lastdigits = frac % DIG_PER_DEC1;
+ for (frac /= DIG_PER_DEC1; frac; frac--) *buf++ = DIG_MAX;
+ if (lastdigits) *buf = frac_max[lastdigits - 1];
+ }
+}
+
+static inline dec1 *remove_leading_zeroes(const decimal_t *from,
+ int *intg_result) {
+ int intg = from->intg, i;
+ dec1 *buf0 = from->buf;
+ i = ((intg - 1) % DIG_PER_DEC1) + 1;
+ while (intg > 0 && *buf0 == 0) {
+ intg -= i;
+ i = DIG_PER_DEC1;
+ buf0++;
+ }
+ if (intg > 0) {
+ intg -= count_leading_zeroes((intg - 1) % DIG_PER_DEC1, *buf0);
+ DBUG_ASSERT(intg > 0);
+ } else
+ intg = 0;
+ *intg_result = intg;
+ return buf0;
+}
+
+/*
+ Count actual length of fraction part (without ending zeroes)
+
+ SYNOPSIS
+ decimal_actual_fraction()
+ from number for processing
+*/
+
+int decimal_actual_fraction(decimal_t *from) {
+ int frac = from->frac, i;
+ dec1 *buf0 = from->buf + ROUND_UP(from->intg) + ROUND_UP(frac) - 1;
+
+ if (frac == 0) return 0;
+
+ i = ((frac - 1) % DIG_PER_DEC1 + 1);
+ while (frac > 0 && *buf0 == 0) {
+ frac -= i;
+ i = DIG_PER_DEC1;
+ buf0--;
+ }
+ if (frac > 0) {
+ frac -= count_trailing_zeroes(DIG_PER_DEC1 - ((frac - 1) % DIG_PER_DEC1),
+ *buf0);
+ }
+ return frac;
+}
+
+/*
+ Convert decimal to its printable string representation
+
+ SYNOPSIS
+ decimal2string()
+ from - value to convert
+ to - points to buffer where string representation
+ should be stored
+ *to_len - in: size of to buffer (incl. terminating '\0')
+ out: length of the actually written string (excl. '\0')
+ fixed_precision - 0 if representation can be variable length and
+ fixed_decimals will not be checked in this case.
+ Put number as with fixed point position with this
+ number of digits (sign counted and decimal point is
+ counted)
+ fixed_decimals - number digits after point.
+ filler - character to fill gaps in case of fixed_precision > 0
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
+*/
+
+int decimal2string(const decimal_t *from, char *to, int *to_len,
+ int fixed_precision, int fixed_decimals, char filler) {
+ /* {intg_len, frac_len} output widths; {intg, frac} places in input */
+ int len, intg, frac = from->frac, i, intg_len, frac_len, fill;
+ /* number digits before decimal point */
+ int fixed_intg = (fixed_precision ? (fixed_precision - fixed_decimals) : 0);
+ int error = E_DEC_OK;
+ char *s = to;
+ dec1 *buf, *buf0 = from->buf, tmp;
+
+ DBUG_ASSERT(*to_len >= 2 + from->sign);
+
+ /* removing leading zeroes */
+ buf0 = remove_leading_zeroes(from, &intg);
+ if (unlikely(intg + frac == 0)) {
+ intg = 1;
+ tmp = 0;
+ buf0 = &tmp;
+ }
+
+ if (!(intg_len = fixed_precision ? fixed_intg : intg)) intg_len = 1;
+ frac_len = fixed_precision ? fixed_decimals : frac;
+ len = from->sign + intg_len + MY_TEST(frac) + frac_len;
+ if (fixed_precision) {
+ if (frac > fixed_decimals) {
+ error = E_DEC_TRUNCATED;
+ frac = fixed_decimals;
+ }
+ if (intg > fixed_intg) {
+ error = E_DEC_OVERFLOW;
+ intg = fixed_intg;
+ }
+ } else if (unlikely(len > --*to_len)) /* reserve one byte for \0 */
+ {
+ int j = len - *to_len; /* excess printable chars */
+ error = (frac && j <= frac + 1) ? E_DEC_TRUNCATED : E_DEC_OVERFLOW;
+
+ /*
+ If we need to cut more places than frac is wide, we'll end up
+ dropping the decimal point as well. Account for this.
+ */
+ if (frac && j >= frac + 1) j--;
+
+ if (j > frac) {
+ intg_len = intg -= j - frac;
+ frac = 0;
+ } else
+ frac -= j;
+ frac_len = frac;
+ len = from->sign + intg_len + MY_TEST(frac) + frac_len;
+ }
+ *to_len = len;
+ s[len] = 0;
+
+ if (from->sign) *s++ = '-';
+
+ if (frac) {
+ char *s1 = s + intg_len;
+ fill = frac_len - frac;
+ buf = buf0 + ROUND_UP(intg);
+ *s1++ = '.';
+ for (; frac > 0; frac -= DIG_PER_DEC1) {
+ dec1 x = *buf++;
+ for (i = MY_MIN(frac, DIG_PER_DEC1); i; i--) {
+ dec1 y = x / DIG_MASK;
+ *s1++ = '0' + (uchar)y;
+ x -= y * DIG_MASK;
+ x *= 10;
+ }
+ }
+ for (; fill > 0; fill--) *s1++ = filler;
+ }
+
+ fill = intg_len - intg;
+ if (intg == 0) fill--; /* symbol 0 before digital point */
+ for (; fill > 0; fill--) *s++ = filler;
+ if (intg) {
+ s += intg;
+ for (buf = buf0 + ROUND_UP(intg); intg > 0; intg -= DIG_PER_DEC1) {
+ dec1 x = *--buf;
+ for (i = MY_MIN(intg, DIG_PER_DEC1); i; i--) {
+ dec1 y = x / 10;
+ *--s = '0' + (uchar)(x - y * 10);
+ x = y;
+ }
+ }
+ } else
+ *s = '0';
+
+ return error;
+}
+
+/*
+ Return bounds of decimal digits in the number
+
+ SYNOPSIS
+ digits_bounds()
+ from - decimal number for processing
+ start_result - index (from 0 ) of first decimal digits will
+ be written by this address
+ end_result - index of position just after last decimal digit
+ be written by this address
+*/
+
+static void digits_bounds(decimal_t *from, int *start_result, int *end_result) {
+ int start, stop, i;
+ dec1 *buf_beg = from->buf;
+ dec1 *end = from->buf + ROUND_UP(from->intg) + ROUND_UP(from->frac);
+ dec1 *buf_end = end - 1;
+
+ /* find non-zero digit from number begining */
+ while (buf_beg < end && *buf_beg == 0) buf_beg++;
+
+ if (buf_beg >= end) {
+ /* it is zero */
+ *start_result = *end_result = 0;
+ return;
+ }
+
+ /* find non-zero decimal digit from number begining */
+ if (buf_beg == from->buf && from->intg) {
+ start = DIG_PER_DEC1 - (i = ((from->intg - 1) % DIG_PER_DEC1 + 1));
+ i--;
+ } else {
+ i = DIG_PER_DEC1 - 1;
+ start = (int)((buf_beg - from->buf) * DIG_PER_DEC1);
+ }
+ if (buf_beg < end) start += count_leading_zeroes(i, *buf_beg);
+
+ *start_result = start; /* index of first decimal digit (from 0) */
+
+ /* find non-zero digit at the end */
+ while (buf_end > buf_beg && *buf_end == 0) buf_end--;
+ /* find non-zero decimal digit from the end */
+ if (buf_end == end - 1 && from->frac) {
+ stop = (int)(((buf_end - from->buf) * DIG_PER_DEC1 +
+ (i = ((from->frac - 1) % DIG_PER_DEC1 + 1))));
+ i = DIG_PER_DEC1 - i + 1;
+ } else {
+ stop = (int)((buf_end - from->buf + 1) * DIG_PER_DEC1);
+ i = 1;
+ }
+ stop -= count_trailing_zeroes(i, *buf_end);
+ *end_result = stop; /* index of position after last decimal digit (from 0) */
+}
+
+/*
+ Left shift for alignment of data in buffer
+
+ SYNOPSIS
+ do_mini_left_shift()
+ dec pointer to decimal number which have to be shifted
+ shift number of decimal digits on which it should be shifted
+ beg/end bounds of decimal digits (see digits_bounds())
+
+ NOTE
+ Result fitting in the buffer should be garanted.
+ 'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
+*/
+
+static void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last) {
+ dec1 *from = dec->buf + ROUND_UP(beg + 1) - 1;
+ dec1 *end = dec->buf + ROUND_UP(last) - 1;
+ int c_shift = DIG_PER_DEC1 - shift;
+ DBUG_ASSERT(from >= dec->buf);
+ DBUG_ASSERT(end < dec->buf + dec->len);
+ if (beg % DIG_PER_DEC1 < shift) *(from - 1) = (*from) / powers10[c_shift];
+ for (; from < end; from++)
+ *from = ((*from % powers10[c_shift]) * powers10[shift] +
+ (*(from + 1)) / powers10[c_shift]);
+ *from = (*from % powers10[c_shift]) * powers10[shift];
+}
+
+/*
+ Right shift for alignment of data in buffer
+
+ SYNOPSIS
+ do_mini_left_shift()
+ dec pointer to decimal number which have to be shifted
+ shift number of decimal digits on which it should be shifted
+ beg/end bounds of decimal digits (see digits_bounds())
+
+ NOTE
+ Result fitting in the buffer should be garanted.
+ 'shift' have to be from 1 to DIG_PER_DEC1-1 (inclusive)
+*/
+
+static void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last) {
+ dec1 *from = dec->buf + ROUND_UP(last) - 1;
+ dec1 *end = dec->buf + ROUND_UP(beg + 1) - 1;
+ int c_shift = DIG_PER_DEC1 - shift;
+ DBUG_ASSERT(from < dec->buf + dec->len);
+ DBUG_ASSERT(end >= dec->buf);
+ if (DIG_PER_DEC1 - ((last - 1) % DIG_PER_DEC1 + 1) < shift)
+ *(from + 1) = (*from % powers10[shift]) * powers10[c_shift];
+ for (; from > end; from--)
+ *from = (*from / powers10[shift] +
+ (*(from - 1) % powers10[shift]) * powers10[c_shift]);
+ *from = *from / powers10[shift];
+}
+
+/*
+ Shift of decimal digits in given number (with rounding if it need)
+
+ SYNOPSIS
+ decimal_shift()
+ dec number to be shifted
+ shift number of decimal positions
+ shift > 0 means shift to left shift
+ shift < 0 meand right shift
+ NOTE
+ In fact it is multipling on 10^shift.
+ RETURN
+ E_DEC_OK OK
+ E_DEC_OVERFLOW operation lead to overflow, number is untoched
+ E_DEC_TRUNCATED number was rounded to fit into buffer
+*/
+
+int decimal_shift(decimal_t *dec, int shift) {
+ /* index of first non zero digit (all indexes from 0) */
+ int beg;
+ /* index of position after last decimal digit */
+ int end;
+ /* index of digit position just after point */
+ int point = ROUND_UP(dec->intg) * DIG_PER_DEC1;
+ /* new point position */
+ int new_point = point + shift;
+ /* number of digits in result */
+ int digits_int, digits_frac;
+ /* length of result and new fraction in big digits*/
+ int new_len, new_frac_len;
+ /* return code */
+ int err = E_DEC_OK;
+ int new_front;
+
+ if (shift == 0) return E_DEC_OK;
+
+ digits_bounds(dec, &beg, &end);
+
+ if (beg == end) {
+ decimal_make_zero(dec);
+ return E_DEC_OK;
+ }
+
+ digits_int = new_point - beg;
+ set_if_bigger(digits_int, 0);
+ digits_frac = end - new_point;
+ set_if_bigger(digits_frac, 0);
+
+ if ((new_len = ROUND_UP(digits_int) +
+ (new_frac_len = ROUND_UP(digits_frac))) > dec->len) {
+ int lack = new_len - dec->len;
+ int diff;
+
+ if (new_frac_len < lack)
+ return E_DEC_OVERFLOW; /* lack more then we have in fraction */
+
+ /* cat off fraction part to allow new number to fit in our buffer */
+ err = E_DEC_TRUNCATED;
+ new_frac_len -= lack;
+ diff = digits_frac - (new_frac_len * DIG_PER_DEC1);
+ /* Make rounding method as parameter? */
+ decimal_round(dec, dec, end - point - diff, HALF_UP);
+ end -= diff;
+ digits_frac = new_frac_len * DIG_PER_DEC1;
+
+ if (end <= beg) {
+ /*
+ we lost all digits (they will be shifted out of buffer), so we can
+ just return 0
+ */
+ decimal_make_zero(dec);
+ return E_DEC_TRUNCATED;
+ }
+ }
+
+ if (shift % DIG_PER_DEC1) {
+ int l_mini_shift, r_mini_shift, mini_shift;
+ int do_left;
+ /*
+ Calculate left/right shift to align decimal digits inside our bug
+ digits correctly
+ */
+ if (shift > 0) {
+ l_mini_shift = shift % DIG_PER_DEC1;
+ r_mini_shift = DIG_PER_DEC1 - l_mini_shift;
+ /*
+ It is left shift so prefer left shift, but if we have not place from
+ left, we have to have it from right, because we checked length of
+ result
+ */
+ do_left = l_mini_shift <= beg;
+ DBUG_ASSERT(do_left || (dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
+ } else {
+ r_mini_shift = (-shift) % DIG_PER_DEC1;
+ l_mini_shift = DIG_PER_DEC1 - r_mini_shift;
+ /* see comment above */
+ do_left = !((dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
+ DBUG_ASSERT(!do_left || l_mini_shift <= beg);
+ }
+ if (do_left) {
+ do_mini_left_shift(dec, l_mini_shift, beg, end);
+ mini_shift = -l_mini_shift;
+ } else {
+ do_mini_right_shift(dec, r_mini_shift, beg, end);
+ mini_shift = r_mini_shift;
+ }
+ new_point += mini_shift;
+ /*
+ If number is shifted and correctly aligned in buffer we can
+ finish
+ */
+ if (!(shift += mini_shift) && (new_point - digits_int) < DIG_PER_DEC1) {
+ dec->intg = digits_int;
+ dec->frac = digits_frac;
+ return err; /* already shifted as it should be */
+ }
+ beg += mini_shift;
+ end += mini_shift;
+ }
+
+ /* if new 'decimal front' is in first digit, we do not need move digits */
+ if ((new_front = (new_point - digits_int)) >= DIG_PER_DEC1 || new_front < 0) {
+ /* need to move digits */
+ int d_shift;
+ dec1 *to, *barier;
+ if (new_front > 0) {
+ /* move left */
+ d_shift = new_front / DIG_PER_DEC1;
+ to = dec->buf + (ROUND_UP(beg + 1) - 1 - d_shift);
+ barier = dec->buf + (ROUND_UP(end) - 1 - d_shift);
+ DBUG_ASSERT(to >= dec->buf);
+ DBUG_ASSERT(barier + d_shift < dec->buf + dec->len);
+ for (; to <= barier; to++) *to = *(to + d_shift);
+ for (barier += d_shift; to <= barier; to++) *to = 0;
+ d_shift = -d_shift;
+ } else {
+ /* move right */
+ d_shift = (1 - new_front) / DIG_PER_DEC1;
+ to = dec->buf + ROUND_UP(end) - 1 + d_shift;
+ barier = dec->buf + ROUND_UP(beg + 1) - 1 + d_shift;
+ DBUG_ASSERT(to < dec->buf + dec->len);
+ DBUG_ASSERT(barier - d_shift >= dec->buf);
+ for (; to >= barier; to--) *to = *(to - d_shift);
+ for (barier -= d_shift; to >= barier; to--) *to = 0;
+ }
+ d_shift *= DIG_PER_DEC1;
+ beg += d_shift;
+ end += d_shift;
+ new_point += d_shift;
+ }
+
+ /*
+ If there are gaps then fill ren with 0.
+
+ Only one of following 'for' loops will work becouse beg <= end
+ */
+ beg = ROUND_UP(beg + 1) - 1;
+ end = ROUND_UP(end) - 1;
+ DBUG_ASSERT(new_point >= 0);
+
+ /* We don't want negative new_point below */
+ if (new_point != 0) new_point = ROUND_UP(new_point) - 1;
+
+ if (new_point > end) {
+ do {
+ dec->buf[new_point] = 0;
+ } while (--new_point > end);
+ } else {
+ for (; new_point < beg; new_point++) dec->buf[new_point] = 0;
+ }
+ dec->intg = digits_int;
+ dec->frac = digits_frac;
+ return err;
+}
+
+/*
+ Convert string to decimal
+
+ SYNOPSIS
+ string2decimal()
+ from - value to convert. Doesn't have to be \0 terminated!
+ to - decimal where where the result will be stored
+ to->buf and to->len must be set.
+ end - Pointer to pointer to end of string. Will on return be
+ set to the char after the last used character
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_BAD_NUM/E_DEC_OOM
+ In case of E_DEC_FATAL_ERROR *to is set to decimal zero
+ (to make error handling easier)
+*/
+
+int string2decimal(const char *from, decimal_t *to, char **end) {
+ const char *s = from, *s1, *endp, *end_of_string = *end;
+ int i, intg, frac, error, intg1, frac1;
+ dec1 x, *buf;
+ sanity(to);
+
+ error = E_DEC_BAD_NUM; /* In case of bad number */
+ while (s < end_of_string && my_isspace(&my_charset_latin1, *s)) s++;
+ if (s == end_of_string) goto fatal_error;
+
+ if ((to->sign = (*s == '-')))
+ s++;
+ else if (*s == '+')
+ s++;
+
+ s1 = s;
+ while (s < end_of_string && my_isdigit(&my_charset_latin1, *s)) s++;
+ intg = (int)(s - s1);
+ if (s < end_of_string && *s == '.') {
+ endp = s + 1;
+ while (endp < end_of_string && my_isdigit(&my_charset_latin1, *endp))
+ endp++;
+ frac = (int)(endp - s - 1);
+ } else {
+ frac = 0;
+ endp = s;
+ }
+
+ *end = (char *)endp;
+
+ if (frac + intg == 0) goto fatal_error;
+
+ error = 0;
+
+ intg1 = ROUND_UP(intg);
+ frac1 = ROUND_UP(frac);
+ FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
+ if (unlikely(error)) {
+ frac = frac1 * DIG_PER_DEC1;
+ if (error == E_DEC_OVERFLOW) intg = intg1 * DIG_PER_DEC1;
+ }
+
+ /* Error is guranteed to be set here */
+ to->intg = intg;
+ to->frac = frac;
+
+ buf = to->buf + intg1;
+ s1 = s;
+
+ for (x = 0, i = 0; intg; intg--) {
+ x += (*--s - '0') * powers10[i];
+
+ if (unlikely(++i == DIG_PER_DEC1)) {
+ *--buf = x;
+ x = 0;
+ i = 0;
+ }
+ }
+ if (i) *--buf = x;
+
+ buf = to->buf + intg1;
+ for (x = 0, i = 0; frac; frac--) {
+ x = (*++s1 - '0') + x * 10;
+
+ if (unlikely(++i == DIG_PER_DEC1)) {
+ *buf++ = x;
+ x = 0;
+ i = 0;
+ }
+ }
+ if (i) *buf = x * powers10[DIG_PER_DEC1 - i];
+
+ /* Handle exponent */
+ if (endp + 1 < end_of_string && (*endp == 'e' || *endp == 'E')) {
+ int str_error;
+ longlong exponent =
+ my_strtoll10(endp + 1, (char **)&end_of_string, &str_error);
+
+ if (end_of_string != endp + 1) /* If at least one digit */
+ {
+ *end = (char *)end_of_string;
+ if (str_error > 0) {
+ error = E_DEC_BAD_NUM;
+ goto fatal_error;
+ }
+ if (exponent > INT_MAX / 2 || (str_error == 0 && exponent < 0)) {
+ error = E_DEC_OVERFLOW;
+ goto fatal_error;
+ }
+ if (exponent < INT_MIN / 2 && error != E_DEC_OVERFLOW) {
+ error = E_DEC_TRUNCATED;
+ goto fatal_error;
+ }
+ if (error != E_DEC_OVERFLOW) error = decimal_shift(to, (int)exponent);
+ }
+ }
+ /* Avoid returning negative zero, cfr. decimal_cmp() */
+ if (to->sign && decimal_is_zero(to)) to->sign = false;
+ return error;
+
+fatal_error:
+ decimal_make_zero(to);
+ return error;
+}
+
+/*
+ Convert decimal to double
+
+ SYNOPSIS
+ decimal2double()
+ from - value to convert
+ to - result will be stored there
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_OVERFLOW/E_DEC_TRUNCATED
+*/
+
+int decimal2double(const decimal_t *from, double *to) {
+ char strbuf[FLOATING_POINT_BUFFER], *end;
+ int len = sizeof(strbuf);
+ int rc, error;
+
+ rc = decimal2string(from, strbuf, &len, 0, 0, 0);
+ end = strbuf + len;
+
+ DBUG_PRINT("info", ("interm.: %s", strbuf));
+
+ *to = my_strtod(strbuf, &end, &error);
+
+ DBUG_PRINT("info", ("result: %f", *to));
+
+ return (rc != E_DEC_OK) ? rc : (error ? E_DEC_OVERFLOW : E_DEC_OK);
+}
+
+/*
+ Convert double to decimal
+
+ SYNOPSIS
+ double2decimal()
+ from - value to convert
+ to - result will be stored there
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_OVERFLOW/E_DEC_TRUNCATED
+*/
+
+int double2decimal(double from, decimal_t *to) {
+ char buff[FLOATING_POINT_BUFFER], *end;
+ int res;
+ DBUG_ENTER("double2decimal");
+ end = buff +
+ my_gcvt(from, MY_GCVT_ARG_DOUBLE, (int)sizeof(buff) - 1, buff, NULL);
+ res = string2decimal(buff, to, &end);
+ DBUG_PRINT("exit", ("res: %d", res));
+ DBUG_RETURN(res);
+}
+
+static int ull2dec(ulonglong from, decimal_t *to) {
+ int intg1;
+ int error = E_DEC_OK;
+ ulonglong x = from;
+ dec1 *buf;
+
+ sanity(to);
+
+ if (from == 0)
+ intg1 = 1;
+ else {
+ /* Count the number of decimal_digit_t's we need. */
+ for (intg1 = 0; from != 0; intg1++, from /= DIG_BASE)
+ ;
+ }
+ if (unlikely(intg1 > to->len)) {
+ intg1 = to->len;
+ error = E_DEC_OVERFLOW;
+ }
+ to->frac = 0;
+ to->intg = intg1 * DIG_PER_DEC1;
+
+ for (buf = to->buf + intg1; intg1; intg1--) {
+ ulonglong y = x / DIG_BASE;
+ *--buf = (dec1)(x - y * DIG_BASE);
+ x = y;
+ }
+ return error;
+}
+
+int ulonglong2decimal(ulonglong from, decimal_t *to) {
+ to->sign = 0;
+ return ull2dec(from, to);
+}
+
+int longlong2decimal(longlong from, decimal_t *to) {
+ if ((to->sign = from < 0))
+ return ull2dec(from == LLONG_MIN ? static_cast<ulonglong>(from) : -from,
+ to);
+ return ull2dec(from, to);
+}
+
+int decimal2ulonglong(decimal_t *from, ulonglong *to) {
+ dec1 *buf = from->buf;
+ ulonglong x = 0;
+ int intg, frac;
+
+ if (from->sign) {
+ *to = 0ULL;
+ return E_DEC_OVERFLOW;
+ }
+
+ for (intg = from->intg; intg > 0; intg -= DIG_PER_DEC1) {
+ ulonglong y = x;
+ x = x * DIG_BASE + *buf++;
+ if (unlikely(y > ((ulonglong)ULLONG_MAX / DIG_BASE) || x < y)) {
+ *to = ULLONG_MAX;
+ return E_DEC_OVERFLOW;
+ }
+ }
+ *to = x;
+ for (frac = from->frac; unlikely(frac > 0); frac -= DIG_PER_DEC1)
+ if (*buf++) return E_DEC_TRUNCATED;
+ return E_DEC_OK;
+}
+
+int decimal2longlong(decimal_t *from, longlong *to) {
+ dec1 *buf = from->buf;
+ longlong x = 0;
+ int intg, frac;
+
+ for (intg = from->intg; intg > 0; intg -= DIG_PER_DEC1) {
+ /*
+ Attention: trick!
+ we're calculating -|from| instead of |from| here
+ because |LLONG_MIN| > LLONG_MAX
+ so we can convert -9223372036854775808 correctly
+ */
+ if (unlikely(x < (LLONG_MIN / DIG_BASE))) {
+ /*
+ the decimal is bigger than any possible integer
+ return border integer depending on the sign
+ */
+ *to = from->sign ? LLONG_MIN : LLONG_MAX;
+ return E_DEC_OVERFLOW;
+ }
+ x = x * DIG_BASE;
+ const longlong digit = *buf++;
+ if (unlikely(x < LLONG_MIN + digit)) {
+ /*
+ the decimal is bigger than any possible integer
+ return border integer depending on the sign
+ */
+ *to = from->sign ? LLONG_MIN : LLONG_MAX;
+ return E_DEC_OVERFLOW;
+ }
+ x = x - digit;
+ }
+ /* boundary case: 9223372036854775808 */
+ if (unlikely(from->sign == 0 && x == LLONG_MIN)) {
+ *to = LLONG_MAX;
+ return E_DEC_OVERFLOW;
+ }
+
+ *to = from->sign ? x : -x;
+ for (frac = from->frac; unlikely(frac > 0); frac -= DIG_PER_DEC1)
+ if (*buf++) return E_DEC_TRUNCATED;
+ return E_DEC_OK;
+}
+
+#define LLDIV_MIN -1000000000000000000LL
+#define LLDIV_MAX 1000000000000000000LL
+
+/**
+ Convert decimal value to lldiv_t value.
+ @param from The decimal value to convert from.
+ @param [out] to The lldiv_t variable to convert to.
+ @return 0 on success, error code on error.
+*/
+int decimal2lldiv_t(const decimal_t *from, lldiv_t *to) {
+ int int_part = ROUND_UP(from->intg);
+ int frac_part = ROUND_UP(from->frac);
+ if (int_part > 2) {
+ to->rem = 0;
+ to->quot = from->sign ? LLDIV_MIN : LLDIV_MAX;
+ return E_DEC_OVERFLOW;
+ }
+ if (int_part == 2)
+ to->quot = ((longlong)from->buf[0]) * DIG_BASE + from->buf[1];
+ else if (int_part == 1)
+ to->quot = from->buf[0];
+ else
+ to->quot = 0;
+ to->rem = frac_part ? from->buf[int_part] : 0;
+ if (from->sign) {
+ to->quot = -to->quot;
+ to->rem = -to->rem;
+ }
+ return 0;
+}
+
+/**
+ Convert double value to lldiv_t valie.
+ @param nr The double value to convert from.
+ @param [out] lld The lldit_t variable to convert to.
+ @return 0 on success, error code on error.
+
+ Integer part goes into lld.quot.
+ Fractional part multiplied to 1000000000 (10^9) goes to lld.rem.
+ Typically used in datetime calculations to split seconds
+ and nanoseconds.
+*/
+int double2lldiv_t(double nr, lldiv_t *lld) {
+ if (nr > LLDIV_MAX) {
+ lld->quot = LLDIV_MAX;
+ lld->rem = 0;
+ return E_DEC_OVERFLOW;
+ } else if (nr < LLDIV_MIN) {
+ lld->quot = LLDIV_MIN;
+ lld->rem = 0;
+ return E_DEC_OVERFLOW;
+ }
+ /* Truncate fractional part toward zero and store into "quot" */
+ lld->quot = (longlong)(nr > 0 ? floor(nr) : ceil(nr));
+ /* Multiply reminder to 10^9 and store into "rem" */
+ lld->rem = (longlong)rint((nr - (double)lld->quot) * 1000000000);
+ /*
+ Sometimes the expression "(double) 0.999999999xxx * (double) 10e9"
+ gives 1,000,000,000 instead of 999,999,999 due to lack of double precision.
+ The callers do not expect lld->rem to be greater than 999,999,999.
+ Let's catch this corner case and put the "nanounit" (e.g. nanosecond)
+ value in ldd->rem back into the valid range.
+ */
+ if (lld->rem > 999999999LL)
+ lld->rem = 999999999LL;
+ else if (lld->rem < -999999999LL)
+ lld->rem = -999999999LL;
+ return E_DEC_OK;
+}
+
+/*
+ Convert decimal to its binary fixed-length representation
+ two representations of the same length can be compared with memcmp
+ with the correct -1/0/+1 result
+
+ SYNOPSIS
+ decimal2bin()
+ from - value to convert
+ to - points to buffer where string representation should be stored
+ precision/scale - see decimal_bin_size() below
+
+ NOTE
+ the buffer is assumed to be of the size decimal_bin_size(precision, scale)
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
+
+ DESCRIPTION
+ for storage decimal numbers are converted to the "binary" format.
+
+ This format has the following properties:
+ 1. length of the binary representation depends on the {precision, scale}
+ as provided by the caller and NOT on the intg/frac of the decimal to
+ convert.
+ 2. binary representations of the same {precision, scale} can be compared
+ with memcmp - with the same result as decimal_cmp() of the original
+ decimals (not taking into account possible precision loss during
+ conversion).
+
+ This binary format is as follows:
+ 1. First the number is converted to have a requested precision and scale.
+ 2. Every full DIG_PER_DEC1 digits of intg part are stored in 4 bytes
+ as is
+ 3. The first intg % DIG_PER_DEC1 digits are stored in the reduced
+ number of bytes (enough bytes to store this number of digits -
+ see dig2bytes)
+ 4. same for frac - full decimal_digit_t's are stored as is,
+ the last frac % DIG_PER_DEC1 digits - in the reduced number of bytes.
+ 5. If the number is negative - every byte is inversed.
+ 5. The very first bit of the resulting byte array is inverted (because
+ memcmp compares unsigned bytes, see property 2 above)
+
+ Example:
+
+ 1234567890.1234
+
+ internally is represented as 3 decimal_digit_t's
+
+ 1 234567890 123400000
+
+ (assuming we want a binary representation with precision=14, scale=4)
+ in hex it's
+
+ 00-00-00-01 0D-FB-38-D2 07-5A-EF-40
+
+ now, middle decimal_digit_t is full - it stores 9 decimal digits. It goes
+ into binary representation as is:
+
+
+ ........... 0D-FB-38-D2 ............
+
+ First decimal_digit_t has only one decimal digit. We can store one digit in
+ one byte, no need to waste four:
+
+ 01 0D-FB-38-D2 ............
+
+ now, last digit. It's 123400000. We can store 1234 in two bytes:
+
+ 01 0D-FB-38-D2 04-D2
+
+ So, we've packed 12 bytes number in 7 bytes.
+ And now we invert the highest bit to get the final result:
+
+ 81 0D FB 38 D2 04 D2
+
+ And for -1234567890.1234 it would be
+
+ 7E F2 04 C7 2D FB 2D
+*/
+int decimal2bin(decimal_t *from, uchar *to, int precision, int frac) {
+ dec1 mask = from->sign ? -1 : 0, *buf1 = from->buf, *stop1;
+ int error = E_DEC_OK, intg = precision - frac, isize1, intg1, intg1x,
+ from_intg, intg0 = intg / DIG_PER_DEC1, frac0 = frac / DIG_PER_DEC1,
+ intg0x = intg - intg0 * DIG_PER_DEC1,
+ frac0x = frac - frac0 * DIG_PER_DEC1, frac1 = from->frac / DIG_PER_DEC1,
+ frac1x = from->frac - frac1 * DIG_PER_DEC1,
+ isize0 = intg0 * sizeof(dec1) + dig2bytes[intg0x],
+ fsize0 = frac0 * sizeof(dec1) + dig2bytes[frac0x],
+ fsize1 = frac1 * sizeof(dec1) + dig2bytes[frac1x];
+ const int orig_isize0 = isize0;
+ const int orig_fsize0 = fsize0;
+ uchar *orig_to = to;
+
+ buf1 = remove_leading_zeroes(from, &from_intg);
+
+ if (unlikely(from_intg + fsize1 == 0)) {
+ mask = 0; /* just in case */
+ intg = 1;
+ buf1 = &mask;
+ }
+
+ intg1 = from_intg / DIG_PER_DEC1;
+ intg1x = from_intg - intg1 * DIG_PER_DEC1;
+ isize1 = intg1 * sizeof(dec1) + dig2bytes[intg1x];
+
+ if (intg < from_intg) {
+ buf1 += intg1 - intg0 + (intg1x > 0) - (intg0x > 0);
+ intg1 = intg0;
+ intg1x = intg0x;
+ error = E_DEC_OVERFLOW;
+ } else if (isize0 > isize1) {
+ while (isize0-- > isize1) *to++ = (char)mask;
+ }
+ if (fsize0 < fsize1) {
+ frac1 = frac0;
+ frac1x = frac0x;
+ error = E_DEC_TRUNCATED;
+ } else if (fsize0 > fsize1 && frac1x) {
+ if (frac0 == frac1) {
+ frac1x = frac0x;
+ fsize0 = fsize1;
+ } else {
+ frac1++;
+ frac1x = 0;
+ }
+ }
+
+ /* intg1x part */
+ if (intg1x) {
+ int i = dig2bytes[intg1x];
+ dec1 x = mod_by_pow10(*buf1++, intg1x) ^ mask;
+ switch (i) {
+ case 1:
+ mi_int1store(to, x);
+ break;
+ case 2:
+ mi_int2store(to, x);
+ break;
+ case 3:
+ mi_int3store(to, x);
+ break;
+ case 4:
+ mi_int4store(to, x);
+ break;
+ default:
+ DBUG_ASSERT(0);
+ }
+ to += i;
+ }
+
+ /* intg1+frac1 part */
+ for (stop1 = buf1 + intg1 + frac1; buf1 < stop1; to += sizeof(dec1)) {
+ dec1 x = *buf1++ ^ mask;
+ DBUG_ASSERT(sizeof(dec1) == 4);
+ mi_int4store(to, x);
+ }
+
+ /* frac1x part */
+ if (frac1x) {
+ dec1 x;
+ int i = dig2bytes[frac1x], lim = (frac1 < frac0 ? DIG_PER_DEC1 : frac0x);
+ while (frac1x < lim && dig2bytes[frac1x] == i) frac1x++;
+ x = div_by_pow10(*buf1, DIG_PER_DEC1 - frac1x) ^ mask;
+ switch (i) {
+ case 1:
+ mi_int1store(to, x);
+ break;
+ case 2:
+ mi_int2store(to, x);
+ break;
+ case 3:
+ mi_int3store(to, x);
+ break;
+ case 4:
+ mi_int4store(to, x);
+ break;
+ default:
+ DBUG_ASSERT(0);
+ }
+ to += i;
+ }
+ if (fsize0 > fsize1) {
+ uchar *to_end = orig_to + orig_fsize0 + orig_isize0;
+
+ while (fsize0-- > fsize1 && to < to_end) *to++ = (uchar)mask;
+ }
+ orig_to[0] ^= 0x80;
+
+ /* Check that we have written the whole decimal and nothing more */
+ DBUG_ASSERT(to == orig_to + orig_fsize0 + orig_isize0);
+ return error;
+}
+
+/*
+ Restores decimal from its binary fixed-length representation
+
+ SYNOPSIS
+ bin2decimal()
+ from - value to convert
+ to - result
+ precision/scale - see decimal_bin_size() below
+
+ NOTE
+ see decimal2bin()
+ the buffer is assumed to be of the size decimal_bin_size(precision, scale)
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
+*/
+
+int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale) {
+ int error = E_DEC_OK, intg = precision - scale, intg0 = intg / DIG_PER_DEC1,
+ frac0 = scale / DIG_PER_DEC1, intg0x = intg - intg0 * DIG_PER_DEC1,
+ frac0x = scale - frac0 * DIG_PER_DEC1, intg1 = intg0 + (intg0x > 0),
+ frac1 = frac0 + (frac0x > 0);
+ dec1 *buf = to->buf, mask = (*from & 0x80) ? 0 : -1;
+ const uchar *stop;
+ uchar *d_copy;
+ int bin_size = decimal_bin_size_inline(precision, scale);
+
+ sanity(to);
+ d_copy = (uchar *)my_alloca(bin_size);
+ memcpy(d_copy, from, bin_size);
+ d_copy[0] ^= 0x80;
+ from = d_copy;
+
+ FIX_INTG_FRAC_ERROR(to->len, intg1, frac1, error);
+ if (unlikely(error)) {
+ if (intg1 < intg0 + (intg0x > 0)) {
+ from += dig2bytes[intg0x] + sizeof(dec1) * (intg0 - intg1);
+ frac0 = frac0x = intg0x = 0;
+ intg0 = intg1;
+ } else {
+ frac0x = 0;
+ frac0 = frac1;
+ }
+ }
+
+ to->sign = (mask != 0);
+ to->intg = intg0 * DIG_PER_DEC1 + intg0x;
+ to->frac = frac0 * DIG_PER_DEC1 + frac0x;
+
+ if (intg0x) {
+ int i = dig2bytes[intg0x];
+ dec1 x = 0;
+ switch (i) {
+ case 1:
+ x = mi_sint1korr(from);
+ break;
+ case 2:
+ x = mi_sint2korr(from);
+ break;
+ case 3:
+ x = mi_sint3korr(from);
+ break;
+ case 4:
+ x = mi_sint4korr(from);
+ break;
+ default:
+ DBUG_ASSERT(0);
+ }
+ from += i;
+ *buf = x ^ mask;
+ if (((ulonglong)*buf) >= (ulonglong)powers10[intg0x + 1]) goto err;
+ if (buf > to->buf || *buf != 0)
+ buf++;
+ else
+ to->intg -= intg0x;
+ }
+ for (stop = from + intg0 * sizeof(dec1); from < stop; from += sizeof(dec1)) {
+ DBUG_ASSERT(sizeof(dec1) == 4);
+ *buf = mi_sint4korr(from) ^ mask;
+ if (((uint32)*buf) > DIG_MAX) goto err;
+ if (buf > to->buf || *buf != 0)
+ buf++;
+ else
+ to->intg -= DIG_PER_DEC1;
+ }
+ DBUG_ASSERT(to->intg >= 0);
+ for (stop = from + frac0 * sizeof(dec1); from < stop; from += sizeof(dec1)) {
+ DBUG_ASSERT(sizeof(dec1) == 4);
+ *buf = mi_sint4korr(from) ^ mask;
+ if (((uint32)*buf) > DIG_MAX) goto err;
+ buf++;
+ }
+ if (frac0x) {
+ int i = dig2bytes[frac0x];
+ dec1 x = 0;
+ switch (i) {
+ case 1:
+ x = mi_sint1korr(from);
+ break;
+ case 2:
+ x = mi_sint2korr(from);
+ break;
+ case 3:
+ x = mi_sint3korr(from);
+ break;
+ case 4:
+ x = mi_sint4korr(from);
+ break;
+ default:
+ DBUG_ASSERT(0);
+ }
+ *buf = (x ^ mask) * powers10[DIG_PER_DEC1 - frac0x];
+ if (((uint32)*buf) > DIG_MAX) goto err;
+ buf++;
+ }
+
+ /*
+ No digits? We have read the number zero, of unspecified precision.
+ Make it a proper zero, with non-zero precision.
+ */
+ if (to->intg == 0 && to->frac == 0) decimal_make_zero(to);
+ return error;
+
+err:
+ decimal_make_zero(to);
+ return (E_DEC_BAD_NUM);
+}
+
+/*
+ Returns the size of array to hold a decimal with given precision and scale
+
+ RETURN VALUE
+ size in dec1
+ (multiply by sizeof(dec1) to get the size if bytes)
+*/
+
+int decimal_size(int precision, int scale) {
+ DBUG_ASSERT(scale >= 0 && precision > 0 && scale <= precision);
+ return ROUND_UP(precision - scale) + ROUND_UP(scale);
+}
+
+/*
+ Returns the size of array to hold a binary representation of a decimal
+
+ RETURN VALUE
+ size in bytes
+*/
+ALWAYS_INLINE static int decimal_bin_size_inline(int precision, int scale) {
+ int intg = precision - scale, intg0 = intg / DIG_PER_DEC1,
+ frac0 = scale / DIG_PER_DEC1, intg0x = intg - intg0 * DIG_PER_DEC1,
+ frac0x = scale - frac0 * DIG_PER_DEC1;
+
+ DBUG_ASSERT(scale >= 0 && precision > 0 && scale <= precision);
+ DBUG_ASSERT(intg0x >= 0);
+ DBUG_ASSERT(intg0x <= DIG_PER_DEC1);
+ DBUG_ASSERT(frac0x >= 0);
+ DBUG_ASSERT(frac0x <= DIG_PER_DEC1);
+ return intg0 * sizeof(dec1) + dig2bytes[intg0x] + frac0 * sizeof(dec1) +
+ dig2bytes[frac0x];
+}
+
+int decimal_bin_size(int precision, int scale) {
+ return decimal_bin_size_inline(precision, scale);
+}
+
+/*
+ Rounds the decimal to "scale" digits
+
+ SYNOPSIS
+ decimal_round()
+ from - decimal to round,
+ to - result buffer. from==to is allowed
+ scale - to what position to round. can be negative!
+ mode - round to nearest even or truncate
+
+ NOTES
+ scale can be negative !
+ one TRUNCATED error (line XXX below) isn't treated very logical :(
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_TRUNCATED
+*/
+
+int decimal_round(const decimal_t *from, decimal_t *to, int scale,
+ decimal_round_mode mode) {
+ int frac0 = scale > 0 ? ROUND_UP(scale) : (scale + 1) / DIG_PER_DEC1,
+ frac1 = ROUND_UP(from->frac), round_digit = 0,
+ intg0 = ROUND_UP(from->intg), error = E_DEC_OK, len = to->len;
+
+ dec1 *buf0 = from->buf, *buf1 = to->buf, x, y, carry = 0;
+ int first_dig;
+
+ sanity(to);
+
+ switch (mode) {
+ case HALF_UP:
+ case HALF_EVEN:
+ round_digit = 5;
+ break;
+ case CEILING:
+ round_digit = from->sign ? 10 : 0;
+ break;
+ case FLOOR:
+ round_digit = from->sign ? 0 : 10;
+ break;
+ case TRUNCATE:
+ round_digit = 10;
+ break;
+ default:
+ DBUG_ASSERT(0);
+ }
+
+ /*
+ For my_decimal we always use len == DECIMAL_BUFF_LENGTH == 9
+ For internal testing here (ifdef MAIN) we always use len == 100/4
+ */
+ DBUG_ASSERT(from->len == to->len);
+
+ if (unlikely(frac0 + intg0 > len)) {
+ frac0 = len - intg0;
+ scale = frac0 * DIG_PER_DEC1;
+ error = E_DEC_TRUNCATED;
+ }
+
+ if (scale + from->intg < 0) {
+ decimal_make_zero(to);
+ return E_DEC_OK;
+ }
+
+ if (to != from) {
+ dec1 *p0 = buf0 + intg0 + MY_MAX(frac1, frac0);
+ dec1 *p1 = buf1 + intg0 + MY_MAX(frac1, frac0);
+
+ DBUG_ASSERT(p0 - buf0 <= len);
+ DBUG_ASSERT(p1 - buf1 <= len);
+
+ while (buf0 < p0) *(--p1) = *(--p0);
+
+ buf0 = to->buf;
+ buf1 = to->buf;
+ to->sign = from->sign;
+ to->intg = MY_MIN(intg0, len) * DIG_PER_DEC1;
+ }
+
+ if (frac0 > frac1) {
+ buf1 += intg0 + frac1;
+ while (frac0-- > frac1) *buf1++ = 0;
+ goto done;
+ }
+
+ if (scale >= from->frac) goto done; /* nothing to do */
+
+ buf0 += intg0 + frac0 - 1;
+ buf1 += intg0 + frac0 - 1;
+ if (scale == frac0 * DIG_PER_DEC1) {
+ int do_inc = false;
+ DBUG_ASSERT(frac0 + intg0 >= 0);
+ switch (round_digit) {
+ case 0: {
+ dec1 *p0 = buf0 + (frac1 - frac0);
+ for (; p0 > buf0; p0--) {
+ if (*p0) {
+ do_inc = true;
+ break;
+ }
+ }
+ break;
+ }
+ case 5: {
+ x = buf0[1] / DIG_MASK;
+ do_inc =
+ (x > 5) ||
+ ((x == 5) && (mode == HALF_UP || (frac0 + intg0 > 0 && *buf0 & 1)));
+ break;
+ }
+ default:
+ break;
+ }
+ if (do_inc) {
+ if (frac0 + intg0 > 0)
+ (*buf1)++;
+ else
+ *(++buf1) = DIG_BASE;
+ } else if (frac0 + intg0 == 0) {
+ decimal_make_zero(to);
+ return E_DEC_OK;
+ }
+ } else {
+ /* TODO - fix this code as it won't work for CEILING mode */
+ int pos = frac0 * DIG_PER_DEC1 - scale - 1;
+ DBUG_ASSERT(frac0 + intg0 > 0);
+ x = *buf1 / powers10[pos];
+ y = x % 10;
+ if (y > round_digit ||
+ (round_digit == 5 && y == 5 && (mode == HALF_UP || (x / 10) & 1)))
+ x += 10;
+ *buf1 = powers10[pos] * (x - y);
+ }
+ /*
+ In case we're rounding e.g. 1.5e9 to 2.0e9, the decimal_digit_t's inside
+ the buffer are as follows.
+
+ Before <1, 5e8>
+ After <2, 5e8>
+
+ Hence we need to set the 2nd field to 0.
+ The same holds if we round 1.5e-9 to 2e-9.
+ */
+ if (frac0 < frac1) {
+ dec1 *buf = to->buf + ((scale == 0 && intg0 == 0) ? 1 : intg0 + frac0);
+ dec1 *end = to->buf + len;
+
+ while (buf < end) *buf++ = 0;
+ }
+ if (*buf1 >= DIG_BASE) {
+ carry = 1;
+ *buf1 -= DIG_BASE;
+ while (carry && --buf1 >= to->buf) ADD(*buf1, *buf1, 0, carry);
+ if (unlikely(carry)) {
+ /* shifting the number to create space for new digit */
+ if (frac0 + intg0 >= len) {
+ frac0--;
+ scale = frac0 * DIG_PER_DEC1;
+ error = E_DEC_TRUNCATED; /* XXX */
+ }
+ for (buf1 = to->buf + intg0 + MY_MAX(frac0, 0); buf1 > to->buf; buf1--) {
+ /* Avoid out-of-bounds write. */
+ if (buf1 < to->buf + len)
+ buf1[0] = buf1[-1];
+ else
+ error = E_DEC_OVERFLOW;
+ }
+ *buf1 = 1;
+ /* We cannot have more than 9 * 9 = 81 digits. */
+ if (to->intg < len * DIG_PER_DEC1)
+ to->intg++;
+ else
+ error = E_DEC_OVERFLOW;
+ }
+ } else {
+ for (;;) {
+ if (likely(*buf1)) break;
+ if (buf1-- == to->buf) {
+ /* making 'zero' with the proper scale */
+ dec1 *p0 = to->buf + frac0 + 1;
+ to->intg = 1;
+ to->frac = MY_MAX(scale, 0);
+ to->sign = 0;
+ for (buf1 = to->buf; buf1 < p0; buf1++) *buf1 = 0;
+ return E_DEC_OK;
+ }
+ }
+ }
+
+ /* Here we check 999.9 -> 1000 case when we need to increase intg */
+ first_dig = to->intg % DIG_PER_DEC1;
+ if (first_dig && (*buf1 >= powers10[first_dig])) to->intg++;
+
+ if (scale < 0) scale = 0;
+
+done:
+ DBUG_ASSERT(to->intg <= (len * DIG_PER_DEC1));
+ to->frac = scale;
+ return error;
+}
+
+/*
+ Returns the size of the result of the operation
+
+ SYNOPSIS
+ decimal_result_size()
+ from1 - operand of the unary operation or first operand of the
+ binary operation
+ from2 - second operand of the binary operation
+ op - operation. one char '+', '-', '*', '/' are allowed
+ others may be added later
+ param - extra param to the operation. unused for '+', '-', '*'
+ scale increment for '/'
+
+ NOTE
+ returned valued may be larger than the actual buffer requred
+ in the operation, as decimal_result_size, by design, operates on
+ precision/scale values only and not on the actual decimal number
+
+ RETURN VALUE
+ size of to->buf array in dec1 elements. to get size in bytes
+ multiply by sizeof(dec1)
+*/
+
+int decimal_result_size(decimal_t *from1, decimal_t *from2, char op,
+ int param) {
+ switch (op) {
+ case '-':
+ return ROUND_UP(MY_MAX(from1->intg, from2->intg)) +
+ ROUND_UP(MY_MAX(from1->frac, from2->frac));
+ case '+':
+ return ROUND_UP(MY_MAX(from1->intg, from2->intg) + 1) +
+ ROUND_UP(MY_MAX(from1->frac, from2->frac));
+ case '*':
+ return ROUND_UP(from1->intg + from2->intg) + ROUND_UP(from1->frac) +
+ ROUND_UP(from2->frac);
+ case '/':
+ return ROUND_UP(from1->intg + from2->intg + 1 + from1->frac +
+ from2->frac + param);
+ default:
+ DBUG_ASSERT(0);
+ }
+ return -1; /* shut up the warning */
+}
+
+static int do_add(const decimal_t *from1, const decimal_t *from2,
+ decimal_t *to) {
+ int intg1 = ROUND_UP(from1->intg), intg2 = ROUND_UP(from2->intg),
+ frac1 = ROUND_UP(from1->frac), frac2 = ROUND_UP(from2->frac),
+ frac0 = MY_MAX(frac1, frac2), intg0 = MY_MAX(intg1, intg2), error;
+ dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry;
+
+ sanity(to);
+
+ /* is there a need for extra word because of carry ? */
+ x = intg1 > intg2
+ ? from1->buf[0]
+ : intg2 > intg1 ? from2->buf[0] : from1->buf[0] + from2->buf[0];
+ if (unlikely(x > DIG_MAX - 1)) /* yes, there is */
+ {
+ intg0++;
+ to->buf[0] = 0; /* safety */
+ }
+
+ FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
+ if (unlikely(error == E_DEC_OVERFLOW)) {
+ max_decimal(to->len * DIG_PER_DEC1, 0, to);
+ return error;
+ }
+
+ buf0 = to->buf + intg0 + frac0;
+
+ to->sign = from1->sign;
+ to->frac = MY_MAX(from1->frac, from2->frac);
+ to->intg = intg0 * DIG_PER_DEC1;
+ if (unlikely(error)) {
+ set_if_smaller(to->frac, frac0 * DIG_PER_DEC1);
+ set_if_smaller(frac1, frac0);
+ set_if_smaller(frac2, frac0);
+ set_if_smaller(intg1, intg0);
+ set_if_smaller(intg2, intg0);
+ }
+
+ /* part 1 - max(frac) ... min (frac) */
+ if (frac1 > frac2) {
+ buf1 = from1->buf + intg1 + frac1;
+ stop = from1->buf + intg1 + frac2;
+ buf2 = from2->buf + intg2 + frac2;
+ stop2 = from1->buf + (intg1 > intg2 ? intg1 - intg2 : 0);
+ } else {
+ buf1 = from2->buf + intg2 + frac2;
+ stop = from2->buf + intg2 + frac1;
+ buf2 = from1->buf + intg1 + frac1;
+ stop2 = from2->buf + (intg2 > intg1 ? intg2 - intg1 : 0);
+ }
+ while (buf1 > stop) *--buf0 = *--buf1;
+
+ /* part 2 - min(frac) ... min(intg) */
+ carry = 0;
+ while (buf1 > stop2) {
+ ADD(*--buf0, *--buf1, *--buf2, carry);
+ }
+
+ /* part 3 - min(intg) ... max(intg) */
+ buf1 = intg1 > intg2 ? ((stop = from1->buf) + intg1 - intg2)
+ : ((stop = from2->buf) + intg2 - intg1);
+ while (buf1 > stop) {
+ ADD(*--buf0, *--buf1, 0, carry);
+ }
+
+ if (unlikely(carry)) *--buf0 = 1;
+ DBUG_ASSERT(buf0 == to->buf || buf0 == to->buf + 1);
+
+ return error;
+}
+
+/* to=from1-from2.
+ if to==0, return -1/0/+1 - the result of the comparison */
+static int do_sub(const decimal_t *from1, const decimal_t *from2,
+ decimal_t *to) {
+ int intg1 = ROUND_UP(from1->intg), intg2 = ROUND_UP(from2->intg),
+ frac1 = ROUND_UP(from1->frac), frac2 = ROUND_UP(from2->frac);
+ int frac0 = MY_MAX(frac1, frac2), error;
+ dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry = 0;
+
+ /* let carry:=1 if from2 > from1 */
+ start1 = buf1 = from1->buf;
+ stop1 = buf1 + intg1;
+ start2 = buf2 = from2->buf;
+ stop2 = buf2 + intg2;
+ if (unlikely(*buf1 == 0)) {
+ while (buf1 < stop1 && *buf1 == 0) buf1++;
+ start1 = buf1;
+ intg1 = (int)(stop1 - buf1);
+ }
+ if (unlikely(*buf2 == 0)) {
+ while (buf2 < stop2 && *buf2 == 0) buf2++;
+ start2 = buf2;
+ intg2 = (int)(stop2 - buf2);
+ }
+ if (intg2 > intg1)
+ carry = 1;
+ else if (intg2 == intg1) {
+ dec1 *end1 = stop1 + (frac1 - 1);
+ dec1 *end2 = stop2 + (frac2 - 1);
+ while (unlikely((buf1 <= end1) && (*end1 == 0))) end1--;
+ while (unlikely((buf2 <= end2) && (*end2 == 0))) end2--;
+ frac1 = (int)(end1 - stop1) + 1;
+ frac2 = (int)(end2 - stop2) + 1;
+ while (buf1 <= end1 && buf2 <= end2 && *buf1 == *buf2) buf1++, buf2++;
+ if (buf1 <= end1) {
+ if (buf2 <= end2)
+ carry = *buf2 > *buf1;
+ else
+ carry = 0;
+ } else {
+ if (buf2 <= end2)
+ carry = 1;
+ else /* short-circuit everything: from1 == from2 */
+ {
+ if (to == 0) /* decimal_cmp() */
+ return 0;
+ decimal_make_zero(to);
+ return E_DEC_OK;
+ }
+ }
+ }
+
+ if (to == 0) /* decimal_cmp() */
+ return carry == from1->sign ? 1 : -1;
+
+ sanity(to);
+
+ to->sign = from1->sign;
+
+ /* ensure that always from1 > from2 (and intg1 >= intg2) */
+ if (carry) {
+ std::swap(from1, from2);
+ std::swap(start1, start2);
+ std::swap(intg1, intg2);
+ std::swap(frac1, frac2);
+ to->sign = 1 - to->sign;
+ }
+
+ FIX_INTG_FRAC_ERROR(to->len, intg1, frac0, error);
+ buf0 = to->buf + intg1 + frac0;
+
+ to->frac = MY_MAX(from1->frac, from2->frac);
+ to->intg = intg1 * DIG_PER_DEC1;
+ if (unlikely(error)) {
+ set_if_smaller(to->frac, frac0 * DIG_PER_DEC1);
+ set_if_smaller(frac1, frac0);
+ set_if_smaller(frac2, frac0);
+ set_if_smaller(intg2, intg1);
+ }
+ carry = 0;
+
+ /* part 1 - max(frac) ... min (frac) */
+ if (frac1 > frac2) {
+ buf1 = start1 + intg1 + frac1;
+ stop1 = start1 + intg1 + frac2;
+ buf2 = start2 + intg2 + frac2;
+ while (frac0-- > frac1) *--buf0 = 0;
+ while (buf1 > stop1) *--buf0 = *--buf1;
+ } else {
+ buf1 = start1 + intg1 + frac1;
+ buf2 = start2 + intg2 + frac2;
+ stop2 = start2 + intg2 + frac1;
+ while (frac0-- > frac2) *--buf0 = 0;
+ while (buf2 > stop2) {
+ SUB(*--buf0, 0, *--buf2, carry);
+ }
+ }
+
+ /* part 2 - min(frac) ... intg2 */
+ while (buf2 > start2) {
+ SUB(*--buf0, *--buf1, *--buf2, carry);
+ }
+
+ /* part 3 - intg2 ... intg1 */
+ while (carry && buf1 > start1) {
+ SUB(*--buf0, *--buf1, 0, carry);
+ }
+
+ while (buf1 > start1) *--buf0 = *--buf1;
+
+ while (buf0 > to->buf) *--buf0 = 0;
+
+ return error;
+}
+
+int decimal_intg(const decimal_t *from) {
+ int res;
+ remove_leading_zeroes(from, &res);
+ return res;
+}
+
+int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to) {
+ if (likely(from1->sign == from2->sign)) return do_add(from1, from2, to);
+ return do_sub(from1, from2, to);
+}
+
+int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to) {
+ if (likely(from1->sign == from2->sign)) return do_sub(from1, from2, to);
+ return do_add(from1, from2, to);
+}
+
+int decimal_cmp(const decimal_t *from1, const decimal_t *from2) {
+ if (likely(from1->sign == from2->sign)) return do_sub(from1, from2, 0);
+
+ // Reject negative zero, cfr. string2decimal()
+ DBUG_ASSERT(!(decimal_is_zero(from1) && from1->sign));
+ DBUG_ASSERT(!(decimal_is_zero(from2) && from2->sign));
+
+ return from1->sign > from2->sign ? -1 : 1;
+}
+
+int decimal_is_zero(const decimal_t *from) {
+ dec1 *buf1 = from->buf,
+ *end = buf1 + ROUND_UP(from->intg) + ROUND_UP(from->frac);
+ while (buf1 < end)
+ if (*buf1++) return 0;
+ return 1;
+}
+
+/*
+ multiply two decimals
+
+ SYNOPSIS
+ decimal_mul()
+ from_1, from_2 - factors
+ to - product
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW;
+
+ NOTES
+ in this implementation, with sizeof(dec1)=4 we have DIG_PER_DEC1=9,
+ and 63-digit number will take only 7 dec1 words (basically a 7-digit
+ "base 999999999" number). Thus there's no need in fast multiplication
+ algorithms, 7-digit numbers can be multiplied with a naive O(n*n)
+ method.
+
+ XXX if this library is to be used with huge numbers of thousands of
+ digits, fast multiplication must be implemented.
+*/
+int decimal_mul(const decimal_t *from_1, const decimal_t *from_2,
+ decimal_t *to) {
+ if (decimal_is_zero(from_1) || decimal_is_zero(from_2)) {
+ decimal_make_zero(to);
+ return E_DEC_OK;
+ }
+ decimal_t f1 = *from_1;
+ decimal_t f2 = *from_2;
+ f1.buf = remove_leading_zeroes(&f1, &f1.intg);
+ f2.buf = remove_leading_zeroes(&f2, &f2.intg);
+
+ const decimal_t *from1 = &f1;
+ const decimal_t *from2 = &f2;
+ int intg1 = ROUND_UP(from1->intg), intg2 = ROUND_UP(from2->intg),
+ frac1 = ROUND_UP(from1->frac), frac2 = ROUND_UP(from2->frac),
+ intg0 = ROUND_UP(from1->intg + from2->intg), frac0 = frac1 + frac2, error,
+ iii, jjj, d_to_move;
+ dec1 *buf1 = from1->buf + intg1, *buf2 = from2->buf + intg2, *buf0, *start2,
+ *stop2, *stop1, *start0, carry;
+
+ sanity(to);
+
+ iii = intg0; /* save 'ideal' values */
+ jjj = frac0;
+ FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error); /* bound size */
+ to->sign = from1->sign != from2->sign;
+ to->frac = from1->frac + from2->frac; /* store size in digits */
+ set_if_smaller(to->frac, NOT_FIXED_DEC);
+ to->intg = intg0 * DIG_PER_DEC1;
+
+ if (unlikely(error)) {
+ set_if_smaller(to->frac, frac0 * DIG_PER_DEC1);
+ set_if_smaller(to->intg, intg0 * DIG_PER_DEC1);
+ if (unlikely(iii > intg0)) /* bounded integer-part */
+ {
+ iii -= intg0;
+ jjj = iii >> 1;
+ intg1 -= jjj;
+ intg2 -= iii - jjj;
+ frac1 = frac2 = 0; /* frac0 is already 0 here */
+ } else /* bounded fract part */
+ {
+ jjj -= frac0;
+ iii = jjj >> 1;
+ if (frac1 <= frac2) {
+ frac1 -= iii;
+ frac2 -= jjj - iii;
+ } else {
+ frac2 -= iii;
+ frac1 -= jjj - iii;
+ }
+ }
+ }
+ start0 = to->buf + intg0 + frac0 - 1;
+ start2 = buf2 + frac2 - 1;
+ stop1 = buf1 - intg1;
+ stop2 = buf2 - intg2;
+
+ memset(to->buf, 0, (intg0 + frac0) * sizeof(dec1));
+
+ for (buf1 += frac1 - 1; buf1 >= stop1; buf1--, start0--) {
+ carry = 0;
+ for (buf0 = start0, buf2 = start2; buf2 >= stop2; buf2--, buf0--) {
+ dec1 hi, lo;
+ dec2 p = ((dec2)*buf1) * ((dec2)*buf2);
+ hi = (dec1)(p / DIG_BASE);
+ lo = (dec1)(p - ((dec2)hi) * DIG_BASE);
+ ADD2(*buf0, *buf0, lo, carry);
+ carry += hi;
+ }
+ if (carry) {
+ if (buf0 < to->buf) return E_DEC_OVERFLOW;
+ ADD2(*buf0, *buf0, 0, carry);
+ }
+ for (buf0--; carry; buf0--) {
+ if (buf0 < to->buf) return E_DEC_OVERFLOW;
+ ADD(*buf0, *buf0, 0, carry);
+ }
+ }
+
+ /* Now we have to check for -0.000 case */
+ if (to->sign) {
+ dec1 *buf = to->buf;
+ dec1 *end = to->buf + intg0 + frac0;
+ DBUG_ASSERT(buf != end);
+ for (;;) {
+ if (*buf) break;
+ if (++buf == end) {
+ /* We got decimal zero */
+ decimal_make_zero(to);
+ break;
+ }
+ }
+ }
+ buf1 = to->buf;
+ d_to_move = intg0 + ROUND_UP(to->frac);
+ while (!*buf1 && (to->intg > DIG_PER_DEC1)) {
+ buf1++;
+ to->intg -= DIG_PER_DEC1;
+ d_to_move--;
+ }
+ if (to->buf < buf1) {
+ dec1 *cur_d = to->buf;
+ for (; d_to_move--; cur_d++, buf1++) *cur_d = *buf1;
+ }
+ return error;
+}
+
+/*
+ naive division algorithm (Knuth's Algorithm D in 4.3.1) -
+ it's ok for short numbers
+ also we're using alloca() to allocate a temporary buffer
+
+ XXX if this library is to be used with huge numbers of thousands of
+ digits, fast division must be implemented and alloca should be
+ changed to malloc (or at least fallback to malloc if alloca() fails)
+ but then, decimal_mul() should be rewritten too :(
+*/
+static int do_div_mod(const decimal_t *from1, const decimal_t *from2,
+ decimal_t *to, decimal_t *mod, int scale_incr) {
+ /*
+ frac* - number of digits in fractional part of the number
+ prec* - precision of the number
+ intg* - number of digits in the integer part
+ buf* - buffer having the actual number
+ All variables ending with 0 - like frac0, intg0 etc are
+ for the final result. Similarly frac1, intg1 etc are for
+ the first number and frac2, intg2 etc are for the second number
+ */
+ int frac1 = ROUND_UP(from1->frac) * DIG_PER_DEC1, prec1 = from1->intg + frac1,
+ frac2 = ROUND_UP(from2->frac) * DIG_PER_DEC1, prec2 = from2->intg + frac2,
+ error = 0, i, intg0, frac0, len1, len2,
+ dintg, /* Holds the estimate of number of integer digits in final result
+ */
+ div_mod = (!mod) /*true if this is division */;
+ dec1 *buf0, *buf1 = from1->buf, *buf2 = from2->buf, *start1, *stop1, *start2,
+ *stop2, *stop0, norm2, carry, dcarry, *tmp1;
+ dec2 norm_factor, x, guess, y;
+
+ if (mod) to = mod;
+
+ sanity(to);
+
+ /*
+ removing all the leading zeroes in the second number. Leading zeroes are
+ added later to the result.
+ */
+ i = ((prec2 - 1) % DIG_PER_DEC1) + 1;
+ while (prec2 > 0 && *buf2 == 0) {
+ prec2 -= i;
+ i = DIG_PER_DEC1;
+ buf2++;
+ }
+ if (prec2 <= 0) /* short-circuit everything: from2 == 0 */
+ return E_DEC_DIV_ZERO;
+
+ /*
+ Remove the remanining zeroes . For ex: for 0.000000000001
+ the above while loop removes 9 zeroes and the result will have 0.0001
+ these remaining zeroes are removed here
+ */
+ prec2 -= count_leading_zeroes((prec2 - 1) % DIG_PER_DEC1, *buf2);
+ DBUG_ASSERT(prec2 > 0);
+
+ /*
+ Do the same for the first number. Remove the leading zeroes.
+ Check if the number is actually 0. Then remove the remaining zeroes.
+ */
+
+ i = ((prec1 - 1) % DIG_PER_DEC1) + 1;
+ while (prec1 > 0 && *buf1 == 0) {
+ prec1 -= i;
+ i = DIG_PER_DEC1;
+ buf1++;
+ }
+ if (prec1 <= 0) { /* short-circuit everything: from1 == 0 */
+ decimal_make_zero(to);
+ return E_DEC_OK;
+ }
+ prec1 -= count_leading_zeroes((prec1 - 1) % DIG_PER_DEC1, *buf1);
+ DBUG_ASSERT(prec1 > 0);
+
+ /* let's fix scale_incr, taking into account frac1,frac2 increase */
+ if ((scale_incr -= frac1 - from1->frac + frac2 - from2->frac) < 0)
+ scale_incr = 0;
+
+ /* Calculate the integer digits in final result */
+ dintg = (prec1 - frac1) - (prec2 - frac2) + (*buf1 >= *buf2);
+ if (dintg < 0) {
+ dintg /= DIG_PER_DEC1;
+ intg0 = 0;
+ } else
+ intg0 = ROUND_UP(dintg);
+ if (mod) {
+ /* we're calculating N1 % N2.
+ The result will have
+ frac=max(frac1, frac2), as for subtraction
+ intg=intg2
+ */
+ to->sign = from1->sign;
+ to->frac = MY_MAX(from1->frac, from2->frac);
+ frac0 = 0;
+ } else {
+ /*
+ we're calculating N1/N2. N1 is in the buf1, has prec1 digits
+ N2 is in the buf2, has prec2 digits. Scales are frac1 and
+ frac2 accordingly.
+ Thus, the result will have
+ frac = ROUND_UP(frac1+frac2+scale_incr)
+ and
+ intg = (prec1-frac1) - (prec2-frac2) + 1
+ prec = intg+frac
+ */
+ frac0 = ROUND_UP(frac1 + frac2 + scale_incr);
+ FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
+ to->sign = from1->sign != from2->sign;
+ to->intg = intg0 * DIG_PER_DEC1;
+ to->frac = frac0 * DIG_PER_DEC1;
+ }
+ buf0 = to->buf;
+ stop0 = buf0 + intg0 + frac0;
+ if (likely(div_mod))
+ while (dintg++ < 0 && buf0 < &to->buf[to->len]) {
+ *buf0++ = 0;
+ }
+
+ len1 = (i = ROUND_UP(prec1)) + ROUND_UP(2 * frac2 + scale_incr + 1) + 1;
+ set_if_bigger(len1, 3);
+ if (!(tmp1 = (dec1 *)my_alloca(len1 * sizeof(dec1)))) return E_DEC_OOM;
+ memcpy(tmp1, buf1, i * sizeof(dec1));
+ memset(tmp1 + i, 0, (len1 - i) * sizeof(dec1));
+
+ start1 = tmp1;
+ stop1 = start1 + len1;
+ start2 = buf2;
+ stop2 = buf2 + ROUND_UP(prec2) - 1;
+
+ /* removing end zeroes */
+ while (*stop2 == 0 && stop2 >= start2) stop2--;
+ len2 = (int)(stop2++ - start2);
+
+ /*
+ calculating norm2 (normalized *start2) - we need *start2 to be large
+ (at least > DIG_BASE/2), but unlike Knuth's Alg. D we don't want to
+ normalize input numbers (as we don't make a copy of the divisor).
+ Thus we normalize first dec1 of buf2 only, and we'll normalize *start1
+ on the fly for the purpose of guesstimation only.
+ It's also faster, as we're saving on normalization of buf2
+ */
+ norm_factor = DIG_BASE / (*start2 + 1);
+ norm2 = (dec1)(norm_factor * start2[0]);
+ if (likely(len2 > 0)) norm2 += (dec1)(norm_factor * start2[1] / DIG_BASE);
+
+ if (*start1 < *start2)
+ dcarry = *start1++;
+ else
+ dcarry = 0;
+
+ /* main loop */
+ for (; buf0 < stop0; buf0++) {
+ /* short-circuit, if possible */
+ if (unlikely(dcarry == 0 && *start1 < *start2))
+ guess = 0;
+ else {
+ /* D3: make a guess */
+ x = start1[0] + ((dec2)dcarry) * DIG_BASE;
+ y = start1[1];
+ guess = (norm_factor * x + norm_factor * y / DIG_BASE) / norm2;
+ if (unlikely(guess >= DIG_BASE)) guess = DIG_BASE - 1;
+ if (likely(len2 > 0)) {
+ /* hmm, this is a suspicious trick - I removed normalization here */
+ if (start2[1] * guess > (x - guess * start2[0]) * DIG_BASE + y) guess--;
+ if (unlikely(start2[1] * guess >
+ (x - guess * start2[0]) * DIG_BASE + y))
+ guess--;
+ DBUG_ASSERT(start2[1] * guess <=
+ (x - guess * start2[0]) * DIG_BASE + y);
+ }
+
+ /* D4: multiply and subtract */
+ buf2 = stop2;
+ buf1 = start1 + len2;
+ DBUG_ASSERT(buf1 < stop1);
+ for (carry = 0; buf2 > start2; buf1--) {
+ dec1 hi, lo;
+ x = guess * (*--buf2);
+ hi = (dec1)(x / DIG_BASE);
+ lo = (dec1)(x - ((dec2)hi) * DIG_BASE);
+ SUB2(*buf1, *buf1, lo, carry);
+ carry += hi;
+ }
+ carry = dcarry < carry;
+
+ /* D5: check the remainder */
+ if (unlikely(carry)) {
+ /* D6: correct the guess */
+ guess--;
+ buf2 = stop2;
+ buf1 = start1 + len2;
+ for (carry = 0; buf2 > start2; buf1--) {
+ ADD(*buf1, *buf1, *--buf2, carry);
+ }
+ }
+ }
+ if (likely(div_mod)) {
+ DBUG_ASSERT(buf0 < to->buf + to->len);
+ *buf0 = (dec1)guess;
+ }
+ dcarry = *start1;
+ start1++;
+ }
+ if (mod) {
+ /*
+ now the result is in tmp1, it has
+ intg=prec1-frac1 if there were no leading zeroes.
+ If leading zeroes were present, they have been removed
+ earlier. We need to now add them back to the result.
+ frac=max(frac1, frac2)=to->frac
+ */
+ if (dcarry) *--start1 = dcarry;
+ buf0 = to->buf;
+ /* Calculate the final result's integer digits */
+ dintg = (prec1 - frac1) - ((start1 - tmp1) * DIG_PER_DEC1);
+ if (dintg < 0) {
+ /* If leading zeroes in the fractional part were earlier stripped */
+ intg0 = dintg / DIG_PER_DEC1;
+ } else
+ intg0 = ROUND_UP(dintg);
+ frac0 = ROUND_UP(to->frac);
+ error = E_DEC_OK;
+ if (unlikely(frac0 == 0 && intg0 == 0)) {
+ decimal_make_zero(to);
+ goto done;
+ }
+ if (intg0 <= 0) {
+ /* Add back the leading zeroes that were earlier stripped */
+ if (unlikely(-intg0 >= to->len)) {
+ decimal_make_zero(to);
+ error = E_DEC_TRUNCATED;
+ goto done;
+ }
+ stop1 = start1 + frac0 + intg0;
+ frac0 += intg0;
+ to->intg = 0;
+ while (intg0++ < 0) *buf0++ = 0;
+ } else {
+ if (unlikely(intg0 > to->len)) {
+ frac0 = 0;
+ intg0 = to->len;
+ error = E_DEC_OVERFLOW;
+ goto done;
+ }
+ DBUG_ASSERT(intg0 <= ROUND_UP(from2->intg));
+ stop1 = start1 + frac0 + intg0;
+ to->intg = MY_MIN(intg0 * DIG_PER_DEC1, from2->intg);
+ }
+ if (unlikely(intg0 + frac0 > to->len)) {
+ stop1 -= frac0 + intg0 - to->len;
+ frac0 = to->len - intg0;
+ to->frac = frac0 * DIG_PER_DEC1;
+ error = E_DEC_TRUNCATED;
+ }
+ DBUG_ASSERT(buf0 + (stop1 - start1) <= to->buf + to->len);
+ while (start1 < stop1) *buf0++ = *start1++;
+ }
+done:
+ if (decimal_is_zero(to)) {
+ // Return "0." rather than "0.000000"
+ decimal_make_zero(to);
+ } else {
+ tmp1 = remove_leading_zeroes(to, &to->intg);
+ if (to->buf != tmp1)
+ memmove(to->buf, tmp1,
+ (ROUND_UP(to->intg) + ROUND_UP(to->frac)) * sizeof(dec1));
+ }
+ DBUG_ASSERT(to->intg + to->frac > 0);
+ return error;
+}
+
+/*
+ division of two decimals
+
+ SYNOPSIS
+ decimal_div()
+ from1 - dividend
+ from2 - divisor
+ to - quotient
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
+
+ NOTES
+ see do_div_mod()
+*/
+
+int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to,
+ int scale_incr) {
+ return do_div_mod(from1, from2, to, 0, scale_incr);
+}
+
+/*
+ modulus
+
+ SYNOPSIS
+ decimal_mod()
+ from1 - dividend
+ from2 - divisor
+ to - modulus
+
+ RETURN VALUE
+ E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW/E_DEC_DIV_ZERO;
+
+ NOTES
+ see do_div_mod()
+
+ DESCRIPTION
+ the modulus R in R = M mod N
+
+ is defined as
+
+ 0 <= |R| < |M|
+ sign R == sign M
+ R = M - k*N, where k is integer
+
+ thus, there's no requirement for M or N to be integers
+*/
+
+int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to) {
+ return do_div_mod(from1, from2, 0, to, 0);
+}
diff --git a/libmysqlclient/mysql/strings/decimal.cc.orig b/libmysqlclient/mysql/strings/decimal.cc.orig
new file mode 120000
index 0000000..a315c4f
--- /dev/null
+++ b/libmysqlclient/mysql/strings/decimal.cc.orig
@@ -0,0 +1 @@
+../../../upstream/strings/decimal.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/do_ctype.cc b/libmysqlclient/mysql/strings/do_ctype.cc
new file mode 120000
index 0000000..05a5894
--- /dev/null
+++ b/libmysqlclient/mysql/strings/do_ctype.cc
@@ -0,0 +1 @@
+../../../upstream/strings/do_ctype.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/dtoa.cc b/libmysqlclient/mysql/strings/dtoa.cc
new file mode 120000
index 0000000..9dd6202
--- /dev/null
+++ b/libmysqlclient/mysql/strings/dtoa.cc
@@ -0,0 +1 @@
+../../../upstream/strings/dtoa.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/dump_map.cc b/libmysqlclient/mysql/strings/dump_map.cc
new file mode 120000
index 0000000..ee16fe3
--- /dev/null
+++ b/libmysqlclient/mysql/strings/dump_map.cc
@@ -0,0 +1 @@
+../../../upstream/strings/dump_map.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/int2str.cc b/libmysqlclient/mysql/strings/int2str.cc
new file mode 120000
index 0000000..817365c
--- /dev/null
+++ b/libmysqlclient/mysql/strings/int2str.cc
@@ -0,0 +1 @@
+../../../upstream/strings/int2str.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/longlong2str.cc b/libmysqlclient/mysql/strings/longlong2str.cc
new file mode 120000
index 0000000..ac8e05e
--- /dev/null
+++ b/libmysqlclient/mysql/strings/longlong2str.cc
@@ -0,0 +1 @@
+../../../upstream/strings/longlong2str.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/mb_wc.h b/libmysqlclient/mysql/strings/mb_wc.h
new file mode 120000
index 0000000..447bf27
--- /dev/null
+++ b/libmysqlclient/mysql/strings/mb_wc.h
@@ -0,0 +1 @@
+../../../upstream/strings/mb_wc.h \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/my_strchr.cc b/libmysqlclient/mysql/strings/my_strchr.cc
new file mode 120000
index 0000000..54a37cb
--- /dev/null
+++ b/libmysqlclient/mysql/strings/my_strchr.cc
@@ -0,0 +1 @@
+../../../upstream/strings/my_strchr.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/my_strtoll10.cc b/libmysqlclient/mysql/strings/my_strtoll10.cc
new file mode 120000
index 0000000..435e12c
--- /dev/null
+++ b/libmysqlclient/mysql/strings/my_strtoll10.cc
@@ -0,0 +1 @@
+../../../upstream/strings/my_strtoll10.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/str2int.cc b/libmysqlclient/mysql/strings/str2int.cc
new file mode 120000
index 0000000..8444367
--- /dev/null
+++ b/libmysqlclient/mysql/strings/str2int.cc
@@ -0,0 +1 @@
+../../../upstream/strings/str2int.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/str_alloc.cc b/libmysqlclient/mysql/strings/str_alloc.cc
new file mode 120000
index 0000000..142c893
--- /dev/null
+++ b/libmysqlclient/mysql/strings/str_alloc.cc
@@ -0,0 +1 @@
+../../../upstream/strings/str_alloc.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/str_uca_type.h b/libmysqlclient/mysql/strings/str_uca_type.h
new file mode 120000
index 0000000..d3e0095
--- /dev/null
+++ b/libmysqlclient/mysql/strings/str_uca_type.h
@@ -0,0 +1 @@
+../../../upstream/strings/str_uca_type.h \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/strcont.cc b/libmysqlclient/mysql/strings/strcont.cc
new file mode 120000
index 0000000..c6bef3c
--- /dev/null
+++ b/libmysqlclient/mysql/strings/strcont.cc
@@ -0,0 +1 @@
+../../../upstream/strings/strcont.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/strmake.cc b/libmysqlclient/mysql/strings/strmake.cc
new file mode 120000
index 0000000..3989f56
--- /dev/null
+++ b/libmysqlclient/mysql/strings/strmake.cc
@@ -0,0 +1 @@
+../../../upstream/strings/strmake.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/strxmov.cc b/libmysqlclient/mysql/strings/strxmov.cc
new file mode 120000
index 0000000..9bdae84
--- /dev/null
+++ b/libmysqlclient/mysql/strings/strxmov.cc
@@ -0,0 +1 @@
+../../../upstream/strings/strxmov.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/strxnmov.cc b/libmysqlclient/mysql/strings/strxnmov.cc
new file mode 120000
index 0000000..b709006
--- /dev/null
+++ b/libmysqlclient/mysql/strings/strxnmov.cc
@@ -0,0 +1 @@
+../../../upstream/strings/strxnmov.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/t_ctype.h b/libmysqlclient/mysql/strings/t_ctype.h
new file mode 120000
index 0000000..4365e75
--- /dev/null
+++ b/libmysqlclient/mysql/strings/t_ctype.h
@@ -0,0 +1 @@
+../../../upstream/strings/t_ctype.h \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/uca-dump.cc b/libmysqlclient/mysql/strings/uca-dump.cc
new file mode 120000
index 0000000..1cf0c98
--- /dev/null
+++ b/libmysqlclient/mysql/strings/uca-dump.cc
@@ -0,0 +1 @@
+../../../upstream/strings/uca-dump.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/uca9-dump.cc b/libmysqlclient/mysql/strings/uca9-dump.cc
new file mode 120000
index 0000000..29a6944
--- /dev/null
+++ b/libmysqlclient/mysql/strings/uca9-dump.cc
@@ -0,0 +1 @@
+../../../upstream/strings/uca9-dump.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/uca900_data.h b/libmysqlclient/mysql/strings/uca900_data.h
new file mode 120000
index 0000000..aa69600
--- /dev/null
+++ b/libmysqlclient/mysql/strings/uca900_data.h
@@ -0,0 +1 @@
+../../../upstream/strings/uca900_data.h \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/uca900_ja_data.h b/libmysqlclient/mysql/strings/uca900_ja_data.h
new file mode 120000
index 0000000..6a88409
--- /dev/null
+++ b/libmysqlclient/mysql/strings/uca900_ja_data.h
@@ -0,0 +1 @@
+../../../upstream/strings/uca900_ja_data.h \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/uca_data.h b/libmysqlclient/mysql/strings/uca_data.h
new file mode 120000
index 0000000..3281baf
--- /dev/null
+++ b/libmysqlclient/mysql/strings/uca_data.h
@@ -0,0 +1 @@
+../../../upstream/strings/uca_data.h \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/uctypedump.cc b/libmysqlclient/mysql/strings/uctypedump.cc
new file mode 120000
index 0000000..72391ea
--- /dev/null
+++ b/libmysqlclient/mysql/strings/uctypedump.cc
@@ -0,0 +1 @@
+../../../upstream/strings/uctypedump.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/utr11-dump.cc b/libmysqlclient/mysql/strings/utr11-dump.cc
new file mode 120000
index 0000000..5e422b8
--- /dev/null
+++ b/libmysqlclient/mysql/strings/utr11-dump.cc
@@ -0,0 +1 @@
+../../../upstream/strings/utr11-dump.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/strings/xml.cc b/libmysqlclient/mysql/strings/xml.cc
new file mode 120000
index 0000000..2f0a38e
--- /dev/null
+++ b/libmysqlclient/mysql/strings/xml.cc
@@ -0,0 +1 @@
+../../../upstream/strings/xml.cc \ No newline at end of file
diff --git a/libmysqlclient/mysql/version.h.in b/libmysqlclient/mysql/version.h.in
new file mode 120000
index 0000000..9c69727
--- /dev/null
+++ b/libmysqlclient/mysql/version.h.in
@@ -0,0 +1 @@
+mysql/mysql_version.h.in \ No newline at end of file
diff --git a/libmysqlclient/tests/basic/driver.c b/libmysqlclient/tests/basic/driver.c
index 3e2a1f0..498a4b2 100644
--- a/libmysqlclient/tests/basic/driver.c
+++ b/libmysqlclient/tests/basic/driver.c
@@ -1,5 +1,4 @@
-/*
- * file : tests/basic/driver.c
+/* file : tests/basic/driver.c
* license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
*/
diff --git a/mysql-client/.gitignore b/mysql-client/.gitignore
new file mode 100644
index 0000000..1c363a0
--- /dev/null
+++ b/mysql-client/.gitignore
@@ -0,0 +1,25 @@
+# Compiler/linker output.
+#
+*.d
+*.t
+*.i
+*.i.*
+*.ii
+*.ii.*
+*.o
+*.obj
+*.gcm
+*.pcm
+*.ifc
+*.so
+*.dylib
+*.dll
+*.a
+*.lib
+*.exp
+*.pdb
+*.ilk
+*.exe
+*.exe.dlls/
+*.exe.manifest
+*.pc
diff --git a/mysql-client/INSTALL b/mysql-client/INSTALL
new file mode 100644
index 0000000..235e581
--- /dev/null
+++ b/mysql-client/INSTALL
@@ -0,0 +1,7 @@
+The aim of this package is to make reading the INSTALL file unnecessary. So
+next time try running:
+
+$ bpkg build mysql-client
+
+But if you don't want to use the package manager, then you can also build this
+package manually using the standard build2 build system.
diff --git a/mysql-client/LICENSE b/mysql-client/LICENSE
new file mode 120000
index 0000000..6246057
--- /dev/null
+++ b/mysql-client/LICENSE
@@ -0,0 +1 @@
+../upstream/LICENSE \ No newline at end of file
diff --git a/mysql-client/README b/mysql-client/README
new file mode 100644
index 0000000..f2a6666
--- /dev/null
+++ b/mysql-client/README
@@ -0,0 +1,21 @@
+MySQL is a relational SQL database management system with mysql utility being
+its command-line client. It supports interactive and noninteractive use. When
+used interactively, query results are presented in an ASCII-table format. When
+used noninteractively, the result is presented in tab-separated format. For
+more information see:
+
+https://www.mysql.com
+
+This package contains the original mysql utility source code overlaid with the
+build2-based build system and packaged for the build2 package manager (bpkg).
+
+See the INSTALL file for the prerequisites and installation instructions.
+
+Send questions, bug reports, or any other feedback about the utility itself to
+the MySQL mailing lists. Send build system and packaging-related feedback to
+the packaging@build2.org mailing list (see https://lists.build2.org for
+posting guidelines, etc).
+
+The packaging of mysql utility for build2 is tracked in a Git repository at:
+
+https://git.build2.org/cgit/packaging/mysql/
diff --git a/mysql-client/README-DEV b/mysql-client/README-DEV
new file mode 100644
index 0000000..6259c46
--- /dev/null
+++ b/mysql-client/README-DEV
@@ -0,0 +1,15 @@
+This document describes how the mysql utility was packaged for build2. In
+particular, this understanding will be useful when upgrading to a new upstream
+version. See ../README-DEV for general notes on MySQL packaging.
+
+Symlink the required upstream components:
+
+$ ln -s ../upstream/LICENSE
+
+$ mkdir -p mysql/{client,downstream}
+$ ln -s ../../../upstream/client/{{mysql,readline,completion_hash,pattern_matcher}.cc,{client_priv,my_readline,pattern_matcher,completion_hash}.h} mysql/client
+$ ln -s ../../libmysqlclient/mysql/{my_config,mysqld_error}.h mysql/
+$ ln -s ../../upstream/{include,libbinlogevents} mysql/
+
+Create mysql/downstream/mysql_version.h, so it can be found when included as
+"mysql_version.h" (which, for example, mysql.h does).
diff --git a/mysql-client/build/.gitignore b/mysql-client/build/.gitignore
new file mode 100644
index 0000000..4a730a3
--- /dev/null
+++ b/mysql-client/build/.gitignore
@@ -0,0 +1,3 @@
+config.build
+root/
+bootstrap/
diff --git a/mysql-client/build/bootstrap.build b/mysql-client/build/bootstrap.build
new file mode 100644
index 0000000..4d085a3
--- /dev/null
+++ b/mysql-client/build/bootstrap.build
@@ -0,0 +1,10 @@
+# file : build/bootstrap.build
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+project = mysql-client
+
+using version
+using config
+using dist
+using test
+using install
diff --git a/mysql-client/build/export.build b/mysql-client/build/export.build
new file mode 100644
index 0000000..3cef2f9
--- /dev/null
+++ b/mysql-client/build/export.build
@@ -0,0 +1,9 @@
+# file : build/export.build
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+$out_root/
+{
+ include mysql/
+}
+
+export $out_root/mysql/$import.target
diff --git a/mysql-client/build/root.build b/mysql-client/build/root.build
new file mode 100644
index 0000000..077c13f
--- /dev/null
+++ b/mysql-client/build/root.build
@@ -0,0 +1,25 @@
+# file : build/root.build
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+using in
+
+using c
+
+h{*}: extension = h
+c{*}: extension = c
+
+# Note that the implementation uses C++14 internally, with some used
+# constructs being deprecated/removed from the later versions of the standard.
+#
+cxx.std = 14
+
+using cxx
+
+hxx{*}: extension = h
+cxx{*}: extension = cc
+
+if ($c.target.system == 'win32-msvc')
+ cc.poptions += -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS
+
+if ($c.class == 'msvc')
+ cc.coptions += /wd4251 /wd4275 /wd4800
diff --git a/mysql-client/buildfile b/mysql-client/buildfile
new file mode 100644
index 0000000..fc7acee
--- /dev/null
+++ b/mysql-client/buildfile
@@ -0,0 +1,9 @@
+# file : buildfile
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+./: {*/ -build/} doc{INSTALL README} legal{LICENSE} manifest
+
+# Don't install tests or the INSTALL file.
+#
+tests/: install = false
+doc{INSTALL}@./: install = false
diff --git a/mysql-client/manifest b/mysql-client/manifest
new file mode 100644
index 0000000..a9fc220
--- /dev/null
+++ b/mysql-client/manifest
@@ -0,0 +1,35 @@
+: 1
+name: mysql-client
+
+# Note: remember to update doc-url and debian-name below!
+#
+version: 8.0.15+15
+
+project: mysql
+summary: MySQL command-line client
+license: other: GPL-2.0-only with MySQL Universal FOSS Exception
+topics: MySQL, SQL, relational database
+description-file: README
+url: https://www.mysql.com
+doc-url: https://dev.mysql.com/doc/refman/8.0/en/mysql.html
+src-url: https://git.build2.org/cgit/packaging/mysql/mysql/tree/mysql-client/
+package-url: https://git.build2.org/cgit/packaging/mysql/
+email: mysql@lists.mysql.com ; Mailing list.
+package-email: packaging@build2.org ; Mailing list.
+build-error-email: builds@build2.org
+builds: all
+builds: -wasm
+builds: -( +windows &gcc ) ; MinGW GCC is not supported.
+builds: -static ; Implementation uses C++ and requires special linking steps.
+depends: * build2 >= 0.15.0
+depends: * bpkg >= 0.15.0
+depends: libmysqlclient == $
+depends: libcrypto ^1.1.1
+depends: libisocline >= 1.0.9 ? ($c.target.class != 'windows')
+
+# System package mapping.
+#
+debian-name: mysql-client-core-8.0
+fedora-name: community-mysql
+rhel-name: mysql
+centos-name: mysql
diff --git a/mysql-client/mysql/.gitattributes b/mysql-client/mysql/.gitattributes
new file mode 100644
index 0000000..d84f83a
--- /dev/null
+++ b/mysql-client/mysql/.gitattributes
@@ -0,0 +1,2 @@
+include symlink=dir
+libbinlogevents symlink=dir
diff --git a/mysql-client/mysql/.gitignore b/mysql-client/mysql/.gitignore
new file mode 100644
index 0000000..0416ee9
--- /dev/null
+++ b/mysql-client/mysql/.gitignore
@@ -0,0 +1 @@
+/mysql
diff --git a/mysql-client/mysql/buildfile b/mysql-client/mysql/buildfile
new file mode 100644
index 0000000..921bff5
--- /dev/null
+++ b/mysql-client/mysql/buildfile
@@ -0,0 +1,93 @@
+# file : mysql/buildfile
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+tclass = $c.target.class
+
+windows = ($tclass == 'windows')
+
+# Note that the mysql utility source code directly refers to the
+# libmysqlclient's implementations details quite a lot. Thus, we just use the
+# libmysqlclient static library in combination with the private headers. Also
+# note that the upstream's mysql utility doesn't link the shared
+# libmysqlclient either.
+#
+import libs = libmysqlclient%liba{mysqlclient}
+
+# Note that the mysql utility doesn't use openssl directly. We however import
+# libcrypto so that the included by some common headers <openssl/opensslv.h>
+# can be found. Also note that we link it statically to be consistent with
+# libssl.a, linked implicitly via libmysqlclient.a.
+#
+import libs += libcrypto%liba{crypto}
+
+if! $windows
+ import libs += libisocline%liba{isocline}
+
+# Note that some of the include/**.h files are C++ headers and some are C
+# headers. We will assume all of them as C headers, which is a bit of a hack
+# but doing it properly is too painful.
+#
+exe{mysql}: {h c }{* } \
+ client/{ hxx cxx}{* } \
+ downstream/{h c }{* -readline} \
+ include/{h }{** } \
+libbinlogevents/{h }{** }
+
+exe{mysql}: downstream/{h c}{readline}: include = (!$windows)
+
+exe{mysql}: $libs
+
+# Build options.
+#
+# We have dropped the macro definitions that are not used in the package code:
+#
+# -DRAPIDJSON_NO_SIZETYPEDEFINE
+# -DUNISTR_FROM_CHAR_EXPLICIT=explicit
+# -DUNISTR_FROM_STRING_EXPLICIT=explicit
+# -DHAVE_LIBEVENT2
+# -DNO_FSEEKO
+#
+cc.poptions += -DHAVE_CONFIG_H \
+ -DHAVE_OPENSSL \
+ -D__STDC_LIMIT_MACROS \
+ -D__STDC_FORMAT_MACROS \
+ -D_USE_MATH_DEFINES \
+ -DDBUG_OFF
+
+switch $tclass
+{
+ case 'windows'
+ cc.poptions += -DWIN32 -D_WINDOWS -D_WIN32_WINNT=0x0601 -DNOGDI \
+ -DNOMINMAX -DWIN32_LEAN_AND_MEAN -D_MBCS
+
+ case 'linux'
+ cc.poptions += -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
+
+ default
+ cc.poptions += -D_FILE_OFFSET_BITS=64
+}
+
+# Note that we add "-I$src_root" for the headers auto-generating machinery to
+# work properly.
+#
+cc.poptions =+ "-I$out_root" "-I$src_root" \
+ "-I$src_base/downstream" \
+ "-I$src_base/include" \
+ "-I$src_base/libbinlogevents/export" \
+ "-I$src_base"
+
+switch $c.class
+{
+ case 'gcc'
+ {
+ cc.coptions += -ffunction-sections -fdata-sections -fno-omit-frame-pointer
+ }
+ case 'msvc'
+ {
+ cc.coptions += /GS /fp:precise /Zc:wchar_t /Zc:forScope
+
+ # Disable warnings that pop up with /W3.
+ #
+ cc.coptions += /wd4996
+ }
+}
diff --git a/mysql-client/mysql/client/client_priv.h b/mysql-client/mysql/client/client_priv.h
new file mode 120000
index 0000000..3fbd2c6
--- /dev/null
+++ b/mysql-client/mysql/client/client_priv.h
@@ -0,0 +1 @@
+../../../upstream/client/client_priv.h \ No newline at end of file
diff --git a/mysql-client/mysql/client/completion_hash.cc b/mysql-client/mysql/client/completion_hash.cc
new file mode 120000
index 0000000..908a80f
--- /dev/null
+++ b/mysql-client/mysql/client/completion_hash.cc
@@ -0,0 +1 @@
+../../../upstream/client/completion_hash.cc \ No newline at end of file
diff --git a/mysql-client/mysql/client/completion_hash.h b/mysql-client/mysql/client/completion_hash.h
new file mode 120000
index 0000000..bb56925
--- /dev/null
+++ b/mysql-client/mysql/client/completion_hash.h
@@ -0,0 +1 @@
+../../../upstream/client/completion_hash.h \ No newline at end of file
diff --git a/mysql-client/mysql/client/my_readline.h b/mysql-client/mysql/client/my_readline.h
new file mode 120000
index 0000000..7056e16
--- /dev/null
+++ b/mysql-client/mysql/client/my_readline.h
@@ -0,0 +1 @@
+../../../upstream/client/my_readline.h \ No newline at end of file
diff --git a/mysql-client/mysql/client/mysql.cc b/mysql-client/mysql/client/mysql.cc
new file mode 120000
index 0000000..89983e9
--- /dev/null
+++ b/mysql-client/mysql/client/mysql.cc
@@ -0,0 +1 @@
+../../../upstream/client/mysql.cc \ No newline at end of file
diff --git a/mysql-client/mysql/client/pattern_matcher.cc b/mysql-client/mysql/client/pattern_matcher.cc
new file mode 120000
index 0000000..1b84867
--- /dev/null
+++ b/mysql-client/mysql/client/pattern_matcher.cc
@@ -0,0 +1 @@
+../../../upstream/client/pattern_matcher.cc \ No newline at end of file
diff --git a/mysql-client/mysql/client/pattern_matcher.h b/mysql-client/mysql/client/pattern_matcher.h
new file mode 120000
index 0000000..770da27
--- /dev/null
+++ b/mysql-client/mysql/client/pattern_matcher.h
@@ -0,0 +1 @@
+../../../upstream/client/pattern_matcher.h \ No newline at end of file
diff --git a/mysql-client/mysql/client/readline.cc b/mysql-client/mysql/client/readline.cc
new file mode 120000
index 0000000..d41de28
--- /dev/null
+++ b/mysql-client/mysql/client/readline.cc
@@ -0,0 +1 @@
+../../../upstream/client/readline.cc \ No newline at end of file
diff --git a/mysql-client/mysql/config.h b/mysql-client/mysql/config.h
new file mode 100644
index 0000000..0e71bcf
--- /dev/null
+++ b/mysql-client/mysql/config.h
@@ -0,0 +1,11 @@
+/*
+ * file : mysql/config.h
+ * license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+ */
+
+/* This file is included by my_config.h, which is symlinked from the
+ * libmysqlclient's source tree. In contrast to libmysqlclient, which
+ * generates this file based on the upstream's template, we can keep it empty
+ * since the mysql utility's source code don't use any configuration macros
+ * besides those defined in my_config.h.
+ */
diff --git a/mysql-client/mysql/downstream/mysql_version.h b/mysql-client/mysql/downstream/mysql_version.h
new file mode 100644
index 0000000..fd4471b
--- /dev/null
+++ b/mysql-client/mysql/downstream/mysql_version.h
@@ -0,0 +1,5 @@
+/* file : mysql/downstream/mysql_version.h
+ * license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+ */
+
+#include <mysql/mysql_version.h>
diff --git a/mysql-client/mysql/downstream/readline.c b/mysql-client/mysql/downstream/readline.c
new file mode 100644
index 0000000..7522be5
--- /dev/null
+++ b/mysql-client/mysql/downstream/readline.c
@@ -0,0 +1,87 @@
+/* file : mysql/downstream/readline.h
+ * license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+ */
+
+#include <downstream/readline.h>
+
+#include <isocline.h>
+
+char* rl_readline_name = NULL;
+rl_completion_func_t* rl_attempted_completion_function = NULL;
+rl_compentry_func_t* rl_completion_entry_function = NULL;
+
+int history_length = 0;
+
+/* Perform some basic libisocline initialization on the first readline() call.
+ */
+static int init = 1;
+
+#define UNUSED(x) (void)(x)
+
+char*
+readline (const char* prompt)
+{
+ if (init)
+ {
+ init = 0;
+
+ ic_set_prompt_marker("" /* prompt_marker */,
+ NULL /* continuation_prompt_marker */);
+
+ }
+
+ return ic_readline (prompt);
+}
+
+int
+add_history (const char* command)
+{
+ UNUSED (command);
+ return 1; /* Unsupported */
+}
+
+int
+read_history (const char* file)
+{
+ UNUSED (file);
+ return 1; /* Unsupported */
+}
+
+int
+write_history (const char* command)
+{
+ UNUSED (command);
+ return 1; /* Unsupported */
+}
+
+HIST_ENTRY*
+history_get (int offset)
+{
+ UNUSED (offset);
+ return NULL;
+}
+
+int
+rl_insert (int count, int chr)
+{
+ UNUSED (count);
+ UNUSED (chr);
+ return 1; /* Unsupported */
+}
+
+int
+rl_add_defun (const char* name, rl_command_func_t* f, int chr)
+{
+ UNUSED (name);
+ UNUSED (f);
+ UNUSED (chr);
+ return 1; /* Unsupported */
+}
+
+char**
+rl_completion_matches (const char* str, rl_compentry_func_t* f)
+{
+ UNUSED (str);
+ UNUSED (f);
+ return NULL;
+}
diff --git a/mysql-client/mysql/downstream/readline.h b/mysql-client/mysql/downstream/readline.h
new file mode 100644
index 0000000..1540200
--- /dev/null
+++ b/mysql-client/mysql/downstream/readline.h
@@ -0,0 +1,59 @@
+/* file : mysql/downstream/readline.h
+ * license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+ */
+
+#ifndef MYSQL_DOWNSTREAM_READLINE_H
+#define MYSQL_DOWNSTREAM_READLINE_H
+
+/*
+ * Reduced implementation of the libedit library based on libisocline.
+ *
+ * Specially, only the basic line editing functionality is supported (no
+ * completion, history, etc).
+ */
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Thin wrapper around libisocline's ic_readline().
+ */
+char*
+readline (const char* prompt);
+
+/*
+ * Reading function stubs.
+ */
+typedef char** rl_completion_func_t (const char*, int, int);
+typedef char* rl_compentry_func_t (const char*, int);
+typedef int rl_command_func_t (int, int);
+typedef char* rl_compentry_func_t (const char*, int);
+
+int rl_insert (int, int);
+int rl_add_defun (const char*, rl_command_func_t*, int);
+char** rl_completion_matches (const char*, rl_compentry_func_t*);
+
+extern char* rl_readline_name;
+extern rl_completion_func_t* rl_attempted_completion_function;
+extern rl_compentry_func_t* rl_completion_entry_function;
+
+/*
+ * Hstory function stubs.
+ */
+typedef struct _hist_entry {
+ const char *line;
+ const char *data;
+} HIST_ENTRY;
+
+int add_history (const char*);
+int read_history (const char*);
+int write_history (const char*);
+HIST_ENTRY* history_get (int);
+
+extern int history_length;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* MYSQL_DOWNSTREAM_READLINE_H */
diff --git a/mysql-client/mysql/include b/mysql-client/mysql/include
new file mode 120000
index 0000000..01fbb48
--- /dev/null
+++ b/mysql-client/mysql/include
@@ -0,0 +1 @@
+../../upstream/include \ No newline at end of file
diff --git a/mysql-client/mysql/libbinlogevents b/mysql-client/mysql/libbinlogevents
new file mode 120000
index 0000000..2a2879b
--- /dev/null
+++ b/mysql-client/mysql/libbinlogevents
@@ -0,0 +1 @@
+../../upstream/libbinlogevents \ No newline at end of file
diff --git a/mysql-client/mysql/my_config.h b/mysql-client/mysql/my_config.h
new file mode 120000
index 0000000..168261b
--- /dev/null
+++ b/mysql-client/mysql/my_config.h
@@ -0,0 +1 @@
+../../libmysqlclient/mysql/my_config.h \ No newline at end of file
diff --git a/mysql-client/mysql/mysqld_error.h b/mysql-client/mysql/mysqld_error.h
new file mode 120000
index 0000000..74d62ad
--- /dev/null
+++ b/mysql-client/mysql/mysqld_error.h
@@ -0,0 +1 @@
+../../libmysqlclient/mysql/mysqld_error.h \ No newline at end of file
diff --git a/mysql-client/tests/.gitignore b/mysql-client/tests/.gitignore
new file mode 100644
index 0000000..ce6eed8
--- /dev/null
+++ b/mysql-client/tests/.gitignore
@@ -0,0 +1,4 @@
+# Testscript output directories (can be symlinks).
+#
+test
+test-*
diff --git a/mysql-client/tests/build/.gitignore b/mysql-client/tests/build/.gitignore
new file mode 100644
index 0000000..4a730a3
--- /dev/null
+++ b/mysql-client/tests/build/.gitignore
@@ -0,0 +1,3 @@
+config.build
+root/
+bootstrap/
diff --git a/mysql-client/tests/build/bootstrap.build b/mysql-client/tests/build/bootstrap.build
new file mode 100644
index 0000000..c2ad98f
--- /dev/null
+++ b/mysql-client/tests/build/bootstrap.build
@@ -0,0 +1,8 @@
+# file : tests/build/bootstrap.build
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+project = # Unnamed tests subproject.
+
+using config
+using test
+using dist
diff --git a/mysql-client/tests/build/root.build b/mysql-client/tests/build/root.build
new file mode 100644
index 0000000..4a6bd2c
--- /dev/null
+++ b/mysql-client/tests/build/root.build
@@ -0,0 +1,15 @@
+# file : tests/build/root.build
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+# We need to configure C module to obtain the target information (see below).
+#
+using c.config
+
+# Setup the mysql program that we are testing.
+#
+import mysql = mysql-client%exe{mysql}
+testscript{*}: test = $mysql
+
+# Specify the test target for cross-testing.
+#
+test.target = $c.target
diff --git a/mysql-client/tests/buildfile b/mysql-client/tests/buildfile
new file mode 100644
index 0000000..2d136c5
--- /dev/null
+++ b/mysql-client/tests/buildfile
@@ -0,0 +1,4 @@
+# file : tests/buildfile
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+./: testscript{*} $mysql
diff --git a/mysql-client/tests/help.testscript b/mysql-client/tests/help.testscript
new file mode 100644
index 0000000..67a9331
--- /dev/null
+++ b/mysql-client/tests/help.testscript
@@ -0,0 +1,12 @@
+# file : tests/help.testscript
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+: basics
+:
+: Test that stdout contains at least one non-empty line.
+:
+$* --help >>~%EOO%
+ %.*
+ %.+%
+ %.*
+ EOO
diff --git a/mysql-client/tests/version.testscript b/mysql-client/tests/version.testscript
new file mode 100644
index 0000000..ce45628
--- /dev/null
+++ b/mysql-client/tests/version.testscript
@@ -0,0 +1,8 @@
+# file : tests/version.testscript
+# license : GPLv2 with Universal FOSS Exception; see accompanying LICENSE file
+
+: basics
+:
+$* --version >>~%EOO%
+ %.*mysql.+%
+ EOO
diff --git a/packages.manifest b/packages.manifest
index d73d77a..fa972fd 100644
--- a/packages.manifest
+++ b/packages.manifest
@@ -1,2 +1,4 @@
: 1
location: libmysqlclient/
+:
+location: mysql-client/
diff --git a/repositories.manifest b/repositories.manifest
index b497312..d61e2dd 100644
--- a/repositories.manifest
+++ b/repositories.manifest
@@ -8,3 +8,7 @@ location: ../../openssl/openssl.git##HEAD
:
role: prerequisite
location: ../../zlib/zlib.git##HEAD
+
+:
+role: prerequisite
+location: https://github.com/build2-packaging/isocline.git##HEAD