Move length-encoded integer/string functions to maxsql

This commit is contained in:
Esa Korhonen
2018-11-29 14:41:10 +02:00
parent 5e03ff35eb
commit 655e5fab5b
12 changed files with 170 additions and 173 deletions

View File

@ -19,17 +19,6 @@
#include <maxscale/protocol/mysql.hh> #include <maxscale/protocol/mysql.hh>
#include <maxscale/server.hh> #include <maxscale/server.hh>
MXS_BEGIN_DECLS
/** Length-encoded integers */
size_t mxs_leint_bytes(const uint8_t* ptr);
uint64_t mxs_leint_value(const uint8_t* c);
uint64_t mxs_leint_consume(uint8_t** c);
/** Length-encoded strings */
char* mxs_lestr_consume_dup(uint8_t** c);
char* mxs_lestr_consume(uint8_t** c, size_t* size);
/** /**
* Creates a connection to a MySQL database engine. If necessary, initializes SSL. * Creates a connection to a MySQL database engine. If necessary, initializes SSL.
* *
@ -141,5 +130,3 @@ std::unique_ptr<mxq::QueryResult> execute_query(MYSQL* conn, const std::string&
std::string* errmsg_out = nullptr, std::string* errmsg_out = nullptr,
unsigned int* errno_out = nullptr); unsigned int* errno_out = nullptr);
} }
MXS_END_DECLS

View File

@ -211,4 +211,14 @@ private:
mutable ConversionError m_error; /**< Error information */ mutable ConversionError m_error; /**< Error information */
std::unordered_map<std::string, int64_t> m_col_indexes; /**< Map of column name -> index */ std::unordered_map<std::string, int64_t> m_col_indexes; /**< Map of column name -> index */
}; };
/** Length-encoded integers */
size_t leint_bytes(const uint8_t* ptr);
uint64_t leint_value(const uint8_t* c);
uint64_t leint_consume(uint8_t** c);
/** Length-encoded strings */
char* lestr_consume_dup(uint8_t** c);
char* lestr_consume(uint8_t** c, size_t* size);
} }

View File

