Provide an up to date version of g_base64_decode() for older GLib.
[claws.git] / src / common / utils.h
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The code of the g_utf8_substring function below is owned by
19  * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
20  * and is got from GLIB 2.30
21  *
22  */
23
24 #ifndef __UTILS_H__
25 #define __UTILS_H__
26
27 #ifdef HAVE_CONFIG_H
28 #include "claws-features.h"
29 #endif
30
31 #ifdef HAVE_BACKTRACE
32 #include <execinfo.h>
33 #endif
34
35 #include <glib.h>
36 #include <glib-object.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <dirent.h>
43 #include <time.h>
44 #if HAVE_ALLOCA_H
45 #  include <alloca.h>
46 #endif
47 #if HAVE_WCHAR_H
48 #  include <wchar.h>
49 #endif
50
51 /* The Hurd doesn't have this limit */
52 #ifndef PATH_MAX
53   #define PATH_MAX 4196
54 #endif
55
56 #ifdef G_OS_WIN32
57
58 #define fsync _commit
59
60 #define pipe(phandles)  _pipe (phandles, 4096, _O_BINARY)
61 #endif
62 /* Wrappers for C library function that take pathname arguments. */
63 #  include <glib/gstdio.h>
64
65 /* why is this sometimes undefined !? */
66 #ifndef G_MAXOFFSET
67 typedef gint64 goffset;
68 #define G_MINOFFSET     G_MININT64
69 #define G_MAXOFFSET     G_MAXINT64
70 #endif
71
72 #ifndef HAVE_U32_TYPEDEF
73   #undef u32        /* maybe there is a macro with this name */
74   typedef guint32 u32;
75   #define HAVE_U32_TYPEDEF
76 #endif
77
78 #if !GLIB_CHECK_VERSION(2, 26, 0)
79 #define g_base64_decode(t,l)    g_base64_decode_wa((t),(l))
80 guchar *g_base64_decode_wa(const gchar *text, gsize *out_len);
81 #endif
82
83 #if !GLIB_CHECK_VERSION(2, 25, 0)
84 # ifdef G_OS_WIN32
85         typedef _g_stat_struct GStatBuf;
86 # else
87         typedef struct stat GStatBuf;
88 # endif
89 #endif
90
91 #ifndef BIG_ENDIAN_HOST
92   #if (G_BYTE_ORDER == G_BIG_ENDIAN)
93     #define BIG_ENDIAN_HOST 1
94   #endif
95 #endif
96
97 #define CHDIR_RETURN_IF_FAIL(dir) \
98 { \
99         if (change_dir(dir) < 0) return; \
100 }
101
102 #define CHDIR_RETURN_VAL_IF_FAIL(dir, val) \
103 { \
104         if (change_dir(dir) < 0) return val; \
105 }
106
107 #define CHDIR_EXEC_CODE_RETURN_VAL_IF_FAIL(dir, val, code) \
108 { \
109         if (change_dir(dir) < 0) { \
110                 code \
111                 return val; \
112         } \
113 }
114
115 #define Xalloca(ptr, size, iffail) \
116 { \
117         if ((ptr = alloca(size)) == NULL) { \
118                 g_warning("can't allocate memory\n"); \
119                 iffail; \
120         } \
121 }
122
123 #define Xstrdup_a(ptr, str, iffail) \
124 { \
125         gchar *__tmp; \
126  \
127         if ((__tmp = alloca(strlen(str) + 1)) == NULL) { \
128                 g_warning("can't allocate memory\n"); \
129                 iffail; \
130         } else \
131                 strcpy(__tmp, str); \
132  \
133         ptr = __tmp; \
134 }
135
136 #define Xstrndup_a(ptr, str, len, iffail) \
137 { \
138         gchar *__tmp; \
139  \
140         if ((__tmp = alloca(len + 1)) == NULL) { \
141                 g_warning("can't allocate memory\n"); \
142                 iffail; \
143         } else { \
144                 strncpy(__tmp, str, len); \
145                 __tmp[len] = '\0'; \
146         } \
147  \
148         ptr = __tmp; \
149 }
150
151 #define Xstrcat_a(ptr, str1, str2, iffail) \
152 { \
153         gchar *__tmp; \
154         gint len1, len2; \
155  \
156         len1 = strlen(str1); \
157         len2 = strlen(str2); \
158         if ((__tmp = alloca(len1 + len2 + 1)) == NULL) { \
159                 g_warning("can't allocate memory\n"); \
160                 iffail; \
161         } else { \
162                 memcpy(__tmp, str1, len1); \
163                 memcpy(__tmp + len1, str2, len2 + 1); \
164         } \
165  \
166         ptr = __tmp; \
167 }
168
169 #define AUTORELEASE_STR(str, iffail) \
170 { \
171         gchar *__str; \
172         Xstrdup_a(__str, str, iffail); \
173         g_free(str); \
174         str = __str; \
175 }
176
177 #define FILE_OP_ERROR(file, func) \
178 { \
179         g_printerr("%s: ", file); \
180         fflush(stderr); \
181         perror(func); \
182 }
183
184 #define IS_ASCII(c) (((guchar) c) <= 0177 ? 1 : 0)
185
186 /* from NetworkManager */
187 #if (defined(HAVE_BACKTRACE) && !defined(__FreeBSD__))
188 #define print_backtrace()                                               \
189 G_STMT_START                                                            \
190 {                                                                       \
191         void *_call_stack[512];                                         \
192         int  _call_stack_size;                                          \
193         char **_symbols;                                                \
194         _call_stack_size = backtrace (_call_stack,                      \
195                                       G_N_ELEMENTS (_call_stack));      \
196         _symbols = backtrace_symbols (_call_stack, _call_stack_size);   \
197         if (_symbols != NULL)                                           \
198         {                                                               \
199                 int _i;                                                 \
200                 _i = 0;                                                 \
201                 g_print ("traceback:\n");                               \
202                 while (_i < _call_stack_size)                           \
203                 {                                                       \
204                         g_print ("%d:\t%s\n", _i, _symbols[_i]);        \
205                         _i++;                                           \
206                 }                                                       \
207                 free (_symbols);                                        \
208         }                                                               \
209 }                                                                       \
210 G_STMT_END
211 #else
212 #define print_backtrace()                                               \
213 G_STMT_START                                                            \
214 {                                                                       \
215 }                                                                       \
216 G_STMT_END
217 #endif
218
219
220 #define cm_return_val_if_fail(expr,val) G_STMT_START {                  \
221         if (!(expr)) {                                                  \
222                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
223                 print_backtrace();                                      \
224                 g_print("\n");                                          \
225                 return val;                                             \
226         }                                                               \
227 } G_STMT_END
228
229 #define cm_return_if_fail(expr) G_STMT_START {                          \
230         if (!(expr)) {                                                  \
231                 g_print("%s:%d Condition %s failed\n", __FILE__, __LINE__, #expr);\
232                 print_backtrace();                                      \
233                 g_print("\n");                                          \
234                 return;                                                 \
235         }                                                               \
236 } G_STMT_END
237
238 #ifndef MIN
239         #define MIN(a, b) ((a) < (b) ? (a) : (b))
240 #endif
241 #ifndef MAX
242         #define MAX(a, b) ((a) > (b) ? (a) : (b))
243 #endif
244
245 #ifdef __cplusplus
246 extern "C" {
247 #endif
248
249 typedef gpointer (*GNodeMapFunc)        (gpointer nodedata, gpointer data);
250
251 /* debug functions */
252 void debug_set_mode             (gboolean mode);
253 gboolean debug_get_mode         (void);
254
255 #ifndef __CYGWIN__
256 #define debug_print \
257         debug_print_real("%s:%d:", debug_srcname(__FILE__), __LINE__), \
258         debug_print_real
259 #else
260   /* FIXME: cygwin: why debug_srcname couldn't be resolved in library? */
261 #define debug_print \
262         debug_print_real("%s:%d:", __FILE__, __LINE__), \
263         debug_print_real
264 #endif
265
266 /* for macro expansion */
267 #define Str(x)  #x
268 #define Xstr(x) Str(x)
269
270
271 /* System related stuff.  */
272
273 gboolean superuser_p (void);
274
275 /* List utilities. */
276
277 GSList *slist_copy_deep         (GSList         *list,
278                                  GCopyFunc       func);
279
280 /* String utilities.  */
281
282 void list_free_strings          (GList          *list);
283 void slist_free_strings         (GSList         *list);
284 void slist_free_strings_full    (GSList         *list);
285
286 void hash_free_strings          (GHashTable     *table);
287
288 gint str_case_equal             (gconstpointer   v,
289                                  gconstpointer   v2);
290 guint str_case_hash             (gconstpointer   key);
291
292 void ptr_array_free_strings     (GPtrArray      *array);
293
294 /* number-string conversion */
295 gint to_number                  (const gchar *nstr);
296 gchar *itos_buf                 (gchar       *nstr,
297                                  gint         n);
298 gchar *itos                     (gint         n);
299 gchar *to_human_readable        (goffset      size);
300
301 /* alternative string functions */
302 gint strcmp2            (const gchar    *s1,
303                          const gchar    *s2);
304 gchar *strstr2          (const gchar    *s1,
305                          const gchar    *s2);
306 gint path_cmp           (const gchar    *s1,
307                          const gchar    *s2);
308 gchar *strretchomp      (gchar          *str);
309 gchar *strtailchomp     (gchar          *str,
310                          gchar           tail_char);
311 gchar *strcrchomp       (gchar          *str);
312 gint file_strip_crs     (const gchar    *file);
313 gchar *strcasestr       (const gchar    *haystack,
314                          const gchar    *needle);
315 gchar *strncasestr      (const gchar    *haystack,
316                          gint            haystack_len,
317                          const gchar    *needle);
318 gpointer my_memmem      (gconstpointer   haystack,
319                          size_t          haystacklen,
320                          gconstpointer   needle,
321                          size_t          needlelen);
322 gchar *strncpy2         (gchar          *dest,
323                          const gchar    *src,
324                          size_t          n);
325
326 gboolean is_next_nonascii       (const gchar *s);
327 gint get_next_word_len          (const gchar *s);
328
329 /* functions for string parsing */
330 gint subject_compare                    (const gchar    *s1,
331                                          const gchar    *s2);
332 gint subject_compare_for_sort           (const gchar    *s1,
333                                          const gchar    *s2);
334 void trim_subject                       (gchar          *str);
335 void eliminate_parenthesis              (gchar          *str,
336                                          gchar           op,
337                                          gchar           cl);
338 void extract_parenthesis                (gchar          *str,
339                                          gchar           op,
340                                          gchar           cl);
341
342 void extract_quote                      (gchar          *str,
343                                          gchar           quote_chr);
344 gchar *escape_internal_quotes           (gchar          *str,
345                                          gchar           quote_chr);
346 void eliminate_address_comment          (gchar          *str);
347 gchar *strchr_with_skip_quote           (const gchar    *str,
348                                          gint            quote_chr,
349                                          gint            c);
350 void extract_address                    (gchar          *str);
351 void extract_list_id_str                (gchar          *str);
352
353 GSList *address_list_append             (GSList         *addr_list,
354                                          const gchar    *str);
355 GSList *address_list_append_with_comments(GSList        *addr_list,
356                                          const gchar    *str);
357 GSList *references_list_prepend         (GSList         *msgid_list,
358                                          const gchar    *str);
359 GSList *references_list_append          (GSList         *msgid_list,
360                                          const gchar    *str);
361 GSList *newsgroup_list_append           (GSList         *group_list,
362                                          const gchar    *str);
363
364 GList *add_history                      (GList          *list,
365                                          const gchar    *str);
366
367 void remove_return                      (gchar          *str);
368 void remove_space                       (gchar          *str);
369 void unfold_line                        (gchar          *str);
370 void subst_char                         (gchar          *str,
371                                          gchar           orig,
372                                          gchar           subst);
373 void subst_chars                        (gchar          *str,   
374                                          gchar          *orig, 
375                                          gchar          subst);
376 void subst_for_filename                 (gchar          *str);
377 void subst_for_shellsafe_filename       (gchar          *str);
378 gboolean is_ascii_str                   (const gchar    *str);
379 gint get_quote_level                    (const gchar    *str,
380                                          const gchar    *quote_chars);
381 gint check_line_length                  (const gchar    *str,
382                                          gint            max_chars,
383                                          gint           *line);
384
385 gchar **strsplit_with_quote             (const gchar    *str,
386                                          const gchar    *delim,
387                                          gint            max_tokens);
388
389 gchar *get_abbrev_newsgroup_name        (const gchar    *group,
390                                          gint            len);
391 gchar *trim_string                      (const gchar    *str,
392                                          gint            len);
393
394 GList *uri_list_extract_filenames       (const gchar    *uri_list);
395 gboolean is_uri_string                  (const gchar    *str);
396 gchar *get_uri_path                     (const gchar    *uri);
397 gint get_uri_len                        (const gchar    *str);
398 void decode_uri                         (gchar          *decoded_uri,
399                                          const gchar    *encoded_uri);
400 void decode_uri_with_plus               (gchar          *decoded_uri, 
401                                          const gchar    *encoded_uri, 
402                                          gboolean        with_plus);
403 gint scan_mailto_url                    (const gchar    *mailto,
404                                          gchar         **from,
405                                          gchar         **to,
406                                          gchar         **cc,
407                                          gchar         **bcc,
408                                          gchar         **subject,
409                                          gchar         **body,
410                                          gchar         ***attach,
411                                          gchar         **inreplyto);
412
413 /* return static strings */
414 const gchar *get_home_dir               (void);
415 const gchar *get_rc_dir                 (void);
416 void  set_rc_dir                        (const gchar *dir);
417 gboolean rc_dir_is_alt                  (void);
418 const gchar *get_mail_base_dir          (void);
419 const gchar *get_news_cache_dir         (void);
420 const gchar *get_imap_cache_dir         (void);
421 const gchar *get_mime_tmp_dir           (void);
422 const gchar *get_template_dir           (void);
423 const gchar *get_plugin_dir             (void);
424 const gchar *get_tmp_dir                (void);
425 const gchar *get_locale_dir             (void);
426 gchar *get_tmp_file                     (void);
427 const gchar *get_domain_name            (void);
428 const gchar *get_desktop_file(void);
429 #ifdef G_OS_WIN32
430 const gchar *get_themes_dir             (void);
431 const gchar *get_cert_file              (void);
432 #endif
433 /* file / directory handling */
434 off_t get_file_size             (const gchar    *file);
435 off_t get_file_size_as_crlf     (const gchar    *file);
436
437 time_t get_file_mtime           (const gchar *file);
438
439 gboolean file_exist             (const gchar    *file,
440                                  gboolean        allow_fifo);
441 gboolean is_relative_filename   (const gchar *file);
442 gboolean is_dir_exist           (const gchar    *dir);
443 gboolean is_file_entry_exist    (const gchar    *file);
444 gboolean dirent_is_regular_file (struct dirent  *d);
445
446 #define is_file_exist(file)             file_exist(file, FALSE)
447 #define is_file_or_fifo_exist(file)     file_exist(file, TRUE)
448
449 gint change_dir                 (const gchar    *dir);
450 gint make_dir                   (const gchar    *dir);
451 gint make_dir_hier              (const gchar    *dir);
452 gint remove_all_files           (const gchar    *dir);
453 gint remove_numbered_files      (const gchar    *dir,
454                                  guint           first,
455                                  guint           last);
456 gint remove_numbered_files_not_in_list(const gchar *dir,
457                                        GSList *numberlist);
458 gint remove_all_numbered_files  (const gchar    *dir);
459 gint remove_dir_recursive       (const gchar    *dir);
460 gint append_file                (const gchar    *src,
461                                  const gchar    *dest,
462                                  gboolean        keep_backup);
463 gint rename_force               (const gchar    *oldpath,
464                                  const gchar    *newpath);
465 gint copy_file                  (const gchar    *src,
466                                  const gchar    *dest,
467                                  gboolean        keep_backup);
468 gint move_file                  (const gchar    *src,
469                                  const gchar    *dest,
470                                  gboolean        overwrite);
471 gint copy_dir                   (const gchar    *src,
472                                  const gchar    *dest);
473 gint copy_file_part_to_fp       (FILE           *fp,
474                                  off_t           offset,
475                                  size_t          length,
476                                  FILE           *dest_fp);
477 gint copy_file_part             (FILE           *fp,
478                                  off_t           offset,
479                                  size_t          length,
480                                  const gchar    *dest);
481
482 gchar *canonicalize_str         (const gchar    *str);
483 gint canonicalize_file          (const gchar    *src,
484                                  const gchar    *dest);
485 gint canonicalize_file_replace  (const gchar    *file);
486
487 gchar *normalize_newlines       (const gchar    *str);
488
489 gchar *get_outgoing_rfc2822_str (FILE           *fp);
490
491 gint change_file_mode_rw        (FILE           *fp,
492                                  const gchar    *file);
493 FILE *my_tmpfile                (void);
494 FILE *get_tmpfile_in_dir        (const gchar    *dir,
495                                  gchar         **filename);
496 FILE *str_open_as_stream        (const gchar    *str);
497 gint str_write_to_file          (const gchar    *str,
498                                  const gchar    *file);
499 gchar *file_read_to_str         (const gchar    *file);
500 gchar *file_read_stream_to_str  (FILE           *fp);
501 gchar *file_read_to_str_no_recode(const gchar *file);
502 gchar *file_read_stream_to_str_no_recode(FILE *fp);
503
504 char *fgets_crlf(char *buf, int size, FILE *stream);
505
506 /* process execution */
507 gint execute_command_line       (const gchar    *cmdline,
508                                  gboolean        async);
509 gchar *get_command_output       (const gchar    *cmdline);
510
511 /* open URI with external browser */
512 gint open_uri(const gchar *uri, const gchar *cmdline);
513 /* open file with text editor */
514 gint open_txt_editor(const gchar *filepath, const gchar *cmdline);
515
516 /* time functions */
517 time_t remote_tzoffset_sec      (const gchar    *zone);
518 time_t tzoffset_sec             (time_t         *now);
519 gchar *tzoffset                 (time_t         *now);
520 void get_rfc822_date            (gchar          *buf,
521                                  gint            len);
522
523 size_t fast_strftime            (gchar                  *buf, 
524                                  gint                    buflen, 
525                                  const gchar            *format, 
526                                  struct tm              *lt);
527
528 /* debugging */
529 void debug_print_real   (const gchar *format, ...) G_GNUC_PRINTF(1, 2);
530 const char * debug_srcname (const char *file);
531
532 /* subject threading */
533 void * subject_table_lookup(GHashTable *subject_table, gchar * subject);
534 void subject_table_insert(GHashTable *subject_table, gchar * subject,
535                           void * data);
536 void subject_table_remove(GHashTable *subject_table, gchar * subject);
537 void utils_free_regex(void);
538 gint subject_get_prefix_length (const gchar *subject);
539
540 /* quoting recognition */
541 const gchar * line_has_quote_char       (const gchar *str,
542                                          const gchar *quote_chars);
543
544 gint g_int_compare      (gconstpointer a, gconstpointer b);
545
546 gchar *generate_msgid           (gchar *buf, gint len, gchar *user_addr);
547 gchar *generate_mime_boundary   (const gchar *prefix);
548
549 gint quote_cmd_argument(gchar * result, guint size,
550                         const gchar * path);
551 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data);
552
553 gboolean get_hex_value(guchar *out, gchar c1, gchar c2);
554 void get_hex_str(gchar *out, guchar ch);
555
556 /* auto pointer for containers that support GType system */
557
558 #define G_TYPE_AUTO_POINTER     g_auto_pointer_register()
559 typedef struct AutoPointer      GAuto;
560 GType g_auto_pointer_register           (void);
561 GAuto *g_auto_pointer_new               (gpointer pointer);
562 GAuto *g_auto_pointer_new_with_free     (gpointer p, 
563                                          GFreeFunc free);
564 gpointer g_auto_pointer_get_ptr         (GAuto *auto_ptr);
565 GAuto *g_auto_pointer_copy              (GAuto *auto_ptr);
566 void g_auto_pointer_free                (GAuto *auto_ptr);
567 void replace_returns                    (gchar *str);
568 gboolean get_uri_part   (const gchar *start,
569                          const gchar *scanpos,
570                          const gchar **bp,
571                          const gchar **ep,
572                          gboolean hdr);
573 gchar *make_uri_string  (const gchar *bp,
574                          const gchar *ep);
575 gboolean get_email_part (const gchar *start, 
576                          const gchar *scanpos,
577                          const gchar **bp, 
578                          const gchar **ep,
579                          gboolean hdr);
580 gchar *make_email_string(const gchar *bp,
581                          const gchar *ep);
582 gchar *make_http_string (const gchar *bp,
583                          const gchar *ep);
584
585 gchar *mailcap_get_command_for_type(const gchar *type, 
586                                     const gchar *file_to_open);
587 void mailcap_update_default        (const gchar *type,
588                                     const gchar *command);
589
590 gboolean file_is_email(const gchar *filename);
591 gboolean sc_g_list_bigger(GList *list, gint max);
592 gboolean sc_g_slist_bigger(GSList *list, gint max);
593
594 int claws_unlink(const gchar *filename);
595
596 GMutex *cm_mutex_new(void);
597 void cm_mutex_free(GMutex *mutex);
598
599 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
600
601 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
602
603 #if !GLIB_CHECK_VERSION(2, 30, 0)
604 gchar   *g_utf8_substring         (const gchar *p,
605                                    glong        start_pos,
606                                    glong        end_pos) G_GNUC_MALLOC;
607 #endif
608
609 #ifdef __cplusplus
610 }
611 #endif
612
613 #endif /* __UTILS_H__ */