my_proto.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * MYSQL 5.0 Protocol Implementation
  3. * Copyright (C)2005-2016 Haxe Foundation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef MY_PROTO_H
  24. #define MY_PROTO_H
  25. #include "mysql.h"
  26. #include "socket.h"
  27. #include "sha1.h"
  28. typedef enum {
  29. FL_LONG_PASSWORD = 1,
  30. FL_FOUND_ROWS = 2,
  31. FL_LONG_FLAG = 4,
  32. FL_CONNECT_WITH_DB = 8,
  33. FL_NO_SCHEMA = 16,
  34. FL_COMPRESS = 32,
  35. FL_ODBC = 64,
  36. FL_LOCAL_FILES = 128,
  37. FL_IGNORE_SPACE = 256,
  38. FL_PROTOCOL_41 = 512,
  39. FL_INTERACTIVE = 1024,
  40. FL_SSL = 2048,
  41. FL_IGNORE_SIGPIPE = 4096,
  42. FL_TRANSACTIONS = 8192,
  43. FL_RESERVED = 16384,
  44. FL_SECURE_CONNECTION = 32768,
  45. FL_MULTI_STATEMENTS = 65536,
  46. FL_MULTI_RESULTS = 131072,
  47. } MYSQL_FLAG;
  48. typedef enum {
  49. COM_SLEEP = 0x00,
  50. COM_QUIT = 0x01,
  51. COM_INIT_DB = 0x02,
  52. COM_QUERY = 0x03,
  53. COM_FIELD_LIST = 0x04,
  54. //COM_CREATE_DB = 0x05,
  55. //COM_DROP_DB = 0x06
  56. COM_REFRESH = 0x07,
  57. COM_SHUTDOWN = 0x08,
  58. COM_STATISTICS = 0x09,
  59. COM_PROCESS_INFO = 0x0A,
  60. //COM_CONNECT = 0x0B,
  61. COM_PROCESS_KILL = 0x0C,
  62. COM_DEBUG = 0x0D,
  63. COM_PING = 0x0E,
  64. //COM_TIME = 0x0F,
  65. //COM_DELAYED_INSERT = 0x10,
  66. COM_CHANGE_USER = 0x11,
  67. COM_BINLOG_DUMP = 0x12,
  68. COM_TABLE_DUMP = 0x13,
  69. COM_CONNECT_OUT = 0x14,
  70. COM_REGISTER_SLAVE = 0x15,
  71. COM_STMT_PREPARE = 0x16,
  72. COM_STMT_EXECUTE = 0x17,
  73. COM_STMT_SEND_LONG_DATA = 0x18,
  74. COM_STMT_CLOSE = 0x19,
  75. COM_STMT_RESET = 0x1A,
  76. COM_SET_OPTION = 0x1B,
  77. COM_STMT_FETCH = 0x1C,
  78. } MYSQL_COMMAND;
  79. typedef enum {
  80. SERVER_STATUS_IN_TRANS = 1,
  81. SERVER_STATUS_AUTOCOMMIT = 2,
  82. SERVER_MORE_RESULTS_EXISTS = 8,
  83. SERVER_QUERY_NO_GOOD_INDEX_USED = 16,
  84. SERVER_QUERY_NO_INDEX_USED = 32,
  85. SERVER_STATUS_CURSOR_EXISTS = 64,
  86. SERVER_STATUS_LAST_ROW_SENT = 128,
  87. SERVER_STATUS_DB_DROPPED = 256,
  88. SERVER_STATUS_NO_BACKSLASH_ESCAPES = 512,
  89. } MYSQL_SERVER_STATUS;
  90. typedef struct {
  91. unsigned char proto_version;
  92. char *server_version;
  93. unsigned int thread_id;
  94. unsigned int server_flags;
  95. unsigned char server_charset;
  96. unsigned short server_status;
  97. } MYSQL_INFOS;
  98. typedef struct {
  99. int id;
  100. int error;
  101. int size;
  102. int pos;
  103. int mem;
  104. char *buf;
  105. } MYSQL_PACKET;
  106. #define MAX_ERR_SIZE 1024
  107. #define IS_QUERY -123456
  108. struct _MYSQL {
  109. PSOCK s;
  110. MYSQL_INFOS infos;
  111. MYSQL_PACKET packet;
  112. int is41;
  113. int errcode;
  114. int last_field_count;
  115. int affected_rows;
  116. int last_insert_id;
  117. char last_error[MAX_ERR_SIZE];
  118. };
  119. typedef struct {
  120. char *raw;
  121. unsigned long *lengths;
  122. char **datas;
  123. } MYSQL_ROW_DATA;
  124. struct _MYSQL_RES {
  125. int nfields;
  126. MYSQL_FIELD *fields;
  127. MYSQL_ROW_DATA *rows;
  128. MYSQL_ROW_DATA *current;
  129. int row_count;
  130. int memory_rows;
  131. };
  132. // network
  133. int myp_recv( MYSQL *m, void *buf, int size );
  134. int myp_send( MYSQL *m, void *buf, int size );
  135. int myp_read_packet( MYSQL *m, MYSQL_PACKET *p );
  136. int myp_send_packet( MYSQL *m, MYSQL_PACKET *p, int *packet_counter );
  137. // packet read
  138. int myp_read( MYSQL_PACKET *p, void *buf, int size );
  139. unsigned char myp_read_byte( MYSQL_PACKET *p );
  140. unsigned short myp_read_ui16( MYSQL_PACKET *p );
  141. int myp_read_int( MYSQL_PACKET *p );
  142. const char *myp_read_string( MYSQL_PACKET *p );
  143. int myp_read_bin( MYSQL_PACKET *p );
  144. char *myp_read_bin_str( MYSQL_PACKET *p );
  145. // packet write
  146. void myp_begin_packet( MYSQL_PACKET *p, int minsize );
  147. void myp_write( MYSQL_PACKET *p, const void *data, int size );
  148. void myp_write_byte( MYSQL_PACKET *p, int b );
  149. void myp_write_ui16( MYSQL_PACKET *p, int b );
  150. void myp_write_int( MYSQL_PACKET *p, int b );
  151. void myp_write_string( MYSQL_PACKET *p, const char *str );
  152. void myp_write_string_eof( MYSQL_PACKET *p, const char *str );
  153. void myp_write_bin( MYSQL_PACKET *p, int size );
  154. // passwords
  155. #define SEED_LENGTH_323 8
  156. void myp_crypt( unsigned char *out, const unsigned char *s1, const unsigned char *s2, unsigned int len );
  157. void myp_encrypt_password( const char *pass, const char *seed, SHA1_DIGEST out );
  158. void myp_encrypt_pass_323( const char *pass, const char seed[SEED_LENGTH_323], char out[SEED_LENGTH_323] );
  159. // escaping
  160. int myp_supported_charset( int charset );
  161. const char *myp_charset_name( int charset );
  162. int myp_escape_string( int charset, char *sout, const char *sin, int length );
  163. int myp_escape_quotes( int charset, char *sout, const char *sin, int length );
  164. #endif
  165. /* ************************************************************************ */