diff options
Diffstat (limited to 'source')
30 files changed, 273 insertions, 334 deletions
diff --git a/source/kit/array_ref.c b/source/kit/array_ref.c index dcc4ff5..4c1b1a4 100644 --- a/source/kit/array_ref.c +++ b/source/kit/array_ref.c @@ -3,26 +3,25 @@ #include <string.h> int kit_ar_equal_bytes(ptrdiff_t left_element_size, - ptrdiff_t left_size, void const *left_data, + ptrdiff_t left_size, void *left_data, ptrdiff_t right_element_size, - ptrdiff_t right_size, void const *right_data) { + ptrdiff_t right_size, void *right_data) { ptrdiff_t i; if (left_element_size != right_element_size) return 0; if (left_size != right_size) return 0; for (i = 0; i < left_size; i++) - if (memcmp((char const *) left_data + i * left_element_size, - (char const *) right_data + i * left_element_size, + if (memcmp((char *) left_data + i * left_element_size, + (char *) right_data + i * left_element_size, left_element_size) != 0) return 0; return 1; } int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, - void const *left_data, - ptrdiff_t right_element_size, ptrdiff_t right_size, - void const *right_data, + void *left_data, ptrdiff_t right_element_size, + ptrdiff_t right_size, void *right_data, kit_ar_compare_fn compare) { ptrdiff_t i; if (left_element_size < right_element_size) @@ -30,9 +29,8 @@ int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, if (left_element_size > right_element_size) return 1; for (i = 0; i < left_size && i < right_size; i++) { - int const c = compare( - (char const *) left_data + i * left_element_size, - (char const *) right_data + i * left_element_size); + int c = compare((char *) left_data + i * left_element_size, + (char *) right_data + i * left_element_size); if (c != 0) return c; } diff --git a/source/kit/array_ref.h b/source/kit/array_ref.h index c893b49..02f7370 100644 --- a/source/kit/array_ref.h +++ b/source/kit/array_ref.h @@ -12,44 +12,31 @@ extern "C" { #endif -typedef int (*kit_ar_compare_fn)(void const *left, void const *right); +typedef int (*kit_ar_compare_fn)(void *left, void *right); int kit_ar_equal_bytes(ptrdiff_t left_element_size, - ptrdiff_t left_size, void const *left_data, + ptrdiff_t left_size, void *left_data, ptrdiff_t right_element_size, - ptrdiff_t right_size, void const *right_data); + ptrdiff_t right_size, void *right_data); int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, - void const *left_data, - ptrdiff_t right_element_size, ptrdiff_t right_size, - void const *right_data, kit_ar_compare_fn compare); - -#define KIT_AR_MUT(type_) \ - struct { \ - ptrdiff_t size; \ - type_ *values; \ - } - -#define KIT_AR(type_) \ - struct { \ - ptrdiff_t size; \ - type_ const *values; \ + void *left_data, ptrdiff_t right_element_size, + ptrdiff_t right_size, void *right_data, + kit_ar_compare_fn compare); + +#define KIT_AR(type_) \ + struct { \ + ptrdiff_t size; \ + type_ *values; \ } -#define KIT_AR_MUT_WRAP(name_, element_type_, array_) \ +#define KIT_AR_WRAP(name_, element_type_, array_) \ struct { \ ptrdiff_t size; \ element_type_ *values; \ } name_ = { .size = (sizeof(array_) / sizeof((array_)[0])), \ .values = (array_) } -#define KIT_AR_WRAP(name_, element_type_, array_) \ - struct { \ - ptrdiff_t size; \ - element_type_ const *values; \ - } name_ = { .size = (sizeof(array_) / sizeof((array_)[0])), \ - .values = (array_) } - #define KIT_AR_EQUAL(left_, right_) \ kit_ar_equal_bytes(sizeof((left_).values[0]), (left_).size, \ (left_).values, sizeof((right_).values[0]), \ @@ -66,9 +53,7 @@ int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, # define ar_equal_bytes kit_ar_equal_bytes # define ar_compare kit_ar_compare -# define AR_MUT KIT_AR_MUT # define AR KIT_AR -# define AR_MUT_WRAP KIT_AR_MUT_WRAP # define AR_WRAP KIT_AR_WRAP # define AR_EQUAL KIT_AR_EQUAL # define AR_COMPARE KIT_AR_COMPARE diff --git a/source/kit/bigint.h b/source/kit/bigint.h index c948b57..7343b56 100644 --- a/source/kit/bigint.h +++ b/source/kit/bigint.h @@ -38,14 +38,14 @@ typedef uint_fast8_t kit_bit_t; # pragma GCC optimize("O3") #endif -static kit_bigint_t kit_bi_uint32(uint32_t const x) { +static kit_bigint_t kit_bi_uint32(uint32_t x) { kit_bigint_t z; memset(&z, 0, sizeof z); z.v[0] = x; return z; } -static kit_bigint_t kit_bi_uint64(uint64_t const x) { +static kit_bigint_t kit_bi_uint64(uint64_t x) { kit_bigint_t z; memset(&z, 0, sizeof z); z.v[0] = (uint32_t) (x & 0xffffffff); @@ -53,14 +53,14 @@ static kit_bigint_t kit_bi_uint64(uint64_t const x) { return z; } -static kit_bigint_t kit_bi_int32(int32_t const x) { +static kit_bigint_t kit_bi_int32(int32_t x) { kit_bigint_t z; memset(&z, x < 0 ? -1 : 0, sizeof z); z.v[0] = x; return z; } -static kit_bigint_t kit_bi_int64(int64_t const x) { +static kit_bigint_t kit_bi_int64(int64_t x) { kit_bigint_t z; memset(&z, x < 0 ? -1 : 0, sizeof z); z.v[0] = (uint32_t) (((uint64_t) x) & 0xffffffff); @@ -68,7 +68,7 @@ static kit_bigint_t kit_bi_int64(int64_t const x) { return z; } -static int kit_bi_is_zero(kit_bigint_t const x) { +static int kit_bi_is_zero(kit_bigint_t x) { ptrdiff_t i; for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) if (x.v[i] != 0) @@ -76,17 +76,16 @@ static int kit_bi_is_zero(kit_bigint_t const x) { return 1; } -static int kit_bi_is_neg(kit_bigint_t const x) { +static int kit_bi_is_neg(kit_bigint_t x) { return (x.v[KIT_BIGINT_SIZE / 4 - 1] & 0x80000000) != 0; } -static int kit_bi_equal(kit_bigint_t const x, kit_bigint_t const y) { +static int kit_bi_equal(kit_bigint_t x, kit_bigint_t y) { return kit_ar_equal_bytes(1, KIT_BIGINT_SIZE, x.v, 1, KIT_BIGINT_SIZE, y.v); } -static int kit_bi_compare(kit_bigint_t const x, - kit_bigint_t const y) { +static int kit_bi_compare(kit_bigint_t x, kit_bigint_t y) { ptrdiff_t i; for (i = KIT_BIGINT_SIZE / 4 - 1; i >= 0; i--) if (x.v[i] < y.v[i]) @@ -96,54 +95,53 @@ static int kit_bi_compare(kit_bigint_t const x, return 0; } -static ptrdiff_t kit_bi_significant_bit_count(kit_bigint_t const x) { +static ptrdiff_t kit_bi_significant_bit_count(kit_bigint_t x) { ptrdiff_t n = KIT_BIGINT_SIZE / 4 - 1; while (n > 0 && x.v[n] == 0) n--; - uint32_t const i32 = x.v[n]; + uint32_t val = x.v[n]; - if (i32 == 0) + if (val == 0) return 0; - ptrdiff_t const bits = (i32 & 0x80000000u) != 0 ? 32 - : (i32 & 0x40000000u) != 0 ? 31 - : (i32 & 0x20000000u) != 0 ? 30 - : (i32 & 0x10000000u) != 0 ? 29 - : (i32 & 0x8000000u) != 0 ? 28 - : (i32 & 0x4000000u) != 0 ? 27 - : (i32 & 0x2000000u) != 0 ? 26 - : (i32 & 0x1000000u) != 0 ? 25 - : (i32 & 0x800000u) != 0 ? 24 - : (i32 & 0x400000u) != 0 ? 23 - : (i32 & 0x200000u) != 0 ? 22 - : (i32 & 0x100000u) != 0 ? 21 - : (i32 & 0x80000u) != 0 ? 20 - : (i32 & 0x40000u) != 0 ? 19 - : (i32 & 0x20000u) != 0 ? 18 - : (i32 & 0x10000u) != 0 ? 17 - : (i32 & 0x8000u) != 0 ? 16 - : (i32 & 0x4000u) != 0 ? 15 - : (i32 & 0x2000u) != 0 ? 14 - : (i32 & 0x1000u) != 0 ? 13 - : (i32 & 0x800u) != 0 ? 12 - : (i32 & 0x400u) != 0 ? 11 - : (i32 & 0x200u) != 0 ? 10 - : (i32 & 0x100u) != 0 ? 9 - : (i32 & 0x80u) != 0 ? 8 - : (i32 & 0x40u) != 0 ? 7 - : (i32 & 0x20u) != 0 ? 6 - : (i32 & 0x10u) != 0 ? 5 - : (i32 & 0x08u) != 0 ? 4 - : (i32 & 0x04u) != 0 ? 3 - : (i32 & 0x02u) != 0 ? 2 - : 1; + ptrdiff_t bits = (val & 0x80000000u) != 0 ? 32 + : (val & 0x40000000u) != 0 ? 31 + : (val & 0x20000000u) != 0 ? 30 + : (val & 0x10000000u) != 0 ? 29 + : (val & 0x8000000u) != 0 ? 28 + : (val & 0x4000000u) != 0 ? 27 + : (val & 0x2000000u) != 0 ? 26 + : (val & 0x1000000u) != 0 ? 25 + : (val & 0x800000u) != 0 ? 24 + : (val & 0x400000u) != 0 ? 23 + : (val & 0x200000u) != 0 ? 22 + : (val & 0x100000u) != 0 ? 21 + : (val & 0x80000u) != 0 ? 20 + : (val & 0x40000u) != 0 ? 19 + : (val & 0x20000u) != 0 ? 18 + : (val & 0x10000u) != 0 ? 17 + : (val & 0x8000u) != 0 ? 16 + : (val & 0x4000u) != 0 ? 15 + : (val & 0x2000u) != 0 ? 14 + : (val & 0x1000u) != 0 ? 13 + : (val & 0x800u) != 0 ? 12 + : (val & 0x400u) != 0 ? 11 + : (val & 0x200u) != 0 ? 10 + : (val & 0x100u) != 0 ? 9 + : (val & 0x80u) != 0 ? 8 + : (val & 0x40u) != 0 ? 7 + : (val & 0x20u) != 0 ? 6 + : (val & 0x10u) != 0 ? 5 + : (val & 0x08u) != 0 ? 4 + : (val & 0x04u) != 0 ? 3 + : (val & 0x02u) != 0 ? 2 + : 1; return n * 32 + bits; } -static kit_bigint_t kit_bi_and(kit_bigint_t const x, - kit_bigint_t const y) { +static kit_bigint_t kit_bi_and(kit_bigint_t x, kit_bigint_t y) { kit_bigint_t z; ptrdiff_t i; @@ -152,8 +150,7 @@ static kit_bigint_t kit_bi_and(kit_bigint_t const x, return z; } -static kit_bigint_t kit_bi_or(kit_bigint_t const x, - kit_bigint_t const y) { +static kit_bigint_t kit_bi_or(kit_bigint_t x, kit_bigint_t y) { kit_bigint_t z; ptrdiff_t i; @@ -162,8 +159,7 @@ static kit_bigint_t kit_bi_or(kit_bigint_t const x, return z; } -static kit_bigint_t kit_bi_xor(kit_bigint_t const x, - kit_bigint_t const y) { +static kit_bigint_t kit_bi_xor(kit_bigint_t x, kit_bigint_t y) { kit_bigint_t z; ptrdiff_t i; @@ -172,14 +168,13 @@ static kit_bigint_t kit_bi_xor(kit_bigint_t const x, return z; } -static kit_bigint_t kit_bi_shl_uint(kit_bigint_t const x, - uint32_t const y) { +static kit_bigint_t kit_bi_shl_uint(kit_bigint_t x, uint32_t y) { kit_bigint_t z; memset(&z, 0, sizeof z); - ptrdiff_t const words = (ptrdiff_t) (y / 32); - ptrdiff_t const bits = (ptrdiff_t) (y % 32); - ptrdiff_t i; + ptrdiff_t words = (ptrdiff_t) (y / 32); + ptrdiff_t bits = (ptrdiff_t) (y % 32); + ptrdiff_t i; for (i = words; i < KIT_BIGINT_SIZE / 4; i++) { z.v[i] |= x.v[i - words] << bits; @@ -190,14 +185,13 @@ static kit_bigint_t kit_bi_shl_uint(kit_bigint_t const x, return z; } -static kit_bigint_t kit_bi_shr_uint(kit_bigint_t const x, - uint32_t const y) { +static kit_bigint_t kit_bi_shr_uint(kit_bigint_t x, uint32_t y) { kit_bigint_t z; memset(&z, 0, sizeof z); - ptrdiff_t const words = (ptrdiff_t) (y / 32); - ptrdiff_t const bits = (ptrdiff_t) (y % 32); - ptrdiff_t i; + ptrdiff_t words = (ptrdiff_t) (y / 32); + ptrdiff_t bits = (ptrdiff_t) (y % 32); + ptrdiff_t i; for (i = KIT_BIGINT_SIZE / 4 - words - 1; i >= 0; i--) { z.v[i] |= x.v[i + words] >> bits; @@ -208,15 +202,15 @@ static kit_bigint_t kit_bi_shr_uint(kit_bigint_t const x, return z; } -static kit_bit_t kit_bi_carry(uint32_t const x, uint32_t const y, - kit_bit_t const carry) { +static kit_bit_t kit_bi_carry(uint32_t x, uint32_t y, + kit_bit_t carry) { assert(carry == 0 || carry == 1); return 0xffffffffu - x < y || 0xffffffffu - x - y < carry ? 1 : 0; } /* Increment. */ -static kit_bigint_t kit_bi_inc(kit_bigint_t const x) { +static kit_bigint_t kit_bi_inc(kit_bigint_t x) { kit_bigint_t z; kit_bit_t carry = 1; ptrdiff_t i; @@ -231,7 +225,7 @@ static kit_bigint_t kit_bi_inc(kit_bigint_t const x) { /* Decrement */ -static kit_bigint_t kit_bi_dec(kit_bigint_t const x) { +static kit_bigint_t kit_bi_dec(kit_bigint_t x) { kit_bigint_t z; kit_bit_t carry = 0; ptrdiff_t i; @@ -246,8 +240,7 @@ static kit_bigint_t kit_bi_dec(kit_bigint_t const x) { /* Addition. */ -static kit_bigint_t kit_bi_add(kit_bigint_t const x, - kit_bigint_t const y) { +static kit_bigint_t kit_bi_add(kit_bigint_t x, kit_bigint_t y) { kit_bigint_t z; kit_bit_t carry = 0; ptrdiff_t i; @@ -262,7 +255,7 @@ static kit_bigint_t kit_bi_add(kit_bigint_t const x, /* Negation. */ -static kit_bigint_t kit_bi_neg(kit_bigint_t const x) { +static kit_bigint_t kit_bi_neg(kit_bigint_t x) { kit_bigint_t y; kit_bit_t carry = 1; ptrdiff_t i; @@ -277,8 +270,7 @@ static kit_bigint_t kit_bi_neg(kit_bigint_t const x) { /* Subtraction. */ -static kit_bigint_t kit_bi_sub(kit_bigint_t const x, - kit_bigint_t const y) { +static kit_bigint_t kit_bi_sub(kit_bigint_t x, kit_bigint_t y) { kit_bigint_t z; kit_bit_t carry = 1; ptrdiff_t i; @@ -291,8 +283,7 @@ static kit_bigint_t kit_bi_sub(kit_bigint_t const x, return z; } -static kit_bigint_t kit_bi_mul_uint32(kit_bigint_t const x, - uint32_t const y) { +static kit_bigint_t kit_bi_mul_uint32(kit_bigint_t x, uint32_t y) { kit_bigint_t z; ptrdiff_t i, k; @@ -306,9 +297,9 @@ static kit_bigint_t kit_bi_mul_uint32(kit_bigint_t const x, uint64_t carry = ((uint64_t) x.v[i]) * ((uint64_t) y); for (k = i; k < KIT_BIGINT_SIZE / 4 && carry != 0; k++) { - uint64_t const sum = ((uint64_t) z.v[k]) + carry; - z.v[k] = ((uint32_t) (sum & 0xffffffffull)); - carry = sum >> 32; + uint64_t sum = ((uint64_t) z.v[k]) + carry; + z.v[k] = ((uint32_t) (sum & 0xffffffffull)); + carry = sum >> 32; } } @@ -317,8 +308,7 @@ static kit_bigint_t kit_bi_mul_uint32(kit_bigint_t const x, /* Multiplication. */ -static kit_bigint_t kit_bi_mul(kit_bigint_t const x, - kit_bigint_t const y) { +static kit_bigint_t kit_bi_mul(kit_bigint_t x, kit_bigint_t y) { kit_bigint_t z; ptrdiff_t i, j, k; @@ -335,9 +325,9 @@ static kit_bigint_t kit_bi_mul(kit_bigint_t const x, uint64_t carry = ((uint64_t) x.v[i]) * ((uint64_t) y.v[j]); for (k = i + j; k < KIT_BIGINT_SIZE / 4 && carry != 0; k++) { - uint64_t const sum = ((uint64_t) z.v[k]) + carry; - z.v[k] = ((uint32_t) (sum & 0xffffffffull)); - carry = sum >> 32; + uint64_t sum = ((uint64_t) z.v[k]) + carry; + z.v[k] = ((uint32_t) (sum & 0xffffffffull)); + carry = sum >> 32; } } } @@ -353,20 +343,19 @@ typedef struct { /* Unsigned division. */ -static kit_bi_division_t kit_bi_udiv(kit_bigint_t const x, - kit_bigint_t y) { +static kit_bi_division_t kit_bi_udiv(kit_bigint_t x, kit_bigint_t y) { kit_bi_division_t z; memset(&z, 0, sizeof z); - ptrdiff_t const y_bits = kit_bi_significant_bit_count(y); + ptrdiff_t y_bits = kit_bi_significant_bit_count(y); if (y_bits == 0) { z.undefined = 1; return z; } - ptrdiff_t const x_bits = kit_bi_significant_bit_count(x); - ptrdiff_t shift = x_bits - y_bits; + ptrdiff_t x_bits = kit_bi_significant_bit_count(x); + ptrdiff_t shift = x_bits - y_bits; z.remainder = x; z.quotient = kit_bi_uint32(0); @@ -391,13 +380,12 @@ static kit_bi_division_t kit_bi_udiv(kit_bigint_t const x, * Remainder is always a non-negative value less than absolute value * of y. */ -static kit_bi_division_t kit_bi_div(kit_bigint_t const x, - kit_bigint_t const y) { - int const x_neg = kit_bi_is_neg(x); - int const y_neg = kit_bi_is_neg(y); +static kit_bi_division_t kit_bi_div(kit_bigint_t x, kit_bigint_t y) { + int x_neg = kit_bi_is_neg(x); + int y_neg = kit_bi_is_neg(y); - kit_bigint_t const x_abs = x_neg ? kit_bi_neg(x) : x; - kit_bigint_t const y_abs = y_neg ? kit_bi_neg(y) : y; + kit_bigint_t x_abs = x_neg ? kit_bi_neg(x) : x; + kit_bigint_t y_abs = y_neg ? kit_bi_neg(y) : y; if (x_neg == y_neg) return kit_bi_udiv(x_abs, y_abs); @@ -412,8 +400,7 @@ static kit_bi_division_t kit_bi_div(kit_bigint_t const x, return z; } -static void kit_bi_serialize(kit_bigint_t const in, - uint8_t *const out) { +static void kit_bi_serialize(kit_bigint_t in, uint8_t *out) { ptrdiff_t i; assert(out != NULL); @@ -426,7 +413,7 @@ static void kit_bi_serialize(kit_bigint_t const in, } } -static kit_bigint_t kit_bi_deserialize(uint8_t const *const in) { +static kit_bigint_t kit_bi_deserialize(uint8_t *in) { ptrdiff_t i; kit_bigint_t out; @@ -440,46 +427,45 @@ static kit_bigint_t kit_bi_deserialize(uint8_t const *const in) { return out; } -static uint8_t kit_bin_digit(char const hex) { +static uint8_t kit_bin_digit(char hex) { assert(hex == '0' || hex == '1'); return hex == '1' ? 1 : 0; } -static kit_bigint_t kit_bi_from_bin(kit_str_t const bin) { +static kit_bigint_t kit_bi_from_bin(kit_str_t bin) { kit_bigint_t z; ptrdiff_t i; memset(&z, 0, sizeof z); for (i = 0; i < bin.size && i / 8 < KIT_BIGINT_SIZE; i++) { - uint8_t const digit = kit_bin_digit(bin.values[bin.size - i - 1]); + uint8_t digit = kit_bin_digit(bin.values[bin.size - i - 1]); z.v[i / 32] |= digit << (i % 32); } return z; } -static uint8_t kit_dec_digit(char const c) { +static uint8_t kit_dec_digit(char c) { assert('c' >= '0' && c <= '9'); return c >= '0' && c <= '9' ? (uint8_t) (c - '0') : 0; } -static kit_bigint_t kit_bi_from_dec(kit_str_t const dec) { +static kit_bigint_t kit_bi_from_dec(kit_str_t dec) { kit_bigint_t z = kit_bi_uint32(0); kit_bigint_t factor = kit_bi_uint32(1); ptrdiff_t i; for (i = 0; i < dec.size; i++) { - uint32_t const digit = kit_dec_digit( - dec.values[dec.size - i - 1]); - z = kit_bi_add(z, kit_bi_mul_uint32(factor, digit)); - factor = kit_bi_mul_uint32(factor, 10); + uint32_t digit = kit_dec_digit(dec.values[dec.size - i - 1]); + z = kit_bi_add(z, kit_bi_mul_uint32(factor, digit)); + factor = kit_bi_mul_uint32(factor, 10); } return z; } -static uint8_t kit_hex_digit(char const hex) { +static uint8_t kit_hex_digit(char hex) { assert((hex >= '0' && hex <= '9') || (hex >= 'a' && hex <= 'f') || (hex >= 'A' && hex <= 'F')); @@ -493,21 +479,21 @@ static uint8_t kit_hex_digit(char const hex) { return 0; } -static kit_bigint_t kit_bi_from_hex(kit_str_t const hex) { +static kit_bigint_t kit_bi_from_hex(kit_str_t hex) { kit_bigint_t z; ptrdiff_t i; memset(&z, 0, sizeof z); for (i = 0; i < hex.size && i / 2 < KIT_BIGINT_SIZE; i++) { - uint8_t const digit = kit_hex_digit(hex.values[hex.size - i - 1]); + uint8_t digit = kit_hex_digit(hex.values[hex.size - i - 1]); z.v[i / 8] |= digit << (4 * (i % 8)); } return z; } -static const uint8_t KIT_BASE32_DIGITS[] = { +static uint8_t KIT_BASE32_DIGITS[] = { ['1'] = 0, ['2'] = 1, ['3'] = 2, ['4'] = 3, ['5'] = 4, ['6'] = 5, ['7'] = 6, ['8'] = 7, ['9'] = 8, ['a'] = 9, ['b'] = 10, ['c'] = 11, ['d'] = 12, ['e'] = 13, ['f'] = 14, @@ -517,7 +503,7 @@ static const uint8_t KIT_BASE32_DIGITS[] = { ['y'] = 30, ['z'] = 31 }; -static uint8_t kit_base32_digit(char const c) { +static uint8_t kit_base32_digit(char c) { assert(c >= '\0' && c < sizeof KIT_BASE32_DIGITS); assert(c == '1' || KIT_BASE32_DIGITS[(size_t) (unsigned char) c] != 0); @@ -527,7 +513,7 @@ static uint8_t kit_base32_digit(char const c) { : 0; } -static kit_bigint_t kit_bi_from_base32(kit_str_t const base32) { +static kit_bigint_t kit_bi_from_base32(kit_str_t base32) { kit_bigint_t z; ptrdiff_t i; @@ -541,7 +527,7 @@ static kit_bigint_t kit_bi_from_base32(kit_str_t const base32) { return z; } -static const uint8_t KIT_BASE58_DIGITS[] = { +static uint8_t KIT_BASE58_DIGITS[] = { ['1'] = 0, ['2'] = 1, ['3'] = 2, ['4'] = 3, ['5'] = 4, ['6'] = 5, ['7'] = 6, ['8'] = 7, ['9'] = 8, ['A'] = 9, ['B'] = 10, ['C'] = 11, ['D'] = 12, ['E'] = 13, ['F'] = 14, @@ -556,7 +542,7 @@ static const uint8_t KIT_BASE58_DIGITS[] = { ['x'] = 55, ['y'] = 56, ['z'] = 57 }; -static uint8_t kit_base58_digit(char const c) { +static uint8_t kit_base58_digit(char c) { assert(c >= '\0' && c < sizeof KIT_BASE58_DIGITS); assert(c == '1' || KIT_BASE58_DIGITS[(size_t) (unsigned char) c] != 0); @@ -566,13 +552,13 @@ static uint8_t kit_base58_digit(char const c) { : 0; } -static kit_bigint_t kit_bi_from_base58(kit_str_t const base58) { +static kit_bigint_t kit_bi_from_base58(kit_str_t base58) { kit_bigint_t z = kit_bi_uint32(0); kit_bigint_t factor = kit_bi_uint32(1); ptrdiff_t i; for (i = 0; i < base58.size; i++) { - uint32_t const digit = kit_base58_digit( + uint32_t digit = kit_base58_digit( base58.values[base58.size - i - 1]); z = kit_bi_add(z, kit_bi_mul_uint32(factor, digit)); factor = kit_bi_mul_uint32(factor, 58); diff --git a/source/kit/condition_variable.h b/source/kit/condition_variable.h index 0e4630d..c27022e 100644 --- a/source/kit/condition_variable.h +++ b/source/kit/condition_variable.h @@ -27,7 +27,7 @@ void cnd_destroy(cnd_t *); int cnd_init(cnd_t *); int cnd_signal(cnd_t *); int cnd_timedwait(cnd_t *__restrict, mtx_t *__restrict mtx_, - const struct timespec *__restrict); + struct timespec const *__restrict); int cnd_wait(cnd_t *, mtx_t *mtx_); # ifdef __cplusplus diff --git a/source/kit/dynamic_array.h b/source/kit/dynamic_array.h index 35dddb3..526fb13 100644 --- a/source/kit/dynamic_array.h +++ b/source/kit/dynamic_array.h @@ -59,21 +59,21 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size, /* Append a value to dynamic array. */ -#define KIT_DA_APPEND(array_, value_) \ - do { \ - ptrdiff_t const kit_index_back_ = (array_).size; \ - KIT_DA_RESIZE((array_), kit_index_back_ + 1); \ - if (kit_index_back_ < (array_).size) \ - (array_).values[kit_index_back_] = (value_); \ +#define KIT_DA_APPEND(array_, value_) \ + do { \ + ptrdiff_t kit_index_back_ = (array_).size; \ + KIT_DA_RESIZE((array_), kit_index_back_ + 1); \ + if (kit_index_back_ < (array_).size) \ + (array_).values[kit_index_back_] = (value_); \ } while (0) /* Insert a value into dynamic array. */ #define KIT_DA_INSERT(array_, index_, value_) \ do { \ - ptrdiff_t kit_i_; \ - ptrdiff_t const kit_index_back_ = (array_).size; \ - ptrdiff_t const kit_indert_n_ = (index_); \ + ptrdiff_t kit_i_; \ + ptrdiff_t kit_index_back_ = (array_).size; \ + ptrdiff_t kit_indert_n_ = (index_); \ KIT_DA_RESIZE((array_), kit_index_back_ + 1); \ if (kit_index_back_ + 1 == (array_).size) { \ for (kit_i_ = kit_index_back_; kit_i_ > kit_indert_n_; \ diff --git a/source/kit/file.c b/source/kit/file.c index f767851..2d52e92 100644 --- a/source/kit/file.c +++ b/source/kit/file.c @@ -22,14 +22,13 @@ enum { PATH_BUF_SIZE = 4096 }; # define st_mtim st_mtimespec #endif -static int is_delim(char const c) { +static int is_delim(char c) { return c == '/' || c == '\\'; } -static kit_string_t kit_get_env_(char *const name, - kit_allocator_t const alloc) { - char *const val = getenv(name); - ptrdiff_t const size = val != NULL ? (ptrdiff_t) strlen(val) : 0; +static kit_string_t kit_get_env_(char *name, kit_allocator_t alloc) { + char *val = getenv(name); + ptrdiff_t size = val != NULL ? (ptrdiff_t) strlen(val) : 0; string_t result; DA_INIT(result, size, alloc); @@ -43,10 +42,9 @@ static kit_string_t kit_get_env_(char *const name, return result; } -kit_string_t kit_path_norm(kit_str_t const path, - kit_allocator_t const alloc) { - str_t const parent = SZ(".."); - ptrdiff_t i, i1, j; +kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t alloc) { + str_t parent = SZ(".."); + ptrdiff_t i, i1, j; string_t norm; DA_INIT(norm, path.size, alloc); @@ -61,8 +59,7 @@ kit_string_t kit_path_norm(kit_str_t const path, if (!is_delim(path.values[i])) continue; - str_t const s = { .size = i - i1 - 1, - .values = path.values + i1 + 1 }; + str_t s = { .size = i - i1 - 1, .values = path.values + i1 + 1 }; if (AR_EQUAL(s, parent)) { int have_parent = 0; ptrdiff_t i0 = 0; @@ -101,12 +98,11 @@ kit_string_t kit_path_norm(kit_str_t const path, return norm; } -kit_string_t kit_path_join(kit_str_t const left, - kit_str_t const right, - kit_allocator_t const alloc) { - ptrdiff_t left_size = left.size; - ptrdiff_t right_size = right.size; - char const *right_values = right.values; +kit_string_t kit_path_join(kit_str_t left, kit_str_t right, + kit_allocator_t alloc) { + ptrdiff_t left_size = left.size; + ptrdiff_t right_size = right.size; + char *right_values = right.values; if (left_size > 0 && is_delim(left.values[left_size - 1])) left_size--; @@ -129,7 +125,7 @@ kit_string_t kit_path_join(kit_str_t const left, return joined; } -kit_string_t kit_path_user(kit_allocator_t const alloc) { +kit_string_t kit_path_user(kit_allocator_t alloc) { kit_string_t user = kit_get_env_(KIT_ENV_HOME, alloc); if (user.size == 0) { DA_RESIZE(user, 1); @@ -167,8 +163,7 @@ kit_string_t kit_path_cache(kit_allocator_t alloc) { return cache; } -kit_str_t kit_path_index(kit_str_t const path, - ptrdiff_t const index) { +kit_str_t kit_path_index(kit_str_t path, ptrdiff_t index) { str_t s = { .size = 0, .values = NULL }; ptrdiff_t i0 = 0; @@ -198,7 +193,7 @@ kit_str_t kit_path_index(kit_str_t const path, return s; } -kit_str_t kit_path_take(kit_str_t const path, ptrdiff_t const count) { +kit_str_t kit_path_take(kit_str_t path, ptrdiff_t count) { str_t s = { .size = 0, .values = path.values }; ptrdiff_t i0 = 0; @@ -226,8 +221,7 @@ kit_str_t kit_path_take(kit_str_t const path, ptrdiff_t const count) { } #if defined(_WIN32) && !defined(__CYGWIN__) -static void win32_prepare_path_(WCHAR *const buf, - kit_str_t const path) { +static void win32_prepare_path_(WCHAR *buf, kit_str_t path) { assert(path.size == 0 || path.values != NULL); assert(path.size + 5 < PATH_BUF_SIZE); @@ -248,8 +242,7 @@ static void win32_prepare_path_(WCHAR *const buf, WCHAR buf[PATH_BUF_SIZE]; \ win32_prepare_path_(buf, path) #else -static void unix_prepare_path_(char *const buf, - kit_str_t const path) { +static void unix_prepare_path_(char *buf, kit_str_t path) { assert(path.size == 0 || path.values != NULL); assert(path.size + 1 < PATH_BUF_SIZE); @@ -262,7 +255,7 @@ static void unix_prepare_path_(char *const buf, unix_prepare_path_(buf, path) #endif -kit_status_t kit_file_create_folder(kit_str_t const path) { +kit_status_t kit_file_create_folder(kit_str_t path) { PREPARE_PATH_BUF_; #if defined(_WIN32) && !defined(__CYGWIN__) return CreateDirectoryW(buf, NULL) ? KIT_OK @@ -272,16 +265,16 @@ kit_status_t kit_file_create_folder(kit_str_t const path) { #endif } -kit_status_t kit_file_create_folder_recursive(kit_str_t const path) { +kit_status_t kit_file_create_folder_recursive(kit_str_t path) { ptrdiff_t i; for (i = 0;; i++) { - str_t const part = kit_path_take(path, i); - int const type = kit_path_type(part); + str_t part = kit_path_take(path, i); + int type = kit_path_type(part); if (type == KIT_PATH_FILE) return KIT_ERROR_FILE_ALREADY_EXISTS; if (type == KIT_PATH_NONE) { - kit_status_t const s = kit_file_create_folder(part); + kit_status_t s = kit_file_create_folder(part); if (s != KIT_OK) return s; } @@ -292,7 +285,7 @@ kit_status_t kit_file_create_folder_recursive(kit_str_t const path) { return KIT_OK; } -kit_status_t kit_file_remove(kit_str_t const path) { +kit_status_t kit_file_remove(kit_str_t path) { PREPARE_PATH_BUF_; #if defined(_WIN32) && !defined(__CYGWIN__) return DeleteFileW(buf) ? KIT_OK : KIT_ERROR_UNLINK_FAILED; @@ -301,7 +294,7 @@ kit_status_t kit_file_remove(kit_str_t const path) { #endif } -kit_status_t kit_file_remove_folder(kit_str_t const path) { +kit_status_t kit_file_remove_folder(kit_str_t path) { PREPARE_PATH_BUF_; #if defined(_WIN32) && !defined(__CYGWIN__) return RemoveDirectoryW(buf) ? KIT_OK : KIT_ERROR_RMDIR_FAILED; @@ -310,8 +303,8 @@ kit_status_t kit_file_remove_folder(kit_str_t const path) { #endif } -kit_status_t kit_file_remove_recursive(kit_str_t const path, - kit_allocator_t const alloc) { +kit_status_t kit_file_remove_recursive(kit_str_t path, + kit_allocator_t alloc) { int type = kit_path_type(path); ptrdiff_t i; @@ -325,8 +318,8 @@ kit_status_t kit_file_remove_recursive(kit_str_t const path, return list.status; } for (i = 0; i < list.files.size; i++) { - str_t const s = { .size = list.files.values[i].size, - .values = list.files.values[i].values }; + str_t s = { .size = list.files.values[i].size, + .values = list.files.values[i].values }; kit_file_remove_recursive(s, alloc); } kit_path_list_destroy(list); @@ -339,7 +332,7 @@ kit_status_t kit_file_remove_recursive(kit_str_t const path, return KIT_ERROR_FILE_DO_NOT_EXIST; } -kit_path_type_t kit_path_type(kit_str_t const path) { +kit_path_type_t kit_path_type(kit_str_t path) { PREPARE_PATH_BUF_; #if defined(_WIN32) && !defined(__CYGWIN__) if (PathFileExistsW(buf)) { @@ -360,7 +353,7 @@ kit_path_type_t kit_path_type(kit_str_t const path) { return KIT_PATH_NONE; } -kit_file_info_t kit_file_info(kit_str_t const path) { +kit_file_info_t kit_file_info(kit_str_t path) { kit_file_info_t result; memset(&result, 0, sizeof result); @@ -372,9 +365,8 @@ kit_file_info_t kit_file_info(kit_str_t const path) { if (f != INVALID_HANDLE_VALUE) { FILETIME ft; if (GetFileTime(f, NULL, NULL, &ft) != 0) { - uint64_t const nsec100 = (((uint64_t) ft.dwHighDateTime) - << 32) | - (uint64_t) ft.dwLowDateTime; + uint64_t nsec100 = (((uint64_t) ft.dwHighDateTime) << 32) | + (uint64_t) ft.dwLowDateTime; result.time_modified_sec = (int64_t) (nsec100 / 10000000); result.time_modified_nsec = (int32_t) (100 * (nsec100 % 10000000)); @@ -413,8 +405,8 @@ kit_file_info_t kit_file_info(kit_str_t const path) { return result; } -kit_path_list_t kit_file_enum_folder(kit_str_t const path, - kit_allocator_t const alloc) { +kit_path_list_t kit_file_enum_folder(kit_str_t path, + kit_allocator_t alloc) { PREPARE_PATH_BUF_; kit_path_list_t result = { .status = KIT_OK }; @@ -436,7 +428,7 @@ kit_path_list_t kit_file_enum_folder(kit_str_t const path, return result; do { - ptrdiff_t const n = result.files.size; + ptrdiff_t n = result.files.size; DA_RESIZE(result.files, n + 1); if (result.files.size != n + 1) { result.status = KIT_ERROR_BAD_ALLOC; @@ -472,14 +464,14 @@ kit_path_list_t kit_file_enum_folder(kit_str_t const path, if (entry->d_name[0] == '.') continue; - ptrdiff_t const n = result.files.size; + ptrdiff_t n = result.files.size; DA_RESIZE(result.files, n + 1); if (result.files.size != n + 1) { result.status = KIT_ERROR_BAD_ALLOC; break; } - ptrdiff_t const size = (ptrdiff_t) strlen(entry->d_name); + ptrdiff_t size = (ptrdiff_t) strlen(entry->d_name); DA_INIT(result.files.values[n], size, alloc); if (result.files.values[n].size != size) { DA_RESIZE(result.files, n); diff --git a/source/kit/input_buffer.c b/source/kit/input_buffer.c index c59031f..2ba3f4c 100644 --- a/source/kit/input_buffer.c +++ b/source/kit/input_buffer.c @@ -13,8 +13,8 @@ typedef struct { static internal_buffer_t *buf_init(kit_is_handle_t upstream, kit_allocator_t alloc) { assert(alloc.allocate != NULL); - internal_buffer_t *const buf = kit_alloc_dispatch( - alloc, KIT_ALLOCATE, sizeof *buf, 0, NULL); + internal_buffer_t *buf = kit_alloc_dispatch(alloc, KIT_ALLOCATE, + sizeof *buf, 0, NULL); if (buf != NULL) { memset(buf, 0, sizeof *buf); @@ -42,7 +42,7 @@ static void buf_acquire(void *p) { static void buf_release(void *p) { assert(p != NULL); - internal_buffer_t *const buf = (internal_buffer_t *) p; + internal_buffer_t *buf = (internal_buffer_t *) p; if (--buf->ref_count == 0) { DA_DESTROY(buf->data); @@ -54,21 +54,20 @@ static void buf_adjust(void *p, ptrdiff_t size) { assert(p != NULL); assert(size >= 0); - internal_buffer_t *const buf = (internal_buffer_t *) p; - ptrdiff_t const offset = buf->data.size; + internal_buffer_t *buf = (internal_buffer_t *) p; + ptrdiff_t offset = buf->data.size; if (offset < size) { DA_RESIZE(buf->data, size); - kit_out_str_t destination = { - .size = size - offset, .values = buf->data.values + offset - }; - ptrdiff_t n = KIT_IS_READ(buf->upstream, destination); + kit_str_t destination = { .size = size - offset, + .values = buf->data.values + offset }; + ptrdiff_t n = KIT_IS_READ(buf->upstream, destination); DA_RESIZE(buf->data, offset + n); } } static ptrdiff_t buf_read(void *p, ptrdiff_t offset, - kit_out_str_t destination) { + kit_str_t destination) { internal_buffer_t *buf = (internal_buffer_t *) p; ptrdiff_t n = destination.size < buf->data.size - offset ? destination.size @@ -105,11 +104,11 @@ kit_ib_handle_t kit_ib_read(kit_ib_handle_t buf, ptrdiff_t size) { if (next.data.size != size) next.status = KIT_ERROR_BAD_ALLOC; - kit_out_str_t destination = { .size = next.data.size, - .values = next.data.values }; - ptrdiff_t const n = buf_read(buf.internal, buf.offset, destination); - next.offset = buf.offset + n; - next.internal = buf.internal; + kit_str_t destination = { .size = next.data.size, + .values = next.data.values }; + ptrdiff_t n = buf_read(buf.internal, buf.offset, destination); + next.offset = buf.offset + n; + next.internal = buf.internal; DA_RESIZE(next.data, n); if (next.data.size != n) @@ -141,10 +140,10 @@ kit_ib_handle_t kit_ib_read_while( if (next.data.size != size + 1) next.status = KIT_ERROR_BAD_ALLOC; - kit_out_str_t destination = { .size = 1, - .values = next.data.values + size }; - ptrdiff_t const n = buf_read(buf.internal, buf.offset + size, - destination); + kit_str_t destination = { .size = 1, + .values = next.data.values + size }; + ptrdiff_t n = buf_read(buf.internal, buf.offset + size, + destination); kit_str_t data = { .size = size + 1, .values = next.data.values }; if (n != 1 || condition == NULL || condition(data) == 0) diff --git a/source/kit/input_stream.c b/source/kit/input_stream.c index 7d8476b..0ca2ee0 100644 --- a/source/kit/input_stream.c +++ b/source/kit/input_stream.c @@ -20,7 +20,7 @@ static int check_type(void *state, ptrdiff_t type) { return basic != NULL && basic->type == type; } -static ptrdiff_t read_str(void *state, kit_out_str_t destination) { +static ptrdiff_t read_str(void *state, kit_str_t destination) { if (!check_type(state, input_stream_str)) return 0; kit_is_state_str_t *str = (kit_is_state_str_t *) state; diff --git a/source/kit/input_stream.h b/source/kit/input_stream.h index 213d2ca..b3c3ae6 100644 --- a/source/kit/input_stream.h +++ b/source/kit/input_stream.h @@ -8,8 +8,8 @@ extern "C" { #endif -typedef ptrdiff_t (*kit_is_read_fn)(void *state, - kit_out_str_t destination); +typedef ptrdiff_t (*kit_is_read_fn)(void *state, + kit_str_t destination); typedef struct { void *state; diff --git a/source/kit/mersenne_twister_64.c b/source/kit/mersenne_twister_64.c index c40806e..9bf238f 100644 --- a/source/kit/mersenne_twister_64.c +++ b/source/kit/mersenne_twister_64.c @@ -5,9 +5,8 @@ #define UM 0xffffffff80000000ull #define LM 0x7fffffffull -void kit_mt64_init_array(kit_mt64_state_t *const state, - ptrdiff_t const size, - uint64_t const *const seed) { +void kit_mt64_init_array(kit_mt64_state_t *state, ptrdiff_t size, + uint64_t *seed) { ptrdiff_t i; for (i = 0; i < size && i < KIT_MT64_N; i++) state->mt[i] = seed[i]; for (state->index = size; state->index < KIT_MT64_N; state->index++) @@ -18,13 +17,12 @@ void kit_mt64_init_array(kit_mt64_state_t *const state, state->index); } -void kit_mt64_init(kit_mt64_state_t *const state, - uint64_t const seed) { +void kit_mt64_init(kit_mt64_state_t *state, uint64_t seed) { kit_mt64_init_array(state, 1, &seed); } -void kit_mt64_rotate(kit_mt64_state_t *const state) { - static uint64_t const mag01[2] = { 0ull, MATRIX_A }; +void kit_mt64_rotate(kit_mt64_state_t *state) { + static uint64_t mag01[2] = { 0ull, MATRIX_A }; uint64_t x; int i; @@ -48,7 +46,7 @@ void kit_mt64_rotate(kit_mt64_state_t *const state) { state->index = 0; } -uint64_t kit_mt64_generate(kit_mt64_state_t *const state) { +uint64_t kit_mt64_generate(kit_mt64_state_t *state) { if (state->index >= KIT_MT64_N) kit_mt64_rotate(state); diff --git a/source/kit/mersenne_twister_64.h b/source/kit/mersenne_twister_64.h index 548aea4..452fd73 100644 --- a/source/kit/mersenne_twister_64.h +++ b/source/kit/mersenne_twister_64.h @@ -22,7 +22,7 @@ typedef struct { } kit_mt64_state_t; void kit_mt64_init_array(kit_mt64_state_t *state, ptrdiff_t size, - uint64_t const *seed); + uint64_t *seed); void kit_mt64_init(kit_mt64_state_t *state, uint64_t seed); void kit_mt64_rotate(kit_mt64_state_t *state); uint64_t kit_mt64_generate(kit_mt64_state_t *state); diff --git a/source/kit/mutex.h b/source/kit/mutex.h index 919d92a..ada3a5f 100644 --- a/source/kit/mutex.h +++ b/source/kit/mutex.h @@ -41,7 +41,7 @@ void mtx_destroy(mtx_t *mtx_); int mtx_init(mtx_t *mtx_, int); int mtx_lock(mtx_t *mtx_); int mtx_timedlock(mtx_t *__restrict mtx_, - const struct timespec *__restrict); + struct timespec const *__restrict); int mtx_trylock(mtx_t *mtx_); int mtx_unlock(mtx_t *mtx_); diff --git a/source/kit/secure_random.c b/source/kit/secure_random.c index 022741a..20b3bf8 100644 --- a/source/kit/secure_random.c +++ b/source/kit/secure_random.c @@ -38,8 +38,7 @@ static void secure_random_fallback_init(void) { } #endif -static void secure_random_fallback(ptrdiff_t const size, - void *const data) { +static void secure_random_fallback(ptrdiff_t size, void *data) { #ifndef KIT_DISABLE_SYSTEM_THREADS call_once(&kit_secure_random_fallback_flag, secure_random_fallback_init); @@ -61,17 +60,16 @@ static void secure_random_fallback(ptrdiff_t const size, kit_mt64_state_t state; if (time_sec == 0 && time_nsec == 0) { - uint64_t const seed[] = { n, get_available_memory(), - (uint64_t) t.tv_sec, - (uint64_t) t.tv_nsec }; + uint64_t seed[] = { n, get_available_memory(), + (uint64_t) t.tv_sec, (uint64_t) t.tv_nsec }; kit_mt64_init_array(&state, sizeof seed / sizeof *seed, seed); } else { - uint64_t const seed[] = { n, - get_available_memory(), - (uint64_t) t.tv_sec, - (uint64_t) t.tv_nsec, - (uint64_t) t.tv_sec - time_sec, - (uint64_t) t.tv_nsec - time_nsec }; + uint64_t seed[] = { n, + get_available_memory(), + (uint64_t) t.tv_sec, + (uint64_t) t.tv_nsec, + (uint64_t) t.tv_sec - time_sec, + (uint64_t) t.tv_nsec - time_nsec }; kit_mt64_init_array(&state, sizeof seed / sizeof *seed, seed); } @@ -90,7 +88,7 @@ static void secure_random_fallback(ptrdiff_t const size, #endif } -void kit_secure_random(ptrdiff_t const size, void *const data) { +void kit_secure_random(ptrdiff_t size, void *data) { assert(size > 0); assert(data != NULL); @@ -108,7 +106,7 @@ void kit_secure_random(ptrdiff_t const size, void *const data) { return; } - size_t const n = fread(data, 1, size, f); + size_t n = fread(data, 1, size, f); fclose(f); assert(n == size); diff --git a/source/kit/secure_random.h b/source/kit/secure_random.h index 21399b7..5531a4a 100644 --- a/source/kit/secure_random.h +++ b/source/kit/secure_random.h @@ -5,8 +5,6 @@ # define _GNU_SOURCE #endif -#include "status.h" - #include <stddef.h> #include <stdint.h> diff --git a/source/kit/sha256.c b/source/kit/sha256.c index 9c99c53..beb1772 100644 --- a/source/kit/sha256.c +++ b/source/kit/sha256.c @@ -13,7 +13,7 @@ #define SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3)) #define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10)) -static const uint32_t k[64] = { +static uint32_t kit_sha256_k[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, @@ -29,8 +29,7 @@ static const uint32_t k[64] = { 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; -static void sha256_transform(uint32_t *const state, - const uint8_t *const data) { +static void kit_sha256_transform(uint32_t *state, uint8_t *data) { assert(state != NULL); assert(data != NULL); @@ -53,7 +52,7 @@ static void sha256_transform(uint32_t *const state, h = state[7]; for (i = 0; i < 64; ++i) { - t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i]; + t1 = h + EP1(e) + CH(e, f, g) + kit_sha256_k[i] + m[i]; t2 = EP0(a) + MAJ(a, b, c); h = g; g = f; @@ -75,8 +74,7 @@ static void sha256_transform(uint32_t *const state, state[7] += h; } -kit_sha256_hash_t kit_sha256(ptrdiff_t const in_size, - uint8_t const *const in_data) { +kit_sha256_hash_t kit_sha256(ptrdiff_t in_size, uint8_t *in_data) { assert(in_size >= 0); assert(in_data != NULL); @@ -98,7 +96,7 @@ kit_sha256_hash_t kit_sha256(ptrdiff_t const in_size, if (datalen != 64) continue; - sha256_transform(state, data); + kit_sha256_transform(state, data); bitlen += 512; datalen = 0; } @@ -112,7 +110,7 @@ kit_sha256_hash_t kit_sha256(ptrdiff_t const in_size, data[i++] = 0x80; while (i < 64) data[i++] = 0x00; - sha256_transform(state, data); + kit_sha256_transform(state, data); memset(data, 0, 56); } @@ -126,7 +124,7 @@ kit_sha256_hash_t kit_sha256(ptrdiff_t const in_size, data[57] = bitlen >> 48; data[56] = bitlen >> 56; - sha256_transform(state, data); + kit_sha256_transform(state, data); kit_sha256_hash_t hash; memset(&hash, 0, sizeof hash); diff --git a/source/kit/sha256.h b/source/kit/sha256.h index ce74ed9..182ee80 100644 --- a/source/kit/sha256.h +++ b/source/kit/sha256.h @@ -18,7 +18,7 @@ typedef struct { uint8_t v[KIT_SHA256_BLOCK_SIZE]; } kit_sha256_hash_t; -kit_sha256_hash_t kit_sha256(ptrdiff_t size, uint8_t const *data); +kit_sha256_hash_t kit_sha256(ptrdiff_t size, uint8_t *data); #ifdef __cplusplus } diff --git a/source/kit/sockets.h b/source/kit/sockets.h index 87e37e6..2d4af90 100644 --- a/source/kit/sockets.h +++ b/source/kit/sockets.h @@ -23,15 +23,6 @@ # define socket_t SOCKET # define socklen_t int -//# define EINPROGRESS WSAEINPROGRESS -//# define EWOULDBLOCK WSAEWOULDBLOCK -//# define EMSGSIZE WSAEMSGSIZE -//# define EISCONN WSAEISCONN -//# define ECONNRESET WSAECONNRESET -//# define EADDRINUSE WSAEADDRINUSE - -//# define errno ((int) WSAGetLastError()) - # ifdef __cplusplus extern "C" { # endif diff --git a/source/kit/string_ref.h b/source/kit/string_ref.h index 8cf1970..b171840 100644 --- a/source/kit/string_ref.h +++ b/source/kit/string_ref.h @@ -9,10 +9,8 @@ extern "C" { #endif -typedef KIT_AR_MUT(char) kit_string_mut_t; typedef KIT_AR(char) kit_string_ref_t; -typedef kit_string_mut_t kit_out_str_t; typedef kit_string_ref_t kit_str_t; #ifdef __GNUC__ @@ -23,9 +21,8 @@ typedef kit_string_ref_t kit_str_t; # pragma GCC optimize("O3") #endif -static kit_str_t kit_str(ptrdiff_t const size, - char const *const static_string) { - kit_str_t const s = { .size = size, .values = static_string }; +static kit_str_t kit_str(ptrdiff_t size, char *static_string) { + kit_str_t s = { .size = size, .values = static_string }; return s; } @@ -33,16 +30,16 @@ static kit_str_t kit_str(ptrdiff_t const size, * Not thread safe. * Use with caution. */ -static char const *kit_make_bs(kit_str_t const s) { +static char *kit_make_bs(kit_str_t s) { static char buf[8][4096]; static int index = 0; ptrdiff_t n = s.size; if (n > 4095) n = 4095; memcpy(buf[index], s.values, n); - buf[index][n] = '\0'; - char const *result = buf[index]; - index = (index + 1) % 8; + buf[index][n] = '\0'; + char *result = buf[index]; + index = (index + 1) % 8; return result; } @@ -62,9 +59,7 @@ static char const *kit_make_bs(kit_str_t const s) { #ifndef KIT_DISABLE_SHORT_NAMES # define BS(string_) kit_make_bs(KIT_WRAP_STR(string_)) -# define string_mut_t kit_string_mut_t # define string_ref_t kit_string_ref_t -# define out_str_t kit_out_str_t # define str_t kit_str_t # define SZ KIT_SZ diff --git a/source/kit/thread.posix.c b/source/kit/thread.posix.c index ee5d439..79a71e8 100644 --- a/source/kit/thread.posix.c +++ b/source/kit/thread.posix.c @@ -75,7 +75,7 @@ int cnd_signal(cnd_t *cond) { } int cnd_timedwait(cnd_t *cond, mtx_t *mtx, - const struct timespec *abs_time) { + struct timespec const *abs_time) { int rt; assert(mtx != NULL); diff --git a/source/tests/_static.c b/source/tests/_static.c index 4b7543d..54587d3 100644 --- a/source/tests/_static.c +++ b/source/tests/_static.c @@ -43,7 +43,7 @@ #include "string_ref.test.c" #undef KIT_TEST_FILE -#include "test_duration.test.c" +#include "duration.test.c" #undef KIT_TEST_FILE #include "thread.test.c" diff --git a/source/tests/array_ref.test.c b/source/tests/array_ref.test.c index da20aa0..cb19850 100644 --- a/source/tests/array_ref.test.c +++ b/source/tests/array_ref.test.c @@ -3,7 +3,7 @@ #define KIT_TEST_FILE array_ref #include "../kit_test/test.h" -TEST("array ref const wrap") { +TEST("array ref wrap") { int foo[] = { 1, 2, 3 }; AR_WRAP(ref, int, foo); @@ -15,7 +15,7 @@ TEST("array ref const wrap") { TEST("array ref wrap") { int foo[] = { 1, 2, 3 }; - AR_MUT_WRAP(ref, int, foo); + AR_WRAP(ref, int, foo); REQUIRE(ref.size == 3); REQUIRE(ref.values[0] == 1); @@ -36,7 +36,7 @@ TEST("array ref equal") { REQUIRE(AR_EQUAL(foo_ref, bar_ref)); } -static int compare(int const *left, int const *right) { +static int compare(int *left, int *right) { return *left - *right; } diff --git a/source/tests/test_duration.test.c b/source/tests/duration.test.c index 27384b4..56919e4 100644 --- a/source/tests/test_duration.test.c +++ b/source/tests/duration.test.c @@ -1,4 +1,4 @@ -#define KIT_TEST_FILE test_duration +#define KIT_TEST_FILE duration #include "../kit_test/test.h" #if defined(_WIN32) && !defined(__CYGWIN__) diff --git a/source/tests/input_stream.test.c b/source/tests/input_stream.test.c index 61c2254..46ee0dd 100644 --- a/source/tests/input_stream.test.c +++ b/source/tests/input_stream.test.c @@ -12,8 +12,8 @@ TEST("input stream wrap string") { is_handle_t in = IS_WRAP_STRING(foo_ref); - char buf[4]; - out_str_t buf_ref = { .size = sizeof(buf), .values = buf }; + char buf[4]; + str_t buf_ref = { .size = sizeof(buf), .values = buf }; REQUIRE(IS_READ(in, buf_ref) == buf_ref.size); REQUIRE(AR_EQUAL(foo_ref, bar_ref)); diff --git a/source/tests/lower_bound.test.c b/source/tests/lower_bound.test.c index 3b62325..56ec816 100644 --- a/source/tests/lower_bound.test.c +++ b/source/tests/lower_bound.test.c @@ -8,7 +8,7 @@ static int kit_less_int(int left, int right) { return left < right; } -static int kit_less_int_ref(int const *left, int const *right) { +static int kit_less_int_ref(int *left, int *right) { return *left < *right; } @@ -21,8 +21,8 @@ TEST("lower bound empty") { } TEST("lower bound single left") { - int const v[1] = { 42 }; - AR(int) ref = { .size = 1, .values = v }; + int v[1] = { 42 }; + AR(int) ref = { .size = 1, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 42, kit_less_int); @@ -30,8 +30,8 @@ TEST("lower bound single left") { } TEST("lower bound single right") { - int const v[1] = { 42 }; - AR(int) ref = { .size = 1, .values = v }; + int v[1] = { 42 }; + AR(int) ref = { .size = 1, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 43, kit_less_int); @@ -39,8 +39,8 @@ TEST("lower bound single right") { } TEST("lower bound first of four") { - int const v[4] = { 1, 2, 3, 4 }; - AR(int) ref = { .size = 4, .values = v }; + int v[4] = { 1, 2, 3, 4 }; + AR(int) ref = { .size = 4, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 1, kit_less_int); @@ -48,8 +48,8 @@ TEST("lower bound first of four") { } TEST("lower bound second of four") { - int const v[4] = { 1, 2, 3, 4 }; - AR(int) ref = { .size = 4, .values = v }; + int v[4] = { 1, 2, 3, 4 }; + AR(int) ref = { .size = 4, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 2, kit_less_int); @@ -57,8 +57,8 @@ TEST("lower bound second of four") { } TEST("lower bound third of four") { - int const v[4] = { 1, 2, 3, 4 }; - AR(int) ref = { .size = 4, .values = v }; + int v[4] = { 1, 2, 3, 4 }; + AR(int) ref = { .size = 4, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 3, kit_less_int); @@ -66,8 +66,8 @@ TEST("lower bound third of four") { } TEST("lower bound forth of four") { - int const v[4] = { 1, 2, 3, 4 }; - AR(int) ref = { .size = 4, .values = v }; + int v[4] = { 1, 2, 3, 4 }; + AR(int) ref = { .size = 4, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 4, kit_less_int); @@ -75,8 +75,8 @@ TEST("lower bound forth of four") { } TEST("lower bound fifth of four") { - int const v[4] = { 1, 2, 3, 4 }; - AR(int) ref = { .size = 4, .values = v }; + int v[4] = { 1, 2, 3, 4 }; + AR(int) ref = { .size = 4, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 5, kit_less_int); @@ -84,8 +84,8 @@ TEST("lower bound fifth of four") { } TEST("lower bound first of five") { - int const v[5] = { 1, 2, 3, 4, 5 }; - AR(int) ref = { .size = 5, .values = v }; + int v[5] = { 1, 2, 3, 4, 5 }; + AR(int) ref = { .size = 5, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 1, kit_less_int); @@ -93,8 +93,8 @@ TEST("lower bound first of five") { } TEST("lower bound second of five") { - int const v[5] = { 1, 2, 3, 4, 5 }; - AR(int) ref = { .size = 5, .values = v }; + int v[5] = { 1, 2, 3, 4, 5 }; + AR(int) ref = { .size = 5, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 2, kit_less_int); @@ -102,8 +102,8 @@ TEST("lower bound second of five") { } TEST("lower bound third of five") { - int const v[5] = { 1, 2, 3, 4, 5 }; - AR(int) ref = { .size = 5, .values = v }; + int v[5] = { 1, 2, 3, 4, 5 }; + AR(int) ref = { .size = 5, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 3, kit_less_int); @@ -111,8 +111,8 @@ TEST("lower bound third of five") { } TEST("lower bound forth of five") { - int const v[5] = { 1, 2, 3, 4, 5 }; - AR(int) ref = { .size = 5, .values = v }; + int v[5] = { 1, 2, 3, 4, 5 }; + AR(int) ref = { .size = 5, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 4, kit_less_int); @@ -120,8 +120,8 @@ TEST("lower bound forth of five") { } TEST("lower bound fifth of five") { - int const v[5] = { 1, 2, 3, 4, 5 }; - AR(int) ref = { .size = 5, .values = v }; + int v[5] = { 1, 2, 3, 4, 5 }; + AR(int) ref = { .size = 5, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 5, kit_less_int); @@ -129,8 +129,8 @@ TEST("lower bound fifth of five") { } TEST("lower bound sixth of five") { - int const v[5] = { 1, 2, 3, 4, 5 }; - AR(int) ref = { .size = 5, .values = v }; + int v[5] = { 1, 2, 3, 4, 5 }; + AR(int) ref = { .size = 5, .values = v }; ptrdiff_t index; LOWER_BOUND(index, ref, 6, kit_less_int); @@ -138,9 +138,9 @@ TEST("lower bound sixth of five") { } TEST("lower bound ref first of four") { - int const v[4] = { 1, 2, 3, 4 }; - int const value = 1; - AR(int) ref = { .size = 4, .values = v }; + int v[4] = { 1, 2, 3, 4 }; + int value = 1; + AR(int) ref = { .size = 4, .values = v }; ptrdiff_t index; LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref); @@ -148,9 +148,9 @@ TEST("lower bound ref first of four") { } TEST("lower bound ref second of four") { - int const v[4] = { 1, 2, 3, 4 }; - int const value = 2; - AR(int) ref = { .size = 4, .values = v }; + int v[4] = { 1, 2, 3, 4 }; + int value = 2; + AR(int) ref = { .size = 4, .values = v }; ptrdiff_t index; LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref); @@ -158,9 +158,9 @@ TEST("lower bound ref second of four") { } TEST("lower bound ref fifth of five") { - int const v[5] = { 1, 2, 3, 4, 5 }; - int const value = 5; - AR(int) ref = { .size = 5, .values = v }; + int v[5] = { 1, 2, 3, 4, 5 }; + int value = 5; + AR(int) ref = { .size = 5, .values = v }; ptrdiff_t index; LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref); @@ -168,9 +168,9 @@ TEST("lower bound ref fifth of five") { } TEST("lower bound ref sixth of five") { - int const v[5] = { 1, 2, 3, 4, 5 }; - int const value = 6; - AR(int) ref = { .size = 5, .values = v }; + int v[5] = { 1, 2, 3, 4, 5 }; + int value = 6; + AR(int) ref = { .size = 5, .values = v }; ptrdiff_t index; LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref); diff --git a/source/tests/main.test.c b/source/tests/main.test.c index 2a41d4b..790c8c2 100644 --- a/source/tests/main.test.c +++ b/source/tests/main.test.c @@ -1,9 +1,10 @@ +#include "../kit/status.h" #include "../kit_test/bench.h" #include "../kit_test/test.h" int main(int argc, char **argv) { int status = run_tests(argc, argv); - if (status == 0) + if (status == KIT_OK) status = run_benchmarks(argc, argv); return status; } diff --git a/source/tests/move_back.test.c b/source/tests/move_back.test.c index f08d190..f3f6dc4 100644 --- a/source/tests/move_back.test.c +++ b/source/tests/move_back.test.c @@ -3,19 +3,19 @@ #define KIT_TEST_FILE move_back #include "../kit_test/test.h" -static int is_equal(int const x, int const y) { +static int is_equal(int x, int y) { return x == y; } -static int is_equal_ref(int const *const x, int const y) { +static int is_equal_ref(int *x, int y) { return *x == y; } -static int is_even(int const x, int const _) { +static int is_even(int x, int _) { return (x % 2) == 0; } -static int is_even_ref(int const *const x, int const _) { +static int is_even_ref(int *x, int _) { return (*x % 2) == 0; } diff --git a/source/tests/cpp.cpp b/source/tests/test_cpp.cpp index 8b762e4..8b762e4 100644 --- a/source/tests/cpp.cpp +++ b/source/tests/test_cpp.cpp diff --git a/source/tests/signals.cpp b/source/tests/test_signals.cpp index 0f6d77a..0f6d77a 100644 --- a/source/tests/signals.cpp +++ b/source/tests/test_signals.cpp diff --git a/source/tests/too_many_assertions.c b/source/tests/test_too_many_assertions.c index 662207d..662207d 100644 --- a/source/tests/too_many_assertions.c +++ b/source/tests/test_too_many_assertions.c diff --git a/source/tests/too_many_tests.c b/source/tests/test_too_many_tests.c index d4842e4..d4842e4 100644 --- a/source/tests/too_many_tests.c +++ b/source/tests/test_too_many_tests.c |