2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2005-2008 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef TR_UTILS_H
|
|
|
|
#define TR_UTILS_H 1
|
|
|
|
|
2007-08-04 00:43:47 +00:00
|
|
|
#include <inttypes.h>
|
2007-07-14 05:28:35 +00:00
|
|
|
#include <stdarg.h>
|
2007-08-14 20:45:23 +00:00
|
|
|
#include <stddef.h> /* for size_t */
|
2007-09-20 23:33:46 +00:00
|
|
|
#include <stdio.h> /* FILE* */
|
2008-06-16 22:11:50 +00:00
|
|
|
#include <time.h> /* time_t* */
|
2007-07-14 05:28:35 +00:00
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
#ifndef UNUSED
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define UNUSED __attribute__((unused))
|
|
|
|
#else
|
|
|
|
#define UNUSED
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TR_GNUC_PRINTF
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define TR_GNUC_PRINTF( fmt, args ) __attribute__((format (printf, fmt, args)))
|
|
|
|
#else
|
|
|
|
#define TR_GNUC_PRINTF( fmt, args )
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TR_GNUC_NULL_TERMINATED
|
|
|
|
#if __GNUC__ >= 4
|
|
|
|
#define TR_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
|
|
|
|
#else
|
|
|
|
#define TR_GNUC_NULL_TERMINATED
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
|
|
|
|
#define TR_GNUC_PURE __attribute__((__pure__))
|
|
|
|
#define TR_GNUC_MALLOC __attribute__((__malloc__))
|
|
|
|
#else
|
|
|
|
#define TR_GNUC_PURE
|
|
|
|
#define TR_GNUC_MALLOC
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-08-22 02:12:58 +00:00
|
|
|
|
2008-03-05 01:05:42 +00:00
|
|
|
#if !defined(_)
|
2008-03-04 02:02:25 +00:00
|
|
|
#if defined(SYS_DARWIN)
|
2008-03-18 19:18:22 +00:00
|
|
|
#define _(a) (a)
|
2008-07-25 11:50:17 +00:00
|
|
|
#elif defined(HAVE_LIBINTL_H)
|
2008-03-04 02:02:25 +00:00
|
|
|
#include <libintl.h>
|
|
|
|
#define _(a) gettext (a)
|
2008-04-05 20:49:51 +00:00
|
|
|
#else
|
|
|
|
#define _(a) (a)
|
2008-03-04 02:02:25 +00:00
|
|
|
#endif
|
2008-03-05 01:05:42 +00:00
|
|
|
#endif
|
2008-03-04 02:02:25 +00:00
|
|
|
|
2008-03-13 03:53:18 +00:00
|
|
|
#define tr_nerr( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, ## a )
|
|
|
|
#define tr_ninf( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, ## a )
|
|
|
|
#define tr_ndbg( n, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, ## a )
|
|
|
|
|
|
|
|
#define tr_torerr( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, ## a )
|
|
|
|
#define tr_torinf( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, ## a )
|
|
|
|
#define tr_tordbg( tor, a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, ## a )
|
|
|
|
|
|
|
|
#define tr_err( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, ## a )
|
|
|
|
#define tr_inf( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, ## a )
|
|
|
|
#define tr_dbg( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, ## a )
|
2008-05-15 15:44:51 +00:00
|
|
|
|
|
|
|
void tr_msgInit( void );
|
|
|
|
|
|
|
|
void tr_msg( const char * file, int line,
|
|
|
|
int level, const char * torrent,
|
|
|
|
const char * fmt, ... ) TR_GNUC_PRINTF( 5, 6 );
|
|
|
|
|
2007-09-20 23:33:46 +00:00
|
|
|
FILE* tr_getLog( void );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
void tr_deepLog( const char * file, int line,
|
|
|
|
const char * name,
|
|
|
|
const char * fmt, ... ) TR_GNUC_PRINTF( 4, 5 );
|
2008-04-25 02:57:33 +00:00
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
char* tr_getLogTimeStr( char * buf, int buflen );
|
|
|
|
|
2008-01-18 01:40:41 +00:00
|
|
|
/**
|
|
|
|
* a portability wrapper around mkdir().
|
|
|
|
* On WIN32, the `permissions' argument is unused.
|
|
|
|
*
|
|
|
|
* @return zero on success, or -1 if an error occurred
|
|
|
|
* (in which case errno is set appropriately).
|
|
|
|
*/
|
|
|
|
int tr_mkdir( const char * path, int permissions );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like mkdir, but makes parent directories as needed.
|
|
|
|
*
|
|
|
|
* @return zero on success, or -1 if an error occurred
|
|
|
|
* (in which case errno is set appropriately).
|
|
|
|
*/
|
2007-10-30 18:35:06 +00:00
|
|
|
int tr_mkdirp( const char * path, int permissions );
|
2007-08-01 00:40:49 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
uint8_t* tr_loadFile( const char * filename, size_t * size ) TR_GNUC_MALLOC;
|
2007-10-15 20:15:34 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
/* creates a filename from a series of elements using the
|
|
|
|
correct separator for filenames. */
|
2007-10-18 18:32:58 +00:00
|
|
|
void tr_buildPath( char* buf, size_t buflen,
|
2008-05-15 15:44:51 +00:00
|
|
|
const char * first_element, ... )
|
|
|
|
TR_GNUC_NULL_TERMINATED;
|
2007-06-18 19:39:52 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
struct timeval tr_timevalMsec( uint64_t milliseconds );
|
2007-08-23 20:33:01 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2008-01-18 19:13:32 +00:00
|
|
|
int tr_ioErrorFromErrno( int err );
|
2007-04-20 02:05:07 +00:00
|
|
|
|
2008-01-18 01:40:41 +00:00
|
|
|
const char * tr_errorString( int code );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
/* return the current date in milliseconds */
|
|
|
|
uint64_t tr_date( void );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
/* wait the specified number of milliseconds */
|
|
|
|
void tr_wait( uint64_t delay_milliseconds );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-19 11:54:37 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-19 11:54:37 +00:00
|
|
|
/* Sometimes the system defines MAX/MIN, sometimes not. In the latter
|
|
|
|
case, define those here since we will use them */
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
|
|
|
#endif
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a,b) ((a)>(b)?(b):(a))
|
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
#define tr_new(struct_type, n_structs) \
|
|
|
|
((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
|
|
|
|
#define tr_new0(struct_type, n_structs) \
|
|
|
|
((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
|
2007-06-29 05:45:17 +00:00
|
|
|
#define tr_renew(struct_type, mem, n_structs) \
|
|
|
|
((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
void* tr_malloc ( size_t ) TR_GNUC_MALLOC;
|
|
|
|
void* tr_malloc0 ( size_t ) TR_GNUC_MALLOC;
|
|
|
|
void* tr_calloc ( size_t nmemb, size_t size ) TR_GNUC_MALLOC;
|
2007-06-27 05:14:38 +00:00
|
|
|
void tr_free ( void* );
|
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
char* tr_strdup( const void * str ) TR_GNUC_MALLOC;
|
|
|
|
char* tr_strndup( const void * str, int len ) TR_GNUC_MALLOC;
|
2008-06-06 23:53:30 +00:00
|
|
|
void* tr_memdup( const void * src, int byteCount ) TR_GNUC_MALLOC;
|
2008-05-15 15:44:51 +00:00
|
|
|
char* tr_strdup_printf( const char * fmt, ... ) TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC;
|
2008-05-20 17:33:54 +00:00
|
|
|
char* tr_base64_encode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC;
|
|
|
|
char* tr_base64_decode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC;
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
size_t tr_strlcpy( char * dst, const char * src, size_t siz );
|
2008-07-15 17:16:57 +00:00
|
|
|
int tr_snprintf( char * buf, size_t buflen, const char * fmt, ... );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2008-06-11 16:15:45 +00:00
|
|
|
int tr_stringEndsWith( const char * string, const char * end );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-02-19 04:16:04 +00:00
|
|
|
const char* tr_strerror( int );
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
typedef void (tr_set_func)(void * element, void * userData );
|
|
|
|
|
|
|
|
void tr_set_compare( const void * a, size_t aCount,
|
|
|
|
const void * b, size_t bCount,
|
|
|
|
int compare( const void * a, const void * b ),
|
|
|
|
size_t elementSize,
|
|
|
|
tr_set_func in_a_cb,
|
|
|
|
tr_set_func in_b_cb,
|
|
|
|
tr_set_func in_both_cb,
|
|
|
|
void * userData );
|
|
|
|
|
|
|
|
int tr_compareUint16( uint16_t a, uint16_t b );
|
|
|
|
int tr_compareUint32( uint32_t a, uint32_t b );
|
2008-05-30 17:22:26 +00:00
|
|
|
int tr_compareUint64( uint64_t a, uint64_t b );
|
2008-06-16 22:11:50 +00:00
|
|
|
int tr_compareDouble( double a, double b );
|
|
|
|
int tr_compareTime( time_t a, time_t b );
|
|
|
|
|
|
|
|
int tr_strcmp( const void * a, const void * b );
|
|
|
|
int tr_strcasecmp( const char * a, const char * b );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-02-25 20:21:22 +00:00
|
|
|
void tr_sha1_to_hex( char * out, const uint8_t * sha1 );
|
|
|
|
|
2008-03-24 15:58:06 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
int tr_httpIsValidURL( const char * url );
|
|
|
|
|
2008-03-24 19:29:25 +00:00
|
|
|
int tr_httpParseURL( const char * url,
|
2008-03-24 15:58:06 +00:00
|
|
|
int url_len,
|
|
|
|
char ** setme_host,
|
|
|
|
int * setme_port,
|
|
|
|
char ** setme_path );
|
|
|
|
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct tr_bitfield
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
uint8_t * bits;
|
2008-06-07 01:44:54 +00:00
|
|
|
size_t bitCount;
|
|
|
|
size_t byteCount;
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct tr_bitfield tr_bitfield;
|
|
|
|
typedef struct tr_bitfield tr_bitfield_t;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC;
|
|
|
|
tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_bitfieldFree( tr_bitfield*);
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_bitfieldClear( tr_bitfield* );
|
2008-03-02 23:44:34 +00:00
|
|
|
int tr_bitfieldAdd( tr_bitfield*, size_t bit );
|
|
|
|
int tr_bitfieldRem( tr_bitfield*, size_t bit );
|
|
|
|
int tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );
|
|
|
|
int tr_bitfieldRemRange ( tr_bitfield*, size_t begin, size_t end );
|
2008-05-06 19:06:46 +00:00
|
|
|
void tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-25 17:43:39 +00:00
|
|
|
int tr_bitfieldHas( const tr_bitfield*, size_t bit );
|
2007-09-20 16:32:01 +00:00
|
|
|
int tr_bitfieldIsEmpty( const tr_bitfield* );
|
|
|
|
size_t tr_bitfieldCountTrueBits( const tr_bitfield* );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-11-11 16:33:04 +00:00
|
|
|
tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-06-09 22:53:45 +00:00
|
|
|
#if 0
|
2008-06-07 14:41:31 +00:00
|
|
|
/** @brief finds the first true bit in the bitfield, starting at `startPos'
|
|
|
|
@param setmePos the position of the true bit, if found, is set here.
|
|
|
|
@return nonzero if a true bit was found */
|
|
|
|
int tr_bitfieldFindTrue( const tr_bitfield * bitfield,
|
|
|
|
size_t startPos,
|
|
|
|
size_t * setmePos );
|
2008-06-09 22:53:45 +00:00
|
|
|
#endif
|
2008-06-07 14:41:31 +00:00
|
|
|
|
|
|
|
|
2008-06-05 20:21:56 +00:00
|
|
|
/** A stripped-down version of bitfieldHas to be used
|
|
|
|
for speed when you're looping quickly. This version
|
|
|
|
has none of tr_bitfieldHas()'s safety checks, so you
|
|
|
|
need to call tr_bitfieldTestFast() first before you
|
|
|
|
start looping. */
|
|
|
|
#define tr_bitfieldHasFast(bitfield,nth) \
|
|
|
|
(( bitfield->bits[(nth)>>3u] << ((nth)&7u) & 0x80) != 0 )
|
|
|
|
|
|
|
|
/** @param high the highest nth bit you're going to access */
|
|
|
|
#define tr_bitfieldTestFast(bitfield,high) \
|
2008-06-07 01:44:54 +00:00
|
|
|
( (bitfield) && ((bitfield)->bits) && ((high)<(bitfield)->bitCount ) )
|
2008-06-05 20:21:56 +00:00
|
|
|
|
2008-01-04 19:13:30 +00:00
|
|
|
double tr_getRatio( double numerator, double denominator );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#endif
|