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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2008-06-16 22:11:50 +00:00
|
|
|
#include <ctype.h> /* isalpha, tolower */
|
2007-07-19 11:54:37 +00:00
|
|
|
#include <errno.h>
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <stdarg.h>
|
2007-07-19 11:54:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-02-19 04:16:04 +00:00
|
|
|
#include <string.h> /* strerror */
|
2007-07-19 11:54:37 +00:00
|
|
|
|
2008-04-25 02:57:33 +00:00
|
|
|
#include <libgen.h> /* basename */
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h> /* usleep, stat */
|
2007-07-18 17:19:14 +00:00
|
|
|
|
2007-11-06 21:14:30 +00:00
|
|
|
#include "event.h"
|
|
|
|
|
2007-08-02 19:43:29 +00:00
|
|
|
#ifdef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <windows.h> /* for Sleep */
|
|
|
|
#elif defined( __BEOS__ )
|
|
|
|
#include <kernel/OS.h>
|
2007-08-02 19:43:29 +00:00
|
|
|
#endif
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-19 11:54:37 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "platform.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static tr_lock * messageLock = NULL;
|
2007-09-20 16:32:01 +00:00
|
|
|
static int messageLevel = 0;
|
|
|
|
static int messageQueuing = FALSE;
|
|
|
|
static tr_msg_list * messageQueue = NULL;
|
|
|
|
static tr_msg_list ** messageQueueTail = &messageQueue;
|
2006-07-16 23:40:22 +00:00
|
|
|
|
2008-08-21 20:39:57 +00:00
|
|
|
void
|
|
|
|
tr_msgInit( void )
|
2006-08-20 18:15:25 +00:00
|
|
|
{
|
2007-07-30 15:27:52 +00:00
|
|
|
if( !messageLock )
|
2008-09-23 19:11:04 +00:00
|
|
|
messageLock = tr_lockNew( );
|
2006-08-20 18:15:25 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 23:33:46 +00:00
|
|
|
FILE*
|
|
|
|
tr_getLog( void )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
static int initialized = FALSE;
|
|
|
|
static FILE * file = NULL;
|
2007-09-20 23:33:46 +00:00
|
|
|
|
|
|
|
if( !initialized )
|
|
|
|
{
|
|
|
|
const char * str = getenv( "TR_DEBUG_FD" );
|
2008-09-23 19:11:04 +00:00
|
|
|
int fd = 0;
|
2007-09-20 23:33:46 +00:00
|
|
|
if( str && *str )
|
|
|
|
fd = atoi( str );
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( fd )
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
file = stdout; break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
file = stderr; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
file = NULL; break;
|
2007-09-20 23:33:46 +00:00
|
|
|
}
|
|
|
|
initialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_setMessageLevel( int level )
|
2006-08-20 18:15:25 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_msgInit( );
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_lockLock( messageLock );
|
|
|
|
messageLevel = MAX( 0, level );
|
|
|
|
tr_lockUnlock( messageLock );
|
2006-08-20 18:15:25 +00:00
|
|
|
}
|
|
|
|
|
2008-03-13 03:53:18 +00:00
|
|
|
int
|
|
|
|
tr_getMessageLevel( void )
|
2006-07-16 23:40:22 +00:00
|
|
|
{
|
2006-08-22 02:12:58 +00:00
|
|
|
int ret;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_msgInit( );
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_lockLock( messageLock );
|
|
|
|
ret = messageLevel;
|
|
|
|
tr_lockUnlock( messageLock );
|
|
|
|
|
|
|
|
return ret;
|
2006-07-16 23:40:22 +00:00
|
|
|
}
|
|
|
|
|
2008-03-13 03:53:18 +00:00
|
|
|
void
|
|
|
|
tr_setMessageQueuing( int enabled )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_msgInit( );
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_lockLock( messageLock );
|
|
|
|
messageQueuing = enabled;
|
|
|
|
tr_lockUnlock( messageLock );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-03-13 03:53:18 +00:00
|
|
|
tr_msg_list *
|
|
|
|
tr_getQueuedMessages( void )
|
2006-08-22 02:12:58 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_msg_list * ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-08-30 16:35:33 +00:00
|
|
|
assert( NULL != messageLock );
|
2006-08-22 02:12:58 +00:00
|
|
|
tr_lockLock( messageLock );
|
|
|
|
ret = messageQueue;
|
|
|
|
messageQueue = NULL;
|
|
|
|
messageQueueTail = &messageQueue;
|
|
|
|
tr_lockUnlock( messageLock );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-13 03:53:18 +00:00
|
|
|
void
|
|
|
|
tr_freeMessageList( tr_msg_list * list )
|
2006-08-22 02:12:58 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_msg_list * next;
|
2006-08-22 02:12:58 +00:00
|
|
|
|
|
|
|
while( NULL != list )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-08-22 02:12:58 +00:00
|
|
|
next = list->next;
|
|
|
|
free( list->message );
|
2008-03-13 03:53:18 +00:00
|
|
|
free( list->name );
|
2006-08-22 02:12:58 +00:00
|
|
|
free( list );
|
|
|
|
list = next;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2006-08-22 02:12:58 +00:00
|
|
|
}
|
|
|
|
|
2008-04-25 02:57:33 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
char*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_getLogTimeStr( char * buf,
|
|
|
|
int buflen )
|
2008-04-25 02:57:33 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char tmp[64];
|
|
|
|
time_t now;
|
|
|
|
struct tm now_tm;
|
2008-04-25 02:57:33 +00:00
|
|
|
struct timeval tv;
|
2008-09-23 19:11:04 +00:00
|
|
|
int milliseconds;
|
2008-04-25 02:57:33 +00:00
|
|
|
|
|
|
|
now = time( NULL );
|
|
|
|
gettimeofday( &tv, NULL );
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
now_tm = *localtime( &now );
|
|
|
|
#else
|
|
|
|
localtime_r( &now, &now_tm );
|
|
|
|
#endif
|
2008-09-23 19:11:04 +00:00
|
|
|
strftime( tmp, sizeof( tmp ), "%H:%M:%S", &now_tm );
|
|
|
|
milliseconds = (int)( tv.tv_usec / 1000 );
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( buf, buflen, "%s.%03d", tmp, milliseconds );
|
2008-04-25 02:57:33 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_deepLog( const char * file,
|
|
|
|
int line,
|
|
|
|
const char * name,
|
|
|
|
const char * fmt,
|
|
|
|
... )
|
2008-04-25 02:57:33 +00:00
|
|
|
{
|
|
|
|
FILE * fp = tr_getLog( );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( fp )
|
2008-04-25 02:57:33 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
va_list args;
|
|
|
|
char timestr[64];
|
2008-04-25 02:57:33 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2008-09-23 19:11:04 +00:00
|
|
|
char * myfile = tr_strdup( file );
|
2008-04-25 02:57:33 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
evbuffer_add_printf( buf, "[%s] ",
|
|
|
|
tr_getLogTimeStr( timestr, sizeof( timestr ) ) );
|
2008-04-25 02:57:33 +00:00
|
|
|
if( name )
|
|
|
|
evbuffer_add_printf( buf, "%s ", name );
|
|
|
|
va_start( args, fmt );
|
|
|
|
evbuffer_add_vprintf( buf, fmt, args );
|
|
|
|
va_end( args );
|
2008-09-23 19:11:04 +00:00
|
|
|
evbuffer_add_printf( buf, " (%s:%d)\n", basename( myfile ), line );
|
2008-08-21 16:12:17 +00:00
|
|
|
fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );
|
2008-04-25 02:57:33 +00:00
|
|
|
|
|
|
|
tr_free( myfile );
|
|
|
|
evbuffer_free( buf );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-12-08 19:34:15 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_msg( const char * file,
|
|
|
|
int line,
|
|
|
|
int level,
|
2008-03-13 03:53:18 +00:00
|
|
|
const char * name,
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * fmt,
|
|
|
|
... )
|
2006-08-22 02:12:58 +00:00
|
|
|
{
|
2007-11-06 21:14:30 +00:00
|
|
|
FILE * fp;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-02-28 16:38:48 +00:00
|
|
|
if( messageLock )
|
|
|
|
tr_lockLock( messageLock );
|
2006-07-16 23:40:22 +00:00
|
|
|
|
2007-09-20 23:33:46 +00:00
|
|
|
fp = tr_getLog( );
|
|
|
|
|
2006-08-22 02:12:58 +00:00
|
|
|
if( !messageLevel )
|
2006-07-16 23:40:22 +00:00
|
|
|
{
|
2007-11-06 23:10:04 +00:00
|
|
|
char * env = getenv( "TR_DEBUG" );
|
2006-08-22 02:12:58 +00:00
|
|
|
messageLevel = ( env ? atoi( env ) : 0 ) + 1;
|
|
|
|
messageLevel = MAX( 1, messageLevel );
|
2006-07-16 23:40:22 +00:00
|
|
|
}
|
2006-08-22 02:12:58 +00:00
|
|
|
|
|
|
|
if( messageLevel >= level )
|
2006-07-16 23:40:22 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
va_list ap;
|
2008-04-29 16:57:16 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2007-11-06 21:14:30 +00:00
|
|
|
|
|
|
|
/* build the text message */
|
|
|
|
va_start( ap, fmt );
|
2008-04-29 16:57:16 +00:00
|
|
|
evbuffer_add_vprintf( buf, fmt, ap );
|
2007-11-06 21:14:30 +00:00
|
|
|
va_end( ap );
|
|
|
|
|
2008-04-29 16:57:16 +00:00
|
|
|
if( EVBUFFER_LENGTH( buf ) )
|
2006-08-22 02:12:58 +00:00
|
|
|
{
|
2007-11-06 23:10:04 +00:00
|
|
|
if( messageQueuing )
|
|
|
|
{
|
|
|
|
tr_msg_list * newmsg;
|
|
|
|
newmsg = tr_new0( tr_msg_list, 1 );
|
|
|
|
newmsg->level = level;
|
|
|
|
newmsg->when = time( NULL );
|
2008-06-07 21:26:41 +00:00
|
|
|
newmsg->message = tr_strdup( EVBUFFER_DATA( buf ) );
|
2007-12-08 19:34:15 +00:00
|
|
|
newmsg->file = file;
|
|
|
|
newmsg->line = line;
|
2008-03-13 03:53:18 +00:00
|
|
|
newmsg->name = tr_strdup( name );
|
2007-11-06 23:10:04 +00:00
|
|
|
|
|
|
|
*messageQueueTail = newmsg;
|
|
|
|
messageQueueTail = &newmsg->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( fp == NULL )
|
|
|
|
fp = stderr;
|
2008-05-15 22:08:43 +00:00
|
|
|
if( name )
|
2008-09-23 19:11:04 +00:00
|
|
|
fprintf( fp, "%s: %s\n", name,
|
|
|
|
(char*)EVBUFFER_DATA( buf ) );
|
2008-05-15 22:08:43 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
fprintf( fp, "%s\n", (char*)EVBUFFER_DATA( buf ) );
|
2007-11-06 23:10:04 +00:00
|
|
|
fflush( fp );
|
|
|
|
}
|
2008-04-29 16:57:16 +00:00
|
|
|
|
|
|
|
evbuffer_free( buf );
|
2006-08-22 02:12:58 +00:00
|
|
|
}
|
2006-07-16 23:40:22 +00:00
|
|
|
}
|
2006-08-22 02:12:58 +00:00
|
|
|
|
2008-02-28 16:38:48 +00:00
|
|
|
if( messageLock )
|
|
|
|
tr_lockUnlock( messageLock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_set_compare( const void * va,
|
|
|
|
size_t aCount,
|
|
|
|
const void * vb,
|
|
|
|
size_t bCount,
|
2007-09-20 16:32:01 +00:00
|
|
|
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 )
|
|
|
|
{
|
|
|
|
const uint8_t * a = (const uint8_t *) va;
|
|
|
|
const uint8_t * b = (const uint8_t *) vb;
|
2008-09-23 19:11:04 +00:00
|
|
|
const uint8_t * aend = a + elementSize * aCount;
|
|
|
|
const uint8_t * bend = b + elementSize * bCount;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
while( a != aend || b != bend )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( a == aend )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
( *in_b_cb )( (void*)b, userData );
|
2007-09-20 16:32:01 +00:00
|
|
|
b += elementSize;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( b == bend )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
( *in_a_cb )( (void*)a, userData );
|
2007-09-20 16:32:01 +00:00
|
|
|
a += elementSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const int val = ( *compare )( a, b );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
if( !val )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
( *in_both_cb )( (void*)a, userData );
|
2007-09-20 16:32:01 +00:00
|
|
|
a += elementSize;
|
|
|
|
b += elementSize;
|
|
|
|
}
|
|
|
|
else if( val < 0 )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
( *in_a_cb )( (void*)a, userData );
|
2007-09-20 16:32:01 +00:00
|
|
|
a += elementSize;
|
|
|
|
}
|
|
|
|
else if( val > 0 )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
( *in_b_cb )( (void*)b, userData );
|
2007-09-20 16:32:01 +00:00
|
|
|
b += elementSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-08-21 21:04:57 +00:00
|
|
|
#ifdef DISABLE_GETTEXT
|
|
|
|
|
|
|
|
const char*
|
|
|
|
tr_strip_positional_args( const char* str )
|
|
|
|
{
|
|
|
|
static size_t bufsize = 0;
|
|
|
|
static char * buf = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
const size_t len = strlen( str );
|
|
|
|
char * out;
|
2008-08-21 21:04:57 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( bufsize < len )
|
|
|
|
{
|
2008-08-21 21:04:57 +00:00
|
|
|
bufsize = len * 2;
|
|
|
|
buf = tr_renew( char, buf, bufsize );
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( out = buf; *str; ++str )
|
|
|
|
{
|
2008-08-21 21:04:57 +00:00
|
|
|
*out++ = *str;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( *str == '%' ) && isdigit( str[1] ) )
|
|
|
|
{
|
2008-08-21 21:04:57 +00:00
|
|
|
const char * tmp = str + 1;
|
|
|
|
while( isdigit( *tmp ) )
|
|
|
|
++tmp;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-21 21:04:57 +00:00
|
|
|
if( *tmp == '$' )
|
|
|
|
str = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2008-06-16 22:11:50 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-08-23 20:33:01 +00:00
|
|
|
struct timeval
|
2008-04-24 01:42:53 +00:00
|
|
|
tr_timevalMsec( uint64_t milliseconds )
|
2007-08-23 20:33:01 +00:00
|
|
|
{
|
|
|
|
struct timeval ret;
|
2007-10-18 18:32:58 +00:00
|
|
|
const uint64_t microseconds = milliseconds * 1000;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-08-23 20:33:01 +00:00
|
|
|
ret.tv_sec = microseconds / 1000000;
|
|
|
|
ret.tv_usec = microseconds % 1000000;
|
|
|
|
return ret;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-15 20:15:34 +00:00
|
|
|
uint8_t *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_loadFile( const char * path,
|
|
|
|
size_t * size )
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint8_t * buf;
|
2007-10-15 20:15:34 +00:00
|
|
|
struct stat sb;
|
2008-09-23 19:11:04 +00:00
|
|
|
FILE * file;
|
2008-03-30 13:52:55 +00:00
|
|
|
const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
|
2007-10-15 20:15:34 +00:00
|
|
|
|
|
|
|
/* try to stat the file */
|
|
|
|
errno = 0;
|
|
|
|
if( stat( path, &sb ) )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_dbg( err_fmt, path, tr_strerror( errno ) );
|
2007-10-15 20:15:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( sb.st_mode & S_IFMT ) != S_IFREG )
|
|
|
|
{
|
2008-03-30 13:52:55 +00:00
|
|
|
tr_err( err_fmt, path, _( "Not a regular file" ) );
|
2007-10-15 20:15:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the torrent file into our buffer */
|
|
|
|
file = fopen( path, "rb" );
|
|
|
|
if( !file )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_err( err_fmt, path, tr_strerror( errno ) );
|
2007-10-15 20:15:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf = malloc( sb.st_size );
|
|
|
|
if( NULL == buf )
|
|
|
|
{
|
2008-03-30 13:52:55 +00:00
|
|
|
tr_err( err_fmt, path, _( "Memory allocation failed" ) );
|
2007-10-15 20:15:34 +00:00
|
|
|
fclose( file );
|
2008-03-04 02:02:25 +00:00
|
|
|
return NULL;
|
2007-10-15 20:15:34 +00:00
|
|
|
}
|
|
|
|
if( fread( buf, sb.st_size, 1, file ) != 1 )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_err( err_fmt, path, tr_strerror( errno ) );
|
2007-10-15 20:15:34 +00:00
|
|
|
fclose( file );
|
2008-09-06 03:02:30 +00:00
|
|
|
free( buf );
|
2007-10-15 20:15:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fclose( file );
|
|
|
|
|
|
|
|
*size = sb.st_size;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-08-01 00:40:49 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_mkdir( const char * path,
|
|
|
|
int permissions
|
2007-08-01 00:40:49 +00:00
|
|
|
#ifdef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
UNUSED
|
2007-08-01 00:40:49 +00:00
|
|
|
#endif
|
2008-09-23 19:11:04 +00:00
|
|
|
)
|
2007-08-01 00:40:49 +00:00
|
|
|
{
|
|
|
|
#ifdef WIN32
|
2008-09-23 19:11:04 +00:00
|
|
|
if( path && isalpha( path[0] ) && path[1] == ':' && !path[2] )
|
2008-02-28 16:38:48 +00:00
|
|
|
return 0;
|
2007-08-01 00:40:49 +00:00
|
|
|
return mkdir( path );
|
|
|
|
#else
|
|
|
|
return mkdir( path, permissions );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_mkdirp( const char * path_in,
|
|
|
|
int permissions )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * path = tr_strdup( path_in );
|
|
|
|
char * p, * pp;
|
2006-07-16 19:39:23 +00:00
|
|
|
struct stat sb;
|
2008-09-23 19:11:04 +00:00
|
|
|
int done;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/* walk past the root */
|
2006-07-16 19:39:23 +00:00
|
|
|
p = path;
|
2007-10-30 18:35:06 +00:00
|
|
|
while( *p == TR_PATH_DELIMITER )
|
|
|
|
++p;
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
pp = p;
|
|
|
|
done = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
while( ( p =
|
|
|
|
strchr( pp, TR_PATH_DELIMITER ) ) || ( p = strchr( pp, '\0' ) ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
if( !*p )
|
2006-07-16 19:39:23 +00:00
|
|
|
done = 1;
|
|
|
|
else
|
|
|
|
*p = '\0';
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( stat( path, &sb ) )
|
|
|
|
{
|
|
|
|
/* Folder doesn't exist yet */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_mkdir( path, permissions ) )
|
|
|
|
{
|
2008-01-18 01:40:41 +00:00
|
|
|
const int err = errno;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_err( _(
|
|
|
|
"Couldn't create \"%1$s\": %2$s" ), path,
|
|
|
|
tr_strerror( err ) );
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( path );
|
2008-01-18 01:40:41 +00:00
|
|
|
errno = err;
|
|
|
|
return -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ( sb.st_mode & S_IFMT ) != S_IFDIR )
|
|
|
|
{
|
|
|
|
/* Node exists but isn't a folder */
|
2008-03-07 20:48:36 +00:00
|
|
|
char buf[MAX_PATH_LENGTH];
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_snprintf( buf, sizeof( buf ), _(
|
|
|
|
"File \"%s\" is in the way" ), path );
|
2008-03-30 13:52:55 +00:00
|
|
|
tr_err( _( "Couldn't create \"%1$s\": %2$s" ), path_in, buf );
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( path );
|
2008-01-18 01:40:41 +00:00
|
|
|
errno = ENOTDIR;
|
|
|
|
return -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( done )
|
|
|
|
break;
|
2007-10-30 18:35:06 +00:00
|
|
|
|
|
|
|
*p = TR_PATH_DELIMITER;
|
2006-07-16 19:39:23 +00:00
|
|
|
p++;
|
|
|
|
pp = p;
|
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( path );
|
2006-07-16 19:39:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_buildPath( char * buf,
|
|
|
|
size_t buflen,
|
|
|
|
const char *first_element,
|
|
|
|
... )
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2008-08-11 04:40:29 +00:00
|
|
|
struct evbuffer * evbuf;
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * element = first_element;
|
|
|
|
va_list vl;
|
2008-08-11 04:40:29 +00:00
|
|
|
|
|
|
|
evbuf = evbuffer_new( );
|
2007-06-18 19:39:52 +00:00
|
|
|
va_start( vl, first_element );
|
2008-08-11 04:40:29 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
while( element )
|
|
|
|
{
|
|
|
|
if( EVBUFFER_LENGTH( evbuf ) )
|
2007-11-27 04:11:39 +00:00
|
|
|
evbuffer_add_printf( evbuf, "%c", TR_PATH_DELIMITER );
|
|
|
|
evbuffer_add_printf( evbuf, "%s", element );
|
2007-06-18 19:39:52 +00:00
|
|
|
element = (const char*) va_arg( vl, const char* );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( EVBUFFER_LENGTH( evbuf ) )
|
2008-08-21 16:12:17 +00:00
|
|
|
tr_strlcpy( buf, EVBUFFER_DATA( evbuf ), buflen );
|
2008-01-04 18:24:42 +00:00
|
|
|
else
|
|
|
|
*buf = '\0';
|
2008-08-11 04:40:29 +00:00
|
|
|
|
|
|
|
va_end( vl );
|
2007-11-27 04:11:39 +00:00
|
|
|
evbuffer_free( evbuf );
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-04-20 02:05:07 +00:00
|
|
|
int
|
2008-01-18 19:13:32 +00:00
|
|
|
tr_ioErrorFromErrno( int err )
|
2007-04-20 02:05:07 +00:00
|
|
|
{
|
2008-01-18 19:13:32 +00:00
|
|
|
switch( err )
|
2007-04-20 02:05:07 +00:00
|
|
|
{
|
2008-01-18 19:13:32 +00:00
|
|
|
case 0:
|
|
|
|
return TR_OK;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
case EACCES:
|
|
|
|
case EROFS:
|
|
|
|
return TR_ERROR_IO_PERMISSIONS;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
case ENOSPC:
|
|
|
|
return TR_ERROR_IO_SPACE;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
case EMFILE:
|
2007-07-01 01:50:14 +00:00
|
|
|
return TR_ERROR_IO_OPEN_FILES;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-08-13 15:40:45 +00:00
|
|
|
case EFBIG:
|
|
|
|
return TR_ERROR_IO_FILE_TOO_BIG;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
default:
|
2008-07-30 14:46:45 +00:00
|
|
|
tr_err( "generic i/o errno from errno: %s", tr_strerror( errno ) );
|
2007-06-18 03:40:41 +00:00
|
|
|
return TR_ERROR_IO_OTHER;
|
2007-04-20 02:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-18 01:40:41 +00:00
|
|
|
const char *
|
2007-01-14 12:00:21 +00:00
|
|
|
tr_errorString( int code )
|
|
|
|
{
|
|
|
|
switch( code )
|
|
|
|
{
|
|
|
|
case TR_OK:
|
2008-03-04 02:02:25 +00:00
|
|
|
return _( "No error" );
|
2008-01-18 19:13:32 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_ERROR:
|
2008-03-19 02:16:07 +00:00
|
|
|
return _( "Unspecified error" );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_ERROR_ASSERT:
|
2008-03-04 02:02:25 +00:00
|
|
|
return _( "Assert error" );
|
2008-01-18 19:13:32 +00:00
|
|
|
|
|
|
|
case TR_ERROR_IO_PARENT:
|
2008-03-08 04:53:11 +00:00
|
|
|
return _( "Destination folder doesn't exist" );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_ERROR_IO_PERMISSIONS:
|
2008-03-04 02:13:53 +00:00
|
|
|
return tr_strerror( EACCES );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-04-20 02:05:07 +00:00
|
|
|
case TR_ERROR_IO_SPACE:
|
2008-03-04 02:13:53 +00:00
|
|
|
return tr_strerror( ENOSPC );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-01 01:50:14 +00:00
|
|
|
case TR_ERROR_IO_FILE_TOO_BIG:
|
2008-03-04 02:13:53 +00:00
|
|
|
return tr_strerror( EFBIG );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-01 01:50:14 +00:00
|
|
|
case TR_ERROR_IO_OPEN_FILES:
|
2008-03-04 02:13:53 +00:00
|
|
|
return tr_strerror( EMFILE );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-18 19:13:32 +00:00
|
|
|
case TR_ERROR_IO_DUP_DOWNLOAD:
|
2008-09-23 19:11:04 +00:00
|
|
|
return _(
|
|
|
|
"A torrent with this name and destination folder already exists." );
|
|
|
|
|
2008-03-18 17:02:08 +00:00
|
|
|
case TR_ERROR_IO_CHECKSUM:
|
|
|
|
return _( "Checksum failed" );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_ERROR_IO_OTHER:
|
2008-03-19 02:16:07 +00:00
|
|
|
return _( "Unspecified I/O error" );
|
2008-01-21 02:11:57 +00:00
|
|
|
|
2008-03-02 23:44:34 +00:00
|
|
|
case TR_ERROR_TC_ERROR:
|
2008-03-04 02:02:25 +00:00
|
|
|
return _( "Tracker error" );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-02 23:44:34 +00:00
|
|
|
case TR_ERROR_TC_WARNING:
|
2008-03-04 02:02:25 +00:00
|
|
|
return _( "Tracker warning" );
|
2008-03-02 23:44:34 +00:00
|
|
|
|
|
|
|
case TR_ERROR_PEER_MESSAGE:
|
2008-03-04 02:02:25 +00:00
|
|
|
return _( "Peer sent a bad message" );
|
2008-03-02 23:44:34 +00:00
|
|
|
|
2008-01-18 19:13:32 +00:00
|
|
|
default:
|
2008-03-04 02:02:25 +00:00
|
|
|
return _( "Unknown error" );
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2008-06-06 23:53:30 +00:00
|
|
|
void*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_memdup( const void * in,
|
|
|
|
int byteCount )
|
2008-06-06 23:53:30 +00:00
|
|
|
{
|
|
|
|
void * out = tr_new( uint8_t, byteCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-06 23:53:30 +00:00
|
|
|
memcpy( out, in, byteCount );
|
|
|
|
return out;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
char*
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_strdup( const void * in )
|
2007-06-18 19:39:52 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
return tr_strndup( in, in ? strlen( (const char*)in ) : 0 );
|
2007-06-18 19:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_strndup( const void * in,
|
|
|
|
int len )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
char * out = NULL;
|
2007-07-15 19:12:54 +00:00
|
|
|
|
2007-11-26 05:03:28 +00:00
|
|
|
if( len < 0 )
|
|
|
|
{
|
|
|
|
out = tr_strdup( in );
|
|
|
|
}
|
2008-08-01 16:43:22 +00:00
|
|
|
else if( in )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
out = tr_malloc( len + 1 );
|
2007-06-18 03:40:41 +00:00
|
|
|
memcpy( out, in, len );
|
2007-07-15 19:12:54 +00:00
|
|
|
out[len] = '\0';
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
char*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_strdup_printf( const char * fmt,
|
|
|
|
... )
|
2008-05-15 15:44:51 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * ret = NULL;
|
2008-05-15 15:44:51 +00:00
|
|
|
struct evbuffer * buf;
|
2008-09-23 19:11:04 +00:00
|
|
|
va_list ap;
|
2008-05-15 15:44:51 +00:00
|
|
|
|
|
|
|
buf = evbuffer_new( );
|
|
|
|
va_start( ap, fmt );
|
2008-08-11 04:40:29 +00:00
|
|
|
|
2008-05-15 15:44:51 +00:00
|
|
|
if( evbuffer_add_vprintf( buf, fmt, ap ) != -1 )
|
2008-08-21 16:12:17 +00:00
|
|
|
ret = tr_strdup( EVBUFFER_DATA( buf ) );
|
2008-05-15 15:44:51 +00:00
|
|
|
|
2008-08-11 04:40:29 +00:00
|
|
|
va_end( ap );
|
|
|
|
evbuffer_free( buf );
|
2008-05-15 15:44:51 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
void*
|
|
|
|
tr_malloc( size_t size )
|
|
|
|
{
|
|
|
|
return size ? malloc( size ) : NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
void*
|
|
|
|
tr_malloc0( size_t size )
|
|
|
|
{
|
|
|
|
void * ret = tr_malloc( size );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
memset( ret, 0, size );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-04 20:31:19 +00:00
|
|
|
void
|
|
|
|
tr_free( void * p )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
if( p )
|
|
|
|
free( p );
|
|
|
|
}
|
|
|
|
|
2008-02-19 04:16:04 +00:00
|
|
|
const char*
|
|
|
|
tr_strerror( int i )
|
|
|
|
{
|
|
|
|
const char * ret = strerror( i );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-19 04:16:04 +00:00
|
|
|
if( ret == NULL )
|
|
|
|
ret = "Unknown Error";
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-05 19:11:30 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
char*
|
|
|
|
tr_strstrip( char * str )
|
|
|
|
{
|
|
|
|
if( str != NULL )
|
|
|
|
{
|
|
|
|
size_t pos;
|
|
|
|
size_t len = strlen( str );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
while( len && isspace( str[len - 1] ) )
|
2008-09-05 19:11:30 +00:00
|
|
|
--len;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-05 19:11:30 +00:00
|
|
|
str[len] = '\0';
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( pos = 0; pos < len && isspace( str[pos] ); )
|
2008-09-05 19:11:30 +00:00
|
|
|
++pos;
|
|
|
|
|
|
|
|
len -= pos;
|
2008-09-23 19:11:04 +00:00
|
|
|
memmove( str, str + pos, len );
|
2008-09-05 19:11:30 +00:00
|
|
|
str[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield*
|
2008-06-07 01:44:54 +00:00
|
|
|
tr_bitfieldNew( size_t bitCount )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-06-07 01:44:54 +00:00
|
|
|
tr_bitfield * ret = tr_new0( tr_bitfield, 1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-07 01:44:54 +00:00
|
|
|
ret->bitCount = bitCount;
|
2008-09-23 19:11:04 +00:00
|
|
|
ret->byteCount = ( bitCount + 7u ) / 8u;
|
2008-06-07 01:44:54 +00:00
|
|
|
ret->bits = tr_new0( uint8_t, ret->byteCount );
|
2007-06-18 03:40:41 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield*
|
|
|
|
tr_bitfieldDup( const tr_bitfield * in )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-08-21 19:03:56 +00:00
|
|
|
tr_bitfield * ret = tr_new0( tr_bitfield, 1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-07 01:44:54 +00:00
|
|
|
ret->bitCount = in->bitCount;
|
|
|
|
ret->byteCount = in->byteCount;
|
|
|
|
ret->bits = tr_memdup( in->bits, in->byteCount );
|
2007-06-18 03:40:41 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-06-05 20:21:56 +00:00
|
|
|
void
|
|
|
|
tr_bitfieldFree( tr_bitfield * bitfield )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
if( bitfield )
|
|
|
|
{
|
2008-06-07 01:44:54 +00:00
|
|
|
tr_free( bitfield->bits );
|
|
|
|
tr_free( bitfield );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldClear( tr_bitfield * bitfield )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-06-07 01:44:54 +00:00
|
|
|
memset( bitfield->bits, 0, bitfield->byteCount );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldIsEmpty( const tr_bitfield * bitfield )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-01-27 16:08:20 +00:00
|
|
|
size_t i;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < bitfield->byteCount; ++i )
|
2007-07-14 04:26:30 +00:00
|
|
|
if( bitfield->bits[i] )
|
2007-06-18 03:40:41 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-03-02 23:44:34 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bitfieldAdd( tr_bitfield * bitfield,
|
|
|
|
size_t nth )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( bitfield );
|
|
|
|
assert( bitfield->bits );
|
2008-03-02 23:44:34 +00:00
|
|
|
|
2008-06-07 01:44:54 +00:00
|
|
|
if( nth >= bitfield->bitCount )
|
2008-03-02 23:44:34 +00:00
|
|
|
return -1;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
bitfield->bits[nth >> 3u] |= ( 0x80 >> ( nth & 7u ) );
|
2008-03-02 23:44:34 +00:00
|
|
|
return 0;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2008-08-14 11:31:25 +00:00
|
|
|
/* Sets bit range [begin, end) to 1 */
|
2008-03-02 23:44:34 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bitfieldAddRange( tr_bitfield * b,
|
|
|
|
size_t begin,
|
|
|
|
size_t end )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t sb, eb;
|
2008-08-14 11:31:25 +00:00
|
|
|
unsigned char sm, em;
|
|
|
|
|
|
|
|
end--;
|
|
|
|
|
|
|
|
if( ( end >= b->bitCount ) || ( begin > end ) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sb = begin >> 3;
|
|
|
|
sm = ~( 0xff << ( 8 - ( begin & 7 ) ) );
|
|
|
|
eb = end >> 3;
|
|
|
|
em = 0xff << ( 7 - ( end & 7 ) );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( sb == eb )
|
|
|
|
{
|
2008-08-14 11:31:25 +00:00
|
|
|
b->bits[sb] |= ( sm & em );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-14 11:31:25 +00:00
|
|
|
b->bits[sb] |= sm;
|
|
|
|
b->bits[eb] |= em;
|
|
|
|
if( ++sb < eb )
|
2008-09-23 19:11:04 +00:00
|
|
|
memset ( b->bits + sb, 0xff, eb - sb );
|
2008-08-14 11:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2008-03-02 23:44:34 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bitfieldRem( tr_bitfield * bitfield,
|
|
|
|
size_t nth )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( bitfield );
|
|
|
|
assert( bitfield->bits );
|
2007-09-25 17:43:39 +00:00
|
|
|
|
2008-06-07 01:44:54 +00:00
|
|
|
if( nth >= bitfield->bitCount )
|
2008-03-02 23:44:34 +00:00
|
|
|
return -1;
|
2007-10-31 04:23:51 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
bitfield->bits[nth >> 3u] &= ( 0xff7f >> ( nth & 7u ) );
|
2008-03-02 23:44:34 +00:00
|
|
|
return 0;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2008-08-14 11:31:25 +00:00
|
|
|
/* Clears bit range [begin, end) to 0 */
|
2008-03-02 23:44:34 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bitfieldRemRange( tr_bitfield * b,
|
|
|
|
size_t begin,
|
|
|
|
size_t end )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t sb, eb;
|
2008-08-13 19:25:08 +00:00
|
|
|
unsigned char sm, em;
|
|
|
|
|
2008-08-14 11:31:25 +00:00
|
|
|
end--;
|
|
|
|
|
2008-08-13 19:25:08 +00:00
|
|
|
if( ( end >= b->bitCount ) || ( begin > end ) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sb = begin >> 3;
|
|
|
|
sm = 0xff << ( 8 - ( begin & 7 ) );
|
|
|
|
eb = end >> 3;
|
2008-08-14 11:31:25 +00:00
|
|
|
em = ~( 0xff << ( 7 - ( end & 7 ) ) );
|
2008-08-13 19:25:08 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( sb == eb )
|
|
|
|
{
|
2008-08-13 19:25:08 +00:00
|
|
|
b->bits[sb] &= ( sm | em );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-13 19:25:08 +00:00
|
|
|
b->bits[sb] &= sm;
|
|
|
|
b->bits[eb] &= em;
|
|
|
|
if( ++sb < eb )
|
2008-09-23 19:11:04 +00:00
|
|
|
memset ( b->bits + sb, 0, eb - sb );
|
2008-08-13 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bitfieldOr( tr_bitfield * a,
|
|
|
|
const tr_bitfield * b )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint8_t * ait;
|
2007-06-18 03:40:41 +00:00
|
|
|
const uint8_t *aend, *bit;
|
|
|
|
|
2008-06-07 01:44:54 +00:00
|
|
|
assert( a->bitCount == b->bitCount );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ait = a->bits, bit = b->bits, aend = ait + a->byteCount;
|
|
|
|
ait != aend; )
|
2007-12-19 05:57:55 +00:00
|
|
|
*ait++ |= *bit++;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2008-05-06 19:06:46 +00:00
|
|
|
/* set 'a' to all the flags that were in 'a' but not 'b' */
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bitfieldDifference( tr_bitfield * a,
|
|
|
|
const tr_bitfield * b )
|
2008-05-06 19:06:46 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint8_t * ait;
|
2008-05-06 19:06:46 +00:00
|
|
|
const uint8_t *aend, *bit;
|
|
|
|
|
2008-06-07 01:44:54 +00:00
|
|
|
assert( a->bitCount == b->bitCount );
|
2008-05-06 19:06:46 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ait = a->bits, bit = b->bits, aend = ait + a->byteCount;
|
|
|
|
ait != aend; )
|
|
|
|
*ait++ &= ~( *bit++ );
|
2008-05-06 19:06:46 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
size_t
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfieldCountTrueBits( const tr_bitfield* b )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t ret = 0;
|
|
|
|
const uint8_t * it, *end;
|
2007-06-18 03:40:41 +00:00
|
|
|
static const int trueBitCount[512] = {
|
2008-09-23 19:11:04 +00:00
|
|
|
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3,
|
|
|
|
4, 2, 3, 3, 4, 3, 4, 4, 5,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
|
|
|
|
5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
|
|
|
|
5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
|
|
|
|
6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
|
|
|
|
5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
|
|
|
|
6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
|
|
|
|
6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6,
|
|
|
|
7, 5, 6, 6, 7, 6, 7, 7, 8,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
|
|
|
|
5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
|
|
|
|
6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
|
|
|
|
6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6,
|
|
|
|
7, 5, 6, 6, 7, 6, 7, 7, 8,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
|
|
|
|
6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6,
|
|
|
|
7, 5, 6, 6, 7, 6, 7, 7, 8,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6,
|
|
|
|
7, 5, 6, 6, 7, 6, 7, 7, 8,
|
|
|
|
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, 5, 6, 6, 7, 6, 7, 7,
|
|
|
|
8, 6, 7, 7, 8, 7, 8, 8, 9
|
2007-06-18 03:40:41 +00:00
|
|
|
};
|
|
|
|
|
2007-07-24 01:33:59 +00:00
|
|
|
if( !b )
|
|
|
|
return 0;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( it = b->bits, end = it + b->byteCount; it != end; ++it )
|
2007-06-18 03:40:41 +00:00
|
|
|
ret += trueBitCount[*it];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-07-12 17:51:45 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
tr_date( void )
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
gettimeofday( &tv, NULL );
|
|
|
|
return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-08-02 19:43:29 +00:00
|
|
|
tr_wait( uint64_t delay_milliseconds )
|
2007-07-12 17:51:45 +00:00
|
|
|
{
|
2007-08-04 01:17:39 +00:00
|
|
|
#ifdef __BEOS__
|
2007-08-02 19:43:29 +00:00
|
|
|
snooze( 1000 * delay_milliseconds );
|
2008-09-23 19:11:04 +00:00
|
|
|
#elif defined( WIN32 )
|
2007-08-02 19:43:29 +00:00
|
|
|
Sleep( (DWORD)delay_milliseconds );
|
2007-07-12 17:51:45 +00:00
|
|
|
#else
|
2007-08-02 19:43:29 +00:00
|
|
|
usleep( 1000 * delay_milliseconds );
|
2007-07-12 17:51:45 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-11-08 04:11:09 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-07-15 17:16:57 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_snprintf( char * buf,
|
|
|
|
size_t buflen,
|
|
|
|
const char * fmt,
|
|
|
|
... )
|
2008-07-15 17:16:57 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int len;
|
2008-07-15 17:16:57 +00:00
|
|
|
va_list args;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-15 17:16:57 +00:00
|
|
|
va_start( args, fmt );
|
|
|
|
len = evutil_vsnprintf( buf, buflen, fmt, args );
|
|
|
|
va_end( args );
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2007-11-08 04:11:09 +00:00
|
|
|
/*
|
|
|
|
* Copy src to string dst of size siz. At most siz-1 characters
|
|
|
|
* will be copied. Always NUL terminates (unless siz == 0).
|
|
|
|
* Returns strlen(src); if retval >= siz, truncation occurred.
|
|
|
|
*/
|
|
|
|
size_t
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_strlcpy( char * dst,
|
|
|
|
const void * src,
|
|
|
|
size_t siz )
|
2007-11-08 04:11:09 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
#ifdef HAVE_STRLCPY
|
|
|
|
return strlcpy( dst, src, siz );
|
|
|
|
#else
|
2008-09-23 19:11:04 +00:00
|
|
|
char * d = dst;
|
2008-05-18 16:44:30 +00:00
|
|
|
const char *s = src;
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t n = siz;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( s );
|
|
|
|
assert( d );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
/* Copy as many bytes as will fit */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( n != 0 )
|
|
|
|
{
|
|
|
|
while( --n != 0 )
|
|
|
|
{
|
|
|
|
if( ( *d++ = *s++ ) == '\0' )
|
2008-05-18 16:44:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-11-08 04:11:09 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( n == 0 )
|
|
|
|
{
|
|
|
|
if( siz != 0 )
|
2008-05-18 16:44:30 +00:00
|
|
|
*d = '\0'; /* NUL-terminate dst */
|
2008-09-23 19:11:04 +00:00
|
|
|
while( *s++ )
|
2008-05-18 16:44:30 +00:00
|
|
|
;
|
|
|
|
}
|
2007-11-08 04:11:09 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
return s - (char*)src - 1; /* count does not include NUL */
|
2008-05-18 16:44:30 +00:00
|
|
|
#endif
|
2007-11-08 04:11:09 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 19:13:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
double
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_getRatio( double numerator,
|
|
|
|
double denominator )
|
2008-01-04 19:13:30 +00:00
|
|
|
{
|
|
|
|
double ratio;
|
|
|
|
|
|
|
|
if( denominator )
|
|
|
|
ratio = numerator / denominator;
|
|
|
|
else if( numerator )
|
|
|
|
ratio = TR_RATIO_INF;
|
|
|
|
else
|
|
|
|
ratio = TR_RATIO_NA;
|
|
|
|
|
|
|
|
return ratio;
|
|
|
|
}
|
2008-02-25 20:21:22 +00:00
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_sha1_to_hex( char * out,
|
|
|
|
const uint8_t * sha1 )
|
2008-02-25 20:21:22 +00:00
|
|
|
{
|
|
|
|
static const char hex[] = "0123456789abcdef";
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; i < 20; i++ )
|
|
|
|
{
|
2008-02-25 20:21:22 +00:00
|
|
|
unsigned int val = *sha1++;
|
|
|
|
*out++ = hex[val >> 4];
|
|
|
|
*out++ = hex[val & 0xf];
|
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
}
|
2008-03-24 15:58:06 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
int
|
|
|
|
tr_httpIsValidURL( const char * url )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * c;
|
2008-05-21 20:56:12 +00:00
|
|
|
static const char * rfc2396_valid_chars =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz" /* lowalpha */
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* upalpha */
|
|
|
|
"0123456789" /* digit */
|
|
|
|
"-_.!~*'()" /* mark */
|
|
|
|
";/?:@&=+$," /* reserved */
|
|
|
|
"<>#%<\"" /* delims */
|
|
|
|
"{}|\\^[]`"; /* unwise */
|
|
|
|
|
2008-08-27 15:47:41 +00:00
|
|
|
if( url == NULL )
|
2008-08-27 15:46:50 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( c = url; c && *c; ++c )
|
2008-05-21 20:56:12 +00:00
|
|
|
if( !strchr( rfc2396_valid_chars, *c ) )
|
|
|
|
return FALSE;
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
return !tr_httpParseURL( url, -1, NULL, NULL, NULL );
|
|
|
|
}
|
|
|
|
|
2008-03-24 15:58:06 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_httpParseURL( const char * url_in,
|
|
|
|
int len,
|
|
|
|
char ** setme_host,
|
|
|
|
int * setme_port,
|
|
|
|
char ** setme_path )
|
2008-03-24 15:58:06 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
|
|
|
int port = 0;
|
|
|
|
int n;
|
|
|
|
char * tmp;
|
|
|
|
char * pch;
|
2008-04-24 01:42:53 +00:00
|
|
|
const char * protocol = NULL;
|
|
|
|
const char * host = NULL;
|
|
|
|
const char * path = NULL;
|
|
|
|
|
|
|
|
tmp = tr_strndup( url_in, len );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( pch = strstr( tmp, "://" ) ) )
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
*pch = '\0';
|
|
|
|
protocol = tmp;
|
|
|
|
pch += 3;
|
|
|
|
/*fprintf( stderr, "protocol is [%s]... what's left is [%s]\n", protocol, pch
|
|
|
|
);*/
|
|
|
|
if( ( n = strcspn( pch, ":/" ) ) )
|
|
|
|
{
|
|
|
|
const int havePort = pch[n] == ':';
|
|
|
|
host = pch;
|
|
|
|
pch += n;
|
|
|
|
*pch++ = '\0';
|
2008-04-24 01:42:53 +00:00
|
|
|
/*fprintf( stderr, "host is [%s]... what's left is [%s]\n", host, pch );*/
|
2008-09-23 19:11:04 +00:00
|
|
|
if( havePort )
|
|
|
|
{
|
|
|
|
char * end;
|
|
|
|
port = strtol( pch, &end, 10 );
|
|
|
|
pch = end;
|
2008-04-24 01:42:53 +00:00
|
|
|
/*fprintf( stderr, "port is [%d]... what's left is [%s]\n", port, pch );*/
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
path = pch;
|
2008-04-24 01:42:53 +00:00
|
|
|
/*fprintf( stderr, "path is [%s]\n", path );*/
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
2008-03-24 15:58:06 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
err = !host || !path || !protocol
|
|
|
|
|| ( strcmp( protocol, "http" ) && strcmp( protocol, "https" ) );
|
2008-03-24 15:58:06 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !err && !port )
|
|
|
|
{
|
|
|
|
if( !strcmp( protocol, "http" ) ) port = 80;
|
|
|
|
if( !strcmp( protocol, "https" ) ) port = 443;
|
2008-03-24 15:58:06 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !err )
|
|
|
|
{
|
|
|
|
if( setme_host ){ ( (char*)host )[-3] = ':'; *setme_host =
|
|
|
|
tr_strdup( protocol ); }
|
|
|
|
if( setme_path ){ ( (char*)path )[-1] = '/'; *setme_path =
|
|
|
|
tr_strdup( path - 1 ); }
|
|
|
|
if( setme_port ) *setme_port = port;
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
2008-03-24 15:58:06 +00:00
|
|
|
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
tr_free( tmp );
|
|
|
|
return err;
|
|
|
|
}
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/hmac.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
|
|
|
|
char *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_base64_encode( const void * input,
|
|
|
|
int length,
|
|
|
|
int * setme_len )
|
2008-05-20 17:33:54 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * ret;
|
|
|
|
BIO * b64;
|
|
|
|
BIO * bmem;
|
2008-05-20 17:33:54 +00:00
|
|
|
BUF_MEM * bptr;
|
|
|
|
|
|
|
|
if( length < 1 )
|
2008-09-23 19:11:04 +00:00
|
|
|
length = strlen( input );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
bmem = BIO_new( BIO_s_mem( ) );
|
|
|
|
b64 = BIO_new( BIO_f_base64( ) );
|
|
|
|
b64 = BIO_push( b64, bmem );
|
|
|
|
BIO_write( b64, input, length );
|
|
|
|
(void) BIO_flush( b64 );
|
|
|
|
BIO_get_mem_ptr( b64, &bptr );
|
|
|
|
ret = tr_strndup( bptr->data, bptr->length );
|
|
|
|
if( setme_len )
|
|
|
|
*setme_len = bptr->length;
|
|
|
|
|
|
|
|
BIO_free_all( b64 );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_base64_decode( const void * input,
|
|
|
|
int length,
|
|
|
|
int * setme_len )
|
2008-05-20 17:33:54 +00:00
|
|
|
{
|
|
|
|
char * ret;
|
2008-09-23 19:11:04 +00:00
|
|
|
BIO * b64;
|
|
|
|
BIO * bmem;
|
|
|
|
int retlen;
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
if( length < 1 )
|
2008-09-23 19:11:04 +00:00
|
|
|
length = strlen( input );
|
2008-05-20 17:33:54 +00:00
|
|
|
|
|
|
|
ret = tr_new0( char, length );
|
|
|
|
b64 = BIO_new( BIO_f_base64( ) );
|
|
|
|
bmem = BIO_new_mem_buf( (unsigned char*)input, length );
|
|
|
|
bmem = BIO_push( b64, bmem );
|
|
|
|
retlen = BIO_read( bmem, ret, length );
|
2008-05-22 03:42:26 +00:00
|
|
|
if( !retlen )
|
|
|
|
{
|
|
|
|
/* try again, but with the BIO_FLAGS_BASE64_NO_NL flag */
|
|
|
|
BIO_free_all( bmem );
|
|
|
|
b64 = BIO_new( BIO_f_base64( ) );
|
|
|
|
BIO_set_flags( b64, BIO_FLAGS_BASE64_NO_NL );
|
|
|
|
bmem = BIO_new_mem_buf( (unsigned char*)input, length );
|
|
|
|
bmem = BIO_push( b64, bmem );
|
|
|
|
retlen = BIO_read( bmem, ret, length );
|
|
|
|
}
|
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
if( setme_len )
|
|
|
|
*setme_len = retlen;
|
|
|
|
|
|
|
|
BIO_free_all( bmem );
|
|
|
|
return ret;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|