]>
git.cameronkatri.com Git - trustcache.git/blob - append.c
2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2022 Cameron Katri. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY CAMERON KATRI AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL CAMERON KATRI OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "trustcache.h"
38 #include "uuid/uuid.h"
43 ishexstring(const char *s
) {
44 for (; *s
!= '\0'; s
++)
51 tcappend(int argc
, char **argv
)
55 const char *errstr
= NULL
;
57 uint16_t category
= 0;
60 while ((ch
= getopt(argc
, argv
, "u:f:c:")) != -1) {
63 if (strlen(optarg
) == 1 && *optarg
== '0') {
66 if (uuid_parse(optarg
, uuid
) != 0) {
67 fprintf(stderr
, "Failed to parse %s as a UUID\n", optarg
);
73 flags
= strtonum(optarg
, 0, UINT8_MAX
, &errstr
);
75 fprintf(stderr
, "flag number is %s: %s\n", errstr
, optarg
);
80 category
= strtonum(optarg
, 0, UINT16_MAX
, &errstr
);
82 fprintf(stderr
, "category number is %s: %s\n", errstr
, optarg
);
96 struct trust_cache cache
= opentrustcache(argv
[0]);
97 struct trust_cache append
= {
98 .version
= cache
.version
,
102 for (int i
= 1; i
< argc
; i
++) {
103 if (strlen(argv
[i
]) == 40 && ishexstring(argv
[i
])) {
104 append
.num_entries
= 1;
105 if (append
.version
== 0) {
106 append
.hashes
= calloc(1, sizeof(trust_cache_hash0
));
107 for (size_t j
= 0; j
< CS_CDHASH_LEN
; j
++)
108 sscanf(argv
[i
] + 2 * j
, "%02hhx", &append
.hashes
[0][j
]);
109 } else if (append
.version
== 1) {
110 append
.entries
= calloc(1, sizeof(struct trust_cache_entry1
));
111 for (size_t j
= 0; j
< CS_CDHASH_LEN
; j
++)
112 sscanf(argv
[i
] + 2 * j
, "%02hhx", &append
.entries
[0].cdhash
[j
]);
113 } else if (append
.version
== 2) {
114 append
.entries2
= calloc(1, sizeof(struct trust_cache_entry2
));
115 for (size_t j
= 0; j
< CS_CDHASH_LEN
; j
++)
116 sscanf(argv
[i
] + 2 * j
, "%02hhx", &append
.entries2
[0].cdhash
[j
]);
119 append
= cache_from_tree(argv
[i
], cache
.version
);
121 if (append
.version
== 0) {
122 if ((cache
.hashes
= realloc(cache
.hashes
, sizeof(trust_cache_hash0
) *
123 (cache
.num_entries
+ append
.num_entries
))) == NULL
)
125 for (uint32_t j
= 0; j
< append
.num_entries
; j
++) {
126 memcpy(cache
.hashes
[cache
.num_entries
+ j
], append
.hashes
[j
], CS_CDHASH_LEN
);
128 } else if (append
.version
== 1) {
129 if ((cache
.entries
= realloc(cache
.entries
, sizeof(struct trust_cache_entry1
) *
130 (cache
.num_entries
+ append
.num_entries
))) == NULL
)
132 for (uint32_t j
= 0; j
< append
.num_entries
; j
++) {
133 cache
.entries
[cache
.num_entries
+ j
].hash_type
= append
.entries
[j
].hash_type
;
134 cache
.entries
[cache
.num_entries
+ j
].flags
= flags
!= 0 ? flags
: append
.entries
[j
].flags
;
135 memcpy(cache
.entries
[cache
.num_entries
+ j
].cdhash
, append
.entries
[j
].cdhash
, CS_CDHASH_LEN
);
137 } else if (append
.version
== 2) {
138 if ((cache
.entries2
= realloc(cache
.entries
, sizeof(struct trust_cache_entry2
) *
139 (cache
.num_entries
+ append
.num_entries
))) == NULL
)
141 for (uint32_t j
= 0; j
< append
.num_entries
; j
++) {
142 cache
.entries2
[cache
.num_entries
+ j
].hash_type
= append
.entries
[j
].hash_type
;
143 cache
.entries2
[cache
.num_entries
+ j
].flags
= flags
!= 0 ? flags
: append
.entries
[j
].flags
;
144 cache
.entries2
[cache
.num_entries
+ j
].category
= category
!= 0 ? category
: append
.entries2
[j
].category
;
145 memcpy(cache
.entries2
[cache
.num_entries
+ j
].cdhash
, append
.entries2
[j
].cdhash
, CS_CDHASH_LEN
);
149 cache
.num_entries
+= append
.num_entries
;
152 if (cache
.version
== 0)
153 qsort(cache
.hashes
, cache
.num_entries
, sizeof(*cache
.hashes
), hash_cmp
);
154 else if (cache
.version
== 1)
155 qsort(cache
.entries
, cache
.num_entries
, sizeof(*cache
.entries
), ent_cmp
);
156 else if (cache
.version
== 2)
157 qsort(cache
.entries
, cache
.num_entries
, sizeof(*cache
.entries2
), ent_cmp
);
161 uuid_generate(cache
.uuid
);
166 uuid_copy(cache
.uuid
, uuid
);
170 if (writetrustcache(cache
, argv
[0]) == -1)