Skip skips over the encoded field value at the current offset, returning the raw bytes so that the caller can decide what to do with the data. The tag and wire type are validated against the provided values and a DecoderSkipError error is returned if they do not match. This check is skipped when u
(tag int, wt WireType)
| 918 | // |
| 919 | // io.ErrUnexpectedEOF is returned if the operation would advance past the end of the data. |
| 920 | func (d *Decoder) Skip(tag int, wt WireType) ([]byte, error) { |
| 921 | if d.offset >= len(d.p) { |
| 922 | return nil, io.ErrUnexpectedEOF |
| 923 | } |
| 924 | sz := SizeOfTagKey(tag) |
| 925 | bof := d.offset - sz |
| 926 | // account for skipping the first field |
| 927 | if bof < 0 { |
| 928 | bof = 0 |
| 929 | } |
| 930 | // validate that the field we're skipping matches the specified tag and wire type |
| 931 | // . skip validation in fast mode |
| 932 | if d.mode == DecoderModeSafe { |
| 933 | v, n, err := DecodeVarint(d.p[bof:]) |
| 934 | if err != nil { |
| 935 | return nil, fmt.Errorf("invalid data at byte %d: %w", bof, err) |
| 936 | } |
| 937 | if n != sz { |
| 938 | return nil, fmt.Errorf("invalid data at byte %d: %w", bof, ErrInvalidVarintData) |
| 939 | } |
| 940 | thisTag, thisWireType := int(v>>3), WireType(v&0x7) |
| 941 | if thisTag != tag || thisWireType != wt { |
| 942 | return nil, &DecoderSkipError{ |
| 943 | ExpectedTag: tag, |
| 944 | ExpectedWireType: wt, |
| 945 | ActualTag: thisTag, |
| 946 | ActualWireType: thisWireType, |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | skipped := 0 |
| 951 | switch wt { |
| 952 | case WireTypeVarint: |
| 953 | _, n, err := DecodeVarint(d.p[d.offset:]) |
| 954 | if err != nil { |
| 955 | return nil, fmt.Errorf("invalid data at byte %d: %w", d.offset, err) |
| 956 | } |
| 957 | skipped = n |
| 958 | case WireTypeFixed64: |
| 959 | skipped = 8 |
| 960 | case WireTypeLengthDelimited: |
| 961 | l, n, err := DecodeVarint(d.p[d.offset:]) |
| 962 | switch { |
| 963 | case err != nil: |
| 964 | return nil, fmt.Errorf("invalid data at byte %d: %w", d.offset, err) |
| 965 | case n == 0: |
| 966 | return nil, fmt.Errorf("invalid data at byte %d: %w", d.offset, ErrInvalidVarintData) |
| 967 | case l > maxFieldLen: |
| 968 | return nil, fmt.Errorf("invalid length (%d) for length-delimited field at byte %d: %w", l, d.offset, ErrLenOverflow) |
| 969 | default: |
| 970 | // length is good |
| 971 | } |
| 972 | |
| 973 | skipped = n + int(l) |
| 974 | |
| 975 | case WireTypeFixed32: |
| 976 | skipped = 4 |
| 977 | default: |