11 #include <drift/dslcore.h>
12 #include <drift/GenLib.h>
13 #include <drift/hash.h>
14 #include <drift/Mutex.h>
16 typedef vector<const HASH_PROVIDER*> hashProviderList;
17 hashProviderList * dslHashProviders() {
18 AutoMutexPtr(dslMutex());
19 static hashProviderList hash_providers { &dsl_native_hashers };
20 return &hash_providers;
23 extern DSL_Mutex * dslMutex();
25 AutoMutexPtr(dslMutex());
28 hashProviderList* hash_providers = dslHashProviders();
29 hash_providers->insert(hash_providers->begin(), p);
31 void DSL_CC dsl_remove_hash_provider(
const HASH_PROVIDER * p) {
32 AutoMutexPtr(dslMutex());
33 hashProviderList * hash_providers = dslHashProviders();
34 for (
auto x = hash_providers->begin(); x != hash_providers->end(); x++) {
36 hash_providers->erase(x);
38 printf(
"Removed hash provider %s -> %zu\n", p->name, hash_providers->size());
44 void DSL_CC dsl_get_hash_providers(vector<const HASH_PROVIDER *>& p) {
45 AutoMutexPtr(dslMutex());
46 p = *dslHashProviders();
51 hashProviderList* hash_providers = dslHashProviders();
52 for (
auto x = hash_providers->begin(); x != hash_providers->end(); x++) {
53 HASH_CTX * ret = (*x)->hash_init(name);
63 if (ctx == NULL || data == NULL) {
66 ctx->provider->hash_update(ctx, data, len);
69 if (ctx == NULL || out == NULL) {
72 if (outlen < ctx->hashSize) {
73 printf(
"DSL: Output buffer too small in hash_finish()!\n");
74 uint8 * tmp = (uint8 *)dsl_malloc(ctx->hashSize);
75 bool ret = ctx->provider->hash_finish(ctx, tmp, ctx->hashSize);
76 memcpy(out, tmp, outlen);
80 return ctx->provider->hash_finish(ctx, out, outlen);
83 DSL_API
bool DSL_CC
hashdata(
const char * name,
const uint8 *data,
size_t datalen,
char * out,
size_t outlen,
bool raw_output) {
88 if (raw_output && outlen < ctx->hashSize) {
90 }
else if (!raw_output && outlen < ((ctx->hashSize * 2) + 1)) {
100 uint8 * hashtmp = (uint8 *)dsl_malloc(ctx->hashSize);
101 size_t hsize = ctx->hashSize;
104 ret = (
bin2hex(hashtmp, hsize, out, outlen) != NULL);
111 DSL_API
bool DSL_CC
hashfile(
const char * name,
const char * fn,
char * out,
size_t outlen,
bool raw_output) {
114 bool ret =
hashfile_rw(name, fp, out, outlen, raw_output);
121 DSL_API
bool DSL_CC
hashfile_fp(
const char * name, FILE * fpp,
char * out,
size_t outlen,
bool raw_output) {
122 if (fpp == NULL) {
return false; }
126 bool ret =
hashfile_rw(name, fp, out, outlen, raw_output);
133 DSL_API
bool DSL_CC
hashfile_rw(
const char * name,
DSL_FILE * fp,
char * out,
size_t outlen,
bool raw_output) {
138 if (raw_output && outlen < ctx->hashSize) {
140 }
else if (!raw_output && outlen < ((ctx->hashSize * 2) + 1)) {
146 fp->
seek(fp, 0, SEEK_END);
147 int64 left = fp->
tell(fp);
148 fp->
seek(fp, 0, SEEK_SET);
152 int64 toRead = (left >=
sizeof(buf)) ?
sizeof(buf):left;
153 if (fp->
read(buf, toRead, fp) == toRead) {
166 size_t hsize = ctx->hashSize;
167 unsigned char * hashtmp = (
unsigned char *)dsl_malloc(ctx->hashSize);
170 ret2 = (
bin2hex(hashtmp, hsize, out, outlen) != NULL);
174 return (ret && ret2);
DSL_API void DSL_CC dsl_free(void *ptr)
DSL_API_CLASS char *DSL_CC bin2hex(const uint8_t *data, size_t datalen, char *out, size_t outsize)
DSL_API bool DSL_CC hashdata(const char *name, const uint8 *data, size_t datalen, char *out, size_t outlen, bool raw_output)
Wrapper around hash_init()/hash_update()/hash_finish(). If raw_output == true out will contain binary...
DSL_API bool DSL_CC hashfile_rw(const char *name, DSL_FILE *fp, char *out, size_t outlen, bool raw_output)
Wrapper around hash_init()/hash_update()/hash_finish(). If raw_output == true out will contain binary...
void DSL_CC hash_update(HASH_CTX *ctx, const uint8 *data, size_t len)
Call with the data you want to hash, can be called multiple times to hash a large file in chunks for ...
DSL_API bool DSL_CC hashfile(const char *name, const char *fn, char *out, size_t outlen, bool raw_output)
Wrapper around hash_init()/hash_update()/hash_finish(). If raw_output == true out will contain binary...
HASH_CTX *DSL_CC hash_init(const char *name)
DSL_API bool DSL_CC hashfile_fp(const char *name, FILE *fpp, char *out, size_t outlen, bool raw_output)
Wrapper around hash_init()/hash_update()/hash_finish(). If raw_output == true out will contain binary...
bool DSL_CC hash_finish(HASH_CTX *ctx, uint8 *out, size_t outlen)
Finalize hash and store in out. outlen should be >= hashSize in the HASH_CTX struct....
DSL_API DSL_FILE *DSL_CC RW_ConvertFile(FILE *fp, bool autoclose)
DSL_API DSL_FILE *DSL_CC RW_OpenFile(const char *fn, const char *mode)
void(* close)(DSL_FILE *fp)
bool(* seek)(DSL_FILE *fp, int64 pos, int mode)
int64(* read)(void *buf, int64 size, DSL_FILE *fp)
int64(* tell)(DSL_FILE *fp)