diff --git a/gen.go b/gen.go index c711db5..48d832a 100644 --- a/gen.go +++ b/gen.go @@ -25,12 +25,12 @@ func doTemplate(w io.Writer, info interface{}, templ string) error { t := template.Must(template.New(""). Funcs(template.FuncMap{ "MajorType": func(wname string, tname string, val string) string { - return fmt.Sprintf(`if err := cbg.WriteMajorTypeHeaderBuf(scratch, %s, %s, uint64(%s)); err != nil { + return fmt.Sprintf(`if err := %s.WriteMajorTypeHeader(%s, uint64(%s)); err != nil { return err }`, wname, tname, val) }, "ReadHeader": func(rdr string) string { - return fmt.Sprintf(`cbg.CborReadHeaderBuf(%s, scratch)`, rdr) + return fmt.Sprintf(`%s.ReadHeader()`, rdr) }, }).Parse(templ)) @@ -145,30 +145,6 @@ func (gti *GenTypeInfo) Imports() []Import { return imports } -func (gti *GenTypeInfo) NeedsScratch() bool { - for _, f := range gti.Fields { - switch f.Type.Kind() { - case reflect.String, - reflect.Uint64, - reflect.Int64, - reflect.Uint8, - reflect.Array, - reflect.Slice, - reflect.Map: - return true - - case reflect.Struct: - if f.Type == bigIntType || f.Type == cidType { - return true - } - // nope - case reflect.Bool: - // nope - } - } - return false -} - func nameIsExported(name string) bool { return strings.ToUpper(name[0:1]) == name[0:1] } @@ -251,18 +227,19 @@ func emitCborMarshalStringField(w io.Writer, f Field) error { return xerrors.Errorf("Value in field {{ .Name | js }} was too long") } - {{ MajorType "w" "cbg.MajTextString" (print "len(" .Name ")") }} + {{ MajorType "cw" "cbg.MajTextString" (print "len(" .Name ")") }} if _, err := io.WriteString(w, string({{ .Name }})); err != nil { return err } `) } + func emitCborMarshalStructField(w io.Writer, f Field) error { switch f.Type { case bigIntType: return doTemplate(w, f, ` { - if err := cbg.CborWriteHeader(w, cbg.MajTag, 2); err != nil { + if err := cw.CborWriteHeader(cbg.MajTag, 2); err != nil { return err } var b []byte @@ -270,10 +247,10 @@ func emitCborMarshalStructField(w io.Writer, f Field) error { b = {{ .Name }}.Bytes() } - if err := cbg.CborWriteHeader(w, cbg.MajByteString, uint64(len(b))); err != nil { + if err := cw.CborWriteHeader(cbg.MajByteString, uint64(len(b))); err != nil { return err } - if _, err := w.Write(b); err != nil { + if _, err := cw.Write(b); err != nil { return err } } @@ -283,42 +260,41 @@ func emitCborMarshalStructField(w io.Writer, f Field) error { return doTemplate(w, f, ` {{ if .Pointer }} if {{ .Name }} == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *{{ .Name }}); err != nil { + if err := cbg.WriteCid(cw, *{{ .Name }}); err != nil { return xerrors.Errorf("failed to write cid field {{ .Name }}: %w", err) } } {{ else }} - if err := cbg.WriteCidBuf(scratch, w, {{ .Name }}); err != nil { + if err := cbg.WriteCid(cw, {{ .Name }}); err != nil { return xerrors.Errorf("failed to write cid field {{ .Name }}: %w", err) } {{ end }} `) default: return doTemplate(w, f, ` - if err := {{ .Name }}.MarshalCBOR(w); err != nil { + if err := {{ .Name }}.MarshalCBOR(cw); err != nil { return err } `) } - } func emitCborMarshalUint64Field(w io.Writer, f Field) error { return doTemplate(w, f, ` {{ if .Pointer }} if {{ .Name }} == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - {{ MajorType "w" "cbg.MajUnsignedInt" (print "*" .Name) }} + {{ MajorType "cw" "cbg.MajUnsignedInt" (print "*" .Name) }} } {{ else }} - {{ MajorType "w" "cbg.MajUnsignedInt" .Name }} + {{ MajorType "cw" "cbg.MajUnsignedInt" .Name }} {{ end }} `) } @@ -328,7 +304,7 @@ func emitCborMarshalUint8Field(w io.Writer, f Field) error { return fmt.Errorf("pointers to integers not supported") } return doTemplate(w, f, ` -{{ MajorType "w" "cbg.MajUnsignedInt" .Name }} +{{ MajorType "cw" "cbg.MajUnsignedInt" .Name }} `) } @@ -343,9 +319,9 @@ func emitCborMarshalInt64Field(w io.Writer, f Field) error { return doTemplate(w, f, ` if {{ .Name }} >= 0 { - {{ MajorType "w" "cbg.MajUnsignedInt" .Name }} + {{ MajorType "cw" "cbg.MajUnsignedInt" .Name }} } else { - {{ MajorType "w" "cbg.MajNegativeInt" (print "-" .Name "-1") }} + {{ MajorType "cw" "cbg.MajNegativeInt" (print "-" .Name "-1") }} } `) } @@ -365,7 +341,7 @@ func emitCborMarshalMapField(w io.Writer, f Field) error { return xerrors.Errorf("cannot marshal {{ .Name }} map too large") } - {{ MajorType "w" "cbg.MajMap" (print "len(" .Name ")") }} + {{ MajorType "cw" "cbg.MajMap" (print "len(" .Name ")") }} keys := make([]string, 0, len({{ .Name }})) for k := range {{ .Name }} { @@ -425,9 +401,9 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error { return xerrors.Errorf("Byte array in field {{ .Name }} was too long") } - {{ MajorType "w" "cbg.MajByteString" (print "len(" .Name ")" ) }} + {{ MajorType "cw" "cbg.MajByteString" (print "len(" .Name ")" ) }} - if _, err := w.Write({{ .Name }}[:]); err != nil { + if _, err := cw.Write({{ .Name }}[:]); err != nil { return err } `) @@ -442,7 +418,7 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error { return xerrors.Errorf("Slice value in field {{ .Name }} was too long") } - {{ MajorType "w" "cbg.MajArray" ( print "len(" .Name ")" ) }} + {{ MajorType "cw" "cbg.MajArray" ( print "len(" .Name ")" ) }} for _, v := range {{ .Name }} {`) if err != nil { return err @@ -455,7 +431,7 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error { switch e { case cidType: err := doTemplate(w, f, ` - if err := cbg.WriteCidBuf(scratch, w, v); err != nil { + if err := cbg.WriteCid(w, v); err != nil { return xerrors.Errorf("failed writing cid field {{ .Name }}: %w", err) } `) @@ -465,7 +441,7 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error { default: err := doTemplate(w, f, ` - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } `) @@ -475,7 +451,7 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error { } case reflect.Uint64: err := doTemplate(w, f, ` - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } `) @@ -484,7 +460,7 @@ func emitCborMarshalSliceField(w io.Writer, f Field) error { } case reflect.Uint8: err := doTemplate(w, f, ` - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } `) @@ -517,12 +493,12 @@ func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBuf{{ .Name }}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBuf{{ .Name }}); err != nil { return err } -{{ if .NeedsScratch }} - scratch := make([]byte, 9) -{{ end }} `) if err != nil { return err @@ -585,7 +561,7 @@ func emitCborUnmarshalStringField(w io.Writer, f Field) error { } return doTemplate(w, f, ` { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -599,7 +575,7 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { switch f.Type { case bigIntType: return doTemplate(w, f, ` - maj, extra, err = {{ ReadHeader "br" }} + maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err } @@ -608,7 +584,7 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { return fmt.Errorf("big ints should be cbor bignums") } - maj, extra, err = {{ ReadHeader "br" }} + maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err } @@ -623,7 +599,7 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { if extra > 0 { buf := make([]byte, extra) - if _, err := io.ReadFull(br, buf); err != nil { + if _, err := io.ReadFull(cr, buf); err != nil { return err } {{ .Name }} = big.NewInt(0).SetBytes(buf) @@ -635,16 +611,16 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { return doTemplate(w, f, ` { {{ if .Pointer }} - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } {{ end }} - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field {{ .Name }}: %w", err) } @@ -662,7 +638,7 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { {{ if .Pointer }} {{ .Name }} = new(cbg.Deferred) {{ end }} - if err := {{ .Name }}.UnmarshalCBOR(br); err != nil { + if err := {{ .Name }}.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("failed to read deferred field: %w", err) } } @@ -672,21 +648,21 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { return doTemplate(w, f, ` { {{ if .Pointer }} - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } {{ .Name }} = new({{ .TypeName }}) - if err := {{ .Name }}.UnmarshalCBOR(br); err != nil { + if err := {{ .Name }}.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling {{ .Name }} pointer: %w", err) } } {{ else }} - if err := {{ .Name }}.UnmarshalCBOR(br); err != nil { + if err := {{ .Name }}.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling {{ .Name }}: %w", err) } {{ end }} @@ -697,7 +673,7 @@ func emitCborUnmarshalStructField(w io.Writer, f Field) error { func emitCborUnmarshalInt64Field(w io.Writer, f Field) error { return doTemplate(w, f, `{ - maj, extra, err := {{ ReadHeader "br" }} + maj, extra, err := {{ ReadHeader "cr" }} var extraI int64 if err != nil { return err @@ -727,15 +703,15 @@ func emitCborUnmarshalUint64Field(w io.Writer, f Field) error { return doTemplate(w, f, ` { {{ if .Pointer }} - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - maj, extra, err = {{ ReadHeader "br" }} + maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err } @@ -746,7 +722,7 @@ func emitCborUnmarshalUint64Field(w io.Writer, f Field) error { {{ .Name }} = &typed } {{ else }} - maj, extra, err = {{ ReadHeader "br" }} + maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err } @@ -761,7 +737,7 @@ func emitCborUnmarshalUint64Field(w io.Writer, f Field) error { func emitCborUnmarshalUint8Field(w io.Writer, f Field) error { return doTemplate(w, f, ` - maj, extra, err = {{ ReadHeader "br" }} + maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err } @@ -777,7 +753,7 @@ func emitCborUnmarshalUint8Field(w io.Writer, f Field) error { func emitCborUnmarshalBoolField(w io.Writer, f Field) error { return doTemplate(w, f, ` - maj, extra, err = {{ ReadHeader "br" }} + maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err } @@ -797,7 +773,7 @@ func emitCborUnmarshalBoolField(w io.Writer, f Field) error { func emitCborUnmarshalMapField(w io.Writer, f Field) error { err := doTemplate(w, f, ` - maj, extra, err = {{ ReadHeader "br" }} + maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err } @@ -882,7 +858,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { } err := doTemplate(w, f, ` - maj, extra, err = {{ ReadHeader "br" }} + maj, extra, err = {{ ReadHeader "cr" }} if err != nil { return err } @@ -910,7 +886,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { {{ .Name }} = make({{ .TypeName }}, extra) } {{end}} - if _, err := io.ReadFull(br, {{ .Name }}[:]); err != nil { + if _, err := io.ReadFull(cr, {{ .Name }}[:]); err != nil { return err } `) @@ -951,7 +927,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { switch fname { case "github.com/ipfs/go-cid.Cid": err := doTemplate(w, f, ` - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("reading cid field {{ .Name }} failed: %w", err) } @@ -970,7 +946,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { err := doTemplate(w, subf, ` var v {{ .TypeName }} - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -982,7 +958,7 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error { } case reflect.Uint64: err := doTemplate(w, f, ` - maj, val, err := {{ ReadHeader "br" }} + maj, val, err := {{ ReadHeader "cr" }} if err != nil { return xerrors.Errorf("failed to read uint64 for {{ .Name }} slice: %w", err) } @@ -1035,10 +1011,9 @@ func emitCborUnmarshalStructTuple(w io.Writer, gti *GenTypeInfo) error { func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { *t = {{.Name}}{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := {{ ReadHeader "br" }} + maj, extra, err := {{ ReadHeader "cr" }} if err != nil { return err } @@ -1129,11 +1104,12 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write({{ .MapHeaderAsByteString }}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write({{ .MapHeaderAsByteString }}); err != nil { return err } - - scratch := make([]byte, 9) `) if err != nil { return err @@ -1199,10 +1175,9 @@ func emitCborUnmarshalStructMap(w io.Writer, gti *GenTypeInfo) error { func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) { *t = {{.Name}}{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := {{ ReadHeader "br" }} + maj, extra, err := {{ ReadHeader "cr" }} if err != nil { return err } diff --git a/io.go b/io.go new file mode 100644 index 0000000..2eb7d19 --- /dev/null +++ b/io.go @@ -0,0 +1,81 @@ +package typegen + +import ( + "io" +) + +var ( + _ io.Reader = (*CborReader)(nil) + _ io.ByteScanner = (*CborReader)(nil) +) + +type CborReader struct { + r BytePeeker + hbuf []byte +} + +func NewCborReader(r io.Reader) *CborReader { + if r, ok := r.(*CborReader); ok { + return r + } + + return &CborReader{ + r: GetPeeker(r), + hbuf: make([]byte, maxHeaderSize), + } +} + +func (cr *CborReader) Read(p []byte) (n int, err error) { + return cr.r.Read(p) +} + +func (cr *CborReader) ReadByte() (byte, error) { + return cr.r.ReadByte() +} + +func (cr *CborReader) UnreadByte() error { + return cr.r.UnreadByte() +} + +func (cr *CborReader) ReadHeader() (byte, uint64, error) { + return CborReadHeaderBuf(cr.r, cr.hbuf) +} + +var ( + _ io.Writer = (*CborWriter)(nil) + _ io.StringWriter = (*CborWriter)(nil) +) + +type CborWriter struct { + w io.Writer + hbuf []byte +} + +func NewCborWriter(w io.Writer) *CborWriter { + if w, ok := w.(*CborWriter); ok { + return w + } + return &CborWriter{ + w: w, + hbuf: make([]byte, maxHeaderSize), + } +} + +func (cw *CborWriter) Write(p []byte) (n int, err error) { + return cw.w.Write(p) +} + +func (cw *CborWriter) WriteMajorTypeHeader(t byte, l uint64) error { + return WriteMajorTypeHeaderBuf(cw.hbuf, cw.w, t, l) +} + +func (cw *CborWriter) CborWriteHeader(t byte, l uint64) error { + return WriteMajorTypeHeaderBuf(cw.hbuf, cw.w, t, l) +} + +func (cw *CborWriter) WriteString(s string) (int, error) { + if sw, ok := cw.w.(io.StringWriter); ok { + return sw.WriteString(s) + } + return cw.w.Write([]byte(s)) +} diff --git a/testing/bench_test.go b/testing/bench_test.go index 88216ed..7ed59c8 100644 --- a/testing/bench_test.go +++ b/testing/bench_test.go @@ -58,7 +58,6 @@ func BenchmarkUnmarshaling(b *testing.B) { b.Fatal(err) } } - } func BenchmarkLinkScan(b *testing.B) { @@ -117,3 +116,50 @@ func BenchmarkDeferred(b *testing.B) { } } } + +func BenchmarkMapMarshaling(b *testing.B) { + r := rand.New(rand.NewSource(56887)) + val, ok := quick.Value(reflect.TypeOf(SimpleTypeTree{}), r) + if !ok { + b.Fatal("failed to construct type") + } + + tt := val.Interface().(SimpleTypeTree) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + if err := tt.MarshalCBOR(ioutil.Discard); err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkMapUnmarshaling(b *testing.B) { + r := rand.New(rand.NewSource(123456)) + val, ok := quick.Value(reflect.TypeOf(SimpleTypeTree{}), r) + if !ok { + b.Fatal("failed to construct type") + } + + tt := val.Interface().(SimpleTypeTree) + + buf := new(bytes.Buffer) + if err := tt.MarshalCBOR(buf); err != nil { + b.Fatal(err) + } + + reader := bytes.NewReader(buf.Bytes()) + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + reader.Seek(0, io.SeekStart) + var tt SimpleTypeTree + if err := tt.UnmarshalCBOR(reader); err != nil { + b.Fatal(err) + } + } +} diff --git a/testing/cbor_gen.go b/testing/cbor_gen.go index 2727be0..d53913e 100644 --- a/testing/cbor_gen.go +++ b/testing/cbor_gen.go @@ -25,22 +25,23 @@ func (t *SignedArray) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufSignedArray); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufSignedArray); err != nil { return err } - scratch := make([]byte, 9) - // t.Signed ([]uint64) (slice) if len(t.Signed) > cbg.MaxLength { return xerrors.Errorf("Slice value in field t.Signed was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Signed))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Signed))); err != nil { return err } for _, v := range t.Signed { - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } @@ -50,10 +51,9 @@ func (t *SignedArray) MarshalCBOR(w io.Writer) error { func (t *SignedArray) UnmarshalCBOR(r io.Reader) (err error) { *t = SignedArray{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -73,7 +73,7 @@ func (t *SignedArray) UnmarshalCBOR(r io.Reader) (err error) { // t.Signed ([]uint64) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -92,7 +92,7 @@ func (t *SignedArray) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { - maj, val, err := cbg.CborReadHeaderBuf(br, scratch) + maj, val, err := cr.ReadHeader() if err != nil { return xerrors.Errorf("failed to read uint64 for t.Signed slice: %w", err) } @@ -114,18 +114,19 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufSimpleTypeOne); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufSimpleTypeOne); err != nil { return err } - scratch := make([]byte, 9) - // t.Foo (string) (string) if len(t.Foo) > cbg.MaxLength { return xerrors.Errorf("Value in field t.Foo was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Foo))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Foo))); err != nil { return err } if _, err := io.WriteString(w, string(t.Foo)); err != nil { @@ -134,7 +135,7 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { // t.Value (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Value)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Value)); err != nil { return err } @@ -143,21 +144,21 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Binary was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Binary))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Binary))); err != nil { return err } - if _, err := w.Write(t.Binary[:]); err != nil { + if _, err := cw.Write(t.Binary[:]); err != nil { return err } // t.Signed (int64) (int64) if t.Signed >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Signed)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Signed)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Signed-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Signed-1)); err != nil { return err } } @@ -167,7 +168,7 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.NString was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.NString))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.NString))); err != nil { return err } if _, err := io.WriteString(w, string(t.NString)); err != nil { @@ -179,10 +180,9 @@ func (t *SimpleTypeOne) MarshalCBOR(w io.Writer) error { func (t *SimpleTypeOne) UnmarshalCBOR(r io.Reader) (err error) { *t = SimpleTypeOne{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -203,7 +203,7 @@ func (t *SimpleTypeOne) UnmarshalCBOR(r io.Reader) (err error) { // t.Foo (string) (string) { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -214,7 +214,7 @@ func (t *SimpleTypeOne) UnmarshalCBOR(r io.Reader) (err error) { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -226,7 +226,7 @@ func (t *SimpleTypeOne) UnmarshalCBOR(r io.Reader) (err error) { } // t.Binary ([]uint8) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -242,12 +242,12 @@ func (t *SimpleTypeOne) UnmarshalCBOR(r io.Reader) (err error) { t.Binary = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Binary[:]); err != nil { + if _, err := io.ReadFull(cr, t.Binary[:]); err != nil { return err } // t.Signed (int64) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -273,7 +273,7 @@ func (t *SimpleTypeOne) UnmarshalCBOR(r io.Reader) (err error) { // t.NString (testing.NamedString) (string) { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -290,14 +290,15 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufSimpleTypeTwo); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufSimpleTypeTwo); err != nil { return err } - scratch := make([]byte, 9) - // t.Stuff (testing.SimpleTypeTwo) (struct) - if err := t.Stuff.MarshalCBOR(w); err != nil { + if err := t.Stuff.MarshalCBOR(cw); err != nil { return err } @@ -306,11 +307,11 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Others was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Others))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Others))); err != nil { return err } for _, v := range t.Others { - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } @@ -320,16 +321,16 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.SignedOthers was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.SignedOthers))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.SignedOthers))); err != nil { return err } for _, v := range t.SignedOthers { if v >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-v-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-v-1)); err != nil { return err } } @@ -340,7 +341,7 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Test was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Test))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Test))); err != nil { return err } for _, v := range t.Test { @@ -348,11 +349,11 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field v was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(v))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(v))); err != nil { return err } - if _, err := w.Write(v[:]); err != nil { + if _, err := cw.Write(v[:]); err != nil { return err } } @@ -362,7 +363,7 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Dog was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Dog))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Dog))); err != nil { return err } if _, err := io.WriteString(w, string(t.Dog)); err != nil { @@ -374,11 +375,11 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Numbers was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Numbers))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Numbers))); err != nil { return err } for _, v := range t.Numbers { - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } @@ -386,11 +387,11 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { // t.Pizza (uint64) (uint64) if t.Pizza == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(*t.Pizza)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(*t.Pizza)); err != nil { return err } } @@ -398,11 +399,11 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { // t.PointyPizza (testing.NamedNumber) (uint64) if t.PointyPizza == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(*t.PointyPizza)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(*t.PointyPizza)); err != nil { return err } } @@ -412,11 +413,11 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Arrrrrghay was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Arrrrrghay))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Arrrrrghay))); err != nil { return err } for _, v := range t.Arrrrrghay { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -426,10 +427,9 @@ func (t *SimpleTypeTwo) MarshalCBOR(w io.Writer) error { func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { *t = SimpleTypeTwo{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -451,16 +451,16 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Stuff = new(SimpleTypeTwo) - if err := t.Stuff.UnmarshalCBOR(br); err != nil { + if err := t.Stuff.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Stuff pointer: %w", err) } } @@ -468,7 +468,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { } // t.Others ([]uint64) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -487,7 +487,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { - maj, val, err := cbg.CborReadHeaderBuf(br, scratch) + maj, val, err := cr.ReadHeader() if err != nil { return xerrors.Errorf("failed to read uint64 for t.Others slice: %w", err) } @@ -501,7 +501,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { // t.SignedOthers ([]int64) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -520,7 +520,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -547,7 +547,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { // t.Test ([][]uint8) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -570,7 +570,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { var extra uint64 var err error - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -586,7 +586,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { t.Test[i] = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Test[i][:]); err != nil { + if _, err := io.ReadFull(cr, t.Test[i][:]); err != nil { return err } } @@ -595,7 +595,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { // t.Dog (string) (string) { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -604,7 +604,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { } // t.Numbers ([]testing.NamedNumber) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -623,7 +623,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { - maj, val, err := cbg.CborReadHeaderBuf(br, scratch) + maj, val, err := cr.ReadHeader() if err != nil { return xerrors.Errorf("failed to read uint64 for t.Numbers slice: %w", err) } @@ -639,15 +639,15 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -663,15 +663,15 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -685,7 +685,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { } // t.Arrrrrghay ([3]testing.SimpleTypeOne) (array) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -707,7 +707,7 @@ func (t *SimpleTypeTwo) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { var v SimpleTypeOne - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -724,25 +724,26 @@ func (t *DeferredContainer) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufDeferredContainer); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufDeferredContainer); err != nil { return err } - scratch := make([]byte, 9) - // t.Stuff (testing.SimpleTypeOne) (struct) - if err := t.Stuff.MarshalCBOR(w); err != nil { + if err := t.Stuff.MarshalCBOR(cw); err != nil { return err } // t.Deferred (typegen.Deferred) (struct) - if err := t.Deferred.MarshalCBOR(w); err != nil { + if err := t.Deferred.MarshalCBOR(cw); err != nil { return err } // t.Value (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Value)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Value)); err != nil { return err } @@ -752,10 +753,9 @@ func (t *DeferredContainer) MarshalCBOR(w io.Writer) error { func (t *DeferredContainer) UnmarshalCBOR(r io.Reader) (err error) { *t = DeferredContainer{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -777,16 +777,16 @@ func (t *DeferredContainer) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Stuff = new(SimpleTypeOne) - if err := t.Stuff.UnmarshalCBOR(br); err != nil { + if err := t.Stuff.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Stuff pointer: %w", err) } } @@ -798,7 +798,7 @@ func (t *DeferredContainer) UnmarshalCBOR(r io.Reader) (err error) { t.Deferred = new(cbg.Deferred) - if err := t.Deferred.UnmarshalCBOR(br); err != nil { + if err := t.Deferred.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("failed to read deferred field: %w", err) } } @@ -806,7 +806,7 @@ func (t *DeferredContainer) UnmarshalCBOR(r io.Reader) (err error) { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -826,22 +826,23 @@ func (t *FixedArrays) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufFixedArrays); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufFixedArrays); err != nil { return err } - scratch := make([]byte, 9) - // t.Bytes ([20]uint8) (array) if len(t.Bytes) > cbg.ByteArrayMaxLen { return xerrors.Errorf("Byte array in field t.Bytes was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Bytes))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Bytes))); err != nil { return err } - if _, err := w.Write(t.Bytes[:]); err != nil { + if _, err := cw.Write(t.Bytes[:]); err != nil { return err } @@ -850,11 +851,11 @@ func (t *FixedArrays) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Uint8 was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Uint8))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Uint8))); err != nil { return err } - if _, err := w.Write(t.Uint8[:]); err != nil { + if _, err := cw.Write(t.Uint8[:]); err != nil { return err } @@ -863,11 +864,11 @@ func (t *FixedArrays) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Uint64 was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Uint64))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Uint64))); err != nil { return err } for _, v := range t.Uint64 { - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } @@ -877,10 +878,9 @@ func (t *FixedArrays) MarshalCBOR(w io.Writer) error { func (t *FixedArrays) UnmarshalCBOR(r io.Reader) (err error) { *t = FixedArrays{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -900,7 +900,7 @@ func (t *FixedArrays) UnmarshalCBOR(r io.Reader) (err error) { // t.Bytes ([20]uint8) (array) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -918,12 +918,12 @@ func (t *FixedArrays) UnmarshalCBOR(r io.Reader) (err error) { t.Bytes = [20]uint8{} - if _, err := io.ReadFull(br, t.Bytes[:]); err != nil { + if _, err := io.ReadFull(cr, t.Bytes[:]); err != nil { return err } // t.Uint8 ([20]uint8) (array) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -941,12 +941,12 @@ func (t *FixedArrays) UnmarshalCBOR(r io.Reader) (err error) { t.Uint8 = [20]uint8{} - if _, err := io.ReadFull(br, t.Uint8[:]); err != nil { + if _, err := io.ReadFull(cr, t.Uint8[:]); err != nil { return err } // t.Uint64 ([20]uint64) (array) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -967,7 +967,7 @@ func (t *FixedArrays) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { - maj, val, err := cbg.CborReadHeaderBuf(br, scratch) + maj, val, err := cr.ReadHeader() if err != nil { return xerrors.Errorf("failed to read uint64 for t.Uint64 slice: %w", err) } @@ -989,24 +989,25 @@ func (t *ThingWithSomeTime) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufThingWithSomeTime); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufThingWithSomeTime); err != nil { return err } - scratch := make([]byte, 9) - // t.When (typegen.CborTime) (struct) - if err := t.When.MarshalCBOR(w); err != nil { + if err := t.When.MarshalCBOR(cw); err != nil { return err } // t.Stuff (int64) (int64) if t.Stuff >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Stuff)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Stuff)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Stuff-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Stuff-1)); err != nil { return err } } @@ -1016,7 +1017,7 @@ func (t *ThingWithSomeTime) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.CatName was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.CatName))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.CatName))); err != nil { return err } if _, err := io.WriteString(w, string(t.CatName)); err != nil { @@ -1028,10 +1029,9 @@ func (t *ThingWithSomeTime) MarshalCBOR(w io.Writer) error { func (t *ThingWithSomeTime) UnmarshalCBOR(r io.Reader) (err error) { *t = ThingWithSomeTime{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -1053,14 +1053,14 @@ func (t *ThingWithSomeTime) UnmarshalCBOR(r io.Reader) (err error) { { - if err := t.When.UnmarshalCBOR(br); err != nil { + if err := t.When.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.When: %w", err) } } // t.Stuff (int64) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1086,7 +1086,7 @@ func (t *ThingWithSomeTime) UnmarshalCBOR(r io.Reader) (err error) { // t.CatName (string) (string) { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } diff --git a/testing/cbor_map_gen.go b/testing/cbor_map_gen.go index 3f6295b..aa7b13c 100644 --- a/testing/cbor_map_gen.go +++ b/testing/cbor_map_gen.go @@ -23,25 +23,26 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{167}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{167}); err != nil { return err } - scratch := make([]byte, 9) - // t.Stuff (testing.SimpleTypeTree) (struct) if len("Stuff") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Stuff\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Stuff"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Stuff"))); err != nil { return err } if _, err := io.WriteString(w, string("Stuff")); err != nil { return err } - if err := t.Stuff.MarshalCBOR(w); err != nil { + if err := t.Stuff.MarshalCBOR(cw); err != nil { return err } @@ -50,14 +51,14 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Stufff\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Stufff"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Stufff"))); err != nil { return err } if _, err := io.WriteString(w, string("Stufff")); err != nil { return err } - if err := t.Stufff.MarshalCBOR(w); err != nil { + if err := t.Stufff.MarshalCBOR(cw); err != nil { return err } @@ -66,7 +67,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Others\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Others"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Others"))); err != nil { return err } if _, err := io.WriteString(w, string("Others")); err != nil { @@ -77,11 +78,11 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Others was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Others))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Others))); err != nil { return err } for _, v := range t.Others { - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } @@ -91,7 +92,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Test\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Test"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Test"))); err != nil { return err } if _, err := io.WriteString(w, string("Test")); err != nil { @@ -102,7 +103,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Test was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Test))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Test))); err != nil { return err } for _, v := range t.Test { @@ -110,11 +111,11 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field v was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(v))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(v))); err != nil { return err } - if _, err := w.Write(v[:]); err != nil { + if _, err := cw.Write(v[:]); err != nil { return err } } @@ -124,7 +125,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Dog\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Dog"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Dog"))); err != nil { return err } if _, err := io.WriteString(w, string("Dog")); err != nil { @@ -135,7 +136,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Dog was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Dog))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Dog))); err != nil { return err } if _, err := io.WriteString(w, string(t.Dog)); err != nil { @@ -147,7 +148,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"SixtyThreeBitIntegerWithASignBit\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("SixtyThreeBitIntegerWithASignBit"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("SixtyThreeBitIntegerWithASignBit"))); err != nil { return err } if _, err := io.WriteString(w, string("SixtyThreeBitIntegerWithASignBit")); err != nil { @@ -155,11 +156,11 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } if t.SixtyThreeBitIntegerWithASignBit >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.SixtyThreeBitIntegerWithASignBit)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.SixtyThreeBitIntegerWithASignBit)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.SixtyThreeBitIntegerWithASignBit-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.SixtyThreeBitIntegerWithASignBit-1)); err != nil { return err } } @@ -169,7 +170,7 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"NotPizza\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("NotPizza"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NotPizza"))); err != nil { return err } if _, err := io.WriteString(w, string("NotPizza")); err != nil { @@ -177,11 +178,11 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { } if t.NotPizza == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(*t.NotPizza)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(*t.NotPizza)); err != nil { return err } } @@ -192,10 +193,9 @@ func (t *SimpleTypeTree) MarshalCBOR(w io.Writer) error { func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { *t = SimpleTypeTree{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -219,7 +219,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -233,16 +233,16 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Stuff = new(SimpleTypeTree) - if err := t.Stuff.UnmarshalCBOR(br); err != nil { + if err := t.Stuff.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Stuff pointer: %w", err) } } @@ -253,16 +253,16 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Stufff = new(SimpleTypeTwo) - if err := t.Stufff.UnmarshalCBOR(br); err != nil { + if err := t.Stufff.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Stufff pointer: %w", err) } } @@ -271,7 +271,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { // t.Others ([]uint64) (slice) case "Others": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -290,7 +290,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { - maj, val, err := cbg.CborReadHeaderBuf(br, scratch) + maj, val, err := cr.ReadHeader() if err != nil { return xerrors.Errorf("failed to read uint64 for t.Others slice: %w", err) } @@ -305,7 +305,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { // t.Test ([][]uint8) (slice) case "Test": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -328,7 +328,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { var extra uint64 var err error - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -344,7 +344,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { t.Test[i] = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Test[i][:]); err != nil { + if _, err := io.ReadFull(cr, t.Test[i][:]); err != nil { return err } } @@ -354,7 +354,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { case "Dog": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -364,7 +364,7 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { // t.SixtyThreeBitIntegerWithASignBit (int64) (int64) case "SixtyThreeBitIntegerWithASignBit": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -392,15 +392,15 @@ func (t *SimpleTypeTree) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -426,18 +426,19 @@ func (t *NeedScratchForMap) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{161}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{161}); err != nil { return err } - scratch := make([]byte, 9) - // t.Thing (bool) (bool) if len("Thing") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Thing\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Thing"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Thing"))); err != nil { return err } if _, err := io.WriteString(w, string("Thing")); err != nil { @@ -453,10 +454,9 @@ func (t *NeedScratchForMap) MarshalCBOR(w io.Writer) error { func (t *NeedScratchForMap) UnmarshalCBOR(r io.Reader) (err error) { *t = NeedScratchForMap{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -480,7 +480,7 @@ func (t *NeedScratchForMap) UnmarshalCBOR(r io.Reader) (err error) { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -492,7 +492,7 @@ func (t *NeedScratchForMap) UnmarshalCBOR(r io.Reader) (err error) { // t.Thing (bool) (bool) case "Thing": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -521,18 +521,19 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{167}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{167}); err != nil { return err } - scratch := make([]byte, 9) - // t.OldStr (string) (string) if len("OldStr") > cbg.MaxLength { return xerrors.Errorf("Value in field \"OldStr\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldStr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStr"))); err != nil { return err } if _, err := io.WriteString(w, string("OldStr")); err != nil { @@ -543,7 +544,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.OldStr was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.OldStr))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.OldStr))); err != nil { return err } if _, err := io.WriteString(w, string(t.OldStr)); err != nil { @@ -555,7 +556,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldBytes\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldBytes"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldBytes"))); err != nil { return err } if _, err := io.WriteString(w, string("OldBytes")); err != nil { @@ -566,11 +567,11 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.OldBytes was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.OldBytes))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.OldBytes))); err != nil { return err } - if _, err := w.Write(t.OldBytes[:]); err != nil { + if _, err := cw.Write(t.OldBytes[:]); err != nil { return err } @@ -579,14 +580,14 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldNum\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldNum"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldNum"))); err != nil { return err } if _, err := io.WriteString(w, string("OldNum")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.OldNum)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.OldNum)); err != nil { return err } @@ -595,7 +596,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldPtr\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldPtr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldPtr"))); err != nil { return err } if _, err := io.WriteString(w, string("OldPtr")); err != nil { @@ -603,11 +604,11 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { } if t.OldPtr == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.OldPtr); err != nil { + if err := cbg.WriteCid(cw, *t.OldPtr); err != nil { return xerrors.Errorf("failed to write cid field t.OldPtr: %w", err) } } @@ -617,7 +618,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldMap\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldMap"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldMap"))); err != nil { return err } if _, err := io.WriteString(w, string("OldMap")); err != nil { @@ -629,7 +630,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("cannot marshal t.OldMap map too large") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajMap, uint64(len(t.OldMap))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajMap, uint64(len(t.OldMap))); err != nil { return err } @@ -645,14 +646,14 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field k was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(k))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(k))); err != nil { return err } if _, err := io.WriteString(w, string(k)); err != nil { return err } - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } @@ -664,7 +665,7 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldArray\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldArray"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldArray"))); err != nil { return err } if _, err := io.WriteString(w, string("OldArray")); err != nil { @@ -675,11 +676,11 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.OldArray was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.OldArray))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.OldArray))); err != nil { return err } for _, v := range t.OldArray { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -689,14 +690,14 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldStruct\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldStruct"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStruct"))); err != nil { return err } if _, err := io.WriteString(w, string("OldStruct")); err != nil { return err } - if err := t.OldStruct.MarshalCBOR(w); err != nil { + if err := t.OldStruct.MarshalCBOR(cw); err != nil { return err } return nil @@ -705,10 +706,9 @@ func (t *SimpleStructV1) MarshalCBOR(w io.Writer) error { func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { *t = SimpleStructV1{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -732,7 +732,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -745,7 +745,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { case "OldStr": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -755,7 +755,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { // t.OldBytes ([]uint8) (slice) case "OldBytes": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -771,7 +771,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { t.OldBytes = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.OldBytes[:]); err != nil { + if _, err := io.ReadFull(cr, t.OldBytes[:]); err != nil { return err } // t.OldNum (uint64) (uint64) @@ -779,7 +779,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -794,16 +794,16 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.OldPtr: %w", err) } @@ -815,7 +815,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { // t.OldMap (map[string]testing.SimpleTypeOne) (map) case "OldMap": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -833,7 +833,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { var k string { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -845,7 +845,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { { - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling v: %w", err) } @@ -857,7 +857,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { // t.OldArray ([]testing.SimpleTypeOne) (slice) case "OldArray": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -877,7 +877,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { var v SimpleTypeOne - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -889,7 +889,7 @@ func (t *SimpleStructV1) UnmarshalCBOR(r io.Reader) (err error) { { - if err := t.OldStruct.UnmarshalCBOR(br); err != nil { + if err := t.OldStruct.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.OldStruct: %w", err) } @@ -908,18 +908,19 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{174}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{174}); err != nil { return err } - scratch := make([]byte, 9) - // t.OldStr (string) (string) if len("OldStr") > cbg.MaxLength { return xerrors.Errorf("Value in field \"OldStr\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldStr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStr"))); err != nil { return err } if _, err := io.WriteString(w, string("OldStr")); err != nil { @@ -930,7 +931,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.OldStr was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.OldStr))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.OldStr))); err != nil { return err } if _, err := io.WriteString(w, string(t.OldStr)); err != nil { @@ -942,7 +943,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"NewStr\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("NewStr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewStr"))); err != nil { return err } if _, err := io.WriteString(w, string("NewStr")); err != nil { @@ -953,7 +954,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.NewStr was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.NewStr))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.NewStr))); err != nil { return err } if _, err := io.WriteString(w, string(t.NewStr)); err != nil { @@ -965,7 +966,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldBytes\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldBytes"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldBytes"))); err != nil { return err } if _, err := io.WriteString(w, string("OldBytes")); err != nil { @@ -976,11 +977,11 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.OldBytes was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.OldBytes))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.OldBytes))); err != nil { return err } - if _, err := w.Write(t.OldBytes[:]); err != nil { + if _, err := cw.Write(t.OldBytes[:]); err != nil { return err } @@ -989,7 +990,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"NewBytes\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("NewBytes"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewBytes"))); err != nil { return err } if _, err := io.WriteString(w, string("NewBytes")); err != nil { @@ -1000,11 +1001,11 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.NewBytes was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.NewBytes))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.NewBytes))); err != nil { return err } - if _, err := w.Write(t.NewBytes[:]); err != nil { + if _, err := cw.Write(t.NewBytes[:]); err != nil { return err } @@ -1013,14 +1014,14 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldNum\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldNum"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldNum"))); err != nil { return err } if _, err := io.WriteString(w, string("OldNum")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.OldNum)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.OldNum)); err != nil { return err } @@ -1029,14 +1030,14 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"NewNum\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("NewNum"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewNum"))); err != nil { return err } if _, err := io.WriteString(w, string("NewNum")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.NewNum)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.NewNum)); err != nil { return err } @@ -1045,7 +1046,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldPtr\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldPtr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldPtr"))); err != nil { return err } if _, err := io.WriteString(w, string("OldPtr")); err != nil { @@ -1053,11 +1054,11 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } if t.OldPtr == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.OldPtr); err != nil { + if err := cbg.WriteCid(cw, *t.OldPtr); err != nil { return xerrors.Errorf("failed to write cid field t.OldPtr: %w", err) } } @@ -1067,7 +1068,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"NewPtr\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("NewPtr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewPtr"))); err != nil { return err } if _, err := io.WriteString(w, string("NewPtr")); err != nil { @@ -1075,11 +1076,11 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { } if t.NewPtr == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.NewPtr); err != nil { + if err := cbg.WriteCid(cw, *t.NewPtr); err != nil { return xerrors.Errorf("failed to write cid field t.NewPtr: %w", err) } } @@ -1089,7 +1090,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldMap\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldMap"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldMap"))); err != nil { return err } if _, err := io.WriteString(w, string("OldMap")); err != nil { @@ -1101,7 +1102,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("cannot marshal t.OldMap map too large") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajMap, uint64(len(t.OldMap))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajMap, uint64(len(t.OldMap))); err != nil { return err } @@ -1117,14 +1118,14 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field k was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(k))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(k))); err != nil { return err } if _, err := io.WriteString(w, string(k)); err != nil { return err } - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } @@ -1136,7 +1137,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"NewMap\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("NewMap"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewMap"))); err != nil { return err } if _, err := io.WriteString(w, string("NewMap")); err != nil { @@ -1148,7 +1149,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("cannot marshal t.NewMap map too large") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajMap, uint64(len(t.NewMap))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajMap, uint64(len(t.NewMap))); err != nil { return err } @@ -1164,14 +1165,14 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field k was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(k))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(k))); err != nil { return err } if _, err := io.WriteString(w, string(k)); err != nil { return err } - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } @@ -1183,7 +1184,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldArray\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldArray"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldArray"))); err != nil { return err } if _, err := io.WriteString(w, string("OldArray")); err != nil { @@ -1194,11 +1195,11 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.OldArray was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.OldArray))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.OldArray))); err != nil { return err } for _, v := range t.OldArray { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -1208,7 +1209,7 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"NewArray\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("NewArray"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewArray"))); err != nil { return err } if _, err := io.WriteString(w, string("NewArray")); err != nil { @@ -1219,11 +1220,11 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.NewArray was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.NewArray))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.NewArray))); err != nil { return err } for _, v := range t.NewArray { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -1233,14 +1234,14 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"OldStruct\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("OldStruct"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("OldStruct"))); err != nil { return err } if _, err := io.WriteString(w, string("OldStruct")); err != nil { return err } - if err := t.OldStruct.MarshalCBOR(w); err != nil { + if err := t.OldStruct.MarshalCBOR(cw); err != nil { return err } @@ -1249,14 +1250,14 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"NewStruct\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("NewStruct"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NewStruct"))); err != nil { return err } if _, err := io.WriteString(w, string("NewStruct")); err != nil { return err } - if err := t.NewStruct.MarshalCBOR(w); err != nil { + if err := t.NewStruct.MarshalCBOR(cw); err != nil { return err } return nil @@ -1265,10 +1266,9 @@ func (t *SimpleStructV2) MarshalCBOR(w io.Writer) error { func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { *t = SimpleStructV2{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -1292,7 +1292,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1305,7 +1305,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { case "OldStr": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1316,7 +1316,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { case "NewStr": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1326,7 +1326,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { // t.OldBytes ([]uint8) (slice) case "OldBytes": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1342,13 +1342,13 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { t.OldBytes = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.OldBytes[:]); err != nil { + if _, err := io.ReadFull(cr, t.OldBytes[:]); err != nil { return err } // t.NewBytes ([]uint8) (slice) case "NewBytes": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1364,7 +1364,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { t.NewBytes = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.NewBytes[:]); err != nil { + if _, err := io.ReadFull(cr, t.NewBytes[:]); err != nil { return err } // t.OldNum (uint64) (uint64) @@ -1372,7 +1372,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1387,7 +1387,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1402,16 +1402,16 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.OldPtr: %w", err) } @@ -1425,16 +1425,16 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.NewPtr: %w", err) } @@ -1446,7 +1446,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { // t.OldMap (map[string]testing.SimpleTypeOne) (map) case "OldMap": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1464,7 +1464,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { var k string { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1476,7 +1476,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { { - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling v: %w", err) } @@ -1488,7 +1488,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { // t.NewMap (map[string]testing.SimpleTypeOne) (map) case "NewMap": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1506,7 +1506,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { var k string { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1518,7 +1518,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { { - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling v: %w", err) } @@ -1530,7 +1530,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { // t.OldArray ([]testing.SimpleTypeOne) (slice) case "OldArray": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1550,7 +1550,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { var v SimpleTypeOne - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -1560,7 +1560,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { // t.NewArray ([]testing.SimpleTypeOne) (slice) case "NewArray": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1580,7 +1580,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { for i := 0; i < int(extra); i++ { var v SimpleTypeOne - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -1592,7 +1592,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { { - if err := t.OldStruct.UnmarshalCBOR(br); err != nil { + if err := t.OldStruct.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.OldStruct: %w", err) } @@ -1602,7 +1602,7 @@ func (t *SimpleStructV2) UnmarshalCBOR(r io.Reader) (err error) { { - if err := t.NewStruct.UnmarshalCBOR(br); err != nil { + if err := t.NewStruct.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.NewStruct: %w", err) } @@ -1621,18 +1621,19 @@ func (t *RenamedFields) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{162}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{162}); err != nil { return err } - scratch := make([]byte, 9) - // t.Foo (int64) (int64) if len("foo") > cbg.MaxLength { return xerrors.Errorf("Value in field \"foo\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("foo"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("foo"))); err != nil { return err } if _, err := io.WriteString(w, string("foo")); err != nil { @@ -1640,11 +1641,11 @@ func (t *RenamedFields) MarshalCBOR(w io.Writer) error { } if t.Foo >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Foo)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Foo)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Foo-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Foo-1)); err != nil { return err } } @@ -1654,7 +1655,7 @@ func (t *RenamedFields) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"beep\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("beep"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("beep"))); err != nil { return err } if _, err := io.WriteString(w, string("beep")); err != nil { @@ -1665,7 +1666,7 @@ func (t *RenamedFields) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Bar was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Bar))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Bar))); err != nil { return err } if _, err := io.WriteString(w, string(t.Bar)); err != nil { @@ -1677,10 +1678,9 @@ func (t *RenamedFields) MarshalCBOR(w io.Writer) error { func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { *t = RenamedFields{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } @@ -1704,7 +1704,7 @@ func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1716,7 +1716,7 @@ func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { // t.Foo (int64) (int64) case "foo": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1743,7 +1743,7 @@ func (t *RenamedFields) UnmarshalCBOR(r io.Reader) (err error) { case "beep": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } diff --git a/utils.go b/utils.go index a02853d..18e1aa7 100644 --- a/utils.go +++ b/utils.go @@ -9,13 +9,16 @@ import ( "io" "io/ioutil" "math" + "sync" "time" cid "github.com/ipfs/go-cid" ) -const maxCidLength = 100 -const maxHeaderSize = 9 +const ( + maxCidLength = 100 + maxHeaderSize = 9 +) // discard is a helper function to discard data from a reader, special-casing // the most common readers we encounter in this library for a significant @@ -67,6 +70,7 @@ func ScanForLinks(br io.Reader, cb func(cid.Cid)) (err error) { err = io.ErrUnexpectedEOF } }() + scratch := make([]byte, maxCidLength) for remaining := uint64(1); remaining > 0; remaining-- { maj, extra, err := CborReadHeaderBuf(br, scratch) @@ -241,6 +245,8 @@ func readByte(r io.Reader) (byte, error) { return r.ReadByte() case *peeker: return r.ReadByte() + case *CborReader: + return readByte(r.r) case io.ByteReader: return r.ReadByte() } @@ -249,7 +255,11 @@ func readByte(r io.Reader) (byte, error) { return buf[0], err } -func CborReadHeader(br io.Reader) (_b byte, _ui uint64, err error) { +func CborReadHeader(br io.Reader) (byte, uint64, error) { + if cr, ok := br.(*CborReader); ok { + return cr.ReadHeader() + } + first, err := readByte(br) if err != nil { return 0, 0, err @@ -322,6 +332,8 @@ func readByteBuf(r io.Reader, scratch []byte) (byte, error) { return r.ReadByte() case *peeker: return r.ReadByte() + case *CborReader: + return readByte(r.r) case io.ByteReader: return r.ReadByte() } @@ -336,6 +348,12 @@ func CborReadHeaderBuf(br io.Reader, scratch []byte) (byte, uint64, error) { return 0, 0, err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + maj := (first & 0xe0) >> 5 low := first & 0x1f @@ -390,6 +408,10 @@ func CborWriteHeader(w io.Writer, t byte, l uint64) error { // TODO: No matter what I do, this function *still* allocates. Its super frustrating. // See issue: https://github.com/golang/go/issues/33160 func WriteMajorTypeHeader(w io.Writer, t byte, l uint64) error { + if w, ok := w.(*CborWriter); ok { + return w.WriteMajorTypeHeader(t, l) + } + switch { case l < 24: _, err := w.Write([]byte{(t << 5) | byte(l)}) @@ -539,6 +561,13 @@ func WriteBool(w io.Writer, b bool) error { return err } +var stringBufPool = sync.Pool{ + New: func() interface{} { + b := make([]byte, MaxLength) + return &b + }, +} + func ReadString(r io.Reader) (string, error) { maj, l, err := CborReadHeader(r) if err != nil { @@ -553,7 +582,15 @@ func ReadString(r io.Reader) (string, error) { return "", fmt.Errorf("string in input was too long") } - buf := make([]byte, l) + bufp := stringBufPool.Get().(*[]byte) + buf := (*bufp)[:l] // shares same backing array as pooled slice + defer func() { + // optimizes to memclr + for i := range buf { + buf[i] = 0 + } + stringBufPool.Put(bufp) + }() _, err = io.ReadAtLeast(r, buf, int(l)) if err != nil { return "", err @@ -562,27 +599,9 @@ func ReadString(r io.Reader) (string, error) { return string(buf), nil } -func ReadStringBuf(r io.Reader, scratch []byte) (string, error) { - maj, l, err := CborReadHeaderBuf(r, scratch) - if err != nil { - return "", err - } - - if maj != MajTextString { - return "", fmt.Errorf("got tag %d while reading string value (l = %d)", maj, l) - } - - if l > MaxLength { - return "", fmt.Errorf("string in input was too long") - } - - buf := make([]byte, l) - _, err = io.ReadAtLeast(r, buf, int(l)) - if err != nil { - return "", err - } - - return string(buf), nil +// Deprecated: use ReadString +func ReadStringBuf(r io.Reader, _ []byte) (string, error) { + return ReadString(r) } func ReadCid(br io.Reader) (cid.Cid, error) { @@ -595,7 +614,6 @@ func ReadCid(br io.Reader) (cid.Cid, error) { } func bufToCid(buf []byte) (cid.Cid, error) { - if len(buf) == 0 { return cid.Undef, fmt.Errorf("undefined cid") } @@ -614,24 +632,25 @@ func bufToCid(buf []byte) (cid.Cid, error) { var byteArrZero = []byte{0} func WriteCid(w io.Writer, c cid.Cid) error { - if err := WriteMajorTypeHeader(w, MajTag, 42); err != nil { + cw := NewCborWriter(w) + if err := cw.WriteMajorTypeHeader(MajTag, 42); err != nil { return err } if c == cid.Undef { return fmt.Errorf("undefined cid") - //return CborWriteHeader(w, MajByteString, 0) + // return CborWriteHeader(w, MajByteString, 0) } - if err := WriteMajorTypeHeader(w, MajByteString, uint64(c.ByteLen()+1)); err != nil { + if err := cw.WriteMajorTypeHeader(MajByteString, uint64(c.ByteLen()+1)); err != nil { return err } // that binary multibase prefix... - if _, err := w.Write(byteArrZero); err != nil { + if _, err := cw.Write(byteArrZero); err != nil { return err } - if _, err := c.WriteBytes(w); err != nil { + if _, err := c.WriteBytes(cw); err != nil { return err } @@ -644,7 +663,7 @@ func WriteCidBuf(buf []byte, w io.Writer, c cid.Cid) error { } if c == cid.Undef { return fmt.Errorf("undefined cid") - //return CborWriteHeader(w, MajByteString, 0) + // return CborWriteHeader(w, MajByteString, 0) } if err := WriteMajorTypeHeaderBuf(buf, w, MajByteString, uint64(c.ByteLen()+1)); err != nil { diff --git a/validate.go b/validate.go index 04a5e26..3c8495c 100644 --- a/validate.go +++ b/validate.go @@ -13,11 +13,8 @@ func ValidateCBOR(b []byte) error { br := bytes.NewReader(b) - // Allocate some scratch space. - scratch := make([]byte, maxHeaderSize) - for remaining := uint64(1); remaining > 0; remaining-- { - maj, extra, err := CborReadHeaderBuf(br, scratch) + maj, extra, err := CborReadHeader(br) if err != nil { return err }