@@ -2306,6 +2306,58 @@ bool THD::convert_string(LEX_STRING *to, CHARSET_INFO *to_cs,
23062306}
23072307
23082308
2309+ /*
2310+ Reinterpret a binary string to a character string
2311+
2312+ @param[OUT] to The result will be written here,
2313+ either the original string as is,
2314+ or a newly alloced fixed string with
2315+ some zero bytes prepended.
2316+ @param cs The destination character set
2317+ @param str The binary string
2318+ @param length The length of the binary string
2319+
2320+ @return false on success
2321+ @return true on error
2322+ */
2323+
2324+ bool THD::reinterpret_string_from_binary (LEX_CSTRING *to, CHARSET_INFO *cs,
2325+ const char *str, size_t length)
2326+ {
2327+ /*
2328+ When reinterpreting from binary to tricky character sets like
2329+ UCS2, UTF16, UTF32, we may need to prepend some zero bytes.
2330+ This is possible in scenarios like this:
2331+ SET COLLATION_CONNECTION=utf32_general_ci, CHARACTER_SET_CLIENT=binary;
2332+ This code is similar to String::copy_aligned().
2333+ */
2334+ size_t incomplete= length % cs->mbminlen ; // Bytes in an incomplete character
2335+ if (incomplete)
2336+ {
2337+ size_t zeros= cs->mbminlen - incomplete;
2338+ size_t aligned_length= zeros + length;
2339+ char *dst= (char *) alloc (aligned_length + 1 );
2340+ if (!dst)
2341+ {
2342+ to->str = NULL ; // Safety
2343+ to->length = 0 ;
2344+ return true ;
2345+ }
2346+ bzero (dst, zeros);
2347+ memcpy (dst + zeros, str, length);
2348+ dst[aligned_length]= ' \0 ' ;
2349+ to->str = dst;
2350+ to->length = aligned_length;
2351+ }
2352+ else
2353+ {
2354+ to->str = str;
2355+ to->length = length;
2356+ }
2357+ return check_string_for_wellformedness (to->str , to->length , cs);
2358+ }
2359+
2360+
23092361/*
23102362 Convert a string between two character sets.
23112363 dstcs and srccs cannot be &my_charset_bin.
0 commit comments