|
| 1 | +package proto |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "time" |
| 6 | + |
| 7 | + "gno.land/p/nt/ufmt" |
| 8 | +) |
| 9 | + |
| 10 | +type WireType int |
| 11 | + |
| 12 | +const ( |
| 13 | + VARINT WireType = 0 // int32, int64, uint32, uint64, sint32, sint64, bool, enum |
| 14 | + FIXED64 WireType = 1 // fixed64, sfixed64, double |
| 15 | + LEN WireType = 2 // string, bytes, embedded messages, packed repeated fields |
| 16 | + FIXED32 WireType = 5 // fixed32, sfixed32, float |
| 17 | +) |
| 18 | + |
| 19 | +// AppendVarint appends a varint field. |
| 20 | +func AppendVarint(buf []byte, fieldNum int, v uint64) []byte { |
| 21 | + if v == 0 { |
| 22 | + return buf |
| 23 | + } |
| 24 | + buf = AppendTag(buf, fieldNum, VARINT) |
| 25 | + return binary.AppendUvarint(buf, v) |
| 26 | +} |
| 27 | + |
| 28 | +// AppendFixed64 appends a fixed 64-bit field. |
| 29 | +func AppendFixed64(buf []byte, fieldNum int, v uint64) []byte { |
| 30 | + if v == 0 { |
| 31 | + return buf |
| 32 | + } |
| 33 | + buf = AppendTag(buf, fieldNum, FIXED64) |
| 34 | + var b [8]byte |
| 35 | + binary.LittleEndian.PutUint64(b[:], v) |
| 36 | + return append(buf, b[:]...) |
| 37 | +} |
| 38 | + |
| 39 | +// AppendLengthDelimited appends a length-delimited field. |
| 40 | +func AppendLengthDelimited(buf []byte, fieldNum int, bz []byte) []byte { |
| 41 | + if len(bz) == 0 { |
| 42 | + return buf |
| 43 | + } |
| 44 | + buf = AppendTag(buf, fieldNum, LEN) |
| 45 | + buf = binary.AppendUvarint(buf, uint64(len(bz))) |
| 46 | + return append(buf, bz...) |
| 47 | +} |
| 48 | + |
| 49 | +// AppendTime appends a google.protobuf.Timestamp field. |
| 50 | +func AppendTime(buf []byte, fieldNum int, t time.Time) []byte { |
| 51 | + var ( |
| 52 | + _buf []byte |
| 53 | + seconds = t.Unix() |
| 54 | + nanos = int32(t.Nanosecond()) |
| 55 | + ) |
| 56 | + // Field 1: seconds (int64 - varint) |
| 57 | + _buf = AppendVarint(_buf, 1, uint64(seconds)) |
| 58 | + // Field 2: nanos (int32 - varint) |
| 59 | + _buf = AppendVarint(_buf, 2, uint64(nanos)) |
| 60 | + |
| 61 | + return AppendLengthDelimited(buf, fieldNum, _buf) |
| 62 | +} |
| 63 | + |
| 64 | +// AppendTag appends a protobuf tag (field number and wire type) |
| 65 | +func AppendTag(buf []byte, fieldNum int, wireType WireType) []byte { |
| 66 | + tag := (fieldNum << 3) | int(wireType) |
| 67 | + return binary.AppendUvarint(buf, uint64(tag)) |
| 68 | +} |
| 69 | + |
| 70 | +// DecodeVarint reads a varint from the byte slice and returns the value and new position |
| 71 | +func DecodeVarint(buf []byte, pos int) (uint64, int, error) { |
| 72 | + var result uint64 |
| 73 | + var shift uint |
| 74 | + for { |
| 75 | + if pos >= len(buf) { |
| 76 | + return 0, pos, ufmt.Errorf("buffer underflow while reading varint") |
| 77 | + } |
| 78 | + b := buf[pos] |
| 79 | + pos++ |
| 80 | + result |= uint64(b&0x7F) << shift |
| 81 | + if b&0x80 == 0 { |
| 82 | + break |
| 83 | + } |
| 84 | + shift += 7 |
| 85 | + } |
| 86 | + return result, pos, nil |
| 87 | +} |
| 88 | + |
| 89 | +// DecodeString reads a string (length-prefixed) from the byte slice |
| 90 | +func DecodeString(buf []byte, pos int) (string, int, error) { |
| 91 | + length, newPos, err := DecodeVarint(buf, pos) |
| 92 | + if err != nil { |
| 93 | + return "", newPos, err |
| 94 | + } |
| 95 | + pos = newPos |
| 96 | + |
| 97 | + if pos+int(length) > len(buf) { |
| 98 | + return "", pos, ufmt.Errorf("buffer underflow while reading string") |
| 99 | + } |
| 100 | + |
| 101 | + str := string(buf[pos : pos+int(length)]) |
| 102 | + return str, pos + int(length), nil |
| 103 | +} |
0 commit comments