| 85 | } |
| 86 | |
| 87 | func EncodePaginationKey(states, prefix, key, unsolicited []byte) ([]byte, error) { |
| 88 | if len(states) == 0 { |
| 89 | return nil, fmt.Errorf("%w: states cannot be empty", ErrInvalidPaginationKey) |
| 90 | } |
| 91 | |
| 92 | if len(prefix) == 0 { |
| 93 | return nil, fmt.Errorf("%w: prefix cannot be empty", ErrInvalidPaginationKey) |
| 94 | } |
| 95 | |
| 96 | if len(key) == 0 { |
| 97 | return nil, fmt.Errorf("%w: key cannot be empty", ErrInvalidPaginationKey) |
| 98 | } |
| 99 | |
| 100 | // 4 bytes for checksum |
| 101 | // 1 byte for states count |
| 102 | // len(states) bytes for states |
| 103 | // 1 byte for prefix length |
| 104 | // len(prefix) bytes for prefix |
| 105 | // 1 byte for key length |
| 106 | // len(key) bytes for key |
| 107 | encLen := 4 + 1 + len(states) + 1 + len(prefix) + 1 + len(key) |
| 108 | |
| 109 | if len(unsolicited) > 0 { |
| 110 | encLen += 1 + len(unsolicited) |
| 111 | } |
| 112 | |
| 113 | buf := make([]byte, encLen) |
| 114 | |
| 115 | data := buf[4:] |
| 116 | |
| 117 | tmp := validation.MustEncodeWithLengthPrefix(states) |
| 118 | copy(data, tmp) |
| 119 | |
| 120 | offset := len(tmp) |
| 121 | tmp = validation.MustEncodeWithLengthPrefix(prefix) |
| 122 | |
| 123 | copy(data[offset:], tmp) |
| 124 | offset += len(tmp) |
| 125 | |
| 126 | tmp = validation.MustEncodeWithLengthPrefix(key) |
| 127 | copy(data[offset:], tmp) |
| 128 | |
| 129 | if len(unsolicited) > 0 { |
| 130 | offset += len(tmp) |
| 131 | tmp = validation.MustEncodeWithLengthPrefix(unsolicited) |
| 132 | copy(data[offset:], tmp) |
| 133 | } |
| 134 | |
| 135 | checksum := crc32.ChecksumIEEE(data) |
| 136 | binary.BigEndian.PutUint32(buf, checksum) |
| 137 | |
| 138 | return buf, nil |
| 139 | } |