Skip to content

Commit 56abf3a

Browse files
authored
Define a footer length for fletcher32 (#718)
1 parent 27aeda2 commit 56abf3a

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

numcodecs/fletcher32.pyx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ from numcodecs.abc import Codec
1414
from numcodecs.compat import ensure_contiguous_ndarray
1515

1616

17+
cdef extern from *:
18+
"""
19+
const Py_ssize_t FOOTER_LENGTH = sizeof(uint32_t);
20+
"""
21+
const Py_ssize_t FOOTER_LENGTH
22+
23+
1724
cdef uint32_t _fletcher32(const uint8_t[::1] _data):
1825
# converted from
1926
# https://github.com/Unidata/netcdf-c/blob/main/plugins/H5checksum.c#L109
@@ -66,13 +73,13 @@ class Fletcher32(Codec):
6673
codec_id = "fletcher32"
6774

6875
def encode(self, buf):
69-
"""Return buffer plus 4-byte fletcher checksum"""
76+
"""Return buffer plus a footer with the fletcher checksum (4-bytes)"""
7077
buf = ensure_contiguous_ndarray(buf).ravel().view('uint8')
7178
cdef const uint8_t[::1] b_mv = buf
7279
cdef uint8_t* b_ptr = &b_mv[0]
7380
cdef Py_ssize_t b_len = len(b_mv)
7481

75-
cdef Py_ssize_t out_len = b_len + 4
82+
cdef Py_ssize_t out_len = b_len + FOOTER_LENGTH
7683
cdef bytes out = PyBytes_FromStringAndSize(NULL, out_len)
7784
cdef uint8_t* out_ptr = <uint8_t*>out
7885

@@ -88,8 +95,8 @@ class Fletcher32(Codec):
8895
cdef uint8_t* b_ptr = &b_mv[0]
8996
cdef Py_ssize_t b_len = len(b_mv)
9097

91-
val = _fletcher32(b_mv[:-4])
92-
found = load_le32(&b_mv[-4])
98+
val = _fletcher32(b_mv[:-FOOTER_LENGTH])
99+
found = load_le32(&b_mv[-FOOTER_LENGTH])
93100
if val != found:
94101
raise RuntimeError(
95102
f"The fletcher32 checksum of the data ({val}) did not"
@@ -102,7 +109,7 @@ class Fletcher32(Codec):
102109
if out is not None:
103110
out_mv = ensure_contiguous_ndarray(out).view("uint8")
104111
out_ptr = &out_mv[0]
105-
memcpy(out_ptr, b_ptr, b_len - 4)
112+
memcpy(out_ptr, b_ptr, b_len - FOOTER_LENGTH)
106113
else:
107-
out = b_mv[:-4]
114+
out = b_mv[:-FOOTER_LENGTH]
108115
return out

0 commit comments

Comments
 (0)