Skip to content

Commit 6b0ae8b

Browse files
zookozooko
zooko
authored andcommitted
* extend runtime_assertN and verbose_abortN; add en/de code for uint64; define SIZE_MAX; vernum bump -> libzutil v0.9.15
1 parent f346b93 commit 6b0ae8b

8 files changed

+167
-18
lines changed

delegate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
is basically the moral equivalent of arguments to the function, but we can't
2929
use actual arguments without defining a different type of function pointer
3030
for each combination of types of arguments. */
31-
typedef void* (*delegatefunc)(void* self, void* data) ;
31+
typedef void* (*delegatefunc)(void* self, void* data);
3232

3333
typedef struct {
3434
void* self;

moreassert.c

+29
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,32 @@ void _verbose_abort8(char const*const filename, const int lineno, char const*con
9090
fprintf(stderr, "%s: %d: %s: %s%s%s%s%s%s%s%s", filename, lineno, funcname, msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8);
9191
exit(EXIT_FAILURE);
9292
}
93+
94+
void _verbose_abort9(char const*const filename, const int lineno, char const*const funcname, char const* msg1, char const* msg2, char const* msg3, char const* msg4, char const* msg5, char const* msg6, char const* msg7, char const* msg8, char const* msg9) {
95+
if (msg1 == NULL) { msg1 = ""; }
96+
if (msg2 == NULL) { msg2 = ""; }
97+
if (msg3 == NULL) { msg3 = ""; }
98+
if (msg4 == NULL) { msg4 = ""; }
99+
if (msg5 == NULL) { msg5 = ""; }
100+
if (msg6 == NULL) { msg6 = ""; }
101+
if (msg7 == NULL) { msg7 = ""; }
102+
if (msg8 == NULL) { msg8 = ""; }
103+
if (msg9 == NULL) { msg9 = ""; }
104+
fprintf(stderr, "%s: %d: %s: %s%s%s%s%s%s%s%s%s", filename, lineno, funcname, msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9);
105+
exit(EXIT_FAILURE);
106+
}
107+
108+
void _verbose_abort10(char const*const filename, const int lineno, char const*const funcname, char const* msg1, char const* msg2, char const* msg3, char const* msg4, char const* msg5, char const* msg6, char const* msg7, char const* msg8, char const* msg9, char const* msg10) {
109+
if (msg1 == NULL) { msg1 = ""; }
110+
if (msg2 == NULL) { msg2 = ""; }
111+
if (msg3 == NULL) { msg3 = ""; }
112+
if (msg4 == NULL) { msg4 = ""; }
113+
if (msg5 == NULL) { msg5 = ""; }
114+
if (msg6 == NULL) { msg6 = ""; }
115+
if (msg7 == NULL) { msg7 = ""; }
116+
if (msg8 == NULL) { msg8 = ""; }
117+
if (msg9 == NULL) { msg9 = ""; }
118+
if (msg10 == NULL) { msg10 = ""; }
119+
fprintf(stderr, "%s: %d: %s: %s%s%s%s%s%s%s%s%s%s", filename, lineno, funcname, msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10);
120+
exit(EXIT_FAILURE);
121+
}

moreassert.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* void runtime_assert(int condition, const char* msg);
4949
* void runtime_assert2(int condition, const char* msg1, const char* msg2);
5050
* void runtime_assert3(int condition, const char* msg1, const char* msg2, const char* msg3);
51+
* void runtime_assert4(int condition, const char* msg1, const char* msg2, const char* msg3, const char* msg4);
5152
*
5253
* Sometimes it can be useful to have two error messages, especially if one of
5354
* them is generated by lower-level code, like this:

moreassertimp.h

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ void _verbose_abort5(char const* filename, int lineno, char const* funcname, cha
2020
void _verbose_abort6(char const* filename, int lineno, char const* funcname, char const* msg1, const char* msg2, const char* msg3, const char* msg4, const char* msg5, const char* msg6);
2121
void _verbose_abort7(char const* filename, int lineno, char const* funcname, char const* msg1, const char* msg2, const char* msg3, const char* msg4, const char* msg5, const char* msg6, const char* msg7);
2222
void _verbose_abort8(char const* filename, int lineno, char const* funcname, char const* msg1, const char* msg2, const char* msg3, const char* msg4, const char* msg5, const char* msg6, const char* msg7, const char* msg8);
23+
void _verbose_abort9(char const* filename, int lineno, char const* funcname, char const* msg1, const char* msg2, const char* msg3, const char* msg4, const char* msg5, const char* msg6, const char* msg7, const char* msg8, const char* msg9);
24+
void _verbose_abort10(char const* filename, int lineno, char const* funcname, char const* msg1, const char* msg2, const char* msg3, const char* msg4, const char* msg5, const char* msg6, const char* msg7, const char* msg8, const char* msg9, const char* msg10);
2325

2426
#define verbose_abort(msg) ((void)(_verbose_abort(__FILE__, __LINE__, __func__, (msg))))
2527
#define verbose_abort2(msg1, msg2) ((void)(_verbose_abort2(__FILE__, __LINE__, __func__, (msg1), (msg2))))
@@ -29,7 +31,10 @@ void _verbose_abort8(char const* filename, int lineno, char const* funcname, cha
2931
#define verbose_abort6(msg1, msg2, msg3, msg4, msg5, msg6) ((void)(_verbose_abort6(__FILE__, __LINE__, __func__, (msg1), (msg2), (msg3), (msg4), (msg5), (msg6))))
3032
#define verbose_abort7(msg1, msg2, msg3, msg4, msg5, msg6, msg7) ((void)(_verbose_abort7(__FILE__, __LINE__, __func__, (msg1), (msg2), (msg3), (msg4), (msg5), (msg6), (msg7))))
3133
#define verbose_abort8(msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8) ((void)(_verbose_abort8(__FILE__, __LINE__, __func__, (msg1), (msg2), (msg3), (msg4), (msg5), (msg6), (msg7), (msg8))))
34+
#define verbose_abort9(msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9) ((void)(_verbose_abort9(__FILE__, __LINE__, __func__, (msg1), (msg2), (msg3), (msg4), (msg5), (msg6), (msg7), (msg8), (msg9))))
35+
#define verbose_abort10(msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10) ((void)(_verbose_abort8(__FILE__, __LINE__, __func__, (msg1), (msg2), (msg3), (msg4), (msg5), (msg6), (msg7), (msg8), (msg9), (msg10))))
3236

3337
#define runtime_assert(expr, msg) ((void)((expr) ? ((void)0) : verbose_abort4("Assertion `", #expr, "' failed.; ", msg)))
3438
#define runtime_assert2(expr, msg1, msg2) ((void)((expr) ? ((void)0) : verbose_abort6("Assertion `", #expr, "' failed.; ", (msg1), "; ", (msg2))))
3539
#define runtime_assert3(expr, msg1, msg2, msg3) ((void)((expr) ? ((void)0) : verbose_abort8("Assertion `", #expr, "' failed.; ", (msg1), "; ", (msg2), "; ", (msg3))))
40+
#define runtime_assert4(expr, msg1, msg2, msg3, msg4) ((void)((expr) ? ((void)0) : verbose_abort10("Assertion `", #expr, "' failed.; ", (msg1), "; ", (msg2), "; ", (msg3), "; ", (msg4))))

morelimits.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <stddef.h>
1919
#include <limits.h>
2020

21-
static char const* const morelimits_h_cvsid = "$Id: morelimits.h,v 1.10 2004/03/20 00:28:20 zooko Exp $";
21+
static char const* const morelimits_h_cvsid = "$Id: morelimits.h,v 1.11 2004/05/07 15:58:59 zooko Exp $";
2222

2323
static int const morelimits_vermaj = 0;
2424
static int const morelimits_vermin = 9;
@@ -38,6 +38,13 @@ static char const* const morelimits_vernum = "0.9.2";
3838
#define SIZE_T_MIN Z_SIZE_T_MIN
3939
#endif
4040

41+
#ifndef SIZE_MAX
42+
#define SIZE_MAX Z_SIZE_T_MAX
43+
#endif
44+
#ifndef SIZE_MIN
45+
#define SIZE_MIN Z_SIZE_T_MIN
46+
#endif
47+
4148
#define Z_TIME_T_MAX Z_MAX(time_t)
4249
#define Z_TIME_T_MIN Z_MIN(time_t)
4350

@@ -87,6 +94,7 @@ static char const* const morelimits_vernum = "0.9.2";
8794
#define Z_UINT_MAX Z_MAX(unsigned int)
8895
#define Z_USHRT_MAX Z_MAX(unsigned short)
8996
#define Z_UCHAR_MAX Z_MAX(unsigned char)
97+
#define Z_BYTE_MAX Z_MAX(unsigned char)
9098

9199
#define Z_LLONG_MAX Z_MAX(long long)
92100
#define Z_LONG_MAX Z_MAX(long)

test.c

+66-4
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,34 @@
2020

2121
#include "zutil.h"
2222

23-
int _help_test_uint32_encode(const unsigned long u)
23+
#ifdef NDEBUG
24+
#warning You are compiling test.c with NDEBUG set, and since the tests use assert() to verify the results, this means the tests will pass even if the code is wrong.
25+
#endif
26+
27+
int _help_test_uint32_encode(const unsigned long lu)
2428
{
2529
unsigned long r;
2630
zbyte p[4];
27-
/*printf("about to try; %lu ", u);*/
28-
uint32_encode(u, p);
31+
/*printf("about to try; %lu ", lu);*/
32+
uint32_encode(lu, p);
2933
r = uint32_decode(p);
30-
assert(u == r);
34+
assert(lu == r);
3135
/*printf("== %lu\n", r);*/
3236
return 1;
3337
}
3438

39+
int _help_test_uint64_encode(const unsigned long long llu)
40+
{
41+
unsigned long long r;
42+
zbyte p[8];
43+
/*printf("about to try; %llu ", llu);*/
44+
uint64_encode(llu, p);
45+
r = uint64_decode(p);
46+
assert(llu == r);
47+
/*printf("== %llu\n", r);*/
48+
return 1;
49+
}
50+
3551
int test_uint32_encode()
3652
{
3753
_help_test_uint32_encode(0);
@@ -62,6 +78,41 @@ int test_uint32_encode()
6278
return 1;
6379
}
6480

81+
int test_uint64_encode()
82+
{
83+
_help_test_uint64_encode(0);
84+
_help_test_uint64_encode(1);
85+
_help_test_uint64_encode(2);
86+
_help_test_uint64_encode(24);
87+
_help_test_uint64_encode(42);
88+
_help_test_uint64_encode(53);
89+
_help_test_uint64_encode(99);
90+
_help_test_uint64_encode((1<<8)-2);
91+
_help_test_uint64_encode((1<<8)-1);
92+
_help_test_uint64_encode((1<<8));
93+
_help_test_uint64_encode((1<<8)+1);
94+
_help_test_uint64_encode((1<<8)+2);
95+
_help_test_uint64_encode((1<<16)-2);
96+
_help_test_uint64_encode((1<<16)-1);
97+
_help_test_uint64_encode((1<<16));
98+
_help_test_uint64_encode((1<<16)+1);
99+
_help_test_uint64_encode((1<<16)+2);
100+
_help_test_uint64_encode((1<<24)-2);
101+
_help_test_uint64_encode((1<<24)-1);
102+
_help_test_uint64_encode((1<<24));
103+
_help_test_uint64_encode((1<<24)+1);
104+
_help_test_uint64_encode((1<<24)+2);
105+
_help_test_uint64_encode(Z_UINT32_MAX-2);
106+
_help_test_uint64_encode(Z_UINT32_MAX-1);
107+
_help_test_uint64_encode(Z_UINT32_MAX);
108+
_help_test_uint64_encode(Z_UINT32_MAX+1LLU);
109+
_help_test_uint64_encode(Z_UINT32_MAX+2LLU);
110+
_help_test_uint64_encode(Z_UINT64_MAX-2);
111+
_help_test_uint64_encode(Z_UINT64_MAX-1);
112+
_help_test_uint64_encode(Z_UINT64_MAX);
113+
return 1;
114+
}
115+
65116
void bench_uint32_encode()
66117
{
67118
unsigned i;
@@ -72,6 +123,16 @@ void bench_uint32_encode()
72123
}
73124
}
74125

126+
void bench_uint64_encode()
127+
{
128+
unsigned i;
129+
for (i = 0; i < 1000000; i++) {
130+
if (! test_uint64_encode()) {
131+
printf("what ???\n");
132+
}
133+
}
134+
}
135+
75136
int test_overflow()
76137
{
77138
assert (add_would_overflow_char(CHAR_MAX - 2, CHAR_MAX - 3));
@@ -1320,6 +1381,7 @@ void print_morelimits() {
13201381
int test() {
13211382
/*print_morelimits();*/
13221383
test_uint32_encode();
1384+
test_uint64_encode();
13231385
test_FITS_INTO_SIGNED_INT();
13241386
test_PROMOTES_TO_SIGNED_TYPE();
13251387
test_overflow();

zutil.c

+32-6
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,39 @@ unsigned long UINT32_DECODE(const zbyte* const bs)
2828
}
2929

3030
#undef uint32_encode
31-
void uint32_encode(const unsigned int u, zbyte* const bs)
31+
void uint32_encode(const unsigned long lu, zbyte* const bs)
3232
{
33-
runtime_assert(u <= Z_UINT32_MAX, "Cannot encode a number this big into 32 bits.");
34-
bs[3] = u % (1U << 8);
35-
bs[2] = (u / (1U << 8)) % (1U << 8);
36-
bs[1] = (u / (1U << 16)) % (1U << 8);
37-
bs[0] = (u / (1U << 24)) % (1U << 8);
33+
runtime_assert(lu <= Z_UINT32_MAX, "Cannot encode a number this big into 32 bits.");
34+
bs[3] = lu % (1U << 8);
35+
bs[2] = (lu / (1U << 8)) % (1U << 8);
36+
bs[1] = (lu / (1LU << 16)) % (1U << 8);
37+
bs[0] = (lu / (1LU << 24)) % (1U << 8);
38+
}
39+
40+
#undef uint64_decode
41+
unsigned long long uint64_decode(const zbyte* const bs)
42+
{
43+
return ((unsigned long long)(bs[7])) | ((unsigned long long)(bs[6])) << 8 | ((unsigned long long)(bs[5])) << 16 | ((unsigned long long)(bs[4])) << 24 | ((unsigned long long)(bs[3])) << 32 | ((unsigned long long)(bs[2])) << 40 | ((unsigned long long)(bs[1])) << 48 | ((unsigned long long)(bs[0])) << 56;
44+
}
45+
46+
#undef UINT64_DECODE
47+
unsigned long long UINT64_DECODE(const zbyte* const bs)
48+
{
49+
return ((unsigned long long)(bs[7])) | ((unsigned long long)(bs[6])) << 8 | ((unsigned long long)(bs[5])) << 16 | ((unsigned long long)(bs[4])) << 24 | ((unsigned long long)(bs[3])) << 32 | ((unsigned long long)(bs[2])) << 40 | ((unsigned long long)(bs[1])) << 48 | ((unsigned long long)(bs[0])) << 56;
50+
}
51+
52+
#undef uint64_encode
53+
void uint64_encode(const unsigned long long llu, zbyte* const bs)
54+
{
55+
runtime_assert(llu <= Z_UINT64_MAX, "Cannot encode a number this big into 64 bits.");
56+
bs[7] = llu % (1U << 8);
57+
bs[6] = (llu / (1U << 8)) % (1U << 8);
58+
bs[5] = (llu / (1LU << 16)) % (1U << 8);
59+
bs[4] = (llu / (1LU << 24)) % (1U << 8);
60+
bs[3] = (llu / (1LLU << 32)) % (1U << 8);
61+
bs[2] = (llu / (1LLU << 40)) % (1U << 8);
62+
bs[1] = (llu / (1LLU << 48)) % (1U << 8);
63+
bs[0] = (llu / (1LLU << 56)) % (1U << 8);
3864
}
3965

4066
#undef divceil

zutil.h

+24-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
#include <stddef.h>
2121

22-
static char const* const zutil_h_cvsid = "$Id: zutil.h,v 1.17 2004/03/20 00:28:20 zooko Exp $";
22+
static char const* const zutil_h_cvsid = "$Id: zutil.h,v 1.18 2004/05/07 15:58:59 zooko Exp $";
2323

2424
static int const zutil_vermaj = 0;
2525
static int const zutil_vermin = 9;
26-
static int const zutil_vermicro = 14;
27-
static char const* const zutil_vernum = "0.9.14";
26+
static int const zutil_vermicro = 15;
27+
static char const* const zutil_vernum = "0.9.15";
2828

2929
#ifndef __cplusplus
3030
typedef int bool;
@@ -39,22 +39,40 @@ typedef int bool;
3939
typedef unsigned char zbyte;
4040

4141
/**
42-
* Decode a four bytes into an unsigned int. bs points to a buffer which must
42+
* Decode four bytes into an unsigned int. bs points to a buffer which must
4343
* have (at least) four elements; the first four elements of bs contain the
4444
* encoding of the unsigned integer in big-endian format.
4545
*/
4646
unsigned long uint32_decode(const zbyte* bs);
4747
unsigned long UINT32_DECODE(const zbyte* bs);
4848

49+
/**
50+
* Decode eight bytes into an unsigned int. bs points to a buffer which must
51+
* have (at least) eight elements; the first eight elements of bs contain the
52+
* encoding of the unsigned integer in big-endian format.
53+
*/
54+
unsigned long long uint64_decode(const zbyte* bs);
55+
unsigned long long UINT64_DECODE(const zbyte* bs);
56+
4957
/**
5058
* Encode an unsigned int into four zbytes in big-endian format. Aborts (via
5159
* runtime_assert()) if the value is too large to encode into 32 bits (that is,
5260
* if it is >= 2^32).
5361
*
54-
* @param u: the value to encode
62+
* @param lu: the value to encode
5563
* @param bs: pointer to the first byte of an array of at least four zbytes
5664
*/
57-
void uint32_encode(unsigned int u, zbyte* bs);
65+
void uint32_encode(unsigned long lu, zbyte* bs);
66+
67+
/**
68+
* Encode an unsigned int into eight zbytes in big-endian format. Aborts (via
69+
* runtime_assert()) if the value is too large to encode into 64 bits (that is,
70+
* if it is >= 2^64).
71+
*
72+
* @param lu: the value to encode
73+
* @param bs: pointer to the first byte of an array of at least eight zbytes
74+
*/
75+
void uint64_encode(unsigned long long llu, zbyte* bs);
5876

5977
/*
6078
Returns ceil(x/y): the smallest integer which is greater than or equal to x/y.

0 commit comments

Comments
 (0)