diff options
Diffstat (limited to 'src/pkg/asn1/marshal.go')
-rw-r--r-- | src/pkg/asn1/marshal.go | 40 |
1 files changed, 10 insertions, 30 deletions
diff --git a/src/pkg/asn1/marshal.go b/src/pkg/asn1/marshal.go index d4f8f782d..24548714b 100644 --- a/src/pkg/asn1/marshal.go +++ b/src/pkg/asn1/marshal.go @@ -96,19 +96,6 @@ func marshalBase128Int(out *forkableWriter, n int64) (err os.Error) { return nil } -func base128Length(i int) (numBytes int) { - if i == 0 { - return 1 - } - - for i > 0 { - numBytes++ - i >>= 7 - } - - return -} - func marshalInt64(out *forkableWriter, i int64) (err os.Error) { n := int64Length(i) @@ -123,11 +110,14 @@ func marshalInt64(out *forkableWriter, i int64) (err os.Error) { } func int64Length(i int64) (numBytes int) { - if i == 0 { - return 1 + numBytes = 1 + + for i > 127 { + numBytes++ + i >>= 8 } - for i > 0 { + for i < -128 { numBytes++ i >>= 8 } @@ -478,25 +468,15 @@ func marshalField(out *forkableWriter, v reflect.Value, params fieldParameters) return nil } -// Marshal serialises val as an ASN.1 structure and writes the result to out. -// In the case of an error, no output is produced. -func Marshal(out io.Writer, val interface{}) os.Error { +// Marshal returns the ASN.1 encoding of val. +func Marshal(val interface{}) ([]byte, os.Error) { + var out bytes.Buffer v := reflect.NewValue(val) f := newForkableWriter() err := marshalField(f, v, fieldParameters{}) if err != nil { - return err - } - _, err = f.writeTo(out) - return err -} - -// MarshalToMemory performs the same actions as Marshal, but returns the result -// as a byte slice. -func MarshalToMemory(val interface{}) ([]byte, os.Error) { - var out bytes.Buffer - if err := Marshal(&out, val); err != nil { return nil, err } + _, err = f.writeTo(&out) return out.Bytes(), nil } |