func readSdParam(r io.RuneScanner) (sdp *SDParam, err error) {
	sdp = &SDParam{}
	sdp.Name, err = readSdParamName(r)
	if err != nil {
		return nil, err
	}
	ch, _, err := r.ReadRune()
	if err != nil {
		return nil, err  
	}
	if ch != '=' {
		return nil, badFormat("StructuredData[].Parameters")  
	}
	sdp.Value, err = readSdParamValue(r)
	if err != nil {
		return nil, err
	}
	return sdp, nil
}
readSdParam reads an SD-PARAM as defined by RFC-5424
SD-PARAM        = PARAM-NAME "=" %d34 PARAM-VALUE %d34
SD-ID           = SD-NAME
PARAM-NAME      = SD-NAME
PARAM-VALUE     = UTF-8-STRING ; characters '"', '\' and ']' MUST be escaped.
SD-NAME         = 1*32PRINTUSASCII except '=', SP, ']', %d34 (")
package rfc5424
import (
	"bytes"
	"fmt"
	"io"
	"strconv"
	"time"
	"unicode"
)
type ErrBadFormat struct {
	Property string
}
func (e ErrBadFormat) Error() string {
	return fmt.Sprintf("Message cannot be unmarshaled because it is not well formed (%s)",
		e.Property)
}
func badFormat(property string) error {
	return ErrBadFormat{Property: property}
}
func (m *Message) UnmarshalBinary(inputBuffer []byte) error {
	r := bytes.NewBuffer(inputBuffer)
	if err := m.readHeader(r); err != nil {
		return err
	}
	if err := readSpace(r); err != nil {
		return err  
	}
	if err := m.readStructuredData(r); err != nil {
		return err
	}
	ch, _, err := r.ReadRune()
	if err == io.EOF {
		return nil
	} else if ch != ' ' {
		return badFormat("MSG")  
	}
	m.Message = copyFrom(r.Bytes())
	return nil
}
func (m *Message) readHeader(r io.RuneScanner) error {
	if err := m.readPriority(r); err != nil {
		return err
	}
	if err := m.readVersion(r); err != nil {
		return err
	}
	if err := readSpace(r); err != nil {
		return err  
	}
	if err := m.readTimestamp(r); err != nil {
		return err
	}
	if err := readSpace(r); err != nil {
		return err  
	}
	if err := m.readHostname(r); err != nil {
		return err
	}
	if err := readSpace(r); err != nil {
		return err  
	}
	if err := m.readAppName(r); err != nil {
		return err
	}
	if err := readSpace(r); err != nil {
		return err  
	}
	if err := m.readProcID(r); err != nil {
		return err
	}
	if err := readSpace(r); err != nil {
		return err  
	}
	if err := m.readMsgID(r); err != nil {
		return err
	}
	return nil
}
func (m *Message) readPriority(r io.RuneScanner) error {
	ch, _, err := r.ReadRune()
	if err != nil {
		return err
	}
	if ch != '<' {
		return badFormat("Priority")
	}
	rv := &bytes.Buffer{}
	for {
		ch, _, err := r.ReadRune()
		if err != nil {
			return err
		}
		if unicode.IsDigit(ch) {
			rv.WriteRune(ch)
			continue
		}
		if ch != '>' {
			return badFormat("Priority")
		}
		priority, err := strconv.ParseInt(string(rv.Bytes()), 10, 32)
		if err != nil {
			return badFormat("Priority")
		}
		m.Priority = Priority(priority)
		return nil
	}
}
func (m *Message) readVersion(r io.RuneScanner) error {
	ch, _, err := r.ReadRune()
	if err != nil {
		return err
	}
	if ch != '1' {
		return badFormat("Version")
	}
	return nil
}
func (m *Message) readTimestamp(r io.RuneScanner) error {
	timestampString, err := readWord(r)
	if err != nil {
		return err
	}
	m.Timestamp, err = time.Parse(RFC5424TimeOffsetNum, timestampString)
	if err == nil {
		return nil
	}
	m.Timestamp, err = time.Parse(RFC5424TimeOffsetUTC, timestampString)
	if err == nil {
		return nil
	}
	return err
}
func (m *Message) readHostname(r io.RuneScanner) (err error) {
	m.Hostname, err = readWord(r)
	return err
}
func (m *Message) readAppName(r io.RuneScanner) (err error) {
	m.AppName, err = readWord(r)
	return err
}
func (m *Message) readProcID(r io.RuneScanner) (err error) {
	m.ProcessID, err = readWord(r)
	return err
}
func (m *Message) readMsgID(r io.RuneScanner) (err error) {
	m.MessageID, err = readWord(r)
	return err
}
func (m *Message) readStructuredData(r io.RuneScanner) (err error) {
	m.StructuredData = []StructuredData{}
	ch, _, err := r.ReadRune()
	if err != nil {
		return err
	}
	if ch == '-' {
		return nil
	}
	r.UnreadRune()
	for {
		ch, _, err := r.ReadRune()
		if err == io.EOF {
			return nil
		} else if err != nil {
			return err  
		} else if ch == ' ' {
			r.UnreadRune()
			return nil
		} else if ch == '[' {
			r.UnreadRune()
			sde, err := readSDElement(r)
			if err != nil {
				return err
			}
			m.StructuredData = append(m.StructuredData, sde)
		} else {
			return badFormat("StructuredData")
		}
	}
}
func readSDElement(r io.RuneScanner) (element StructuredData, err error) {
	ch, _, err := r.ReadRune()
	if err != nil {
		return element, err  
	}
	if ch != '[' {
		return element, badFormat("StructuredData[]")  
	}
	element.ID, err = readSdID(r)
	if err != nil {
		return element, err
	}
	for {
		ch, _, err := r.ReadRune()
		if err != nil {
			return element, err
		} else if ch == ']' {
			return element, nil
		} else if ch == ' ' {
			param, err := readSdParam(r)
			if err != nil {
				return element, err
			}
			element.Parameters = append(element.Parameters, *param)
		} else {
			return element, badFormat("StructuredData[]")
		}
	}
}
func readSdID(r io.RuneScanner) (string, error) {
	rv := &bytes.Buffer{}
	for {
		ch, _, err := r.ReadRune()
		if err != nil {
			return "", err
		}
		if ch == ' ' || ch == ']' {
			r.UnreadRune()
			return string(rv.Bytes()), nil
		}
		rv.WriteRune(ch)
	}
}