/* The same as above but returns longlong */ my_ulonglong net_field_length_ll(uchar **packet) { uchar *pos= *packet; if (*pos < 251) { (*packet)++; return (my_ulonglong) *pos; } if (*pos == 251) { (*packet)++; return (my_ulonglong) NULL_LENGTH; } if (*pos == 252) { (*packet)+=3; return (my_ulonglong) uint2korr(pos+1); } if (*pos == 253) { (*packet)+=4; return (my_ulonglong) uint3korr(pos+1); } DBUG_ASSERT(*pos == 254); (*packet)+=9; /* Must be 254 when here */ return (my_ulonglong) uint8korr(pos+1); }
/* Taken from original MySQL sources, some versions of libmysqlclient does not export this function. */ static ulong __net_field_length(uchar **packet) { reg1 uchar *pos= (uchar *)*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); }
void update_max_auto_val(const char *data, int size) { union { Uint8 u8; Uint16 u16; Uint32 u32; } val; Uint64 v; switch(size){ case 64: memcpy(&v,data,8); break; case 32: memcpy(&val.u32,data,4); v= val.u32; break; case 24: v= uint3korr((unsigned char*)data); break; case 16: memcpy(&val.u16,data,2); v= val.u16; break; case 8: memcpy(&val.u8,data,1); v= val.u8; break; default: return; }; if(v > m_max_auto_val) m_max_auto_val= v; };
const char* Field_set::unpack(const char* from) { ulonglong tmp; switch(pack_length()) { case 1: tmp = ulonglong(*((unsigned char*)(from))); break; case 2: tmp = ulonglong(uint2korr(from)); break; case 3: tmp = ulonglong(uint3korr(from)); break; case 4: tmp = ulonglong(uint4korr(from)); break; case 8: tmp = uint8korr(from); break; default: tmp = uint8korr(from); break; } field_data = tmp; LOG_TRACE(log, " set: " << tmp << " // " << pack_length()); return from + pack_length(); }
/* The same as above but returns longlong */ my_ulonglong net_field_length_ll(uchar **packet) { reg1 uchar *pos= *packet; if (*pos < 251) { (*packet)++; return (my_ulonglong) *pos; } if (*pos == 251) { (*packet)++; return (my_ulonglong) NULL_LENGTH; } if (*pos == 252) { (*packet)+=3; return (my_ulonglong) uint2korr(pos+1); } if (*pos == 253) { (*packet)+=4; return (my_ulonglong) uint3korr(pos+1); } (*packet)+=9; /* Must be 254 when here */ #ifdef NO_CLIENT_LONGLONG return (my_ulonglong) uint4korr(pos+1); #else return (my_ulonglong) uint8korr(pos+1); #endif }
const char* Field_medium::unpack(const char* from) { uint32 tmp = uint3korr(from); field_data = tmp; LOG_TRACE(log, " medium: " << tmp << " // " << pack_length()); return from + pack_length(); }
uint64_t CRowLogEvent::build_column_mask(const uint8_t** ptr, size_t* len, uint64_t n) { uint64_t column_mask; uint8_t l = uint8_t((n+7)/8); switch( l ) { case 1: // [1..8] columns { column_mask = 0xFFFFFFFFFFFFFFFFLL & (uint64_t)*(*ptr); break; } case 2: // [9..16] columns { column_mask = 0xFFFFFFFFFFFFFFFFLL & (uint64_t)uint2korr(*ptr); break; } case 3: // [17..24] columns { column_mask = 0xFFFFFFFFFFFFFFFFLL & (uint64_t)uint3korr(*ptr); break; } case 4: // [25..32] columns { column_mask = 0xFFFFFFFFFFFFFFFFLL & (uint64_t)uint4korr(*ptr); break; } case 5: // [33..40] columns { column_mask = 0xFFFFFFFFFFFFFFFFLL & (uint64_t)uint5korr(*ptr); break; } case 6: // [41..48] columns { column_mask = 0xFFFFFFFFFFFFFFFFLL & (uint64_t)uint6korr(*ptr); break; } default: { // wrong column number return (uint64_t) - 1; } } if (ptr && *ptr) (*ptr) += l; if (len) *len -= l; return column_mask; }
// numeric template<class T> T as_int() const { if (is_null() || !is_valid() || sizeof(T)>8) return (T)0; switch (_size) { case 1: return (T)*_storage; case 2: return (T)uint2korr(_storage); case 3: return (T)uint3korr(_storage); case 4: return (T)uint4korr(_storage); case 8: return (T)uint8korr(_storage); } return (T)0; }
uint hp_calc_blob_length(uint bytes, const uchar *pos) { switch (bytes) { case 1: return (uint) *pos; case 2: return uint2korr(pos); case 3: return uint3korr(pos); case 4: return uint4korr(pos); default: break; } return 0; /* Impossible */ }
unsigned int Field_blob::get_length(const char *pos) { switch (packlength) { case 1: return (unsigned int) (unsigned char) pos[0]; case 2: { unsigned short tmp = 0; /* if (db_low_byte_first) { tmp = sint2korr(pos); } else { shortget(tmp,pos); } */ tmp = sint2korr(pos); return (unsigned int) tmp; } case 3: return (unsigned int) uint3korr(pos); case 4: { unsigned int tmp; /* if (db_low_byte_first) tmp = uint4korr(pos); else longget(tmp,pos); */ tmp = uint4korr(pos); return (unsigned int) tmp; } } throw std::runtime_error("Oops, wrong packlength in Field_blob::get_length(): wanted 1, 2, 3 or 4."); }
/* {{{ mysqlnd_net::read_compressed_packet_from_stream_and_fill_read_buffer */ static enum_func_status MYSQLND_METHOD(mysqlnd_net, read_compressed_packet_from_stream_and_fill_read_buffer) (MYSQLND_NET * net, size_t net_payload_size, MYSQLND_STATS * conn_stats, MYSQLND_ERROR_INFO * error_info) { size_t decompressed_size; enum_func_status retval = PASS; zend_uchar * compressed_data = NULL; zend_uchar comp_header[COMPRESSED_HEADER_SIZE]; DBG_ENTER("mysqlnd_net::read_compressed_packet_from_stream_and_fill_read_buffer"); /* Read the compressed header */ if (FAIL == net->data->m.network_read_ex(net, comp_header, COMPRESSED_HEADER_SIZE, conn_stats, error_info)) { DBG_RETURN(FAIL); } decompressed_size = uint3korr(comp_header); /* When decompressed_size is 0, then the data is not compressed, and we have wasted 3 bytes */ /* we need to decompress the data */ if (decompressed_size) { compressed_data = mnd_emalloc(net_payload_size); if (FAIL == net->data->m.network_read_ex(net, compressed_data, net_payload_size, conn_stats, error_info)) { retval = FAIL; goto end; } net->uncompressed_data = mysqlnd_create_read_buffer(decompressed_size); retval = net->data->m.decode(net->uncompressed_data->data, decompressed_size, compressed_data, net_payload_size); if (FAIL == retval) { goto end; } } else { DBG_INF_FMT("The server decided not to compress the data. Our job is easy. Copying %u bytes", net_payload_size); net->uncompressed_data = mysqlnd_create_read_buffer(net_payload_size); if (FAIL == net->data->m.network_read_ex(net, net->uncompressed_data->data, net_payload_size, conn_stats, error_info)) { retval = FAIL; goto end; } } end: if (compressed_data) { mnd_efree(compressed_data); } DBG_RETURN(retval); }
my_ulonglong safe_net_field_length_ll(uchar **packet, size_t packet_len) { uchar *pos= *packet; if (packet_len < 1) goto err; if (*pos < 251) { (*packet)++; return (my_ulonglong) *pos; } if (*pos == 251) { (*packet)++; return (my_ulonglong) NULL_LENGTH; } if (*pos == 252) { if (packet_len < 3) goto err; (*packet)+=3; return (my_ulonglong) uint2korr(pos+1); } if (*pos == 253) { if (packet_len < 4) goto err; (*packet)+=4; return (my_ulonglong) uint3korr(pos+1); } if (packet_len < 9 || *pos != 254) goto err; (*packet)+=9; return (my_ulonglong) uint8korr(pos+1); err: *packet = NULL; return 0; }
/* {{{ ps_fetch_from_1_to_8_bytes */ void ps_fetch_from_1_to_8_bytes(zval *zv, const MYSQLND_FIELD * const field, unsigned int pack_len, zend_uchar **row, zend_bool as_unicode, unsigned int byte_count TSRMLS_DC) { char tmp[22]; size_t tmp_len = 0; zend_bool is_bit = field->type == MYSQL_TYPE_BIT; DBG_ENTER("ps_fetch_from_1_to_8_bytes"); DBG_INF_FMT("zv=%p byte_count=%d", zv, byte_count); if (field->flags & UNSIGNED_FLAG) { uint64_t uval = 0; switch (byte_count) { case 8:uval = is_bit? (uint64_t) bit_uint8korr(*row):(uint64_t) uint8korr(*row);break; case 7:uval = bit_uint7korr(*row);break; case 6:uval = bit_uint6korr(*row);break; case 5:uval = bit_uint5korr(*row);break; case 4:uval = is_bit? (uint64_t) bit_uint4korr(*row):(uint64_t) uint4korr(*row);break; case 3:uval = is_bit? (uint64_t) bit_uint3korr(*row):(uint64_t) uint3korr(*row);break; case 2:uval = is_bit? (uint64_t) bit_uint2korr(*row):(uint64_t) uint2korr(*row);break; case 1:uval = (uint64_t) uint1korr(*row);break; } #if SIZEOF_LONG==4 if (uval > INT_MAX) { DBG_INF("stringify"); tmp_len = sprintf((char *)&tmp, MYSQLND_LLU_SPEC, uval); } else #endif /* #if SIZEOF_LONG==4 */ { if (byte_count < 8 || uval <= L64(9223372036854775807)) { ZVAL_LONG(zv, uval); } else { DBG_INF("stringify"); tmp_len = sprintf((char *)&tmp, MYSQLND_LLU_SPEC, uval); } } } else { /* SIGNED */ int64_t lval = 0; switch (byte_count) { case 8:lval = (int64_t) sint8korr(*row);break; /* 7, 6 and 5 are not possible. BIT is only unsigned, thus only uint5|6|7 macroses exist */ case 4:lval = (int64_t) sint4korr(*row);break; case 3:lval = (int64_t) sint3korr(*row);break; case 2:lval = (int64_t) sint2korr(*row);break; case 1:lval = (int64_t) *(int8_t*)*row;break; } #if SIZEOF_LONG==4 if ((L64(2147483647) < (int64_t) lval) || (L64(-2147483648) > (int64_t) lval)) { DBG_INF("stringify"); tmp_len = sprintf((char *)&tmp, MYSQLND_LL_SPEC, lval); } else #endif /* SIZEOF */ { ZVAL_LONG(zv, lval); } } if (tmp_len) { #if PHP_MAJOR_VERSION >= 6 if (as_unicode) { DBG_INF("stringify"); ZVAL_UTF8_STRINGL(zv, tmp, tmp_len, ZSTR_DUPLICATE); } else #endif { DBG_INF("stringify"); ZVAL_STRINGL(zv, tmp, tmp_len, 1); } } (*row)+= byte_count; DBG_VOID_RETURN; }
int mysac_decode_field(char *buf, int len, MYSQL_FIELD *col) { int i; unsigned long size; char nul; char *wh; int tmp_len; /* VERSION 4.0 Bytes Name ----- ---- n (Length Coded String) table n (Length Coded String) name 4 (Length Coded Binary) length 2 (Length Coded Binary) type 2 (Length Coded Binary) flags 1 decimals n (Length Coded Binary) default -> VERSION 4.1 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 */ wh = buf; i = 0; /* n (Length Coded String) catalog */ tmp_len = my_lcb(&buf[i], &size, &nul, len-i); if (tmp_len == -1) return -MYERR_BAD_LCB; i += tmp_len; if (i + size > (unsigned int)len) return -MYERR_LEN_OVER_BUFFER; col->catalog_length = size; memmove(wh, &buf[i], size); col->catalog = wh; col->catalog[size] = '\0'; wh += size + 1; i += size; /* n (Length Coded String) db */ tmp_len = my_lcb(&buf[i], &size, &nul, len-i); if (tmp_len == -1) return -MYERR_BAD_LCB; i += tmp_len; if (i + size > (unsigned int)len) return -MYERR_LEN_OVER_BUFFER; col->db_length = size; memmove(wh, &buf[i], size); col->db = wh; col->db[size] = '\0'; wh += size + 1; i += size; /* n (Length Coded String) table */ tmp_len = my_lcb(&buf[i], &size, &nul, len-i); if (tmp_len == -1) return -MYERR_BAD_LCB; i += tmp_len; if (i + size > (unsigned int)len) return -MYERR_LEN_OVER_BUFFER; col->table_length = size; memmove(wh, &buf[i], size); col->table = wh; col->table[size] = '\0'; wh += size + 1; i += size; /* n (Length Coded String) org_table */ tmp_len = my_lcb(&buf[i], &size, &nul, len-i); if (tmp_len == -1) return -MYERR_BAD_LCB; i += tmp_len; if (i + size > (unsigned int)len) return -MYERR_LEN_OVER_BUFFER; col->org_table_length = size; memmove(wh, &buf[i], size); col->org_table = wh; col->org_table[size] = '\0'; wh += size + 1; i += size; /* n (Length Coded String) name */ tmp_len = my_lcb(&buf[i], &size, &nul, len-i); if (tmp_len == -1) return -MYERR_BAD_LCB; i += tmp_len; if (i + size > (unsigned int)len) return -MYERR_LEN_OVER_BUFFER; col->name_length = size; memmove(wh, &buf[i], size); col->name = wh; col->name[size] = '\0'; wh += size + 1; i += size; /* n (Length Coded String) org_name */ tmp_len = my_lcb(&buf[i], &size, &nul, len-i); if (tmp_len == -1) return -MYERR_BAD_LCB; i += tmp_len; if (i + size > (unsigned int)len) return -MYERR_LEN_OVER_BUFFER; col->org_name_length = size; memmove(wh, &buf[i], size); col->org_name = wh; col->org_name[size] = '\0'; wh += size + 1; i += size; /* check len */ if (i + 13 > len) return -MYERR_LEN_OVER_BUFFER; /* (filler) */ i += 1; /* charset */ col->charsetnr = uint2korr(&buf[i]); i += 2; /* length */ col->length = uint4korr(&buf[i]); i += 4; /* type */ col->type = (unsigned char)buf[i]; i += 1; /* flags */ col->flags = uint3korr(&buf[i]); i += 2; /* decimals */ col->decimals = buf[i]; i += 1; /* filler */ i += 2; /* default - a priori facultatif */ if (len-i > 0) { tmp_len = my_lcb(&buf[i], &size, &nul, len-i); if (tmp_len == -1) return -MYERR_BAD_LCB; i += tmp_len; if (i + size > (unsigned int)len) return -MYERR_LEN_OVER_BUFFER; col->def_length = size; memmove(wh, &buf[i], size); col->def = wh; col->def[size] = '\0'; wh += size + 1; i += size; } else { col->def = NULL; col->def_length = 0; } /* set write pointer */ return wh - buf; }
void _nisam_print_key(FILE *stream, register N_KEYSEG *keyseg, const uchar *key) { int flag; short int s_1; long int l_1; float f_1; double d_1; uchar *end; VOID(fputs("Key: \"",stream)); flag=0; for (; keyseg->base.type ;keyseg++) { if (flag++) VOID(putc('-',stream)); end= (uchar*) key+ keyseg->base.length; switch (keyseg->base.type) { case HA_KEYTYPE_BINARY: if (!(keyseg->base.flag & HA_SPACE_PACK) && keyseg->base.length == 1) { /* packed binary digit */ VOID(fprintf(stream,"%d",(uint) *key++)); break; } /* fall through */ case HA_KEYTYPE_TEXT: case HA_KEYTYPE_NUM: if (keyseg->base.flag & HA_SPACE_PACK) { VOID(fprintf(stream,"%.*s",(int) *key,key+1)); key+= (int) *key+1; } else { VOID(fprintf(stream,"%.*s",(int) keyseg->base.length,key)); key=end; } break; case HA_KEYTYPE_INT8: VOID(fprintf(stream,"%d",(int) *((signed char*) key))); key=end; break; case HA_KEYTYPE_SHORT_INT: shortget(s_1,key); VOID(fprintf(stream,"%d",(int) s_1)); key=end; break; case HA_KEYTYPE_USHORT_INT: { ushort u_1; ushortget(u_1,key); VOID(fprintf(stream,"%u",(uint) u_1)); key=end; break; } case HA_KEYTYPE_LONG_INT: longget(l_1,key); VOID(fprintf(stream,"%ld",l_1)); key=end; break; case HA_KEYTYPE_ULONG_INT: longget(l_1,key); VOID(fprintf(stream,"%lu",(ulong) l_1)); key=end; break; case HA_KEYTYPE_INT24: VOID(fprintf(stream,"%ld",(long) sint3korr(key))); key=end; break; case HA_KEYTYPE_UINT24: VOID(fprintf(stream,"%ld",(long) uint3korr(key))); key=end; break; case HA_KEYTYPE_FLOAT: bmove((byte*) &f_1,(byte*) key,(int) sizeof(float)); VOID(fprintf(stream,"%g",(double) f_1)); key=end; break; case HA_KEYTYPE_DOUBLE: doubleget(d_1,key); VOID(fprintf(stream,"%g",d_1)); key=end; break; #ifdef HAVE_LONG_LONG case HA_KEYTYPE_LONGLONG: { char buff[21]; longlong tmp; longlongget(tmp,key); longlong2str(tmp,buff,-10); VOID(fprintf(stream,"%s",buff)); key=end; break; } case HA_KEYTYPE_ULONGLONG: { char buff[21]; longlong tmp; longlongget(tmp,key); longlong2str(tmp,buff,10); VOID(fprintf(stream,"%s",buff)); key=end; break; } #endif default: break; /* This never happens */ } } VOID(fputs("\n",stream)); return; } /* print_key */
/* {{{ mysqlnd_net::receive_ex */ static enum_func_status MYSQLND_METHOD(mysqlnd_net, receive_ex)(MYSQLND_NET * const net, zend_uchar * const buffer, const size_t count, MYSQLND_STATS * const conn_stats, MYSQLND_ERROR_INFO * const error_info) { size_t to_read = count; zend_uchar * p = buffer; DBG_ENTER("mysqlnd_net::receive_ex"); #ifdef MYSQLND_COMPRESSION_ENABLED if (net->data->compressed) { if (net->uncompressed_data) { size_t to_read_from_buffer = MIN(net->uncompressed_data->bytes_left(net->uncompressed_data), to_read); DBG_INF_FMT("reading "MYSQLND_SZ_T_SPEC" from uncompressed_data buffer", to_read_from_buffer); if (to_read_from_buffer) { net->uncompressed_data->read(net->uncompressed_data, to_read_from_buffer, (zend_uchar *) p); p += to_read_from_buffer; to_read -= to_read_from_buffer; } DBG_INF_FMT("left "MYSQLND_SZ_T_SPEC" to read", to_read); if (TRUE == net->uncompressed_data->is_empty(net->uncompressed_data)) { /* Everything was consumed. This should never happen here, but for security */ net->uncompressed_data->free_buffer(&net->uncompressed_data); } } if (to_read) { zend_uchar net_header[MYSQLND_HEADER_SIZE]; size_t net_payload_size; zend_uchar packet_no; if (FAIL == net->data->m.network_read_ex(net, net_header, MYSQLND_HEADER_SIZE, conn_stats, error_info)) { DBG_RETURN(FAIL); } net_payload_size = uint3korr(net_header); packet_no = uint1korr(net_header + 3); if (net->compressed_envelope_packet_no != packet_no) { DBG_ERR_FMT("Transport level: packets out of order. Expected %u received %u. Packet size="MYSQLND_SZ_T_SPEC, net->compressed_envelope_packet_no, packet_no, net_payload_size); php_error(E_WARNING, "Packets out of order. Expected %u received %u. Packet size="MYSQLND_SZ_T_SPEC, net->compressed_envelope_packet_no, packet_no, net_payload_size); DBG_RETURN(FAIL); } net->compressed_envelope_packet_no++; #ifdef MYSQLND_DUMP_HEADER_N_BODY DBG_INF_FMT("HEADER: hwd_packet_no=%u size=%3u", packet_no, (zend_ulong) net_payload_size); #endif /* Now let's read from the wire, decompress it and fill the read buffer */ net->data->m.read_compressed_packet_from_stream_and_fill_read_buffer(net, net_payload_size, conn_stats, error_info); /* Now a bit of recursion - read from the read buffer, if the data which we have just read from the wire is not enough, then the recursive call will try to satisfy it until it is satisfied. */ DBG_RETURN(net->data->m.receive_ex(net, p, to_read, conn_stats, error_info)); } DBG_RETURN(PASS); } #endif /* MYSQLND_COMPRESSION_ENABLED */ DBG_RETURN(net->data->m.network_read_ex(net, p, to_read, conn_stats, error_info)); }