2016-02-03 23:55:33 -08:00
|
|
|
package common
|
|
|
|
|
2016-02-14 23:10:37 -08:00
|
|
|
/*
|
|
|
|
I2P Integer
|
2016-06-16 23:17:21 -07:00
|
|
|
https://geti2p.net/spec/common-structures#integer
|
2016-02-14 23:10:37 -08:00
|
|
|
Accurate for version 0.9.24
|
|
|
|
*/
|
|
|
|
|
2016-02-03 23:55:33 -08:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
)
|
|
|
|
|
2016-06-17 21:07:16 -07:00
|
|
|
// Total byte length of an I2P integer
|
2016-02-14 22:40:29 -08:00
|
|
|
const (
|
|
|
|
INTEGER_SIZE = 8
|
|
|
|
)
|
|
|
|
|
2016-02-06 01:42:47 -08:00
|
|
|
//
|
2016-02-14 22:40:29 -08:00
|
|
|
// Interpret a slice of bytes from length 0 to length 8 as a big-endian
|
|
|
|
// integer and return an int representation.
|
2016-02-06 01:42:47 -08:00
|
|
|
//
|
2016-02-14 22:40:29 -08:00
|
|
|
func Integer(number []byte) (value int) {
|
2016-02-04 00:54:51 -08:00
|
|
|
num_len := len(number)
|
2016-02-14 22:40:29 -08:00
|
|
|
if num_len < INTEGER_SIZE {
|
2016-02-04 00:54:51 -08:00
|
|
|
number = append(
|
2016-02-14 22:40:29 -08:00
|
|
|
make([]byte, INTEGER_SIZE-num_len),
|
2016-02-04 00:54:51 -08:00
|
|
|
number...,
|
|
|
|
)
|
|
|
|
}
|
2016-02-14 22:40:29 -08:00
|
|
|
value = int(binary.BigEndian.Uint64(number))
|
|
|
|
return
|
2016-02-03 23:55:33 -08:00
|
|
|
}
|