1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/*
This file is part of tgl-library
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Copyright Vitaly Valtman 2013-2015
*/
#ifndef __TOOLS_H__
#define __TOOLS_H__
#include <time.h>
#include <openssl/err.h>
#include <assert.h>
#include "tgl.h"
#define talloc tgl_allocator->alloc
#define talloc0 tgl_alloc0
#define tfree tgl_allocator->free
#define tfree_str tgl_free_str
#define tfree_secure tgl_free_secure
#define trealloc tgl_allocator->realloc
#define tcheck tgl_allocator->check
#define texists tgl_allocator->exists
#define tstrdup tgl_strdup
#define tmemdup tgl_memdup
#define tstrndup tgl_strndup
#define tasprintf tgl_asprintf
#define tsnprintf tgl_snprintf
#if(_MSC_VER < 1900)
struct _timespec32
{
__time32_t tv_sec;
long tv_nsec;
};
struct _timespec64
{
__time64_t tv_sec;
long tv_nsec;
};
#ifndef _CRT_NO_TIME_T
struct timespec
{
time_t tv_sec; // Seconds - >= 0
long tv_nsec; // Nanoseconds - [0, 999999999]
};
#endif
#endif
extern struct tgl_allocator *tgl_allocator;
double tglt_get_double_time (void);
int tgl_inflate (void *input, int ilen, void *output, int olen);
//void ensure (int r);
//void ensure_ptr (void *p);
static inline void out_of_memory (void) {
fprintf (stderr, "Out of memory\n");
exit (1);
}
static inline void ensure (int r) {
if (!r) {
fprintf (stderr, "Open SSL error\n");
ERR_print_errors_fp (stderr);
assert (0);
}
}
static inline void ensure_ptr (void *p) {
if (p == NULL) {
out_of_memory ();
}
}
void *tgl_alloc_debug (size_t size);
void *tgl_alloc_release (size_t size);
void *tgl_realloc_debug (void *ptr, size_t old_size, size_t size);
void *tgl_realloc_release (void *ptr, size_t old_size, size_t size);
void *tgl_alloc0 (size_t size);
char *tgl_strdup (const char *s);
char *tgl_strndup (const char *s, size_t n);
void tgl_free_debug (void *ptr, int size);
void tgl_free_release (void *ptr, int size);
//void tgl_free_str (void *ptr);
//void tgl_free_secure (void *ptr, int size);
void tgl_check_debug (void);
void tgl_exists_debug (void *ptr, int size);
void tgl_check_release (void);
void tgl_exists_release (void *ptr, int size);
void *tgl_memdup (const void *s, size_t n);
int tgl_snprintf (char *buf, int len, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
int tgl_asprintf (char **res, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
void tglt_secure_random (void *s, int l);
void tgl_my_clock_gettime (int clock_id, struct timespec *T);
static inline void tgl_free_str (void *ptr) {
if (!ptr) { return; }
tfree (ptr, strlen ((const char *)ptr) + 1);
}
static inline void tgl_free_secure (void *ptr, int size) {
memset (ptr, 0, size);
tfree (ptr, size);
}
static inline void hexdump (void *ptr, void *end_ptr) {
int total = 0;
unsigned char *bptr = (unsigned char *)ptr;
while (bptr < (unsigned char *)end_ptr) {
fprintf (stderr, "%02x", (int)*bptr);
bptr ++;
total ++;
if (total == 16) {
fprintf (stderr, "\n");
total = 0;
}
}
if (total) { fprintf (stderr, "\n"); }
}
#endif
|