Commit 1b1445dd authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodbpickle: Factor/generalize pickletools.Xstrbytes8 into here as zodbpickle.Unpack64

Similarly to ZODB/py which provides u64 and p64 utilities. Add
zodbpickle.Pack64 for symmetry.
parent fb69a8ca
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
package zodbpickle package zodbpickle
import ( import (
"encoding/binary"
"fmt"
"io" "io"
pickle "github.com/kisielk/og-rek" pickle "github.com/kisielk/og-rek"
...@@ -77,3 +79,27 @@ func NewUnpickler(r io.Reader, loadref func(ref pickle.Ref) (any, error)) *pickl ...@@ -77,3 +79,27 @@ func NewUnpickler(r io.Reader, loadref func(ref pickle.Ref) (any, error)) *pickl
PersistentLoad: loadref, PersistentLoad: loadref,
}) })
} }
// ---- pack/unpack uint64 into [8]bytes ----
// Pack64 packs uint64 into big-endian bytes.
func Pack64(x uint64) pickle.Bytes {
var b [8]byte
binary.BigEndian.PutUint64(b[:], x)
return pickle.Bytes(b[:])
}
// Unpack64 tries to extract [8](bytestr|bytes) from unpickled value as big-endian uint64.
func Unpack64(x interface{}) (uint64, error) {
s, err := pickle.AsBytes(x)
if err != nil {
return 0, err
}
if len(s) != 8 {
return 0, fmt.Errorf("expect [8]bytes; got [%d]bytes", len(s))
}
return binary.BigEndian.Uint64([]byte(s)), nil
}
// Copyright (C) 2017-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// Package pickletools provides utilities related to python pickles.
//
// It complements package ogórek (github.com/kisielk/og-rek).
package pickletools
import (
"encoding/binary"
"fmt"
pickle "github.com/kisielk/og-rek"
)
// Xstrbytes8 verifies and extracts [8](str|bytes) from unpickled value as big-endian u64.
func Xstrbytes8(x interface{}) (uint64, error) {
s, err := pickle.AsBytes(x)
if err != nil {
return 0, err
}
if len(s) != 8 {
return 0, fmt.Errorf("expect [8]bytes; got [%d]bytes", len(s))
}
return binary.BigEndian.Uint64([]byte(s)), nil
}
...@@ -26,7 +26,6 @@ import ( ...@@ -26,7 +26,6 @@ import (
"strings" "strings"
pickle "github.com/kisielk/og-rek" pickle "github.com/kisielk/og-rek"
"lab.nexedi.com/kirr/neo/go/zodb/internal/pickletools"
"lab.nexedi.com/kirr/neo/go/internal/zodbpickle" "lab.nexedi.com/kirr/neo/go/internal/zodbpickle"
"lab.nexedi.com/kirr/go123/xerr" "lab.nexedi.com/kirr/go123/xerr"
...@@ -233,7 +232,7 @@ func xoid(x interface{}) (_ Oid, err error) { ...@@ -233,7 +232,7 @@ func xoid(x interface{}) (_ Oid, err error) {
// ZODB >= 5.4 encodes oid as bytes; before - as str: // ZODB >= 5.4 encodes oid as bytes; before - as str:
// https://github.com/zopefoundation/ZODB/commit/12ee41c473 // https://github.com/zopefoundation/ZODB/commit/12ee41c473
v, err := pickletools.Xstrbytes8(x) v, err := zodbpickle.Unpack64(x)
if err != nil { if err != nil {
return InvalidOid, err return InvalidOid, err
} }
......
...@@ -35,7 +35,6 @@ import ( ...@@ -35,7 +35,6 @@ import (
"lab.nexedi.com/kirr/go123/mem" "lab.nexedi.com/kirr/go123/mem"
"lab.nexedi.com/kirr/neo/go/zodb" "lab.nexedi.com/kirr/neo/go/zodb"
"lab.nexedi.com/kirr/neo/go/zodb/internal/pickletools"
) )
// msg represents 1 message. // msg represents 1 message.
...@@ -315,7 +314,7 @@ func (e encoding) xuint64Unpack(xv interface{}) (uint64, bool) { ...@@ -315,7 +314,7 @@ func (e encoding) xuint64Unpack(xv interface{}) (uint64, bool) {
case 'Z': case 'Z':
// pickle: str|bytes // pickle: str|bytes
v, err := pickletools.Xstrbytes8(xv) v, err := zodbpickle.Unpack64(xv)
if err != nil { if err != nil {
return 0, false return 0, false
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment