aboutsummaryrefslogtreecommitdiff
path: root/goldap/asn1.go
diff options
context:
space:
mode:
Diffstat (limited to 'goldap/asn1.go')
-rw-r--r--goldap/asn1.go40
1 files changed, 30 insertions, 10 deletions
diff --git a/goldap/asn1.go b/goldap/asn1.go
index d1539d9..08164eb 100644
--- a/goldap/asn1.go
+++ b/goldap/asn1.go
@@ -223,19 +223,33 @@ func parseInt64(bytes []byte) (ret int64, err error) {
return
}
-func sizeInt64(i int64) (size int) {
- for ; i != 0 || size == 0; i >>= 8 {
- size++
+func sizeInt64(i int64) int {
+ n := 1
+
+ for i > 127 {
+ n++
+ i >>= 8
}
- return
+
+ for i < -128 {
+ n++
+ i >>= 8
+ }
+
+ return n
}
-func writeInt64(bytes *Bytes, i int64) (size int) {
- for ; i != 0 || size == 0; i >>= 8 { // Write at least one byte even if the value is 0
- bytes.writeBytes([]byte{byte(i)})
- size++
+func writeInt64(bytes *Bytes, i int64) int {
+ n := sizeInt64(i)
+ buf := [8]byte{}
+
+ for j := 0; j < n; j++ {
+ b := i >> uint((n-1-j)*8)
+ buf[j] = byte(b)
}
- return
+ bytes.writeBytes(buf[:n])
+
+ return n
}
// parseInt treats the given bytes as a big-endian, signed integer and returns
@@ -713,7 +727,13 @@ func writeTagAndLength(bytes *Bytes, t TagAndLength) (size int) {
panic("Can't have a negative length")
} else if t.Length >= 128 {
- lengthBytes := writeInt64(bytes, int64(t.Length))
+ lengthBytes := 0
+ val := t.Length
+ for val > 0 {
+ lengthBytes++
+ bytes.writeBytes([]byte{byte(val & 0xff)})
+ val >>= 8
+ }
bytes.writeBytes([]byte{byte(0x80 | byte(lengthBytes))})
size += lengthBytes + 1