/** * read_string - Retrieve a string from the buffer * * This reads a string from the buffer. It reads the length of the string * as the first byte. * * offset[in] offset from start of buffer * * Returns string - String from the buffer */ char *Connector::read_string(int *offset) { int len_bytes = get_lcb_len(buffer[*offset]); int len = read_int(*offset, len_bytes); char *str = (char *)malloc(len+1); strncpy(str, (char *)&buffer[*offset+len_bytes], len); str[len] = 0x00; *offset += len_bytes+len; return str; }
/* read_int - Retrieve an integer from the buffer in size bytes. This reads an integer from the buffer at offset position indicated for the number of bytes specified (size). offset[in] offset from start of buffer size[in] number of bytes to use to store the integer Returns integer - integer from the buffer */ int MySQL_Packet::read_int(int offset, int size) { int value = 0; int new_size = 0; if (size == 0) new_size = get_lcb_len(offset); if (size == 1) return buffer[offset]; new_size = size; int shifter = (new_size - 1) * 8; for (int i = new_size; i > 0; i--) { value += (byte)(buffer[i-1] << shifter); shifter -= 8; } return value; }
/** * get_field - Read a field from the server * * This method reads a field packet from the server. Field packets are * defined as: * * Bytes Name * ----- ---- * n (Length Coded String) catalog * n (Length Coded String) db * n (Length Coded String) table * n (Length Coded String) org_table * n (Length Coded String) name * n (Length Coded String) org_name * 1 (filler) * 2 charsetnr * 4 length * 1 type * 2 flags * 1 decimals * 2 (filler), always 0x00 * n (Length Coded Binary) default * * Note: the sum of all db, column, and field names must be < 255 in length * */ int Connector::get_field(field_struct *fs) { int len_bytes; int len; int offset; // Read field packets until EOF read_packet(); if (buffer[4] != EOF_PACKET) { // calculate location of db len_bytes = get_lcb_len(4); len = read_int(4, len_bytes); offset = 4+len_bytes+len; fs->db = read_string(&offset); // get table fs->table = read_string(&offset); // calculate location of name len_bytes = get_lcb_len(offset); len = read_int(offset, len_bytes); offset += len_bytes+len; fs->name = read_string(&offset); return 0; } return EOF_PACKET; }