aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuit van Drongelen <luitvd@gmail.com>2011-04-11 10:28:34 -0400
committerAdam Langley <agl@golang.org>2011-04-11 10:28:34 -0400
commitfe3dcfee274a41456187970f6059d091f9dbb525 (patch)
tree94e82bedddf6a5af3099cf13ac033ab2b115d4c2
parent69a9bf4195af3a4537bf4e1c23a99599cdc7b163 (diff)
downloadgo-fe3dcfee274a41456187970f6059d091f9dbb525.tar.gz
go-fe3dcfee274a41456187970f6059d091f9dbb525.zip
asn1: Implement correct marshalling of length octets
Fixes #1683 R=agl1 CC=golang-dev, rsc https://golang.org/cl/4367049
-rw-r--r--src/pkg/asn1/marshal.go26
-rw-r--r--src/pkg/asn1/marshal_test.go24
2 files changed, 48 insertions, 2 deletions
diff --git a/src/pkg/asn1/marshal.go b/src/pkg/asn1/marshal.go
index dfcc15eb20..64cb0f2bbc 100644
--- a/src/pkg/asn1/marshal.go
+++ b/src/pkg/asn1/marshal.go
@@ -125,6 +125,28 @@ func int64Length(i int64) (numBytes int) {
return
}
+func marshalLength(out *forkableWriter, i int) (err os.Error) {
+ n := lengthLength(i)
+
+ for ; n > 0; n-- {
+ err = out.WriteByte(byte(i >> uint((n-1)*8)))
+ if err != nil {
+ return
+ }
+ }
+
+ return nil
+}
+
+func lengthLength(i int) (numBytes int) {
+ numBytes = 1
+ for i > 255 {
+ numBytes++
+ i >>= 8
+ }
+ return
+}
+
func marshalTagAndLength(out *forkableWriter, t tagAndLength) (err os.Error) {
b := uint8(t.class) << 6
if t.isCompound {
@@ -149,12 +171,12 @@ func marshalTagAndLength(out *forkableWriter, t tagAndLength) (err os.Error) {
}
if t.length >= 128 {
- l := int64Length(int64(t.length))
+ l := lengthLength(t.length)
err = out.WriteByte(0x80 | byte(l))
if err != nil {
return
}
- err = marshalInt64(out, int64(t.length))
+ err = marshalLength(out, t.length)
if err != nil {
return
}
diff --git a/src/pkg/asn1/marshal_test.go b/src/pkg/asn1/marshal_test.go
index 85eafc9e4d..cd165d2035 100644
--- a/src/pkg/asn1/marshal_test.go
+++ b/src/pkg/asn1/marshal_test.go
@@ -77,6 +77,30 @@ var marshalTests = []marshalTest{
{ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"},
{ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"},
{"test", "130474657374"},
+ {
+ "" +
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 127 times 'x'
+ "137f" +
+ "7878787878787878787878787878787878787878787878787878787878787878" +
+ "7878787878787878787878787878787878787878787878787878787878787878" +
+ "7878787878787878787878787878787878787878787878787878787878787878" +
+ "78787878787878787878787878787878787878787878787878787878787878",
+ },
+ {
+ "" +
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 128 times 'x'
+ "138180" +
+ "7878787878787878787878787878787878787878787878787878787878787878" +
+ "7878787878787878787878787878787878787878787878787878787878787878" +
+ "7878787878787878787878787878787878787878787878787878787878787878" +
+ "7878787878787878787878787878787878787878787878787878787878787878",
+ },
{ia5StringTest{"test"}, "3006160474657374"},
{printableStringTest{"test"}, "3006130474657374"},
{printableStringTest{"test*"}, "30071305746573742a"},