mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-04 13:32:52 -04:00
I2P string format
This commit is contained in:
@ -43,7 +43,7 @@ func (c Certificate) Len() int {
|
|||||||
// invalid size
|
// invalid size
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
return Integer(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
|
||||||
@ -69,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 := Integer(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
|
||||||
|
@ -2,6 +2,3 @@ package common
|
|||||||
|
|
||||||
// the sha256 of some datastructure
|
// the sha256 of some datastructure
|
||||||
type IdentHash [32]byte
|
type IdentHash [32]byte
|
||||||
|
|
||||||
// i2p bytestring
|
|
||||||
type String []byte
|
|
||||||
|
@ -7,6 +7,6 @@ import (
|
|||||||
type Date [8]byte
|
type Date [8]byte
|
||||||
|
|
||||||
func GoDate(date Date) time.Time {
|
func GoDate(date Date) time.Time {
|
||||||
seconds := Integer(date[:]...)
|
seconds := Integer(date[:])
|
||||||
return time.Unix(0, int64(seconds*1000000))
|
return time.Unix(0, int64(seconds*1000000))
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,14 @@ import (
|
|||||||
"encoding/binary"
|
"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(
|
return int(
|
||||||
binary.BigEndian.Uint64(number),
|
binary.BigEndian.Uint64(number),
|
||||||
)
|
)
|
||||||
|
@ -6,14 +6,14 @@ import (
|
|||||||
|
|
||||||
func TestIntegerBigEndian(t *testing.T) {
|
func TestIntegerBigEndian(t *testing.T) {
|
||||||
bytes := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
|
bytes := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
|
||||||
i := Integer(bytes...)
|
i := Integer(bytes)
|
||||||
if i != 1 {
|
if i != 1 {
|
||||||
t.Fatal("Integer() not big endian")
|
t.Fatal("Integer() not big endian")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TextWorksWith1Byte(t *testing.T) {
|
func TestWorksWith1Byte(t *testing.T) {
|
||||||
i := Integer(0x01)
|
i := Integer([]byte{0x01})
|
||||||
if i != 1 {
|
if i != 1 {
|
||||||
t.Fatal("Integer() does not work with 1 byte")
|
t.Fatal("Integer() does not work with 1 byte")
|
||||||
}
|
}
|
||||||
|
@ -17,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(
|
||||||
Integer(lease[32:36]...),
|
Integer(lease[32:36]),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +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()
|
||||||
return Integer(lease_set[head+1])
|
return Integer([]byte{lease_set[head+1]})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lease_set LeaseSet) Leases() []Lease {
|
func (lease_set LeaseSet) Leases() []Lease {
|
||||||
|
@ -3,7 +3,7 @@ package common
|
|||||||
type RouterAddress []byte
|
type RouterAddress []byte
|
||||||
|
|
||||||
func (router_address RouterAddress) Cost() int {
|
func (router_address RouterAddress) Cost() int {
|
||||||
return Integer(router_address[0])
|
return Integer([]byte{router_address[0]})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (router_address RouterAddress) Expiration() (d Date) {
|
func (router_address RouterAddress) Expiration() (d Date) {
|
||||||
@ -24,7 +24,7 @@ func (router_address RouterAddress) Options() Mapping {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (router_address RouterAddress) stringLength() int {
|
func (router_address RouterAddress) stringLength() int {
|
||||||
return Integer(router_address[9])
|
return Integer([]byte{router_address[9]})
|
||||||
}
|
}
|
||||||
|
|
||||||
func readRouterAddress(data []byte) (RouterAddress, []byte, error) {
|
func readRouterAddress(data []byte) (RouterAddress, []byte, error) {
|
||||||
@ -34,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 := 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]...)
|
router_address = append(router_address, data[string_len+10:11+string_len+options_len]...)
|
||||||
|
|
||||||
return router_address, data[:], nil
|
return router_address, data[:], nil
|
||||||
|
@ -15,7 +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)
|
||||||
return Integer(remainder[8])
|
return Integer([]byte{remainder[8]})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (router_info RouterInfo) RouterAddresses() []RouterAddress {
|
func (router_info RouterInfo) RouterAddresses() []RouterAddress {
|
||||||
@ -64,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 Integer(router_info[head : head+1]...)
|
return Integer(router_info[head : head+1])
|
||||||
}
|
}
|
||||||
|
24
lib/common/string.go
Normal file
24
lib/common/string.go
Normal 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
31
lib/common/string_test.go
Normal 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) {
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user