]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/database.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / database.c
index 106cedec4b91ded814d88e2d43801eb55cb27939..617b61eb5a8fe5966136aa54c7cc690579fa2bba 100644 (file)
@@ -52,7 +52,6 @@
 
 struct loc_database_objects {
        char* data;
-       off_t offset;
        size_t length;
        size_t count;
 };
@@ -274,12 +273,10 @@ static int loc_database_mmap(struct loc_database* db) {
 /*
        Maps arbitrary objects from the database into memory.
 */
-static int loc_database_map_objects(struct loc_database* db,
-               struct loc_database_objects* objects, const size_t size,
-               off_t offset, size_t length) {
+static int loc_database_map_objects(struct loc_database* db, struct loc_database_objects* objects,
+               const size_t size, const off_t offset, const size_t length) {
        // Store parameters
        objects->data   = db->data + offset;
-       objects->offset = offset;
        objects->length = length;
        objects->count  = objects->length / size;
 
@@ -483,7 +480,7 @@ static void loc_database_free(struct loc_database* db) {
 
 LOC_EXPORT int loc_database_new(struct loc_ctx* ctx, struct loc_database** database, FILE* f) {
        struct loc_database* db = NULL;
-       int r;
+       int r = 1;
 
        // Fail on invalid file handle
        if (!f) {
@@ -644,8 +641,11 @@ LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
                }
        }
 
+       int sig1_valid = 0;
+       int sig2_valid = 0;
+
        // Check first signature
-       if (db->signature1.data) {
+       if (db->signature1.length) {
                hexdump(db->ctx, db->signature1.data, db->signature1.length);
 
                r = EVP_DigestVerifyFinal(mdctx,
@@ -653,19 +653,19 @@ LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
 
                if (r == 0) {
                        DEBUG(db->ctx, "The first signature is invalid\n");
-                       r = 1;
                } else if (r == 1) {
                        DEBUG(db->ctx, "The first signature is valid\n");
-                       r = 0;
+                       sig1_valid = 1;
                } else {
                        ERROR(db->ctx, "Error verifying the first signature: %s\n",
                                ERR_error_string(ERR_get_error(), NULL));
                        r = -1;
+                       goto CLEANUP;
                }
        }
 
        // Check second signature only when the first one was invalid
-       if (r && db->signature2.data) {
+       if (db->signature2.length) {
                hexdump(db->ctx, db->signature2.data, db->signature2.length);
 
                r = EVP_DigestVerifyFinal(mdctx,
@@ -673,14 +673,14 @@ LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
 
                if (r == 0) {
                        DEBUG(db->ctx, "The second signature is invalid\n");
-                       r = 1;
                } else if (r == 1) {
                        DEBUG(db->ctx, "The second signature is valid\n");
-                       r = 0;
+                       sig2_valid = 1;
                } else {
                        ERROR(db->ctx, "Error verifying the second signature: %s\n",
                                ERR_error_string(ERR_get_error(), NULL));
                        r = -1;
+                       goto CLEANUP;
                }
        }
 
@@ -688,6 +688,12 @@ LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
        INFO(db->ctx, "Signature checked in %.4fms\n",
                (double)(end - start) / CLOCKS_PER_SEC * 1000);
 
+       // Check if at least one signature as okay
+       if (sig1_valid || sig2_valid)
+               r = 0;
+       else
+               r = 1;
+
 CLEANUP:
        // Cleanup
        EVP_MD_CTX_free(mdctx);
@@ -1107,7 +1113,6 @@ LOC_EXPORT int loc_database_enumerator_new(struct loc_database_enumerator** enum
        // Initialise graph search
        e->network_stack_depth = 1;
        e->networks_visited = calloc(db->network_node_objects.count, sizeof(*e->networks_visited));
-       printf("COUNT = %zu, P = %p\n", db->network_node_objects.count, e->networks_visited);
        if (!e->networks_visited) {
                ERROR(db->ctx, "Could not allocated visited networks: %m\n");
                r = 1;