Skip to content
This repository has been archived by the owner on Dec 14, 2020. It is now read-only.

Commit

Permalink
Merge 9c2790f into e2ed1be
Browse files Browse the repository at this point in the history
  • Loading branch information
vcabbage committed Jan 13, 2018
2 parents e2ed1be + 9c2790f commit 661305f
Show file tree
Hide file tree
Showing 54 changed files with 583 additions and 18 deletions.
47 changes: 43 additions & 4 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ type unmarshaler interface {
unmarshal(r reader) error
}

// constantUnmarshaler allows the returned type to be a non-pointer
type constantUnmarshaler interface {
unmarshalConstant(r reader) error
}

// unmarshal decodes AMQP encoded data into i.
//
// The decoding method is based on the type of i.
Expand Down Expand Up @@ -136,12 +141,38 @@ func unmarshal(r reader, i interface{}) (isNull bool, err error) {
switch t := i.(type) {
case unmarshaler:
return isNull, t.unmarshal(r)
case constantUnmarshaler: // must be after unmarshaler
return false, t.unmarshalConstant(r)
case *int:
val, err := readInt(r)
if err != nil {
return isNull, err
}
*t = val
case *int8:
val, err := readInt(r)
if err != nil {
return isNull, err
}
*t = int8(val)
case *int16:
val, err := readInt(r)
if err != nil {
return isNull, err
}
*t = int16(val)
case *int32:
val, err := readInt(r)
if err != nil {
return isNull, err
}
*t = int32(val)
case *int64:
val, err := readInt(r)
if err != nil {
return isNull, err
}
*t = int64(val)
case *uint64:
val, err := readUint(r)
if err != nil {
Expand Down Expand Up @@ -241,12 +272,12 @@ func unmarshal(r reader, i interface{}) (isNull bool, err error) {
}
*t = v
default:
// handle both *T and **T
// handle **T
v := reflect.Indirect(reflect.ValueOf(i))

// can't unmarshal into a non-pointer
if v.Kind() != reflect.Ptr {
return isNull, errorErrorf("unable to unmarshal into non-pointer %T", i)
return isNull, errorErrorf("unable to unmarshal %T", i)
}

// if the value being unmarshaled is null,
Expand Down Expand Up @@ -678,15 +709,23 @@ func readComposite(r reader) (interface{}, error) {
}

iface := construct()
return unmarshal(r, iface)
_, err = unmarshal(r, iface)
return iface, err
}

var compositeTypes = [256]func() interface{}{
typeCodeError: func() interface{} { return new(Error) },
typeCodeError: func() interface{} { return new(Error) },
// Lifetime Policies
typeCodeDeleteOnClose: func() interface{} { return deleteOnClose },
typeCodeDeleteOnNoMessages: func() interface{} { return deleteOnNoMessages },
typeCodeDeleteOnNoLinks: func() interface{} { return deleteOnNoLinks },
typeCodeDeleteOnNoLinksOrMessages: func() interface{} { return deleteOnNoLinksOrMessages },
// Delivery States
typeCodeStateAccepted: func() interface{} { return new(stateAccepted) },
typeCodeStateModified: func() interface{} { return new(stateModified) },
typeCodeStateReceived: func() interface{} { return new(stateReceived) },
typeCodeStateRejected: func() interface{} { return new(stateRejected) },
typeCodeStateReleased: func() interface{} { return new(stateReleased) },
}

func readTimestamp(r reader) (time.Time, error) {
Expand Down
49 changes: 43 additions & 6 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ func marshal(wr writer, i interface{}) error {
return writeInt64(wr, int64(t))
}
return writeInt32(wr, int32(t))
case int8:
err = wr.WriteByte(byte(typeCodeByte))
if err != nil {
return err
}
binary.Write(wr, binary.BigEndian, t)
case int16:
err = wr.WriteByte(byte(typeCodeShort))
if err != nil {
return err
}
binary.Write(wr, binary.BigEndian, t)
case int64:
return writeInt64(wr, t)
case int32:
Expand All @@ -123,6 +135,10 @@ func marshal(wr writer, i interface{}) error {
err = writeMap(wr, t)
case map[string]interface{}:
err = writeMap(wr, t)
case map[symbol]interface{}:
err = writeMap(wr, t)
case unsettled:
err = writeMap(wr, t)
case time.Time:
err = writeTimestamp(wr, t)
default:
Expand All @@ -132,7 +148,7 @@ func marshal(wr writer, i interface{}) error {
}

func writeInt32(wr writer, n int32) error {
if n < 256 {
if n < 128 && n >= -128 {
_, err := wr.Write([]byte{byte(typeCodeSmallint), byte(n)})
return err
}
Expand All @@ -145,7 +161,7 @@ func writeInt32(wr writer, n int32) error {
}

func writeInt64(wr writer, n int64) error {
if n < 256 {
if n < 128 && n >= -128 {
_, err := wr.Write([]byte{byte(typeCodeSmalllong), byte(n)})
return err
}
Expand Down Expand Up @@ -399,10 +415,7 @@ func writeSlice(wr writer, isArray bool, of amqpType, numFields int, size int) e

switch {
// list0
case numFields == 0:
if isArray {
return errorNew("invalid array length 0")
}
case numFields == 0 && isArray:
return wr.WriteByte(byte(typeCodeList0))

// list8
Expand Down Expand Up @@ -470,6 +483,30 @@ func writeMap(wr writer, m interface{}) error {
return err
}
}
case map[symbol]interface{}:
length = len(m)
for key, val := range m {
err := marshal(buf, key)
if err != nil {
return err
}
err = marshal(buf, val)
if err != nil {
return err
}
}
case unsettled:
length = len(m)
for key, val := range m {
err := marshal(buf, key)
if err != nil {
return err
}
err = marshal(buf, val)
if err != nil {
return err
}
}
default:
return errorErrorf("unsupported type or map type %T", m)
}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added fuzz/marshal/corpus/Error.bin
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/ErrorCondition.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�the condition
Expand Down
Binary file added fuzz/marshal/corpus/Message.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/MessageHeader.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/MessageProperties.bin
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/ReceiverSettleMode.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
P
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/SenderSettleMode.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
P
Binary file added fuzz/marshal/corpus/UUID.bin
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/bool.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A
Binary file not shown.
Binary file not shown.
Binary file added fuzz/marshal/corpus/int16.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/int32.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/int64.bin
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/int8.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Q�
Binary file added fuzz/marshal/corpus/lifetimePolicy.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/mapAnyAny.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/mapStringAny.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/mapSymbolAny.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/milliseconds.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/performAttach.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/performBegin.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/performClose.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/performDetach.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/performDisposition.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/performEnd.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/performFlow.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/performOpen.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/performTransfer.bin
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/role.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A
Binary file added fuzz/marshal/corpus/saslInit.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/saslMechanisms.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/saslOutcome.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/source.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/stateAccepted.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/stateModified.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/stateReceived.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/stateRejected.bin
Binary file not shown.
Binary file added fuzz/marshal/corpus/stateReleased.bin
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/symbol.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�a symbol
Binary file added fuzz/marshal/corpus/target.bin
Binary file not shown.
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/uint16.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`��
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/uint32.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p����
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/uint64.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���������
1 change: 1 addition & 0 deletions fuzz/marshal/corpus/uint8.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
P�
Binary file added fuzz/marshal/corpus/unsettled.bin
Binary file not shown.

0 comments on commit 661305f

Please sign in to comment.