commit c438f5c73658f40ac49da65ea8ed62f3dc551953 parent 7b43c293feca24aed7af327fdb518530995bfd01 Author: vaplv <vaplv@free.fr> Date: Mon, 2 Jan 2023 11:44:31 +0100 Merge branch 'release_0.13' Diffstat:
123 files changed, 670 insertions(+), 300 deletions(-)
diff --git a/README.md b/README.md @@ -17,6 +17,18 @@ project can be now edited, built, tested and installed as any CMake project. ## Release notes +### Version 0.13 + +- Complete rewrite of sha256 calculations. To compute a hash following the + sha256 cipher, we no longer rely on data pre-cut by the caller. As in GnuPG + or the GNU C library, sha256 calculations now use a context that the caller + updates with the data to digest. This rewrite introduces an API break. +- Add `size_to_cstr` function: it fills a C string with human readable text + corresponding to a given size (in bytes). For example, 1024 can be formatted + as `1 KB` or `1024 B`, depending on the input arguments. +- Fix `time_dump` function: correct invalid memory write as well as incorrect + text formatting when write time is 0 and `TIME_DAY` flag is set. + ### Version 0.12.1 - Fix compilation warnings with GCC 11. @@ -153,7 +165,7 @@ project can be now edited, built, tested and installed as any CMake project. ## License -Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr). RSys free software +Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr). RSys is free software released under the GPL v3+ license: GNU GPL version 3 or later. You are welcome to redistribute it under certain conditions; refer to the COPYING files for details. diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +# Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) # # This CMake script is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -38,8 +38,8 @@ include(rcmake) # Configure and define targets ################################################################################ set(VERSION_MAJOR 0) -set(VERSION_MINOR 12) -set(VERSION_PATCH 1) +set(VERSION_MINOR 13) +set(VERSION_PATCH 0) set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) set(RSYS_FILES_SRC diff --git a/src/algorithm.h b/src/algorithm.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/binary_heap.h b/src/binary_heap.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/clock_time.c b/src/clock_time.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -157,7 +157,7 @@ time_dump ASSERT(time && (!max_dump_len || dump)); if(real_dump_len) *real_dump_len = 0; - if(dump) dump[0] = '\0'; + if(max_dump_len > 0) dump[0] = '\0'; if(!flag) return; #define DUMP(Time, Suffix) \ @@ -216,7 +216,7 @@ time_dump /* Remove last space */ if(real_dump_len) *real_dump_len -= 1; - if(dump) { + if(max_dump_len > 0) { size_t dump_len = strlen(dump); if(!dump_len && flag) { if(flag & TIME_NSEC) { DUMP(0, "nsec"); @@ -225,6 +225,7 @@ time_dump } else if(flag & TIME_SEC) { DUMP(0, "sec"); } else if(flag & TIME_MIN) { DUMP(0, "min"); } else if(flag & TIME_HOUR) { DUMP(0, "hour"); + } else if(flag & TIME_DAY) { DUMP(0, "day"); } dump_len = strlen(dump); } diff --git a/src/clock_time.h b/src/clock_time.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -72,9 +72,9 @@ RSYS_API void time_dump (const struct time* time, int flag, /* Combination of time_unit or TIME_ALL */ - size_t* real_dump_len, /* May be NULL */ + size_t* real_dump_len, /* May be NULL (wo '\0') */ char* dump, /* May be NULL */ - size_t max_dump_len); + size_t max_dump_len); /* With '\0' */ END_DECLS diff --git a/src/condition.h b/src/condition.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/cstr.c b/src/cstr.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -28,7 +28,7 @@ #define CSTR_LIST_SUFFIX uint #include "cstr_to_list.h" -RSYS_API res_T +res_T cstr_parse_list (const char* str, const char delimiter, @@ -68,3 +68,94 @@ error: goto exit; } +void +size_to_cstr + (const size_t size_byte, + const int flag, /* Combination of size_unit */ + size_t* real_cstr_len, /* May be NULL */ + char* cstr, /* May be NULL */ + const size_t sizeof_cstr) +{ + size_t available_cstr_space = sizeof_cstr; + size_t size = size_byte; + char* dst = cstr; + ASSERT((!sizeof_cstr || cstr)); + ASSERT(size_byte <= INT64_MAX); + + if(real_cstr_len) *real_cstr_len = 0; + if(sizeof_cstr > 0) cstr[0] = '\0'; + if(!flag) return; + + #define NBYTES_PER_KBYTE ((size_t)1024) + #define NBYTES_PER_MBYTE ((size_t)1024 * NBYTES_PER_KBYTE) + #define NBYTES_PER_GBYTE ((size_t)1024 * NBYTES_PER_MBYTE) + #define NBYTES_PER_TBYTE ((size_t)1024 * NBYTES_PER_GBYTE) + + #define DUMP(Size, Suffix) \ + { \ + const int len = snprintf \ + (dst, available_cstr_space, "%li %s ", (long)Size, Suffix); \ + ASSERT(len >= 0); \ + if(real_cstr_len) { \ + *real_cstr_len += (size_t)len; \ + } \ + if((size_t)len < available_cstr_space) { \ + dst += len; \ + available_cstr_space -= (size_t)len; \ + } else if(dst) { \ + available_cstr_space = 0; \ + dst = NULL; \ + } \ + } (void) 0 + + if(flag & SIZE_TBYTE) { + const size_t nb_teras = size / NBYTES_PER_TBYTE; + if(nb_teras) DUMP(nb_teras, "TB"); + size -= nb_teras * NBYTES_PER_TBYTE; + } + if(flag & SIZE_GBYTE) { + const size_t nb_gigas = size / NBYTES_PER_GBYTE; + if(nb_gigas) DUMP(nb_gigas, "GB"); + size -= nb_gigas * NBYTES_PER_GBYTE; + } + if(flag & SIZE_MBYTE) { + const size_t nb_megas = size / NBYTES_PER_MBYTE; + if(nb_megas) DUMP(nb_megas, "MB"); + size -= nb_megas * NBYTES_PER_MBYTE; + } + if(flag & SIZE_KBYTE) { + const size_t nb_kilos = size / NBYTES_PER_KBYTE; + if(nb_kilos) DUMP(nb_kilos, "KB"); + size -= nb_kilos * NBYTES_PER_KBYTE; + } + if(flag & SIZE_BYTE) { + if(size) DUMP(size, "B"); + } + + /* Remove last space */ + if(real_cstr_len) *real_cstr_len -= 1; + + if(sizeof_cstr > 0) { + size_t cstr_len = strlen(cstr); + + if(!cstr_len && flag) { + ASSERT(size_byte == 0); + if(flag & SIZE_BYTE) { DUMP(0, "B"); + } else if(flag & SIZE_KBYTE) { DUMP(0, "KB"); + } else if(flag & SIZE_MBYTE) { DUMP(0, "MB"); + } else if(flag & SIZE_GBYTE) { DUMP(0, "GB"); + } else if(flag & SIZE_TBYTE) { DUMP(0, "TB"); + } + cstr_len = strlen(cstr); + } + /* Remove last space */ + if(cstr[cstr_len-1] == ' ') { + cstr[cstr_len-1] = '\0'; + } + } + #undef NBYTES_PER_KBYTE + #undef NBYTES_PER_MBYTE + #undef NBYTES_PER_GBYTE + #undef NBYTES_PER_TBYTE + #undef DUMP +} diff --git a/src/cstr.h b/src/cstr.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -25,6 +25,15 @@ #include <limits.h> #include <stdlib.h> +enum size_unit { + SIZE_BYTE = BIT(0), + SIZE_KBYTE = BIT(1), + SIZE_MBYTE = BIT(2), + SIZE_GBYTE = BIT(3), + SIZE_TBYTE = BIT(4), + SIZE_ALL = -1 +}; + static INLINE res_T cstr_to_double(const char* str, double* dst) { @@ -188,6 +197,14 @@ cstr_to_list_uint size_t* length, const size_t max_length); +RSYS_API void +size_to_cstr + (const size_t size, + const int flag, /* Combination of size_unit */ + size_t* real_cstr_len, /* May be NULL (does not handle null char) */ + char* cstr, /* May be NULL */ + const size_t sizeof_cstr); /* Include null char */ + END_DECLS #endif /* CSTR_H */ diff --git a/src/cstr_to_list.h b/src/cstr_to_list.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/double2.h b/src/double2.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/double22.h b/src/double22.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/double3.h b/src/double3.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/double33.h b/src/double33.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/double4.h b/src/double4.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/double44.h b/src/double44.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array.h b/src/dynamic_array.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_char.h b/src/dynamic_array_char.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_double.h b/src/dynamic_array_double.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_float.h b/src/dynamic_array_float.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_int.h b/src/dynamic_array_int.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_size_t.h b/src/dynamic_array_size_t.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_str.h b/src/dynamic_array_str.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_u32.h b/src/dynamic_array_u32.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_u64.h b/src/dynamic_array_u64.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_uchar.h b/src/dynamic_array_uchar.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/dynamic_array_uint.h b/src/dynamic_array_uint.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/endianness.h b/src/endianness.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/float2.h b/src/float2.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/float22.h b/src/float22.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/float3.h b/src/float3.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/float33.h b/src/float33.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/float4.h b/src/float4.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/float44.h b/src/float44.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/free_list.h b/src/free_list.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/hash.c b/src/hash.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -43,177 +43,234 @@ static const uint32_t k[64] = { 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; -/******************************************************************************* - * Helper functions - ******************************************************************************/ -/* Right rotation */ -static FINLINE uint32_t -rrot(const uint32_t ui, const unsigned int count) -{ - ASSERT(count <= 32); - return ui >> count | ui << (32 - count); -} - +/* Most of this code comes from GnuPG's cipher/sha1.c */ static void -get_chunk512(char dst[64], const size_t ichunk512, void* ctx) +sha256_process_chunk(struct sha256_ctx* ctx, const char chunk[64]) { - struct buffer* buf = ctx; - const char* chunk = NULL; - size_t chunk_sz = 0; - const size_t offset = ichunk512*(sizeof(char[64])); - ASSERT(buf && offset < buf->len); - - chunk = buf->mem + offset; - chunk_sz = MMIN(buf->len - offset, sizeof(char[64])); - memcpy(dst, chunk, chunk_sz); + uint32_t w[16]; + uint32_t a, b, c, d, e, f, g, h; + uint32_t i; + + uint32_t tm; + uint32_t t0, t1; + + ASSERT(ctx); + + FOR_EACH(i, 0, 16) { + w[i] = big_endian_32(((uint32_t*)chunk)[i]); + } + + a = ctx->state[0]; + b = ctx->state[1]; + c = ctx->state[2]; + d = ctx->state[3]; + e = ctx->state[4]; + f = ctx->state[5]; + g = ctx->state[6]; + h = ctx->state[7]; + + #define ROL(X, N) (((X) << (N)) | ((X) >> (32 - (N)))) + #define S0(X) (ROL(X,25)^ROL(X,14)^(X>>3)) + #define S1(X) (ROL(X,15)^ROL(X,13)^(X>>10)) + #define SS0(X) (ROL(X,30)^ROL(X,19)^ROL(X,10)) + #define SS1(X) (ROL(X,26)^ROL(X,21)^ROL(X,7)) + #define M(I) (tm = S1(w[(I- 2)&0x0f]) + w[(I-7)&0x0f] \ + + S0(w[(I-15)&0x0f]) + w[I&0x0f], w[I&0x0f] = tm) + #define F2(A, B, C) (( A & B ) | (C & (A | B))) + #define F1(E, F, G) (G ^ (E & (F ^ G))) + #define R(A, B, C, D, E, F, G, H, K, M) { \ + t0 = SS0(A) + F2(A, B, C); \ + t1 = H + SS1(E) + F1(E, F, G) + K + M; \ + D += t1; \ + H = t0 + t1; \ + } (void)0 + + R( a, b, c, d, e, f, g, h, k[ 0], w[ 0] ); + R( h, a, b, c, d, e, f, g, k[ 1], w[ 1] ); + R( g, h, a, b, c, d, e, f, k[ 2], w[ 2] ); + R( f, g, h, a, b, c, d, e, k[ 3], w[ 3] ); + R( e, f, g, h, a, b, c, d, k[ 4], w[ 4] ); + R( d, e, f, g, h, a, b, c, k[ 5], w[ 5] ); + R( c, d, e, f, g, h, a, b, k[ 6], w[ 6] ); + R( b, c, d, e, f, g, h, a, k[ 7], w[ 7] ); + R( a, b, c, d, e, f, g, h, k[ 8], w[ 8] ); + R( h, a, b, c, d, e, f, g, k[ 9], w[ 9] ); + R( g, h, a, b, c, d, e, f, k[10], w[10] ); + R( f, g, h, a, b, c, d, e, k[11], w[11] ); + R( e, f, g, h, a, b, c, d, k[12], w[12] ); + R( d, e, f, g, h, a, b, c, k[13], w[13] ); + R( c, d, e, f, g, h, a, b, k[14], w[14] ); + R( b, c, d, e, f, g, h, a, k[15], w[15] ); + R( a, b, c, d, e, f, g, h, k[16], M(16) ); + R( h, a, b, c, d, e, f, g, k[17], M(17) ); + R( g, h, a, b, c, d, e, f, k[18], M(18) ); + R( f, g, h, a, b, c, d, e, k[19], M(19) ); + R( e, f, g, h, a, b, c, d, k[20], M(20) ); + R( d, e, f, g, h, a, b, c, k[21], M(21) ); + R( c, d, e, f, g, h, a, b, k[22], M(22) ); + R( b, c, d, e, f, g, h, a, k[23], M(23) ); + R( a, b, c, d, e, f, g, h, k[24], M(24) ); + R( h, a, b, c, d, e, f, g, k[25], M(25) ); + R( g, h, a, b, c, d, e, f, k[26], M(26) ); + R( f, g, h, a, b, c, d, e, k[27], M(27) ); + R( e, f, g, h, a, b, c, d, k[28], M(28) ); + R( d, e, f, g, h, a, b, c, k[29], M(29) ); + R( c, d, e, f, g, h, a, b, k[30], M(30) ); + R( b, c, d, e, f, g, h, a, k[31], M(31) ); + R( a, b, c, d, e, f, g, h, k[32], M(32) ); + R( h, a, b, c, d, e, f, g, k[33], M(33) ); + R( g, h, a, b, c, d, e, f, k[34], M(34) ); + R( f, g, h, a, b, c, d, e, k[35], M(35) ); + R( e, f, g, h, a, b, c, d, k[36], M(36) ); + R( d, e, f, g, h, a, b, c, k[37], M(37) ); + R( c, d, e, f, g, h, a, b, k[38], M(38) ); + R( b, c, d, e, f, g, h, a, k[39], M(39) ); + R( a, b, c, d, e, f, g, h, k[40], M(40) ); + R( h, a, b, c, d, e, f, g, k[41], M(41) ); + R( g, h, a, b, c, d, e, f, k[42], M(42) ); + R( f, g, h, a, b, c, d, e, k[43], M(43) ); + R( e, f, g, h, a, b, c, d, k[44], M(44) ); + R( d, e, f, g, h, a, b, c, k[45], M(45) ); + R( c, d, e, f, g, h, a, b, k[46], M(46) ); + R( b, c, d, e, f, g, h, a, k[47], M(47) ); + R( a, b, c, d, e, f, g, h, k[48], M(48) ); + R( h, a, b, c, d, e, f, g, k[49], M(49) ); + R( g, h, a, b, c, d, e, f, k[50], M(50) ); + R( f, g, h, a, b, c, d, e, k[51], M(51) ); + R( e, f, g, h, a, b, c, d, k[52], M(52) ); + R( d, e, f, g, h, a, b, c, k[53], M(53) ); + R( c, d, e, f, g, h, a, b, k[54], M(54) ); + R( b, c, d, e, f, g, h, a, k[55], M(55) ); + R( a, b, c, d, e, f, g, h, k[56], M(56) ); + R( h, a, b, c, d, e, f, g, k[57], M(57) ); + R( g, h, a, b, c, d, e, f, k[58], M(58) ); + R( f, g, h, a, b, c, d, e, k[59], M(59) ); + R( e, f, g, h, a, b, c, d, k[60], M(60) ); + R( d, e, f, g, h, a, b, c, k[61], M(61) ); + R( c, d, e, f, g, h, a, b, k[62], M(62) ); + R( b, c, d, e, f, g, h, a, k[63], M(63) ); + + ctx->state[0] += a; + ctx->state[1] += b; + ctx->state[2] += c; + ctx->state[3] += d; + ctx->state[4] += e; + ctx->state[5] += f; + ctx->state[6] += g; + ctx->state[7] += h; } /******************************************************************************* * Exported functions ******************************************************************************/ void -hash_sha256 - (const void* data, - const size_t len, - hash256_T hash) +sha256_ctx_init(struct sha256_ctx* ctx) { - struct chunked_data_desc desc = CHUNKED_DATA_DESC_NULL; - struct buffer buf; + /* Initial hash values: first 32 bits of the fractional parts of the square + * roots of the first 8 primes 2..19 */ + ctx->state[0] = 0x6a09e667; + ctx->state[1] = 0xbb67ae85; + ctx->state[2] = 0x3c6ef372; + ctx->state[3] = 0xa54ff53a; + ctx->state[4] = 0x510e527f; + ctx->state[5] = 0x9b05688c; + ctx->state[6] = 0x1f83d9ab; + ctx->state[7] = 0x5be0cd19; + ctx->len = 0; + ctx->nbits = 0; +} - buf.mem = data; - buf.len = len; +void +sha256_ctx_update + (struct sha256_ctx* ctx, + const char* bytes, + size_t len) +{ + size_t n; + uint32_t i; + ASSERT(ctx); + ASSERT(bytes || !len); + + if(ctx->len) { + n = MMIN(64 - ctx->len, len); + memcpy(ctx->chunk + ctx->len, bytes, n); + ctx->len += (uint32_t)n; + bytes += n; + len -= n; + + if(ctx->len == 64) { + sha256_process_chunk(ctx, ctx->chunk); + ctx->nbits += 512; + ctx->len = 0; + } + } - desc.get_chunk512 = get_chunk512; - desc.size = len; - desc.context = &buf; + if(len >= 64) { + n = len / 64; + FOR_EACH(i, 0, n) { + sha256_process_chunk(ctx, bytes); + bytes += 64; + } + ctx->nbits += n * 512; + len -= n * 64; + } - hash_sha256_chunked_data(&desc, hash); + if(len) { + memcpy(ctx->chunk, bytes, len); + ctx->len = (uint32_t)len; + } } void -hash_sha256_chunked_data - (struct chunked_data_desc* data, - hash256_T hash) +sha256_ctx_finalize(struct sha256_ctx* ctx, hash256_T hash) { - /* Actually, the size of one chunk is 64 bytes. Anyway, note that after the - * last byte of the data to digest, one has to store 9 additionnal bytes: 1 - * to mark the end of the data and 8 used to save the effective data length. - * Such tailing bytes can exceed the 64 bytes length of the chunk. Since the - * overall message length must be aligned on 64 bytes we thus pre-allocate an - * array whose size is 2 times the chunk size */ - char buffer[64*2]; - - size_t msg_sz = 0; - size_t ichunk = 0; - size_t nchunks = 0; - size_t ichunk_last_byte = 0; /* Chunk id the last data byte */ - size_t remaining_data_sz = 0; + uint32_t i; - /* Initial hash values: first 32 bits of the fractional parts of the square - * roots of the first 8 primes 2..19 */ - uint32_t h0 = 0x6a09e667; - uint32_t h1 = 0xbb67ae85; - uint32_t h2 = 0x3c6ef372; - uint32_t h3 = 0xa54ff53a; - uint32_t h4 = 0x510e527f; - uint32_t h5 = 0x9b05688c; - uint32_t h6 = 0x1f83d9ab; - uint32_t h7 = 0x5be0cd19; - ASSERT(data && hash); - - remaining_data_sz = data->size; - - /* Compute the overall size of the message */ - msg_sz = ALIGN_SIZE - (data->size + 1/*Byte of the '1' bit*/ + 8/*message len*/, 64u); - - ASSERT((msg_sz % 64) == 0); - nchunks = msg_sz / 64; /* #chunks of 512 bits */ - - /* Index of the chunk containing the last data byte */ - ichunk_last_byte = data->size / 64; - - FOR_EACH(ichunk, 0, nchunks) { - char* chunk = NULL; - uint32_t w[64] = {0}; - uint32_t a=h0, b=h1, c=h2, d=h3, e=h4, f=h5, g=h6, h=h7; - int i; - - if(ichunk > ichunk_last_byte) { - chunk = buffer + 64; - } else { - chunk = buffer; - - /* Fetch the user data if any */ - if(remaining_data_sz) { - ASSERT(data->get_chunk512); - data->get_chunk512(buffer, ichunk, data->context); - remaining_data_sz = remaining_data_sz > 64 - ? remaining_data_sz - 64 : 0; - } - - /* Setup the tailing bytes of the message */ - if(ichunk == ichunk_last_byte) { - char* remaining_msg = buffer; - const size_t remaining_msg_sz = ichunk_last_byte==nchunks-1 ? 64 : 128; - - /* Id after the last data byte */ - const size_t ibyte_len = data->size % 64; - - /* Setup the '1' bit that marks the end of the data */ - remaining_msg[ibyte_len] = (char)0x80u; - - /* Clean up the bytes after the data up to the last 8 bytes */ - memset(remaining_msg + ibyte_len + 1, 0, - remaining_msg_sz - ibyte_len - 1 - 8); - - /* Store the message 8*len in big endian */ - *(uint64_t*)(remaining_msg + remaining_msg_sz-8) = - big_endian_64(data->size*8); - } - } + ASSERT(ctx && hash); - FOR_EACH(i, 0, 16) { - w[i] = big_endian_32(((uint32_t*)chunk)[i]); - } - FOR_EACH(i, 16, 64) { - const uint32_t s0 = rrot(w[i-15],7) ^ rrot(w[i-15],18) ^ (w[i-15] >> 3); - const uint32_t s1 = rrot(w[i-2],17) ^ rrot(w[i-2], 19) ^ (w[i-2] >> 10); - w[i] = w[i-16] + s0 + w[i-7] + s1; - } + ctx->nbits += ctx->len * 8; /* Update the message's length */ + i = ctx->len; - /* Compress the chunk */ - FOR_EACH(i, 0, 64) { - const uint32_t s1 = rrot(e, 6) ^ rrot(e, 11) ^ rrot(e, 25); - const uint32_t ch = (e & f) ^ ((~e) & g); - const uint32_t tmp1 = h + s1 + ch + k[i] + w[i]; - const uint32_t s0 = rrot(a, 2) ^ rrot(a, 13) ^ rrot(a, 22); - const uint32_t maj = (a & b) ^ (a & c) ^ (b & c); - const uint32_t tmp2 = s0 + maj; - - h = g; - g = f; - f = e; - e = d + tmp1; - d = c; - c = b; - b = a; - a = tmp1 + tmp2; - } + /* Setup the '1' bit that marks the end of the data */ + ctx->chunk[i++] = (char)0x80; - /* Update hash with the current compressed chunk */ - h0+=a; h1+=b; h2+=c; h3+=d; h4+=e; h5+=f; h6+=g; h7+=h; + /* Clean up the bytes after the data up to the last 8 bytes */ + if(ctx->len < 56) { + memset(ctx->chunk+i, 0, 56-i); + } else { + memset(ctx->chunk+i, 0, 64-i); + sha256_process_chunk(ctx, ctx->chunk); + memset(ctx->chunk, 0, 56); } - /* Write the results */ - ((uint32_t*)hash)[0] = big_endian_32(h0); - ((uint32_t*)hash)[1] = big_endian_32(h1); - ((uint32_t*)hash)[2] = big_endian_32(h2); - ((uint32_t*)hash)[3] = big_endian_32(h3); - ((uint32_t*)hash)[4] = big_endian_32(h4); - ((uint32_t*)hash)[5] = big_endian_32(h5); - ((uint32_t*)hash)[6] = big_endian_32(h6); - ((uint32_t*)hash)[7] = big_endian_32(h7); + /* Store the message's length in bits */ + *((uint64_t*)(ctx->chunk + 56)) = big_endian_64(ctx->nbits); + sha256_process_chunk(ctx, ctx->chunk); + + /* Store result the result */ + ((uint32_t*)hash)[0] = big_endian_32(ctx->state[0]); + ((uint32_t*)hash)[1] = big_endian_32(ctx->state[1]); + ((uint32_t*)hash)[2] = big_endian_32(ctx->state[2]); + ((uint32_t*)hash)[3] = big_endian_32(ctx->state[3]); + ((uint32_t*)hash)[4] = big_endian_32(ctx->state[4]); + ((uint32_t*)hash)[5] = big_endian_32(ctx->state[5]); + ((uint32_t*)hash)[6] = big_endian_32(ctx->state[6]); + ((uint32_t*)hash)[7] = big_endian_32(ctx->state[7]); +} + +void +hash_sha256 + (const void* data, + const size_t len, + hash256_T hash) +{ + struct sha256_ctx ctx; + ASSERT(hash); + ASSERT(data || !len); + + sha256_ctx_init(&ctx); + sha256_ctx_update(&ctx, data, len); + sha256_ctx_finalize(&ctx, hash); } void diff --git a/src/hash.h b/src/hash.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -20,6 +20,13 @@ struct mem_allocator; +struct sha256_ctx { + char chunk[64]; + uint32_t len; /* #bytes registered in chunk */ + uint32_t state[8]; /* Current hash state */ + uint64_t nbits; /* Overall size of the message in bits */ +}; + typedef char hash256_T[32]; struct chunked_data_desc { @@ -77,14 +84,24 @@ hash_fnv64(const void* data, const size_t len) BEGIN_DECLS RSYS_API void -hash_sha256 - (const void* data, - const size_t len, +sha256_ctx_init + (struct sha256_ctx* ctx); + +RSYS_API void +sha256_ctx_update + (struct sha256_ctx* ctx, + const char* bytes, + const size_t len); /* #bytes */ + +RSYS_API void +sha256_ctx_finalize + (struct sha256_ctx* ctx, hash256_T hash); RSYS_API void -hash_sha256_chunked_data - (struct chunked_data_desc* data, +hash_sha256 + (const void* data, + const size_t len, hash256_T hash); RSYS_API void diff --git a/src/hash_table.h b/src/hash_table.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/image.c b/src/image.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/image.h b/src/image.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/io_c99.h b/src/io_c99.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/library.c b/src/library.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/library.h b/src/library.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/list.h b/src/list.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/logger.c b/src/logger.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/logger.h b/src/logger.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/math.h b/src/math.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/mem_allocator.c b/src/mem_allocator.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/mem_allocator.h b/src/mem_allocator.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/mem_lifo_allocator.c b/src/mem_lifo_allocator.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/mem_proxy_allocator.c b/src/mem_proxy_allocator.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/morton.h b/src/morton.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/mutex.h b/src/mutex.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/pthread/pthread_condition.c b/src/pthread/pthread_condition.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/pthread/pthread_mutex.c b/src/pthread/pthread_mutex.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/quaternion.c b/src/quaternion.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/quaternion.h b/src/quaternion.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/real2.h b/src/real2.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/real22.h b/src/real22.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/real3.h b/src/real3.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/real33.h b/src/real33.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/real44.h b/src/real44.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/realX.h b/src/realX.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/realXY.h b/src/realXY.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/realXY_begin.h b/src/realXY_begin.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/realXY_end.h b/src/realXY_end.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/realX_begin.h b/src/realX_begin.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/realX_end.h b/src/realX_end.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/ref_count.h b/src/ref_count.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/rsys.h b/src/rsys.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/signal.h b/src/signal.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/str.c b/src/str.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/str.h b/src/str.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/stretchy_array.h b/src/stretchy_array.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_algorithm.c b/src/test_algorithm.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_atomic.c b/src/test_atomic.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_binary_heap.c b/src/test_binary_heap.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_condition.c b/src/test_condition.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_cstr.c b/src/test_cstr.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -343,6 +343,102 @@ test_res_to_cstr(void) printf("%s\n", res_to_cstr(RES_EOF)); } +static INLINE size_t +size + (const size_t teras, + const size_t gigas, + const size_t megas, + const size_t kilos, + const size_t bytes) +{ + return (size_t)1024*((size_t)1024*((size_t)1024*((size_t)1024* + teras + gigas) + megas) + kilos) + bytes; +} + +static void +test_size_to_cstr(void) +{ + char dump[512]; + size_t len; + size_t sz; + size_t dump_len; + char* tk = 0; + + sz = size(2, 450, 987, 243, 42); + + size_to_cstr(sz, SIZE_ALL, &len, NULL, 0); + CHK(len == 30); + size_to_cstr(sz, SIZE_ALL, NULL, dump, sizeof(dump)); + printf("%s\n", dump); + dump_len = strlen(dump); + CHK(len == dump_len); + + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "2")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "TB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "450")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "GB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "987")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "MB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "243")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "KB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "42")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "B")); + CHK((tk = strtok(NULL, " ")) == NULL); + + /* Check string truncation */ + size_to_cstr(sz, SIZE_ALL, &len, dump, dump_len - 3 + 1/*null char*/); + CHK(len == 30); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "2")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "TB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "450")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "GB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "987")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "MB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "243")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "KB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "4")); + CHK((tk = strtok(NULL, " ")) == NULL); + + size_to_cstr(sz, SIZE_GBYTE|SIZE_MBYTE, &len, dump, sizeof(dump)); + CHK(len == strlen(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "2498")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "GB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "987")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "MB")); + CHK((tk = strtok(NULL, " ")) == NULL); + + size_to_cstr(sz, SIZE_GBYTE|SIZE_KBYTE|SIZE_BYTE, &len, dump, sizeof(dump)); + CHK(len == strlen(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "2498")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "GB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "1010931")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "KB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "42")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "B")); + CHK((tk = strtok(NULL, " ")) == NULL); + + size_to_cstr(sz, 0, &len, dump, sizeof(dump)); + CHK(len == 0 && dump[0] == '\0'); + + size_to_cstr(0, SIZE_ALL, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "0")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "B")); + CHK((tk = strtok(NULL, " ")) == NULL); + + size_to_cstr(0, SIZE_TBYTE, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "0")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "TB")); + CHK((tk = strtok(NULL, " ")) == NULL); + + sz = size(0, 3, 0, 17, 0); + size_to_cstr(sz, SIZE_ALL, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "3")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "GB")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "17")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "KB")); + CHK((tk = strtok(NULL, " ")) == NULL); +} + int main(int argc, char** argv) { @@ -358,5 +454,6 @@ main(int argc, char** argv) test_list_float(); test_list_uint(); test_res_to_cstr(); + test_size_to_cstr(); return 0; } diff --git a/src/test_double2.c b/src/test_double2.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_double22.c b/src/test_double22.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_double3.c b/src/test_double3.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_double33.c b/src/test_double33.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_double4.c b/src/test_double4.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_double44.c b/src/test_double44.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_dynamic_array.c b/src/test_dynamic_array.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_endianness.c b/src/test_endianness.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_float2.c b/src/test_float2.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_float22.c b/src/test_float22.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_float3.c b/src/test_float3.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_float33.c b/src/test_float33.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_float4.c b/src/test_float4.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_float44.c b/src/test_float44.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_free_list.c b/src/test_free_list.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_func_name.c b/src/test_func_name.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_hash_sha256.c b/src/test_hash_sha256.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_hash_table.c b/src/test_hash_table.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_image.c b/src/test_image.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_library.c b/src/test_library.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_list.c b/src/test_list.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_logger.c b/src/test_logger.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_math.c b/src/test_math.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_mem_allocator.c b/src/test_mem_allocator.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_misc.c b/src/test_misc.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_morton.c b/src/test_morton.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_mutex.c b/src/test_mutex.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_quaternion.c b/src/test_quaternion.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_real2.h b/src/test_real2.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_real22.h b/src/test_real22.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_real3.h b/src/test_real3.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_real33.h b/src/test_real33.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_real4.h b/src/test_real4.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_real44.h b/src/test_real44.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_ref.c b/src/test_ref.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_signal.c b/src/test_signal.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_str.c b/src/test_str.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -183,7 +183,7 @@ main(int argc, char** argv) CHK(str_append_printf(&str, ":%s", __FILE__) == RES_OK); CHK(!strcmp(str_cget(&str), __FILE__":3737844653:"__FILE__)); chk_vprintf(&str, "You should have received a copy of the %s %s %d", - "GNU", "Lesser General Public License", 3); + "GNU", "General Public License", 3); CHK(!strcmp(str_cget(&str), "You should have received a copy of the " "GNU General Public License 3")); diff --git a/src/test_text_reader.c b/src/test_text_reader.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/test_time.c b/src/test_time.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published @@ -17,12 +17,36 @@ #include <stdlib.h> #include <string.h> +static struct time +build_time + (const int64_t days, + const int64_t hours, + const int64_t mins, + const int64_t secs, + const int64_t msecs, + const int64_t usecs, + const int64_t nsecs) +{ + struct time t; + + t.sec = days * 3600 * 24; + t.sec += hours * 3600; + t.sec += mins * 60; + t.sec += secs; + + t.nsec = msecs * 1000000; + t.nsec += usecs * 1000; + t.nsec += nsecs; + + return t; +} + int main(int argc, char** argv) { struct time start, end, res; char dump[512]; - char buf[32]; + char* tk; int64_t time = 0; int64_t i = 0; size_t dump_len; @@ -42,14 +66,14 @@ main(int argc, char** argv) time_dump (&res, TIME_SEC|TIME_MSEC|TIME_USEC, &dump_len, dump, sizeof(dump)); CHK(dump_len == strlen(dump)); - printf(">>> %s.\n", dump); + printf("%s\n", dump); time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); - printf(">>> %s.\n", dump); + printf("%s\n", dump); CHK(time_add(&res, &res, &res) == &res); CHK(time_val(&res, TIME_NSEC) == 2*time); time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); - printf(">>> %s.\n", dump); + printf("%s\n", dump); time = time_val(&res, TIME_NSEC); CHK(time_zero(&start) == &start); @@ -63,20 +87,74 @@ main(int argc, char** argv) CHK(time_sub(&res, &end, &end) == &res); CHK(time_val(&res, TIME_NSEC) == 0); + + res = build_time(1, 10, 55, 30, 998, 763, 314); + dump[0] = '?'; + time_dump(&res, TIME_ALL, &dump_len, dump, 0); + CHK(dump[0] = '?'); time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); - printf(">>> %s.\n", dump); - time_dump(&res, 0, NULL, dump, sizeof(dump)); - printf(">>> %s.\n", dump); + CHK(dump_len == strlen(dump)); - res.sec = 5; - res.nsec = 524198207; - time_dump(&res, TIME_ALL, &dump_len, buf, sizeof(buf)); - CHK(dump_len >= strlen(buf)); - printf(">>> %s.\n", buf); + printf("%s\n", dump); - time_dump(&res, TIME_ALL, &dump_len, dump, sizeof(dump)); - CHK(dump_len >= strlen(dump)); - printf(">>> %s.\n", dump); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "1")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "day")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "10")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "hours")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "55")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "mins")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "30")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "secs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "998")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "msecs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "763")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "usecs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "314")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "nsecs")); + CHK((tk = strtok(NULL, " ")) == NULL); + /* Check string truncation */ + time_dump(&res, TIME_ALL, NULL, dump, dump_len - 27 + 1/*null char*/); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "1")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "day")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "10")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "hours")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "55")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "mins")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "30")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "secs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "99")); + CHK((tk = strtok(NULL, " ")) == NULL); + + time_dump(&res, TIME_MIN|TIME_MSEC, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "2095")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "mins")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "30998")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "msecs")); + CHK((tk = strtok(NULL, " ")) == NULL); + + time_dump(&res, 0, &dump_len, dump, sizeof(dump)); + CHK(dump_len == 0 && dump[0] == '\0'); + + res = build_time(0, 0, 0, 0, 0, 0, 0); + time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "0")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "nsec")); + CHK((tk = strtok(NULL, " ")) == NULL); + + time_dump(&res, TIME_DAY, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "0")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "day")); + CHK((tk = strtok(NULL, " ")) == NULL); + + res = build_time(0, 0, 1, 0, 235, 10, 0); + time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "1")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "min")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "235")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "msecs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "10")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "usecs")); + CHK((tk = strtok(NULL, " ")) == NULL); return 0; } diff --git a/src/test_vmacros.c b/src/test_vmacros.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/text_reader.c b/src/text_reader.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/text_reader.h b/src/text_reader.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/win32/win32_condition.c b/src/win32/win32_condition.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published diff --git a/src/win32/win32_mutex.c b/src/win32/win32_mutex.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2013-2022 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2013-2023 Vincent Forest (vaplv@free.fr) * * The RSys library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published