my_bool mariadb_get_infov(MYSQL *mysql, enum mariadb_value value, void *arg, ...)
{
  va_list ap;

  va_start(ap, arg);

  switch(value) {
  case MARIADB_MAX_ALLOWED_PACKET:
    *((size_t *)arg)= (size_t)max_allowed_packet;
    break;
  case MARIADB_NET_BUFFER_LENGTH:
    *((size_t *)arg)= (size_t)net_buffer_length;
    break;
  case MARIADB_CONNECTION_ERROR_ID:
    if (!mysql)
      goto error;
    *((unsigned int *)arg)= mysql->net.last_errno;
    break;
  case MARIADB_CONNECTION_ERROR:
    if (!mysql)
      goto error;
    *((char **)arg)= mysql->net.last_error;
    break;
  case MARIADB_CONNECTION_SQLSTATE:
    if (!mysql)
      goto error;
    *((char **)arg)= mysql->net.sqlstate;
    break;
  case MARIADB_CONNECTION_TLS_VERSION:
    #ifdef HAVE_TLS
    if (mysql && mysql->net.pvio && mysql->net.pvio->ctls)
      *((char **)arg)= (char *)ma_pvio_tls_get_protocol_version(mysql->net.pvio->ctls);
    else
    #endif
      goto error;
    break;
  case MARIADB_CONNECTION_TLS_VERSION_ID:
    #ifdef HAVE_TLS
    if (mysql && mysql->net.pvio && mysql->net.pvio->ctls)
      *((unsigned int *)arg)= ma_pvio_tls_get_protocol_version_id(mysql->net.pvio->ctls);
    else
    #endif
      goto error;
    break;
  case MARIADB_TLS_LIBRARY:
#ifdef HAVE_TLS
    *((const char **)arg)= tls_library_version;
#else
    *((const char **)arg)= "Off";
#endif
    break;
  case MARIADB_CLIENT_VERSION:
    *((const char **)arg)= MARIADB_CLIENT_VERSION_STR;
    break;
  case MARIADB_CLIENT_VERSION_ID:
    *((size_t *)arg)= MARIADB_VERSION_ID;
    break;
  case MARIADB_CONNECTION_SERVER_VERSION:
    if (mysql)
      *((char **)arg)= mysql->server_version;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_SERVER_TYPE:
    if (mysql)
      *((const char **)arg)= mariadb_connection(mysql) ? "MariaDB" : "MySQL";
    else
      goto error;
    break;
  case MARIADB_CONNECTION_SERVER_VERSION_ID:
    if (mysql)
      *((size_t *)arg)= mariadb_server_version_id(mysql);
    else
      goto error;
    break;
  case MARIADB_CONNECTION_PROTOCOL_VERSION_ID:
    if (mysql)
      *((unsigned int *)arg)= mysql->protocol_version;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_MARIADB_CHARSET_INFO:
    if (mysql)
      mariadb_get_charset_info(mysql, (MY_CHARSET_INFO *)arg);
    else
      goto error;
    break;
  case MARIADB_CONNECTION_SOCKET:
    if (mysql)
      *((my_socket *)arg)= mariadb_get_socket(mysql);
    else
      goto error;
    break;
  case MARIADB_CONNECTION_TYPE:
    if (mysql  && mysql->net.pvio)
      *((int *)arg)= (int)mysql->net.pvio->type;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_ASYNC_TIMEOUT_MS:
    if (mysql && mysql->options.extension && mysql->options.extension->async_context)
      *((unsigned int *)arg)= mysql->options.extension->async_context->timeout_value;
    break;
  case MARIADB_CONNECTION_ASYNC_TIMEOUT:
    if (mysql && mysql->options.extension && mysql->options.extension->async_context)
    {
      unsigned int timeout= mysql->options.extension->async_context->timeout_value;
      if (timeout > UINT_MAX - 999)
        *((unsigned int *)arg)= (timeout - 1)/1000 + 1;
      else
        *((unsigned int *)arg)= (timeout+999)/1000;
    }
    break;
  case MARIADB_CHARSET_NAME:
    {
      char *name;
      name= va_arg(ap, char *);
      if (name)
        *((MARIADB_CHARSET_INFO **)arg)= (MARIADB_CHARSET_INFO *)mysql_find_charset_name(name);
      else
        goto error;
    }
    break;
  case MARIADB_CHARSET_ID:
    {
      unsigned int nr;
      nr= va_arg(ap, unsigned int);
      *((MARIADB_CHARSET_INFO **)arg)= (MARIADB_CHARSET_INFO *)mysql_find_charset_nr(nr);
    }
    break;
  case MARIADB_CONNECTION_SSL_CIPHER:
    #ifdef HAVE_TLS
    if (mysql && mysql->net.pvio && mysql->net.pvio->ctls)
      *((char **)arg)= (char *)ma_pvio_tls_cipher(mysql->net.pvio->ctls);
    else
    #endif
      goto error;
    break;
  case MARIADB_CLIENT_ERRORS:
    *((char ***)arg)= (char **)client_errors;
    break;
  case MARIADB_CONNECTION_INFO:
    if (mysql)
      *((char **)arg)= (char *)mysql->info;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_PVIO_TYPE:
    if (mysql && mysql->net.pvio)
      *((unsigned int *)arg)= (unsigned int)mysql->net.pvio->type;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_SCHEMA:
    if (mysql)
      *((char **)arg)= mysql->db;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_USER:
    if (mysql)
      *((char **)arg)= mysql->user;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_PORT:
    if (mysql)
      *((unsigned int *)arg)= mysql->port;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_UNIX_SOCKET:
    if (mysql)
      *((char **)arg)= mysql->unix_socket;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_HOST:
    if (mysql)
      *((char **)arg)= mysql->host;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_SERVER_STATUS:
    if (mysql)
      *((unsigned int *)arg)= mysql->server_status;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_SERVER_CAPABILITIES:
    if (mysql)
      *((unsigned long *)arg)= mysql->server_capabilities;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_EXTENDED_SERVER_CAPABILITIES:
    if (mysql)
      *((unsigned long *)arg)= mysql->extension->mariadb_server_capabilities;
    else
      goto error;
    break;
  case MARIADB_CONNECTION_CLIENT_CAPABILITIES:
    if (mysql)
      *((unsigned long *)arg)= mysql->client_flag;
    else
      goto error;
    break;
  default:
    va_end(ap);
    return(-1);
  }
  va_end(ap);
  return(0);
error:
  va_end(ap);
  return(-1);
}

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

static MYSQL_PARAMETERS mariadb_internal_parameters= {&max_allowed_packet, &net_buffer_length, 0};

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern char **get_default_configuration_dirs();

extern my_bool  ma_init_done;

extern my_bool  mysql_ps_subsystem_initialized;

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern my_bool mthd_supported_buffer_type(enum enum_field_types type);

