Change spamsum API.

This commit is contained in:
2021-09-22 17:04:03 +01:00
parent 7553a3ce40
commit 637759931c
2 changed files with 8 additions and 9 deletions

View File

@@ -16,11 +16,11 @@
* License along with this library; if not, see <http://www.gnu.org/licenses/>. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/ */
#include <assert.h> #include <assert.h>
#include <errno.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include "library.h" #include "library.h"
#include "spamsum.h" #include "spamsum.h"
@@ -49,7 +49,7 @@ AARU_EXPORT int AARU_CALL spamsum_update(spamsum_ctx* ctx, const uint8_t* data,
int i; int i;
if(!ctx || !data) return -1; if(!ctx || !data) return -1;
for( i = 0; i < len; i++) fuzzy_engine_step(ctx, data[i]); for(i = 0; i < len; i++) fuzzy_engine_step(ctx, data[i]);
ctx->total_size += len; ctx->total_size += len;
@@ -172,14 +172,13 @@ AARU_LOCAL void fuzzy_try_fork_blockhash(spamsum_ctx* ctx)
++ctx->bh_end; ++ctx->bh_end;
} }
AARU_EXPORT uint8_t* AARU_CALL spamsum_final(spamsum_ctx* ctx) AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result)
{ {
uint32_t bi = ctx->bh_start; uint32_t bi = ctx->bh_start;
uint32_t h = ROLL_SUM(ctx); uint32_t h = ROLL_SUM(ctx);
int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */ int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */
uint8_t* result = (uint8_t*)malloc(FUZZY_MAX_RESULT);
if(!result) return NULL; if(!result) return -1;
/* Verify that our elimination was not overeager. */ /* 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);
@@ -192,7 +191,7 @@ AARU_EXPORT uint8_t* AARU_CALL spamsum_final(spamsum_ctx* ctx)
if(bi >= NUM_BLOCKHASHES) if(bi >= NUM_BLOCKHASHES)
{ {
errno = EOVERFLOW; errno = EOVERFLOW;
return NULL; return -1;
} }
} }
@@ -206,7 +205,7 @@ AARU_EXPORT uint8_t* AARU_CALL spamsum_final(spamsum_ctx* ctx)
int i = snprintf((char*)result, (size_t)remain, "%lu:", (unsigned long)SSDEEP_BS(bi)); int i = snprintf((char*)result, (size_t)remain, "%lu:", (unsigned long)SSDEEP_BS(bi));
if(i <= 0) /* Maybe snprintf has set errno here? */ if(i <= 0) /* Maybe snprintf has set errno here? */
return NULL; return -1;
assert(i < remain); assert(i < remain);
@@ -308,5 +307,5 @@ AARU_EXPORT uint8_t* AARU_CALL spamsum_final(spamsum_ctx* ctx)
*result = 0; *result = 0;
return result; return 0;
} }

View File

@@ -53,7 +53,7 @@ typedef struct
AARU_EXPORT spamsum_ctx* AARU_CALL spamsum_init(void); AARU_EXPORT spamsum_ctx* AARU_CALL spamsum_init(void);
AARU_EXPORT int AARU_CALL spamsum_update(spamsum_ctx* ctx, const uint8_t* data, uint32_t len); AARU_EXPORT int AARU_CALL spamsum_update(spamsum_ctx* ctx, const uint8_t* data, uint32_t len);
AARU_EXPORT uint8_t* AARU_CALL spamsum_final(spamsum_ctx* ctx); AARU_EXPORT int AARU_CALL spamsum_final(spamsum_ctx* ctx, uint8_t* result);
AARU_EXPORT void AARU_CALL spamsum_free(spamsum_ctx* ctx); AARU_EXPORT void AARU_CALL spamsum_free(spamsum_ctx* ctx);
AARU_LOCAL void fuzzy_engine_step(spamsum_ctx* ctx, uint8_t c); AARU_LOCAL void fuzzy_engine_step(spamsum_ctx* ctx, uint8_t c);