Change CRC algorithms to use slicing.

This commit is contained in:
2021-09-22 20:09:52 +01:00
parent 25fcc7f474
commit 886d613f6e
9 changed files with 920 additions and 151 deletions

View File

@@ -65,7 +65,7 @@ AARU_EXPORT void AARU_CALL spamsum_free(spamsum_ctx* ctx)
#define SUM_HASH(c, h) (((h)*HASH_PRIME) ^ (c));
#define SSDEEP_BS(index) (MIN_BLOCKSIZE << (index))
AARU_LOCAL void fuzzy_engine_step(spamsum_ctx* ctx, uint8_t c)
AARU_LOCAL inline void fuzzy_engine_step(spamsum_ctx* ctx, uint8_t c)
{
uint32_t i;
/* At each character we update the rolling hash and the normal hashes.
@@ -118,7 +118,7 @@ AARU_LOCAL void fuzzy_engine_step(spamsum_ctx* ctx, uint8_t c)
}
}
AARU_LOCAL void roll_hash(spamsum_ctx* ctx, uint8_t c)
AARU_LOCAL inline void roll_hash(spamsum_ctx* ctx, uint8_t c)
{
ctx->roll.h2 -= ctx->roll.h1;
ctx->roll.h2 += ROLLING_WINDOW * c;
@@ -136,9 +136,9 @@ AARU_LOCAL void roll_hash(spamsum_ctx* ctx, uint8_t c)
ctx->roll.h3 ^= c;
}
AARU_LOCAL void fuzzy_try_reduce_blockhash(spamsum_ctx* ctx)
AARU_LOCAL inline void fuzzy_try_reduce_blockhash(spamsum_ctx* ctx)
{
assert(ctx->bh_start < ctx->bh_end);
//assert(ctx->bh_start < ctx->bh_end);
if(ctx->bh_end - ctx->bh_start < 2) /* Need at least two working hashes. */
return;
@@ -156,11 +156,11 @@ AARU_LOCAL void fuzzy_try_reduce_blockhash(spamsum_ctx* ctx)
++ctx->bh_start;
}
AARU_LOCAL void fuzzy_try_fork_blockhash(spamsum_ctx* ctx)
AARU_LOCAL inline void fuzzy_try_fork_blockhash(spamsum_ctx* ctx)
{
if(ctx->bh_end >= NUM_BLOCKHASHES) return;
assert(ctx->bh_end != 0);
//assert(ctx->bh_end != 0);
uint32_t obh = ctx->bh_end - 1;
uint32_t nbh = ctx->bh_end;
@@ -181,7 +181,7 @@ AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result)
if(!result) return -1;
/* Verify that our elimination was not overeager. */
assert(bi == 0 || (uint64_t)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < ctx->total_size);
//assert(bi == 0 || (uint64_t)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < ctx->total_size);
/* Initial blocksize guess. */
while((uint64_t)SSDEEP_BS(bi) * SPAMSUM_LENGTH < ctx->total_size)
@@ -200,21 +200,21 @@ AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result)
while(bi > ctx->bh_start && ctx->bh[bi].d_len < SPAMSUM_LENGTH / 2) --bi;
assert(!(bi > 0 && ctx->bh[bi].d_len < SPAMSUM_LENGTH / 2));
//assert(!(bi > 0 && ctx->bh[bi].d_len < SPAMSUM_LENGTH / 2));
int i = snprintf((char*)result, (size_t)remain, "%lu:", (unsigned long)SSDEEP_BS(bi));
if(i <= 0) /* Maybe snprintf has set errno here? */
return -1;
assert(i < remain);
//assert(i < remain);
remain -= i;
result += i;
i = (int)ctx->bh[bi].d_len;
assert(i <= remain);
//assert(i <= remain);
memcpy(result, ctx->bh[bi].digest, (size_t)i);
result += i;
@@ -222,7 +222,7 @@ AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result)
if(h != 0)
{
assert(remain > 0);
//assert(remain > 0);
*result = b64[ctx->bh[bi].h % 64];
@@ -234,7 +234,7 @@ AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result)
}
else if(ctx->bh[bi].digest[i] != 0)
{
assert(remain > 0);
//assert(remain > 0);
*result = ctx->bh[bi].digest[i];
@@ -245,7 +245,7 @@ AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result)
}
}
assert(remain > 0);
//assert(remain > 0);
*result++ = ':';
--remain;
@@ -264,7 +264,7 @@ AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result)
if(h != 0)
{
assert(remain > 0);
//assert(remain > 0);
h = ctx->bh[bi].half_h;
*result = b64[h % 64];
@@ -281,7 +281,7 @@ AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result)
if(i != 0)
{
assert(remain > 0);
//assert(remain > 0);
*result = (uint8_t)i;
@@ -295,9 +295,9 @@ AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result)
}
else if(h != 0)
{
assert(ctx->bh[bi].d_len == 0);
//assert(ctx->bh[bi].d_len == 0);
assert(remain > 0);
//assert(remain > 0);
*result++ = b64[ctx->bh[bi].h % 64];
/* No need to bother with FUZZY_FLAG_ELIMSEQ, because this