Note: this is equivalent to net_field_length in sql-common/pack.c
(valBytes []byte)
| 19 | |
| 20 | // Note: this is equivalent to net_field_length in sql-common/pack.c |
| 21 | func readFieldLength(valBytes []byte) ( |
| 22 | length uint64, |
| 23 | remaining []byte, |
| 24 | err error) { |
| 25 | |
| 26 | if len(valBytes) == 0 { |
| 27 | return 0, nil, errors.New("Empty field length input") |
| 28 | } |
| 29 | |
| 30 | val := uint64(valBytes[0]) |
| 31 | if val < 251 { |
| 32 | return val, valBytes[1:], nil |
| 33 | } else if val == 251 { |
| 34 | return NullLength, valBytes[1:], nil |
| 35 | } |
| 36 | size := 9 |
| 37 | if val == 252 { |
| 38 | size = 3 |
| 39 | } else if val == 253 { |
| 40 | size = 4 |
| 41 | } |
| 42 | if len(valBytes) < size { |
| 43 | return 0, nil, errors.Newf( |
| 44 | "Invalid field length input (expected at least %d bytes)", |
| 45 | size) |
| 46 | } |
| 47 | // NOTE: mysql's net_field_length implementation is somewhat broken. |
| 48 | // In particular, when net_store_length encode a ulong using 8 bytes, |
| 49 | // net_field_length will only read the first 4 bytes, and ignore the |
| 50 | // rest .... |
| 51 | return bytesToLEUint(valBytes[1:size]), valBytes[size:], nil |
| 52 | } |
| 53 | |
| 54 | func readLittleEndian(valBytes []byte, val interface{}) ([]byte, error) { |
| 55 | r := bytes.NewBuffer(valBytes) |
no test coverage detected