Просмотр исходного кода

codec: msgpack: support option to encode positive numbers as unsigned (compatibility).

Previously (prior to November 2017), positive integers were encoded as unsigned.
This was remedied thereafter. However, for compatibility, we need to ensure that
previously encoded values remain canonical (ie re-encoding should not change the bits).

This option allows folks get the prior behaviour.

Fixes #257
Ugorji Nwoke 7 лет назад
Родитель
Сommit
00b869d2f4
1 измененных файлов с 6 добавлено и 4 удалено
  1. 6 4
      codec/msgpack.go

+ 6 - 4
codec/msgpack.go

@@ -210,10 +210,9 @@ func (e *msgpackEncDriver) EncodeNil() {
 }
 
 func (e *msgpackEncDriver) EncodeInt(i int64) {
-	// if i >= 0 {
-	// 	e.EncodeUint(uint64(i))
-	// } else if false &&
-	if i > math.MaxInt8 {
+	if e.h.PositiveIntUnsigned && i >= 0 {
+		e.EncodeUint(uint64(i))
+	} else if i > math.MaxInt8 {
 		if i <= math.MaxInt16 {
 			e.w.writen1(mpInt16)
 			bigenHelper{e.x[:2], e.w}.writeUint16(uint16(i))
@@ -942,6 +941,9 @@ type MsgpackHandle struct {
 	// a []byte or string based on the setting of RawToString.
 	WriteExt bool
 
+	// PositiveIntUnsigned says to encode positive integers as unsigned.
+	PositiveIntUnsigned bool
+
 	binaryEncodingType
 	noElemSeparators