diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-04-05 21:33:27 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-04-12 18:19:07 +0300 |
commit | b024ca101ec0c274595407158edad945bec7868d (patch) | |
tree | a9bed365a498d9e508e5760cb54d2eb5b29b5cba /core/divesite.c | |
parent | d5953318cac32355b6115086f2b9e4ac9d71cf57 (diff) | |
download | subsurface-b024ca101ec0c274595407158edad945bec7868d.tar.gz |
Dive site: make UUID generation deterministic
Instead of using a random UUID, use an SHA1 hash of name, description
and notes (if defined). This is necessary for testing.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/divesite.c')
-rw-r--r-- | core/divesite.c | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/core/divesite.c b/core/divesite.c index 8757aeed7..85ce8da37 100644 --- a/core/divesite.c +++ b/core/divesite.c @@ -6,6 +6,7 @@ #include "divelist.h" #include "membuffer.h" #include "table.h" +#include "sha1.h" #include <math.h> @@ -129,14 +130,27 @@ static MAKE_REMOVE(dive_site_table, struct dive_site *, dive_site) int add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_table) { + /* If the site doesn't yet have an UUID, create a new one. + * Make this deterministic for testing. */ + if (!ds->uuid) { + SHA_CTX ctx; + uint32_t csum[5]; + + SHA1_Init(&ctx); + if (ds->name) + SHA1_Update(&ctx, ds->name, strlen(ds->name)); + if (ds->description) + SHA1_Update(&ctx, ds->description, strlen(ds->description)); + if (ds->notes) + SHA1_Update(&ctx, ds->notes, strlen(ds->notes)); + SHA1_Final((unsigned char *)csum, &ctx); + ds->uuid = csum[0]; + } + /* Take care to never have the same uuid twice. This could happen on * reimport of a log where the dive sites have diverged */ - while (ds->uuid == 0 || get_dive_site_by_uuid(ds->uuid, ds_table) != NULL) { - ds->uuid = rand() & 0xff; - ds->uuid |= (rand() & 0xff) << 8; - ds->uuid |= (rand() & 0xff) << 16; - ds->uuid |= (rand() & 0xff) << 24; - } + while (ds->uuid == 0 || get_dive_site_by_uuid(ds->uuid, ds_table) != NULL) + ++ds->uuid; int idx = dive_site_table_get_insertion_index(ds_table, ds); add_to_dive_site_table(ds_table, idx, ds); |