-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from multiformats/fix/deterministic
fix: minimal varint decoding
- Loading branch information
Showing
6 changed files
with
28 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
module github.com/multiformats/go-multiaddr | ||
|
||
require github.com/multiformats/go-multihash v0.0.8 | ||
require ( | ||
github.com/multiformats/go-multihash v0.0.8 | ||
github.com/multiformats/go-varint v0.0.1 | ||
) | ||
|
||
go 1.12 | ||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,27 @@ | ||
package multiaddr | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"math/bits" | ||
) | ||
"math" | ||
|
||
// VarintSize returns the size (in bytes) of `num` encoded as a varint. | ||
func VarintSize(num int) int { | ||
bits := bits.Len(uint(num)) | ||
q, r := bits/7, bits%7 | ||
size := q | ||
if r > 0 || size == 0 { | ||
size++ | ||
} | ||
return size | ||
} | ||
"github.com/multiformats/go-varint" | ||
) | ||
|
||
// CodeToVarint converts an integer to a varint-encoded []byte | ||
func CodeToVarint(num int) []byte { | ||
buf := make([]byte, VarintSize(num)) | ||
n := binary.PutUvarint(buf, uint64(num)) | ||
return buf[:n] | ||
if num < 0 || num > math.MaxInt32 { | ||
panic("invalid code") | ||
} | ||
return varint.ToUvarint(uint64(num)) | ||
} | ||
|
||
// VarintToCode converts a varint-encoded []byte to an integer protocol code | ||
func VarintToCode(buf []byte) int { | ||
num, _, err := ReadVarintCode(buf) | ||
func ReadVarintCode(b []byte) (int, int, error) { | ||
code, n, err := varint.FromUvarint(b) | ||
if err != nil { | ||
panic(err) | ||
return 0, 0, err | ||
} | ||
return num | ||
} | ||
|
||
// ReadVarintCode reads a varint code from the beginning of buf. | ||
// returns the code, and the number of bytes read. | ||
func ReadVarintCode(buf []byte) (int, int, error) { | ||
num, n := binary.Uvarint(buf) | ||
if n < 0 { | ||
return 0, 0, fmt.Errorf("varints larger than uint64 not yet supported") | ||
if code > math.MaxInt32 { | ||
// we only allow 32bit codes. | ||
return 0, 0, varint.ErrOverflow | ||
} | ||
return int(num), n, nil | ||
return int(code), n, err | ||
} |
This file was deleted.
Oops, something went wrong.