source: trunk/libtransmission/utils.c @ 10916

Last change on this file since 10916 was 10916, checked in by charles, 13 years ago

(trunk libT) #3381 "replace calls to usleep() with calls to nanosleep()" -- done.

  • Property svn:keywords set to Date Rev Author Id
File size: 35.6 KB
Line 
1/*
2 * This file Copyright (C) 2009-2010 Mnemosyne LLC
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.c 10916 2010-07-01 03:59:06Z charles $
11 */
12
13#ifdef HAVE_MEMMEM
14 #define _GNU_SOURCE /* glibc's string.h needs this to pick up memmem */
15#endif
16
17#if defined(SYS_DARWIN)
18 #define HAVE_GETPAGESIZE
19 #define HAVE_VALLOC
20 #undef HAVE_POSIX_MEMALIGN /* not supported on OS X 10.5 and lower */
21#endif
22
23#include <assert.h>
24#include <ctype.h> /* isalpha(), tolower() */
25#include <errno.h>
26#include <math.h> /* pow(), fabs() */
27#include <stdarg.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h> /* strerror(), memset(), memmem() */
31#include <time.h> /* nanosleep() */
32
33#include <libgen.h> /* basename() */
34#include <sys/time.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <unistd.h> /* stat(), getcwd(), getpagesize() */
38
39#include "event.h"
40
41#ifdef WIN32
42 #include <w32api.h>
43 #define WINVER WindowsXP /* freeaddrinfo(), getaddrinfo(), getnameinfo() */
44 #include <direct.h> /* _getcwd() */
45 #include <windows.h> /* Sleep() */
46#endif
47
48#include "transmission.h"
49#include "fdlimit.h"
50#include "ConvertUTF.h"
51#include "list.h"
52#include "net.h"
53#include "utils.h"
54#include "platform.h"
55#include "version.h"
56
57
58time_t transmission_now = 0;
59
60tr_msg_level messageLevel = TR_MSG_INF;
61static tr_lock *      messageLock = NULL;
62static tr_bool        messageQueuing = FALSE;
63static tr_msg_list *  messageQueue = NULL;
64static tr_msg_list ** messageQueueTail = &messageQueue;
65static int            messageQueueCount = 0;
66
67#ifndef WIN32
68    /* make null versions of these win32 functions */
69    static inline int IsDebuggerPresent( void ) { return FALSE; }
70    static inline void OutputDebugString( const void * unused UNUSED ) { }
71#endif
72
73/***
74****
75***/
76
77void
78tr_msgInit( void )
79{
80    const char * env = getenv( "TR_DEBUG" );
81    messageLevel = ( env ? atoi( env ) : 0 ) + 1;
82    messageLevel = MAX( 1, messageLevel );
83
84    if( messageLock == NULL )
85        messageLock = tr_lockNew( );
86}
87
88FILE*
89tr_getLog( void )
90{
91    static tr_bool initialized = FALSE;
92    static FILE * file = NULL;
93
94    if( !initialized )
95    {
96        const char * str = getenv( "TR_DEBUG_FD" );
97        int          fd = 0;
98        if( str && *str )
99            fd = atoi( str );
100        switch( fd )
101        {
102            case 1:
103                file = stdout; break;
104
105            case 2:
106                file = stderr; break;
107
108            default:
109                file = NULL; break;
110        }
111        initialized = TRUE;
112    }
113
114    return file;
115}
116
117void
118tr_setMessageLevel( tr_msg_level level )
119{
120    messageLevel = level;
121}
122
123tr_msg_level
124tr_getMessageLevel( void )
125{
126    return messageLevel;
127}
128
129void
130tr_setMessageQueuing( tr_bool enabled )
131{
132    messageQueuing = enabled;
133}
134
135tr_bool
136tr_getMessageQueuing( void )
137{
138    return messageQueuing != 0;
139}
140
141tr_msg_list *
142tr_getQueuedMessages( void )
143{
144    tr_msg_list * ret;
145    tr_lockLock( messageLock );
146
147    ret = messageQueue;
148    messageQueue = NULL;
149    messageQueueTail = &messageQueue;
150
151    messageQueueCount = 0;
152
153    tr_lockUnlock( messageLock );
154    return ret;
155}
156
157void
158tr_freeMessageList( tr_msg_list * list )
159{
160    tr_msg_list * next;
161
162    while( NULL != list )
163    {
164        next = list->next;
165        free( list->message );
166        free( list->name );
167        free( list );
168        list = next;
169    }
170}
171
172/**
173***
174**/
175
176struct tm *
177tr_localtime_r( const time_t *_clock, struct tm *_result )
178{
179#ifdef HAVE_LOCALTIME_R
180    return localtime_r( _clock, _result );
181#else
182    struct tm *p = localtime( _clock );
183    if( p )
184        *(_result) = *p;
185    return p;
186#endif
187}
188
189char*
190tr_getLogTimeStr( char * buf, int buflen )
191{
192    char           tmp[64];
193    struct tm      now_tm;
194    struct timeval tv;
195    time_t         seconds;
196    int            milliseconds;
197
198    gettimeofday( &tv, NULL );
199
200    seconds = tv.tv_sec;
201    tr_localtime_r( &seconds, &now_tm );
202    strftime( tmp, sizeof( tmp ), "%H:%M:%S", &now_tm );
203    milliseconds = (int)( tv.tv_usec / 1000 );
204    tr_snprintf( buf, buflen, "%s.%03d", tmp, milliseconds );
205
206    return buf;
207}
208
209tr_bool
210tr_deepLoggingIsActive( void )
211{
212    static int8_t deepLoggingIsActive = -1;
213
214    if( deepLoggingIsActive < 0 )
215        deepLoggingIsActive = IsDebuggerPresent() || (tr_getLog()!=NULL);
216
217    return deepLoggingIsActive != 0;
218}
219
220void
221tr_deepLog( const char  * file,
222            int           line,
223            const char  * name,
224            const char  * fmt,
225            ... )
226{
227    FILE * fp = tr_getLog( );
228    if( fp || IsDebuggerPresent( ) )
229    {
230        va_list           args;
231        char              timestr[64];
232        struct evbuffer * buf = evbuffer_new( );
233        char *            base = tr_basename( file );
234
235        evbuffer_add_printf( buf, "[%s] ",
236                            tr_getLogTimeStr( timestr, sizeof( timestr ) ) );
237        if( name )
238            evbuffer_add_printf( buf, "%s ", name );
239        va_start( args, fmt );
240        evbuffer_add_vprintf( buf, fmt, args );
241        va_end( args );
242        evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
243        /* FIXME(libevent2) ifdef this out for nonwindows platforms */
244        OutputDebugString( EVBUFFER_DATA( buf ) );
245        if(fp) /* FIXME(libevent2) tr_getLog() should return an fd, then use evbuffer_write() here ) */
246            (void) fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );
247
248        tr_free( base );
249        evbuffer_free( buf );
250    }
251}
252
253/***
254****
255***/
256
257void
258tr_msg( const char * file, int line,
259        tr_msg_level level,
260        const char * name,
261        const char * fmt, ... )
262{
263    const int err = errno; /* message logging shouldn't affect errno */
264    char buf[1024];
265    va_list ap;
266
267    if( messageLock != NULL )
268        tr_lockLock( messageLock );
269
270    /* build the text message */
271    *buf = '\0';
272    va_start( ap, fmt );
273    evutil_vsnprintf( buf, sizeof( buf ), fmt, ap );
274    va_end( ap );
275
276    OutputDebugString( buf );
277
278    if( *buf )
279    {
280        if( messageQueuing )
281        {
282            tr_msg_list * newmsg;
283            newmsg = tr_new0( tr_msg_list, 1 );
284            newmsg->level = level;
285            newmsg->when = tr_time( );
286            newmsg->message = tr_strdup( buf );
287            newmsg->file = file;
288            newmsg->line = line;
289            newmsg->name = tr_strdup( name );
290
291            *messageQueueTail = newmsg;
292            messageQueueTail = &newmsg->next;
293            ++messageQueueCount;
294
295            if( messageQueueCount > TR_MAX_MSG_LOG )
296            {
297                tr_msg_list * old = messageQueue;
298                messageQueue = old->next;
299                old->next = NULL;
300                tr_freeMessageList(old);
301
302                --messageQueueCount;
303
304                assert( messageQueueCount == TR_MAX_MSG_LOG );
305            }
306        }
307        else
308        {
309            char timestr[64];
310            FILE * fp;
311
312            fp = tr_getLog( );
313            if( fp == NULL )
314                fp = stderr;
315
316            tr_getLogTimeStr( timestr, sizeof( timestr ) );
317
318            if( name )
319                fprintf( fp, "[%s] %s: %s\n", timestr, name, buf );
320            else
321                fprintf( fp, "[%s] %s\n", timestr, buf );
322            fflush( fp );
323        }
324    }
325
326    if( messageLock != NULL )
327        tr_lockUnlock( messageLock );
328
329    errno = err;
330}
331
332/***
333****
334***/
335
336void
337tr_set_compare( const void * va,
338                size_t aCount,
339                const void * vb,
340                size_t bCount,
341                int compare( const void * a, const void * b ),
342                size_t elementSize,
343                tr_set_func in_a_cb,
344                tr_set_func in_b_cb,
345                tr_set_func in_both_cb,
346                void * userData )
347{
348    const uint8_t * a = (const uint8_t *) va;
349    const uint8_t * b = (const uint8_t *) vb;
350    const uint8_t * aend = a + elementSize * aCount;
351    const uint8_t * bend = b + elementSize * bCount;
352
353    while( a != aend || b != bend )
354    {
355        if( a == aend )
356        {
357            ( *in_b_cb )( (void*)b, userData );
358            b += elementSize;
359        }
360        else if( b == bend )
361        {
362            ( *in_a_cb )( (void*)a, userData );
363            a += elementSize;
364        }
365        else
366        {
367            const int val = ( *compare )( a, b );
368
369            if( !val )
370            {
371                ( *in_both_cb )( (void*)a, userData );
372                a += elementSize;
373                b += elementSize;
374            }
375            else if( val < 0 )
376            {
377                ( *in_a_cb )( (void*)a, userData );
378                a += elementSize;
379            }
380            else if( val > 0 )
381            {
382                ( *in_b_cb )( (void*)b, userData );
383                b += elementSize;
384            }
385        }
386    }
387}
388
389/***
390****
391***/
392
393const char*
394tr_strip_positional_args( const char* str )
395{
396    const char * in = str;
397    static size_t bufsize = 0;
398    static char * buf = NULL;
399    const size_t  len = strlen( str );
400    char *        out;
401
402    if( bufsize < len )
403    {
404        bufsize = len * 2;
405        buf = tr_renew( char, buf, bufsize );
406    }
407
408    for( out = buf; *str; ++str )
409    {
410        *out++ = *str;
411
412        if( ( *str == '%' ) && isdigit( str[1] ) )
413        {
414            const char * tmp = str + 1;
415            while( isdigit( *tmp ) )
416                ++tmp;
417            if( *tmp == '$' )
418                str = tmp[1]=='\'' ? tmp+1 : tmp;
419        }
420
421        if( ( *str == '%' ) && ( str[1] == '\'' ) )
422            str = str + 1;
423 
424    }
425    *out = '\0';
426
427    return strcmp( buf, in ) ? buf : in;
428}
429
430/**
431***
432**/
433
434void
435tr_timerAdd( struct event * timer, int seconds, int microseconds )
436{
437    struct timeval tv;
438    tv.tv_sec = seconds;
439    tv.tv_usec = microseconds;
440
441    assert( tv.tv_sec >= 0 );
442    assert( tv.tv_usec >= 0 );
443    assert( tv.tv_usec < 1000000 );
444
445    evtimer_add( timer, &tv );
446}
447
448void
449tr_timerAddMsec( struct event * timer, int msec )
450{
451    const int seconds =  msec / 1000;
452    const int usec = (msec%1000) * 1000;
453    tr_timerAdd( timer, seconds, usec );
454}
455
456/**
457***
458**/
459
460uint8_t *
461tr_loadFile( const char * path,
462             size_t *     size )
463{
464    uint8_t * buf;
465    struct stat  sb;
466    int fd;
467    ssize_t n;
468    const char * const err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
469
470    /* try to stat the file */
471    errno = 0;
472    if( stat( path, &sb ) )
473    {
474        const int err = errno;
475        tr_dbg( err_fmt, path, tr_strerror( errno ) );
476        errno = err;
477        return NULL;
478    }
479
480    if( ( sb.st_mode & S_IFMT ) != S_IFREG )
481    {
482        tr_err( err_fmt, path, _( "Not a regular file" ) );
483        errno = EISDIR;
484        return NULL;
485    }
486
487    /* Load the torrent file into our buffer */
488    fd = tr_open_file_for_scanning( path );
489    if( fd < 0 )
490    {
491        const int err = errno;
492        tr_err( err_fmt, path, tr_strerror( errno ) );
493        errno = err;
494        return NULL;
495    }
496    buf = malloc( sb.st_size + 1 );
497    if( !buf )
498    {
499        const int err = errno;
500        tr_err( err_fmt, path, _( "Memory allocation failed" ) );
501        tr_close_file( fd );
502        errno = err;
503        return NULL;
504    }
505    n = read( fd, buf, (size_t)sb.st_size );
506    if( n == -1 )
507    {
508        const int err = errno;
509        tr_err( err_fmt, path, tr_strerror( errno ) );
510        tr_close_file( fd );
511        free( buf );
512        errno = err;
513        return NULL;
514    }
515
516    tr_close_file( fd );
517    buf[ sb.st_size ] = '\0';
518    *size = sb.st_size;
519    return buf;
520}
521
522char*
523tr_basename( const char * path )
524{
525    char * tmp = tr_strdup( path );
526    char * ret = tr_strdup( basename( tmp ) );
527    tr_free( tmp );
528    return ret;
529}
530
531char*
532tr_dirname( const char * path )
533{
534    char * tmp = tr_strdup( path );
535    char * ret = tr_strdup( dirname( tmp ) );
536    tr_free( tmp );
537    return ret;
538}
539
540int
541tr_mkdir( const char * path,
542          int permissions
543#ifdef WIN32
544                       UNUSED
545#endif
546        )
547{
548#ifdef WIN32
549    if( path && isalpha( path[0] ) && path[1] == ':' && !path[2] )
550        return 0;
551    return mkdir( path );
552#else
553    return mkdir( path, permissions );
554#endif
555}
556
557int
558tr_mkdirp( const char * path_in,
559           int          permissions )
560{
561    char *      path = tr_strdup( path_in );
562    char *      p, * pp;
563    struct stat sb;
564    int         done;
565
566    /* walk past the root */
567    p = path;
568    while( *p == TR_PATH_DELIMITER )
569        ++p;
570
571    pp = p;
572    done = 0;
573    while( ( p =
574                strchr( pp, TR_PATH_DELIMITER ) ) || ( p = strchr( pp, '\0' ) ) )
575    {
576        if( !*p )
577            done = 1;
578        else
579            *p = '\0';
580
581        if( stat( path, &sb ) )
582        {
583            /* Folder doesn't exist yet */
584            if( tr_mkdir( path, permissions ) )
585            {
586                const int err = errno;
587                tr_err( _(
588                           "Couldn't create \"%1$s\": %2$s" ), path,
589                       tr_strerror( err ) );
590                tr_free( path );
591                errno = err;
592                return -1;
593            }
594        }
595        else if( ( sb.st_mode & S_IFMT ) != S_IFDIR )
596        {
597            /* Node exists but isn't a folder */
598            char * buf = tr_strdup_printf( _( "File \"%s\" is in the way" ), path );
599            tr_err( _( "Couldn't create \"%1$s\": %2$s" ), path_in, buf );
600            tr_free( buf );
601            tr_free( path );
602            errno = ENOTDIR;
603            return -1;
604        }
605
606        if( done )
607            break;
608
609        *p = TR_PATH_DELIMITER;
610        p++;
611        pp = p;
612    }
613
614    tr_free( path );
615    return 0;
616}
617
618char*
619tr_buildPath( const char *first_element, ... )
620{
621    size_t bufLen = 0;
622    const char * element;
623    char * buf;
624    char * pch;
625    va_list vl;
626
627    /* pass 1: allocate enough space for the string */
628    va_start( vl, first_element );
629    element = first_element;
630    while( element ) {
631        bufLen += strlen( element ) + 1;
632        element = (const char*) va_arg( vl, const char* );
633    }
634    pch = buf = tr_new( char, bufLen );
635    va_end( vl );
636
637    /* pass 2: build the string piece by piece */
638    va_start( vl, first_element );
639    element = first_element;
640    while( element ) {
641        const size_t elementLen = strlen( element );
642        memcpy( pch, element, elementLen );
643        pch += elementLen;
644        *pch++ = TR_PATH_DELIMITER;
645        element = (const char*) va_arg( vl, const char* );
646    }
647    va_end( vl );
648
649    /* terminate the string.  if nonempty, eat the unwanted trailing slash */
650    if( pch != buf )
651        --pch;
652    *pch++ = '\0';
653
654    /* sanity checks & return */
655    assert( pch - buf == (off_t)bufLen );
656    return buf;
657}
658
659/****
660*****
661****/
662
663char*
664tr_strndup( const void * in, int len )
665{
666    char * out = NULL;
667
668    if( len < 0 )
669    {
670        out = tr_strdup( in );
671    }
672    else if( in )
673    {
674        out = tr_malloc( len + 1 );
675        memcpy( out, in, len );
676        out[len] = '\0';
677    }
678
679    return out;
680}
681
682const char*
683tr_memmem( const char * haystack, size_t haystacklen,
684           const char * needle, size_t needlelen )
685{
686#ifdef HAVE_MEMMEM
687    return memmem( haystack, haystacklen, needle, needlelen );
688#else
689    size_t i;
690    if( !needlelen )
691        return haystack;
692    if( needlelen > haystacklen || !haystack || !needle )
693        return NULL;
694    for( i=0; i<=haystacklen-needlelen; ++i )
695        if( !memcmp( haystack+i, needle, needlelen ) )
696            return haystack+i;
697    return NULL;
698#endif
699}
700
701char*
702tr_strdup_printf( const char * fmt, ... )
703{
704    va_list ap;
705    char * ret;
706    size_t len;
707    char statbuf[2048];
708
709    va_start( ap, fmt );
710    len = evutil_vsnprintf( statbuf, sizeof( statbuf ), fmt, ap );
711    va_end( ap );
712    if( len < sizeof( statbuf ) )
713        ret = tr_strndup( statbuf, len );
714    else {
715        ret = tr_new( char, len + 1 );
716        va_start( ap, fmt );
717        evutil_vsnprintf( ret, len + 1, fmt, ap );
718        va_end( ap );
719    }
720
721    return ret;
722}
723
724const char*
725tr_strerror( int i )
726{
727    const char * ret = strerror( i );
728
729    if( ret == NULL )
730        ret = "Unknown Error";
731    return ret;
732}
733
734/****
735*****
736****/
737
738char*
739tr_strstrip( char * str )
740{
741    if( str != NULL )
742    {
743        size_t pos;
744        size_t len = strlen( str );
745
746        while( len && isspace( str[len - 1] ) )
747            --len;
748
749        for( pos = 0; pos < len && isspace( str[pos] ); )
750            ++pos;
751
752        len -= pos;
753        memmove( str, str + pos, len );
754        str[len] = '\0';
755    }
756
757    return str;
758}
759
760tr_bool
761tr_str_has_suffix( const char *str, const char *suffix )
762{
763    size_t str_len;
764    size_t suffix_len;
765
766    if( !str )
767        return FALSE;
768    if( !suffix )
769        return TRUE;
770
771    str_len = strlen( str );
772    suffix_len = strlen( suffix );
773    if( str_len < suffix_len )
774        return FALSE;
775
776    return !strncasecmp( str + str_len - suffix_len, suffix, suffix_len );
777}
778
779/****
780*****
781****/
782
783uint64_t
784tr_date( void )
785{
786    struct timeval tv;
787
788    gettimeofday( &tv, NULL );
789    return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
790}
791
792void
793tr_wait_msec( long int msec )
794{
795#ifdef WIN32
796    Sleep( (DWORD)msec );
797#else
798    struct timespec ts;
799    ts.tv_sec = msec / 1000;
800    ts.tv_sec = ( msec % 1000 ) * 1000000;
801    nanosleep( &ts, NULL );
802#endif
803}
804
805/***
806****
807***/
808
809int
810tr_snprintf( char * buf, size_t buflen, const char * fmt, ... )
811{
812    int     len;
813    va_list args;
814
815    va_start( args, fmt );
816    len = evutil_vsnprintf( buf, buflen, fmt, args );
817    va_end( args );
818    return len;
819}
820
821/*
822 * Copy src to string dst of size siz.  At most siz-1 characters
823 * will be copied.  Always NUL terminates (unless siz == 0).
824 * Returns strlen(src); if retval >= siz, truncation occurred.
825 */
826size_t
827tr_strlcpy( char *       dst,
828            const void * src,
829            size_t       siz )
830{
831#ifdef HAVE_STRLCPY
832    return strlcpy( dst, src, siz );
833#else
834    char *      d = dst;
835    const char *s = src;
836    size_t      n = siz;
837
838    assert( s );
839    assert( d );
840
841    /* Copy as many bytes as will fit */
842    if( n != 0 )
843    {
844        while( --n != 0 )
845        {
846            if( ( *d++ = *s++ ) == '\0' )
847                break;
848        }
849    }
850
851    /* Not enough room in dst, add NUL and traverse rest of src */
852    if( n == 0 )
853    {
854        if( siz != 0 )
855            *d = '\0'; /* NUL-terminate dst */
856        while( *s++ )
857            ;
858    }
859
860    return s - (char*)src - 1;  /* count does not include NUL */
861#endif
862}
863
864/***
865****
866***/
867
868double
869tr_getRatio( double numerator, double denominator )
870{
871    double ratio;
872
873    if( fabs(denominator) > 0.01 )
874        ratio = numerator / denominator;
875    else if( fabs(numerator) > 0.01 )
876        ratio = TR_RATIO_INF;
877    else
878        ratio = TR_RATIO_NA;
879
880    return ratio;
881}
882
883void
884tr_sha1_to_hex( char * out, const uint8_t * sha1 )
885{
886    int i;
887    static const char hex[] = "0123456789abcdef";
888
889    for( i=0; i<20; ++i )
890    {
891        const unsigned int val = *sha1++;
892        *out++ = hex[val >> 4];
893        *out++ = hex[val & 0xf];
894    }
895
896    *out = '\0';
897}
898
899void
900tr_hex_to_sha1( uint8_t * out, const char * in )
901{
902    int i;
903    static const char hex[] = "0123456789abcdef";
904
905    for( i=0; i<20; ++i )
906    {
907        const int hi = strchr( hex, tolower( *in++ ) ) - hex;
908        const int lo = strchr( hex, tolower( *in++ ) ) - hex;
909        *out++ = (uint8_t)( (hi<<4) | lo );
910    }
911}
912
913/***
914****
915***/
916
917static tr_bool
918isValidURLChars( const char * url )
919{
920    const char * c;
921    static const char * rfc2396_valid_chars =
922        "abcdefghijklmnopqrstuvwxyz" /* lowalpha */
923        "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* upalpha */
924        "0123456789"                 /* digit */
925        "-_.!~*'()"                  /* mark */
926        ";/?:@&=+$,"                 /* reserved */
927        "<>#%<\""                    /* delims */
928        "{}|\\^[]`";                 /* unwise */
929
930    if( url == NULL )
931        return FALSE;
932
933    for( c = url; c && *c; ++c )
934        if( !strchr( rfc2396_valid_chars, *c ) )
935            return FALSE;
936
937    return TRUE;
938}
939
940/** @brief return TRUE if the url is a http or https url that Transmission understands */
941tr_bool
942tr_urlIsValidTracker( const char * url )
943{
944    tr_bool valid;
945    char * scheme = NULL;
946
947    valid = isValidURLChars( url )
948         && !tr_urlParse( url, -1, &scheme, NULL, NULL, NULL )
949         && ( scheme != NULL )
950         && ( !strcmp(scheme,"http") || !strcmp(scheme,"https") );
951
952    tr_free( scheme );
953    return valid;
954}
955
956/** @brief return TRUE if the url is a http or https or ftp or sftp url that Transmission understands */
957tr_bool
958tr_urlIsValid( const char * url )
959{
960    tr_bool valid;
961    char * scheme = NULL;
962
963    valid = isValidURLChars( url )
964         && !tr_urlParse( url, -1, &scheme, NULL, NULL, NULL )
965         && ( scheme != NULL )
966         && ( !strcmp(scheme,"http") || !strcmp(scheme,"https") || !strcmp(scheme,"ftp") || !strcmp(scheme,"sftp") );
967
968    tr_free( scheme );
969    return valid;
970}
971
972tr_bool
973tr_addressIsIP( const char * address )
974{
975    tr_address tempAddr;
976    return tr_pton(address, &tempAddr) != NULL;
977}
978
979int
980tr_urlParse( const char * url_in,
981             int          len,
982             char **      setme_protocol,
983             char **      setme_host,
984             int *        setme_port,
985             char **      setme_path )
986{
987    int          err;
988    int          port = 0;
989    int          n;
990    char *       tmp;
991    char *       pch;
992    const char * protocol = NULL;
993    const char * host = NULL;
994    const char * path = NULL;
995
996    tmp = tr_strndup( url_in, len );
997    if( ( pch = strstr( tmp, "://" ) ) )
998    {
999        *pch = '\0';
1000        protocol = tmp;
1001        pch += 3;
1002/*fprintf( stderr, "protocol is [%s]... what's left is [%s]\n", protocol, pch);*/
1003        if( ( n = strcspn( pch, ":/" ) ) )
1004        {
1005            const int havePort = pch[n] == ':';
1006            host = pch;
1007            pch += n;
1008            *pch++ = '\0';
1009/*fprintf( stderr, "host is [%s]... what's left is [%s]\n", host, pch );*/
1010            if( havePort )
1011            {
1012                char * end;
1013                port = strtol( pch, &end, 10 );
1014                pch = end;
1015/*fprintf( stderr, "port is [%d]... what's left is [%s]\n", port, pch );*/
1016            }
1017            path = pch;
1018/*fprintf( stderr, "path is [%s]\n", path );*/
1019        }
1020    }
1021
1022    err = !host || !path || !protocol;
1023
1024    if( !err && !port )
1025    {
1026        if( !strcmp( protocol, "ftp" ) ) port = 21;
1027        if( !strcmp( protocol, "sftp" ) ) port = 22;
1028        if( !strcmp( protocol, "http" ) ) port = 80;
1029        if( !strcmp( protocol, "https" ) ) port = 443;
1030    }
1031
1032    if( !err )
1033    {
1034        if( setme_protocol ) *setme_protocol = tr_strdup( protocol );
1035
1036        if( setme_host ){ ( (char*)host )[-3] = ':'; *setme_host =
1037                              tr_strdup( host ); }
1038        if( setme_path ){ if( path[0] == '/' ) *setme_path = tr_strdup( path );
1039                          else { ( (char*)path )[-1] = '/'; *setme_path = tr_strdup( path - 1 ); } }
1040        if( setme_port ) *setme_port = port;
1041    }
1042
1043
1044    tr_free( tmp );
1045    return err;
1046}
1047
1048#include <string.h>
1049#include <openssl/sha.h>
1050#include <openssl/hmac.h>
1051#include <openssl/evp.h>
1052#include <openssl/bio.h>
1053#include <openssl/buffer.h>
1054
1055char *
1056tr_base64_encode( const void * input, int length, int * setme_len )
1057{
1058    int retlen = 0;
1059    char * ret = NULL;
1060
1061    if( input != NULL )
1062    {
1063        BIO * b64;
1064        BIO * bmem;
1065        BUF_MEM * bptr;
1066
1067        if( length < 1 )
1068            length = strlen( input );
1069
1070        bmem = BIO_new( BIO_s_mem( ) );
1071        b64 = BIO_new( BIO_f_base64( ) );
1072        b64 = BIO_push( b64, bmem );
1073        BIO_write( b64, input, length );
1074        (void) BIO_flush( b64 );
1075        BIO_get_mem_ptr( b64, &bptr );
1076        ret = tr_strndup( bptr->data, bptr->length );
1077        retlen = bptr->length;
1078        BIO_free_all( b64 );
1079    }
1080
1081    if( setme_len )
1082        *setme_len = retlen;
1083
1084    return ret;
1085}
1086
1087char *
1088tr_base64_decode( const void * input,
1089                  int          length,
1090                  int *        setme_len )
1091{
1092    char * ret;
1093    BIO *  b64;
1094    BIO *  bmem;
1095    int    retlen;
1096
1097    if( length < 1 )
1098        length = strlen( input );
1099
1100    ret = tr_new0( char, length );
1101    b64 = BIO_new( BIO_f_base64( ) );
1102    bmem = BIO_new_mem_buf( (unsigned char*)input, length );
1103    bmem = BIO_push( b64, bmem );
1104    retlen = BIO_read( bmem, ret, length );
1105    if( !retlen )
1106    {
1107        /* try again, but with the BIO_FLAGS_BASE64_NO_NL flag */
1108        BIO_free_all( bmem );
1109        b64 = BIO_new( BIO_f_base64( ) );
1110        BIO_set_flags( b64, BIO_FLAGS_BASE64_NO_NL );
1111        bmem = BIO_new_mem_buf( (unsigned char*)input, length );
1112        bmem = BIO_push( b64, bmem );
1113        retlen = BIO_read( bmem, ret, length );
1114    }
1115
1116    if( setme_len )
1117        *setme_len = retlen;
1118
1119    BIO_free_all( bmem );
1120    return ret;
1121}
1122
1123/***
1124****
1125***/
1126
1127int
1128tr_lowerBound( const void * key,
1129               const void * base,
1130               size_t       nmemb,
1131               size_t       size,
1132               int       (* compar)(const void* key, const void* arrayMember),
1133               tr_bool    * exact_match )
1134{
1135    size_t first = 0;
1136    const char * cbase = base;
1137    tr_bool exact = FALSE;
1138
1139    while( nmemb != 0 )
1140    {
1141        const size_t half = nmemb / 2;
1142        const size_t middle = first + half;
1143        const int c = compar( key, cbase + size*middle );
1144
1145        if( c <= 0 ) {
1146            if( c == 0 )
1147                exact = TRUE;
1148            nmemb = half;
1149        } else {
1150            first = middle + 1;
1151            nmemb = nmemb - half - 1;
1152        }
1153    }
1154
1155    *exact_match = exact;
1156
1157    return first;
1158}
1159
1160/***
1161****
1162***/
1163
1164char*
1165tr_utf8clean( const char * str, int max_len, tr_bool * err )
1166{
1167    char * ret;
1168    const char * end;
1169
1170    if( max_len < 0 )
1171        max_len = (int) strlen( str );
1172
1173    if( err != NULL )
1174        *err = FALSE;
1175
1176    if( tr_utf8_validate( str, max_len, &end  ) )
1177    {
1178        ret = tr_strndup( str, max_len );
1179    }
1180    else
1181    {
1182        const char zero = '\0';
1183        struct evbuffer * buf = evbuffer_new( );
1184
1185        while( !tr_utf8_validate ( str, max_len, &end ) )
1186        {
1187            const int good_len = end - str;
1188
1189            evbuffer_add( buf, str, good_len );
1190            max_len -= ( good_len + 1 );
1191            str += ( good_len + 1 );
1192            evbuffer_add( buf, "?", 1 );
1193
1194            if( err != NULL )
1195                *err = TRUE;
1196        }
1197
1198        evbuffer_add( buf, str, max_len );
1199        evbuffer_add( buf, &zero, 1 );
1200        ret = tr_memdup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
1201        evbuffer_free( buf );
1202    }
1203
1204    assert( tr_utf8_validate( ret, -1, NULL ) );
1205    return ret;
1206}
1207
1208/***
1209****
1210***/
1211
1212struct number_range
1213{
1214    int low;
1215    int high;
1216};
1217
1218/**
1219 * This should be a single number (ex. "6") or a range (ex. "6-9").
1220 * Anything else is an error and will return failure.
1221 */
1222static tr_bool
1223parseNumberSection( const char * str, int len, struct number_range * setme )
1224{
1225    long a, b;
1226    tr_bool success;
1227    char * end;
1228    const int error = errno;
1229    char * tmp = tr_strndup( str, len );
1230
1231    errno = 0;
1232    a = b = strtol( tmp, &end, 10 );
1233    if( errno || ( end == tmp ) ) {
1234        success = FALSE;
1235    } else if( *end != '-' ) {
1236        b = a;
1237        success = TRUE;
1238    } else {
1239        const char * pch = end + 1;
1240        b = strtol( pch, &end, 10 );
1241        if( errno || ( pch == end ) )
1242            success = FALSE;
1243        else if( *end ) /* trailing data */
1244            success = FALSE;
1245        else
1246            success = TRUE;
1247    }
1248    tr_free( tmp );
1249
1250    setme->low = MIN( a, b );
1251    setme->high = MAX( a, b );
1252
1253    errno = error;
1254    return success;
1255}
1256
1257static int
1258compareInt( const void * va, const void * vb )
1259{
1260    const int a = *(const int *)va;
1261    const int b = *(const int *)vb;
1262    return a - b;
1263}
1264
1265/**
1266 * Given a string like "1-4" or "1-4,6,9,14-51", this allocates and returns an
1267 * array of setmeCount ints of all the values in the array.
1268 * For example, "5-8" will return [ 5, 6, 7, 8 ] and setmeCount will be 4.
1269 * It's the caller's responsibility to call tr_free() on the returned array.
1270 * If a fragment of the string can't be parsed, NULL is returned.
1271 */
1272int*
1273tr_parseNumberRange( const char * str_in, int len, int * setmeCount )
1274{
1275    int n = 0;
1276    int * uniq = NULL;
1277    char * str = tr_strndup( str_in, len );
1278    const char * walk;
1279    tr_list * ranges = NULL;
1280    tr_bool success = TRUE;
1281
1282    walk = str;
1283    while( walk && *walk && success ) {
1284        struct number_range range;
1285        const char * pch = strchr( walk, ',' );
1286        if( pch ) {
1287            success = parseNumberSection( walk, pch-walk, &range );
1288            walk = pch + 1;
1289        } else {
1290            success = parseNumberSection( walk, strlen( walk ), &range );
1291            walk += strlen( walk );
1292        }
1293        if( success )
1294            tr_list_append( &ranges, tr_memdup( &range, sizeof( struct number_range ) ) );
1295    }
1296
1297    if( !success )
1298    {
1299        *setmeCount = 0;
1300        uniq = NULL;
1301    }
1302    else
1303    {
1304        int i;
1305        int n2;
1306        tr_list * l;
1307        int * sorted = NULL;
1308
1309        /* build a sorted number array */
1310        n = n2 = 0;
1311        for( l=ranges; l!=NULL; l=l->next ) {
1312            const struct number_range * r = l->data;
1313            n += r->high + 1 - r->low;
1314        }
1315        sorted = tr_new( int, n );
1316        for( l=ranges; l!=NULL; l=l->next ) {
1317            const struct number_range * r = l->data;
1318            int i;
1319            for( i=r->low; i<=r->high; ++i )
1320                sorted[n2++] = i;
1321        }
1322        qsort( sorted, n, sizeof( int ), compareInt );
1323        assert( n == n2 );
1324
1325        /* remove duplicates */
1326        uniq = tr_new( int, n );
1327        for( i=n=0; i<n2; ++i )
1328            if( !n || uniq[n-1] != sorted[i] )
1329                uniq[n++] = sorted[i];
1330
1331        tr_free( sorted );
1332    }
1333
1334    /* cleanup */
1335    tr_list_free( &ranges, tr_free );
1336    tr_free( str );
1337
1338    /* return the result */
1339    *setmeCount = n;
1340    return uniq;
1341}
1342
1343/***
1344****
1345***/
1346
1347double
1348tr_truncd( double x, int decimal_places )
1349{
1350    const int i = (int) pow( 10, decimal_places ); 
1351    double x2 = (int)(x*i);
1352    return x2 / i;
1353}
1354
1355char*
1356tr_strtruncd( char * buf, double x, int precision, size_t buflen )
1357{
1358    tr_snprintf( buf, buflen, "%.*f", precision, tr_truncd( x, precision ) );
1359
1360    return buf;
1361}
1362
1363char*
1364tr_strpercent( char * buf, double x, size_t buflen )
1365{
1366    if( x < 10.0 )
1367        tr_strtruncd( buf, x, 2, buflen );
1368    else if( x < 100.0 )
1369        tr_strtruncd( buf, x, 1, buflen );
1370    else
1371        tr_strtruncd( buf, x, 0, buflen );
1372    return buf;
1373}
1374
1375char*
1376tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity )
1377{
1378    if( (int)ratio == TR_RATIO_NA )
1379        tr_strlcpy( buf, _( "None" ), buflen );
1380    else if( (int)ratio == TR_RATIO_INF )
1381        tr_strlcpy( buf, infinity, buflen );
1382    else
1383        tr_strpercent( buf, ratio, buflen );
1384    return buf;
1385}
1386
1387/***
1388****
1389***/
1390
1391int
1392tr_moveFile( const char * oldpath, const char * newpath, tr_bool * renamed )
1393{
1394    int in;
1395    int out;
1396    char * buf;
1397    struct stat st;
1398    off_t bytesLeft;
1399    const size_t buflen = 1024 * 128; /* 128 KiB buffer */
1400
1401    /* make sure the old file exists */
1402    if( stat( oldpath, &st ) ) {
1403        const int err = errno;
1404        errno = err;
1405        return -1;
1406    }
1407    if( !S_ISREG( st.st_mode ) ) {
1408        errno = ENOENT;
1409        return -1;
1410    }
1411    bytesLeft = st.st_size;
1412
1413    /* make sure the target directory exists */
1414    {
1415        char * newdir = tr_dirname( newpath );
1416        int i = tr_mkdirp( newdir, 0777 );
1417        tr_free( newdir );
1418        if( i )
1419            return i;
1420    }
1421
1422    /* they might be on the same filesystem... */
1423    {
1424        const int i = rename( oldpath, newpath );
1425        if( renamed != NULL )
1426            *renamed = i == 0;
1427        if( !i )
1428            return 0;
1429    }
1430
1431    /* copy the file */
1432    in = tr_open_file_for_scanning( oldpath );
1433    out = tr_open_file_for_writing( newpath );
1434    buf = tr_valloc( buflen );
1435    while( bytesLeft > 0 )
1436    {
1437        ssize_t bytesWritten;
1438        const off_t bytesThisPass = MIN( bytesLeft, buflen );
1439        const int numRead = read( in, buf, bytesThisPass );
1440        if( numRead < 0 )
1441            break;
1442        bytesWritten = write( out, buf, numRead );
1443        if( bytesWritten < 0 )
1444            break;
1445        bytesLeft -= bytesWritten;
1446    }
1447
1448    /* cleanup */
1449    tr_free( buf );
1450    tr_close_file( out );
1451    tr_close_file( in );
1452    if( bytesLeft != 0 )
1453        return -1;
1454
1455    unlink( oldpath );
1456    return 0;
1457}
1458
1459/***
1460****
1461***/
1462
1463void*
1464tr_valloc( size_t bufLen )
1465{
1466    size_t allocLen;
1467    void * buf = NULL;
1468    static size_t pageSize = 0;
1469
1470    if( !pageSize ) {
1471#ifdef HAVE_GETPAGESIZE
1472        pageSize = getpagesize();
1473#else /* guess */
1474        pageSize = 4096;
1475#endif
1476    }
1477
1478    allocLen = pageSize;
1479    while( allocLen < bufLen )
1480        allocLen += pageSize;
1481
1482#ifdef HAVE_POSIX_MEMALIGN
1483    if( !buf )
1484        posix_memalign( &buf, pageSize, allocLen );
1485#endif
1486#ifdef HAVE_VALLOC
1487    if( !buf )
1488        buf = valloc( allocLen );
1489#endif
1490    if( !buf )
1491        buf = malloc( allocLen );
1492
1493    tr_dbg( "tr_valloc(%zu) allocating %zu bytes", bufLen, allocLen );
1494    return buf;
1495}
1496
1497char *
1498tr_realpath( const char * path, char * resolved_path )
1499{
1500#ifdef WIN32
1501    /* From a message to the Mingw-msys list, Jun 2, 2005 by Mark Junker. */
1502    if( GetFullPathNameA( path, TR_PATH_MAX, resolved_path, NULL ) == 0 )
1503        return NULL;
1504    return resolved_path;
1505#else
1506    return realpath( path, resolved_path );
1507#endif
1508}
1509
1510/***
1511****
1512****
1513****
1514***/
1515
1516struct formatter_unit
1517{
1518    char * name;
1519    double value;
1520};
1521 
1522struct formatter_units
1523{
1524    struct formatter_unit units[4];
1525};
1526
1527static void
1528formatter_init( struct formatter_units * units,
1529                double kilo,
1530                const char * b, const char * kb,
1531                const char * mb, const char * gb )
1532{
1533    units->units[TR_FMT_B].name = tr_strdup( b );
1534    units->units[TR_FMT_B].value = 1;
1535
1536    units->units[TR_FMT_KB].name = tr_strdup( kb );
1537    units->units[TR_FMT_KB].value = kilo;
1538
1539    units->units[TR_FMT_MB].name = tr_strdup( mb );
1540    units->units[TR_FMT_MB].value = pow( kilo, 2 );
1541
1542    units->units[TR_FMT_GB].name = tr_strdup( gb );
1543    units->units[TR_FMT_GB].value = pow( kilo, 3 );
1544}
1545
1546static const char*
1547tr_formatter_units( const struct formatter_units * u, int size )
1548{
1549    assert( u != NULL );
1550    assert( 0<=size && size<4 );
1551
1552    return u->units[size].name;
1553}
1554
1555static char*
1556formatter_get_size_str( const struct formatter_units * u,
1557                        char * buf, uint64_t bytes, size_t buflen )
1558{
1559    int precision;
1560    double value;
1561    const char * units;
1562    const struct formatter_unit * unit;
1563
1564         if( bytes < u->units[1].value ) unit = &u->units[0];
1565    else if( bytes < u->units[2].value ) unit = &u->units[1];
1566    else if( bytes < u->units[3].value ) unit = &u->units[2];
1567    else                                 unit = &u->units[3];
1568
1569    value = bytes / unit->value;
1570    units = unit->name;
1571    if( unit->value == 1 )
1572        precision = 0;
1573    else if( value < 100 )
1574        precision = 2;
1575    else
1576        precision = 1;
1577    tr_snprintf( buf, buflen, "%.*f %s", precision, value, units );
1578    return buf;
1579}
1580
1581static struct formatter_units size_units;
1582
1583void
1584tr_formatter_size_init( double kilo, const char * b, const char * kb,
1585                                     const char * mb, const char * gb )
1586{
1587    formatter_init( &size_units, kilo, b, kb, mb, gb );
1588}
1589
1590char*
1591tr_formatter_size( char * buf, uint64_t bytes, size_t buflen )
1592{
1593    return formatter_get_size_str( &size_units, buf, bytes, buflen );
1594}
1595
1596const char*
1597tr_formatter_size_units( int i )
1598{
1599    return tr_formatter_units( &size_units, i );
1600}
1601
1602static struct formatter_units speed_units;
1603
1604void
1605tr_formatter_speed_init( double kilo, const char * b, const char * kb,
1606                                      const char * mb, const char * gb )
1607{
1608    formatter_init( &speed_units, kilo, b, kb, mb, gb );
1609}
1610
1611char*
1612tr_formatter_speed( char * buf, uint64_t bytes_per_second, size_t buflen )
1613{
1614    return formatter_get_size_str( &speed_units, buf, bytes_per_second, buflen );
1615}
1616
1617const char*
1618tr_formatter_speed_units( int i )
1619{
1620    return tr_formatter_units( &speed_units, i );
1621}
Note: See TracBrowser for help on using the repository browser.