38#include <openssl/sha.h>
40#include "xbps_api_impl.h"
48digest2string(
const uint8_t *digest,
char *
string,
size_t len)
51 if (*digest / 16 < 10)
52 *
string++ =
'0' + *digest / 16;
54 *
string++ =
'a' + *digest / 16 - 10;
55 if (*digest % 16 < 10)
56 *
string++ =
'0' + *digest % 16;
58 *
string++ =
'a' + *digest % 16 - 10;
68 size_t pgsize = (size_t)sysconf(_SC_PAGESIZE);
69 size_t pgmask = pgsize - 1, mapsize;
71 bool need_guard =
false;
76 if ((fd = open(file, O_RDONLY|O_CLOEXEC)) == -1)
79 if (fstat(fd, &st) == -1) {
83 if (st.st_size > SSIZE_MAX - 1) {
87 mapsize = ((size_t)st.st_size + pgmask) & ~pgmask;
88 if (mapsize < (
size_t)st.st_size) {
97 if ((st.st_size & pgmask) == 0)
100 mf = mmap(NULL, need_guard ? mapsize + pgsize : mapsize,
101 PROT_READ, MAP_PRIVATE, fd, 0);
103 if (mf == MAP_FAILED) {
104 (void)munmap(mf, mapsize);
110 *filelen = st.st_size;
123 assert(dstlen >= XBPS_SHA256_DIGEST_SIZE);
124 if (dstlen < XBPS_SHA256_DIGEST_SIZE) {
129 if ((fd = open(file, O_RDONLY)) < 0)
132 SHA256_Init(&sha256);
134 while ((len = read(fd, buf,
sizeof(buf))) > 0)
135 SHA256_Update(&sha256, buf, len);
142 SHA256_Final(dst, &sha256);
150 unsigned char digest[XBPS_SHA256_DIGEST_SIZE];
152 assert(dstlen >= XBPS_SHA256_SIZE);
153 if (dstlen < XBPS_SHA256_SIZE) {
161 digest2string(digest, dst, XBPS_SHA256_DIGEST_SIZE);
167sha256_digest_compare(
const char *sha256,
size_t shalen,
168 const unsigned char *digest,
size_t digestlen)
171 assert(shalen == XBPS_SHA256_SIZE - 1);
172 if (shalen != XBPS_SHA256_SIZE -1)
175 assert(digestlen == XBPS_SHA256_DIGEST_SIZE);
176 if (digestlen != XBPS_SHA256_DIGEST_SIZE)
180 if (*digest / 16 < 10) {
181 if (*sha256++ !=
'0' + *digest / 16)
184 if (*sha256++ !=
'a' + *digest / 16 - 10)
187 if (*digest % 16 < 10) {
188 if (*sha256++ !=
'0' + *digest % 16)
191 if (*sha256++ !=
'a' + *digest % 16 - 10)
203 unsigned char digest[XBPS_SHA256_DIGEST_SIZE];
205 assert(file != NULL);
206 assert(sha256 != NULL);
211 if (!sha256_digest_compare(sha256, strlen(sha256), digest,
sizeof digest))
218file_hash_dictionary(xbps_dictionary_t d,
const char *key,
const char *file)
221 xbps_object_iterator_t iter;
222 const char *curfile = NULL, *sha256 = NULL;
224 assert(xbps_object_type(d) == XBPS_TYPE_DICTIONARY);
226 assert(file != NULL);
233 while ((obj = xbps_object_iterator_next(iter)) != NULL) {
234 xbps_dictionary_get_cstring_nocopy(obj,
236 if (strcmp(file, curfile) == 0) {
238 xbps_dictionary_get_cstring_nocopy(obj,
243 xbps_object_iterator_release(iter);
251xbps_file_hash_check_dictionary(
struct xbps_handle *xhp,
256 const char *sha256d = NULL;
260 assert(xbps_object_type(d) == XBPS_TYPE_DICTIONARY);
262 assert(file != NULL);
264 if ((sha256d = file_hash_dictionary(d, key, file)) == NULL) {
271 if (strcmp(xhp->
rootdir,
"/") == 0) {
280 else if (rv == ERANGE || rv == ENOENT)
char rootdir[XBPS_MAXPATH]
Generic XBPS structure handler for initialization.
xbps_object_iterator_t xbps_array_iter_from_dict(xbps_dictionary_t dict, const char *key)
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
int xbps_file_sha256_check(const char *file, const char *sha256)
bool xbps_mmap_file(const char *file, void **mmf, size_t *mmflen, size_t *filelen)
bool xbps_file_sha256(char *dst, size_t dstlen, const char *file)
bool xbps_file_sha256_raw(unsigned char *dst, size_t dstlen, const char *file)