package

simple and robust package manager
Log | Files | Refs | README

commit cb72f0ce1211844241aeb99eae7ba529d06c3a91
parent 6473a11e4fb8b5b09ac31048a4336b90ddfad3d2
Author: Josuah Demangeon <mail@josuah.net>
Date:   Mon, 21 Jan 2019 23:51:00 +0100

initial import of blake2b

Diffstat:
MMakefile | 4+++-
Abin/package-del | 26++++++++++++++++++++++++++
Ablake2b.c | 385+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ablake2b.h | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlog.c | 12++++--------
Mmem.c | 6++++++
Mmem.h | 1+
Mpackage-add.c | 12++++--------
Mpackage-install.c | 3+--
Mpackage-tree.c | 6++----
Mpackage.c | 44+++++++++++++++++---------------------------
Mpackage.h | 2--
Auint32.c | 39+++++++++++++++++++++++++++++++++++++++
Auint32.h | 14++++++++++++++
Muint64.c | 54++++++++++++++++++++++++------------------------------
Muint64.h | 11+++++++----
16 files changed, 598 insertions(+), 86 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ bin = package-build package-dep package-get package-tree package-add package-install -obj = package.o buffer.o fmt.o stat.o hier.o log.o mem.o str.o stralloc.o forkexec.o tai.o tain.o +obj = package.o buffer.o fmt.o stat.o hier.o log.o mem.o str.o stralloc.o forkexec.o tai.o tain.o blake2b.o uint64.o uint32.o .PHONY: all all: ${bin} @@ -35,6 +35,7 @@ package-install: package-install.o ${obj} .c.o: ./make-o $< +blake2b.o: blake2b.c blake2b.h mem.h uint32.h uint64.h buffer.o: buffer.c buffer.h mem.h stralloc.h fmt.o: fmt.c fmt.h str.h forkexec.o: forkexec.c open.h forkexec.h @@ -53,4 +54,5 @@ str.o: str.c str.h stralloc.o: stralloc.c mem.h fmt.h str.h stralloc.h tai.o: tai.c tai.h tain.o: tain.c tain.h uint64.h fmt.h +uint32.o: uint32.c uint32.h uint64.o: uint64.c uint64.h diff --git a/bin/package-del b/bin/package-del @@ -0,0 +1,26 @@ +#!/bin/sh -e + +PACKAGE_DIR=/package +PACKAGE_ROOT=/ + +usage() { + printf '%s [-p pkgdir] package/version\n' "$0" + exit 1 +} + +while [ "$#" -gt 0 ]; do + case "$1" in + (-p) shift; PACKAGE_DIR=$1 ;; + (-r) shift; PACKAGE_ROOT=$1 ;; + (-*) usage ;; + (*) break ;; + esac + shift +done + +[ "$#" -eq 1 ] || usage +case "$1" in (*/*) ;; (*) usage ;; esac + +rm "$PACKAGE_DIR/$1/current" + +find -L "$PACKAGE_ROOT/bin" -type l -exec rm {} + diff --git a/blake2b.c b/blake2b.c @@ -0,0 +1,385 @@ +/* + * BLAKE2 reference source code package - C implementations + * + * Written in 2012 by Samuel Neves <sneves@dei.uc.pt> + * Changed in 2019 by Josuah Demangeon <josuah@h-o.st> + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * You should have received a copy of the CC0 Public Domain Dedication along + * with + * this software. If not, see + * <http://creativecommons.org/publicdomain/zero/1.0/>. + */ + +#include <assert.h> + +#include "blake2b.h" +#include "mem.h" +#include "uint32.h" +#include "uint64.h" + +#define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1]) + +static const uint64_t blake2b_IV[8] = { + 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, + 0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, + 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL +}; + +static const uint8_t blake2b_sigma[12][16] = { + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, + { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, + { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, + { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, + { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, + { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, + { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, + { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, + { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, + { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 }, + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, + { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } +}; + +static inline int +blake2b_set_lastnode(blake2b_state *S) +{ + S->f[1] = -1; + return 0; +} + +static inline int +blake2b_is_lastblock(const blake2b_state *S) +{ + return S->f[0] != 0; +} + +static inline int +blake2b_set_lastblock(blake2b_state *S) +{ + if (S->last_node) + blake2b_set_lastnode(S); + S->f[0] = -1; + return 0; +} + +static inline int +blake2b_increment_counter(blake2b_state *S, const uint64_t inc) +{ + S->t[0] += inc; + S->t[1] += (S->t[0] < inc); + return 0; +} + +/* Parameter-related functions */ +static inline int +blake2b_param_set_salt(blake2b_param *P, const uint8_t salt[BLAKE2B_SALTBYTES]) +{ + mem_copy(P->salt, salt, BLAKE2B_SALTBYTES); + return 0; +} + +static inline int +blake2b_param_set_personal(blake2b_param *P, const uint8_t personal[BLAKE2B_PERSONALBYTES]) +{ + mem_copy(P->personal, personal, BLAKE2B_PERSONALBYTES); + return 0; +} + +static inline int +blake2b_init0(blake2b_state *S) +{ + int i; + + for (i = 0; i < 8; i++) { + S->h[i] = blake2b_IV[i]; + } + /* zero everything between .t and .last_node */ + mem_zero((void *) &S->t, + offsetof(blake2b_state, last_node) + sizeof(S->last_node) + - offsetof(blake2b_state, t)); + return 0; +} + +/* init xors IV with input parameter block */ +int +blake2b_init_param(blake2b_state *S, const blake2b_param *P) +{ + size_t i; + const uint8_t *p; + + COMPILER_ASSERT(sizeof *P == 64); + blake2b_init0(S); + p = (const uint8_t *) (P); + + /* IV XOR ParamBlock */ + for (i = 0; i < 8; i++) + S->h[i] ^= uint64_unpack_le(p + sizeof(S->h[i]) * i); + return 0; +} + +int +blake2b_init(blake2b_state *S, const uint8_t outlen) +{ + blake2b_param P[1]; + + P->digest_length = outlen; + P->key_length = 0; + P->fanout = 1; + P->depth = 1; + uint32_pack_le(P->leaf_length, 0); + uint64_pack_le(P->node_offset, 0); + P->node_depth = 0; + P->inner_length = 0; + mem_zero(P->reserved, sizeof(P->reserved)); + mem_zero(P->salt, sizeof(P->salt)); + mem_zero(P->personal, sizeof(P->personal)); + return blake2b_init_param(S, P); +} + +int +blake2b_init_salt_personal(blake2b_state *S, const uint8_t outlen, const void *salt, const void *personal) +{ + blake2b_param P[1]; + + P->digest_length = outlen; + P->key_length = 0; + P->fanout = 1; + P->depth = 1; + uint32_pack_le(P->leaf_length, 0); + uint64_pack_le(P->node_offset, 0); + P->node_depth = 0; + P->inner_length = 0; + mem_zero(P->reserved, sizeof(P->reserved)); + + if (salt != NULL) + blake2b_param_set_salt(P, (const uint8_t *) salt); + else + mem_zero(P->salt, sizeof(P->salt)); + + if (personal != NULL) + blake2b_param_set_personal(P, (const uint8_t *) personal); + else + mem_zero(P->personal, sizeof(P->personal)); + + return blake2b_init_param(S, P); +} + +int +blake2b_init_key(blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen) +{ + blake2b_param P[1]; + + P->digest_length = outlen; + P->key_length = keylen; + P->fanout = 1; + P->depth = 1; + uint32_pack_le(P->leaf_length, 0); + uint64_pack_le(P->node_offset, 0); + P->node_depth = 0; + P->inner_length = 0; + mem_zero(P->reserved, sizeof(P->reserved)); + mem_zero(P->salt, sizeof(P->salt)); + mem_zero(P->personal, sizeof(P->personal)); + + { + uint8_t block[BLAKE2B_BLOCKBYTES]; + mem_zero(block, BLAKE2B_BLOCKBYTES); + mem_copy(block, key, keylen); /* key and keylen cannot be 0 */ + blake2b_update(S, block, BLAKE2B_BLOCKBYTES); + mem_zero(block, BLAKE2B_BLOCKBYTES); /* Burn the key from stack */ + } + return 0; +} + +int +blake2b_init_key_salt_personal(blake2b_state *S, const uint8_t outlen, const void *key, const uint8_t keylen, const void *salt, const void *personal) +{ + blake2b_param P[1]; + + P->digest_length = outlen; + P->key_length = keylen; + P->fanout = 1; + P->depth = 1; + uint32_pack_le(P->leaf_length, 0); + uint64_pack_le(P->node_offset, 0); + P->node_depth = 0; + P->inner_length = 0; + mem_zero(P->reserved, sizeof(P->reserved)); + + if (salt != NULL) + blake2b_param_set_salt(P, (const uint8_t *) salt); + else + mem_zero(P->salt, sizeof(P->salt)); + + if (personal != NULL) + blake2b_param_set_personal(P, (const uint8_t *) personal); + else + mem_zero(P->personal, sizeof(P->personal)); + + { + uint8_t block[BLAKE2B_BLOCKBYTES]; + mem_zero(block, BLAKE2B_BLOCKBYTES); + mem_copy(block, key, keylen); /* key and keylen cannot be 0 */ + blake2b_update(S, block, BLAKE2B_BLOCKBYTES); + mem_zero(block, BLAKE2B_BLOCKBYTES); /* Burn the key from stack */ + } + return 0; +} + +/* inlen now in bytes */ +int +blake2b_update(blake2b_state *S, const uint8_t *in, uint64_t inlen) +{ + while (inlen > 0) { + size_t left = S->buflen; + size_t fill = 2 * BLAKE2B_BLOCKBYTES - left; + + if (inlen > fill) { + mem_copy(S->buf + left, in, fill); /* Fill buffer */ + S->buflen += fill; + blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES); + blake2b_compress(S, S->buf); /* Compress */ + mem_copy(S->buf, S->buf + BLAKE2B_BLOCKBYTES, + BLAKE2B_BLOCKBYTES); /* Shift buffer left */ + S->buflen -= BLAKE2B_BLOCKBYTES; + in += fill; + inlen -= fill; + } else { + mem_copy(S->buf + left, in, inlen); + S->buflen += inlen; /* Be lazy, do not compress */ + in += inlen; + inlen -= inlen; + } + } + + return 0; +} + +int +blake2b_final(blake2b_state *S, uint8_t *out, uint8_t outlen) +{ + unsigned char buffer[BLAKE2B_OUTBYTES]; + + if (blake2b_is_lastblock(S)) + return -1; + + if (S->buflen > BLAKE2B_BLOCKBYTES) { + blake2b_increment_counter(S, BLAKE2B_BLOCKBYTES); + blake2b_compress(S, S->buf); + S->buflen -= BLAKE2B_BLOCKBYTES; + assert(S->buflen <= BLAKE2B_BLOCKBYTES); + mem_copy(S->buf, S->buf + BLAKE2B_BLOCKBYTES, S->buflen); + } + + blake2b_increment_counter(S, S->buflen); + blake2b_set_lastblock(S); + mem_zero(S->buf + S->buflen, 2 * BLAKE2B_BLOCKBYTES - S->buflen); /* Padding */ + blake2b_compress(S, S->buf); + + COMPILER_ASSERT(sizeof buffer == 64U); + uint64_pack_le(buffer + 8 * 0, S->h[0]); + uint64_pack_le(buffer + 8 * 1, S->h[1]); + uint64_pack_le(buffer + 8 * 2, S->h[2]); + uint64_pack_le(buffer + 8 * 3, S->h[3]); + uint64_pack_le(buffer + 8 * 4, S->h[4]); + uint64_pack_le(buffer + 8 * 5, S->h[5]); + uint64_pack_le(buffer + 8 * 6, S->h[6]); + uint64_pack_le(buffer + 8 * 7, S->h[7]); + mem_copy(out, buffer, outlen); /* outlen <= BLAKE2B_OUTBYTES (64) */ + + mem_zero(S->h, sizeof S->h); + mem_zero(S->buf, sizeof S->buf); + + return 0; +} + +/* inlen, at least, should be uint64_t. Others can be size_t. */ +int +blake2b(uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen) +{ + blake2b_state S[1]; + + blake2b_update(S, (const uint8_t *) in, inlen); + blake2b_final(S, out, outlen); + return 0; +} + +int +blake2b_salt_personal(uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen, const void *salt, const void *personal) +{ + blake2b_state S[1]; + + blake2b_update(S, (const uint8_t *) in, inlen); + blake2b_final(S, out, outlen); + return 0; +} +int +blake2b_compress(blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES]) +{ + uint64_t m[16]; + uint64_t v[16]; + int i; + + for (i = 0; i < 16; ++i) + m[i] = uint64_unpack_le(block + i * sizeof(m[i])); + for (i = 0; i < 8; ++i) + v[i] = S->h[i]; + + v[8] = blake2b_IV[0]; + v[9] = blake2b_IV[1]; + v[10] = blake2b_IV[2]; + v[11] = blake2b_IV[3]; + v[12] = S->t[0] ^ blake2b_IV[4]; + v[13] = S->t[1] ^ blake2b_IV[5]; + v[14] = S->f[0] ^ blake2b_IV[6]; + v[15] = S->f[1] ^ blake2b_IV[7]; + +#define G(r, i, a, b, c, d) \ + do { \ + a = a + b + m[blake2b_sigma[r][2 * i + 0]]; \ + d = uint64_rotr(d ^ a, 32); \ + c = c + d; \ + b = uint64_rotr(b ^ c, 24); \ + a = a + b + m[blake2b_sigma[r][2 * i + 1]]; \ + d = uint64_rotr(d ^ a, 16); \ + c = c + d; \ + b = uint64_rotr(b ^ c, 63); \ + } while (0) + +#define ROUND(r) \ + do { \ + G(r, 0, v[0], v[4], v[8], v[12]); \ + G(r, 1, v[1], v[5], v[9], v[13]); \ + G(r, 2, v[2], v[6], v[10], v[14]); \ + G(r, 3, v[3], v[7], v[11], v[15]); \ + G(r, 4, v[0], v[5], v[10], v[15]); \ + G(r, 5, v[1], v[6], v[11], v[12]); \ + G(r, 6, v[2], v[7], v[8], v[13]); \ + G(r, 7, v[3], v[4], v[9], v[14]); \ + } while (0) + + ROUND(0); + ROUND(1); + ROUND(2); + ROUND(3); + ROUND(4); + ROUND(5); + ROUND(6); + ROUND(7); + ROUND(8); + ROUND(9); + ROUND(10); + ROUND(11); + +#undef G +#undef ROUND + + for (i = 0; i < 8; ++i) + S->h[i] = S->h[i] ^ v[i] ^ v[i + 8]; + + return 0; +} diff --git a/blake2b.h b/blake2b.h @@ -0,0 +1,65 @@ +/* + * BLAKE2 reference source code package - reference C implementations + * + * Written in 2012 by Samuel Neves <sneves@dei.uc.pt> + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide. This software is distributed without any warranty. + * + * All code is triple-licensed under the + * [CC0](http://creativecommons.org/publicdomain/zero/1.0), the + * [OpenSSL Licence](https://www.openssl.org/source/license.html), or + * the [Apache Public License 2.0](http://www.apache.org/licenses/LICENSE-2.0), + * at your choosing. + */ + +#ifndef BLAKE2_H +#define BLAKE2_H + +#include <stddef.h> +#include <stdint.h> + +enum blake2b_constant { + BLAKE2B_BLOCKBYTES = 128, + BLAKE2B_OUTBYTES = 64, + BLAKE2B_KEYBYTES = 64, + BLAKE2B_SALTBYTES = 16, + BLAKE2B_PERSONALBYTES = 16 +}; + +typedef struct blake2b_param_ { + uint8_t digest_length; /* 1 */ + uint8_t key_length; /* 2 */ + uint8_t fanout; /* 3 */ + uint8_t depth; /* 4 */ + uint8_t leaf_length[4]; /* 8 */ + uint8_t node_offset[8]; /* 16 */ + uint8_t node_depth; /* 17 */ + uint8_t inner_length; /* 18 */ + uint8_t reserved[14]; /* 32 */ + uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */ + uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */ +} blake2b_param; + +typedef struct blake2b_state { + uint64_t h[8]; + uint64_t t[2]; + uint64_t f[2]; + uint8_t buf[2 * 128]; + size_t buflen; + uint8_t last_node; +} blake2b_state; + +int blake2b(uint8_t *, const void *, const void *, const uint8_t, const uint64_t, uint8_t); +int blake2b_compress(blake2b_state *, const uint8_t block[BLAKE2B_BLOCKBYTES]); +int blake2b_final(blake2b_state *, uint8_t *, uint8_t); +int blake2b_init(blake2b_state *, const uint8_t); +int blake2b_init_key(blake2b_state *, const uint8_t, const void *, const uint8_t); +int blake2b_init_key_salt_personal(blake2b_state *, const uint8_t, const void *, const uint8_t, const void *, const void *); +int blake2b_init_param(blake2b_state *, const blake2b_param *); +int blake2b_init_salt_personal(blake2b_state *, const uint8_t, const void *, const void *); +int blake2b_salt_personal(uint8_t *, const void *, const void *, const uint8_t, const uint64_t, uint8_t, const void *, const void *); +int blake2b_update(blake2b_state *, const uint8_t *, uint64_t); + +#endif diff --git a/log.c b/log.c @@ -24,8 +24,7 @@ log_put(int level, char const *type, int argc, ...) va_start(a, argc); buffer_puts(buffer_2, type); buffer_puts(buffer_2, ": "); - for (int x = 0; x < argc; x++) - { + for (int x = 0; x < argc; x++) { char *s = va_arg(a, char *); buffer_puts(buffer_2, s); } @@ -43,8 +42,7 @@ log_put_sys(int level, char const *type, int argc, ...) va_start(a, argc); buffer_puts(buffer_2, type); buffer_puts(buffer_2, ": "); - for (int x = 0; x < argc; x++) - { + for (int x = 0; x < argc; x++) { char *s = va_arg(a, char *); buffer_puts(buffer_2, s); } @@ -63,8 +61,7 @@ log_fatal(int e, char const *type, int argc, ...) va_start(a, argc); buffer_puts(buffer_2, type); buffer_puts(buffer_2, ": "); - for (int x = 0; x < argc; x++) - { + for (int x = 0; x < argc; x++) { char *s = va_arg(a, char *); buffer_puts(buffer_2, s); } @@ -83,8 +80,7 @@ log_fatal_sys(int e, char const *type, int argc, ...) va_start(a, argc); buffer_puts(buffer_2, type); buffer_puts(buffer_2, ": "); - for (int x = 0; x < argc; x++) - { + for (int x = 0; x < argc; x++) { char *s = va_arg(a, char *); buffer_puts(buffer_2, s); } diff --git a/mem.c b/mem.c @@ -6,3 +6,9 @@ mem_chr(char const *buf, size_t n, char c) char *p = memchr(buf, c, n); return p ? p - buf : n; } + +void +mem_xor(unsigned char *out, const unsigned char *in, size_t n) +{ + for (size_t i = 0; i < n; i++) out[i] ^= in[i]; +} diff --git a/mem.h b/mem.h @@ -12,5 +12,6 @@ #define mem_zero(buf, n) memset(buf, 0, n) size_t mem_chr(char const *, size_t, char); +void mem_xor(unsigned char *, const unsigned char *, size_t); #endif diff --git a/package-add.c b/package-add.c @@ -122,8 +122,7 @@ main(int argc, char **argv) if (!genalloc_append(&ga, p)) die_nomem(); // download and build download-time dependencies - for (size_t i = 0; i < genalloc_len(&ga, package); ++i) - { + for (size_t i = 0; i < genalloc_len(&ga, package); ++i) { package *p = genalloc_get(&ga, package, i); if (!p->dep_download) continue; if ((x = get(p, def, pkg)) != 0) return x; @@ -131,15 +130,13 @@ main(int argc, char **argv) } // download everything - for (size_t i = 0; i < genalloc_len(&ga, package); ++i) - { + for (size_t i = 0; i < genalloc_len(&ga, package); ++i) { package *p = genalloc_get(&ga, package, i); if ((x = get(p, def, pkg)) != 0) return x; } // build everything - for (size_t i = 0; i < genalloc_len(&ga, package); ++i) - { + for (size_t i = 0; i < genalloc_len(&ga, package); ++i) { package *p = genalloc_get(&ga, package, i); if ((x = build(p, def, pkg)) != 0) return x; } @@ -147,8 +144,7 @@ main(int argc, char **argv) if (flag_n) return 0; // install run-time dependencies - for (size_t i = 0; i < genalloc_len(&ga, package); ++i) - { + for (size_t i = 0; i < genalloc_len(&ga, package); ++i) { package *p = genalloc_get(&ga, package, i); if (!p->dep_run) continue; if ((x = install(p, def, pkg, root)) != 0) goto error; diff --git a/package-install.c b/package-install.c @@ -24,8 +24,7 @@ main(int argc, char **argv) (void)argc; - while (*++av) - { + while (*++av) { if (str_equal(*av, "-p")) { if (!(pkg = *++av)) break; continue; } if (str_equal(*av, "-d")) { if (!(def = *++av)) break; continue; } if (str_equal(*av, "-r")) { if (!(root = *++av)) break; continue; } diff --git a/package-tree.c b/package-tree.c @@ -40,12 +40,10 @@ tree(package *p, stralloc *prefix, char const *def) fd[1] = package_dep_open_version(p, def); if (fd[1] == -1 && errno != ENOTDIR && errno != ENOENT) goto error; - for (int i = 0; i < 2; ++i) - { + for (int i = 0; i < 2; ++i) { if (fd[i] == -1) continue; buffer_init(&b, &read, fd[i], buf, sizeof buf); - while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) - { + while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) { package p; sa.n -= (sa.x[sa.n - 1] == '\n'); diff --git a/package.c b/package.c @@ -39,8 +39,8 @@ error: return ret; } -int -package_env_dep_l(package const *p, char const *pkg) +static int +package_env_lib(package const *p, char const *pkg) { stralloc sa; char buf[NAME_MAX]; @@ -68,7 +68,7 @@ error: } static int -package_env_dep_bd(package const *p, char const *pkg) +package_env_path(package const *p, char const *pkg) { stralloc sa; char *path; @@ -108,20 +108,19 @@ package_env_dep(package *p, char const *def, char const *pkg) fd[1] = package_dep_open_version(p, def); if (fd[1] == -1 && errno != ENOTDIR && errno != ENOENT) goto error; - for (int i = 0; i < 2; ++i) - { + for (int i = 0; i < 2; ++i) { if (fd[i] == -1) continue; + buffer_init(&b, &read, fd[i], buf, sizeof buf); - while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) - { + while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) { package p; sa.n -= (sa.x[sa.n - 1] == '\n'); if (!stralloc_cat0(&sa)) goto error; if (sa.x[package_dep_scan(&p, sa.x, sa.n)] != '\0') goto error; if (!package_version(&p, def)) goto error; - if (!package_env_dep_l(&p, pkg)) goto error; - if (!package_env_dep_bd(&p, pkg)) goto error; + if (!package_env_lib(&p, pkg)) goto error; + if (!package_env_path(&p, pkg)) goto error; } if (r != 0) goto error; } @@ -156,16 +155,14 @@ package_scan(package *p, char const *s, size_t n) if (s[0] == '/') return 0; - for (i = 0; i < n; ++i, ++x) - { + for (i = 0; i < n; ++i, ++x) { if (!isprint(s[i]) || s[i] == ' ') goto end; if (s[i] == '/') { ++i; break; } if (i >= sizeof p->name) return 0; p->name[x] = s[i]; } - for (; i < n; ++i, ++y) - { + for (; i < n; ++i, ++y) { if (!isprint(s[i]) || s[i] == ' ') goto end; if (s[i] == '/') break; if (i > sizeof p->ver) return 0; @@ -192,10 +189,8 @@ package_dep_scan(package *p, char const *s, size_t n) y = package_scan(p, s + x + 1, n - x - 1); if (x + 1 + y == n) return 0; - for (size_t i = 0; i < x; ++i) - { - switch (s[i]) - { + for (size_t i = 0; i < x; ++i) { + switch (s[i]) { case 'd': p->dep_download = 1; break; case 'b': p->dep_build = 1; break; case 'l': p->dep_lib = 1; break; @@ -210,11 +205,9 @@ package_dep_scan(package *p, char const *s, size_t n) static int package_dep_has(package const *p, genalloc *packages) { - for (size_t i = 0; i < genalloc_len(packages, package); ++i) - { + for (size_t i = 0; i < genalloc_len(packages, package); ++i) { package *x = genalloc_get(packages, package, i); - if (str_equal(x->name, p->name)) - if (str_equal(x->ver, p->ver)) + if (str_equal(x->name, p->name) && str_equal(x->ver, p->ver)) return 1; } return 0; @@ -313,14 +306,11 @@ package_dep(package const *p, genalloc *packages, char const *def) fd[1] = package_dep_open_version(p, def); if (fd[1] == -1 && errno != ENOTDIR && errno != ENOENT) goto error; - for (int i = 0; i < 2; ++i) - { + for (int i = 0; i < 2; ++i) { if (fd[i] == -1) continue; buffer_init(&b, &read, fd[i], buf, sizeof buf); - stralloc_init(&sa); - while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) - { + while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) { package p; sa.n -= (sa.x[sa.n - 1] == '\n'); @@ -333,8 +323,8 @@ package_dep(package const *p, genalloc *packages, char const *def) } if (r != 0) goto error; } - ret = 1; + error: if (fd[0] >= 0) close(fd[0]); if (fd[1] >= 0) close(fd[1]); diff --git a/package.h b/package.h @@ -22,8 +22,6 @@ int package_dep(package const *, genalloc *, char const *); int package_dep_open_default(package const *, char const *); int package_dep_open_version(package const *, char const *); int package_env_dep(package *, char const *, char const *); -int package_env_dep_build_download(package const *, char const *); -int package_env_dep_lib(package const *, char const *); int package_env_prefix(package *, char const *, char const *); int package_version(package *, char const *); size_t package_dep_scan(package *, char const *, size_t); diff --git a/uint32.c b/uint32.c @@ -0,0 +1,39 @@ +#include "uint32.h" + +void +uint32_pack_le(unsigned char s[4], uint32_t u) +{ + s[3] = u & 255; + s[2] = u >> 8 & 255; + s[1] = u >> 16 & 255; + s[0] = u >> 24; +} + +void +uint32_pack_be(unsigned char s[4], uint32_t u) +{ + s[0] = u & 255; + s[1] = u >> 8 & 255; + s[2] = u >> 16 & 255; + s[3] = u >> 24; +} + +uint32_t +uint32_unpack_le(const unsigned char s[4]) +{ + uint32_t x = (unsigned char)s[3]; + x |= (uint32_t)(unsigned char)s[2] << 8; + x |= (uint32_t)(unsigned char)s[1] << 16; + x |= (uint32_t)(unsigned char)s[0] << 24; + return x; +} + +uint32_t +uint32_unpack_be(const unsigned char s[4]) +{ + uint32_t x = (unsigned char)s[0]; + x |= (uint32_t)(unsigned char)s[1] << 8; + x |= (uint32_t)(unsigned char)s[2] << 16; + x |= (uint32_t)(unsigned char)s[3] << 24; + return x; +} diff --git a/uint32.h b/uint32.h @@ -0,0 +1,14 @@ +#ifndef UINT32_H +#define UINT32_H + +#include <stdint.h> + +#define uint32_rotr(x, b) (uint32_t)(((x) << (b)) | ((x) >> (32 - (b)))) +#define uint32_rotl(x, b) (uint32_t)(((x) >> (b)) | ((x) << (32 - (b)))) + +uint32_t uint32_unpack_be(const unsigned char s[4]); +uint32_t uint32_unpack_le(const unsigned char s[4]); +void uint32_pack_be(unsigned char s[4], uint32_t); +void uint32_pack_le(unsigned char s[4], uint32_t); + +#endif diff --git a/uint64.c b/uint64.c @@ -1,7 +1,7 @@ #include "uint64.h" void -uint64_pack(char s[8], uint64_t u) +uint64_pack_le(unsigned char s[8], uint64_t u) { s[7] = u & 255; s[6] = u >> 8 & 255; @@ -14,7 +14,7 @@ uint64_pack(char s[8], uint64_t u) } void -uint64_pack_big(char s[8], uint64_t u) +uint64_pack_be(unsigned char s[8], uint64_t u) { s[0] = u & 255; s[1] = u >> 8 & 255; @@ -26,36 +26,30 @@ uint64_pack_big(char s[8], uint64_t u) s[7] = u >> 56; } -void -uint64_unpack(const char s[8], uint64_t *u) +uint64_t +uint64_unpack_le(const unsigned char s[8]) { - uint64_t x; - - x = (unsigned char)s[7]; - x += (uint64_t)(unsigned char)s[6] << 8; - x += (uint64_t)(unsigned char)s[5] << 16; - x += (uint64_t)(unsigned char)s[4] << 24; - x += (uint64_t)(unsigned char)s[3] << 32; - x += (uint64_t)(unsigned char)s[2] << 40; - x += (uint64_t)(unsigned char)s[1] << 48; - x += (uint64_t)(unsigned char)s[0] << 56; - - *u = x; + uint64_t x = (unsigned char)s[7]; + x |= (uint64_t)(unsigned char)s[6] << 8; + x |= (uint64_t)(unsigned char)s[5] << 16; + x |= (uint64_t)(unsigned char)s[4] << 24; + x |= (uint64_t)(unsigned char)s[3] << 32; + x |= (uint64_t)(unsigned char)s[2] << 40; + x |= (uint64_t)(unsigned char)s[1] << 48; + x |= (uint64_t)(unsigned char)s[0] << 56; + return x; } -void -uint64_unpack_big(const char s[8], uint64_t *u) +uint64_t +uint64_unpack_be(const unsigned char s[8]) { - uint64_t x; - - x = (unsigned char)s[0]; - x += (uint64_t)(unsigned char)s[1] << 8; - x += (uint64_t)(unsigned char)s[2] << 16; - x += (uint64_t)(unsigned char)s[3] << 24; - x += (uint64_t)(unsigned char)s[4] << 32; - x += (uint64_t)(unsigned char)s[5] << 40; - x += (uint64_t)(unsigned char)s[6] << 48; - x += (uint64_t)(unsigned char)s[7] << 56; - - *u = x; + uint64_t x = (unsigned char)s[0]; + x |= (uint64_t)(unsigned char)s[1] << 8; + x |= (uint64_t)(unsigned char)s[2] << 16; + x |= (uint64_t)(unsigned char)s[3] << 24; + x |= (uint64_t)(unsigned char)s[4] << 32; + x |= (uint64_t)(unsigned char)s[5] << 40; + x |= (uint64_t)(unsigned char)s[6] << 48; + x |= (uint64_t)(unsigned char)s[7] << 56; + return x; } diff --git a/uint64.h b/uint64.h @@ -3,9 +3,12 @@ #include <stdint.h> -void uint64_pack(char s[8], uint64_t); -void uint64_pack_big(char s[8], uint64_t); -void uint64_unpack(const char s[8], uint64_t *); -void uint64_unpack_big(const char s[8], uint64_t *); +#define uint64_rotr(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) +#define uint64_rotl(x, b) (uint64_t)(((x) >> (b)) | ((x) << (64 - (b)))) + +uint64_t uint64_unpack_be(const unsigned char s[8]); +uint64_t uint64_unpack_le(const unsigned char s[8]); +void uint64_pack_be(unsigned char s[8], uint64_t); +void uint64_pack_le(unsigned char s[8], uint64_t); #endif