summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-04-05 21:33:27 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-04-12 18:19:07 +0300
commitb024ca101ec0c274595407158edad945bec7868d (patch)
treea9bed365a498d9e508e5760cb54d2eb5b29b5cba
parentd5953318cac32355b6115086f2b9e4ac9d71cf57 (diff)
downloadsubsurface-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>
-rw-r--r--core/divesite.c26
-rw-r--r--dives/TestDiveDivelogsDE.xml4
2 files changed, 22 insertions, 8 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);
diff --git a/dives/TestDiveDivelogsDE.xml b/dives/TestDiveDivelogsDE.xml
index 38045119d..fe56f858f 100644
--- a/dives/TestDiveDivelogsDE.xml
+++ b/dives/TestDiveDivelogsDE.xml
@@ -4,11 +4,11 @@
<divecomputerid model='Vyper Air' deviceid='ffffffff' serial='20400612'/>
</settings>
<divesites>
-<site uuid='8c1836f0' name='USA, Hoodsport, WA / Sund Rock' gps='47.400269 -123.142632'>
+<site uuid='64785a00' name='USA, Hoodsport, WA / Sund Rock' gps='47.400269 -123.142632'>
</site>
</divesites>
<dives>
-<dive number='1' divesiteid='8c1836f0' date='2011-05-17' time='11:01:00' duration='32:00 min'>
+<dive number='1' divesiteid='64785a00' date='2011-05-17' time='11:01:00' duration='32:00 min'>
<buddy>Linus</buddy>
<notes>Second deep dive
Linus and I planned and executed - Alex just watching