DecodeBytes decodes a length-delimited slice of bytes from the stream and returns the value. io.ErrUnexpectedEOF is returned if the operation would read past the end of the data.
()
| 185 | // |
| 186 | // io.ErrUnexpectedEOF is returned if the operation would read past the end of the data. |
| 187 | func (d *Decoder) DecodeBytes() ([]byte, error) { |
| 188 | if d.offset >= len(d.p) { |
| 189 | return nil, io.ErrUnexpectedEOF |
| 190 | } |
| 191 | |
| 192 | l, n, err := DecodeVarint(d.p[d.offset:]) |
| 193 | switch { |
| 194 | case err != nil: |
| 195 | return nil, fmt.Errorf("invalid data at byte %d: %w", d.offset, err) |
| 196 | case n == 0: |
| 197 | return nil, fmt.Errorf("invalid data at byte %d: %w", d.offset, ErrInvalidVarintData) |
| 198 | case l > maxFieldLen: |
| 199 | return nil, fmt.Errorf("invalid length (%d) for length-delimited field at byte %d: %w", l, d.offset, ErrLenOverflow) |
| 200 | default: |
| 201 | // length is good |
| 202 | } |
| 203 | |
| 204 | nb := int(l) |
| 205 | if d.offset+n+nb > len(d.p) { |
| 206 | return nil, io.ErrUnexpectedEOF |
| 207 | } |
| 208 | b := d.p[d.offset+n : d.offset+n+nb] |
| 209 | d.offset += n + nb |
| 210 | return b, nil |
| 211 | } |
| 212 | |
| 213 | // DecodeUInt32 decodes a varint-encoded 32-bit unsigned integer from the stream and returns the value. |
| 214 | // |