From e6cc4425c66bd3b825a2a49d55ceea14b26c20f5 Mon Sep 17 00:00:00 2001 From: Hayden Parker Date: Thu, 4 Feb 2016 00:54:51 -0800 Subject: [PATCH] I2P string format --- lib/common/certificate.go | 4 ++-- lib/common/data.go | 3 --- lib/common/date.go | 2 +- lib/common/integer.go | 9 ++++++++- lib/common/integer_test.go | 6 +++--- lib/common/lease.go | 2 +- lib/common/lease_set.go | 2 +- lib/common/router_address.go | 6 +++--- lib/common/router_info.go | 4 ++-- lib/common/string.go | 24 ++++++++++++++++++++++++ lib/common/string_test.go | 31 +++++++++++++++++++++++++++++++ 11 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 lib/common/string.go create mode 100644 lib/common/string_test.go diff --git a/lib/common/certificate.go b/lib/common/certificate.go index 52676fa..eeb6786 100644 --- a/lib/common/certificate.go +++ b/lib/common/certificate.go @@ -43,7 +43,7 @@ func (c Certificate) Len() int { // invalid size return -1 } - return Integer(c[1:3]...) + return Integer(c[1:3]) } // get the data for this certificate or null if none exists @@ -69,7 +69,7 @@ func (c KeyCert) Data() []byte { // get the signing public key from this key cert func (c KeyCert) SigningPublicKey() (k crypto.SigningPublicKey) { data := c.Data() - ktype := Integer(data[:2]...) + ktype := Integer(data[:2]) // set data to be the key data now data = data[4:] // determine the key type diff --git a/lib/common/data.go b/lib/common/data.go index 7b4eb4a..9d54432 100644 --- a/lib/common/data.go +++ b/lib/common/data.go @@ -2,6 +2,3 @@ package common // the sha256 of some datastructure type IdentHash [32]byte - -// i2p bytestring -type String []byte diff --git a/lib/common/date.go b/lib/common/date.go index 06bbb94..bfcb143 100644 --- a/lib/common/date.go +++ b/lib/common/date.go @@ -7,6 +7,6 @@ import ( type Date [8]byte func GoDate(date Date) time.Time { - seconds := Integer(date[:]...) + seconds := Integer(date[:]) return time.Unix(0, int64(seconds*1000000)) } diff --git a/lib/common/integer.go b/lib/common/integer.go index 8ef3823..2c9bf54 100644 --- a/lib/common/integer.go +++ b/lib/common/integer.go @@ -4,7 +4,14 @@ import ( "encoding/binary" ) -func Integer(number ...byte) int { +func Integer(number []byte) int { + num_len := len(number) + if num_len < 8 { + number = append( + make([]byte, 8-num_len), + number..., + ) + } return int( binary.BigEndian.Uint64(number), ) diff --git a/lib/common/integer_test.go b/lib/common/integer_test.go index b344177..06b6641 100644 --- a/lib/common/integer_test.go +++ b/lib/common/integer_test.go @@ -6,14 +6,14 @@ import ( func TestIntegerBigEndian(t *testing.T) { bytes := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} - i := Integer(bytes...) + i := Integer(bytes) if i != 1 { t.Fatal("Integer() not big endian") } } -func TextWorksWith1Byte(t *testing.T) { - i := Integer(0x01) +func TestWorksWith1Byte(t *testing.T) { + i := Integer([]byte{0x01}) if i != 1 { t.Fatal("Integer() does not work with 1 byte") } diff --git a/lib/common/lease.go b/lib/common/lease.go index 0f2ce35..d24963e 100644 --- a/lib/common/lease.go +++ b/lib/common/lease.go @@ -17,7 +17,7 @@ func (lease Lease) TunnelGateway() (h IdentHash) { func (lease Lease) TunnelID() tunnel.TunnelID { return tunnel.TunnelID( - Integer(lease[32:36]...), + Integer(lease[32:36]), ) } diff --git a/lib/common/lease_set.go b/lib/common/lease_set.go index 841f194..03ad857 100644 --- a/lib/common/lease_set.go +++ b/lib/common/lease_set.go @@ -24,7 +24,7 @@ func (lease_set LeaseSet) SigningKey() (k []byte) { func (lease_set LeaseSet) LeaseCount() int { head := 387 + 256 + lease_set.signingKeySize() - return Integer(lease_set[head+1]) + return Integer([]byte{lease_set[head+1]}) } func (lease_set LeaseSet) Leases() []Lease { diff --git a/lib/common/router_address.go b/lib/common/router_address.go index 28f0fb9..00fa624 100644 --- a/lib/common/router_address.go +++ b/lib/common/router_address.go @@ -3,7 +3,7 @@ package common type RouterAddress []byte func (router_address RouterAddress) Cost() int { - return Integer(router_address[0]) + return Integer([]byte{router_address[0]}) } func (router_address RouterAddress) Expiration() (d Date) { @@ -24,7 +24,7 @@ func (router_address RouterAddress) Options() Mapping { } func (router_address RouterAddress) stringLength() int { - return Integer(router_address[9]) + return Integer([]byte{router_address[9]}) } func readRouterAddress(data []byte) (RouterAddress, []byte, error) { @@ -34,7 +34,7 @@ func readRouterAddress(data []byte) (RouterAddress, []byte, error) { string_len := router_address.stringLength() router_address = append(router_address, data[10:10+string_len]...) - options_len := Integer(data[string_len+10 : string_len+11]...) + options_len := Integer(data[string_len+10 : string_len+11]) router_address = append(router_address, data[string_len+10:11+string_len+options_len]...) return router_address, data[:], nil diff --git a/lib/common/router_info.go b/lib/common/router_info.go index 21f0ceb..25bf209 100644 --- a/lib/common/router_info.go +++ b/lib/common/router_info.go @@ -15,7 +15,7 @@ func (router_info RouterInfo) Published() (d Date) { func (router_info RouterInfo) RouterAddressCount() int { _, remainder, _ := readRouterIdentity(router_info) - return Integer(remainder[8]) + return Integer([]byte{remainder[8]}) } func (router_info RouterInfo) RouterAddresses() []RouterAddress { @@ -64,5 +64,5 @@ func (router_info RouterInfo) optionsLocation() int { func (router_info RouterInfo) optionsSize() int { head := router_info.optionsLocation() - return Integer(router_info[head : head+1]...) + return Integer(router_info[head : head+1]) } diff --git a/lib/common/string.go b/lib/common/string.go new file mode 100644 index 0000000..b0e081e --- /dev/null +++ b/lib/common/string.go @@ -0,0 +1,24 @@ +package common + +import ( + "errors" +) + +type String []byte + +func ReadString(data []byte) (str String, remainder []byte, err error) { + if len(data) == 0 { + err = errors.New("no string in empty byte slice") + return + } + length := Integer([]byte{data[0]}) + data = data[1:] + + if len(data) < length { + err = errors.New("string longer than provided slice") + return + } + str = data[:length] + remainder = data[length:] + return +} diff --git a/lib/common/string_test.go b/lib/common/string_test.go new file mode 100644 index 0000000..fff48b9 --- /dev/null +++ b/lib/common/string_test.go @@ -0,0 +1,31 @@ +package common + +import "testing" + +func TestReadStringReadsLength(t *testing.T) { + bytes := []byte{0x01, 0x04, 0x06} + str, remainder, err := ReadString(bytes) + if err != nil { + t.Fatal("ReadString() returner error,", err) + } + if len(str) != 1 { + t.Fatal("ReadString() did not return correct string length:", len(str)) + } + if str[0] != 0x04 { + t.Fatal("ReadString() did not return correct string") + } + if len(remainder) != 1 { + t.Fatal("ReadString() did not return correct remainder length") + } + if remainder[0] != 0x06 { + t.Fatal("ReadString() did not return correct remainder") + } +} + +func TestReadStringErrWhenEmptySlice(t *testing.T) { + +} + +func TestReadStringErrWhenStringTooLong(t *testing.T) { + +}