Skip to content

Commit dfd79a7

Browse files
committed
Define a footer length for fletcher32
1 parent 27aeda2 commit dfd79a7

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

numcodecs/fletcher32.pyx

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

1616

17+
cdef const Py_ssize_t FOOTER_LENGTH = sizeof(uint32_t)
18+
19+
1720
cdef uint32_t _fletcher32(const uint8_t[::1] _data):
1821
# converted from
1922
# https://github.com/Unidata/netcdf-c/blob/main/plugins/H5checksum.c#L109
@@ -66,13 +69,13 @@ class Fletcher32(Codec):
6669
codec_id = "fletcher32"
6770

6871
def encode(self, buf):
69-
"""Return buffer plus 4-byte fletcher checksum"""
72+
"""Return buffer plus a footer with the fletcher checksum (4-bytes)"""
7073
buf = ensure_contiguous_ndarray(buf).ravel().view('uint8')
7174
cdef const uint8_t[::1] b_mv = buf
7275
cdef uint8_t* b_ptr = &b_mv[0]
7376
cdef Py_ssize_t b_len = len(b_mv)
7477

75-
cdef Py_ssize_t out_len = b_len + 4
78+
cdef Py_ssize_t out_len = b_len + FOOTER_LENGTH
7679
cdef bytes out = PyBytes_FromStringAndSize(NULL, out_len)
7780
cdef uint8_t* out_ptr = <uint8_t*>out
7881

@@ -88,8 +91,8 @@ class Fletcher32(Codec):
8891
cdef uint8_t* b_ptr = &b_mv[0]
8992
cdef Py_ssize_t b_len = len(b_mv)
9093

91-
val = _fletcher32(b_mv[:-4])
92-
found = load_le32(&b_mv[-4])
94+
val = _fletcher32(b_mv[:-FOOTER_LENGTH])
95+
found = load_le32(&b_mv[-FOOTER_LENGTH])
9396
if val != found:
9497
raise RuntimeError(
9598
f"The fletcher32 checksum of the data ({val}) did not"
@@ -102,7 +105,7 @@ class Fletcher32(Codec):
102105
if out is not None:
103106
out_mv = ensure_contiguous_ndarray(out).view("uint8")
104107
out_ptr = &out_mv[0]
105-
memcpy(out_ptr, b_ptr, b_len - 4)
108+
memcpy(out_ptr, b_ptr, b_len - FOOTER_LENGTH)
106109
else:
107-
out = b_mv[:-4]
110+
out = b_mv[:-FOOTER_LENGTH]
108111
return out

0 commit comments

Comments
 (0)