From: Mark Wooding Date: Sat, 29 Apr 2017 12:55:40 +0000 (+0100) Subject: Catacomb import: Provide fake-mLib-bits.h X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=65961b7ca4feb0d84181290fb0af3763fdf28403;p=secnet.git Catacomb import: Provide fake-mLib-bits.h Here we provide a fake version of , for the benefit of the code from Catacomb. Signed-off-by: Mark Wooding --- diff --git a/fake-mLib-bits.h b/fake-mLib-bits.h new file mode 100644 index 0000000..e2e83ca --- /dev/null +++ b/fake-mLib-bits.h @@ -0,0 +1,83 @@ +/* + * This file is part of secnet. + * See README for full list of copyright holders. + * + * secnet is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version d of the License, or + * (at your option) any later version. + * + * secnet is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 3 along with secnet; if not, see + * https://www.gnu.org/licenses/gpl.html. + */ + +#ifndef fake_mLib_bits_h +#define fake_mLib_bits_h + +/* The header defines a large number of types and macros for + * various kinds of bithacking. Notably, it has machinery for autodetecting + * suitable types holding (at least) various numbers of bits -- a service + * which C99 provides through other means. + * + * This file provides a small portion of the interface, just + * enough to make the pieces of code lifted from Catacomb feel at home. + */ + +#include +#include "u64.h" +#include "unaligned.h" + +typedef uint8_t octet; +typedef uint32_t uint32; +typedef uint64_t uint64; + +typedef int32_t int32; +typedef int64_t int64; + +#define LOAD32_L(p) (get_uint32_le(p)) +#define LOAD32_B(p) (get_uint32(p)) +#define STORE32_L(p, x) put_uint32_le((p), (x)) +#define STORE32_B(p, x) put_uint32((p), (x)) +#define ROL32(x, n) ((n) ? ((x) << (n) | (x) >> (32 - (n))) : (x)) + +#define MASK32 0xffffffffu +#define U32(x) ((uint32)(x) & MASK32) + +typedef u64 kludge64; +#define X64(hi, lo) u64init(0x##hi, 0x##lo) +#define HI64(x) u64gethi(x) +#define LO64(x) u64getlo(x) +#define LOAD64_L_(x, p) do { \ + const uint8_t *p_ = (const uint8_t *)(p); \ + uint32_t lo_ = LOAD32_L(p_ + 0), hi_ = LOAD32_L(p_ + 4); \ + (x) = u64hilo(hi_, lo_); \ +} while (0) +#define LOAD64_B_(x, p) do { \ + const uint8_t *p_ = (const uint8_t *)(p); \ + uint32_t hi_ = LOAD32_B(p_ + 0), lo_ = LOAD32_B(p_ + 4); \ + (x) = u64hilo(hi_, lo_); \ +} while (0) +#define STORE64_L_(p, x) do { \ + uint8_t *p_ = (uint8_t *)(p); \ + uint32_t lo_ = LO64(x), hi_ = HI64(x); \ + STORE32_L(p_ + 0, lo_); STORE32_L(p_ + 4, hi_); \ +} while (0) +#define STORE64_B_(p, x) do { \ + uint8_t *p_ = (uint8_t *)(p); \ + uint32_t lo_ = LO64(x), hi_ = HI64(x); \ + STORE32_B(p_ + 0, hi_); STORE32_B(p_ + 4, lo_); \ +} while (0) +#define SET64(z, hi, lo) ((z) = u64hilo((hi), (lo))) +#define AND64(z, x, y) ((z) = u64and((x), (y))) +#define OR64(z, x, y) ((z) = u64or((x), (y))) +#define XOR64(z, x, y) ((z) = u64xor((x), (y))) +#define CPL64(z, x) ((z) = u64not((x))) +#define ROL64_(z, x, n) ((n) ? (z) = u64rol((x), (n)) : (x)) + +#endif /* fake_mLib_bits_h */