extern my_bool mthd_stmt_read_prepare_response(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_param_metadata(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_result_metadata(MYSQL_STMT *stmt);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

extern void mysql_client_plugin_deinit();

char* getlogin(void);

enum enum_option_type {
  MARIADB_OPTION_NONE,
  MARIADB_OPTION_BOOL,
  MARIADB_OPTION_INT,
  MARIADB_OPTION_SIZET,
  MARIADB_OPTION_STR,
}

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


char *ma_send_connect_attr(MYSQL *mysql, unsigned char *buffer)
{
  if (mysql->server_capabilities & CLIENT_CONNECT_ATTRS)
  {
    buffer= (unsigned char *)mysql_net_store_length((unsigned char *)buffer, (mysql->options.extension) ?
                             mysql->options.extension->connect_attrs_len : 0);
    if (mysql->options.extension &&
        hash_inited(&mysql->options.extension->connect_attrs))
    {
      uint i;
      for (i=0; i < mysql->options.extension->connect_attrs.records; i++)
      {
        size_t len;
        uchar *p= hash_element(&mysql->options.extension->connect_attrs, i);

        len= strlen((char *)p);
        buffer= mysql_net_store_length(buffer, len);
        memcpy(buffer, p, len);
        buffer+= (len);
        p+= (len + 1);
        len= strlen((char *)p);
        buffer= mysql_net_store_length(buffer, len);
        memcpy(buffer, p, len);
        buffer+= len;
      }
    }
  }
  return (char *)buffer;
}

sanity checks for client-supplied OK packet content

reported by Matthias Kaiser, Apple Information Security

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

uint mysql_port=0;

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


int mthd_my_read_query_result(MYSQL *mysql)
{
  uchar *pos;
  ulong field_count;
  MYSQL_DATA *fields;
  ulong length;
  my_bool can_local_infile= (mysql->options.extension) && (mysql->extension->auto_local_infile != WAIT_FOR_QUERY);

  if (mysql->options.extension && mysql->extension->auto_local_infile == ACCEPT_FILE_REQUEST)
    mysql->extension->auto_local_infile= WAIT_FOR_QUERY;

  if ((length = ma_net_safe_read(mysql)) == packet_error)
  {
    return(1);
  }
  free_old_query(mysql);			/* Free old result */
get_info:
  pos=(uchar*) mysql->net.read_pos;
  if ((field_count= net_field_length(&pos)) == 0)
    return ma_read_ok_packet(mysql, pos, length);
  if (field_count == NULL_LENGTH)		/* LOAD DATA LOCAL INFILE */
  {
    int error=mysql_handle_local_infile(mysql, (char *)pos, can_local_infile);

    if ((length=ma_net_safe_read(mysql)) == packet_error || error)
      return(-1);
    goto get_info;				/* Get info packet */
  }
  if (!(mysql->server_status & SERVER_STATUS_AUTOCOMMIT))
    mysql->server_status|= SERVER_STATUS_IN_TRANS;

  mysql->extra_info= net_field_length_ll(&pos); /* Maybe number of rec */
  if (!(fields=mysql->methods->db_read_rows(mysql,(MYSQL_FIELD*) 0,
                                            ma_result_set_rows(mysql))))
    return(-1);
  if (!(mysql->fields=unpack_fields(mysql, fields, &mysql->field_alloc,
				    (uint) field_count, 1)))
    return(-1);
  mysql->status=MYSQL_STATUS_GET_RESULT;
  mysql->field_count=field_count;
  return(0);
}

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool  ma_init_done;

extern my_bool  mysql_ps_subsystem_initialized;

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern my_bool mthd_supported_buffer_type(enum enum_field_types type);

extern my_bool mthd_stmt_read_prepare_response(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_param_metadata(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_result_metadata(MYSQL_STMT *stmt);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

uint mysql_port=0;

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


static void ignore_sigpipe()
{
  signal(SIGPIPE, SIG_IGN);
}

static MYSQL_PARAMETERS mariadb_internal_parameters= {&max_allowed_packet, &net_buffer_length, 0};

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern void mysql_client_plugin_deinit();

char* getlogin(void);

static MARIADB_CONST_STRING null_const_string= {0,0};

static size_t rset_field_offsets[]= {
  OFFSET(MYSQL_FIELD, catalog),
  OFFSET(MYSQL_FIELD, catalog_length),
  OFFSET(MYSQL_FIELD, db),
  OFFSET(MYSQL_FIELD, db_length),
  OFFSET(MYSQL_FIELD, table),
  OFFSET(MYSQL_FIELD, table_length),
  OFFSET(MYSQL_FIELD, org_table),
  OFFSET(MYSQL_FIELD, org_table_length),
  OFFSET(MYSQL_FIELD, name),
  OFFSET(MYSQL_FIELD, name_length),
  OFFSET(MYSQL_FIELD, org_name),
  OFFSET(MYSQL_FIELD, org_name_length)
};

#define ignore_sigpipe()

static INIT_ONCE init_once= INIT_ONCE_STATIC_INIT;

static pthread_once_t init_once = PTHREAD_ONCE_INIT;


mysql_query(MYSQL *mysql, const char *query)
{
  return mysql_real_query(mysql,query, (unsigned long) strlen(query));
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


void ma_int_hash_free(void *p)
{
  free(p);
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

extern void mysql_client_plugin_deinit();

char* getlogin(void);

#define OPT_SET_EXTENDED_VALUE_STR(OPTS, KEY, VAL)               \
    CHECK_OPT_EXTENSION_SET(OPTS)                                \
    free((gptr)(OPTS)->extension->KEY);                          \
    if((VAL))                                                   \
      (OPTS)->extension->KEY= strdup((char *)(VAL));             \
    else                                                         \
      (OPTS)->extension->KEY= NULL

#define OPT_SET_VALUE_STR(OPTS, KEY, VAL)                        \
    free((OPTS)->KEY);                                           \
    if((VAL))                                                   \
      (OPTS)->KEY= strdup((char *)(VAL));                        \
    else                                                         \
      (OPTS)->KEY= NULL


void free_rows(MYSQL_DATA *cur)
{
  if (cur)
  {
    ma_free_root(&cur->alloc,MYF(0));
    free(cur);
  }
}


static void free_old_query(MYSQL *mysql)
{
  if (mysql->fields)
    ma_free_root(&mysql->field_alloc,MYF(0));
  ma_init_alloc_root(&mysql->field_alloc,8192,0);	/* Assume rowlength < 8192 */
  mysql->fields=0;
  mysql->field_count=0;				/* For API */
  mysql->info= 0;
  return;
}

static MYSQL_PARAMETERS mariadb_internal_parameters= {&max_allowed_packet, &net_buffer_length, 0};

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern void mysql_client_plugin_deinit();

char* getlogin(void);


int ma_read_ok_packet(MYSQL *mysql, uchar *pos, ulong length)
{
  uchar *end= mysql->net.read_pos+length;
  size_t item_len;
  mysql->affected_rows= net_field_length_ll(&pos);
  mysql->insert_id=	  net_field_length_ll(&pos);
  mysql->server_status=uint2korr(pos);
  pos+=2;
  mysql->warning_count=uint2korr(pos);
  pos+=2;
  if (pos > end)
    goto corrupted;
  if (pos < end)
  {
    if ((item_len= net_field_length(&pos)))
      mysql->info=(char*) pos;
    if (pos + item_len > end)
      goto corrupted;

    /* check if server supports session tracking */
    if (mysql->server_capabilities & CLIENT_SESSION_TRACKING)
    {
      ma_clear_session_state(mysql);
      pos+= item_len;

      if (mysql->server_status & SERVER_SESSION_STATE_CHANGED)
      {
        int i;
        if (pos < end)
        {
          LIST *session_item;
          MYSQL_LEX_STRING *str= NULL;
          enum enum_session_state_type si_type;
          uchar *old_pos= pos;

          item_len= net_field_length(&pos);  /* length for all items */
          if (pos + item_len > end)
            goto corrupted;
          end= pos + item_len;

          /* length was already set, so make sure that info will be zero terminated */
          if (mysql->info)
            *old_pos= 0;

          while (pos < end)
          {
            size_t plen;
            char *data;
            si_type= (enum enum_session_state_type)net_field_length(&pos);
            switch(si_type) {
            case SESSION_TRACK_SCHEMA:
            case SESSION_TRACK_STATE_CHANGE:
            case SESSION_TRACK_TRANSACTION_CHARACTERISTICS:
            case SESSION_TRACK_SYSTEM_VARIABLES:
              if (si_type != SESSION_TRACK_STATE_CHANGE)
                net_field_length(&pos); /* ignore total length, item length will follow next */
              plen= net_field_length(&pos);
              if (pos + plen > end)
                goto corrupted;
              if (!(session_item= ma_multi_malloc(0,
                                  &session_item, sizeof(LIST),
                                  &str, sizeof(MYSQL_LEX_STRING),
                                  &data, plen,
                                  NULL)))
                  goto oom;
              str->length= plen;
              str->str= data;
              memcpy(str->str, (char *)pos, plen);
              pos+= plen;
              session_item->data= str;
              mysql->extension->session_state[si_type].list= list_add(mysql->extension->session_state[si_type].list, session_item);

              /* in case schema has changed, we have to update mysql->db */
              if (si_type == SESSION_TRACK_SCHEMA)
              {
                free(mysql->db);
                mysql->db= malloc(plen + 1);
                memcpy(mysql->db, str->str, plen);
                mysql->db[plen]= 0;
              }
              else if (si_type == SESSION_TRACK_SYSTEM_VARIABLES)
              {
                my_bool set_charset= 0;
                /* make sure that we update charset in case it has changed */
                if (!strncmp(str->str, "character_set_client", str->length))
                  set_charset= 1;
                plen= net_field_length(&pos);
                if (pos + plen > end)
                  goto corrupted;
                if (!(session_item= ma_multi_malloc(0,
                                    &session_item, sizeof(LIST),
                                    &str, sizeof(MYSQL_LEX_STRING),
                                    &data, plen,
                                    NULL)))
                  goto oom;
                str->length= plen;
                str->str= data;
                memcpy(str->str, (char *)pos, plen);
                pos+= plen;
                session_item->data= str;
                mysql->extension->session_state[si_type].list= list_add(mysql->extension->session_state[si_type].list, session_item);
                if (set_charset && str->length < CHARSET_NAME_LEN &&
                    strncmp(mysql->charset->csname, str->str, str->length) != 0)
                {
                  char cs_name[CHARSET_NAME_LEN];
                  const MARIADB_CHARSET_INFO *cs_info;
                  memcpy(cs_name, str->str, str->length);
                  cs_name[str->length]= 0;
                  if ((cs_info = mysql_find_charset_name(cs_name)))
                    mysql->charset= cs_info;
                }
              }
              break;
            default:
              /* not supported yet */
              plen= net_field_length(&pos);
              if (pos + plen > end)
                goto corrupted;
              pos+= plen;
              break;
            }
          }
        }
        for (i= SESSION_TRACK_BEGIN; i <= SESSION_TRACK_END; i++)
        {
          mysql->extension->session_state[i].list= list_reverse(mysql->extension->session_state[i].list);
          mysql->extension->session_state[i].current= mysql->extension->session_state[i].list;
        }
      }
    }
  }
  /* CONC-351: clear session state information */
  else if (mysql->server_capabilities & CLIENT_SESSION_TRACKING)
    ma_clear_session_state(mysql);
  return(0);

oom:
  ma_clear_session_state(mysql);
  SET_CLIENT_ERROR(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
  return -1;

corrupted:
  ma_clear_session_state(mysql);
  SET_CLIENT_ERROR(mysql, CR_MALFORMED_PACKET, SQLSTATE_UNKNOWN, 0);
  return -1;
}

#define CHARSET_NAME_LEN 64

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool  ma_init_done;

extern my_bool  mysql_ps_subsystem_initialized;

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern LIST *pvio_callback;

extern my_bool mthd_supported_buffer_type(enum enum_field_types type);

extern my_bool mthd_stmt_read_prepare_response(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_param_metadata(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_result_metadata(MYSQL_STMT *stmt);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

char* getlogin(void);

enum enum_option_type {
  MARIADB_OPTION_NONE,
  MARIADB_OPTION_BOOL,
  MARIADB_OPTION_INT,
  MARIADB_OPTION_SIZET,
  MARIADB_OPTION_STR,
}

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql)
{
  return (mysql)->insert_id;
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);


ma_net_safe_read(MYSQL *mysql)
{
  NET *net= &mysql->net;
  ulong len=0;

restart:
  if (net->pvio != 0)
    len=ma_net_read(net);

  if (len == packet_error || len == 0)
  {
    end_server(mysql);
    my_set_error(mysql, net->last_errno == ER_NET_PACKET_TOO_LARGE ?
		     CR_NET_PACKET_TOO_LARGE:
		     CR_SERVER_LOST,
         SQLSTATE_UNKNOWN, 0, errno);
    return(packet_error);
  }
  if (net->read_pos[0] == 255)
  {
    if (len > 3)
    {
      char *pos=(char*) net->read_pos+1;
      uint last_errno=uint2korr(pos);
      pos+=2;
      len-=2;

      if (last_errno== 65535 &&
          ((mariadb_connection(mysql) && (mysql->server_capabilities & CLIENT_PROGRESS)) ||
           (!(mysql->extension->mariadb_server_capabilities & MARIADB_CLIENT_PROGRESS << 32))))
      {
        if (cli_report_progress(mysql, (uchar *)pos, (uint) (len-1)))
        {
          /* Wrong packet */
          my_set_error(mysql, CR_MALFORMED_PACKET, SQLSTATE_UNKNOWN, 0);
          return (packet_error);
        }
        goto restart;
      }
      net->last_errno= last_errno;
      if (pos[0]== '#')
      {
        ma_strmake(net->sqlstate, pos+1, SQLSTATE_LENGTH);
        pos+= SQLSTATE_LENGTH + 1;
      }
      else
      {
        strncpy(net->sqlstate, SQLSTATE_UNKNOWN, SQLSTATE_LENGTH);
      }
      ma_strmake(net->last_error,(char*) pos,
              min(len,sizeof(net->last_error)-1));
    }
    else
    {
      my_set_error(mysql, CR_UNKNOWN_ERROR, SQLSTATE_UNKNOWN, 0);
    }

    mysql->server_status&= ~SERVER_MORE_RESULTS_EXIST;

    return(packet_error);
  }
  return len;
}

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

uint mysql_port=0;

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


mysql_list_processes(MYSQL *mysql)
{
  MYSQL_DATA *fields;
  uint field_count;
  uchar *pos;

  LINT_INIT(fields);
  if (ma_simple_command(mysql, COM_PROCESS_INFO,0,0,0,0))
    return(NULL);
  free_old_query(mysql);
  pos=(uchar*) mysql->net.read_pos;
  field_count=(uint) net_field_length(&pos);
  if (!(fields = mysql->methods->db_read_rows(mysql,(MYSQL_FIELD*) 0,7)))
    return(NULL);
  if (!(mysql->fields= unpack_fields(mysql, fields, &mysql->field_alloc,
                                     field_count, 0)))
    return(NULL);
  mysql->status=MYSQL_STATUS_GET_RESULT;
  mysql->field_count=field_count;
  return(mysql_store_result(mysql));
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

uint mysql_port=0;

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);


net_field_length(uchar **packet)
{
  reg1 uchar *pos= *packet;
  if (*pos < 251)
  {
    (*packet)++;
    return (ulong) *pos;
  }
  if (*pos == 251)
  {
    (*packet)++;
    return NULL_LENGTH;
  }
  if (*pos == 252)
  {
    (*packet)+=3;
    return (ulong) uint2korr(pos+1);
  }
  if (*pos == 253)
  {
    (*packet)+=4;
    return (ulong) uint3korr(pos+1);
  }
  (*packet)+=9;					/* Must be 254 when here */
  return (ulong) uint4korr(pos+1);
}

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);


mysql_ping(MYSQL *mysql)
{
  int rc;
  rc= ma_simple_command(mysql, COM_PING, 0, 0, 0, 0);
  if (rc && mysql->options.reconnect)
    rc= ma_simple_command(mysql, COM_PING, 0, 0, 0, 0);
  return rc;
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();


unsigned int STDCALL mysql_num_fields(MYSQL_RES *res)
{
  return res->field_count;
}

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();


void ma_invalidate_stmts(MYSQL *mysql, const char *function_name)
{
  if (mysql->stmts)
  {
    LIST *li_stmt= mysql->stmts;

    for (; li_stmt; li_stmt= li_stmt->next)
    {
      MYSQL_STMT *stmt= (MYSQL_STMT *)li_stmt->data;
      stmt->mysql= NULL;
      SET_CLIENT_STMT_ERROR(stmt, CR_STMT_CLOSED, SQLSTATE_UNKNOWN, function_name);
    }
    mysql->stmts= NULL;
  }
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern LIST *pvio_callback;

extern my_bool mthd_stmt_read_prepare_response(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_param_metadata(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_result_metadata(MYSQL_STMT *stmt);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern void mysql_client_plugin_deinit();

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


mysql_get_socket(MYSQL *mysql)
{
  return mariadb_get_socket(mysql);
}


static void mariadb_get_charset_info(MYSQL *mysql, MY_CHARSET_INFO *cs)
{
  if (!cs)
    return;

  cs->number= mysql->charset->nr;
  cs->csname=  mysql->charset->csname;
  cs->name= mysql->charset->name;
  cs->state= 0;
  cs->comment= NULL;
  cs->dir= NULL;
  cs->mbminlen= mysql->charset->char_minlen;
  cs->mbmaxlen= mysql->charset->char_maxlen;

  return;
}

static MYSQL_PARAMETERS mariadb_internal_parameters= {&max_allowed_packet, &net_buffer_length, 0};

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern void mysql_client_plugin_deinit();

char* getlogin(void);


MYSQL_DATA *mthd_my_read_rows(MYSQL *mysql,MYSQL_FIELD *mysql_fields,
			     uint fields)
{
  uint	field;
  ulong pkt_len;
  ulong len;
  uchar *cp;
  char	*to, *end_to;
  MYSQL_DATA *result;
  MYSQL_ROWS **prev_ptr,*cur;
  NET *net = &mysql->net;

  if ((pkt_len= ma_net_safe_read(mysql)) == packet_error)
    return(0);
  if (!(result=(MYSQL_DATA*) calloc(1, sizeof(MYSQL_DATA))))
  {
    SET_CLIENT_ERROR(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
    return(0);
  }
  ma_init_alloc_root(&result->alloc,8192,0);	/* Assume rowlength < 8192 */
  result->alloc.min_malloc=sizeof(MYSQL_ROWS);
  prev_ptr= &result->data;
  result->rows=0;
  result->fields=fields;

  while (*(cp=net->read_pos) != 254 || pkt_len >= 8)
  {
    result->rows++;
    if (!(cur= (MYSQL_ROWS*) ma_alloc_root(&result->alloc,
					    sizeof(MYSQL_ROWS))) ||
	      !(cur->data= ((MYSQL_ROW)
		      ma_alloc_root(&result->alloc,
				     (fields+1)*sizeof(char *)+fields+pkt_len))))
    {
      free_rows(result);
      SET_CLIENT_ERROR(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
      return(0);
    }
    *prev_ptr=cur;
    prev_ptr= &cur->next;
    to= (char*) (cur->data+fields+1);
    end_to=to+fields+pkt_len-1;
    for (field=0 ; field < fields ; field++)
    {
      if ((len=(ulong) net_field_length(&cp)) == NULL_LENGTH)
      {						/* null field */
        cur->data[field] = 0;
      }
      else
      {
        cur->data[field] = to;
        if (len > (ulong)(end_to - to) || to > end_to)
        {
          free_rows(result);
          SET_CLIENT_ERROR(mysql, CR_UNKNOWN_ERROR, SQLSTATE_UNKNOWN, 0);
          return(0);
        }
        memcpy(to,(char*) cp,len); to[len]=0;
        to+=len+1;
        cp+=len;
        if (mysql_fields)
        {
          if (mysql_fields[field].max_length < len)
            mysql_fields[field].max_length=len;
         }
      }
    }
    cur->data[field]=to;			/* End of last field */
    if ((pkt_len=ma_net_safe_read(mysql)) == packet_error)
    {
      free_rows(result);
      return(0);
    }
  }
  *prev_ptr=0;					/* last pointer is null */
  /* save status */
  if (pkt_len > 1)
  {
    cp++;
    mysql->warning_count= uint2korr(cp);
    cp+= 2;
    mysql->server_status= uint2korr(cp);
  }
  return(result);
}


mysql_real_escape_string(MYSQL *mysql, char *to,const char *from,
			 ulong length)
{
  if (mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
    return (ulong)mysql_cset_escape_quotes(mysql->charset, to, from, length);
  else
    return (ulong)mysql_cset_escape_slashes(mysql->charset, to, from, length);
}

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


my_bool STDCALL mysql_thread_init(void)
{
  return 0;
}

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern my_bool  ma_init_done;

extern my_bool  mysql_ps_subsystem_initialized;

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern my_bool mthd_supported_buffer_type(enum enum_field_types type);

extern my_bool mthd_stmt_read_prepare_response(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_param_metadata(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_result_metadata(MYSQL_STMT *stmt);

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

extern void mysql_client_plugin_deinit();

char* getlogin(void);


static size_t get_store_length(size_t length)
{
  #define MAX_STORE_SIZE 9
  unsigned char buffer[MAX_STORE_SIZE], *p;

  /* We just store the length and substract offset of our buffer
     to determine the length */
  p= mysql_net_store_length(buffer, length);
  return p - buffer;
}

# define __has_feature(x) 0

#define INADDR_NONE -1

#define strncasecmp _strnicmp

#define ASYNC_CONTEXT_DEFAULT_STACK_SIZE (4096*15)

#define MA_RPL_VERSION_HACK "5.5.5-"

#define CHARSET_NAME_LEN 64

static MYSQL_PARAMETERS mariadb_internal_parameters= {&max_allowed_packet, &net_buffer_length, 0};

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

#define CONNECT_TIMEOUT 0

#define perror(A)

#define SOCKET_ERROR -1

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


ulong STDCALL mysql_hex_string(char *to, const char *from, unsigned long len)
{
  char *start= to;
  char hexdigits[]= "0123456789ABCDEF";

  while (len--)
  {
    *to++= hexdigits[((unsigned char)*from) >> 4];
    *to++= hexdigits[((unsigned char)*from) & 0x0F];
    from++;
  }
  *to= 0;
  return (ulong)(to - start);
}

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


uchar *ma_get_hash_keyval(const uchar *hash_entry,
                       unsigned int *length,
                       my_bool not_used __attribute__((unused)))
{
  /* Hash entry has the following format:
     Offset: 0               key (\0 terminated)
             key_length + 1  value (\0 terminated)
  */
  uchar *p= (uchar *)hash_entry;
  size_t len= strlen((char *)p);
  *length= (unsigned int)len;
  return p;
}

static my_bool mysql_client_init=0;

extern char **get_default_configuration_dirs();

extern my_bool  ma_init_done;

extern my_bool  mysql_ps_subsystem_initialized;

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern my_bool mthd_supported_buffer_type(enum enum_field_types type);

extern my_bool mthd_stmt_read_prepare_response(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_param_metadata(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_result_metadata(MYSQL_STMT *stmt);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


mysql_get_timeout_value(const MYSQL *mysql)
{
  unsigned int timeout= 0;

  if (mysql->options.extension && mysql->options.extension->async_context)
    timeout= mysql->options.extension->async_context->timeout_value;
  /* Avoid overflow. */
  if (timeout > UINT_MAX - 999)
    return (timeout - 1)/1000 + 1;
  else
    return (timeout+999)/1000;
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


mysql_get_server_name(MYSQL *mysql)
{
  if (mysql->options.extension &&
      mysql->options.extension->db_driver != NULL)
    return mysql->options.extension->db_driver->name;
  return mariadb_connection(mysql) ? "MariaDB" : "MySQL";
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);


unpack_fields(const MYSQL *mysql,
              MYSQL_DATA *data, MA_MEM_ROOT *alloc, uint fields,
	      my_bool default_value)
{
  MYSQL_ROWS	*row;
  MYSQL_FIELD	*field,*result;
  char    *p;
  unsigned int i, field_count= sizeof(rset_field_offsets)/sizeof(size_t)/2;

  field=result=(MYSQL_FIELD*) ma_alloc_root(alloc,sizeof(MYSQL_FIELD)*fields);
  if (!result)
    return(0);

  for (row=data->data; row ; row = row->next,field++)
  {
    if (field >= result + fields)
      goto error;

    for (i=0; i < field_count; i++)
    {
      uint length= (uint)(row->data[i+1] - row->data[i] - 1);
      if (!row->data[i] && row->data[i][length])
        goto error;

      *(char **)(((char *)field) + rset_field_offsets[i*2])=
        ma_strdup_root(alloc, (char *)row->data[i]);
      *(unsigned int *)(((char *)field) + rset_field_offsets[i*2+1])= length;
    }

    field->extension= NULL;
    if (ma_has_extended_type_info(mysql))
    {
      if (row->data[i+1] - row->data[i] > 1)
      {
        size_t len= row->data[i+1] - row->data[i] - 1;
        MA_FIELD_EXTENSION *ext= new_ma_field_extension(alloc);
        if ((field->extension= ext))
          ma_field_extension_init_type_info(alloc, ext, row->data[i], len);
      }
      i++;
    }

    p= (char *)row->data[i];
    /* filler */
    field->charsetnr= uint2korr(p);
    p+= 2;
    field->length= (uint) uint4korr(p);
    p+= 4;
    field->type=   (enum enum_field_types)uint1korr(p);
    p++;
    field->flags= uint2korr(p);
    p+= 2;
    field->decimals= (uint) p[0];
    p++;

    /* filler */
    p+= 2;

    if (INTERNAL_NUM_FIELD(field))
      field->flags|= NUM_FLAG;

    i++;
    /* This is used by deprecated function mysql_list_fields only,
       however the reported length is not correct, so we always zero it */
    if (default_value && row->data[i])
      field->def=ma_strdup_root(alloc,(char*) row->data[i]);
    else
      field->def=0;
    field->def_length= 0;

    field->max_length= 0;
  }
  if (field < result + fields)
    goto error;
  free_rows(data);				/* Free old data */
  return(result);
error:
  free_rows(data);
  ma_free_root(alloc, MYF(0));
  return(0);
}

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool  ma_init_done;

extern my_bool  mysql_ps_subsystem_initialized;

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern my_bool mthd_supported_buffer_type(enum enum_field_types type);

extern my_bool mthd_stmt_read_prepare_response(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_param_metadata(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_result_metadata(MYSQL_STMT *stmt);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

uint mysql_port=0;

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

char* getlogin(void);

enum enum_option_type {
  MARIADB_OPTION_NONE,
  MARIADB_OPTION_BOOL,
  MARIADB_OPTION_INT,
  MARIADB_OPTION_SIZET,
  MARIADB_OPTION_STR,
}

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


void STDCALL mysql_get_character_set_info(MYSQL *mysql, MY_CHARSET_INFO *cs)
{
  mariadb_get_charset_info(mysql, cs);
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern void mysql_client_plugin_deinit();

char* getlogin(void);

struct my_hook_data {
  MYSQL *orig_mysql;
  MYSQL *new_mysql;
  /* This is always NULL currently, but restoring does not hurt just in case. */
  MARIADB_PVIO *orig_pvio;
}


mysql_list_fields(MYSQL *mysql, const char *table, const char *wild)
{
  MYSQL_RES *result;
  MYSQL_DATA *query;
  char buff[255];
  int length= 0;

  LINT_INIT(query);

  length= snprintf(buff, 128, "%s%c%s", table, '\0', wild ? wild : "");

  if (ma_simple_command(mysql, COM_FIELD_LIST,buff,length,1,0) ||
      !(query = mysql->methods->db_read_rows(mysql,(MYSQL_FIELD*) 0,
                                             ma_result_set_rows(mysql))))
    return(NULL);

  free_old_query(mysql);
  if (!(result = (MYSQL_RES *) calloc(1, sizeof(MYSQL_RES))))
  {
    free_rows(query);
    return(NULL);
  }
  result->field_alloc=mysql->field_alloc;
  mysql->fields=0;
  result->eof=1;
  result->field_count = (uint) query->rows;
  result->fields= unpack_fields(mysql, query, &result->field_alloc,
				result->field_count, 1);
  if (result->fields)
    return(result);

  free(result);
  return(NULL);
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

uint mysql_port=0;

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


MARIADB_CHARSET_INFO * STDCALL mariadb_get_charset_by_nr(unsigned int csnr)
{
  return (MARIADB_CHARSET_INFO *)mysql_find_charset_nr(csnr);
}

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();


unsigned long STDCALL mysql_get_server_version(MYSQL *mysql)
{
  return (unsigned long)mariadb_server_version_id(mysql);
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);


ma_set_connect_attrs(MYSQL *mysql, const char *host)
{
  char buffer[255];
  int rc= 0;

  rc= mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "_client_name") +
      mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "_client_version") +
      mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "_os") +
      mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "_server_host") +
#ifdef _WIN32
      mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "_thread") +
#endif
      mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "_pid") +
      mysql_options(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, "_platform");

  rc+= mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "libmariadb")
       + mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_version", MARIADB_PACKAGE_VERSION)
       + mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "_os", MARIADB_SYSTEM_TYPE);

  if (host && *host)
    rc+= mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "_server_host", host);

#ifdef _WIN32
  snprintf(buffer, 255, "%lu", (ulong) GetCurrentThreadId());
  rc+= mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "_thread", buffer);
  snprintf(buffer, 255, "%lu", (ulong) GetCurrentProcessId());
#else
  snprintf(buffer, 255, "%lu", (ulong) getpid());
#endif
  rc+= mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "_pid", buffer);

  rc+= mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, "_platform", MARIADB_MACHINE_TYPE);
  return(test(rc>0));
}

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


int STDCALL mysql_session_track_get_first(MYSQL *mysql, enum enum_session_state_type type,
                                          const char **data, size_t *length)
{
  mysql->extension->session_state[type].current= mysql->extension->session_state[type].list;
  return mysql_session_track_get_next(mysql, type, data, length);
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern my_bool mthd_supported_buffer_type(enum enum_field_types type);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

char* getlogin(void);

enum enum_option_type {
  MARIADB_OPTION_NONE,
  MARIADB_OPTION_BOOL,
  MARIADB_OPTION_INT,
  MARIADB_OPTION_SIZET,
  MARIADB_OPTION_STR,
}

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


mysql_optionsv(MYSQL *mysql,enum mysql_option option, ...)
{
  va_list ap;
  void *arg1;
  size_t stacksize;
  struct mysql_async_context *ctxt;

  va_start(ap, option);

  arg1= va_arg(ap, void *);

  switch (option) {
  case MYSQL_OPT_CONNECT_TIMEOUT:
    mysql->options.connect_timeout= *(uint*) arg1;
    break;
  case MYSQL_OPT_COMPRESS:
    mysql->options.compress= 1;			/* Remember for connect */
    mysql->options.client_flag|= CLIENT_COMPRESS;
    break;
  case MYSQL_OPT_NAMED_PIPE:
    mysql->options.named_pipe=1;		/* Force named pipe */
    break;
  case MYSQL_OPT_LOCAL_INFILE:			/* Allow LOAD DATA LOCAL ?*/
    if (!arg1 || test(*(unsigned int*) arg1))
      mysql->options.client_flag|= CLIENT_LOCAL_FILES;
    else
      mysql->options.client_flag&= ~CLIENT_LOCAL_FILES;
    if (arg1) {
      CHECK_OPT_EXTENSION_SET(&mysql->options);
      mysql->extension->auto_local_infile= *(uint*)arg1 == LOCAL_INFILE_MODE_AUTO
                                           ? WAIT_FOR_QUERY : ALWAYS_ACCEPT;
    }
    break;
  case MYSQL_INIT_COMMAND:
    options_add_initcommand(&mysql->options, (char *)arg1);
    break;
  case MYSQL_READ_DEFAULT_FILE:
    OPT_SET_VALUE_STR(&mysql->options, my_cnf_file, (char *)arg1);
    break;
  case MYSQL_READ_DEFAULT_GROUP:
    OPT_SET_VALUE_STR(&mysql->options, my_cnf_group, arg1 ? (char *)arg1 : "");
    break;
  case MYSQL_SET_CHARSET_DIR:
    OPT_SET_VALUE_STR(&mysql->options, charset_dir, arg1);
    break;
  case MYSQL_SET_CHARSET_NAME:
    OPT_SET_VALUE_STR(&mysql->options, charset_name, arg1);
    break;
  case MYSQL_OPT_RECONNECT:
    mysql->options.reconnect= *(my_bool *)arg1;
    break;
  case MYSQL_OPT_PROTOCOL:
    mysql->options.protocol= *((uint *)arg1);
    break;
#ifdef _WIN32
  case MYSQL_SHARED_MEMORY_BASE_NAME:
    OPT_SET_VALUE_STR(&mysql->options, shared_memory_base_name, arg1);
    break;
#endif
  case MYSQL_OPT_READ_TIMEOUT:
    mysql->options.read_timeout= *(uint *)arg1;
    break;
  case MYSQL_OPT_WRITE_TIMEOUT:
    mysql->options.write_timeout= *(uint *)arg1;
    break;
  case MYSQL_REPORT_DATA_TRUNCATION:
    mysql->options.report_data_truncation= *(my_bool *)arg1;
    break;
  case MYSQL_PROGRESS_CALLBACK:
    CHECK_OPT_EXTENSION_SET(&mysql->options);
    if (mysql->options.extension)
      mysql->options.extension->report_progress=
        (void (*)(const MYSQL *, uint, uint, double, const char *, uint)) arg1;
    break;
  case MYSQL_SERVER_PUBLIC_KEY:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, server_public_key, (char *)arg1);
    break;
  case MYSQL_PLUGIN_DIR:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, plugin_dir, (char *)arg1);
    break;
  case MYSQL_DEFAULT_AUTH:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, default_auth, (char *)arg1);
    break;
  case MYSQL_OPT_NONBLOCK:
    if (mysql->options.extension &&
        (ctxt = mysql->options.extension->async_context) != 0)
    {
      /*
        We must not allow changing the stack size while a non-blocking call is
        suspended (as the stack is then in use).
      */
      if (ctxt->suspended)
        goto end;
      my_context_destroy(&ctxt->async_context);
      free(ctxt);
    }
    if (!(ctxt= (struct mysql_async_context *)
          calloc(1, sizeof(*ctxt))))
    {
      SET_CLIENT_ERROR(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
      goto end;
    }
    stacksize= 0;
    if (arg1)
      stacksize= *(const size_t *)arg1;
    if (!stacksize)
      stacksize= ASYNC_CONTEXT_DEFAULT_STACK_SIZE;
    if (my_context_init(&ctxt->async_context, stacksize))
    {
      free(ctxt);
      goto end;
    }
    if (!mysql->options.extension)
      if(!(mysql->options.extension= (struct st_mysql_options_extension *)
        calloc(1, sizeof(struct st_mysql_options_extension))))
      {
        free(ctxt);
        SET_CLIENT_ERROR(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
        goto end;
      }
    mysql->options.extension->async_context= ctxt;
    break;
  case MYSQL_OPT_MAX_ALLOWED_PACKET:
    if (mysql)
      mysql->options.max_allowed_packet= (unsigned long)(*(size_t *)arg1);
    else
      max_allowed_packet= (unsigned long)(*(size_t *)arg1);
    break;
  case MYSQL_OPT_NET_BUFFER_LENGTH:
    net_buffer_length= (unsigned long)(*(size_t *)arg1);
    break;
  case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
    if (*(my_bool *)arg1)
      mysql->options.client_flag |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
    else
      mysql->options.client_flag &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
    break;
  case MYSQL_OPT_SSL_ENFORCE:
    mysql->options.use_ssl= (*(my_bool *)arg1);
    break;
  case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
    if (*(my_bool *)arg1)
      mysql->options.client_flag |= CLIENT_SSL_VERIFY_SERVER_CERT;
    else
      mysql->options.client_flag &= ~CLIENT_SSL_VERIFY_SERVER_CERT;
    break;
  case MYSQL_OPT_SSL_KEY:
    OPT_SET_VALUE_STR(&mysql->options, ssl_key, (char *)arg1);
    break;
  case MYSQL_OPT_SSL_CERT:
    OPT_SET_VALUE_STR(&mysql->options, ssl_cert, (char *)arg1);
    break;
  case MYSQL_OPT_SSL_CA:
    OPT_SET_VALUE_STR(&mysql->options, ssl_ca, (char *)arg1);
    break;
  case MYSQL_OPT_SSL_CAPATH:
    OPT_SET_VALUE_STR(&mysql->options, ssl_capath, (char *)arg1);
    break;
  case MYSQL_OPT_SSL_CIPHER:
    OPT_SET_VALUE_STR(&mysql->options, ssl_cipher, (char *)arg1);
    break;
  case MYSQL_OPT_SSL_CRL:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, ssl_crl, (char *)arg1);
    break;
  case MYSQL_OPT_SSL_CRLPATH:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, ssl_crlpath, (char *)arg1);
    break;
  case MYSQL_OPT_CONNECT_ATTR_DELETE:
    {
      uchar *h;
      CHECK_OPT_EXTENSION_SET(&mysql->options);
      if (hash_inited(&mysql->options.extension->connect_attrs) &&
          (h= (uchar *)hash_search(&mysql->options.extension->connect_attrs, (uchar *)arg1,
                      arg1 ? (uint)strlen((char *)arg1) : 0)))
      {
        uchar *p= h;
        size_t key_len= strlen((char *)p);
        mysql->options.extension->connect_attrs_len-= key_len + get_store_length(key_len);
        p+= key_len + 1;
        key_len= strlen((char *)p);
        mysql->options.extension->connect_attrs_len-= key_len + get_store_length(key_len);
        hash_delete(&mysql->options.extension->connect_attrs, h);
      }

    }
    break;
  case MYSQL_OPT_CONNECT_ATTR_RESET:
    CHECK_OPT_EXTENSION_SET(&mysql->options);
    if (hash_inited(&mysql->options.extension->connect_attrs))
    {
      hash_free(&mysql->options.extension->connect_attrs);
      mysql->options.extension->connect_attrs_len= 0;
    }
    break;
  case MARIADB_OPT_CONNECTION_HANDLER:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, connection_handler, (char *)arg1);
    break;
  case MARIADB_OPT_PORT:
    OPT_SET_VALUE_INT(&mysql->options, port, *((uint *)arg1));
    break;
  case MARIADB_OPT_UNIXSOCKET:
    OPT_SET_VALUE_STR(&mysql->options, unix_socket, arg1);
    break;
  case MARIADB_OPT_USER:
    OPT_SET_VALUE_STR(&mysql->options, user, arg1);
    break;
  case MARIADB_OPT_HOST:
    OPT_SET_VALUE_STR(&mysql->options, host, arg1);
    break;
  case MARIADB_OPT_SCHEMA:
    OPT_SET_VALUE_STR(&mysql->options, db, arg1);
    break;
  case MARIADB_OPT_DEBUG:
    break;
  case MARIADB_OPT_FOUND_ROWS:
    mysql->options.client_flag|= CLIENT_FOUND_ROWS;
    break;
  case MARIADB_OPT_INTERACTIVE:
    mysql->options.client_flag|= CLIENT_INTERACTIVE;
    break;
  case MARIADB_OPT_MULTI_RESULTS:
    mysql->options.client_flag|= CLIENT_MULTI_RESULTS;
    break;
  case MARIADB_OPT_MULTI_STATEMENTS:
    mysql->options.client_flag|= CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS;
    break;
  case MARIADB_OPT_PASSWORD:
    OPT_SET_VALUE_STR(&mysql->options, password, arg1);
    break;
  case MARIADB_OPT_USERDATA:
    {
      void *data= va_arg(ap, void *);
      uchar *buffer, *p;
      char *key= (char *)arg1;

      if (!key || !data)
      {
        SET_CLIENT_ERROR(mysql, CR_INVALID_PARAMETER_NO, SQLSTATE_UNKNOWN, 0);
        goto end;
      }

      CHECK_OPT_EXTENSION_SET(&mysql->options);
      if (!hash_inited(&mysql->options.extension->userdata))
      {
        if (_hash_init(&mysql->options.extension->userdata,
                       0, 0, 0, ma_get_hash_keyval, ma_int_hash_free, 0))
        {
          SET_CLIENT_ERROR(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
          goto end;
        }
      }
      /* check if key is already in buffer */
      p= (uchar *)hash_search(&mysql->options.extension->userdata,
                              (uchar *)key,
                              (uint)strlen(key));
      if (p)
      {
        p+= strlen(key) + 1;
        memcpy(p, &data, sizeof(void *));
        break;
      }

      if (!(buffer= (uchar *)malloc(strlen(key) + 1 + sizeof(void *))))
      {
        SET_CLIENT_ERROR(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
        goto end;
      }

      p= buffer;
      strcpy((char *)p, key);
      p+= strlen(key) + 1;
      memcpy(p, &data, sizeof(void *));

      if (hash_insert(&mysql->options.extension->userdata, buffer))
      {
        free(buffer);
        SET_CLIENT_ERROR(mysql, CR_INVALID_PARAMETER_NO, SQLSTATE_UNKNOWN, 0);
        goto end;
      }
    }
    break;
  case MYSQL_OPT_CONNECT_ATTR_ADD:
    {
      uchar *buffer;
      void *arg2= va_arg(ap, void *);
      size_t storage_len, key_len= arg1 ? strlen((char *)arg1) : 0,
             value_len= arg2 ? strlen((char *)arg2) : 0;
      if (!key_len || !value_len)
      {
        SET_CLIENT_ERROR(mysql, CR_INVALID_PARAMETER_NO, SQLSTATE_UNKNOWN, 0);
        goto end;
      }
      storage_len= key_len + value_len +
                   get_store_length(key_len) +
                   get_store_length(value_len);

      /* since we store terminating zero character in hash, we need
       * to increase lengths */
      key_len++;
      value_len++;

      CHECK_OPT_EXTENSION_SET(&mysql->options);
      if (!key_len ||
          storage_len + mysql->options.extension->connect_attrs_len > 0xFFFF)
      {
        SET_CLIENT_ERROR(mysql, CR_INVALID_PARAMETER_NO, SQLSTATE_UNKNOWN, 0);
        goto end;
      }

      if (!hash_inited(&mysql->options.extension->connect_attrs))
      {
        if (_hash_init(&mysql->options.extension->connect_attrs,
                       0, 0, 0, ma_get_hash_keyval, ma_int_hash_free, 0))
        {
          SET_CLIENT_ERROR(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
          goto end;
        }
      }
      if ((buffer= (uchar *)malloc(key_len + value_len)))
      {
        uchar *p= buffer;
        strcpy((char *)p, arg1);
        p+= (strlen(arg1) + 1);
        if (arg2)
          strcpy((char *)p, arg2);

        if (hash_insert(&mysql->options.extension->connect_attrs, buffer))
        {
          free(buffer);
          SET_CLIENT_ERROR(mysql, CR_INVALID_PARAMETER_NO, SQLSTATE_UNKNOWN, 0);
          goto end;
        }
        mysql->options.extension->connect_attrs_len+= storage_len;
      }
      else
      {
        SET_CLIENT_ERROR(mysql, CR_OUT_OF_MEMORY, SQLSTATE_UNKNOWN, 0);
        goto end;
      }
    }
    break;
  case MYSQL_ENABLE_CLEARTEXT_PLUGIN:
    break;
  case MYSQL_SECURE_AUTH:
    mysql->options.secure_auth= *(my_bool *)arg1;
    break;
  case MYSQL_OPT_BIND:
    OPT_SET_VALUE_STR(&mysql->options, bind_address, arg1);
    break;
  case MARIADB_OPT_TLS_CIPHER_STRENGTH:
    OPT_SET_EXTENDED_VALUE_INT(&mysql->options, tls_cipher_strength, *((unsigned int *)arg1));
    break;
  case MARIADB_OPT_SSL_FP:
  case MARIADB_OPT_TLS_PEER_FP:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, tls_fp, (char *)arg1);
    mysql->options.use_ssl= 1;
    break;
  case MARIADB_OPT_SSL_FP_LIST:
  case MARIADB_OPT_TLS_PEER_FP_LIST:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, tls_fp_list, (char *)arg1);
    mysql->options.use_ssl= 1;
    break;
  case MARIADB_OPT_TLS_PASSPHRASE:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, tls_pw, (char *)arg1);
    break;
  case MARIADB_OPT_CONNECTION_READ_ONLY:
    OPT_SET_EXTENDED_VALUE_INT(&mysql->options, read_only, *(my_bool *)arg1);
    break;
  case MARIADB_OPT_PROXY_HEADER:
    {
    size_t arg2 = va_arg(ap, size_t);
    OPT_SET_EXTENDED_VALUE(&mysql->options, proxy_header, (char *)arg1);
    OPT_SET_EXTENDED_VALUE(&mysql->options, proxy_header_len, arg2);
    }
    break;
  case MARIADB_OPT_TLS_VERSION:
  case MYSQL_OPT_TLS_VERSION:
    OPT_SET_EXTENDED_VALUE_STR(&mysql->options, tls_version, (char *)arg1);
    break;
  case MARIADB_OPT_IO_WAIT:
    CHECK_OPT_EXTENSION_SET(&mysql->options);
    mysql->options.extension->io_wait = (int(*)(my_socket, my_bool, int))arg1;
    break;
  default:
    va_end(ap);
    SET_CLIENT_ERROR(mysql, CR_NOT_IMPLEMENTED, SQLSTATE_UNKNOWN, 0);
    return(1);
  }
  va_end(ap);
  return(0);
end:
  va_end(ap);
  return(1);
}

#define ASYNC_CONTEXT_DEFAULT_STACK_SIZE (4096*15)

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

static MYSQL_PARAMETERS mariadb_internal_parameters= {&max_allowed_packet, &net_buffer_length, 0};

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern char **get_default_configuration_dirs();

extern my_bool  ma_init_done;

extern my_bool  mysql_ps_subsystem_initialized;

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern my_bool mthd_supported_buffer_type(enum enum_field_types type);

extern my_bool mthd_stmt_read_prepare_response(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_param_metadata(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_result_metadata(MYSQL_STMT *stmt);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

uint mysql_port=0;

struct st_mariadb_methods MARIADB_DEFAULT_METHODS;

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

extern void mysql_client_plugin_deinit();

struct passwd *getpwuid(uid_t);

char* getlogin(void);

enum enum_option_type {
  MARIADB_OPTION_NONE,
  MARIADB_OPTION_BOOL,
  MARIADB_OPTION_INT,
  MARIADB_OPTION_SIZET,
  MARIADB_OPTION_STR,
}

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


ulong STDCALL mysql_get_client_version(void)
{
  return MARIADB_VERSION_ID;
}

extern ulong max_allowed_packet;

extern ulong net_buffer_length;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern void release_configuration_dirs();

extern void mthd_stmt_flush_unbuffered(MYSQL_STMT *stmt);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

extern void mysql_client_plugin_deinit();

char* getlogin(void);


my_bool STDCALL mariadb_connection(MYSQL *mysql)
{
  return (strstr(mysql->server_version, "MariaDB") ||
          strstr(mysql->server_version, "-maria-"));
}

static my_bool mysql_client_init=0;

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern my_bool  ma_init_done;

extern my_bool  mysql_ps_subsystem_initialized;

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern my_bool mthd_supported_buffer_type(enum enum_field_types type);

extern my_bool mthd_stmt_read_prepare_response(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_param_metadata(MYSQL_STMT *stmt);

extern my_bool mthd_stmt_get_result_metadata(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern void
my_context_install_suspend_resume_hook(struct mysql_async_context *b,
                                       void (*hook)(my_bool, void *),
                                       void *user_data);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);


MA_FIELD_EXTENSION *ma_field_extension_deep_dup(MA_MEM_ROOT *memroot,
                                                const MA_FIELD_EXTENSION *from)
{
  MA_FIELD_EXTENSION *ext= new_ma_field_extension(memroot);
  uint i;
  if (!ext)
    return NULL;
  for (i= 0; i < MARIADB_FIELD_ATTR_LAST; i++)
  {
    if (from->metadata[i].str)
      ext->metadata[i]= ma_const_string_copy_root(memroot,
                                                  from->metadata[i].str,
                                                  from->metadata[i].length);
  }
  return ext;
}

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

uint mysql_port=0;

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


int STDCALL mysql_set_character_set(MYSQL *mysql, const char *csname)
{
  const MARIADB_CHARSET_INFO *cs;

  if (!csname)
    goto error;

  if ((cs= mysql_find_charset_name(csname)))
  {
    char buff[64];

    snprintf(buff, 63, "SET NAMES %s", cs->csname);
    if (!mysql_real_query(mysql, buff, (unsigned long)strlen(buff)))
    {
      mysql->charset= cs;
      return(0);
    }
    return(mysql->net.last_errno);
  }

error:
  my_set_error(mysql, CR_CANT_READ_CHARSET, SQLSTATE_UNKNOWN,
               0, csname, "compiled_in");
  return(mysql->net.last_errno);
}

static void mysql_close_options(MYSQL *mysql);

static void ma_clear_session_state(MYSQL *mysql);

extern char **get_default_configuration_dirs();

extern my_bool mysql_handle_local_infile(MYSQL *mysql, const char *filename, my_bool can_local_infile);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_nr(uint charsetnr);

extern const MARIADB_CHARSET_INFO * mysql_find_charset_name(const char * const name);

extern my_bool set_default_charset_by_name(const char *cs_name, myf flags __attribute__((unused)));

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int net_add_multi_command(NET *net, uchar command, const uchar *packet,
                                 size_t length);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);

extern int mthd_stmt_read_all_rows(MYSQL_STMT *stmt);

extern my_bool _mariadb_read_options(MYSQL *mysql, const char *dir, const char *config_file, char *group, unsigned int recursion);

extern unsigned char *mysql_net_store_length(unsigned char *packet, size_t length);

#define IS_CONNHDLR_ACTIVE(mysql)\
  ((mysql)->extension && (mysql)->extension->conn_hdlr)

static void end_server(MYSQL *mysql);

static void mysql_close_memory(MYSQL *mysql);

void read_user_name(char *name);

my_bool STDCALL mariadb_reconnect(MYSQL *mysql);

static int cli_report_progress(MYSQL *mysql, uchar *packet, uint length);

extern int mysql_client_plugin_init();

char* getlogin(void);

struct st_default_options {
  enum mysql_option option;
  enum enum_option_type type;
  const char *conf_key;
}


mysql_free_result(MYSQL_RES *result)
{
  if (result)
  {
    if (result->handle && result->handle->status == MYSQL_STATUS_USE_RESULT)
    {
      result->handle->methods->db_skip_result(result->handle);
      result->handle->status=MYSQL_STATUS_READY;
    }
    free_rows(result->data);
    if (result->fields)
      ma_free_root(&result->field_alloc,MYF(0));
    if (result->row)
      free(result->row);
    free(result);
  }
  return;
}

extern int run_plugin_auth(MYSQL *mysql, char *data, uint data_len,
                           const char *data_plugin, const char *db);

extern int mthd_stmt_fetch_row(MYSQL_STMT *stmt, unsigned char **row);

extern int mthd_stmt_fetch_to_bind(MYSQL_STMT *stmt, unsigned char *row);