Skip to content

Commit

Permalink
Add an hts_crc32 function to use zlib or libdeflate. (#1850)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkbonfield authored Oct 31, 2024
1 parent cf0e756 commit 1d2e493
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 9 additions & 7 deletions bgzf.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,10 @@ BGZF *bgzf_hopen(hFILE *hfp, const char *mode)
}

#ifdef HAVE_LIBDEFLATE
uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) {
return libdeflate_crc32(crc, buf, len);
}

int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level)
{
if (slen == 0) {
Expand Down Expand Up @@ -607,6 +611,10 @@ int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int le

#else

uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len) {
return crc32(crc, buf, len);
}

int bgzf_compress(void *_dst, size_t *dlen, const void *src, size_t slen, int level)
{
uint32_t crc;
Expand Down Expand Up @@ -1350,13 +1358,7 @@ static void *bgzf_encode_level0_func(void *arg) {
u16_to_le(~j->uncomp_len, j->comp_data + BLOCK_HEADER_LENGTH + 3);

// Trailer (CRC, uncompressed length)
#ifdef HAVE_LIBDEFLATE
crc = libdeflate_crc32(0, j->comp_data + BLOCK_HEADER_LENGTH + 5,
j->uncomp_len);
#else
crc = crc32(crc32(0L, NULL, 0L),
(Bytef*)j->comp_data + BLOCK_HEADER_LENGTH + 5, j->uncomp_len);
#endif
crc = hts_crc32(0, j->comp_data + BLOCK_HEADER_LENGTH + 5, j->uncomp_len);
u32_to_le(crc, j->comp_data + j->comp_len - 8);
u32_to_le(j->uncomp_len, j->comp_data + j->comp_len - 4);

Expand Down
8 changes: 8 additions & 0 deletions htslib/hts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,14 @@ static inline int hts_bin_level(int bin) {
return l;
}

/**************************************
* Exposing the CRC32 implementation *
* Either from zlib or libdeflate. *
*************************************/
HTSLIB_EXPORT
uint32_t hts_crc32(uint32_t crc, const void *buf, size_t len);


//! Compute the corresponding entry into the linear index of a given bin from
//! a binning index
/*!
Expand Down

0 comments on commit 1d2e493

Please sign in to comment.