I2P string format

This commit is contained in:
Hayden Parker
2016-02-04 00:54:51 -08:00
parent 7e32bd2458
commit e6cc4425c6
11 changed files with 76 additions and 17 deletions

View File

@ -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

View File

@ -2,6 +2,3 @@ package common
// the sha256 of some datastructure
type IdentHash [32]byte
// i2p bytestring
type String []byte

View File

@ -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))
}

View File

@ -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),
)

View File

@ -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")
}

View File

@ -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]),
)
}

View File

@ -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 {

View File

@ -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

View File

@ -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])
}

24
lib/common/string.go Normal file
View File

@ -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
}

31
lib/common/string_test.go Normal file
View File

@ -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) {
}