aboutsummaryrefslogtreecommitdiffstats
path: root/common/hexutil/json.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2017-03-23 23:36:38 +0800
committerGitHub <noreply@github.com>2017-03-23 23:36:38 +0800
commit8771c3061f340451d0966adcc547338a25f2231f (patch)
treec566cab81cf95a39f85fbe2c98a932af9495eb68 /common/hexutil/json.go
parent11e7a712f469fb24ddb88ecebcefab6ed8880eb8 (diff)
parent37dd9086ec491900311fc39837f4a62ef5fd3a4a (diff)
downloaddexon-8771c3061f340451d0966adcc547338a25f2231f.tar
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.gz
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.bz2
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.lz
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.xz
dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.zst
dexon-8771c3061f340451d0966adcc547338a25f2231f.zip
Merge pull request #3794 from fjl/core-genesis-refactor
core: refactor genesis handling
Diffstat (limited to 'common/hexutil/json.go')
-rw-r--r--common/hexutil/json.go32
1 files changed, 27 insertions, 5 deletions
diff --git a/common/hexutil/json.go b/common/hexutil/json.go
index 23393ed2c..1bc1d014c 100644
--- a/common/hexutil/json.go
+++ b/common/hexutil/json.go
@@ -51,7 +51,7 @@ func (b *Bytes) UnmarshalJSON(input []byte) error {
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *Bytes) UnmarshalText(input []byte) error {
- raw, err := checkText(input)
+ raw, err := checkText(input, true)
if err != nil {
return err
}
@@ -73,7 +73,28 @@ func (b Bytes) String() string {
// determines the required input length. This function is commonly used to implement the
// UnmarshalText method for fixed-size types.
func UnmarshalFixedText(typname string, input, out []byte) error {
- raw, err := checkText(input)
+ raw, err := checkText(input, true)
+ if err != nil {
+ return err
+ }
+ if len(raw)/2 != len(out) {
+ return fmt.Errorf("hex string has length %d, want %d for %s", len(raw), len(out)*2, typname)
+ }
+ // Pre-verify syntax before modifying out.
+ for _, b := range raw {
+ if decodeNibble(b) == badNibble {
+ return ErrSyntax
+ }
+ }
+ hex.Decode(out, raw)
+ return nil
+}
+
+// UnmarshalFixedUnprefixedText decodes the input as a string with optional 0x prefix. The
+// length of out determines the required input length. This function is commonly used to
+// implement the UnmarshalText method for fixed-size types.
+func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error {
+ raw, err := checkText(input, false)
if err != nil {
return err
}
@@ -243,14 +264,15 @@ func bytesHave0xPrefix(input []byte) bool {
return len(input) >= 2 && input[0] == '0' && (input[1] == 'x' || input[1] == 'X')
}
-func checkText(input []byte) ([]byte, error) {
+func checkText(input []byte, wantPrefix bool) ([]byte, error) {
if len(input) == 0 {
return nil, nil // empty strings are allowed
}
- if !bytesHave0xPrefix(input) {
+ if bytesHave0xPrefix(input) {
+ input = input[2:]
+ } else if wantPrefix {
return nil, ErrMissingPrefix
}
- input = input[2:]
if len(input)%2 != 0 {
return nil, ErrOddLength
}