source: trunk/libtransmission/utils.c @ 10044

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

(trunk libT) #2833 "LP64 bug on OpenBSD" -- fixed in trunk for 1.90

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