1 /* Declarations for utils.c.
2    Copyright (C) 1996-2011, 2015, 2018-2021 Free Software Foundation,
3    Inc.
4 
5 This file is part of GNU Wget.
6 
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11 
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Wget.  If not, see <http://www.gnu.org/licenses/>.
19 
20 Additional permission under GNU GPL version 3 section 7
21 
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work.  */
30 
31 #ifndef UTILS_H
32 #define UTILS_H
33 
34 #include <stdlib.h>
35 #include <wget.h>
36 
37 /* Constant is using when we don`t know attempted size exactly */
38 #define UNKNOWN_ATTEMPTED_SIZE -3
39 
40 #ifndef MAX_PINNED_PUBKEY_SIZE
41 #define MAX_PINNED_PUBKEY_SIZE 1048576 /* 1MB */
42 #endif
43 
44 /* Macros that interface to malloc, but know about type sizes, and
45    cast the result to the appropriate type.  The casts are not
46    necessary in standard C, but Wget performs them anyway for the sake
47    of pre-standard environments and possibly C++.  */
48 
49 #define xnew(type) (xmalloc (sizeof (type)))
50 #define xnew0(type) (xcalloc (1, sizeof (type)))
51 #define xnew_array(type, len) (xmalloc ((len) * sizeof (type)))
52 #define xnew0_array(type, len) (xcalloc ((len), sizeof (type)))
53 
54 #define xfree(p) do { free ((void *) (p)); p = NULL; } while (0)
55 
56 struct hash_table;
57 
58 struct file_memory {
59   char *content;
60   long length;
61   int mmap_p;
62 };
63 
64 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
65 
66 char *time_str (time_t);
67 char *datetime_str (time_t);
68 
69 char *xstrdup_lower (const char *);
70 
71 char *strdupdelim (const char *, const char *);
72 char **sepstring (const char *);
73 bool subdir_p (const char *, const char *);
74 bool fork_to_background (void);
75 
76 char *aprintf (const char *, ...) GCC_FORMAT_ATTR (1, 2);
77 char *concat_strings (const char *, ...);
78 
79 typedef struct file_stat_s {
80   int access_err;               /* Error in accecssing file : Not present vs permission */
81   ino_t st_ino;                 /* st_ino from stats() on the file before open() */
82   dev_t st_dev;                 /* st_dev from stats() on the file before open() */
83 } file_stats_t;
84 
85 void touch (const char *, time_t);
86 int remove_link (const char *);
87 bool file_exists_p (const char *, file_stats_t *);
88 bool file_non_directory_p (const char *);
89 wgint file_size (const char *);
90 int make_directory (const char *);
91 char *unique_name_passthrough (const char *);
92 char *unique_name (const char *);
93 FILE *unique_create (const char *, bool, char **);
94 FILE *fopen_excl (const char *, int);
95 FILE *fopen_stat (const char *, const char *, file_stats_t *);
96 int   open_stat  (const char *, int, mode_t, file_stats_t *);
97 char *file_merge (const char *, const char *);
98 
99 int fnmatch_nocase (const char *, const char *, int);
100 bool acceptable (const char *);
101 bool accept_url (const char *);
102 bool accdir (const char *s);
103 char *suffix (const char *s);
104 bool match_tail (const char *, const char *, bool);
105 bool has_wildcards_p (const char *);
106 
107 bool has_html_suffix_p (const char *);
108 
109 struct file_memory *wget_read_file (const char *);
110 void wget_read_file_free (struct file_memory *);
111 
112 void free_vec (char **);
113 char **merge_vecs (char **, char **);
114 char **vec_append (char **, const char *);
115 
116 void string_set_add (struct hash_table *, const char *);
117 int string_set_contains (struct hash_table *, const char *);
118 void string_set_to_array (struct hash_table *, char **);
119 void string_set_free (struct hash_table *);
120 void free_keys_and_values (struct hash_table *);
121 
122 const char *with_thousand_seps (wgint);
123 
124 /* human_readable must be able to accept wgint arguments. */
125 char *human_readable (wgint, const int, const int);
126 
127 
128 int numdigit (wgint);
129 char *number_to_string (char *, wgint);
130 char *number_to_static_string (wgint);
131 wgint convert_to_bits (wgint);
132 
133 int determine_screen_width (void);
134 int random_number (int);
135 double random_float (void);
136 
137 bool run_with_timeout (double, void (*) (void *), void *);
138 void xsleep (double);
139 
140 /* How many bytes it will take to store LEN bytes in base64.  */
141 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
142 
143 size_t wget_base64_encode (const void *, size_t, char *);
144 ssize_t wget_base64_decode (const char *, void *, size_t);
145 
146 #ifdef HAVE_LIBPCRE2
147 void *compile_pcre2_regex (const char *);
148 bool match_pcre2_regex (const void *, const char *);
149 #endif
150 
151 #ifdef HAVE_LIBPCRE
152 void *compile_pcre_regex (const char *);
153 bool match_pcre_regex (const void *, const char *);
154 #endif
155 
156 void *compile_posix_regex (const char *);
157 bool match_posix_regex (const void *, const char *);
158 
159 void stable_sort (void *, size_t, size_t, int (*) (const void *, const void *));
160 
161 const char *print_decimal (double);
162 
163 long get_max_length (const char *path, int length, int name);
164 
165 #ifndef HAVE_STRLCPY
166 size_t strlcpy (char *dst, const char *src, size_t size);
167 #endif
168 
169 void wg_hex_to_string (char *str_buffer, const char *hex_buffer, size_t hex_len);
170 
171 extern unsigned char char_prop[];
172 
173 #ifdef HAVE_SSL
174 /* Check pinned public key. */
175 bool wg_pin_peer_pubkey (const char *pinnedpubkey, const char *pubkey, size_t pubkeylen);
176 #endif
177 
178 #endif /* UTILS_H */
179