1 | /****************************************************************************** |
---|
2 | * $Id: utils.h 5860 2008-05-20 17:33:54Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #ifndef TR_UTILS_H |
---|
26 | #define TR_UTILS_H 1 |
---|
27 | |
---|
28 | #include <inttypes.h> |
---|
29 | #include <stdarg.h> |
---|
30 | #include <stddef.h> /* for size_t */ |
---|
31 | #include <stdio.h> /* FILE* */ |
---|
32 | |
---|
33 | /*** |
---|
34 | **** |
---|
35 | ***/ |
---|
36 | |
---|
37 | #ifndef FALSE |
---|
38 | #define FALSE 0 |
---|
39 | #endif |
---|
40 | |
---|
41 | #ifndef TRUE |
---|
42 | #define TRUE 1 |
---|
43 | #endif |
---|
44 | |
---|
45 | #ifndef UNUSED |
---|
46 | #ifdef __GNUC__ |
---|
47 | #define UNUSED __attribute__((unused)) |
---|
48 | #else |
---|
49 | #define UNUSED |
---|
50 | #endif |
---|
51 | #endif |
---|
52 | |
---|
53 | #ifndef TR_GNUC_PRINTF |
---|
54 | #ifdef __GNUC__ |
---|
55 | #define TR_GNUC_PRINTF( fmt, args ) __attribute__((format (printf, fmt, args))) |
---|
56 | #else |
---|
57 | #define TR_GNUC_PRINTF( fmt, args ) |
---|
58 | #endif |
---|
59 | #endif |
---|
60 | |
---|
61 | #ifndef TR_GNUC_NULL_TERMINATED |
---|
62 | #if __GNUC__ >= 4 |
---|
63 | #define TR_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) |
---|
64 | #else |
---|
65 | #define TR_GNUC_NULL_TERMINATED |
---|
66 | #endif |
---|
67 | #endif |
---|
68 | |
---|
69 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) |
---|
70 | #define TR_GNUC_PURE __attribute__((__pure__)) |
---|
71 | #define TR_GNUC_MALLOC __attribute__((__malloc__)) |
---|
72 | #else |
---|
73 | #define TR_GNUC_PURE |
---|
74 | #define TR_GNUC_MALLOC |
---|
75 | #endif |
---|
76 | |
---|
77 | |
---|
78 | /*** |
---|
79 | **** |
---|
80 | ***/ |
---|
81 | |
---|
82 | #if !defined(_) |
---|
83 | #if defined(SYS_DARWIN) |
---|
84 | #define _(a) (a) |
---|
85 | #elif HAVE_LIBINTL |
---|
86 | #include <libintl.h> |
---|
87 | #define _(a) gettext (a) |
---|
88 | #else |
---|
89 | #define _(a) (a) |
---|
90 | #endif |
---|
91 | #endif |
---|
92 | |
---|
93 | #define tr_nerr( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, ## a ) |
---|
94 | #define tr_ninf( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, ## a ) |
---|
95 | #define tr_ndbg( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, ## a ) |
---|
96 | |
---|
97 | #define tr_torerr( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, ## a ) |
---|
98 | #define tr_torinf( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, ## a ) |
---|
99 | #define tr_tordbg( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, ## a ) |
---|
100 | |
---|
101 | #define tr_err( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, ## a ) |
---|
102 | #define tr_inf( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, ## a ) |
---|
103 | #define tr_dbg( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, ## a ) |
---|
104 | |
---|
105 | void tr_msgInit( void ); |
---|
106 | |
---|
107 | void tr_msg( const char * file, int line, |
---|
108 | int level, const char * torrent, |
---|
109 | const char * fmt, ... ) TR_GNUC_PRINTF( 5, 6 ); |
---|
110 | |
---|
111 | FILE* tr_getLog( void ); |
---|
112 | |
---|
113 | void tr_deepLog( const char * file, int line, |
---|
114 | const char * name, |
---|
115 | const char * fmt, ... ) TR_GNUC_PRINTF( 4, 5 ); |
---|
116 | |
---|
117 | char* tr_getLogTimeStr( char * buf, int buflen ); |
---|
118 | |
---|
119 | /** Returns a random number in the range of [0...n) */ |
---|
120 | int tr_rand ( int n ); |
---|
121 | |
---|
122 | /** |
---|
123 | * a portability wrapper around mkdir(). |
---|
124 | * On WIN32, the `permissions' argument is unused. |
---|
125 | * |
---|
126 | * @return zero on success, or -1 if an error occurred |
---|
127 | * (in which case errno is set appropriately). |
---|
128 | */ |
---|
129 | int tr_mkdir( const char * path, int permissions ); |
---|
130 | |
---|
131 | /** |
---|
132 | * Like mkdir, but makes parent directories as needed. |
---|
133 | * |
---|
134 | * @return zero on success, or -1 if an error occurred |
---|
135 | * (in which case errno is set appropriately). |
---|
136 | */ |
---|
137 | int tr_mkdirp( const char * path, int permissions ); |
---|
138 | |
---|
139 | |
---|
140 | uint8_t* tr_loadFile( const char * filename, size_t * size ) TR_GNUC_MALLOC; |
---|
141 | |
---|
142 | |
---|
143 | /* creates a filename from a series of elements using the |
---|
144 | correct separator for filenames. */ |
---|
145 | void tr_buildPath( char* buf, size_t buflen, |
---|
146 | const char * first_element, ... ) |
---|
147 | TR_GNUC_NULL_TERMINATED; |
---|
148 | |
---|
149 | struct timeval tr_timevalMsec( uint64_t milliseconds ); |
---|
150 | |
---|
151 | |
---|
152 | int tr_ioErrorFromErrno( int err ); |
---|
153 | |
---|
154 | const char * tr_errorString( int code ); |
---|
155 | |
---|
156 | /* return the current date in milliseconds */ |
---|
157 | uint64_t tr_date( void ); |
---|
158 | |
---|
159 | /* wait the specified number of milliseconds */ |
---|
160 | void tr_wait( uint64_t delay_milliseconds ); |
---|
161 | |
---|
162 | /*** |
---|
163 | **** |
---|
164 | ***/ |
---|
165 | |
---|
166 | /* Sometimes the system defines MAX/MIN, sometimes not. In the latter |
---|
167 | case, define those here since we will use them */ |
---|
168 | #ifndef MAX |
---|
169 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
---|
170 | #endif |
---|
171 | #ifndef MIN |
---|
172 | #define MIN(a,b) ((a)>(b)?(b):(a)) |
---|
173 | #endif |
---|
174 | |
---|
175 | /*** |
---|
176 | **** |
---|
177 | ***/ |
---|
178 | |
---|
179 | #define tr_new(struct_type, n_structs) \ |
---|
180 | ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs)))) |
---|
181 | #define tr_new0(struct_type, n_structs) \ |
---|
182 | ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs)))) |
---|
183 | #define tr_renew(struct_type, mem, n_structs) \ |
---|
184 | ((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs)))) |
---|
185 | |
---|
186 | void* tr_malloc ( size_t ) TR_GNUC_MALLOC; |
---|
187 | void* tr_malloc0 ( size_t ) TR_GNUC_MALLOC; |
---|
188 | void* tr_calloc ( size_t nmemb, size_t size ) TR_GNUC_MALLOC; |
---|
189 | void tr_free ( void* ); |
---|
190 | |
---|
191 | char* tr_strdup( const char * str ) TR_GNUC_MALLOC; |
---|
192 | char* tr_strndup( const char * str, int len ) TR_GNUC_MALLOC; |
---|
193 | char* tr_strdup_printf( const char * fmt, ... ) TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC; |
---|
194 | char* tr_base64_encode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC; |
---|
195 | char* tr_base64_decode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC; |
---|
196 | |
---|
197 | size_t tr_strlcpy( char * dst, const char * src, size_t siz ); |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | const char* tr_strerror( int ); |
---|
202 | |
---|
203 | /*** |
---|
204 | **** |
---|
205 | ***/ |
---|
206 | |
---|
207 | typedef void (tr_set_func)(void * element, void * userData ); |
---|
208 | |
---|
209 | void tr_set_compare( const void * a, size_t aCount, |
---|
210 | const void * b, size_t bCount, |
---|
211 | int compare( const void * a, const void * b ), |
---|
212 | size_t elementSize, |
---|
213 | tr_set_func in_a_cb, |
---|
214 | tr_set_func in_b_cb, |
---|
215 | tr_set_func in_both_cb, |
---|
216 | void * userData ); |
---|
217 | |
---|
218 | int tr_compareUint16( uint16_t a, uint16_t b ); |
---|
219 | int tr_compareUint32( uint32_t a, uint32_t b ); |
---|
220 | |
---|
221 | void tr_sha1_to_hex( char * out, const uint8_t * sha1 ); |
---|
222 | |
---|
223 | |
---|
224 | int tr_httpIsValidURL( const char * url ); |
---|
225 | |
---|
226 | int tr_httpParseURL( const char * url, |
---|
227 | int url_len, |
---|
228 | char ** setme_host, |
---|
229 | int * setme_port, |
---|
230 | char ** setme_path ); |
---|
231 | |
---|
232 | |
---|
233 | /*** |
---|
234 | **** |
---|
235 | ***/ |
---|
236 | |
---|
237 | struct tr_bitfield |
---|
238 | { |
---|
239 | uint8_t * bits; |
---|
240 | size_t len; |
---|
241 | }; |
---|
242 | |
---|
243 | typedef struct tr_bitfield tr_bitfield; |
---|
244 | typedef struct tr_bitfield tr_bitfield_t; |
---|
245 | |
---|
246 | tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC; |
---|
247 | tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC; |
---|
248 | void tr_bitfieldFree( tr_bitfield*); |
---|
249 | |
---|
250 | void tr_bitfieldClear( tr_bitfield* ); |
---|
251 | int tr_bitfieldAdd( tr_bitfield*, size_t bit ); |
---|
252 | int tr_bitfieldRem( tr_bitfield*, size_t bit ); |
---|
253 | int tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end ); |
---|
254 | int tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end ); |
---|
255 | void tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * ); |
---|
256 | |
---|
257 | int tr_bitfieldHas( const tr_bitfield*, size_t bit ); |
---|
258 | int tr_bitfieldIsEmpty( const tr_bitfield* ); |
---|
259 | size_t tr_bitfieldCountTrueBits( const tr_bitfield* ); |
---|
260 | |
---|
261 | tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* ); |
---|
262 | |
---|
263 | double tr_getRatio( double numerator, double denominator ); |
---|
264 | |
---|
265 | #endif |
---|