@ -17,6 +17,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <maxsql/mariadb.hh>
#include <maxscale/buffer.hh> #include <maxscale/buffer.hh>
#include <maxscale/mysql_utils.hh> #include <maxscale/mysql_utils.hh>
#include <maxscale/protocol/mysql.hh> #include <maxscale/protocol/mysql.hh>
@ -40,7 +41,7 @@ public:
*/ */
LEncInt(uint8_t* pData) LEncInt(uint8_t* pData)
{ {
m_value = mxs_leint_value(pData); m_value = leint_value(pData);
} }
/** /**
@ -52,8 +53,8 @@ public:
*/ */
LEncInt(uint8_t** ppData) LEncInt(uint8_t** ppData)
{ {
size_t nBytes = mxs_leint_bytes(*ppData); size_t nBytes = leint_bytes(*ppData);
m_value = mxs_leint_value(*ppData); m_value = leint_value(*ppData);
*ppData += nBytes; *ppData += nBytes;
} }
@ -229,7 +230,7 @@ public:
// NULL is sent as 0xfb. See https://dev.mysql.com/doc/internals/en/com-query-response.html // NULL is sent as 0xfb. See https://dev.mysql.com/doc/internals/en/com-query-response.html
if (*pData != 0xfb) if (*pData != 0xfb)
{ {
m_pString = mxs_lestr_consume(&pData, &m_length); m_pString = lestr_consume(&pData, &m_length);
} }
else else
{ {
@ -250,7 +251,7 @@ public:
// NULL is sent as 0xfb. See https://dev.mysql.com/doc/internals/en/com-query-response.html // NULL is sent as 0xfb. See https://dev.mysql.com/doc/internals/en/com-query-response.html
if (**ppData != 0xfb) if (**ppData != 0xfb)
{ {
m_pString = mxs_lestr_consume(ppData, &m_length); m_pString = lestr_consume(ppData, &m_length);
} }
else else
{ {

View File

@ -15,6 +15,7 @@
#include <errmsg.h> #include <errmsg.h>
#include <string> #include <string>
#include <string.h> #include <string.h>
#include <maxbase/alloc.h>
#include <maxbase/assert.h> #include <maxbase/assert.h>
#include <maxbase/format.hh> #include <maxbase/format.hh>
@ -333,4 +334,121 @@ bool QueryResult::ConversionError::error() const
{ {
return !m_target_type.empty(); return !m_target_type.empty();
} }
/**
* @brief Calculate the length of a length-encoded integer in bytes
*
* @param ptr Start of the length encoded value
* @return Number of bytes before the actual value
*/
size_t leint_bytes(const uint8_t* ptr)
{
uint8_t val = *ptr;
if (val < 0xfb)
{
return 1;
}
else if (val == 0xfc)
{
return 3;
}
else if (val == 0xfd)
{
return 4;
}
else
{
return 9;
}
}
/**
* @brief Converts a length-encoded integer to @c uint64_t
*
* @see https://dev.mysql.com/doc/internals/en/integer.html
* @param c Pointer to the first byte of a length-encoded integer
* @return The value converted to a standard unsigned integer
*/
uint64_t leint_value(const uint8_t* c)
{
uint64_t sz = 0;
if (*c < 0xfb)
{
sz = *c;
}
else if (*c == 0xfc)
{
memcpy(&sz, c + 1, 2);
}
else if (*c == 0xfd)
{
memcpy(&sz, c + 1, 3);
}
else if (*c == 0xfe)
{
memcpy(&sz, c + 1, 8);
}
else
{
mxb_assert(*c == 0xff);
MXB_ERROR("Unexpected length encoding '%x' encountered when reading length-encoded integer.", *c);
}
return sz;
}
/**
* Converts a length-encoded integer into a standard unsigned integer
* and advances the pointer to the next unrelated byte.
*
* @param c Pointer to the first byte of a length-encoded integer
*/
uint64_t leint_consume(uint8_t** c)
{
uint64_t rval = leint_value(*c);
*c += leint_bytes(*c);
return rval;
}
/**
* @brief Consume and duplicate a length-encoded string
*
* Converts a length-encoded string to a C string and advances the pointer to
* the first byte after the string. The caller is responsible for freeing
* the returned string.
* @param c Pointer to the first byte of a valid packet.
* @return The newly allocated string or NULL if memory allocation failed
*/
char* lestr_consume_dup(uint8_t** c)
{
uint64_t slen = leint_consume(c);
char* str = (char*)MXS_MALLOC((slen + 1) * sizeof(char));
if (str)
{
memcpy(str, *c, slen);
str[slen] = '\0';
*c += slen;
}
return str;
}
/**
* @brief Consume a length-encoded string
*
* Converts length-encoded strings to character strings and advanced
* the pointer to the next unrelated byte.
* @param c Pointer to the start of the length-encoded string
* @param size Pointer to a variable where the size of the string is stored
* @return Pointer to the start of the string
*/
char* lestr_consume(uint8_t** c, size_t* size)
{
uint64_t slen = leint_consume(c);
*size = slen;
char* start = (char*) *c;
*c += slen;
return start;
}
} }

View File

@ -24,6 +24,7 @@
#include <functional> #include <functional>
#include <cctype> #include <cctype>
#include <maxsql/mariadb.hh>
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <maxscale/buffer.hh> #include <maxscale/buffer.hh>
#include <maxscale/modutil.hh> #include <maxscale/modutil.hh>
@ -730,8 +731,8 @@ int modutil_count_signal_packets(GWBUF* reply, int n_found, bool* more_out, modu
gwbuf_copy_data(reply, offset + MYSQL_HEADER_LEN + 1, sizeof(data), data); gwbuf_copy_data(reply, offset + MYSQL_HEADER_LEN + 1, sizeof(data), data);
uint8_t* ptr = data; uint8_t* ptr = data;
ptr += mxs_leint_bytes(ptr); ptr += mxq::leint_bytes(ptr);
ptr += mxs_leint_bytes(ptr); ptr += mxq::leint_bytes(ptr);
uint16_t* status = (uint16_t*)ptr; uint16_t* status = (uint16_t*)ptr;
more = (*status) & SERVER_MORE_RESULTS_EXIST; more = (*status) & SERVER_MORE_RESULTS_EXIST;

View File

@ -33,126 +33,6 @@
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <maxscale/config.hh> #include <maxscale/config.hh>
/**
* @brief Calculate the length of a length-encoded integer in bytes
*
* @param ptr Start of the length encoded value
* @return Number of bytes before the actual value
*/
size_t mxs_leint_bytes(const uint8_t* ptr)
{
uint8_t val = *ptr;
if (val < 0xfb)
{
return 1;
}
else if (val == 0xfc)
{
return 3;
}
else if (val == 0xfd)
{
return 4;
}
else
{
return 9;
}
}
/**
* @brief Converts a length-encoded integer to @c uint64_t
*
* @see https://dev.mysql.com/doc/internals/en/integer.html
* @param c Pointer to the first byte of a length-encoded integer
* @return The value converted to a standard unsigned integer
*/
uint64_t mxs_leint_value(const uint8_t* c)
{
uint64_t sz = 0;
if (*c < 0xfb)
{
sz = *c;
}
else if (*c == 0xfc)
{
memcpy(&sz, c + 1, 2);
}
else if (*c == 0xfd)
{
memcpy(&sz, c + 1, 3);
}
else if (*c == 0xfe)
{
memcpy(&sz, c + 1, 8);
}
else
{
mxb_assert(*c == 0xff);
MXS_ERROR("Unexpected length encoding '%x' encountered when reading "
"length-encoded integer.",
*c);
}
return sz;
}
/**
* Converts a length-encoded integer into a standard unsigned integer
* and advances the pointer to the next unrelated byte.
*
* @param c Pointer to the first byte of a length-encoded integer
*/
uint64_t mxs_leint_consume(uint8_t** c)
{
uint64_t rval = mxs_leint_value(*c);
*c += mxs_leint_bytes(*c);
return rval;
}
/**
* @brief Consume and duplicate a length-encoded string
*
* Converts a length-encoded string to a C string and advances the pointer to
* the first byte after the string. The caller is responsible for freeing
* the returned string.
* @param c Pointer to the first byte of a valid packet.
* @return The newly allocated string or NULL if memory allocation failed
*/
char* mxs_lestr_consume_dup(uint8_t** c)
{
uint64_t slen = mxs_leint_consume(c);
char* str = (char*)MXS_MALLOC((slen + 1) * sizeof(char));
if (str)
{
memcpy(str, *c, slen);
str[slen] = '\0';
*c += slen;
}
return str;
}
/**
* @brief Consume a length-encoded string
*
* Converts length-encoded strings to character strings and advanced
* the pointer to the next unrelated byte.
* @param c Pointer to the start of the length-encoded string
* @param size Pointer to a variable where the size of the string is stored
* @return Pointer to the start of the string
*/
char* mxs_lestr_consume(uint8_t** c, size_t* size)
{
uint64_t slen = mxs_leint_consume(c);
*size = slen;
char* start = (char*) *c;
*c += slen;
return start;
}
MYSQL* mxs_mysql_real_connect(MYSQL* con, SERVER* server, const char* user, const char* passwd) MYSQL* mxs_mysql_real_connect(MYSQL* con, SERVER* server, const char* user, const char* passwd)
{ {
auto ssl = server->ssl().config(); auto ssl = server->ssl().config();

View File

@ -573,7 +573,7 @@ int CacheFilterSession::handle_expecting_response()
{ {
// mxs_leint_bytes() returns the length of the int type field + the size of the // mxs_leint_bytes() returns the length of the int type field + the size of the
// integer. // integer.
size_t n_bytes = mxs_leint_bytes(&header[4]); size_t n_bytes = mxq::leint_bytes(&header[4]);
if (MYSQL_HEADER_LEN + n_bytes <= buflen) if (MYSQL_HEADER_LEN + n_bytes <= buflen)
{ {
@ -581,7 +581,7 @@ int CacheFilterSession::handle_expecting_response()
// need to copy some more data. // need to copy some more data.
copy_data(MYSQL_HEADER_LEN + 1, n_bytes - 1, &header[MYSQL_HEADER_LEN + 1]); copy_data(MYSQL_HEADER_LEN + 1, n_bytes - 1, &header[MYSQL_HEADER_LEN + 1]);
m_res.nTotalFields = mxs_leint_value(&header[4]); m_res.nTotalFields = mxq::leint_value(&header[4]);
m_res.offset = MYSQL_HEADER_LEN + n_bytes; m_res.offset = MYSQL_HEADER_LEN + n_bytes;
m_state = CACHE_EXPECTING_FIELDS; m_state = CACHE_EXPECTING_FIELDS;

View File

@ -37,7 +37,7 @@ public:
*/ */
LEncInt(uint8_t* pData) LEncInt(uint8_t* pData)
{ {
m_value = mxs_leint_value(pData); m_value = mxq::leint_value(pData);
} }
/** /**
@ -49,8 +49,8 @@ public:
*/ */
LEncInt(uint8_t** ppData) LEncInt(uint8_t** ppData)
{ {
size_t nBytes = mxs_leint_bytes(*ppData); size_t nBytes = mxq::leint_bytes(*ppData);
m_value = mxs_leint_value(*ppData); m_value = mxq::leint_value(*ppData);
*ppData += nBytes; *ppData += nBytes;
} }
@ -226,7 +226,7 @@ public:
// NULL is sent as 0xfb. See https://dev.mysql.com/doc/internals/en/com-query-response.html // NULL is sent as 0xfb. See https://dev.mysql.com/doc/internals/en/com-query-response.html
if (*pData != 0xfb) if (*pData != 0xfb)
{ {
m_pString = mxs_lestr_consume(&pData, &m_length); m_pString = mxq::lestr_consume(&pData, &m_length);
} }
else else
{ {
@ -247,7 +247,7 @@ public:
// NULL is sent as 0xfb. See https://dev.mysql.com/doc/internals/en/com-query-response.html // NULL is sent as 0xfb. See https://dev.mysql.com/doc/internals/en/com-query-response.html
if (**ppData != 0xfb) if (**ppData != 0xfb)
{ {
m_pString = mxs_lestr_consume(ppData, &m_length); m_pString = mxq::lestr_consume(ppData, &m_length);
} }
else else
{ {

View File

@ -768,7 +768,7 @@ static int handle_expecting_response(MAXROWS_SESSION_DATA* csdata)
{ {
// mxs_leint_bytes() returns the length of the int type field + the size of the // mxs_leint_bytes() returns the length of the int type field + the size of the
// integer. // integer.
size_t n_bytes = mxs_leint_bytes(&header[4]); size_t n_bytes = mxq::leint_bytes(&header[4]);
if (MYSQL_HEADER_LEN + n_bytes <= buflen) if (MYSQL_HEADER_LEN + n_bytes <= buflen)
{ {
@ -779,7 +779,7 @@ static int handle_expecting_response(MAXROWS_SESSION_DATA* csdata)
n_bytes - 1, n_bytes - 1,
&header[MYSQL_HEADER_LEN + 1]); &header[MYSQL_HEADER_LEN + 1]);
csdata->res.n_totalfields = mxs_leint_value(&header[4]); csdata->res.n_totalfields = mxq::leint_value(&header[4]);
csdata->res.offset += MYSQL_HEADER_LEN + n_bytes; csdata->res.offset += MYSQL_HEADER_LEN + n_bytes;
csdata->state = MAXROWS_EXPECTING_FIELDS; csdata->state = MAXROWS_EXPECTING_FIELDS;

View File

@ -21,6 +21,7 @@
#include <sstream> #include <sstream>
#include <map> #include <map>
#include <maxsql/mariadb.hh>
#include <maxscale/alloc.h> #include <maxscale/alloc.h>
#include <maxscale/clock.h> #include <maxscale/clock.h>
#include <maxscale/modutil.hh> #include <maxscale/modutil.hh>
@ -1245,8 +1246,8 @@ bool mxs_mysql_more_results_after_ok(GWBUF* buffer)
gwbuf_copy_data(buffer, MYSQL_HEADER_LEN + 1, sizeof(data), data); gwbuf_copy_data(buffer, MYSQL_HEADER_LEN + 1, sizeof(data), data);
uint8_t* ptr = data; uint8_t* ptr = data;
ptr += mxs_leint_bytes(ptr); ptr += mxq::leint_bytes(ptr);
ptr += mxs_leint_bytes(ptr); ptr += mxq::leint_bytes(ptr);
uint16_t* status = (uint16_t*)ptr; uint16_t* status = (uint16_t*)ptr;
rval = (*status) & SERVER_MORE_RESULTS_EXIST; rval = (*status) & SERVER_MORE_RESULTS_EXIST;
} }
@ -1482,8 +1483,8 @@ void mxs_mysql_parse_ok_packet(GWBUF* buff, size_t packet_offset, size_t packet_
gwbuf_copy_data(buff, packet_offset, packet_len, local_buf); gwbuf_copy_data(buff, packet_offset, packet_len, local_buf);
ptr += (MYSQL_HEADER_LEN + 1); // Header and Command type ptr += (MYSQL_HEADER_LEN + 1); // Header and Command type
mxs_leint_consume(&ptr); // Affected rows mxq::leint_consume(&ptr); // Affected rows
mxs_leint_consume(&ptr); // Last insert-id mxq::leint_consume(&ptr); // Last insert-id
uint16_t server_status = gw_mysql_get_byte2(ptr); uint16_t server_status = gw_mysql_get_byte2(ptr);
ptr += 2; // status ptr += 2; // status
ptr += 2; // number of warnings ptr += 2; // number of warnings
@ -1491,19 +1492,18 @@ void mxs_mysql_parse_ok_packet(GWBUF* buff, size_t packet_offset, size_t packet_
if (ptr < (local_buf + packet_len)) if (ptr < (local_buf + packet_len))
{ {
size_t size; size_t size;
mxs_lestr_consume(&ptr, &size); // info mxq::lestr_consume(&ptr, &size); // info
if (server_status & SERVER_SESSION_STATE_CHANGED) if (server_status & SERVER_SESSION_STATE_CHANGED)
{ {
MXB_AT_DEBUG(uint64_t data_size = ) mxs_leint_consume(&ptr); // total MXB_AT_DEBUG(uint64_t data_size = ) mxq::leint_consume(&ptr); // total
// SERVER_SESSION_STATE_CHANGED // SERVER_SESSION_STATE_CHANGED
// length // length
mxb_assert(data_size == packet_len - (ptr - local_buf)); mxb_assert(data_size == packet_len - (ptr - local_buf));
while (ptr < (local_buf + packet_len)) while (ptr < (local_buf + packet_len))
{ {
enum_session_state_type type = enum_session_state_type type = (enum enum_session_state_type)mxq::leint_consume(&ptr);
(enum enum_session_state_type)mxs_leint_consume(&ptr);
#if defined (SS_DEBUG) #if defined (SS_DEBUG)
mxb_assert(type <= SESSION_TRACK_TRANSACTION_TYPE); mxb_assert(type <= SESSION_TRACK_TRANSACTION_TYPE);
#endif #endif
@ -1511,30 +1511,30 @@ void mxs_mysql_parse_ok_packet(GWBUF* buff, size_t packet_offset, size_t packet_
{ {
case SESSION_TRACK_STATE_CHANGE: case SESSION_TRACK_STATE_CHANGE:
case SESSION_TRACK_SCHEMA: case SESSION_TRACK_SCHEMA:
size = mxs_leint_consume(&ptr); // Length of the overall entity. size = mxq::leint_consume(&ptr); // Length of the overall entity.
ptr += size; ptr += size;
break; break;
case SESSION_TRACK_GTIDS: case SESSION_TRACK_GTIDS:
mxs_leint_consume(&ptr); // Length of the overall entity. mxq::leint_consume(&ptr); // Length of the overall entity.
mxs_leint_consume(&ptr); // encoding specification mxq::leint_consume(&ptr); // encoding specification
var_value = mxs_lestr_consume_dup(&ptr); var_value = mxq::lestr_consume_dup(&ptr);
gwbuf_add_property(buff, MXS_LAST_GTID, var_value); gwbuf_add_property(buff, MXS_LAST_GTID, var_value);
MXS_FREE(var_value); MXS_FREE(var_value);
break; break;
case SESSION_TRACK_TRANSACTION_CHARACTERISTICS: case SESSION_TRACK_TRANSACTION_CHARACTERISTICS:
mxs_leint_consume(&ptr); // length mxq::leint_consume(&ptr); // length
var_value = mxs_lestr_consume_dup(&ptr); var_value = mxq::lestr_consume_dup(&ptr);
gwbuf_add_property(buff, "trx_characteristics", var_value); gwbuf_add_property(buff, "trx_characteristics", var_value);
MXS_FREE(var_value); MXS_FREE(var_value);
break; break;
case SESSION_TRACK_SYSTEM_VARIABLES: case SESSION_TRACK_SYSTEM_VARIABLES:
mxs_leint_consume(&ptr); // lenth mxq::leint_consume(&ptr); // lenth
// system variables like autocommit, schema, charset ... // system variables like autocommit, schema, charset ...
var_name = mxs_lestr_consume_dup(&ptr); var_name = mxq::lestr_consume_dup(&ptr);
var_value = mxs_lestr_consume_dup(&ptr); var_value = mxq::lestr_consume_dup(&ptr);
gwbuf_add_property(buff, var_name, var_value); gwbuf_add_property(buff, var_name, var_value);
MXS_DEBUG("SESSION_TRACK_SYSTEM_VARIABLES, name:%s, value:%s", var_name, var_value); MXS_DEBUG("SESSION_TRACK_SYSTEM_VARIABLES, name:%s, value:%s", var_name, var_value);
MXS_FREE(var_name); MXS_FREE(var_name);
@ -1542,15 +1542,15 @@ void mxs_mysql_parse_ok_packet(GWBUF* buff, size_t packet_offset, size_t packet_
break; break;
case SESSION_TRACK_TRANSACTION_TYPE: case SESSION_TRACK_TRANSACTION_TYPE:
mxs_leint_consume(&ptr); // length mxq::leint_consume(&ptr); // length
trx_info = mxs_lestr_consume_dup(&ptr); trx_info = mxq::lestr_consume_dup(&ptr);
MXS_DEBUG("get trx_info:%s", trx_info); MXS_DEBUG("get trx_info:%s", trx_info);
gwbuf_add_property(buff, (char*)"trx_state", trx_info); gwbuf_add_property(buff, (char*)"trx_state", trx_info);
MXS_FREE(trx_info); MXS_FREE(trx_info);
break; break;
default: default:
mxs_lestr_consume(&ptr, &size); mxq::lestr_consume(&ptr, &size);
MXS_WARNING("recieved unexpecting session track type:%d", type); MXS_WARNING("recieved unexpecting session track type:%d", type);
break; break;
} }

View File

@ -588,7 +588,7 @@ bool Rpl::handle_row_event(REP_HEADER* hdr, uint8_t* ptr)
} }
/** Number of columns in the table */ /** Number of columns in the table */
uint64_t ncolumns = mxs_leint_consume(&ptr); uint64_t ncolumns = mxq::leint_consume(&ptr);
/** If full row image is used, all columns are present. Currently only full /** If full row image is used, all columns are present. Currently only full
* row image is supported and thus the bitfield should be all ones. In * row image is supported and thus the bitfield should be all ones. In

View File

@ -585,15 +585,15 @@ TableMapEvent* table_map_alloc(uint8_t* ptr, uint8_t hdr_len, TableCreateEvent*
memcpy(table_name, ptr, table_name_len + 1); memcpy(table_name, ptr, table_name_len + 1);
ptr += table_name_len + 1; ptr += table_name_len + 1;
uint64_t column_count = mxs_leint_value(ptr); uint64_t column_count = mxq::leint_value(ptr);
ptr += mxs_leint_bytes(ptr); ptr += mxq::leint_bytes(ptr);
/** Column types */ /** Column types */
uint8_t* column_types = ptr; uint8_t* column_types = ptr;
ptr += column_count; ptr += column_count;
size_t metadata_size = 0; size_t metadata_size = 0;
uint8_t* metadata = (uint8_t*)mxs_lestr_consume(&ptr, &metadata_size); uint8_t* metadata = (uint8_t*) mxq::lestr_consume(&ptr, &metadata_size);
uint8_t* nullmap = ptr; uint8_t* nullmap = ptr;
size_t nullmap_size = (column_count + 7) / 8; size_t nullmap_size = (column_count + 7) / 8;