Skip to content

Commit

Permalink
Remove padding zeros in file output.
Browse files Browse the repository at this point in the history
  • Loading branch information
diagprov committed Jan 7, 2020
1 parent 02c4094 commit 2a24a24
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 46 deletions.
29 changes: 0 additions & 29 deletions src/e4c_pk_store_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ int e4c_load(e4storage *store, const char *path)
return E4_ERROR_PERSISTENCE_ERROR;
}

/*size_t filesize = */ lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);

memset(mbuf, 0, sizeof mbuf);
rlen = read(fd, mbuf, sizeof E4V2_MAGIC);
if (rlen != sizeof E4V2_MAGIC)
Expand All @@ -86,15 +83,12 @@ int e4c_load(e4storage *store, const char *path)
goto err;
}

lseek(fd, 4, SEEK_CUR);

rlen = read(fd, store->id, sizeof store->id);
if (rlen != sizeof store->id)
{
goto err;
}

lseek(fd, 4, SEEK_CUR);

r = e4c_derive_control_topic(controltopic, E4_CTRLTOPIC_LEN + 1, store->id);
if (r != 0)
Expand All @@ -111,29 +105,24 @@ int e4c_load(e4storage *store, const char *path)
{
goto err;
}
lseek(fd, 4, SEEK_CUR);

rlen = read(fd, store->pubkey, sizeof store->pubkey);
if (rlen != sizeof store->pubkey)
{
goto err;
}
lseek(fd, 4, SEEK_CUR);

rlen = read(fd, store->c2key, sizeof store->c2key);
if (rlen != sizeof store->c2key)
{
goto err;
}
lseek(fd, 4, SEEK_CUR);

rlen = read(fd, &store->topiccount, sizeof store->topiccount);
if (rlen != sizeof store->topiccount)
{
goto err;
}

lseek(fd, 4, SEEK_CUR);
/* TODO: detect if we cannot read everything based on the topiccount */


Expand All @@ -145,21 +134,18 @@ int e4c_load(e4storage *store, const char *path)
goto err;
}

lseek(fd, 4, SEEK_CUR);
rlen = read(fd, store->topics[i].key, E4_KEY_LEN);
if (rlen != E4_KEY_LEN)
{
goto err;
}
lseek(fd, 4, SEEK_CUR);
}

rlen = read(fd, &store->devicecount, sizeof store->devicecount);
if (rlen != sizeof store->devicecount)
{
goto err;
}
lseek(fd, 4, SEEK_CUR);

for (i = 0; i < store->devicecount; i++)
{
Expand All @@ -169,13 +155,11 @@ int e4c_load(e4storage *store, const char *path)
goto err;
}

lseek(fd, 4, SEEK_CUR);
rlen = read(fd, store->devices[i].pubkey, E4_PK_EDDSA_PUBKEY_LEN);
if (rlen != E4_PK_EDDSA_PUBKEY_LEN)
{
goto err;
}
lseek(fd, 4, SEEK_CUR);
}

e4c_debug_print(store);
Expand Down Expand Up @@ -205,41 +189,28 @@ int e4c_sync(e4storage *store)
return E4_ERROR_PERSISTENCE_ERROR;
}

uint32_t zero = 0;

write(fd, E4V2_MAGIC, sizeof E4V2_MAGIC);
write(fd, &zero, sizeof zero);
write(fd, store->id, sizeof store->id);
write(fd, &zero, sizeof zero);
write(fd, store->privkey, sizeof store->privkey);
write(fd, &zero, sizeof zero);
write(fd, store->pubkey, sizeof store->pubkey);
write(fd, &zero, sizeof zero);
write(fd, store->c2key, sizeof store->c2key);
write(fd, &zero, sizeof zero);
write(fd, &store->topiccount, sizeof store->topiccount);
write(fd, &zero, sizeof zero);

for (i = 0; i < store->topiccount; i++)
{
topic_key *t = &(store->topics[0]) + i;

write(fd, t->topic, sizeof t->topic);
write(fd, &zero, sizeof zero);
write(fd, t->key, sizeof t->key);
write(fd, &zero, sizeof zero);
}
write(fd, &store->devicecount, sizeof store->devicecount);
write(fd, &zero, sizeof zero);

for (i = 0; i < store->devicecount; i++)
{
device_key *d = &(store->devices[0]) + i;

write(fd, d->id, sizeof d->id);
write(fd, &zero, sizeof zero);
write(fd, d->pubkey, sizeof d->pubkey);
write(fd, &zero, sizeof zero);
}
close(fd);

Expand Down
17 changes: 0 additions & 17 deletions src/e4c_store_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ int e4c_load(e4storage *store, const char *path)
return E4_ERROR_PERSISTENCE_ERROR;
}

/*size_t filesize = */ lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);

memset(mbuf, 0, sizeof mbuf);
rlen = read(fd, mbuf, sizeof E4V1_MAGIC);
if (rlen != sizeof E4V1_MAGIC)
Expand All @@ -86,15 +83,13 @@ int e4c_load(e4storage *store, const char *path)
goto err;
}

lseek(fd, 4, SEEK_CUR);

rlen = read(fd, store->id, sizeof store->id);
if (rlen != sizeof store->id)
{
goto err;
}

lseek(fd, 4, SEEK_CUR);

r = e4c_derive_control_topic(controltopic, E4_CTRLTOPIC_LEN + 1, store->id);
if (r != 0)
Expand All @@ -111,15 +106,13 @@ int e4c_load(e4storage *store, const char *path)
goto err;
}

lseek(fd, 4, SEEK_CUR);

rlen = read(fd, &store->topiccount, sizeof store->topiccount);
if (rlen != sizeof store->topiccount)
{
goto err;
}

lseek(fd, 4, SEEK_CUR);
/* TODO: detect if we cannot read everything based on the topiccount */


Expand All @@ -131,13 +124,11 @@ int e4c_load(e4storage *store, const char *path)
goto err;
}

lseek(fd, 4, SEEK_CUR);
rlen = read(fd, store->topics[i].key, E4_KEY_LEN);
if (rlen != E4_KEY_LEN)
{
goto err;
}
lseek(fd, 4, SEEK_CUR);
}

e4c_debug_print(store);
Expand Down Expand Up @@ -167,25 +158,17 @@ int e4c_sync(e4storage *store)
return E4_ERROR_PERSISTENCE_ERROR;
}

uint32_t zero = 0;

write(fd, E4V1_MAGIC, sizeof E4V1_MAGIC);
write(fd, &zero, sizeof zero);
write(fd, store->id, sizeof store->id);
write(fd, &zero, sizeof zero);
write(fd, store->key, sizeof store->key);
write(fd, &zero, sizeof zero);
write(fd, &store->topiccount, sizeof store->topiccount);
write(fd, &zero, sizeof zero);

for (i = 0; i < store->topiccount; i++)
{
topic_key *t = &(store->topics[0]) + i;

write(fd, t->topic, sizeof t->topic);
write(fd, &zero, sizeof zero);
write(fd, t->key, sizeof t->key);
write(fd, &zero, sizeof zero);
}
close(fd);

Expand Down

0 comments on commit 2a24a24

Please sign in to comment.