From c7facfebd42a611cf6888f4ec22a0b764d445e32 Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Sat, 1 Mar 2025 22:09:58 +0100 Subject: [PATCH] pcrc32c namespace --- pclsync/pcrc32c.c | 14 +++++++------- pclsync/pcrc32c.h | 13 +++++-------- pclsync/pdiff.c | 6 +++--- pclsync/pfscrypto.c | 28 ++++++++++++++-------------- pclsync/ppagecache.c | 24 ++++++++++++------------ 5 files changed, 41 insertions(+), 44 deletions(-) diff --git a/pclsync/pcrc32c.c b/pclsync/pcrc32c.c index dadba6d..03b8b6f 100644 --- a/pclsync/pcrc32c.c +++ b/pclsync/pcrc32c.c @@ -436,7 +436,7 @@ static int crc_hashw = 0; PSYNC_NOINLINE static uint32_t psync_crc32c_sw(uint32_t crc, const void *ptr, size_t len) { #else -uint32_t psync_crc32c(uint32_t crc, const void *ptr, size_t len) { +uint32_t pcrc32c_compute(uint32_t crc, const void *ptr, size_t len) { #endif const char *data; data = (const char *)ptr; @@ -779,7 +779,7 @@ PSYNC_NOINLINE static uint32_t psync_crc32c_init(uint32_t crc, const void *ptr, return psync_crc32c_sw(crc, ptr, len); } -uint32_t psync_crc32c(uint32_t crc, const void *ptr, size_t len) { +uint32_t pcrc32c_compute(uint32_t crc, const void *ptr, size_t len) { #if defined(CRC32_GNUC) && defined(__SSE4_2__) return psync_crc32c_hw(crc, ptr, len); #else @@ -800,7 +800,7 @@ uint32_t psync_crc32c(uint32_t crc, const void *ptr, size_t len) { * https://code.google.com/p/cityhash/ */ -void psync_fast_hash256_init(psync_fast_hash256_ctx *ctx) { +void pcrc32c_fast_hash256_init(psync_fast_hash256_ctx *ctx) { #ifdef CRC32_HW if (unlikely(!crc_hashw)) crc_hashw = psync_has_hw_crc() + 1; @@ -810,10 +810,10 @@ void psync_fast_hash256_init(psync_fast_hash256_ctx *ctx) { ctx->buff[PSYNC_FAST_HASH256_BLOCK_LEN - 1] = 0; } -void psync_fast_hash256_init_seed(psync_fast_hash256_ctx *ctx, const void *seed, +void pcrc32c_fast_hash256_seed(psync_fast_hash256_ctx *ctx, const void *seed, size_t seedlen) { size_t i; - psync_fast_hash256_init(ctx); + pcrc32c_fast_hash256_init(ctx); if (seedlen > sizeof(ctx->state)) seedlen = sizeof(ctx->state); for (i = 0; i < seedlen; i++) @@ -950,7 +950,7 @@ static void psync_fast_hash256_update_long_sw(psync_fast_hash256_ctx *ctx, static void psync_fast_hash256_update_short_sw(psync_fast_hash256_ctx *ctx, const char *cdata, size_t len); -void psync_fast_hash256_update(psync_fast_hash256_ctx *ctx, const void *data, +void pcrc32c_fast_hash256_update(psync_fast_hash256_ctx *ctx, const void *data, size_t len) { ctx->length += len; if (len + ctx->buff[PSYNC_FAST_HASH256_BLOCK_LEN - 1] < @@ -1023,7 +1023,7 @@ psync_fast_hash256_update_long_sw(psync_fast_hash256_ctx *ctx, ctx->buff[PSYNC_FAST_HASH256_BLOCK_LEN - 1] = len; } -void psync_fast_hash256_final(void *hash, psync_fast_hash256_ctx *ctx) { +void pcrc32c_fast_hash256_final(void *hash, psync_fast_hash256_ctx *ctx) { uint64_t a, b, c, d, x, y, len; len = ctx->buff[PSYNC_FAST_HASH256_BLOCK_LEN - 1]; memcpy(ctx->buff + len, &crc32c_table[1][2], diff --git a/pclsync/pcrc32c.h b/pclsync/pcrc32c.h index a32cb68..cc5971f 100644 --- a/pclsync/pcrc32c.h +++ b/pclsync/pcrc32c.h @@ -51,7 +51,7 @@ typedef struct { }; } psync_fast_hash256_ctx; -uint32_t psync_crc32c(uint32_t crc, const void *ptr, size_t len); +uint32_t pcrc32c_compute(uint32_t crc, const void *ptr, size_t len); /* psync_fast_hash256 is supposed to be fast, non-cryptographic strength, * non-collision resistant hash function with large output. Think of it as CRC32 @@ -59,12 +59,9 @@ uint32_t psync_crc32c(uint32_t crc, const void *ptr, size_t len); * chances of CRC32 _random_ collision. It is intended for large inputs, * finalization is relatively expensive. */ - -void psync_fast_hash256_init(psync_fast_hash256_ctx *ctx); -void psync_fast_hash256_init_seed(psync_fast_hash256_ctx *ctx, const void *seed, - size_t seedlen); -void psync_fast_hash256_update(psync_fast_hash256_ctx *ctx, const void *data, - size_t len); -void psync_fast_hash256_final(void *hash, psync_fast_hash256_ctx *ctx); +void pcrc32c_fast_hash256_init(psync_fast_hash256_ctx *ctx); +void pcrc32c_fast_hash256_seed(psync_fast_hash256_ctx *ctx, const void *seed, size_t seedlen); +void pcrc32c_fast_hash256_update(psync_fast_hash256_ctx *ctx, const void *data, size_t len); +void pcrc32c_fast_hash256_final(void *hash, psync_fast_hash256_ctx *ctx); #endif diff --git a/pclsync/pdiff.c b/pclsync/pdiff.c index cb1dffb..67c6533 100644 --- a/pclsync/pdiff.c +++ b/pclsync/pdiff.c @@ -2848,10 +2848,10 @@ static void psync_diff_adapter_hash(void *out) { psync_fast_hash256_ctx ctx; psock_ifaces_t *list; list = psock_list_adapters(); - psync_fast_hash256_init(&ctx); - psync_fast_hash256_update(&ctx, list->interfaces, + pcrc32c_fast_hash256_init(&ctx); + pcrc32c_fast_hash256_update(&ctx, list->interfaces, list->interfacecnt * sizeof(psock_iface_t)); - psync_fast_hash256_final(out, &ctx); + pcrc32c_fast_hash256_final(out, &ctx); psync_free(list); } diff --git a/pclsync/pfscrypto.c b/pclsync/pfscrypto.c index 3b73fda..07bf30e 100644 --- a/pclsync/pfscrypto.c +++ b/pclsync/pfscrypto.c @@ -221,7 +221,7 @@ int pfscrypto_init_log(psync_openfile_t *of) { return -EIO; } of->logoffset = PSYNC_CRYPTO_SECTOR_SIZE; - psync_fast_hash256_init(&of->loghashctx); + pcrc32c_fast_hash256_init(&of->loghashctx); return 0; } @@ -624,7 +624,7 @@ static int psync_fs_crypto_switch_sectors(psync_openfile_t *of, (unsigned)sizeof(hdr), (int)wrt); return -EIO; } - psync_fast_hash256_update(&of->loghashctx, &hdr, sizeof(hdr)); + pcrc32c_fast_hash256_update(&of->loghashctx, &hdr, sizeof(hdr)); wrt = pfile_pwrite(of->logfile, &autharr[level], sz, of->logoffset + sizeof(hdr)); if (unlikely(wrt != sz)) { @@ -632,7 +632,7 @@ static int psync_fs_crypto_switch_sectors(psync_openfile_t *of, (int)wrt); return -EIO; } - psync_fast_hash256_update(&of->loghashctx, &autharr[level], sz); + pcrc32c_fast_hash256_update(&of->loghashctx, &autharr[level], sz); if (!of->newfile) psync_interval_tree_add(&of->writeintervals, hdr.offset, hdr.offset + sz); @@ -712,7 +712,7 @@ psync_fs_crypto_write_master_auth(psync_openfile_t *of, (unsigned)sizeof(data), (int)wrt); return -EIO; } - psync_fast_hash256_update(&of->loghashctx, &data, sizeof(data)); + pcrc32c_fast_hash256_update(&of->loghashctx, &data, sizeof(data)); if (!of->newfile) psync_interval_tree_add(&of->writeintervals, offsets->masterauthoff, offsets->masterauthoff + PSYNC_CRYPTO_AUTH_SIZE); @@ -770,7 +770,7 @@ psync_fs_crypto_check_log_hash(int lfd, debug(D_WARNING, "invalid hashid in log file %u", (unsigned)mr->hashid); return -1; } - psync_fast_hash256_init(&ctx); + pcrc32c_fast_hash256_init(&ctx); off = PSYNC_CRYPTO_SECTOR_SIZE; while ((rd = pfile_pread(lfd, buff, PSYNC_CRYPTO_SECTOR_SIZE, off)) != 0) { @@ -779,10 +779,10 @@ psync_fs_crypto_check_log_hash(int lfd, (int)errno); return -1; } - psync_fast_hash256_update(&ctx, buff, rd); + pcrc32c_fast_hash256_update(&ctx, buff, rd); off += rd; } - psync_fast_hash256_final(chash, &ctx); + pcrc32c_fast_hash256_final(chash, &ctx); if (memcmp(chash, mr->hash, PSYNC_FAST_HASH256_LEN)) { debug(D_WARNING, "calculated hash does not match"); #if IS_DEBUG @@ -827,12 +827,12 @@ static int psync_fs_crypto_process_log(int lfd, int dfd, return -1; } if (unlikely(mr->crc != - psync_crc32c(PSYNC_CRC_INITIAL, mr, + pcrc32c_compute(PSYNC_CRC_INITIAL, mr, offsetof(psync_crypto_master_record, crc)))) { debug(D_WARNING, "got log file with bad master record CRC, expected %u got %u", (unsigned)mr->crc, - (unsigned)psync_crc32c(PSYNC_CRC_INITIAL, mr, + (unsigned)pcrc32c_compute(PSYNC_CRC_INITIAL, mr, offsetof(psync_crypto_master_record, crc))); return -1; } @@ -953,8 +953,8 @@ static int psync_fs_crypto_mark_log_finalized(psync_openfile_t *of, mr.hashid = PSYNC_LOG_HASHID_FH256; mr.logsize = lsize; mr.filesize = filesize; - psync_fast_hash256_final(&mr.hash, &of->loghashctx); - mr.crc = psync_crc32c(PSYNC_CRC_INITIAL, &mr, + pcrc32c_fast_hash256_final(&mr.hash, &of->loghashctx); + mr.crc = pcrc32c_compute(PSYNC_CRC_INITIAL, &mr, offsetof(psync_crypto_master_record, crc)); wrt = pfile_pwrite(of->logfile, &mr, sizeof(mr), 0); if (unlikely_log(wrt != sizeof(mr))) @@ -1020,7 +1020,7 @@ static int psync_fs_write_interval_tree_to_log(psync_openfile_t *of) { (unsigned)sizeof(logs), (int)wrt); return -EIO; } - psync_fast_hash256_update(&of->loghashctx, logs, sizeof(logs)); + pcrc32c_fast_hash256_update(&of->loghashctx, logs, sizeof(logs)); of->logoffset += sizeof(logs); last = 0; } @@ -1041,7 +1041,7 @@ static int psync_fs_write_interval_tree_to_log(psync_openfile_t *of) { (unsigned)(sizeof(psync_crypto_log_header) * last), (int)wrt); return -EIO; } - psync_fast_hash256_update(&of->loghashctx, logs, last); + pcrc32c_fast_hash256_update(&of->loghashctx, logs, last); of->logoffset += last; } return 0; @@ -1200,7 +1200,7 @@ psync_fs_crypto_write_newfile_full_sector(psync_openfile_t *of, const char *buf, psync_fs_crypto_reset_log_to_off(of, of->logoffset); return -EIO; } - psync_fast_hash256_update(&of->loghashctx, &rec, len); + pcrc32c_fast_hash256_update(&of->loghashctx, &rec, len); psync_fs_crypto_set_sector_log_offset(of, sectorid, of->logoffset, auth); of->logoffset += len; if (!of->newfile) diff --git a/pclsync/ppagecache.c b/pclsync/ppagecache.c index 8483150..20f8a61 100644 --- a/pclsync/ppagecache.c +++ b/pclsync/ppagecache.c @@ -381,7 +381,7 @@ static int psync_pagecache_read_range_from_api(psync_request_t *request, page->lastuse = ptimer_time(); page->size = rb; page->usecnt = 0; - page->crc = psync_crc32c(PSYNC_CRC_INITIAL, page->page, rb); + page->crc = pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, rb); page->type = PAGE_TYPE_READ; h = waiterhash_by_hash_and_pageid(page->hash, page->pageid); lock_wait(page->hash); @@ -848,7 +848,7 @@ static long check_page_in_memory_by_hash(uint64_t hash, uint64_t pageid, page->usecnt++; page->lastuse = tm; } - crc = psync_crc32c(PSYNC_CRC_INITIAL, page->page, page->size); + crc = pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, page->size); if (unlikely(crc != page->crc)) { debug(D_WARNING, "memory page CRC does not match %u!=%u, this is most likely memory " @@ -1677,7 +1677,7 @@ static long check_page_in_database_by_hash(uint64_t hash, ret = -1; } else { if (unlikely(size == dsize && off == 0 && - psync_crc32c(PSYNC_CRC_INITIAL, buff, size) != crc)) { + pcrc32c_compute(PSYNC_CRC_INITIAL, buff, size) != crc)) { debug(D_WARNING, "got bad CRC when reading data from cache at offset %lu", (unsigned long)(pagecacheid * PSYNC_FS_PAGE_SIZE + off)); @@ -1736,7 +1736,7 @@ static void check_pages_in_database_by_hash(uint64_t hash, continue; } for (j = 0; j < cnt; j++) - if (psync_crc32c(PSYNC_CRC_INITIAL, + if (pcrc32c_compute(PSYNC_CRC_INITIAL, buff + (cpid - first_page_id + j) * PSYNC_FS_PAGE_SIZE, PSYNC_FS_PAGE_SIZE) == psync_get_result_cell(fres, i + j, 3)) @@ -1799,7 +1799,7 @@ static long check_page_in_database_by_hash_and_cache(uint64_t hash, psync_pagecache_return_free_page(page); ret = -1; } else { - ccrc = psync_crc32c(PSYNC_CRC_INITIAL, page->page, dsize); + ccrc = pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, dsize); if (unlikely(ccrc != crc)) { debug(D_WARNING, "got bad CRC when reading data from cache at offset %lu, size " @@ -1996,7 +1996,7 @@ static int psync_pagecache_read_range_from_sock(psync_request_t *request, page->lastuse = ptimer_time(); page->size = rb; page->usecnt = 0; - page->crc = psync_crc32c(PSYNC_CRC_INITIAL, page->page, rb); + page->crc = pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, rb); page->type = PAGE_TYPE_READ; h = waiterhash_by_hash_and_pageid(page->hash, page->pageid); lock_wait(page->hash); @@ -3235,7 +3235,7 @@ static void psync_pagecache_new_upload_to_cache(uint64_t taskid, uint64_t hash, page->lastuse = tm; page->size = rd; page->usecnt = 1; - page->crc = psync_crc32c(PSYNC_CRC_INITIAL, page->page, rd); + page->crc = pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, rd); page->type = PAGE_TYPE_READ; psync_pagecache_add_page_if_not_exists(page, hash, pageid); if (rd < PSYNC_FS_PAGE_SIZE) @@ -3354,7 +3354,7 @@ static void psync_pagecache_modify_to_cache(uint64_t taskid, uint64_t hash, page->lastuse = tm; page->size = pdb; page->usecnt = 1; - page->crc = psync_crc32c(PSYNC_CRC_INITIAL, page->page, pdb); + page->crc = pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, pdb); page->type = PAGE_TYPE_READ; psync_pagecache_add_page_if_not_exists(page, hash, pageid); break; @@ -3386,7 +3386,7 @@ static void psync_pagecache_modify_to_cache(uint64_t taskid, uint64_t hash, page->lastuse = tm; page->size = rd; page->usecnt = 1; - page->crc = psync_crc32c(PSYNC_CRC_INITIAL, page->page, rd); + page->crc = pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, rd); page->type = PAGE_TYPE_READ; // debug(D_NOTICE, "new page %lu crc %lu size %lu", (unsigned // long)pageid, (unsigned long)page->crc, (unsigned long)rd); @@ -3461,7 +3461,7 @@ static void psync_pagecache_modify_to_cache(uint64_t taskid, uint64_t hash, page->lastuse = tm; page->size = pdb; page->usecnt = 1; - page->crc = psync_crc32c(PSYNC_CRC_INITIAL, page->page, pdb); + page->crc = pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, pdb); page->type = PAGE_TYPE_READ; // debug(D_NOTICE, "combined page %lu crc %lu size %lu", (unsigned // long)pageid, (unsigned long)page->crc, (unsigned long)pdb); @@ -3723,13 +3723,13 @@ static int psync_pagecache_free_page_from_read_cache() { page->usecnt = row[4]; page->crc = row[6]; psync_sql_free_result(res); - if (unlikely(psync_crc32c(PSYNC_CRC_INITIAL, page->page, page->size) != + if (unlikely(pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, page->size) != page->crc)) { debug( D_WARNING, "page CRC check failed, dropping page, db CRC %u calculated CRC %u", (unsigned)page->crc, - (unsigned)psync_crc32c(PSYNC_CRC_INITIAL, page->page, page->size)); + (unsigned)pcrc32c_compute(PSYNC_CRC_INITIAL, page->page, page->size)); psync_pagecache_return_free_page(page); } else { page->type = PAGE_TYPE_READ;