From 325178506187df037f9a85eb2e09100eb794f4f9 Mon Sep 17 00:00:00 2001 From: Luke Shumaker <lukeshu@lukeshu.com> Date: Sat, 22 Jul 2023 23:27:02 -0600 Subject: Pull the json-hex-encoding from shortsum to jsonutil --- lib/jsonutil/hex_decoder.go | 61 +++++++++++++++++++++++++++++++++++++++++++++ lib/jsonutil/hex_string.go | 42 +++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 lib/jsonutil/hex_decoder.go create mode 100644 lib/jsonutil/hex_string.go (limited to 'lib/jsonutil') diff --git a/lib/jsonutil/hex_decoder.go b/lib/jsonutil/hex_decoder.go new file mode 100644 index 0000000..e5c84a7 --- /dev/null +++ b/lib/jsonutil/hex_decoder.go @@ -0,0 +1,61 @@ +// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package jsonutil + +import ( + "fmt" + "io" + "math" +) + +type invalidHexRuneError rune + +func (e invalidHexRuneError) Error() string { + return fmt.Sprintf("jsonutil: invalid hex digit: %q", rune(e)) +} + +// hexDecoder is like an encoding/hex.Decoder, but has a "push" +// interface rather than a "pull" interface. +type hexDecoder struct { + dst io.ByteWriter + + buf byte + bufOK bool +} + +func (d *hexDecoder) WriteRune(r rune) (int, error) { + if r > math.MaxUint8 { + return 0, invalidHexRuneError(r) + } + + c := byte(r) + var v byte + //nolint:gomnd // Hex conversion. + switch { + case '0' <= c && c <= '9': + v = c - '0' + case 'a' <= c && c <= 'f': + v = c - 'a' + 10 + case 'A' <= c && c <= 'F': + v = c - 'A' + 10 + default: + return 0, invalidHexRuneError(r) + } + + if !d.bufOK { + d.buf = v + d.bufOK = true + return 1, nil + } + d.bufOK = false + return 1, d.dst.WriteByte(d.buf<<4 | v) +} + +func (d *hexDecoder) Close() error { + if d.bufOK { + return io.ErrUnexpectedEOF + } + return nil +} diff --git a/lib/jsonutil/hex_string.go b/lib/jsonutil/hex_string.go new file mode 100644 index 0000000..06970ce --- /dev/null +++ b/lib/jsonutil/hex_string.go @@ -0,0 +1,42 @@ +// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +// Package jsonutil provides utilities for implementing the interfaces +// consumed by the "git.lukeshu.com/go/lowmemjson" package. +package jsonutil + +import ( + "io" + + "git.lukeshu.com/go/lowmemjson" +) + +func EncodeHexString[T ~[]byte | ~string](w io.Writer, str T) 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(str); i++ { + buf[0] = hextable[str[i]>>4] + buf[1] = hextable[str[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 DecodeHexString(r io.RuneScanner, dst io.ByteWriter) error { + dec := &hexDecoder{dst: dst} + if err := lowmemjson.DecodeString(r, dec); err != nil { + return err + } + return dec.Close() +} -- cgit v1.2.3-2-g168b From c65d6effc26c3d97a6193f65c5b7698c830d9ff0 Mon Sep 17 00:00:00 2001 From: Luke Shumaker <lukeshu@lukeshu.com> Date: Fri, 6 Jan 2023 00:52:43 -0700 Subject: btrfssum: Don't emit JSON strings that are too long Split it, and wrap it in an array. --- lib/jsonutil/hex_string.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'lib/jsonutil') diff --git a/lib/jsonutil/hex_string.go b/lib/jsonutil/hex_string.go index 06970ce..3e0b154 100644 --- a/lib/jsonutil/hex_string.go +++ b/lib/jsonutil/hex_string.go @@ -40,3 +40,47 @@ func DecodeHexString(r io.RuneScanner, dst io.ByteWriter) error { } return dec.Close() } + +func EncodeSplitHexString[T ~[]byte | ~string](w io.Writer, str T, maxStrLen int) error { + if maxStrLen <= 0 || len(str) <= maxStrLen/2 { + return EncodeHexString(w, str) + } + var buf [1]byte + buf[0] = '[' + if _, err := w.Write(buf[:]); err != nil { + return err + } + for len(str) > maxStrLen/2 { + if err := EncodeHexString(w, str[:maxStrLen/2]); err != nil { + return err + } + str = str[maxStrLen/2:] + if len(str) > 0 { + buf[0] = ',' + if _, err := w.Write(buf[:]); err != nil { + return err + } + } + } + if len(str) > 0 { + if err := EncodeHexString(w, str); err != nil { + return err + } + } + buf[0] = ']' + if _, err := w.Write(buf[:]); err != nil { + return err + } + return nil +} + +func DecodeSplitHexString(r io.RuneScanner, dst io.ByteWriter) error { + c, _, _ := r.ReadRune() + _ = r.UnreadRune() + if c == '"' { + return DecodeHexString(r, dst) + } + return lowmemjson.DecodeArray(r, func(r io.RuneScanner) error { + return DecodeHexString(r, dst) + }) +} -- cgit v1.2.3-2-g168b From 6e104326f81ec59ece1817988af41b70e4f4cd15 Mon Sep 17 00:00:00 2001 From: Luke Shumaker <lukeshu@lukeshu.com> Date: Sun, 23 Jul 2023 00:18:47 -0600 Subject: rebuildmappings scan: Include the device size and superblock --- lib/jsonutil/binstruct.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/jsonutil/binstruct.go (limited to 'lib/jsonutil') diff --git a/lib/jsonutil/binstruct.go b/lib/jsonutil/binstruct.go new file mode 100644 index 0000000..7f4bd3f --- /dev/null +++ b/lib/jsonutil/binstruct.go @@ -0,0 +1,48 @@ +// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package jsonutil + +import ( + "bytes" + "fmt" + "io" + + "git.lukeshu.com/go/lowmemjson" + + "git.lukeshu.com/btrfs-progs-ng/lib/binstruct" + "git.lukeshu.com/btrfs-progs-ng/lib/textui" +) + +type Binary[T any] struct { + Val T +} + +var ( + _ lowmemjson.Encodable = Binary[int]{} + _ lowmemjson.Decodable = (*Binary[int])(nil) +) + +func (o Binary[T]) EncodeJSON(w io.Writer) error { + bs, err := binstruct.Marshal(o.Val) + if err != nil { + return err + } + return EncodeSplitHexString(w, bs, textui.Tunable(80)) +} + +func (o *Binary[T]) DecodeJSON(r io.RuneScanner) error { + var buf bytes.Buffer + if err := DecodeSplitHexString(r, &buf); err != nil { + return err + } + n, err := binstruct.Unmarshal(buf.Bytes(), &o.Val) + if err != nil { + return err + } + if n < buf.Len() { + return fmt.Errorf("%d bytes of garbage after value", n-buf.Len()) + } + return nil +} -- cgit v1.2.3-2-g168b