Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message type identifier and 4 byte message length.
(src []byte)
| 28 | // Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message |
| 29 | // type identifier and 4 byte message length. |
| 30 | func (dst *StartupMessage) Decode(src []byte) error { |
| 31 | if len(src) < 4 { |
| 32 | return errors.New("startup message too short") |
| 33 | } |
| 34 | |
| 35 | dst.ProtocolVersion = binary.BigEndian.Uint32(src) |
| 36 | rp := 4 |
| 37 | |
| 38 | if dst.ProtocolVersion != ProtocolVersion30 && dst.ProtocolVersion != ProtocolVersion32 { |
| 39 | return fmt.Errorf("Bad startup message version number. Expected %d or %d, got %d", ProtocolVersion30, ProtocolVersion32, dst.ProtocolVersion) |
| 40 | } |
| 41 | |
| 42 | dst.Parameters = make(map[string]string) |
| 43 | for { |
| 44 | idx := bytes.IndexByte(src[rp:], 0) |
| 45 | if idx < 0 { |
| 46 | return &invalidMessageFormatErr{messageType: "StartupMessage"} |
| 47 | } |
| 48 | key := string(src[rp : rp+idx]) |
| 49 | rp += idx + 1 |
| 50 | |
| 51 | idx = bytes.IndexByte(src[rp:], 0) |
| 52 | if idx < 0 { |
| 53 | return &invalidMessageFormatErr{messageType: "StartupMessage"} |
| 54 | } |
| 55 | value := string(src[rp : rp+idx]) |
| 56 | rp += idx + 1 |
| 57 | |
| 58 | dst.Parameters[key] = value |
| 59 | |
| 60 | if len(src[rp:]) == 1 { |
| 61 | if src[rp] != 0 { |
| 62 | return fmt.Errorf("Bad startup message last byte. Expected 0, got %d", src[rp]) |
| 63 | } |
| 64 | break |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | // Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length. |
| 72 | func (src *StartupMessage) Encode(dst []byte) ([]byte, error) { |