diff --git a/src/protocol/MySQLMessage.cc b/src/protocol/MySQLMessage.cc index 04225afe..c13c9723 100644 --- a/src/protocol/MySQLMessage.cc +++ b/src/protocol/MySQLMessage.cc @@ -102,7 +102,7 @@ int MySQLMessage::append(const void *buf, size_t *size) { seqid_ = mysql_stream_get_seq(stream_); mysql_stream_get_buf(&stream_buf, &stream_len, stream_); - ret = decode_packet((const char *)stream_buf, stream_len); + ret = decode_packet((const unsigned char *)stream_buf, stream_len); if (ret == -2) { errno = EBADMSG; @@ -115,10 +115,10 @@ int MySQLMessage::append(const void *buf, size_t *size) int MySQLMessage::encode(struct iovec vectors[], int max) { - const char *p = buf_.c_str(); + const unsigned char *p = (unsigned char *)buf_.c_str(); size_t nleft = buf_.size(); uint8_t seqid_start = seqid_; - char *head; + unsigned char *head; uint32_t length; int i = 0; @@ -132,7 +132,7 @@ int MySQLMessage::encode(struct iovec vectors[], int max) vectors[i].iov_base = head; vectors[i].iov_len = 4; i++; - vectors[i].iov_base = const_cast(p); + vectors[i].iov_base = const_cast(p); vectors[i].iov_len = length; i++; @@ -188,10 +188,10 @@ int MySQLHandshakeResponse::encode(struct iovec vectors[], int max) return this->MySQLMessage::encode(vectors, max); } -int MySQLHandshakeResponse::decode_packet(const char *buf, size_t buflen) +int MySQLHandshakeResponse::decode_packet(const unsigned char *buf, size_t buflen) { - const char *end = buf + buflen; - const char *pos; + const unsigned char *end = buf + buflen; + const unsigned char *pos; if (buflen == 0) return -2; @@ -201,7 +201,7 @@ int MySQLHandshakeResponse::decode_packet(const char *buf, size_t buflen) { if (buflen >= 4) { - const_cast(buf)[3] = '#'; + const_cast(buf)[3] = '#'; if (mysql_parser_parse(buf, buflen, parser_) == 1) { disallowed_ = true; @@ -220,7 +220,7 @@ int MySQLHandshakeResponse::decode_packet(const char *buf, size_t buflen) if (pos >= end || end - pos < 43) return -2; - server_version_.assign(buf, pos - buf); + server_version_.assign((const char *)buf, pos - buf); buf = pos + 1; connection_id_ = uint4korr(buf); buf += 4; @@ -262,8 +262,8 @@ static inline std::string __sha1_bin(const std::string& str) int MySQLAuthRequest::encode(struct iovec vectors[], int max) { std::string native; - char header[32] = {0}; - char *pos = header; + unsigned char header[32] = {0}; + unsigned char *pos = header; int4store(pos, MYSQL_CAPFLAG_CLIENT_PROTOCOL_41 | MYSQL_CAPFLAG_CLIENT_SECURE_CONNECTION | @@ -290,17 +290,17 @@ int MySQLAuthRequest::encode(struct iovec vectors[], int max) } buf_.clear(); - buf_.append(header, 32); + buf_.append((char *)header, 32); buf_.append(username_.c_str(), username_.size() + 1); buf_.append(native); buf_.append(db_.c_str(), db_.size() + 1); return this->MySQLMessage::encode(vectors, max); } -int MySQLAuthRequest::decode_packet(const char *buf, size_t buflen) +int MySQLAuthRequest::decode_packet(const unsigned char *buf, size_t buflen) { - const char *end = buf + buflen; - const char *pos; + const unsigned char *end = buf + buflen; + const unsigned char *pos; if (buflen < 32) return -2; @@ -321,7 +321,7 @@ int MySQLAuthRequest::decode_packet(const char *buf, size_t buflen) if (pos >= end) return -2; - username_.assign(buf, pos - buf); + username_.assign((const char *)buf, pos - buf); buf = pos + 1; return 1; @@ -337,7 +337,7 @@ void MySQLResponse::set_ok_packet() buf_.append((const char *)&zero16, 2); } -int MySQLResponse::decode_packet(const char *buf, size_t buflen) +int MySQLResponse::decode_packet(const unsigned char *buf, size_t buflen) { return mysql_parser_parse(buf, buflen, parser_); } diff --git a/src/protocol/MySQLMessage.h b/src/protocol/MySQLMessage.h index a42b431f..728901ba 100644 --- a/src/protocol/MySQLMessage.h +++ b/src/protocol/MySQLMessage.h @@ -45,7 +45,7 @@ public: protected: virtual int append(const void *buf, size_t *size); virtual int encode(struct iovec vectors[], int max); - virtual int decode_packet(const char *buf, size_t buflen) { return 1; } + virtual int decode_packet(const unsigned char *buf, size_t buflen) { return 1; } void set_command(int cmd) const; @@ -54,7 +54,7 @@ protected: mysql_parser_t *parser_; //encode - char heads_[256][4]; + unsigned char heads_[256][4]; uint8_t seqid_; std::string buf_; size_t cur_size_; @@ -112,7 +112,7 @@ public: MySQLResponse& operator= (MySQLResponse&& move) = default; protected: - virtual int decode_packet(const char *buf, size_t buflen); + virtual int decode_packet(const unsigned char *buf, size_t buflen); }; } diff --git a/src/protocol/MySQLMessage.inl b/src/protocol/MySQLMessage.inl index c4e677dd..bcfe3eab 100644 --- a/src/protocol/MySQLMessage.inl +++ b/src/protocol/MySQLMessage.inl @@ -58,7 +58,7 @@ public: bool host_disallowed() const { return this->disallowed_; } private: - virtual int decode_packet(const char *buf, size_t buflen); + virtual int decode_packet(const unsigned char *buf, size_t buflen); uint8_t protocol_version_; std::string server_version_; @@ -98,7 +98,7 @@ public: private: virtual int encode(struct iovec vectors[], int max); - virtual int decode_packet(const char *buf, size_t buflen); + virtual int decode_packet(const unsigned char *buf, size_t buflen); std::string username_; std::string password_; diff --git a/src/protocol/MySQLResult.cc b/src/protocol/MySQLResult.cc index e3dcd677..01a95727 100644 --- a/src/protocol/MySQLResult.cc +++ b/src/protocol/MySQLResult.cc @@ -162,18 +162,18 @@ bool MySQLResultCursor::fetch_row(std::vector& row_arr) return false; unsigned long long len; - const char *data; + const unsigned char *data; int data_type; - const char *p = (const char *)this->pos; - const char *end = (const char *)this->end; + const unsigned char *p = (const unsigned char *)this->pos; + const unsigned char *end = (const unsigned char *)this->end; row_arr.clear(); for (int i = 0; i < this->field_count; i++) { data_type = this->fields[i]->get_data_type(); - if (*(const unsigned char *)p == MYSQL_PACKET_HEADER_NULL) + if (*p == MYSQL_PACKET_HEADER_NULL) { data = NULL; len = 0; @@ -211,14 +211,14 @@ bool MySQLResultCursor::fetch_row_nocopy(const void **data, size_t *len, int *da return false; unsigned long long cell_len; - const char *cell_data; + const unsigned char *cell_data; - const char *p = (const char *)this->pos; - const char *end = (const char *)this->end; + const unsigned char *p = (const unsigned char *)this->pos; + const unsigned char *end = (const unsigned char *)this->end; for (int i = 0; i < this->field_count; i++) { - if (*(const unsigned char *)p == MYSQL_PACKET_HEADER_NULL) + if (*p == MYSQL_PACKET_HEADER_NULL) { cell_data = NULL; cell_len = 0; @@ -246,11 +246,11 @@ bool MySQLResultCursor::fetch_all(std::vector>& rows) return false; unsigned long long len; - const char *data; + const unsigned char *data; int data_type; - const char *p = (const char *)this->pos; - const char *end = (const char *)this->end; + const unsigned char *p = (const unsigned char *)this->pos; + const unsigned char *end = (const unsigned char *)this->end; rows.clear(); @@ -260,7 +260,7 @@ bool MySQLResultCursor::fetch_all(std::vector>& rows) for (int j = 0; j < this->field_count; j++) { data_type = this->fields[j]->get_data_type(); - if (*(const unsigned char *)p == MYSQL_PACKET_HEADER_NULL) + if (*p == MYSQL_PACKET_HEADER_NULL) { data = NULL; len = 0; diff --git a/src/protocol/MySQLResult.inl b/src/protocol/MySQLResult.inl index f48c4a3a..95805cf5 100644 --- a/src/protocol/MySQLResult.inl +++ b/src/protocol/MySQLResult.inl @@ -294,18 +294,18 @@ bool MySQLResultCursor::fetch_row(T& row_map) return false; unsigned long long len; - const char *data; + const unsigned char *data; int data_type; - const char *p = (const char *)this->pos; - const char *end = (const char *)this->end; + const unsigned char *p = (const unsigned char *)this->pos; + const unsigned char *end = (const unsigned char *)this->end; row_map.clear(); for (int i = 0; i < this->field_count; i++) { data_type = this->fields[i]->get_data_type(); - if (*(const unsigned char *)p == MYSQL_PACKET_HEADER_NULL) + if (*p == MYSQL_PACKET_HEADER_NULL) { data = NULL; len = 0; diff --git a/src/protocol/mysql_byteorder.c b/src/protocol/mysql_byteorder.c index 387b68bd..47de046d 100644 --- a/src/protocol/mysql_byteorder.c +++ b/src/protocol/mysql_byteorder.c @@ -18,15 +18,15 @@ #include "mysql_byteorder.h" -int decode_length_safe(unsigned long long *res, const char **pos, const char *end) +int decode_length_safe(unsigned long long *res, const unsigned char **pos, + const unsigned char *end) { - //const unsigned char *p = (const unsigned char *)*pos; - const char *p = *pos; + const unsigned char *p = *pos; - switch (*(const unsigned char *)p) + switch (*p) { default: - *res = *(const unsigned char *)p; + *res = *p; *pos = p + 1; break; @@ -63,8 +63,8 @@ int decode_length_safe(unsigned long long *res, const char **pos, const char *en return 1; } -int decode_string(const char **str, unsigned long long *len, - const char **pos, const char *end) +int decode_string(const unsigned char **str, unsigned long long *len, + const unsigned char **pos, const unsigned char *end) { unsigned long long length; diff --git a/src/protocol/mysql_byteorder.h b/src/protocol/mysql_byteorder.h index ccc12885..42d06163 100644 --- a/src/protocol/mysql_byteorder.h +++ b/src/protocol/mysql_byteorder.h @@ -25,248 +25,377 @@ #if __BYTE_ORDER == __LITTLE_ENDIAN -static inline int16_t sint2korr(const char *A) { - int16_t ret; - memcpy(&ret, A, sizeof(ret)); - return ret; +static inline int16_t sint2korr(const unsigned char *A) +{ + int16_t ret; + memcpy(&ret, A, sizeof(ret)); + return ret; } -static inline int32_t sint4korr(const char *A) { - int32_t ret; - memcpy(&ret, A, sizeof(ret)); - return ret; +static inline int32_t sint4korr(const unsigned char *A) +{ + int32_t ret; + memcpy(&ret, A, sizeof(ret)); + return ret; } -static inline uint16_t uint2korr(const char *A) { - uint16_t ret; - memcpy(&ret, A, sizeof(ret)); - return ret; +static inline uint16_t uint2korr(const unsigned char *A) +{ + uint16_t ret; + memcpy(&ret, A, sizeof(ret)); + return ret; } -static inline uint32_t uint4korr(const char *A) { - uint32_t ret; - memcpy(&ret, A, sizeof(ret)); - return ret; +static inline uint32_t uint4korr(const unsigned char *A) +{ + uint32_t ret; + memcpy(&ret, A, sizeof(ret)); + return ret; } -static inline uint64_t uint8korr(const char *A) { - uint64_t ret; - memcpy(&ret, A, sizeof(ret)); - return ret; +static inline uint64_t uint8korr(const unsigned char *A) +{ + uint64_t ret; + memcpy(&ret, A, sizeof(ret)); + return ret; } -static inline int64_t sint8korr(const char *A) { - int64_t ret; - memcpy(&ret, A, sizeof(ret)); - return ret; +static inline int64_t sint8korr(const unsigned char *A) +{ + int64_t ret; + memcpy(&ret, A, sizeof(ret)); + return ret; } -static inline void int2store(char *T, uint16_t A) { memcpy(T, &A, sizeof(A)); } - -static inline void int4store(char *T, uint32_t A) { memcpy(T, &A, sizeof(A)); } - -static inline void int7store(char *T, uint64_t A) { memcpy(T, &A, 7); } - -static inline void int8store(char *T, uint64_t A) { - memcpy(T, &A, sizeof(A)); +static inline void int2store(unsigned char *T, uint16_t A) +{ + memcpy(T, &A, sizeof(A)); } -static inline void float4get(float *V, const char *M) { - memcpy(V, (M), sizeof(float)); +static inline void int4store(unsigned char *T, uint32_t A) +{ + memcpy(T, &A, sizeof(A)); } -static inline void float4store(char *V, float M) { - memcpy(V, (&M), sizeof(float)); +static inline void int7store(unsigned char *T, uint64_t A) +{ + memcpy(T, &A, 7); } -static inline void float8get(double *V, const char *M) { - memcpy(V, M, sizeof(double)); +static inline void int8store(unsigned char *T, uint64_t A) +{ + memcpy(T, &A, sizeof(A)); } -static inline void float8store(char *V, double M) { - memcpy(V, &M, sizeof(double)); +static inline void float4get(float *V, const unsigned char *M) +{ + memcpy(V, (M), sizeof(float)); } -static inline void floatget(float *V, const char *M) { float4get(V, M); } -static inline void floatstore(char *V, float M) { float4store(V, M); } - -static inline void doublestore(char *T, double V) { - memcpy(T, &V, sizeof(double)); -} -static inline void doubleget(double *V, const char *M) { - memcpy(V, M, sizeof(double)); +static inline void float4store(unsigned char *V, float M) +{ + memcpy(V, (&M), sizeof(float)); } -static inline void ushortget(uint16_t *V, const char *pM) { *V = uint2korr(pM); } -static inline void shortget(int16_t *V, const char *pM) { *V = sint2korr(pM); } -static inline void longget(int32_t *V, const char *pM) { *V = sint4korr(pM); } -static inline void ulongget(uint32_t *V, const char *pM) { *V = uint4korr(pM); } -static inline void shortstore(char *T, int16_t V) { int2store(T, V); } -static inline void longstore(char *T, int32_t V) { int4store(T, V); } - -static inline void longlongget(int64_t *V, const char *M) { - memcpy(V, (M), sizeof(uint64_t)); +static inline void float8get(double *V, const unsigned char *M) +{ + memcpy(V, M, sizeof(double)); } -static inline void longlongstore(char *T, int64_t V) { - memcpy((T), &V, sizeof(uint64_t)); + +static inline void float8store(unsigned char *V, double M) +{ + memcpy(V, &M, sizeof(double)); +} + +static inline void floatget(float *V, const unsigned char *M) +{ + float4get(V, M); +} + +static inline void floatstore(unsigned char *V, float M) +{ + float4store(V, M); +} + +static inline void doublestore(unsigned char *T, double V) +{ + memcpy(T, &V, sizeof(double)); +} +static inline void doubleget(double *V, const unsigned char *M) +{ + memcpy(V, M, sizeof(double)); +} + +static inline void ushortget(uint16_t *V, const unsigned char *pM) +{ + *V = uint2korr(pM); +} + +static inline void shortget(int16_t *V, const unsigned char *pM) +{ + *V = sint2korr(pM); +} + +static inline void longget(int32_t *V, const unsigned char *pM) +{ + *V = sint4korr(pM); +} + +static inline void ulongget(uint32_t *V, const unsigned char *pM) +{ + *V = uint4korr(pM); +} + +static inline void shortstore(unsigned char *T, int16_t V) +{ + int2store(T, V); +} + +static inline void longstore(unsigned char *T, int32_t V) +{ + int4store(T, V); +} + +static inline void longlongget(int64_t *V, const unsigned char *M) +{ + memcpy(V, (M), sizeof(uint64_t)); +} + +static inline void longlongstore(unsigned char *T, int64_t V) +{ + memcpy((T), &V, sizeof(uint64_t)); +} + +static inline int32_t sint3korr(const unsigned char *A) +{ + int32_t ret = 0; + memcpy(&ret, A, 3); + return ret; +} + +static inline uint32_t uint3korr(const unsigned char *A) +{ + uint32_t ret = 0; + memcpy(&ret, A, 3); + return ret; +} + +static inline void int3store(unsigned char *T, uint32_t A) +{ + memcpy(T, &A, 3); } #elif __BYTE_ORDER == __BIG_ENDIAN -static inline int16_t sint2korr(const char *A) { - return (int16_t)(((int16_t)(A[0])) + ((int16_t)(A[1]) << 8)); +static inline int16_t sint2korr(const unsigned char *A) +{ + return (int16_t)(((int16_t)(A[0])) + ((int16_t)(A[1]) << 8)); } -static inline int32_t sint4korr(const char *A) { - return (int32_t)(((int32_t)(A[0])) + (((int32_t)(A[1]) << 8)) + - (((int32_t)(A[2]) << 16)) + (((int32_t)(A[3]) << 24))); +static inline int32_t sint4korr(const unsigned char *A) +{ + return (int32_t)(((int32_t)(A[0])) + (((int32_t)(A[1]) << 8)) + + (((int32_t)(A[2]) << 16)) + (((int32_t)(A[3]) << 24))); } -static inline uint16_t uint2korr(const char *A) { - return (uint16_t)(((uint16_t)(A[0])) + ((uint16_t)(A[1]) << 8)); +static inline uint16_t uint2korr(const unsigned char *A) +{ + return (uint16_t)(((uint16_t)(A[0])) + ((uint16_t)(A[1]) << 8)); } -static inline uint32_t uint4korr(const char *A) { - return (uint32_t)(((uint32_t)(A[0])) + (((uint32_t)(A[1])) << 8) + - (((uint32_t)(A[2])) << 16) + (((uint32_t)(A[3])) << 24)); +static inline uint32_t uint4korr(const unsigned char *A) +{ + return (uint32_t)(((uint32_t)(A[0])) + (((uint32_t)(A[1])) << 8) + + (((uint32_t)(A[2])) << 16) + (((uint32_t)(A[3])) << 24)); } -static inline uint64_t uint8korr(const char *A) { - return ((uint64_t)(((uint32_t)(A[0])) + (((uint32_t)(A[1])) << 8) + - (((uint32_t)(A[2])) << 16) + (((uint32_t)(A[3])) << 24)) + - (((uint64_t)(((uint32_t)(A[4])) + (((uint32_t)(A[5])) << 8) + - (((uint32_t)(A[6])) << 16) + (((uint32_t)(A[7])) << 24))) - << 32)); +static inline uint64_t uint8korr(const unsigned char *A) +{ + return ((uint64_t)(((uint32_t)(A[0])) + (((uint32_t)(A[1])) << 8) + + (((uint32_t)(A[2])) << 16) + (((uint32_t)(A[3])) << 24)) + + (((uint64_t)(((uint32_t)(A[4])) + (((uint32_t)(A[5])) << 8) + + (((uint32_t)(A[6])) << 16) + (((uint32_t)(A[7])) << 24))) << 32)); } -static inline int64_t sint8korr(const char *A) { - return (int64_t)uint8korr(A); +static inline int64_t sint8korr(const unsigned char *A) +{ + return (int64_t)uint8korr(A); } -static inline void int2store(char *T, uint16_t A) { - uint def_temp = A; - *(T) = (char)(def_temp); - *(T + 1) = (char)(def_temp >> 8); +static inline void int2store(unsigned char *T, uint16_t A) +{ + uint def_temp = A; + *(T) = (unsigned char)(def_temp); + *(T + 1) = (unsigned char)(def_temp >> 8); } -static inline void int4store(char *T, uint32_t A) { - *(T) = (char)(A); - *(T + 1) = (char)(A >> 8); - *(T + 2) = (char)(A >> 16); - *(T + 3) = (char)(A >> 24); +static inline void int4store(unsigned char *T, uint32_t A) +{ + *(T) = (unsigned char)(A); + *(T + 1) = (unsigned char)(A >> 8); + *(T + 2) = (unsigned char)(A >> 16); + *(T + 3) = (unsigned char)(A >> 24); } -static inline void int7store(char *T, uint64_t A) { - *(T) = (char)(A); - *(T + 1) = (char)(A >> 8); - *(T + 2) = (char)(A >> 16); - *(T + 3) = (char)(A >> 24); - *(T + 4) = (char)(A >> 32); - *(T + 5) = (char)(A >> 40); - *(T + 6) = (char)(A >> 48); +static inline void int7store(unsigned char *T, uint64_t A) +{ + *(T) = (unsigned char)(A); + *(T + 1) = (unsigned char)(A >> 8); + *(T + 2) = (unsigned char)(A >> 16); + *(T + 3) = (unsigned char)(A >> 24); + *(T + 4) = (unsigned char)(A >> 32); + *(T + 5) = (unsigned char)(A >> 40); + *(T + 6) = (unsigned char)(A >> 48); } -static inline void int8store(char *T, uint64_t A) { - uint def_temp = (uint)A, def_temp2 = (uint)(A >> 32); - int4store(T, def_temp); - int4store(T + 4, def_temp2); +static inline void int8store(unsigned char *T, uint64_t A) +{ + uint def_temp = (uint)A, def_temp2 = (uint)(A >> 32); + int4store(T, def_temp); + int4store(T + 4, def_temp2); } -static inline void float4store(char *T, float A) { - *(T) = ((char *)&A)[3]; - *((T) + 1) = (char)((char *)&A)[2]; - *((T) + 2) = (char)((char *)&A)[1]; - *((T) + 3) = (char)((char *)&A)[0]; +static inline void float4store(unsigned char *T, float A) +{ + *(T) = ((unsigned char *)&A)[3]; + *((T) + 1) = (unsigned char)((unsigned char *)&A)[2]; + *((T) + 2) = (unsigned char)((unsigned char *)&A)[1]; + *((T) + 3) = (unsigned char)((unsigned char *)&A)[0]; } -static inline void float4get(float *V, const char *M) { - float def_temp; - ((char *)&def_temp)[0] = (M)[3]; - ((char *)&def_temp)[1] = (M)[2]; - ((char *)&def_temp)[2] = (M)[1]; - ((char *)&def_temp)[3] = (M)[0]; - (*V) = def_temp; +static inline void float4get(float *V, const unsigned char *M) +{ + float def_temp; + ((unsigned char *)&def_temp)[0] = (M)[3]; + ((unsigned char *)&def_temp)[1] = (M)[2]; + ((unsigned char *)&def_temp)[2] = (M)[1]; + ((unsigned char *)&def_temp)[3] = (M)[0]; + (*V) = def_temp; } -static inline void float8store(char *T, double V) { - *(T) = ((char *)&V)[7]; - *((T) + 1) = (char)((char *)&V)[6]; - *((T) + 2) = (char)((char *)&V)[5]; - *((T) + 3) = (char)((char *)&V)[4]; - *((T) + 4) = (char)((char *)&V)[3]; - *((T) + 5) = (char)((char *)&V)[2]; - *((T) + 6) = (char)((char *)&V)[1]; - *((T) + 7) = (char)((char *)&V)[0]; +static inline void float8store(unsigned char *T, double V) { + *(T) = ((unsigned char *)&V)[7]; + *((T) + 1) = (unsigned char)((unsigned char *)&V)[6]; + *((T) + 2) = (unsigned char)((unsigned char *)&V)[5]; + *((T) + 3) = (unsigned char)((unsigned char *)&V)[4]; + *((T) + 4) = (unsigned char)((unsigned char *)&V)[3]; + *((T) + 5) = (unsigned char)((unsigned char *)&V)[2]; + *((T) + 6) = (unsigned char)((unsigned char *)&V)[1]; + *((T) + 7) = (unsigned char)((unsigned char *)&V)[0]; } -static inline void float8get(double *V, const char *M) { - double def_temp; - ((char *)&def_temp)[0] = (M)[7]; - ((char *)&def_temp)[1] = (M)[6]; - ((char *)&def_temp)[2] = (M)[5]; - ((char *)&def_temp)[3] = (M)[4]; - ((char *)&def_temp)[4] = (M)[3]; - ((char *)&def_temp)[5] = (M)[2]; - ((char *)&def_temp)[6] = (M)[1]; - ((char *)&def_temp)[7] = (M)[0]; - (*V) = def_temp; +static inline void float8get(double *V, const unsigned char *M) +{ + double def_temp; + ((unsigned char *)&def_temp)[0] = (M)[7]; + ((unsigned char *)&def_temp)[1] = (M)[6]; + ((unsigned char *)&def_temp)[2] = (M)[5]; + ((unsigned char *)&def_temp)[3] = (M)[4]; + ((unsigned char *)&def_temp)[4] = (M)[3]; + ((unsigned char *)&def_temp)[5] = (M)[2]; + ((unsigned char *)&def_temp)[6] = (M)[1]; + ((unsigned char *)&def_temp)[7] = (M)[0]; + (*V) = def_temp; } -static inline void ushortget(uint16_t *V, const char *pM) { - *V = (uint16_t)(((uint16_t)((char)(pM)[1])) + ((uint16_t)((uint16_t)(pM)[0]) << 8)); -} -static inline void shortget(int16_t *V, const char *pM) { - *V = (short)(((short)((char)(pM)[1])) + ((short)((short)(pM)[0]) << 8)); -} -static inline void longget(int32_t *V, const char *pM) { - int32_t def_temp; - ((char *)&def_temp)[0] = (pM)[0]; - ((char *)&def_temp)[1] = (pM)[1]; - ((char *)&def_temp)[2] = (pM)[2]; - ((char *)&def_temp)[3] = (pM)[3]; - (*V) = def_temp; -} -static inline void ulongget(uint32_t *V, const char *pM) { - uint32_t def_temp; - ((char *)&def_temp)[0] = (pM)[0]; - ((char *)&def_temp)[1] = (pM)[1]; - ((char *)&def_temp)[2] = (pM)[2]; - ((char *)&def_temp)[3] = (pM)[3]; - (*V) = def_temp; -} -static inline void shortstore(char *T, int16_t A) { - uint def_temp = (uint)(A); - *(((char *)T) + 1) = (char)(def_temp); - *(((char *)T) + 0) = (char)(def_temp >> 8); -} -static inline void longstore(char *T, int32_t A) { - *(((char *)T) + 3) = ((A)); - *(((char *)T) + 2) = (((A) >> 8)); - *(((char *)T) + 1) = (((A) >> 16)); - *(((char *)T) + 0) = (((A) >> 24)); +static inline void ushortget(uint16_t *V, const unsigned char *pM) +{ + *V = (uint16_t)(((uint16_t)((unsigned char)(pM)[1])) + + ((uint16_t)((uint16_t)(pM)[0]) << 8)); } -static inline void floatget(float *V, const char *M) { - memcpy(V, (M), sizeof(float)); +static inline void shortget(int16_t *V, const unsigned char *pM) +{ + *V = (short)(((short)((unsigned char)(pM)[1])) + + ((short)((short)(pM)[0]) << 8)); } -static inline void floatstore(char *T, float V) { - memcpy((T), (&V), sizeof(float)); +static inline void longget(int32_t *V, const unsigned char *pM) +{ + int32_t def_temp; + ((unsigned char *)&def_temp)[0] = (pM)[0]; + ((unsigned char *)&def_temp)[1] = (pM)[1]; + ((unsigned char *)&def_temp)[2] = (pM)[2]; + ((unsigned char *)&def_temp)[3] = (pM)[3]; + (*V) = def_temp; } -static inline void doubleget(double *V, const char *M) { - memcpy(V, (M), sizeof(double)); +static inline void ulongget(uint32_t *V, const unsigned char *pM) +{ + uint32_t def_temp; + ((unsigned char *)&def_temp)[0] = (pM)[0]; + ((unsigned char *)&def_temp)[1] = (pM)[1]; + ((unsigned char *)&def_temp)[2] = (pM)[2]; + ((unsigned char *)&def_temp)[3] = (pM)[3]; + (*V) = def_temp; } -static inline void doublestore(char *T, double V) { - memcpy((T), &V, sizeof(double)); +static inline void shortstore(unsigned char *T, int16_t A) +{ + uint def_temp = (uint)(A); + *(((unsigned char *)T) + 1) = (unsigned char)(def_temp); + *(((unsigned char *)T) + 0) = (unsigned char)(def_temp >> 8); } -static inline void longlongget(int64_t *V, const char *M) { - memcpy(V, (M), sizeof(uint64_t)); +static inline void longstore(unsigned char *T, int32_t A) +{ + *(((unsigned char *)T) + 3) = ((A)); + *(((unsigned char *)T) + 2) = (((A) >> 8)); + *(((unsigned char *)T) + 1) = (((A) >> 16)); + *(((unsigned char *)T) + 0) = (((A) >> 24)); } -static inline void longlongstore(char *T, int64_t V) { - memcpy((T), &V, sizeof(uint64_t)); + +static inline void floatget(float *V, const unsigned char *M) +{ + memcpy(V, (M), sizeof(float)); +} + +static inline void floatstore(unsigned char *T, float V) +{ + memcpy((T), (&V), sizeof(float)); +} + +static inline void doubleget(double *V, const unsigned char *M) +{ + memcpy(V, (M), sizeof(double)); +} + +static inline void doublestore(unsigned char *T, double V) +{ + memcpy((T), &V, sizeof(double)); +} + +static inline void longlongget(int64_t *V, const unsigned char *M) +{ + memcpy(V, (M), sizeof(uint64_t)); +} + +static inline void longlongstore(unsigned char *T, int64_t V) +{ + memcpy((T), &V, sizeof(uint64_t)); +} + +static inline int32_t sint3korr(const unsigned char *p) +{ + return ((int32_t)(((p[2]) & 128) + ? (((uint32_t)255L << 24) | (((uint32_t)p[2]) << 16) + | (((uint32_t)p[1]) << 8) | ((uint32_t)p[0])) + : (((uint32_t)p[2]) << 16) | (((uint32_t)p[1]) << 8) + | ((uint32_t)p[0]))); +} + +static inline uint32_t uint3korr(const unsigned char *p) +{ + return (uint32_t)(((uint32_t)(p[0])) + + (((uint32_t)(p[1])) << 8) + + (((uint32_t)(p[2])) << 16)); +} + +static inline void int3store(unsigned char *p, uint32_t x) +{ + *(p) = (unsigned char)(x); + *(p + 1) = (unsigned char)(x >> 8); + *(p + 2) = (unsigned char)(x >> 16); } #else @@ -288,40 +417,18 @@ static inline unsigned int get_length_size(unsigned long long num) return 9; } -static inline int32_t sint3korr(const char *p) -{ - return ((int32_t)(((p[2]) & 128) - ? (((uint32_t)255L << 24) | (((uint32_t)p[2]) << 16) - | (((uint32_t)p[1]) << 8) | ((uint32_t)p[0])) - : (((uint32_t)p[2]) << 16) | (((uint32_t)p[1]) << 8) - | ((uint32_t)p[0]))); -} - -static inline uint32_t uint3korr(const char *p) -{ - return (uint32_t)(((uint32_t)(p[0])) - + (((uint32_t)(p[1])) << 8) - + (((uint32_t)(p[2])) << 16)); -} - -static inline void int3store(char *p, uint32_t x) -{ - *(p) = (char)(x); - *(p + 1) = (char)(x >> 8); - *(p + 2) = (char)(x >> 16); -} - #ifdef __cplusplus extern "C" { #endif // decode encoded length integer within *end, move pos forward -int decode_length_safe(unsigned long long *res, const char **pos, const char *end); +int decode_length_safe(unsigned long long *res, const unsigned char **pos, + const unsigned char *end); // decode encoded length string within *end, move pos forward -int decode_string(const char **str, unsigned long long *len, - const char **pos, const char *end); +int decode_string(const unsigned char **str, unsigned long long *len, + const unsigned char **pos, const unsigned char *end); #ifdef __cplusplus } diff --git a/src/protocol/mysql_parser.c b/src/protocol/mysql_parser.c index 9d54bdaf..54959ab0 100644 --- a/src/protocol/mysql_parser.c +++ b/src/protocol/mysql_parser.c @@ -148,8 +148,8 @@ static int parse_base_packet(const void *buf, size_t len, mysql_parser_t *parser // 1:0xFF|2:err_no|1:#|5:server_state|0-512:err_msg static int parse_error_packet(const void *buf, size_t len, mysql_parser_t *parser) { - const char *p = (const char *)buf + parser->offset; - const char *buf_end = (const char *)buf + len; + const unsigned char *p = (const unsigned char *)buf + parser->offset; + const unsigned char *buf_end = (const unsigned char *)buf + len; if (p + 9 > buf_end) return -2; @@ -160,11 +160,11 @@ static int parse_error_packet(const void *buf, size_t len, mysql_parser_t *parse if (*p == '#') { p += 1; - parser->net_state_offset = p - (const char *)buf; + parser->net_state_offset = p - (const unsigned char *)buf; p += MYSQL_STATE_LENGTH; size_t msg_len = len - parser->offset - 9; - parser->err_msg_offset = p - (const char *)buf; + parser->err_msg_offset = p - (const unsigned char *)buf; parser->err_msg_len = msg_len; } else { parser->err_msg_offset = (size_t)-1; @@ -180,8 +180,8 @@ static int parse_error_packet(const void *buf, size_t len, mysql_parser_t *parse // 1:0x00|1-9:affect_row|1-9:insert_id|2:server_status|2:warning_count|0-n:server_msg static int parse_ok_packet(const void *buf, size_t len, mysql_parser_t *parser) { - const char *p = (const char *)buf + parser->offset; - const char *buf_end = (const char *)buf + len; + const unsigned char *p = (const unsigned char *)buf + parser->offset; + const unsigned char *buf_end = (const unsigned char *)buf + len; unsigned long long affected_rows, insert_id, info_len; int server_status; @@ -215,9 +215,10 @@ static int parse_ok_packet(const void *buf, size_t len, mysql_parser_t *parser) parser->info_len = 0; } - parser->info_offset = p - (const char *)buf; + parser->info_offset = p - (const unsigned char *)buf; parser->offset += parser->info_offset + parser->info_len; - parser->affected_rows = (affected_rows == (unsigned long long)-1) ? 0 : affected_rows; + parser->affected_rows = (affected_rows == (unsigned long long)-1) ? + 0 : affected_rows; parser->insert_id = (insert_id == (unsigned long long)-1) ? 0 : insert_id; parser->server_status = server_status; parser->warning_count = warning_count; @@ -236,8 +237,8 @@ static int parse_ok_packet(const void *buf, size_t len, mysql_parser_t *parser) // 1:0xfe|2:warnings|2:status_flag static int parse_eof_packet(const void *buf, size_t len, mysql_parser_t *parser) { - const char *p = (const char *)buf + parser->offset; - const char *buf_end = (const char *)buf + len; + const unsigned char *p = (const unsigned char *)buf + parser->offset; + const unsigned char *buf_end = (const unsigned char *)buf + len; if (p + 5 > buf_end) return -2; @@ -258,14 +259,15 @@ static int parse_eof_packet(const void *buf, size_t len, mysql_parser_t *parser) static int parse_field_eof_packet(const void *buf, size_t len, mysql_parser_t *parser) { - const char *p = (const char *)buf + parser->offset; - const char *buf_end = (const char *)buf + len; + const unsigned char *p = (const unsigned char *)buf + parser->offset; + const unsigned char *buf_end = (const unsigned char *)buf + len; if (p + 5 > buf_end) return -2; parser->offset += 5; - parser->current_result_set->rows_begin_offset = p + 5 - (const char *)buf; + parser->current_result_set->rows_begin_offset = p + 5 - + (const unsigned char *)buf; parser->parse = parse_row_packet; return 0; } @@ -285,30 +287,31 @@ static int parse_local_inline(const void *buf, size_t len, mysql_parser_t *parse // NULL as 0xfb, or a length-encoded-string static int parse_row_packet(const void *buf, size_t len, mysql_parser_t *parser) { - const char *p = (const char *)buf + parser->offset; - const char *buf_end = (const char *)buf + len; + const unsigned char *p = (const unsigned char *)buf + parser->offset; + const unsigned char *buf_end = (const unsigned char *)buf + len; unsigned long long cell_len; - const char *cell_data; + const unsigned char *cell_data; size_t i; - if (*(const unsigned char *)p == MYSQL_PACKET_HEADER_ERROR) + if (*p == MYSQL_PACKET_HEADER_ERROR) { parser->parse = parse_error_packet; return 0; } - if (*(const unsigned char *)p == MYSQL_PACKET_HEADER_EOF) + if (*p == MYSQL_PACKET_HEADER_EOF) { parser->parse = parse_eof_packet; - parser->current_result_set->rows_end_offset = p - (const char *)buf; + parser->current_result_set->rows_end_offset = p - + (const unsigned char *)buf; return 0; } for (i = 0; i < parser->current_result_set->field_count; i++) { - if (*(const unsigned char *)p == MYSQL_PACKET_HEADER_NULL) + if (*p == MYSQL_PACKET_HEADER_NULL) { p++; } else { @@ -321,14 +324,14 @@ static int parse_row_packet(const void *buf, size_t len, mysql_parser_t *parser) return -2; parser->current_result_set->row_count++; - parser->offset = p - (const char *)buf; + parser->offset = p - (const unsigned char *)buf; return 0; } static int parse_field_count(const void *buf, size_t len, mysql_parser_t *parser) { - const char *p = (const char *)buf + parser->offset; - const char *buf_end = (const char *)buf + len; + const unsigned char *p = (const unsigned char *)buf + parser->offset; + const unsigned char *buf_end = (const unsigned char *)buf + len; unsigned long long field_count; struct __mysql_result_set *result_set; @@ -361,7 +364,7 @@ static int parse_field_count(const void *buf, size_t len, mysql_parser_t *parser parser->result_set_count++; parser->parse = parse_column_def_packet; - parser->offset = p - (const char *)buf; + parser->offset = p - (const unsigned char *)buf; } else { parser->parse = parse_ok_packet; } @@ -373,11 +376,11 @@ static int parse_field_count(const void *buf, size_t len, mysql_parser_t *parser // 2:charsetnr|4:length|1:type|2:flags|1:decimals|1:0x00|1:0x00|n:str(if COM_FIELD_LIST) static int parse_column_def_packet(const void *buf, size_t len, mysql_parser_t *parser) { - const char *p = (const char *)buf + parser->offset; - const char *buf_end = (const char *)buf + len; + const unsigned char *p = (const unsigned char *)buf + parser->offset; + const unsigned char *buf_end = (const unsigned char *)buf + len; int flag = 0; - const char *str; + const unsigned char *str; unsigned long long str_len; mysql_field_t *field = (mysql_field_t *)malloc(sizeof(mysql_field_t)); @@ -387,32 +390,32 @@ static int parse_column_def_packet(const void *buf, size_t len, mysql_parser_t * do { if (decode_string(&str, &str_len, &p, buf_end) == 0) break; - field->catalog_offset = str - (const char *)buf; + field->catalog_offset = str - (const unsigned char *)buf; field->catalog_length = str_len; if (decode_string(&str, &str_len, &p, buf_end) == 0) break; - field->db_offset = str - (const char *)buf; + field->db_offset = str - (const unsigned char *)buf; field->db_length = str_len; if (decode_string(&str, &str_len, &p, buf_end) == 0) break; - field->table_offset = str - (const char *)buf; + field->table_offset = str - (const unsigned char *)buf; field->table_length = str_len; if (decode_string(&str, &str_len, &p, buf_end) == 0) break; - field->org_table_offset = str - (const char *)buf; + field->org_table_offset = str - (const unsigned char *)buf; field->org_table_length = str_len; if (decode_string(&str, &str_len, &p, buf_end) == 0) break; - field->name_offset = str - (const char *)buf; + field->name_offset = str - (const unsigned char *)buf; field->name_length = str_len; if (decode_string(&str, &str_len, &p, buf_end) == 0) break; - field->org_name_offset = str - (const char *)buf; + field->org_name_offset = str - (const unsigned char *)buf; field->org_name_length = str_len; // the rest needs at least 13 @@ -422,7 +425,7 @@ static int parse_column_def_packet(const void *buf, size_t len, mysql_parser_t * p++; // length of the following fields (always 0x0c) field->charsetnr = uint2korr(p); field->length = uint4korr(p + 2); - field->data_type = *((const unsigned char *)p + 6); + field->data_type = *(p + 6); field->flags = uint2korr(p + 7); field->decimals = (int)p[9]; p += 12; @@ -432,7 +435,7 @@ static int parse_column_def_packet(const void *buf, size_t len, mysql_parser_t * { if (decode_string(&str, &str_len, &p, buf_end) == 0) break; - field->def_offset = str - (const char *)buf; + field->def_offset = str - (const unsigned char *)buf; field->def_length = str_len; } else { field->def_offset = (size_t)-1; @@ -450,7 +453,7 @@ static int parse_column_def_packet(const void *buf, size_t len, mysql_parser_t * //parser->fields.emplace_back(std::move(field)); parser->current_result_set->fields[parser->current_field_count] = field; - parser->offset = p - (const char *)buf; + parser->offset = p - (const unsigned char *)buf; if (++parser->current_field_count == parser->current_result_set->field_count) parser->parse = parse_field_eof_packet;