1 | /****************************************************************************** |
---|
2 | * $Id: utils.h 7533 2008-12-29 21:17:48Z 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 | #include <time.h> /* time_t* */ |
---|
33 | |
---|
34 | #ifdef __cplusplus |
---|
35 | extern "C" { |
---|
36 | #endif |
---|
37 | |
---|
38 | /*** |
---|
39 | **** |
---|
40 | ***/ |
---|
41 | |
---|
42 | #ifndef FALSE |
---|
43 | #define FALSE 0 |
---|
44 | #endif |
---|
45 | |
---|
46 | #ifndef TRUE |
---|
47 | #define TRUE 1 |
---|
48 | #endif |
---|
49 | |
---|
50 | #ifndef UNUSED |
---|
51 | #ifdef __GNUC__ |
---|
52 | #define UNUSED __attribute__ ( ( unused ) ) |
---|
53 | #else |
---|
54 | #define UNUSED |
---|
55 | #endif |
---|
56 | #endif |
---|
57 | |
---|
58 | #ifndef TR_GNUC_PRINTF |
---|
59 | #ifdef __GNUC__ |
---|
60 | #define TR_GNUC_PRINTF( fmt,\ |
---|
61 | args ) __attribute__ ( ( format ( printf, fmt,\ |
---|
62 | args ) ) ) |
---|
63 | #else |
---|
64 | #define TR_GNUC_PRINTF( fmt, args ) |
---|
65 | #endif |
---|
66 | #endif |
---|
67 | |
---|
68 | #ifndef TR_GNUC_NULL_TERMINATED |
---|
69 | #if __GNUC__ >= 4 |
---|
70 | #define TR_GNUC_NULL_TERMINATED __attribute__ ( ( __sentinel__ ) ) |
---|
71 | #else |
---|
72 | #define TR_GNUC_NULL_TERMINATED |
---|
73 | #endif |
---|
74 | #endif |
---|
75 | |
---|
76 | #if __GNUC__ > 2 || ( __GNUC__ == 2 && __GNUC_MINOR__ >= 96 ) |
---|
77 | #define TR_GNUC_PURE __attribute__ ( ( __pure__ ) ) |
---|
78 | #define TR_GNUC_MALLOC __attribute__ ( ( __malloc__ ) ) |
---|
79 | #else |
---|
80 | #define TR_GNUC_PURE |
---|
81 | #define TR_GNUC_MALLOC |
---|
82 | #endif |
---|
83 | |
---|
84 | |
---|
85 | /*** |
---|
86 | **** |
---|
87 | ***/ |
---|
88 | |
---|
89 | #if !defined( _ ) |
---|
90 | #if defined( SYS_DARWIN ) |
---|
91 | #define _( a ) ( a ) |
---|
92 | #elif defined( HAVE_LIBINTL_H ) |
---|
93 | #include <libintl.h> |
---|
94 | #define _( a ) gettext ( a ) |
---|
95 | #else |
---|
96 | #define _( a ) ( a ) |
---|
97 | #endif |
---|
98 | #endif |
---|
99 | |
---|
100 | /* #define DISABLE_GETTEXT */ |
---|
101 | #if defined(TR_EMBEDDED) && !defined(DISABLE_GETTEXT) |
---|
102 | #define DISABLE_GETTEXT |
---|
103 | #endif |
---|
104 | #ifdef DISABLE_GETTEXT |
---|
105 | const char * tr_strip_positional_args( const char * fmt ); |
---|
106 | #undef _ |
---|
107 | #define _( a ) tr_strip_positional_args( a ) |
---|
108 | #endif |
---|
109 | |
---|
110 | /**** |
---|
111 | ***** |
---|
112 | ****/ |
---|
113 | |
---|
114 | int tr_msgLoggingIsActive( int level ); |
---|
115 | |
---|
116 | void tr_msg( const char * file, |
---|
117 | int line, |
---|
118 | int level, |
---|
119 | const char * torrent, |
---|
120 | const char * fmt, |
---|
121 | ... ) TR_GNUC_PRINTF( 5, 6 ); |
---|
122 | |
---|
123 | #define tr_nerr( n, ... ) \ |
---|
124 | do { \ |
---|
125 | if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \ |
---|
126 | tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, __VA_ARGS__ ); \ |
---|
127 | } while( 0 ) |
---|
128 | |
---|
129 | #define tr_ninf( n, ... ) \ |
---|
130 | do { \ |
---|
131 | if( tr_msgLoggingIsActive( TR_MSG_INF) ) \ |
---|
132 | tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, __VA_ARGS__ ); \ |
---|
133 | } while( 0 ) |
---|
134 | |
---|
135 | #define tr_ndbg( n, ... ) \ |
---|
136 | do { \ |
---|
137 | if( tr_msgLoggingIsActive( TR_MSG_DBG) ) \ |
---|
138 | tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, __VA_ARGS__ ); \ |
---|
139 | } while( 0 ) |
---|
140 | |
---|
141 | #define tr_torerr( tor, ... ) \ |
---|
142 | do { \ |
---|
143 | if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \ |
---|
144 | tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, __VA_ARGS__ ); \ |
---|
145 | } while( 0 ) |
---|
146 | |
---|
147 | #define tr_torinf( tor, ... ) \ |
---|
148 | do { \ |
---|
149 | if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \ |
---|
150 | tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, __VA_ARGS__ ); \ |
---|
151 | } while( 0 ) |
---|
152 | |
---|
153 | #define tr_tordbg( tor, ... ) \ |
---|
154 | do { \ |
---|
155 | if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \ |
---|
156 | tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, __VA_ARGS__ ); \ |
---|
157 | } while( 0 ) |
---|
158 | |
---|
159 | #define tr_err( ... ) \ |
---|
160 | do { \ |
---|
161 | if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \ |
---|
162 | tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, __VA_ARGS__ ); \ |
---|
163 | } while( 0 ) |
---|
164 | |
---|
165 | #define tr_inf( ... ) \ |
---|
166 | do { \ |
---|
167 | if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \ |
---|
168 | tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, __VA_ARGS__ ); \ |
---|
169 | } while( 0 ) |
---|
170 | |
---|
171 | #define tr_dbg( ... ) \ |
---|
172 | do { \ |
---|
173 | if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \ |
---|
174 | tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, __VA_ARGS__ ); \ |
---|
175 | } while( 0 ) |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | FILE* tr_getLog( void ); |
---|
180 | |
---|
181 | int tr_deepLoggingIsActive( void ); |
---|
182 | |
---|
183 | void tr_deepLog( const char * file, |
---|
184 | int line, |
---|
185 | const char * name, |
---|
186 | const char * fmt, |
---|
187 | ... ) TR_GNUC_PRINTF( 4, 5 ); |
---|
188 | |
---|
189 | char* tr_getLogTimeStr( char * buf, |
---|
190 | int buflen ); |
---|
191 | |
---|
192 | |
---|
193 | int tr_wildmat( const char * text, |
---|
194 | const char * pattern ); |
---|
195 | |
---|
196 | /** a portability wrapper for basename(). */ |
---|
197 | char* tr_basename( const char * path ) TR_GNUC_MALLOC; |
---|
198 | |
---|
199 | /** a portability wrapper for dirname(). */ |
---|
200 | char* tr_dirname( const char * path ) TR_GNUC_MALLOC; |
---|
201 | |
---|
202 | /** |
---|
203 | * a portability wrapper around mkdir(). |
---|
204 | * On WIN32, the `permissions' argument is unused. |
---|
205 | * |
---|
206 | * @return zero on success, or -1 if an error occurred |
---|
207 | * (in which case errno is set appropriately). |
---|
208 | */ |
---|
209 | int tr_mkdir( const char * path, |
---|
210 | int permissions ); |
---|
211 | |
---|
212 | /** |
---|
213 | * Like mkdir, but makes parent directories as needed. |
---|
214 | * |
---|
215 | * @return zero on success, or -1 if an error occurred |
---|
216 | * (in which case errno is set appropriately). |
---|
217 | */ |
---|
218 | int tr_mkdirp( const char * path, |
---|
219 | int permissions ); |
---|
220 | |
---|
221 | |
---|
222 | /** |
---|
223 | * Loads a file and returns its contents. |
---|
224 | * On failure, NULL is returned and errno is set. |
---|
225 | */ |
---|
226 | uint8_t* tr_loadFile( const char * filename, |
---|
227 | size_t * size ) TR_GNUC_MALLOC; |
---|
228 | |
---|
229 | |
---|
230 | /* creates a filename from a series of elements using the |
---|
231 | correct separator for filenames. */ |
---|
232 | char* tr_buildPath( const char * first_element, ... ) |
---|
233 | TR_GNUC_NULL_TERMINATED |
---|
234 | TR_GNUC_MALLOC; |
---|
235 | |
---|
236 | struct timeval; |
---|
237 | |
---|
238 | void tr_timevalMsec( uint64_t milliseconds, |
---|
239 | struct timeval * setme ); |
---|
240 | |
---|
241 | |
---|
242 | /* return the current date in milliseconds */ |
---|
243 | uint64_t tr_date( void ); |
---|
244 | |
---|
245 | /* wait the specified number of milliseconds */ |
---|
246 | void tr_wait( uint64_t delay_milliseconds ); |
---|
247 | |
---|
248 | /*** |
---|
249 | **** |
---|
250 | ***/ |
---|
251 | |
---|
252 | /* Sometimes the system defines MAX/MIN, sometimes not. In the latter |
---|
253 | case, define those here since we will use them */ |
---|
254 | #ifndef MAX |
---|
255 | #define MAX( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) ) |
---|
256 | #endif |
---|
257 | #ifndef MIN |
---|
258 | #define MIN( a, b ) ( ( a ) > ( b ) ? ( b ) : ( a ) ) |
---|
259 | #endif |
---|
260 | |
---|
261 | /*** |
---|
262 | **** |
---|
263 | ***/ |
---|
264 | |
---|
265 | #define tr_new( struct_type, n_structs ) \ |
---|
266 | ( (struct_type *) tr_malloc ( ( (size_t) sizeof ( struct_type ) ) * ( (\ |
---|
267 | size_t) (\ |
---|
268 | n_structs ) ) ) ) |
---|
269 | #define tr_new0( struct_type, n_structs ) \ |
---|
270 | ( (struct_type *) tr_malloc0 ( ( (size_t) sizeof ( struct_type ) ) * ( (\ |
---|
271 | size_t) (\ |
---|
272 | n_structs ) ) ) ) |
---|
273 | #define tr_renew( struct_type, mem, n_structs ) \ |
---|
274 | ( (struct_type *) realloc ( ( mem ),\ |
---|
275 | ( (size_t) sizeof ( struct_type ) ) * ( (\ |
---|
276 | size_t) (\ |
---|
277 | n_structs ) ) ) ) |
---|
278 | |
---|
279 | void* tr_malloc( size_t ) TR_GNUC_MALLOC; |
---|
280 | |
---|
281 | void* tr_malloc0( size_t ) TR_GNUC_MALLOC; |
---|
282 | |
---|
283 | void tr_free( void* ); |
---|
284 | |
---|
285 | char* tr_strdup( const void * str ) TR_GNUC_MALLOC; |
---|
286 | |
---|
287 | char* tr_strndup( const void * str, |
---|
288 | int len ) TR_GNUC_MALLOC; |
---|
289 | |
---|
290 | void* tr_memdup( const void * src, |
---|
291 | int byteCount ) TR_GNUC_MALLOC; |
---|
292 | |
---|
293 | char* tr_strdup_printf( const char * fmt, |
---|
294 | ... ) TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC; |
---|
295 | |
---|
296 | char* tr_base64_encode( const void * input, |
---|
297 | int inlen, |
---|
298 | int * outlen ) TR_GNUC_MALLOC; |
---|
299 | |
---|
300 | char* tr_base64_decode( const void * input, |
---|
301 | int inlen, |
---|
302 | int * outlen ) TR_GNUC_MALLOC; |
---|
303 | |
---|
304 | size_t tr_strlcpy( char * dst, |
---|
305 | const void * src, |
---|
306 | size_t siz ); |
---|
307 | |
---|
308 | int tr_snprintf( char * buf, |
---|
309 | size_t buflen, |
---|
310 | const char * fmt, |
---|
311 | ... ) TR_GNUC_PRINTF( 3, 4 ); |
---|
312 | |
---|
313 | const char* tr_strerror( int ); |
---|
314 | |
---|
315 | char* tr_strstrip( char * str ); |
---|
316 | |
---|
317 | /*** |
---|
318 | **** |
---|
319 | ***/ |
---|
320 | |
---|
321 | typedef void ( tr_set_func )( void * element, void * userData ); |
---|
322 | |
---|
323 | void tr_set_compare( const void * a, |
---|
324 | size_t aCount, |
---|
325 | const void * b, |
---|
326 | size_t bCount, |
---|
327 | int compare( const void * a, const void * b ), |
---|
328 | size_t elementSize, |
---|
329 | tr_set_func in_a_cb, |
---|
330 | tr_set_func in_b_cb, |
---|
331 | tr_set_func in_both_cb, |
---|
332 | void * userData ); |
---|
333 | |
---|
334 | void tr_sha1_to_hex( char * out, |
---|
335 | const uint8_t * sha1 ); |
---|
336 | |
---|
337 | |
---|
338 | int tr_httpIsValidURL( const char * url ); |
---|
339 | |
---|
340 | int tr_httpParseURL( const char * url, |
---|
341 | int url_len, |
---|
342 | char ** setme_host, |
---|
343 | int * setme_port, |
---|
344 | char ** setme_path ); |
---|
345 | |
---|
346 | |
---|
347 | /*** |
---|
348 | **** |
---|
349 | ***/ |
---|
350 | |
---|
351 | typedef struct tr_bitfield |
---|
352 | { |
---|
353 | uint8_t * bits; |
---|
354 | size_t bitCount; |
---|
355 | size_t byteCount; |
---|
356 | } |
---|
357 | tr_bitfield; |
---|
358 | |
---|
359 | tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount ); |
---|
360 | |
---|
361 | void tr_bitfieldDestruct( tr_bitfield* ); |
---|
362 | |
---|
363 | tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC; |
---|
364 | |
---|
365 | tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC; |
---|
366 | |
---|
367 | void tr_bitfieldFree( tr_bitfield* ); |
---|
368 | |
---|
369 | void tr_bitfieldClear( tr_bitfield* ); |
---|
370 | |
---|
371 | int tr_bitfieldAdd( tr_bitfield*, size_t bit ); |
---|
372 | |
---|
373 | int tr_bitfieldRem( tr_bitfield*, size_t bit ); |
---|
374 | |
---|
375 | int tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end ); |
---|
376 | |
---|
377 | int tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end ); |
---|
378 | |
---|
379 | void tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * ); |
---|
380 | |
---|
381 | int tr_bitfieldIsEmpty( const tr_bitfield* ); |
---|
382 | |
---|
383 | size_t tr_bitfieldCountTrueBits( const tr_bitfield* ); |
---|
384 | |
---|
385 | tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* ); |
---|
386 | |
---|
387 | /** A stripped-down version of bitfieldHas to be used |
---|
388 | for speed when you're looping quickly. This version |
---|
389 | has none of tr_bitfieldHas()'s safety checks, so you |
---|
390 | need to call tr_bitfieldTestFast() first before you |
---|
391 | start looping. */ |
---|
392 | #define tr_bitfieldHasFast( bitfield, nth ) \ |
---|
393 | ( ( (bitfield)->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 ) |
---|
394 | |
---|
395 | /** @param high the highest nth bit you're going to access */ |
---|
396 | #define tr_bitfieldTestFast( bitfield, high ) \ |
---|
397 | ( ( bitfield ) && ( ( bitfield )->bits )\ |
---|
398 | && ( ( high ) < ( bitfield )->bitCount ) ) |
---|
399 | |
---|
400 | #define tr_bitfieldHas( bitfield, nth ) \ |
---|
401 | ( tr_bitfieldTestFast( bitfield, nth ) \ |
---|
402 | && tr_bitfieldHasFast( bitfield, nth ) ) |
---|
403 | |
---|
404 | double tr_getRatio( double numerator, double denominator ); |
---|
405 | |
---|
406 | |
---|
407 | int tr_ptr2int( void* ); |
---|
408 | |
---|
409 | void* tr_int2ptr( int ); |
---|
410 | |
---|
411 | #ifdef __cplusplus |
---|
412 | } |
---|
413 | #endif |
---|
414 | |
---|
415 | #endif |
---|