Skip to content

Commit 1d2e493

Browse files
authored
Add an hts_crc32 function to use zlib or libdeflate. (#1850)
This follows on from the hts_md5* functions which wrap up either OpenSSL or our own implementation. Libdeflate's crc32 function is considerably faster than the native zlib, so we want to use it in (for example) the new "samtools checksum" code, but we do not wish to add baggage of looking for libdeflate in the configure script.
1 parent cf0e756 commit 1d2e493

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

bgzf.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ BGZF *bgzf_hopen(hFILE *hfp, const char *mode)
548548
}
549549

550550
#ifdef HAVE_LIBDEFLATE
551+
uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) {
552+
return libdeflate_crc32(crc, buf, len);
553+
}
554+
551555
int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level)
552556
{
553557
if (slen == 0) {
@@ -607,6 +611,10 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le
607611

608612
#else
609613

614+
uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) {
615+
return crc32(crc, buf, len);
616+
}
617+
610618
int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level)
611619
{
612620
uint32_t crc;
@@ -1350,13 +1358,7 @@ static void *bgzf_encode_level0_func(void *arg) {
13501358
u16_to_le(~j->uncomp_len, j->comp_data + BLOCK_HEADER_LENGTH + 3);
13511359

13521360
// Trailer (CRC, uncompressed length)
1353-
#ifdef HAVE_LIBDEFLATE
1354-
crc = libdeflate_crc32(0, j->comp_data + BLOCK_HEADER_LENGTH + 5,
1355-
j->uncomp_len);
1356-
#else
1357-
crc = crc32(crc32(0L, NULL, 0L),
1358-
(Bytef*)j->comp_data + BLOCK_HEADER_LENGTH + 5, j->uncomp_len);
1359-
#endif
1361+
crc = hts_crc32(0, j->comp_data + BLOCK_HEADER_LENGTH + 5, j->uncomp_len);
13601362
u32_to_le(crc, j->comp_data + j->comp_len - 8);
13611363
u32_to_le(j->uncomp_len, j->comp_data + j->comp_len - 4);
13621364

htslib/hts.h

+8
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,14 @@ static inline int hts_bin_level(int bin) {
15181518
return l;
15191519
}
15201520

1521+
/**************************************
1522+
* Exposing the CRC32 implementation *
1523+
* Either from zlib or libdeflate. *
1524+
*************************************/
1525+
HTSLIB_EXPORT
1526+
uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len);
1527+
1528+
15211529
//! Compute the corresponding entry into the linear index of a given bin from
15221530
//! a binning index
15231531
/*!

0 commit comments

Comments
 (0)