aboutsummaryrefslogtreecommitdiffstats
path: root/append.c
diff options
context:
space:
mode:
authorCameron Katri <me@cameronkatri.com>2022-05-21 23:44:49 -0400
committerCameron Katri <me@cameronkatri.com>2022-05-21 23:44:49 -0400
commit2288b178e612386e7a75471c8861c3f6d81c300d (patch)
tree18209f47e0bc635000f7bf86b83f43a9ab0aa3ea /append.c
parentaa035f73ce081b3f07247bd15860d72355a096b2 (diff)
downloadtrustcache-2288b178e612386e7a75471c8861c3f6d81c300d.tar.gz
trustcache-2288b178e612386e7a75471c8861c3f6d81c300d.zip
Add tc remove and append -f flags
Also fix some conflicting types to hopefully prevent possible overflows.
Diffstat (limited to 'append.c')
-rw-r--r--append.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/append.c b/append.c
index 475200e..a5cd031 100644
--- a/append.c
+++ b/append.c
@@ -28,6 +28,7 @@
#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -38,14 +39,13 @@
int
tcappend(int argc, char **argv)
{
- if (argc < 3)
- return -1;
-
int keepuuid = 0;
uuid_t uuid;
+ const char *errstr = NULL;
+ uint8_t flags = 0;
int ch;
- while ((ch = getopt(argc, argv, "u:")) != -1) {
+ while ((ch = getopt(argc, argv, "u:f:")) != -1) {
switch (ch) {
case 'u':
if (strlen(optarg) == 1 && *optarg == '0') {
@@ -57,12 +57,22 @@ tcappend(int argc, char **argv)
keepuuid = 2;
}
break;
+ case 'f':
+ flags = strtonum(optarg, 0, UINT8_MAX, &errstr);
+ if (errstr != NULL) {
+ fprintf(stderr, "flag number is %s: %s\n", errstr, optarg);
+ exit(1);
+ }
+ break;
}
}
argc -= optind;
argv += optind;
+ if (argc < 2)
+ return -1;
+
FILE *f = NULL;
struct trust_cache cache = opentrustcache(argv[0]);
struct trust_cache append = {
@@ -76,16 +86,16 @@ tcappend(int argc, char **argv)
if ((cache.hashes = realloc(cache.hashes, sizeof(trust_cache_hash0) *
(cache.num_entries + append.num_entries))) == NULL)
exit(1);
- for (int j = 0; j < append.num_entries; j++) {
+ for (uint32_t j = 0; j < append.num_entries; j++) {
memcpy(cache.hashes[cache.num_entries + j], append.hashes[j], CS_CDHASH_LEN);
}
} else if (append.version == 1) {
if ((cache.entries = realloc(cache.entries, sizeof(struct trust_cache_entry1) *
(cache.num_entries + append.num_entries))) == NULL)
exit(1);
- for (int j = 0; j < append.num_entries; j++) {
+ for (uint32_t j = 0; j < append.num_entries; j++) {
cache.entries[cache.num_entries + j].hash_type = append.entries[j].hash_type;
- cache.entries[cache.num_entries + j].flags = append.entries[j].flags;
+ cache.entries[cache.num_entries + j].flags = flags != 0 ? flags : append.entries[j].flags;
memcpy(cache.entries[cache.num_entries + j].cdhash, append.entries[j].cdhash, CS_CDHASH_LEN);
}
}
@@ -120,7 +130,7 @@ tcappend(int argc, char **argv)
cache.version = le32toh(cache.version);
cache.num_entries = le32toh(cache.num_entries);
- for (int i = 0; i < cache.num_entries; i++) {
+ for (uint32_t i = 0; i < cache.num_entries; i++) {
if (cache.version == 0)
fwrite(&cache.hashes[i], sizeof(trust_cache_hash0), 1, f);
else if (cache.version == 1)