| 1 |
/* Declarations for utils.c. |
| 2 |
Copyright (C) 1996-2011, 2015, 2018-2024 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 <wget.h> |
| 35 |
#include <stdlib.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_from_file (const char *, bool *); |
| 110 |
struct file_memory *wget_read_file (const char *); |
| 111 |
void wget_read_file_free (struct file_memory *); |
| 112 |
|
| 113 |
void free_vec (char **); |
| 114 |
char **merge_vecs (char **, char **); |
| 115 |
char **vec_append (char **, const char *); |
| 116 |
|
| 117 |
void string_set_add (struct hash_table *, const char *); |
| 118 |
int string_set_contains (struct hash_table *, const char *); |
| 119 |
void string_set_to_array (struct hash_table *, char **); |
| 120 |
void string_set_free (struct hash_table *); |
| 121 |
void free_keys_and_values (struct hash_table *); |
| 122 |
|
| 123 |
const char *with_thousand_seps (wgint); |
| 124 |
|
| 125 |
/* human_readable must be able to accept wgint arguments. */ |
| 126 |
char *human_readable (wgint, const int, const int); |
| 127 |
|
| 128 |
|
| 129 |
int numdigit (wgint); |
| 130 |
char *number_to_string (char *, wgint); |
| 131 |
char *number_to_static_string (wgint); |
| 132 |
wgint convert_to_bits (wgint); |
| 133 |
|
| 134 |
int determine_screen_width (void); |
| 135 |
int random_number (int); |
| 136 |
double random_float (void); |
| 137 |
|
| 138 |
bool run_with_timeout (double, void (*) (void *), void *); |
| 139 |
void xsleep (double); |
| 140 |
|
| 141 |
/* How many bytes it will take to store LEN bytes in base64. */ |
| 142 |
#define BASE64_LENGTH(len) (4 * (((len) + 2) / 3)) |
| 143 |
|
| 144 |
size_t wget_base64_encode (const void *, size_t, char *); |
| 145 |
ssize_t wget_base64_decode (const char *, void *, size_t); |
| 146 |
|
| 147 |
#ifdef HAVE_LIBPCRE2 |
| 148 |
void *compile_pcre2_regex (const char *); |
| 149 |
bool match_pcre2_regex (const void *, const char *); |
| 150 |
#endif |
| 151 |
|
| 152 |
#ifdef HAVE_LIBPCRE |
| 153 |
void *compile_pcre_regex (const char *); |
| 154 |
bool match_pcre_regex (const void *, const char *); |
| 155 |
#endif |
| 156 |
|
| 157 |
void *compile_posix_regex (const char *); |
| 158 |
bool match_posix_regex (const void *, const char *); |
| 159 |
|
| 160 |
void stable_sort (void *, size_t, size_t, int (*) (const void *, const void *)); |
| 161 |
|
| 162 |
const char *print_decimal (double); |
| 163 |
|
| 164 |
long get_max_length (const char *path, int length, int name); |
| 165 |
|
| 166 |
#ifndef HAVE_STRLCPY |
| 167 |
size_t strlcpy (char * restrict dst, const char * restrict src, size_t size); |
| 168 |
#endif |
| 169 |
|
| 170 |
void wg_hex_to_string (char *str_buffer, const char *hex_buffer, size_t hex_len); |
| 171 |
|
| 172 |
extern unsigned char char_prop[]; |
| 173 |
|
| 174 |
#ifdef HAVE_SSL |
| 175 |
/* Check pinned public key. */ |
| 176 |
bool wg_pin_peer_pubkey (const char *pinnedpubkey, const char *pubkey, size_t pubkeylen); |
| 177 |
#endif |
| 178 |
|
| 179 |
#endif /* UTILS_H */ |