Skip to content

Commit

Permalink
Add an hts_crc32 function to use zlib or libdeflate.
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 committed Oct 15, 2024
1 parent ca92061 commit ae20105
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 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
7 changes: 7 additions & 0 deletions htslib/hts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,13 @@ static inline int hts_bin_level(int bin) {
return l;
}

/**************************************
* Exposing the CRC32 implementation *
* Either from zlib or libdeflate. *
*************************************/
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 ae20105

Please sign in to comment.