mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-04 13:32:52 -04:00
use I2P integer
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"github.com/bounce-chat/go-i2p/lib/crypto"
|
||||
)
|
||||
|
||||
@ -44,7 +43,7 @@ func (c Certificate) Len() int {
|
||||
// invalid size
|
||||
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
|
||||
@ -70,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 := binary.BigEndian.Uint16(data[:2])
|
||||
ktype := Integer(data[:2]...)
|
||||
// set data to be the key data now
|
||||
data = data[4:]
|
||||
// determine the key type
|
||||
|
11
lib/common/integer.go
Normal file
11
lib/common/integer.go
Normal file
@ -0,0 +1,11 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
func Integer(number ...byte) int {
|
||||
return int(
|
||||
binary.BigEndian.Uint64(number),
|
||||
)
|
||||
}
|
20
lib/common/integer_test.go
Normal file
20
lib/common/integer_test.go
Normal 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")
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"github.com/bounce-chat/go-i2p/lib/tunnel"
|
||||
)
|
||||
|
||||
@ -18,7 +17,7 @@ func (lease Lease) TunnelGateway() (h IdentHash) {
|
||||
|
||||
func (lease Lease) TunnelID() tunnel.TunnelID {
|
||||
return tunnel.TunnelID(
|
||||
binary.BigEndian.Uint32(lease[32:36]),
|
||||
Integer(lease[32:36]...),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"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 {
|
||||
head := 387 + 256 + lease_set.signingKeySize()
|
||||
var count int
|
||||
buf := bytes.NewReader(
|
||||
[]byte{lease_set[head+1]},
|
||||
)
|
||||
binary.Read(buf, binary.BigEndian, &count)
|
||||
return count
|
||||
return Integer(lease_set[head+1])
|
||||
}
|
||||
|
||||
func (lease_set LeaseSet) Leases() []Lease {
|
||||
|
@ -1,19 +1,9 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
type RouterAddress []byte
|
||||
|
||||
func (router_address RouterAddress) Cost() int {
|
||||
var cost int
|
||||
buf := bytes.NewReader(
|
||||
[]byte{router_address[0]},
|
||||
)
|
||||
binary.Read(buf, binary.BigEndian, &cost)
|
||||
return cost
|
||||
return Integer(router_address[0])
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) Expiration() (d Date) {
|
||||
@ -34,13 +24,7 @@ func (router_address RouterAddress) Options() Mapping {
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) stringLength() int {
|
||||
var string_len int
|
||||
buf := bytes.NewReader(
|
||||
[]byte{router_address[9]},
|
||||
)
|
||||
binary.Read(buf, binary.BigEndian, &string_len)
|
||||
return string_len
|
||||
|
||||
return Integer(router_address[9])
|
||||
}
|
||||
|
||||
func readRouterAddress(data []byte) (RouterAddress, []byte, error) {
|
||||
@ -50,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 := 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]...)
|
||||
|
||||
return router_address, data[:], nil
|
||||
|
@ -1,10 +1,5 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
type RouterInfo []byte
|
||||
|
||||
func (router_info RouterInfo) RouterIdentity() RouterIdentity {
|
||||
@ -20,12 +15,7 @@ func (router_info RouterInfo) Published() (d Date) {
|
||||
|
||||
func (router_info RouterInfo) RouterAddressCount() int {
|
||||
_, remainder, _ := readRouterIdentity(router_info)
|
||||
var count int
|
||||
buf := bytes.NewReader(
|
||||
[]byte{remainder[8]},
|
||||
)
|
||||
binary.Read(buf, binary.BigEndian, &count)
|
||||
return count
|
||||
return Integer(remainder[8])
|
||||
}
|
||||
|
||||
func (router_info RouterInfo) RouterAddresses() []RouterAddress {
|
||||
@ -74,5 +64,5 @@ func (router_info RouterInfo) optionsLocation() int {
|
||||
|
||||
func (router_info RouterInfo) optionsSize() int {
|
||||
head := router_info.optionsLocation()
|
||||
return int(binary.BigEndian.Uint16(router_info[head : head+1]))
|
||||
return Integer(router_info[head : head+1]...)
|
||||
}
|
||||
|
Reference in New Issue
Block a user