func (thrift *thriftPlugin) readI16(data []byte) (value string, ok bool, complete bool, off int) { if len(data) < 2 { return "", true, false, 0 } i16 := common.BytesNtohs(data[:2]) value = strconv.Itoa(int(i16)) return value, true, true, 2 }
// Parse 16bit binary value from the buffer at index. Will not advance the read // buffer func (b *Buffer) ReadNetUint16At(index int) (uint16, error) { if b.Failed() { return 0, b.err } if !b.Avail(2 + index) { return 0, b.bufferEndError() } return common.BytesNtohs(b.data[index+b.mark:]), nil }
// Parse 16bit binary value in network byte order from Buffer // (converted to Host order). func (b *Buffer) ReadNetUint16() (uint16, error) { if b.Failed() { return 0, b.err } tmp := b.data[b.mark:] if err := b.Advance(2); err != nil { return 0, err } value := common.BytesNtohs(tmp) return value, nil }
func (thrift *thriftPlugin) readStruct(data []byte) (value string, ok bool, complete bool, off int) { var bytesRead int offset := 0 fields := []thriftField{} // Loop until hitting a STOP or reaching the maximum number of elements // we follow in a stream (at which point, we assume we interpreted something // wrong). for i := 0; ; i++ { var field thriftField if i >= thrift.dropAfterNStructFields { logp.Debug("thrift", "Too many fields in struct. Dropping as error") return "", false, false, 0 } if len(data) < 1 { return "", true, false, 0 } field.Type = byte(data[offset]) offset++ if field.Type == ThriftTypeStop { return thrift.formatStruct(fields, false, []*string{}), true, true, offset } if len(data[offset:]) < 2 { return "", true, false, 0 // not complete } field.id = common.BytesNtohs(data[offset : offset+2]) offset += 2 funcReader, typeFound := thrift.funcReadersByType(field.Type) if !typeFound { logp.Debug("thrift", "Field type %d not known", field.Type) return "", false, false, 0 } field.value, ok, complete, bytesRead = funcReader(data[offset:]) if !ok { return "", false, false, 0 } if !complete { return "", true, false, 0 } fields = append(fields, field) offset += bytesRead } }
func (thrift *thriftPlugin) readField(s *thriftStream) (ok bool, complete bool, field *thriftField) { var off int field = new(thriftField) if len(s.data) == 0 { return true, false, nil // ok, not complete } field.Type = byte(s.data[s.parseOffset]) offset := s.parseOffset + 1 if field.Type == ThriftTypeStop { s.parseOffset = offset return true, true, nil // done } if len(s.data[offset:]) < 2 { return true, false, nil // ok, not complete } field.id = common.BytesNtohs(s.data[offset : offset+2]) offset += 2 funcReader, typeFound := thrift.funcReadersByType(field.Type) if !typeFound { logp.Debug("thrift", "Field type %d not known", field.Type) return false, false, nil } field.value, ok, complete, off = funcReader(s.data[offset:]) if !ok { return false, false, nil } if !complete { return true, false, nil } offset += off s.parseOffset = offset return true, false, field }
func readCount(b []byte) int { return int(common.BytesNtohs(b)) }
func pgsqlFieldsParser(s *pgsqlStream, buf []byte) error { m := s.message if len(buf) < 2 { return errEmptyFieldsBuffer } // read field count (int16) off := 2 fieldCount := readCount(buf) detailedf("Row Description field count=%d", fieldCount) fields := []string{} fieldsFormat := []byte{} for i := 0; i < fieldCount; i++ { if len(buf) <= off { return errFieldBufferShort } // read field name (null terminated string) fieldName, err := common.ReadString(buf[off:]) if err != nil { return errNoFieldName } fields = append(fields, fieldName) m.numberOfFields++ off += len(fieldName) + 1 // read Table OID (int32) off += 4 // read Column Index (int16) off += 2 // read Type OID (int32) off += 4 // read column length (int16) off += 2 // read type modifier (int32) off += 4 // read format (int16) format := common.BytesNtohs(buf[off : off+2]) off += 2 fieldsFormat = append(fieldsFormat, byte(format)) detailedf("Field name=%s, format=%d", fieldName, format) } if off < len(buf) { return errFieldBufferBig } m.fields = fields m.fieldsFormat = fieldsFormat if m.numberOfFields != fieldCount { logp.Err("Missing fields from RowDescription. Expected %d. Received %d", fieldCount, m.numberOfFields) } return nil }