|
| 1 | +// Package codec re-exports the canonical byte encoders used by the |
| 2 | +// p4runtime-go-controller SDK so external callers can produce wire |
| 3 | +// values without reaching into an internal package. |
| 4 | +// |
| 5 | +// All helpers follow the P4Runtime 1.3.0 canonical encoding rule: |
| 6 | +// integer-typed byte strings have no leading zero bytes; the value |
| 7 | +// zero is encoded as an empty byte slice. |
| 8 | +package codec |
| 9 | + |
| 10 | +import ( |
| 11 | + internalcodec "github.com/zhh2001/p4runtime-go-controller/internal/codec" |
| 12 | +) |
| 13 | + |
| 14 | +// EncodeUint returns the canonical byte encoding of v for the given |
| 15 | +// bit width. bitwidth must be in the range [1, 64]. Returns an error if |
| 16 | +// v does not fit in the requested width. |
| 17 | +func EncodeUint(v uint64, bitwidth int) ([]byte, error) { |
| 18 | + return internalcodec.EncodeUint(v, bitwidth) |
| 19 | +} |
| 20 | + |
| 21 | +// MustEncodeUint is the panic-on-error variant of EncodeUint. |
| 22 | +func MustEncodeUint(v uint64, bitwidth int) []byte { |
| 23 | + return internalcodec.MustEncodeUint(v, bitwidth) |
| 24 | +} |
| 25 | + |
| 26 | +// EncodeBytes canonicalises a big-endian byte string for the declared |
| 27 | +// bit width. |
| 28 | +func EncodeBytes(value []byte, bitwidth int) ([]byte, error) { |
| 29 | + return internalcodec.EncodeBytes(value, bitwidth) |
| 30 | +} |
| 31 | + |
| 32 | +// DecodeUint interprets a canonical byte string as uint64. An empty |
| 33 | +// slice decodes to zero. |
| 34 | +func DecodeUint(b []byte) (uint64, error) { |
| 35 | + return internalcodec.DecodeUint(b) |
| 36 | +} |
| 37 | + |
| 38 | +// MAC parses "aa:bb:cc:dd:ee:ff" / "aa-bb-cc-dd-ee-ff" / "aabbccddeeff" |
| 39 | +// and returns the canonical P4Runtime encoding. |
| 40 | +func MAC(s string) ([]byte, error) { |
| 41 | + return internalcodec.MAC(s) |
| 42 | +} |
| 43 | + |
| 44 | +// MustMAC is the panic-on-error variant of MAC. |
| 45 | +func MustMAC(s string) []byte { |
| 46 | + return internalcodec.MustMAC(s) |
| 47 | +} |
| 48 | + |
| 49 | +// IPv4 parses a dotted-quad literal. |
| 50 | +func IPv4(s string) ([]byte, error) { |
| 51 | + return internalcodec.IPv4(s) |
| 52 | +} |
| 53 | + |
| 54 | +// MustIPv4 is the panic-on-error variant of IPv4. |
| 55 | +func MustIPv4(s string) []byte { |
| 56 | + return internalcodec.MustIPv4(s) |
| 57 | +} |
| 58 | + |
| 59 | +// IPv6 parses a colon-separated IPv6 literal. |
| 60 | +func IPv6(s string) ([]byte, error) { |
| 61 | + return internalcodec.IPv6(s) |
| 62 | +} |
| 63 | + |
| 64 | +// MustIPv6 is the panic-on-error variant of IPv6. |
| 65 | +func MustIPv6(s string) []byte { |
| 66 | + return internalcodec.MustIPv6(s) |
| 67 | +} |
| 68 | + |
| 69 | +// LPMMask truncates value to prefixLen bits and returns the canonical |
| 70 | +// encoding. prefixLen must be in [0, bitwidth]; a zero prefix yields an |
| 71 | +// empty slice (caller should omit the match field entirely in that |
| 72 | +// case). |
| 73 | +func LPMMask(value []byte, prefixLen int, bitwidth int) ([]byte, error) { |
| 74 | + return internalcodec.LPMMask(value, prefixLen, bitwidth) |
| 75 | +} |
| 76 | + |
| 77 | +// TernaryMask builds a prefix-style ternary mask with the top prefixLen |
| 78 | +// bits set. |
| 79 | +func TernaryMask(prefixLen, bitwidth int) ([]byte, error) { |
| 80 | + return internalcodec.TernaryMask(prefixLen, bitwidth) |
| 81 | +} |
| 82 | + |
| 83 | +// TernaryApply returns value & mask in canonical encoding. |
| 84 | +func TernaryApply(value, mask []byte, bitwidth int) ([]byte, error) { |
| 85 | + return internalcodec.TernaryApply(value, mask, bitwidth) |
| 86 | +} |
| 87 | + |
| 88 | +// ValidateRange checks that low <= high for the declared bit width. |
| 89 | +func ValidateRange(low, high []byte, bitwidth int) error { |
| 90 | + return internalcodec.ValidateRange(low, high, bitwidth) |
| 91 | +} |
| 92 | + |
| 93 | +// ParseHex parses an optionally-prefixed and colon-separated hex literal. |
| 94 | +func ParseHex(s string) ([]byte, error) { |
| 95 | + return internalcodec.ParseHex(s) |
| 96 | +} |
| 97 | + |
| 98 | +// FormatHex renders v as a colon-separated hex string. |
| 99 | +func FormatHex(v []byte) string { |
| 100 | + return internalcodec.FormatHex(v) |
| 101 | +} |
0 commit comments