Created
April 6, 2014 14:21
-
-
Save qikcoin/10006754 to your computer and use it in GitHub Desktop.
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
from: | |
https://github.com/conformal/btcwire/blob/master/common.go | |
// readVarInt reads a variable length integer from r and returns it as a uint64. | |
func readVarInt(r io.Reader, pver uint32) (uint64, error) { | |
b := make([]byte, 8) | |
_, err := io.ReadFull(r, b[0:1]) | |
if err != nil { | |
return 0, err | |
} | |
var rv uint64 | |
discriminant := uint8(b[0]) | |
switch discriminant { | |
case 0xff: | |
_, err := io.ReadFull(r, b) | |
if err != nil { | |
return 0, err | |
} | |
rv = binary.LittleEndian.Uint64(b) | |
case 0xfe: | |
_, err := io.ReadFull(r, b[0:4]) | |
if err != nil { | |
return 0, err | |
} | |
rv = uint64(binary.LittleEndian.Uint32(b)) | |
case 0xfd: | |
_, err := io.ReadFull(r, b[0:2]) | |
if err != nil { | |
return 0, err | |
} | |
rv = uint64(binary.LittleEndian.Uint16(b)) | |
default: | |
rv = uint64(discriminant) | |
} | |
return rv, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment