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