Detects if the data given might have been encoded with the blocking mode of the xerial snappy library. This mode writes a magic header of the format: +--------+--------------+------------+---------+--------+ | Marker | Magic String | Null / Pad | Version | Co
(payload)
| 163 | |
| 164 | |
| 165 | def _detect_xerial_stream(payload): |
| 166 | """Detects if the data given might have been encoded with the blocking mode |
| 167 | of the xerial snappy library. |
| 168 | |
| 169 | This mode writes a magic header of the format: |
| 170 | +--------+--------------+------------+---------+--------+ |
| 171 | | Marker | Magic String | Null / Pad | Version | Compat | |
| 172 | +--------+--------------+------------+---------+--------+ |
| 173 | | byte | c-string | byte | int32 | int32 | |
| 174 | +--------+--------------+------------+---------+--------+ |
| 175 | | -126 | 'SNAPPY' | \0 | | | |
| 176 | +--------+--------------+------------+---------+--------+ |
| 177 | |
| 178 | The pad appears to be to ensure that SNAPPY is a valid cstring |
| 179 | The version is the version of this format as written by xerial, |
| 180 | in the wild this is currently 1 as such we only support v1. |
| 181 | |
| 182 | Compat is there to claim the minimum supported version that |
| 183 | can read a xerial block stream, presently in the wild this is |
| 184 | 1. |
| 185 | """ |
| 186 | |
| 187 | if len(payload) > 16: |
| 188 | magic = struct.unpack('!' + _XERIAL_V1_FORMAT[:8], bytes(payload)[:8]) |
| 189 | version, compat = struct.unpack('!' + _XERIAL_V1_FORMAT[8:], bytes(payload)[8:16]) |
| 190 | # Until there is more than one way to do xerial blocking, the version + compat |
| 191 | # fields can be ignored. Also some producers (i.e., redpanda) are known to |
| 192 | # incorrectly encode these as little-endian, and that causes us to fail decoding |
| 193 | # when we otherwise would have succeeded. |
| 194 | # See https://github.com/dpkp/kafka-python/issues/2414 |
| 195 | if magic == _XERIAL_V1_HEADER[:8]: |
| 196 | return True |
| 197 | return False |
| 198 | |
| 199 | |
| 200 | def snappy_decode(payload): |
no outgoing calls
searching dependent graphs…