summaryrefslogtreecommitdiff
path: root/lib/btrfs
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-07-22 23:27:02 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2023-07-23 00:07:30 -0600
commit325178506187df037f9a85eb2e09100eb794f4f9 (patch)
tree4031d78531ffffd863ed3a08e405316a37adee1f /lib/btrfs
parentab268a66f3554976cc1ffac9dbf03c6ad2bcdf0c (diff)
Pull the json-hex-encoding from shortsum to jsonutil
Diffstat (limited to 'lib/btrfs')
-rw-r--r--lib/btrfs/btrfssum/shortsum.go67
1 files changed, 4 insertions, 63 deletions
diff --git a/lib/btrfs/btrfssum/shortsum.go b/lib/btrfs/btrfssum/shortsum.go
index 754a79d..490c40a 100644
--- a/lib/btrfs/btrfssum/shortsum.go
+++ b/lib/btrfs/btrfssum/shortsum.go
@@ -5,12 +5,12 @@
package btrfssum
import (
- "fmt"
"io"
- "math"
"strings"
"git.lukeshu.com/go/lowmemjson"
+
+ "git.lukeshu.com/btrfs-progs-ng/lib/jsonutil"
)
type ShortSum string
@@ -27,72 +27,13 @@ func (sum ShortSum) ToFullSum() CSum {
}
func (sum ShortSum) EncodeJSON(w io.Writer) error {
- const hextable = "0123456789abcdef"
- var buf [2]byte
- buf[0] = '"'
- if _, err := w.Write(buf[:1]); err != nil {
- return err
- }
- for i := 0; i < len(sum); i++ {
- buf[0] = hextable[sum[i]>>4]
- buf[1] = hextable[sum[i]&0x0f]
- if _, err := w.Write(buf[:]); err != nil {
- return err
- }
- }
- buf[0] = '"'
- if _, err := w.Write(buf[:1]); err != nil {
- return err
- }
- return nil
-}
-
-func deHex(r rune) (byte, bool) {
- if r > math.MaxUint8 {
- return 0, false
- }
- c := byte(r)
- //nolint:gomnd // Hex conversion.
- switch {
- case '0' <= c && c <= '9':
- return c - '0', true
- case 'a' <= c && c <= 'f':
- return c - 'a' + 10, true
- case 'A' <= c && c <= 'F':
- return c - 'A' + 10, true
- default:
- return 0, false
- }
+ return jsonutil.EncodeHexString(w, sum)
}
func (sum *ShortSum) DecodeJSON(r io.RuneScanner) error {
var out strings.Builder
- if c, _, err := r.ReadRune(); err != nil {
+ if err := jsonutil.DecodeHexString(r, &out); err != nil {
return err
- } else if c != '"' {
- return fmt.Errorf("expected %q, got %q", '"', c)
- }
- for {
- a, _, err := r.ReadRune()
- if err != nil {
- return err
- }
- if a == '"' {
- break
- }
- aN, ok := deHex(a)
- if !ok {
- return fmt.Errorf("expected a hex digit, got %q", a)
- }
- b, _, err := r.ReadRune()
- if err != nil {
- return err
- }
- bN, ok := deHex(b)
- if !ok {
- return fmt.Errorf("expected a hex digit, got %q", b)
- }
- out.WriteByte(aN<<4 | bN)
}
*sum = ShortSum(out.String())
return nil