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