add telegram account, fix db insert error.
[apps/native/telegram-tizen.git] / tg-engine-service / mtprotocol / tools.h
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 <openssl/err.h>
25 #include <assert.h>
26 #include "tgl.h"
27
28 #define talloc tgl_allocator->alloc
29 #define talloc0 tgl_alloc0
30 #define tfree tgl_allocator->free 
31 #define tfree_str tgl_free_str
32 #define tfree_secure tgl_free_secure
33 #define trealloc tgl_allocator->realloc
34 #define tcheck tgl_allocator->check
35 #define texists tgl_allocator->exists
36 #define tstrdup tgl_strdup
37 #define tstrndup tgl_strndup
38 #define tasprintf tgl_asprintf
39 #define tsnprintf tgl_snprintf
40
41
42 struct tgl_allocator *tgl_allocator;
43 double tglt_get_double_time(void);
44
45 void logprintf_x(int level, const char *format, ...);
46
47 int tgl_inflate(void *input, int ilen, void *output, int olen);
48 //void ensure(int r);
49 //void ensure_ptr(void *p);
50
51 static inline void out_of_memory(void)
52 {
53         fprintf(stderr, "Out of memory\n");
54         exit(1);
55 }
56
57 static inline void ensure(int r)
58 {
59         if (!r) {
60                 fprintf(stderr, "Open SSL error\n");
61                 ERR_print_errors_fp(stderr);
62                 assert(0);
63         }
64 }
65
66 static inline void ensure_ptr(void *p)
67 {
68         if (p == NULL) {
69                 out_of_memory();
70         }
71 }
72
73 void *tgl_alloc_debug(size_t size);
74 void *tgl_alloc_release(size_t size);
75 void *tgl_realloc_debug(void *ptr, size_t old_size, size_t size);
76 void *tgl_realloc_release(void *ptr, size_t old_size, size_t size);
77 void *tgl_alloc0(size_t size);
78 char *tgl_strdup(const char *s);
79 char *tgl_strndup(const char *s, size_t n);
80 void tgl_free_debug(void *ptr, int size);
81 void tgl_free_release(void *ptr, int size);
82 //void tgl_free_str(void *ptr);
83 //void tgl_free_secure(void *ptr, int size);
84 void tgl_check_debug(void);
85 void tgl_exists_debug(void *ptr, int size);
86 void tgl_check_release(void);
87 void tgl_exists_release(void *ptr, int size);
88 int tgl_snprintf(char *buf, int len, const char *format, ...) __attribute__((format(printf, 3, 4)));
89 int tgl_asprintf(char **res, const char *format, ...) __attribute__((format(printf, 2, 3)));
90 void tglt_secure_random(void *s, int l);
91 void tgl_my_clock_gettime(int clock_id, struct timespec *T);
92
93
94 static inline void tgl_free_str(void *ptr)
95 {
96         if (!ptr) {
97                 return;
98         }
99         tfree(ptr, strlen(ptr) + 1);
100 }
101
102 static inline void tgl_free_secure(void *ptr, int size)
103 {
104         memset(ptr, 0, size);
105         tfree(ptr, size);
106 }
107
108 static inline void hexdump(void *ptr, void *end_ptr)
109 {
110         int total = 0;
111         while (ptr < end_ptr) {
112                 fprintf(stderr, "%02x",(int)*(unsigned char *)ptr);
113                 ptr ++;
114                 total ++;
115                 if (total == 16) {
116                         fprintf(stderr, "\n");
117                         total = 0;
118                 }
119         }
120         if (total) {
121                 fprintf(stderr, "\n");
122         }
123 }
124
125
126 #endif