From 9efad0f349571ac24e2755944f4de64697da0955 Mon Sep 17 00:00:00 2001 From: vvaltman Date: Thu, 21 Aug 2014 05:56:53 +0400 Subject: [PATCH] fixes --- config.h.in | 3 ++ configure | 45 ++++++++++++++++++++++ configure.ac | 1 + generate.c | 4 +- interface.c | 4 +- interface.h | 2 +- queries.c | 19 ++++++---- tgl.c | 1 + tgl.h | 19 ++++------ tools.c | 105 +++++++++++++++++++++++---------------------------- tools.h | 75 +++++++++++++++++++++++++++++------- 11 files changed, 181 insertions(+), 97 deletions(-) diff --git a/config.h.in b/config.h.in index 1a20b5b..f0315e0 100644 --- a/config.h.in +++ b/config.h.in @@ -30,6 +30,9 @@ /* Define to 1 if you have the `edit' library (-ledit). */ #undef HAVE_LIBEDIT +/* Define to 1 if you have the `event' library (-levent). */ +#undef HAVE_LIBEVENT + /* Define to 1 if you have the `m' library (-lm). */ #undef HAVE_LIBM diff --git a/configure b/configure index 0a54e9f..bf5cc3e 100755 --- a/configure +++ b/configure @@ -3424,6 +3424,51 @@ _ACEOF fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for event_base_new in -levent" >&5 +$as_echo_n "checking for event_base_new in -levent... " >&6; } +if ${ac_cv_lib_event_event_base_new+:} false; then : + $as_echo_n "(cached) " >&6 +else + ac_check_lib_save_LIBS=$LIBS +LIBS="-levent $LIBS" +cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +/* Override any GCC internal prototype to avoid an error. + Use char because int might match the return type of a GCC + builtin and then its argument prototype would still apply. */ +#ifdef __cplusplus +extern "C" +#endif +char event_base_new (); +int +main () +{ +return event_base_new (); + ; + return 0; +} +_ACEOF +if ac_fn_c_try_link "$LINENO"; then : + ac_cv_lib_event_event_base_new=yes +else + ac_cv_lib_event_event_base_new=no +fi +rm -f core conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext +LIBS=$ac_check_lib_save_LIBS +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_event_event_base_new" >&5 +$as_echo "$ac_cv_lib_event_event_base_new" >&6; } +if test "x$ac_cv_lib_event_event_base_new" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_LIBEVENT 1 +_ACEOF + + LIBS="-levent $LIBS" + +fi + EXTRA_LIBS="" { $as_echo "$as_me:${as_lineno-$LINENO}: checking for rl_save_prompt in -lreadline" >&5 diff --git a/configure.ac b/configure.ac index 6b0f5d7..d257359 100644 --- a/configure.ac +++ b/configure.ac @@ -18,6 +18,7 @@ AC_SEARCH_LIBS([clock_gettime], [rt]) AC_SEARCH_LIBS([backtrace], [execinfo]) AC_CHECK_LIB([z], [inflate]) AC_CHECK_LIB([crypto], [AES_set_encrypt_key]) +AC_CHECK_LIB([event], [event_base_new]) EXTRA_LIBS="" AC_CHECK_LIB([readline], [rl_save_prompt], diff --git a/generate.c b/generate.c index 03d432d..e4313f3 100644 --- a/generate.c +++ b/generate.c @@ -942,12 +942,12 @@ void usage (void) { void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2))); void logprintf (const char *format __attribute__ ((unused)), ...) { } - +/* void hexdump (int *in_ptr, int *in_end) { int *ptr = in_ptr; while (ptr < in_end) { printf (" %08x", *(ptr ++)); } printf ("\n"); -} +}*/ #ifdef HAVE_EXECINFO_H void print_backtrace (void) { diff --git a/interface.c b/interface.c index c1c2f4e..b396af7 100644 --- a/interface.c +++ b/interface.c @@ -1514,13 +1514,13 @@ void print_end (void) { prompt_was = 0; } -void hexdump (int *in_ptr, int *in_end) { +/*void hexdump (int *in_ptr, int *in_end) { print_start (); int *ptr = in_ptr; while (ptr < in_end) { printf (" %08x", *(ptr ++)); } printf ("\n"); print_end (); -} +}*/ void logprintf (const char *format, ...) { int x = 0; diff --git a/interface.h b/interface.h index dd363ec..e52ec0d 100644 --- a/interface.h +++ b/interface.h @@ -50,7 +50,7 @@ void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2))) } while (0);\ -void hexdump (int *in_ptr, int *in_end); +//void hexdump (int *in_ptr, int *in_end); struct tgl_message; void print_message (struct tgl_message *M); diff --git a/queries.c b/queries.c index 89920b4..c8404ce 100644 --- a/queries.c +++ b/queries.c @@ -61,7 +61,7 @@ #define PATH_MAX 4096 #endif -int want_dc_num; +//int want_dc_num; char *get_downloads_directory (void); //extern int offline_mode; @@ -80,7 +80,7 @@ static void out_peer_id (tgl_peer_id_t id); #define memcmp8(a,b) memcmp ((a), (b), 8) DEFINE_TREE (query, struct query *, memcmp8, 0) ; -struct tree_query *queries_tree; +static struct tree_query *queries_tree; struct query *tglq_query_get (long long id) { return tree_lookup_query (queries_tree, (void *)&id); @@ -208,7 +208,7 @@ void tglq_query_error (long long id) { static int packed_buffer[MAX_PACKED_SIZE / 4]; void tglq_query_result (long long id UU) { - vlogprintf (E_DEBUG, "result for query #%lld\n", id); + vlogprintf (E_DEBUG, "result for query #%lld. Size %ld bytes\n", id, 4 * (in_end - in_ptr)); /*if (verbosity >= 4) { logprintf ( "result: "); hexdump_in (); @@ -221,6 +221,7 @@ void tglq_query_result (long long id UU) { int l = prefetch_strlen (); char *s = fetch_str (l); int total_out = tgl_inflate (s, l, packed_buffer, MAX_PACKED_SIZE); + vlogprintf (E_DEBUG, "inflated %d bytes\n", total_out); end = in_ptr; eend = in_end; //assert (total_out % 4 == 0); @@ -246,6 +247,7 @@ void tglq_query_result (long long id UU) { if (q->methods && q->methods->on_answer) { if (q->methods->type) { int *save = in_ptr; + vlogprintf (E_DEBUG, "in_ptr = %p, end_ptr = %p\n", in_ptr, in_end); if (skip_type_any (q->methods->type) < 0) { vlogprintf (E_ERROR, "Skipped %ld int out of %ld (type %s)\n", in_ptr - save, in_end - save, q->methods->type->type->id); assert (0); @@ -269,8 +271,8 @@ void tglq_query_result (long long id UU) { } -int max_chat_size; -int max_bcast_size; +//int max_chat_size; +//int max_bcast_size; //int want_dc_num; //int new_dc_num; //extern struct dc *DC_list[]; @@ -337,11 +339,12 @@ static int help_get_config_on_answer (struct query *q UU) { for (i = 0; i < n; i++) { fetch_dc_option (); } - max_chat_size = fetch_int (); + int max_chat_size = fetch_int (); + int max_bcast_size = 0; if (op == CODE_config) { max_bcast_size = fetch_int (); } - vlogprintf (E_DEBUG, "chat_size = %d\n", max_chat_size); + vlogprintf (E_DEBUG, "chat_size = %d, bcast_size = %d\n", max_chat_size, max_bcast_size); if (q->callback) { ((void (*)(void *, int))(q->callback))(q->callback_extra, 1); @@ -447,7 +450,7 @@ static struct query_methods phone_call_methods = { void tgl_do_phone_call (const char *user, const char *hash,void (*callback)(void *callback_extra, int success), void *callback_extra) { vlogprintf (E_DEBUG, "calling user\n"); //suser = tstrdup (user); - want_dc_num = 0; + //want_dc_num = 0; clear_packet (); tgl_do_insert_header (); out_int (CODE_auth_send_call); diff --git a/tgl.c b/tgl.c index a1f0017..594bd78 100644 --- a/tgl.c +++ b/tgl.c @@ -60,3 +60,4 @@ int tgl_signed_dc (struct dc *DC) { assert (DC); return DC->has_auth; } + diff --git a/tgl.h b/tgl.h index a9152da..59b4d2b 100644 --- a/tgl.h +++ b/tgl.h @@ -33,19 +33,14 @@ struct bingnum_ctx; #define TGL_UPDATE_ACCESS_HASH 16384 struct tgl_allocator { - void *(*talloc)(size_t size); - void *(*trealloc)(void *ptr, size_t old_size, size_t size); - void *(*talloc0)(size_t size); - char *(*tstrdup)(const char *s); - char *(*tstrndup)(const char *s, size_t n); - void (*tfree)(void *ptr, int size); - void (*tfree_str)(void *ptr); - void (*tfree_secure)(void *ptr, int size); - int (*tasprintf)(char **res, const char *format, ...) __attribute__ ((format (printf, 2, 3))); - int (*tsnprintf)(char *buf, int len, const char *format, ...) __attribute__ ((format (printf, 3, 4))); - void (*tcheck)(void); - void (*texists)(void *ptr, int size); + void *(*alloc)(size_t size); + void *(*realloc)(void *ptr, size_t old_size, size_t size); + void (*free)(void *ptr, int size); + void (*check)(void); + void (*exists)(void *ptr, int size); }; +extern struct tgl_allocator tgl_allocator_release; +extern struct tgl_allocator tgl_allocator_debug; struct tgl_update_callback { void (*new_msg)(struct tgl_message *M); diff --git a/tools.c b/tools.c index f674dba..ff86041 100644 --- a/tools.c +++ b/tools.c @@ -36,7 +36,6 @@ //#include "interface.h" #include "tools.h" -#ifdef DEBUG #define RES_PRE 8 #define RES_AFTER 8 #define MAX_BLOCKS 1000000 @@ -44,8 +43,8 @@ static void *blocks[MAX_BLOCKS]; static void *free_blocks[MAX_BLOCKS]; static int used_blocks; static int free_blocks_cnt; -#endif +static long long total_allocated_bytes; void logprintf (const char *format, ...) __attribute__ ((format (printf, 1, 2), weak)); void logprintf (const char *format, ...) { @@ -55,11 +54,11 @@ void logprintf (const char *format, ...) { va_end (ap); } -extern int verbosity; +//extern int verbosity; -long long total_allocated_bytes; +//static long long total_allocated_bytes; -int tsnprintf (char *buf, int len, const char *format, ...) { +int tgl_snprintf (char *buf, int len, const char *format, ...) { va_list ap; va_start (ap, format); int r = vsnprintf (buf, len, format, ap); @@ -68,7 +67,7 @@ int tsnprintf (char *buf, int len, const char *format, ...) { return r; } -int tasprintf (char **res, const char *format, ...) { +int tgl_asprintf (char **res, const char *format, ...) { va_list ap; va_start (ap, format); int r = vasprintf (res, format, ap); @@ -81,9 +80,7 @@ int tasprintf (char **res, const char *format, ...) { return r; } -void print_backtrace (void); -void tfree (void *ptr, int size __attribute__ ((unused))) { -#ifdef DEBUG +void tgl_free_debug (void *ptr, int size __attribute__ ((unused))) { total_allocated_bytes -= size; ptr -= RES_PRE; if (size != (int)((*(int *)ptr) ^ 0xbedabeda)) { @@ -107,36 +104,28 @@ void tfree (void *ptr, int size __attribute__ ((unused))) { memset (ptr, 0, size + RES_PRE + RES_AFTER); *(int *)ptr = size + 12; free_blocks[free_blocks_cnt ++] = ptr; -#else +} + +void tgl_free_release (void *ptr, int size) { free (ptr); -#endif } -void tfree_str (void *ptr) { - if (!ptr) { return; } - tfree (ptr, strlen (ptr) + 1); -} -void tfree_secure (void *ptr, int size) { - memset (ptr, 0, size); - tfree (ptr, size); -} -void *trealloc (void *ptr, size_t old_size __attribute__ ((unused)), size_t size) { -#ifdef DEBUG +void *tgl_realloc_debug (void *ptr, size_t old_size __attribute__ ((unused)), size_t size) { void *p = talloc (size); memcpy (p, ptr, size >= old_size ? old_size : size); tfree (ptr, old_size); return p; -#else +} + +void *tgl_realloc_release (void *ptr, size_t old_size __attribute__ ((unused)), size_t size) { void *p = realloc (ptr, size); ensure_ptr (p); return p; -#endif } -void *talloc (size_t size) { -#ifdef DEBUG +void *tgl_alloc_debug (size_t size) { total_allocated_bytes += size; void *p = malloc (size + RES_PRE + RES_AFTER); ensure_ptr (p); @@ -151,49 +140,34 @@ void *talloc (size_t size) { } tcheck (); return p + 8; -#else +} + +void *tgl_alloc_release (size_t size) { void *p = malloc (size); ensure_ptr (p); return p; -#endif } -void *talloc0 (size_t size) { +void *tgl_alloc0 (size_t size) { void *p = talloc (size); memset (p, 0, size); return p; } -char *tstrdup (const char *s) { -#ifdef DEBUG +char *tgl_strdup (const char *s) { int l = strlen (s); char *p = talloc (l + 1); memcpy (p, s, l + 1); return p; -#else - char *p = strdup (s); - if (p == NULL) { - out_of_memory (); - } - return p; -#endif } -char *tstrndup (const char *s, size_t n) { -#ifdef DEBUG +char *tgl_strndup (const char *s, size_t n) { size_t l = 0; for (l = 0; l < n && s[l]; l++) { } char *p = talloc (l + 1); memcpy (p, s, l); p[l] = 0; return p; -#else - char *p = strndup (s, n); - if (p == NULL) { - out_of_memory (); - } - return p; -#endif } @@ -205,23 +179,19 @@ int tgl_inflate (void *input, int ilen, void *output, int olen) { strm.next_in = input; strm.avail_out = olen ; strm.next_out = output; - int err = inflate (&strm, Z_FINISH), total_out = 0; - if (err == Z_OK || err == Z_STREAM_END) { - total_out = (int) strm.total_out; - if (err == Z_STREAM_END && verbosity >= 2) { - logprintf ( "inflated %d bytes\n", (int) strm.total_out); - } - } - if (verbosity && err != Z_STREAM_END) { + int err = inflate (&strm, Z_FINISH); + int total_out = strm.total_out; + + if (err != Z_OK && err != Z_STREAM_END) { logprintf ( "inflate error = %d\n", err); logprintf ( "inflated %d bytes\n", (int) strm.total_out); + total_out = 0; } inflateEnd (&strm); return total_out; } -#ifdef DEBUG -void tcheck (void) { +void tgl_check_debug (void) { int i; for (i = 0; i < used_blocks; i++) { void *ptr = blocks[i]; @@ -248,7 +218,7 @@ void tcheck (void) { //logprintf ("ok. Used_blocks = %d. Free blocks = %d\n", used_blocks, free_blocks_cnt); } -void texists (void *ptr, int size) { +void tgl_exists_debug (void *ptr, int size) { ptr -= RES_PRE; if (size != (int)((*(int *)ptr) ^ 0xbedabeda)) { logprintf ("size = %d, ptr = %d\n", size, (*(int *)ptr) ^ 0xbedabeda); @@ -262,7 +232,9 @@ void texists (void *ptr, int size) { } assert (block_num < used_blocks); } -#endif + +void tgl_exists_release (void *ptr, int size) {} +void tgl_check_release (void) {} void tgl_my_clock_gettime (int clock_id, struct timespec *T) { #ifdef __MACH__ @@ -295,3 +267,20 @@ void tglt_secure_random (void *s, int l) { } } +struct tgl_allocator tgl_allocator_debug = { + .alloc = tgl_alloc_debug, + .realloc = tgl_realloc_debug, + .free = tgl_free_debug, + .check = tgl_check_debug, + .exists = tgl_exists_debug +}; + +struct tgl_allocator tgl_allocator_release = { + .alloc = tgl_alloc_release, + .realloc = tgl_realloc_release, + .free = tgl_free_release, + .check = tgl_check_release, + .exists = tgl_exists_release +}; + +struct tgl_allocator *tgl_allocator = &tgl_allocator_release; diff --git a/tools.h b/tools.h index 4a2c1c6..cf20027 100644 --- a/tools.h +++ b/tools.h @@ -22,14 +22,25 @@ #include #include #include +#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 tstrndup tgl_strndup +#define tasprintf tgl_asprintf +#define tsnprintf tgl_snprintf + + +struct tgl_allocator *tgl_allocator; double tglt_get_double_time (void); -void *talloc (size_t size); -void *trealloc (void *ptr, size_t old_size, size_t size); -void *talloc0 (size_t size); -char *tstrdup (const char *s); -char *tstrndup (const char *s, size_t n); int tgl_inflate (void *input, int ilen, void *output, int olen); //void ensure (int r); //void ensure_ptr (void *p); @@ -53,20 +64,56 @@ static inline void ensure_ptr (void *p) { } } -void tfree (void *ptr, int size); -void tfree_str (void *ptr); -void tfree_secure (void *ptr, int size); +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); -int tsnprintf (char *buf, int len, const char *format, ...) __attribute__ ((format (printf, 3, 4))); -int tasprintf (char **res, const char *format, ...) __attribute__ ((format (printf, 2, 3))); +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); -#ifdef DEBUG -void tcheck (void); -void texists (void *ptr, int size); +static inline void tgl_free_str (void *ptr) { + if (!ptr) { return; } + tfree (ptr, strlen (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; + while (ptr < end_ptr) { + fprintf (stderr, "%08x", (int)*(char *)ptr); + ptr ++; + total ++; + if (total == 16) { + fprintf (stderr, "\n"); + total = 0; + } + } + if (total) { fprintf (stderr, "\n"); } +} + #endif -#endif