1 /**********************************************************************
2  * Copyright (c) 2013, 2014 Pieter Wuille                             *
3  * Distributed under the MIT software license, see the accompanying   *
4  * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5  **********************************************************************/
6 
7 #ifndef _SECP256K1_UTIL_H_
8 #define _SECP256K1_UTIL_H_
9 
10 #if defined HAVE_CONFIG_H
11 #include "libsecp256k1-config.h"
12 #endif
13 
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <stdio.h>
17 
18 typedef struct {
19     void (*fn)(const char *text, void* data);
20     const void* data;
21 } secp256k1_callback;
22 
secp256k1_callback_call(const secp256k1_callback * const cb,const char * const text)23 static SECP256K1_INLINE void secp256k1_callback_call(const secp256k1_callback * const cb, const char * const text) {
24     cb->fn(text, (void*)cb->data);
25 }
26 
27 #ifdef DETERMINISTIC
28 #define TEST_FAILURE(msg) do { \
29     fprintf(stderr, "%s\n", msg); \
30     abort(); \
31 } while(0);
32 #else
33 #define TEST_FAILURE(msg) do { \
34     fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg); \
35     abort(); \
36 } while(0)
37 #endif
38 
39 #ifdef HAVE_BUILTIN_EXPECT
40 #define EXPECT(x,c) __builtin_expect((x),(c))
41 #else
42 #define EXPECT(x,c) (x)
43 #endif
44 
45 #ifdef DETERMINISTIC
46 #define CHECK(cond) do { \
47     if (EXPECT(!(cond), 0)) { \
48         TEST_FAILURE("test condition failed"); \
49     } \
50 } while(0)
51 #else
52 #define CHECK(cond) do { \
53     if (EXPECT(!(cond), 0)) { \
54         TEST_FAILURE("test condition failed: " #cond); \
55     } \
56 } while(0)
57 #endif
58 
59 /* Like assert(), but when VERIFY is defined, and side-effect safe. */
60 #ifdef VERIFY
61 #define VERIFY_CHECK CHECK
62 #define VERIFY_SETUP(stmt) do { stmt; } while(0)
63 #else
64 #define VERIFY_CHECK(cond) do { (void)(cond); } while(0)
65 #define VERIFY_SETUP(stmt)
66 #endif
67 
checked_malloc(const secp256k1_callback * cb,size_t size)68 static SECP256K1_INLINE void *checked_malloc(const secp256k1_callback* cb, size_t size) {
69     void *ret = malloc(size);
70     if (ret == NULL) {
71         secp256k1_callback_call(cb, "Out of memory");
72     }
73     return ret;
74 }
75 
76 /* Macro for restrict, when available and not in a VERIFY build. */
77 #if defined(SECP256K1_BUILD) && defined(VERIFY)
78 # define SECP256K1_RESTRICT
79 #else
80 # if (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) )
81 #  if SECP256K1_GNUC_PREREQ(3,0)
82 #   define SECP256K1_RESTRICT __restrict__
83 #  elif (defined(_MSC_VER) && _MSC_VER >= 1400)
84 #   define SECP256K1_RESTRICT __restrict
85 #  else
86 #   define SECP256K1_RESTRICT
87 #  endif
88 # else
89 #  define SECP256K1_RESTRICT restrict
90 # endif
91 #endif
92 
93 #if defined(_WIN32)
94 # define I64FORMAT "I64d"
95 # define I64uFORMAT "I64u"
96 #else
97 # define I64FORMAT "lld"
98 # define I64uFORMAT "llu"
99 #endif
100 
101 #if defined(HAVE___INT128)
102 # if defined(__GNUC__)
103 #  define SECP256K1_GNUC_EXT __extension__
104 # else
105 #  define SECP256K1_GNUC_EXT
106 # endif
107 SECP256K1_GNUC_EXT typedef unsigned __int128 uint128_t;
108 #endif
109 
110 #endif
111