use I2P integer

This commit is contained in:
Hayden Parker
2016-02-03 23:55:33 -08:00
parent 28cf578847
commit 51e944ec28
7 changed files with 40 additions and 44 deletions

View File

@ -1,7 +1,6 @@
package common package common
import ( import (
"encoding/binary"
"github.com/bounce-chat/go-i2p/lib/crypto" "github.com/bounce-chat/go-i2p/lib/crypto"
) )
@ -44,7 +43,7 @@ func (c Certificate) Len() int {
// invalid size // invalid size
return -1 return -1
} }
return int(binary.BigEndian.Uint16(c[1:3])) return Integer(c[1:3]...)
} }
// get the data for this certificate or null if none exists // get the data for this certificate or null if none exists
@ -70,7 +69,7 @@ func (c KeyCert) Data() []byte {
// get the signing public key from this key cert // get the signing public key from this key cert
func (c KeyCert) SigningPublicKey() (k crypto.SigningPublicKey) { func (c KeyCert) SigningPublicKey() (k crypto.SigningPublicKey) {
data := c.Data() data := c.Data()
ktype := binary.BigEndian.Uint16(data[:2]) ktype := Integer(data[:2]...)
// set data to be the key data now // set data to be the key data now
data = data[4:] data = data[4:]
// determine the key type // determine the key type

11
lib/common/integer.go Normal file
View File

@ -0,0 +1,11 @@
package common
import (
"encoding/binary"
)
func Integer(number ...byte) int {
return int(
binary.BigEndian.Uint64(number),
)
}

View File

@ -0,0 +1,20 @@
package common
import (
"testing"
)
func TestIntegerBigEndian(t *testing.T) {
bytes := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
i := Integer(bytes...)
if i != 1 {
t.Fatal("Integer() not big endian")
}
}
func TextWorksWith1Byte(t *testing.T) {
i := Integer(0x01)
if i != 1 {
t.Fatal("Integer() does not work with 1 byte")
}
}

View File

@ -1,7 +1,6 @@
package common package common
import ( import (
"encoding/binary"
"github.com/bounce-chat/go-i2p/lib/tunnel" "github.com/bounce-chat/go-i2p/lib/tunnel"
) )
@ -18,7 +17,7 @@ func (lease Lease) TunnelGateway() (h IdentHash) {
func (lease Lease) TunnelID() tunnel.TunnelID { func (lease Lease) TunnelID() tunnel.TunnelID {
return tunnel.TunnelID( return tunnel.TunnelID(
binary.BigEndian.Uint32(lease[32:36]), Integer(lease[32:36]...),
) )
} }

View File

@ -1,8 +1,6 @@
package common package common
import ( import (
"bytes"
"encoding/binary"
"github.com/bounce-chat/go-i2p/lib/crypto" "github.com/bounce-chat/go-i2p/lib/crypto"
) )
@ -26,12 +24,7 @@ func (lease_set LeaseSet) SigningKey() (k []byte) {
func (lease_set LeaseSet) LeaseCount() int { func (lease_set LeaseSet) LeaseCount() int {
head := 387 + 256 + lease_set.signingKeySize() head := 387 + 256 + lease_set.signingKeySize()
var count int return Integer(lease_set[head+1])
buf := bytes.NewReader(
[]byte{lease_set[head+1]},
)
binary.Read(buf, binary.BigEndian, &count)
return count
} }
func (lease_set LeaseSet) Leases() []Lease { func (lease_set LeaseSet) Leases() []Lease {

View File

@ -1,19 +1,9 @@
package common package common
import (
"bytes"
"encoding/binary"
)
type RouterAddress []byte type RouterAddress []byte
func (router_address RouterAddress) Cost() int { func (router_address RouterAddress) Cost() int {
var cost int return Integer(router_address[0])
buf := bytes.NewReader(
[]byte{router_address[0]},
)
binary.Read(buf, binary.BigEndian, &cost)
return cost
} }
func (router_address RouterAddress) Expiration() (d Date) { func (router_address RouterAddress) Expiration() (d Date) {
@ -34,13 +24,7 @@ func (router_address RouterAddress) Options() Mapping {
} }
func (router_address RouterAddress) stringLength() int { func (router_address RouterAddress) stringLength() int {
var string_len int return Integer(router_address[9])
buf := bytes.NewReader(
[]byte{router_address[9]},
)
binary.Read(buf, binary.BigEndian, &string_len)
return string_len
} }
func readRouterAddress(data []byte) (RouterAddress, []byte, error) { func readRouterAddress(data []byte) (RouterAddress, []byte, error) {
@ -50,7 +34,7 @@ func readRouterAddress(data []byte) (RouterAddress, []byte, error) {
string_len := router_address.stringLength() string_len := router_address.stringLength()
router_address = append(router_address, data[10:10+string_len]...) router_address = append(router_address, data[10:10+string_len]...)
options_len := int(binary.BigEndian.Uint16(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]...) router_address = append(router_address, data[string_len+10:11+string_len+options_len]...)
return router_address, data[:], nil return router_address, data[:], nil

View File

@ -1,10 +1,5 @@
package common package common
import (
"bytes"
"encoding/binary"
)
type RouterInfo []byte type RouterInfo []byte
func (router_info RouterInfo) RouterIdentity() RouterIdentity { func (router_info RouterInfo) RouterIdentity() RouterIdentity {
@ -20,12 +15,7 @@ func (router_info RouterInfo) Published() (d Date) {
func (router_info RouterInfo) RouterAddressCount() int { func (router_info RouterInfo) RouterAddressCount() int {
_, remainder, _ := readRouterIdentity(router_info) _, remainder, _ := readRouterIdentity(router_info)
var count int return Integer(remainder[8])
buf := bytes.NewReader(
[]byte{remainder[8]},
)
binary.Read(buf, binary.BigEndian, &count)
return count
} }
func (router_info RouterInfo) RouterAddresses() []RouterAddress { func (router_info RouterInfo) RouterAddresses() []RouterAddress {
@ -74,5 +64,5 @@ func (router_info RouterInfo) optionsLocation() int {
func (router_info RouterInfo) optionsSize() int { func (router_info RouterInfo) optionsSize() int {
head := router_info.optionsLocation() head := router_info.optionsLocation()
return int(binary.BigEndian.Uint16(router_info[head : head+1])) return Integer(router_info[head : head+1]...)
} }