DecodePaginationKey parses the pagination key and returns the states, prefix and key to be used by the FilteredPaginate
(key []byte)
| 14 | |
| 15 | // DecodePaginationKey parses the pagination key and returns the states, prefix and key to be used by the FilteredPaginate |
| 16 | func DecodePaginationKey(key []byte) ([]byte, []byte, []byte, []byte, error) { |
| 17 | if len(key) < 5 { |
| 18 | return nil, nil, nil, nil, fmt.Errorf("%w: invalid key length", ErrInvalidPaginationKey) |
| 19 | } |
| 20 | |
| 21 | expectedChecksum := binary.BigEndian.Uint32(key) |
| 22 | |
| 23 | key = key[4:] |
| 24 | |
| 25 | checksum := crc32.ChecksumIEEE(key) |
| 26 | |
| 27 | if expectedChecksum != checksum { |
| 28 | return nil, nil, nil, nil, fmt.Errorf("%w: invalid checksum, 0x%08x != 0x%08x", ErrInvalidPaginationKey, expectedChecksum, checksum) |
| 29 | } |
| 30 | |
| 31 | statesC := int(key[0]) |
| 32 | key = key[1:] |
| 33 | |
| 34 | if len(key) < statesC { |
| 35 | return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 36 | } |
| 37 | |
| 38 | states := make([]byte, 0, statesC) |
| 39 | states = append(states, key[:statesC]...) |
| 40 | |
| 41 | key = key[len(states):] |
| 42 | |
| 43 | if len(key) < 1 { |
| 44 | return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 45 | } |
| 46 | |
| 47 | prefixLength := int(key[0]) |
| 48 | key = key[1:] |
| 49 | if len(key) < prefixLength { |
| 50 | return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 51 | } |
| 52 | |
| 53 | prefix := key[:prefixLength] |
| 54 | |
| 55 | key = key[prefixLength:] |
| 56 | |
| 57 | if len(key) == 0 { |
| 58 | return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 59 | } |
| 60 | |
| 61 | keyLength := int(key[0]) |
| 62 | key = key[1:] |
| 63 | |
| 64 | if len(key) < keyLength { |
| 65 | return nil, nil, nil, nil, fmt.Errorf("%w: invalid state length", ErrInvalidPaginationKey) |
| 66 | } |
| 67 | |
| 68 | pkey := key[:keyLength] |
| 69 | |
| 70 | key = key[keyLength:] |
| 71 | var unsolicited []byte |
| 72 | |
| 73 | if len(key) > 0 { |
no outgoing calls