1 | /* |
---|
2 | * This file Copyright (C) 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 13191 2012-02-03 16:44:07Z jordan $ |
---|
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_ICONV_OPEN |
---|
20 | #define HAVE_MKDTEMP |
---|
21 | #define HAVE_VALLOC |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <assert.h> |
---|
25 | #include <ctype.h> /* isdigit(), isalpha(), tolower() */ |
---|
26 | #include <errno.h> |
---|
27 | #include <float.h> /* DBL_EPSILON */ |
---|
28 | #include <locale.h> /* localeconv() */ |
---|
29 | #include <math.h> /* pow(), fabs(), floor() */ |
---|
30 | #include <stdarg.h> |
---|
31 | #include <stdio.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include <string.h> /* strerror(), memset(), memmem() */ |
---|
34 | #include <time.h> /* nanosleep() */ |
---|
35 | |
---|
36 | #ifdef HAVE_ICONV_OPEN |
---|
37 | #include <iconv.h> |
---|
38 | #endif |
---|
39 | #include <libgen.h> /* basename() */ |
---|
40 | #include <sys/time.h> |
---|
41 | #include <sys/types.h> |
---|
42 | #include <sys/stat.h> |
---|
43 | #include <unistd.h> /* stat(), getcwd(), getpagesize(), unlink() */ |
---|
44 | |
---|
45 | #include <event2/buffer.h> |
---|
46 | #include <event2/event.h> |
---|
47 | |
---|
48 | #ifdef WIN32 |
---|
49 | #include <w32api.h> |
---|
50 | #define WINVER WindowsXP /* freeaddrinfo(), getaddrinfo(), getnameinfo() */ |
---|
51 | #include <direct.h> /* _getcwd() */ |
---|
52 | #include <windows.h> /* Sleep() */ |
---|
53 | #endif |
---|
54 | |
---|
55 | #include "transmission.h" |
---|
56 | #include "bencode.h" |
---|
57 | #include "fdlimit.h" |
---|
58 | #include "ConvertUTF.h" |
---|
59 | #include "list.h" |
---|
60 | #include "utils.h" |
---|
61 | #include "platform.h" /* tr_lockLock(), TR_PATH_MAX */ |
---|
62 | #include "version.h" |
---|
63 | |
---|
64 | |
---|
65 | time_t __tr_current_time = 0; |
---|
66 | tr_msg_level __tr_message_level = TR_MSG_ERR; |
---|
67 | |
---|
68 | static bool messageQueuing = false; |
---|
69 | static tr_msg_list * messageQueue = NULL; |
---|
70 | static tr_msg_list ** messageQueueTail = &messageQueue; |
---|
71 | static int messageQueueCount = 0; |
---|
72 | |
---|
73 | #ifndef WIN32 |
---|
74 | /* make null versions of these win32 functions */ |
---|
75 | static inline int IsDebuggerPresent( void ) { return false; } |
---|
76 | static inline void OutputDebugString( const void * unused UNUSED ) { } |
---|
77 | #endif |
---|
78 | |
---|
79 | /*** |
---|
80 | **** |
---|
81 | ***/ |
---|
82 | |
---|
83 | static tr_lock* |
---|
84 | getMessageLock( void ) |
---|
85 | { |
---|
86 | static tr_lock * l = NULL; |
---|
87 | |
---|
88 | if( !l ) |
---|
89 | l = tr_lockNew( ); |
---|
90 | |
---|
91 | return l; |
---|
92 | } |
---|
93 | |
---|
94 | void* |
---|
95 | tr_getLog( void ) |
---|
96 | { |
---|
97 | static bool initialized = false; |
---|
98 | static FILE * file = NULL; |
---|
99 | |
---|
100 | if( !initialized ) |
---|
101 | { |
---|
102 | const char * str = getenv( "TR_DEBUG_FD" ); |
---|
103 | int fd = 0; |
---|
104 | if( str && *str ) |
---|
105 | fd = atoi( str ); |
---|
106 | switch( fd ) |
---|
107 | { |
---|
108 | case 1: |
---|
109 | file = stdout; break; |
---|
110 | |
---|
111 | case 2: |
---|
112 | file = stderr; break; |
---|
113 | |
---|
114 | default: |
---|
115 | file = NULL; break; |
---|
116 | } |
---|
117 | initialized = true; |
---|
118 | } |
---|
119 | |
---|
120 | return file; |
---|
121 | } |
---|
122 | |
---|
123 | void |
---|
124 | tr_setMessageLevel( tr_msg_level level ) |
---|
125 | { |
---|
126 | __tr_message_level = level; |
---|
127 | } |
---|
128 | |
---|
129 | void |
---|
130 | tr_setMessageQueuing( bool enabled ) |
---|
131 | { |
---|
132 | messageQueuing = enabled; |
---|
133 | } |
---|
134 | |
---|
135 | bool |
---|
136 | tr_getMessageQueuing( void ) |
---|
137 | { |
---|
138 | return messageQueuing != 0; |
---|
139 | } |
---|
140 | |
---|
141 | tr_msg_list * |
---|
142 | tr_getQueuedMessages( void ) |
---|
143 | { |
---|
144 | tr_msg_list * ret; |
---|
145 | tr_lockLock( getMessageLock( ) ); |
---|
146 | |
---|
147 | ret = messageQueue; |
---|
148 | messageQueue = NULL; |
---|
149 | messageQueueTail = &messageQueue; |
---|
150 | |
---|
151 | messageQueueCount = 0; |
---|
152 | |
---|
153 | tr_lockUnlock( getMessageLock( ) ); |
---|
154 | return ret; |
---|
155 | } |
---|
156 | |
---|
157 | void |
---|
158 | tr_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 | |
---|
176 | struct tm * |
---|
177 | tr_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 | |
---|
189 | char* |
---|
190 | tr_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 = tv.tv_usec / 1000; |
---|
204 | tr_snprintf( buf, buflen, "%s.%03d", tmp, milliseconds ); |
---|
205 | |
---|
206 | return buf; |
---|
207 | } |
---|
208 | |
---|
209 | bool |
---|
210 | tr_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 | |
---|
220 | void |
---|
221 | tr_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_pullup( buf, -1 ) ); |
---|
245 | if( fp ) |
---|
246 | fputs( (const char*)evbuffer_pullup( buf, -1 ), fp ); |
---|
247 | |
---|
248 | tr_free( base ); |
---|
249 | evbuffer_free( buf ); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | /*** |
---|
254 | **** |
---|
255 | ***/ |
---|
256 | |
---|
257 | void |
---|
258 | tr_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 | tr_lockLock( getMessageLock( ) ); |
---|
267 | |
---|
268 | /* build the text message */ |
---|
269 | *buf = '\0'; |
---|
270 | va_start( ap, fmt ); |
---|
271 | evutil_vsnprintf( buf, sizeof( buf ), fmt, ap ); |
---|
272 | va_end( ap ); |
---|
273 | |
---|
274 | OutputDebugString( buf ); |
---|
275 | |
---|
276 | if( *buf ) |
---|
277 | { |
---|
278 | if( messageQueuing ) |
---|
279 | { |
---|
280 | tr_msg_list * newmsg; |
---|
281 | newmsg = tr_new0( tr_msg_list, 1 ); |
---|
282 | newmsg->level = level; |
---|
283 | newmsg->when = tr_time( ); |
---|
284 | newmsg->message = tr_strdup( buf ); |
---|
285 | newmsg->file = file; |
---|
286 | newmsg->line = line; |
---|
287 | newmsg->name = tr_strdup( name ); |
---|
288 | |
---|
289 | *messageQueueTail = newmsg; |
---|
290 | messageQueueTail = &newmsg->next; |
---|
291 | ++messageQueueCount; |
---|
292 | |
---|
293 | if( messageQueueCount > TR_MAX_MSG_LOG ) |
---|
294 | { |
---|
295 | tr_msg_list * old = messageQueue; |
---|
296 | messageQueue = old->next; |
---|
297 | old->next = NULL; |
---|
298 | tr_freeMessageList(old); |
---|
299 | |
---|
300 | --messageQueueCount; |
---|
301 | |
---|
302 | assert( messageQueueCount == TR_MAX_MSG_LOG ); |
---|
303 | } |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | char timestr[64]; |
---|
308 | FILE * fp; |
---|
309 | |
---|
310 | fp = tr_getLog( ); |
---|
311 | if( fp == NULL ) |
---|
312 | fp = stderr; |
---|
313 | |
---|
314 | tr_getLogTimeStr( timestr, sizeof( timestr ) ); |
---|
315 | |
---|
316 | if( name ) |
---|
317 | fprintf( fp, "[%s] %s: %s\n", timestr, name, buf ); |
---|
318 | else |
---|
319 | fprintf( fp, "[%s] %s\n", timestr, buf ); |
---|
320 | fflush( fp ); |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | tr_lockUnlock( getMessageLock( ) ); |
---|
325 | errno = err; |
---|
326 | } |
---|
327 | |
---|
328 | /*** |
---|
329 | **** |
---|
330 | ***/ |
---|
331 | |
---|
332 | void* |
---|
333 | tr_malloc( size_t size ) |
---|
334 | { |
---|
335 | return size ? malloc( size ) : NULL; |
---|
336 | } |
---|
337 | |
---|
338 | void* |
---|
339 | tr_malloc0( size_t size ) |
---|
340 | { |
---|
341 | return size ? calloc( 1, size ) : NULL; |
---|
342 | } |
---|
343 | |
---|
344 | void |
---|
345 | tr_free( void * p ) |
---|
346 | { |
---|
347 | if( p != NULL ) |
---|
348 | free( p ); |
---|
349 | } |
---|
350 | |
---|
351 | void* |
---|
352 | tr_memdup( const void * src, size_t byteCount ) |
---|
353 | { |
---|
354 | return memcpy( tr_malloc( byteCount ), src, byteCount ); |
---|
355 | } |
---|
356 | |
---|
357 | /*** |
---|
358 | **** |
---|
359 | ***/ |
---|
360 | |
---|
361 | const char* |
---|
362 | tr_strip_positional_args( const char* str ) |
---|
363 | { |
---|
364 | const char * in = str; |
---|
365 | static size_t bufsize = 0; |
---|
366 | static char * buf = NULL; |
---|
367 | const size_t len = str ? strlen( str ) : 0; |
---|
368 | char * out; |
---|
369 | |
---|
370 | if( !buf || ( bufsize < len ) ) |
---|
371 | { |
---|
372 | bufsize = len * 2 + 1; |
---|
373 | buf = tr_renew( char, buf, bufsize ); |
---|
374 | } |
---|
375 | |
---|
376 | for( out = buf; str && *str; ++str ) |
---|
377 | { |
---|
378 | *out++ = *str; |
---|
379 | |
---|
380 | if( ( *str == '%' ) && isdigit( str[1] ) ) |
---|
381 | { |
---|
382 | const char * tmp = str + 1; |
---|
383 | while( isdigit( *tmp ) ) |
---|
384 | ++tmp; |
---|
385 | if( *tmp == '$' ) |
---|
386 | str = tmp[1]=='\'' ? tmp+1 : tmp; |
---|
387 | } |
---|
388 | |
---|
389 | if( ( *str == '%' ) && ( str[1] == '\'' ) ) |
---|
390 | str = str + 1; |
---|
391 | |
---|
392 | } |
---|
393 | *out = '\0'; |
---|
394 | |
---|
395 | return !in || strcmp( buf, in ) ? buf : in; |
---|
396 | } |
---|
397 | |
---|
398 | /** |
---|
399 | *** |
---|
400 | **/ |
---|
401 | |
---|
402 | void |
---|
403 | tr_timerAdd( struct event * timer, int seconds, int microseconds ) |
---|
404 | { |
---|
405 | struct timeval tv; |
---|
406 | tv.tv_sec = seconds; |
---|
407 | tv.tv_usec = microseconds; |
---|
408 | |
---|
409 | assert( tv.tv_sec >= 0 ); |
---|
410 | assert( tv.tv_usec >= 0 ); |
---|
411 | assert( tv.tv_usec < 1000000 ); |
---|
412 | |
---|
413 | evtimer_add( timer, &tv ); |
---|
414 | } |
---|
415 | |
---|
416 | void |
---|
417 | tr_timerAddMsec( struct event * timer, int msec ) |
---|
418 | { |
---|
419 | const int seconds = msec / 1000; |
---|
420 | const int usec = (msec%1000) * 1000; |
---|
421 | tr_timerAdd( timer, seconds, usec ); |
---|
422 | } |
---|
423 | |
---|
424 | /** |
---|
425 | *** |
---|
426 | **/ |
---|
427 | |
---|
428 | uint8_t * |
---|
429 | tr_loadFile( const char * path, |
---|
430 | size_t * size ) |
---|
431 | { |
---|
432 | uint8_t * buf; |
---|
433 | struct stat sb; |
---|
434 | int fd; |
---|
435 | ssize_t n; |
---|
436 | const char * const err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); |
---|
437 | |
---|
438 | /* try to stat the file */ |
---|
439 | errno = 0; |
---|
440 | if( stat( path, &sb ) ) |
---|
441 | { |
---|
442 | const int err = errno; |
---|
443 | tr_dbg( err_fmt, path, tr_strerror( errno ) ); |
---|
444 | errno = err; |
---|
445 | return NULL; |
---|
446 | } |
---|
447 | |
---|
448 | if( ( sb.st_mode & S_IFMT ) != S_IFREG ) |
---|
449 | { |
---|
450 | tr_err( err_fmt, path, _( "Not a regular file" ) ); |
---|
451 | errno = EISDIR; |
---|
452 | return NULL; |
---|
453 | } |
---|
454 | |
---|
455 | /* Load the torrent file into our buffer */ |
---|
456 | fd = tr_open_file_for_scanning( path ); |
---|
457 | if( fd < 0 ) |
---|
458 | { |
---|
459 | const int err = errno; |
---|
460 | tr_err( err_fmt, path, tr_strerror( errno ) ); |
---|
461 | errno = err; |
---|
462 | return NULL; |
---|
463 | } |
---|
464 | buf = tr_malloc( sb.st_size + 1 ); |
---|
465 | if( !buf ) |
---|
466 | { |
---|
467 | const int err = errno; |
---|
468 | tr_err( err_fmt, path, _( "Memory allocation failed" ) ); |
---|
469 | tr_close_file( fd ); |
---|
470 | errno = err; |
---|
471 | return NULL; |
---|
472 | } |
---|
473 | n = read( fd, buf, (size_t)sb.st_size ); |
---|
474 | if( n == -1 ) |
---|
475 | { |
---|
476 | const int err = errno; |
---|
477 | tr_err( err_fmt, path, tr_strerror( errno ) ); |
---|
478 | tr_close_file( fd ); |
---|
479 | free( buf ); |
---|
480 | errno = err; |
---|
481 | return NULL; |
---|
482 | } |
---|
483 | |
---|
484 | tr_close_file( fd ); |
---|
485 | buf[ sb.st_size ] = '\0'; |
---|
486 | *size = sb.st_size; |
---|
487 | return buf; |
---|
488 | } |
---|
489 | |
---|
490 | char* |
---|
491 | tr_basename( const char * path ) |
---|
492 | { |
---|
493 | char * tmp = tr_strdup( path ); |
---|
494 | char * ret = tr_strdup( basename( tmp ) ); |
---|
495 | tr_free( tmp ); |
---|
496 | return ret; |
---|
497 | } |
---|
498 | |
---|
499 | char* |
---|
500 | tr_dirname( const char * path ) |
---|
501 | { |
---|
502 | char * tmp = tr_strdup( path ); |
---|
503 | char * ret = tr_strdup( dirname( tmp ) ); |
---|
504 | tr_free( tmp ); |
---|
505 | return ret; |
---|
506 | } |
---|
507 | |
---|
508 | char* |
---|
509 | tr_mkdtemp( char * template ) |
---|
510 | { |
---|
511 | #ifdef HAVE_MKDTEMP |
---|
512 | return mkdtemp( template ); |
---|
513 | #else |
---|
514 | if( !mktemp( template ) || mkdir( template, 0700 ) ) |
---|
515 | return NULL; |
---|
516 | return template; |
---|
517 | #endif |
---|
518 | } |
---|
519 | |
---|
520 | int |
---|
521 | tr_mkdir( const char * path, |
---|
522 | int permissions |
---|
523 | #ifdef WIN32 |
---|
524 | UNUSED |
---|
525 | #endif |
---|
526 | ) |
---|
527 | { |
---|
528 | #ifdef WIN32 |
---|
529 | if( path && isalpha( path[0] ) && path[1] == ':' && !path[2] ) |
---|
530 | return 0; |
---|
531 | return mkdir( path ); |
---|
532 | #else |
---|
533 | return mkdir( path, permissions ); |
---|
534 | #endif |
---|
535 | } |
---|
536 | |
---|
537 | int |
---|
538 | tr_mkdirp( const char * path_in, |
---|
539 | int permissions ) |
---|
540 | { |
---|
541 | char * path = tr_strdup( path_in ); |
---|
542 | char * p, * pp; |
---|
543 | struct stat sb; |
---|
544 | int done; |
---|
545 | |
---|
546 | /* walk past the root */ |
---|
547 | p = path; |
---|
548 | while( *p == TR_PATH_DELIMITER ) |
---|
549 | ++p; |
---|
550 | |
---|
551 | pp = p; |
---|
552 | done = 0; |
---|
553 | while( ( p = |
---|
554 | strchr( pp, TR_PATH_DELIMITER ) ) || ( p = strchr( pp, '\0' ) ) ) |
---|
555 | { |
---|
556 | if( !*p ) |
---|
557 | done = 1; |
---|
558 | else |
---|
559 | *p = '\0'; |
---|
560 | |
---|
561 | if( stat( path, &sb ) ) |
---|
562 | { |
---|
563 | /* Folder doesn't exist yet */ |
---|
564 | if( tr_mkdir( path, permissions ) ) |
---|
565 | { |
---|
566 | const int err = errno; |
---|
567 | tr_err( _( |
---|
568 | "Couldn't create \"%1$s\": %2$s" ), path, |
---|
569 | tr_strerror( err ) ); |
---|
570 | tr_free( path ); |
---|
571 | errno = err; |
---|
572 | return -1; |
---|
573 | } |
---|
574 | } |
---|
575 | else if( ( sb.st_mode & S_IFMT ) != S_IFDIR ) |
---|
576 | { |
---|
577 | /* Node exists but isn't a folder */ |
---|
578 | char * buf = tr_strdup_printf( _( "File \"%s\" is in the way" ), path ); |
---|
579 | tr_err( _( "Couldn't create \"%1$s\": %2$s" ), path_in, buf ); |
---|
580 | tr_free( buf ); |
---|
581 | tr_free( path ); |
---|
582 | errno = ENOTDIR; |
---|
583 | return -1; |
---|
584 | } |
---|
585 | |
---|
586 | if( done ) |
---|
587 | break; |
---|
588 | |
---|
589 | *p = TR_PATH_DELIMITER; |
---|
590 | p++; |
---|
591 | pp = p; |
---|
592 | } |
---|
593 | |
---|
594 | tr_free( path ); |
---|
595 | return 0; |
---|
596 | } |
---|
597 | |
---|
598 | char* |
---|
599 | tr_buildPath( const char *first_element, ... ) |
---|
600 | { |
---|
601 | size_t bufLen = 0; |
---|
602 | const char * element; |
---|
603 | char * buf; |
---|
604 | char * pch; |
---|
605 | va_list vl; |
---|
606 | |
---|
607 | /* pass 1: allocate enough space for the string */ |
---|
608 | va_start( vl, first_element ); |
---|
609 | element = first_element; |
---|
610 | while( element ) { |
---|
611 | bufLen += strlen( element ) + 1; |
---|
612 | element = va_arg( vl, const char* ); |
---|
613 | } |
---|
614 | pch = buf = tr_new( char, bufLen ); |
---|
615 | va_end( vl ); |
---|
616 | |
---|
617 | /* pass 2: build the string piece by piece */ |
---|
618 | va_start( vl, first_element ); |
---|
619 | element = first_element; |
---|
620 | while( element ) { |
---|
621 | const size_t elementLen = strlen( element ); |
---|
622 | memcpy( pch, element, elementLen ); |
---|
623 | pch += elementLen; |
---|
624 | *pch++ = TR_PATH_DELIMITER; |
---|
625 | element = va_arg( vl, const char* ); |
---|
626 | } |
---|
627 | va_end( vl ); |
---|
628 | |
---|
629 | /* terminate the string. if nonempty, eat the unwanted trailing slash */ |
---|
630 | if( pch != buf ) |
---|
631 | --pch; |
---|
632 | *pch++ = '\0'; |
---|
633 | |
---|
634 | /* sanity checks & return */ |
---|
635 | assert( pch - buf == (off_t)bufLen ); |
---|
636 | return buf; |
---|
637 | } |
---|
638 | |
---|
639 | /**** |
---|
640 | ***** |
---|
641 | ****/ |
---|
642 | |
---|
643 | char* |
---|
644 | evbuffer_free_to_str( struct evbuffer * buf ) |
---|
645 | { |
---|
646 | const size_t n = evbuffer_get_length( buf ); |
---|
647 | char * ret = tr_new( char, n + 1 ); |
---|
648 | evbuffer_copyout( buf, ret, n ); |
---|
649 | evbuffer_free( buf ); |
---|
650 | ret[n] = '\0'; |
---|
651 | return ret; |
---|
652 | } |
---|
653 | |
---|
654 | char* |
---|
655 | tr_strdup( const void * in ) |
---|
656 | { |
---|
657 | return tr_strndup( in, in ? (int)strlen((const char *)in) : 0 ); |
---|
658 | } |
---|
659 | |
---|
660 | char* |
---|
661 | tr_strndup( const void * in, int len ) |
---|
662 | { |
---|
663 | char * out = NULL; |
---|
664 | |
---|
665 | if( len < 0 ) |
---|
666 | { |
---|
667 | out = tr_strdup( in ); |
---|
668 | } |
---|
669 | else if( in ) |
---|
670 | { |
---|
671 | out = tr_malloc( len + 1 ); |
---|
672 | memcpy( out, in, len ); |
---|
673 | out[len] = '\0'; |
---|
674 | } |
---|
675 | |
---|
676 | return out; |
---|
677 | } |
---|
678 | |
---|
679 | const char* |
---|
680 | tr_memmem( const char * haystack, size_t haystacklen, |
---|
681 | const char * needle, size_t needlelen ) |
---|
682 | { |
---|
683 | #ifdef HAVE_MEMMEM |
---|
684 | return memmem( haystack, haystacklen, needle, needlelen ); |
---|
685 | #else |
---|
686 | size_t i; |
---|
687 | if( !needlelen ) |
---|
688 | return haystack; |
---|
689 | if( needlelen > haystacklen || !haystack || !needle ) |
---|
690 | return NULL; |
---|
691 | for( i=0; i<=haystacklen-needlelen; ++i ) |
---|
692 | if( !memcmp( haystack+i, needle, needlelen ) ) |
---|
693 | return haystack+i; |
---|
694 | return NULL; |
---|
695 | #endif |
---|
696 | } |
---|
697 | |
---|
698 | char* |
---|
699 | tr_strdup_printf( const char * fmt, ... ) |
---|
700 | { |
---|
701 | va_list ap; |
---|
702 | char * ret; |
---|
703 | size_t len; |
---|
704 | char statbuf[2048]; |
---|
705 | |
---|
706 | va_start( ap, fmt ); |
---|
707 | len = evutil_vsnprintf( statbuf, sizeof( statbuf ), fmt, ap ); |
---|
708 | va_end( ap ); |
---|
709 | if( len < sizeof( statbuf ) ) |
---|
710 | ret = tr_strndup( statbuf, len ); |
---|
711 | else { |
---|
712 | ret = tr_new( char, len + 1 ); |
---|
713 | va_start( ap, fmt ); |
---|
714 | evutil_vsnprintf( ret, len + 1, fmt, ap ); |
---|
715 | va_end( ap ); |
---|
716 | } |
---|
717 | |
---|
718 | return ret; |
---|
719 | } |
---|
720 | |
---|
721 | const char* |
---|
722 | tr_strerror( int i ) |
---|
723 | { |
---|
724 | const char * ret = strerror( i ); |
---|
725 | |
---|
726 | if( ret == NULL ) |
---|
727 | ret = "Unknown Error"; |
---|
728 | return ret; |
---|
729 | } |
---|
730 | |
---|
731 | int |
---|
732 | tr_strcmp0( const char * str1, const char * str2 ) |
---|
733 | { |
---|
734 | if( str1 && str2 ) return strcmp( str1, str2 ); |
---|
735 | if( str1 ) return 1; |
---|
736 | if( str2 ) return -1; |
---|
737 | return 0; |
---|
738 | } |
---|
739 | |
---|
740 | /**** |
---|
741 | ***** |
---|
742 | ****/ |
---|
743 | |
---|
744 | /* https://bugs.launchpad.net/percona-patches/+bug/526863/+attachment/1160199/+files/solaris_10_fix.patch */ |
---|
745 | char* |
---|
746 | tr_strsep( char ** str, const char * delims ) |
---|
747 | { |
---|
748 | #ifdef HAVE_STRSEP |
---|
749 | return strsep( str, delims ); |
---|
750 | #else |
---|
751 | char *token; |
---|
752 | |
---|
753 | if (*str == NULL) { |
---|
754 | /* No more tokens */ |
---|
755 | return NULL; |
---|
756 | } |
---|
757 | |
---|
758 | token = *str; |
---|
759 | while (**str != '\0') { |
---|
760 | if (strchr(delims, **str) != NULL) { |
---|
761 | **str = '\0'; |
---|
762 | (*str)++; |
---|
763 | return token; |
---|
764 | } |
---|
765 | (*str)++; |
---|
766 | } |
---|
767 | |
---|
768 | /* There is not another token */ |
---|
769 | *str = NULL; |
---|
770 | |
---|
771 | return token; |
---|
772 | #endif |
---|
773 | } |
---|
774 | |
---|
775 | char* |
---|
776 | tr_strstrip( char * str ) |
---|
777 | { |
---|
778 | if( str != NULL ) |
---|
779 | { |
---|
780 | size_t pos; |
---|
781 | size_t len = strlen( str ); |
---|
782 | |
---|
783 | while( len && isspace( str[len - 1] ) ) |
---|
784 | --len; |
---|
785 | |
---|
786 | for( pos = 0; pos < len && isspace( str[pos] ); ) |
---|
787 | ++pos; |
---|
788 | |
---|
789 | len -= pos; |
---|
790 | memmove( str, str + pos, len ); |
---|
791 | str[len] = '\0'; |
---|
792 | } |
---|
793 | |
---|
794 | return str; |
---|
795 | } |
---|
796 | |
---|
797 | bool |
---|
798 | tr_str_has_suffix( const char *str, const char *suffix ) |
---|
799 | { |
---|
800 | size_t str_len; |
---|
801 | size_t suffix_len; |
---|
802 | |
---|
803 | if( !str ) |
---|
804 | return false; |
---|
805 | if( !suffix ) |
---|
806 | return true; |
---|
807 | |
---|
808 | str_len = strlen( str ); |
---|
809 | suffix_len = strlen( suffix ); |
---|
810 | if( str_len < suffix_len ) |
---|
811 | return false; |
---|
812 | |
---|
813 | return !evutil_ascii_strncasecmp( str + str_len - suffix_len, suffix, suffix_len ); |
---|
814 | } |
---|
815 | |
---|
816 | /**** |
---|
817 | ***** |
---|
818 | ****/ |
---|
819 | |
---|
820 | uint64_t |
---|
821 | tr_time_msec( void ) |
---|
822 | { |
---|
823 | struct timeval tv; |
---|
824 | |
---|
825 | gettimeofday( &tv, NULL ); |
---|
826 | return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 ); |
---|
827 | } |
---|
828 | |
---|
829 | void |
---|
830 | tr_wait_msec( long int msec ) |
---|
831 | { |
---|
832 | #ifdef WIN32 |
---|
833 | Sleep( (DWORD)msec ); |
---|
834 | #else |
---|
835 | struct timespec ts; |
---|
836 | ts.tv_sec = msec / 1000; |
---|
837 | ts.tv_nsec = ( msec % 1000 ) * 1000000; |
---|
838 | nanosleep( &ts, NULL ); |
---|
839 | #endif |
---|
840 | } |
---|
841 | |
---|
842 | /*** |
---|
843 | **** |
---|
844 | ***/ |
---|
845 | |
---|
846 | int |
---|
847 | tr_snprintf( char * buf, size_t buflen, const char * fmt, ... ) |
---|
848 | { |
---|
849 | int len; |
---|
850 | va_list args; |
---|
851 | |
---|
852 | va_start( args, fmt ); |
---|
853 | len = evutil_vsnprintf( buf, buflen, fmt, args ); |
---|
854 | va_end( args ); |
---|
855 | return len; |
---|
856 | } |
---|
857 | |
---|
858 | /* |
---|
859 | * Copy src to string dst of size siz. At most siz-1 characters |
---|
860 | * will be copied. Always NUL terminates (unless siz == 0). |
---|
861 | * Returns strlen(src); if retval >= siz, truncation occurred. |
---|
862 | */ |
---|
863 | size_t |
---|
864 | tr_strlcpy( char * dst, const void * src, size_t siz ) |
---|
865 | { |
---|
866 | #ifdef HAVE_STRLCPY |
---|
867 | return strlcpy( dst, src, siz ); |
---|
868 | #else |
---|
869 | char * d = dst; |
---|
870 | const char *s = src; |
---|
871 | size_t n = siz; |
---|
872 | |
---|
873 | assert( s ); |
---|
874 | assert( d ); |
---|
875 | |
---|
876 | /* Copy as many bytes as will fit */ |
---|
877 | if( n != 0 ) |
---|
878 | { |
---|
879 | while( --n != 0 ) |
---|
880 | { |
---|
881 | if( ( *d++ = *s++ ) == '\0' ) |
---|
882 | break; |
---|
883 | } |
---|
884 | } |
---|
885 | |
---|
886 | /* Not enough room in dst, add NUL and traverse rest of src */ |
---|
887 | if( n == 0 ) |
---|
888 | { |
---|
889 | if( siz != 0 ) |
---|
890 | *d = '\0'; /* NUL-terminate dst */ |
---|
891 | while( *s++ ) |
---|
892 | ; |
---|
893 | } |
---|
894 | |
---|
895 | return s - (char*)src - 1; /* count does not include NUL */ |
---|
896 | #endif |
---|
897 | } |
---|
898 | |
---|
899 | /*** |
---|
900 | **** |
---|
901 | ***/ |
---|
902 | |
---|
903 | double |
---|
904 | tr_getRatio( uint64_t numerator, uint64_t denominator ) |
---|
905 | { |
---|
906 | double ratio; |
---|
907 | |
---|
908 | if( denominator > 0 ) |
---|
909 | ratio = numerator / (double)denominator; |
---|
910 | else if( numerator > 0 ) |
---|
911 | ratio = TR_RATIO_INF; |
---|
912 | else |
---|
913 | ratio = TR_RATIO_NA; |
---|
914 | |
---|
915 | return ratio; |
---|
916 | } |
---|
917 | |
---|
918 | void |
---|
919 | tr_sha1_to_hex( char * out, const uint8_t * sha1 ) |
---|
920 | { |
---|
921 | int i; |
---|
922 | static const char hex[] = "0123456789abcdef"; |
---|
923 | |
---|
924 | for( i=0; i<20; ++i ) |
---|
925 | { |
---|
926 | const unsigned int val = *sha1++; |
---|
927 | *out++ = hex[val >> 4]; |
---|
928 | *out++ = hex[val & 0xf]; |
---|
929 | } |
---|
930 | |
---|
931 | *out = '\0'; |
---|
932 | } |
---|
933 | |
---|
934 | void |
---|
935 | tr_hex_to_sha1( uint8_t * out, const char * in ) |
---|
936 | { |
---|
937 | int i; |
---|
938 | static const char hex[] = "0123456789abcdef"; |
---|
939 | |
---|
940 | for( i=0; i<20; ++i ) |
---|
941 | { |
---|
942 | const int hi = strchr( hex, tolower( *in++ ) ) - hex; |
---|
943 | const int lo = strchr( hex, tolower( *in++ ) ) - hex; |
---|
944 | *out++ = (uint8_t)( (hi<<4) | lo ); |
---|
945 | } |
---|
946 | } |
---|
947 | |
---|
948 | /*** |
---|
949 | **** |
---|
950 | ***/ |
---|
951 | |
---|
952 | static bool |
---|
953 | isValidURLChars( const char * url, int url_len ) |
---|
954 | { |
---|
955 | const char * c; |
---|
956 | const char * end; |
---|
957 | static const char * rfc2396_valid_chars = |
---|
958 | "abcdefghijklmnopqrstuvwxyz" /* lowalpha */ |
---|
959 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* upalpha */ |
---|
960 | "0123456789" /* digit */ |
---|
961 | "-_.!~*'()" /* mark */ |
---|
962 | ";/?:@&=+$," /* reserved */ |
---|
963 | "<>#%<\"" /* delims */ |
---|
964 | "{}|\\^[]`"; /* unwise */ |
---|
965 | |
---|
966 | if( url == NULL ) |
---|
967 | return false; |
---|
968 | |
---|
969 | for( c=url, end=c+url_len; c && *c && c!=end; ++c ) |
---|
970 | if( !strchr( rfc2396_valid_chars, *c ) ) |
---|
971 | return false; |
---|
972 | |
---|
973 | return true; |
---|
974 | } |
---|
975 | |
---|
976 | /** @brief return true if the URL is a http or https or UDP one that Transmission understands */ |
---|
977 | bool |
---|
978 | tr_urlIsValidTracker( const char * url ) |
---|
979 | { |
---|
980 | bool valid; |
---|
981 | |
---|
982 | if( url == NULL ) |
---|
983 | { |
---|
984 | valid = false; |
---|
985 | } |
---|
986 | else |
---|
987 | { |
---|
988 | const int len = strlen( url ); |
---|
989 | |
---|
990 | valid = isValidURLChars( url, len ) |
---|
991 | && !tr_urlParse( url, len, NULL, NULL, NULL, NULL ) |
---|
992 | && ( !memcmp(url,"http://",7) || !memcmp(url,"https://",8) || !memcmp(url,"udp://",6) ); |
---|
993 | } |
---|
994 | |
---|
995 | return valid; |
---|
996 | } |
---|
997 | |
---|
998 | /** @brief return true if the URL is a http or https or ftp or sftp one that Transmission understands */ |
---|
999 | bool |
---|
1000 | tr_urlIsValid( const char * url, int url_len ) |
---|
1001 | { |
---|
1002 | bool valid; |
---|
1003 | |
---|
1004 | if( url == NULL ) |
---|
1005 | { |
---|
1006 | valid = false; |
---|
1007 | } |
---|
1008 | else |
---|
1009 | { |
---|
1010 | if( url_len < 0 ) |
---|
1011 | url_len = strlen( url ); |
---|
1012 | |
---|
1013 | valid = isValidURLChars( url, url_len ) |
---|
1014 | && !tr_urlParse( url, url_len, NULL, NULL, NULL, NULL ) |
---|
1015 | && ( !memcmp(url,"http://",7) || !memcmp(url,"https://",8) || !memcmp(url,"ftp://",6) || !memcmp(url,"sftp://",7) ); |
---|
1016 | } |
---|
1017 | |
---|
1018 | return valid; |
---|
1019 | } |
---|
1020 | |
---|
1021 | bool |
---|
1022 | tr_addressIsIP( const char * str ) |
---|
1023 | { |
---|
1024 | tr_address tmp; |
---|
1025 | return tr_address_from_string( &tmp, str ); |
---|
1026 | } |
---|
1027 | |
---|
1028 | int |
---|
1029 | tr_urlParse( const char * url_in, |
---|
1030 | int len, |
---|
1031 | char ** setme_protocol, |
---|
1032 | char ** setme_host, |
---|
1033 | int * setme_port, |
---|
1034 | char ** setme_path ) |
---|
1035 | { |
---|
1036 | int err; |
---|
1037 | int port = 0; |
---|
1038 | int n; |
---|
1039 | char * tmp; |
---|
1040 | char * pch; |
---|
1041 | size_t host_len; |
---|
1042 | size_t protocol_len; |
---|
1043 | const char * host = NULL; |
---|
1044 | const char * protocol = NULL; |
---|
1045 | const char * path = NULL; |
---|
1046 | |
---|
1047 | tmp = tr_strndup( url_in, len ); |
---|
1048 | if( ( pch = strstr( tmp, "://" ) ) ) |
---|
1049 | { |
---|
1050 | *pch = '\0'; |
---|
1051 | protocol = tmp; |
---|
1052 | protocol_len = pch - protocol; |
---|
1053 | pch += 3; |
---|
1054 | /*fprintf( stderr, "protocol is [%s]... what's left is [%s]\n", protocol, pch);*/ |
---|
1055 | if( ( n = strcspn( pch, ":/" ) ) ) |
---|
1056 | { |
---|
1057 | const int havePort = pch[n] == ':'; |
---|
1058 | host = pch; |
---|
1059 | host_len = n; |
---|
1060 | pch += n; |
---|
1061 | if( pch && *pch ) |
---|
1062 | *pch++ = '\0'; |
---|
1063 | /*fprintf( stderr, "host is [%s]... what's left is [%s]\n", host, pch );*/ |
---|
1064 | if( havePort ) |
---|
1065 | { |
---|
1066 | char * end; |
---|
1067 | port = strtol( pch, &end, 10 ); |
---|
1068 | pch = end; |
---|
1069 | /*fprintf( stderr, "port is [%d]... what's left is [%s]\n", port, pch );*/ |
---|
1070 | } |
---|
1071 | path = pch; |
---|
1072 | /*fprintf( stderr, "path is [%s]\n", path );*/ |
---|
1073 | } |
---|
1074 | } |
---|
1075 | |
---|
1076 | err = !host || !path || !protocol; |
---|
1077 | |
---|
1078 | if( !err && !port ) |
---|
1079 | { |
---|
1080 | if( !strcmp( protocol, "udp" ) ) port = 80; |
---|
1081 | else if( !strcmp( protocol, "ftp" ) ) port = 21; |
---|
1082 | else if( !strcmp( protocol, "sftp" ) ) port = 22; |
---|
1083 | else if( !strcmp( protocol, "http" ) ) port = 80; |
---|
1084 | else if( !strcmp( protocol, "https" ) ) port = 443; |
---|
1085 | } |
---|
1086 | |
---|
1087 | if( !err ) |
---|
1088 | { |
---|
1089 | if( setme_protocol ) *setme_protocol = tr_strndup( protocol, protocol_len ); |
---|
1090 | |
---|
1091 | if( setme_host ){ ( (char*)host )[-3] = ':'; *setme_host = |
---|
1092 | tr_strndup( host, host_len ); } |
---|
1093 | if( setme_path ){ if( !*path ) *setme_path = tr_strdup( "/" ); |
---|
1094 | else if( path[0] == '/' ) *setme_path = tr_strdup( path ); |
---|
1095 | else { ( (char*)path )[-1] = '/'; *setme_path = tr_strdup( path - 1 ); } } |
---|
1096 | if( setme_port ) *setme_port = port; |
---|
1097 | } |
---|
1098 | |
---|
1099 | |
---|
1100 | tr_free( tmp ); |
---|
1101 | return err; |
---|
1102 | } |
---|
1103 | |
---|
1104 | #include <string.h> |
---|
1105 | #include <openssl/sha.h> |
---|
1106 | #include <openssl/hmac.h> |
---|
1107 | #include <openssl/evp.h> |
---|
1108 | #include <openssl/bio.h> |
---|
1109 | #include <openssl/buffer.h> |
---|
1110 | |
---|
1111 | char * |
---|
1112 | tr_base64_encode( const void * input, int length, int * setme_len ) |
---|
1113 | { |
---|
1114 | int retlen = 0; |
---|
1115 | char * ret = NULL; |
---|
1116 | |
---|
1117 | if( input != NULL ) |
---|
1118 | { |
---|
1119 | BIO * b64; |
---|
1120 | BIO * bmem; |
---|
1121 | BUF_MEM * bptr; |
---|
1122 | |
---|
1123 | if( length < 1 ) |
---|
1124 | length = (int)strlen( input ); |
---|
1125 | |
---|
1126 | bmem = BIO_new( BIO_s_mem( ) ); |
---|
1127 | b64 = BIO_new( BIO_f_base64( ) ); |
---|
1128 | BIO_set_flags( b64, BIO_FLAGS_BASE64_NO_NL ); |
---|
1129 | b64 = BIO_push( b64, bmem ); |
---|
1130 | BIO_write( b64, input, length ); |
---|
1131 | (void) BIO_flush( b64 ); |
---|
1132 | BIO_get_mem_ptr( b64, &bptr ); |
---|
1133 | ret = tr_strndup( bptr->data, bptr->length ); |
---|
1134 | retlen = bptr->length; |
---|
1135 | BIO_free_all( b64 ); |
---|
1136 | } |
---|
1137 | |
---|
1138 | if( setme_len ) |
---|
1139 | *setme_len = retlen; |
---|
1140 | |
---|
1141 | return ret; |
---|
1142 | } |
---|
1143 | |
---|
1144 | char * |
---|
1145 | tr_base64_decode( const void * input, |
---|
1146 | int length, |
---|
1147 | int * setme_len ) |
---|
1148 | { |
---|
1149 | char * ret; |
---|
1150 | BIO * b64; |
---|
1151 | BIO * bmem; |
---|
1152 | int retlen; |
---|
1153 | |
---|
1154 | if( length < 1 ) |
---|
1155 | length = strlen( input ); |
---|
1156 | |
---|
1157 | ret = tr_new0( char, length ); |
---|
1158 | b64 = BIO_new( BIO_f_base64( ) ); |
---|
1159 | bmem = BIO_new_mem_buf( (unsigned char*)input, length ); |
---|
1160 | bmem = BIO_push( b64, bmem ); |
---|
1161 | retlen = BIO_read( bmem, ret, length ); |
---|
1162 | if( !retlen ) |
---|
1163 | { |
---|
1164 | /* try again, but with the BIO_FLAGS_BASE64_NO_NL flag */ |
---|
1165 | BIO_free_all( bmem ); |
---|
1166 | b64 = BIO_new( BIO_f_base64( ) ); |
---|
1167 | BIO_set_flags( b64, BIO_FLAGS_BASE64_NO_NL ); |
---|
1168 | bmem = BIO_new_mem_buf( (unsigned char*)input, length ); |
---|
1169 | bmem = BIO_push( b64, bmem ); |
---|
1170 | retlen = BIO_read( bmem, ret, length ); |
---|
1171 | } |
---|
1172 | |
---|
1173 | if( setme_len ) |
---|
1174 | *setme_len = retlen; |
---|
1175 | |
---|
1176 | BIO_free_all( bmem ); |
---|
1177 | return ret; |
---|
1178 | } |
---|
1179 | |
---|
1180 | /*** |
---|
1181 | **** |
---|
1182 | ***/ |
---|
1183 | |
---|
1184 | void |
---|
1185 | tr_removeElementFromArray( void * array, |
---|
1186 | unsigned int index_to_remove, |
---|
1187 | size_t sizeof_element, |
---|
1188 | size_t nmemb ) |
---|
1189 | { |
---|
1190 | char * a = array; |
---|
1191 | |
---|
1192 | memmove( a + sizeof_element * index_to_remove, |
---|
1193 | a + sizeof_element * ( index_to_remove + 1 ), |
---|
1194 | sizeof_element * ( --nmemb - index_to_remove ) ); |
---|
1195 | } |
---|
1196 | |
---|
1197 | int |
---|
1198 | tr_lowerBound( const void * key, |
---|
1199 | const void * base, |
---|
1200 | size_t nmemb, |
---|
1201 | size_t size, |
---|
1202 | int (* compar)(const void* key, const void* arrayMember), |
---|
1203 | bool * exact_match ) |
---|
1204 | { |
---|
1205 | size_t first = 0; |
---|
1206 | const char * cbase = base; |
---|
1207 | bool exact = false; |
---|
1208 | |
---|
1209 | while( nmemb != 0 ) |
---|
1210 | { |
---|
1211 | const size_t half = nmemb / 2; |
---|
1212 | const size_t middle = first + half; |
---|
1213 | const int c = compar( key, cbase + size*middle ); |
---|
1214 | |
---|
1215 | if( c <= 0 ) { |
---|
1216 | if( c == 0 ) |
---|
1217 | exact = true; |
---|
1218 | nmemb = half; |
---|
1219 | } else { |
---|
1220 | first = middle + 1; |
---|
1221 | nmemb = nmemb - half - 1; |
---|
1222 | } |
---|
1223 | } |
---|
1224 | |
---|
1225 | *exact_match = exact; |
---|
1226 | |
---|
1227 | return first; |
---|
1228 | } |
---|
1229 | |
---|
1230 | /*** |
---|
1231 | **** |
---|
1232 | ***/ |
---|
1233 | |
---|
1234 | static char* |
---|
1235 | strip_non_utf8( const char * in, size_t inlen ) |
---|
1236 | { |
---|
1237 | const char * end; |
---|
1238 | const char zero = '\0'; |
---|
1239 | struct evbuffer * buf = evbuffer_new( ); |
---|
1240 | |
---|
1241 | while( !tr_utf8_validate( in, inlen, &end ) ) |
---|
1242 | { |
---|
1243 | const int good_len = end - in; |
---|
1244 | |
---|
1245 | evbuffer_add( buf, in, good_len ); |
---|
1246 | inlen -= ( good_len + 1 ); |
---|
1247 | in += ( good_len + 1 ); |
---|
1248 | evbuffer_add( buf, "?", 1 ); |
---|
1249 | } |
---|
1250 | |
---|
1251 | evbuffer_add( buf, in, inlen ); |
---|
1252 | evbuffer_add( buf, &zero, 1 ); |
---|
1253 | return evbuffer_free_to_str( buf ); |
---|
1254 | } |
---|
1255 | |
---|
1256 | static char* |
---|
1257 | to_utf8( const char * in, size_t inlen ) |
---|
1258 | { |
---|
1259 | char * ret = NULL; |
---|
1260 | |
---|
1261 | #ifdef HAVE_ICONV_OPEN |
---|
1262 | int i; |
---|
1263 | const char * encodings[] = { "CURRENT", "ISO-8859-15" }; |
---|
1264 | const int encoding_count = sizeof(encodings) / sizeof(encodings[1]); |
---|
1265 | const size_t buflen = inlen*4 + 10; |
---|
1266 | char * out = tr_new( char, buflen ); |
---|
1267 | |
---|
1268 | for( i=0; !ret && i<encoding_count; ++i ) |
---|
1269 | { |
---|
1270 | char * inbuf = (char*) in; |
---|
1271 | char * outbuf = out; |
---|
1272 | size_t inbytesleft = inlen; |
---|
1273 | size_t outbytesleft = buflen; |
---|
1274 | const char * test_encoding = encodings[i]; |
---|
1275 | |
---|
1276 | iconv_t cd = iconv_open( "UTF-8", test_encoding ); |
---|
1277 | if( cd != (iconv_t)-1 ) { |
---|
1278 | if( iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ) != (size_t)-1 ) |
---|
1279 | ret = tr_strndup( out, buflen-outbytesleft ); |
---|
1280 | iconv_close( cd ); |
---|
1281 | } |
---|
1282 | } |
---|
1283 | |
---|
1284 | tr_free( out ); |
---|
1285 | #endif |
---|
1286 | |
---|
1287 | if( ret == NULL ) |
---|
1288 | ret = strip_non_utf8( in, inlen ); |
---|
1289 | |
---|
1290 | return ret; |
---|
1291 | } |
---|
1292 | |
---|
1293 | char* |
---|
1294 | tr_utf8clean( const char * str, int max_len ) |
---|
1295 | { |
---|
1296 | char * ret; |
---|
1297 | const char * end; |
---|
1298 | |
---|
1299 | if( max_len < 0 ) |
---|
1300 | max_len = (int) strlen( str ); |
---|
1301 | |
---|
1302 | if( tr_utf8_validate( str, max_len, &end ) ) |
---|
1303 | ret = tr_strndup( str, max_len ); |
---|
1304 | else |
---|
1305 | ret = to_utf8( str, max_len ); |
---|
1306 | |
---|
1307 | assert( tr_utf8_validate( ret, -1, NULL ) ); |
---|
1308 | return ret; |
---|
1309 | } |
---|
1310 | |
---|
1311 | /*** |
---|
1312 | **** |
---|
1313 | ***/ |
---|
1314 | |
---|
1315 | struct number_range |
---|
1316 | { |
---|
1317 | int low; |
---|
1318 | int high; |
---|
1319 | }; |
---|
1320 | |
---|
1321 | /** |
---|
1322 | * This should be a single number (ex. "6") or a range (ex. "6-9"). |
---|
1323 | * Anything else is an error and will return failure. |
---|
1324 | */ |
---|
1325 | static bool |
---|
1326 | parseNumberSection( const char * str, int len, struct number_range * setme ) |
---|
1327 | { |
---|
1328 | long a, b; |
---|
1329 | bool success; |
---|
1330 | char * end; |
---|
1331 | const int error = errno; |
---|
1332 | char * tmp = tr_strndup( str, len ); |
---|
1333 | |
---|
1334 | errno = 0; |
---|
1335 | a = b = strtol( tmp, &end, 10 ); |
---|
1336 | if( errno || ( end == tmp ) ) { |
---|
1337 | success = false; |
---|
1338 | } else if( *end != '-' ) { |
---|
1339 | success = true; |
---|
1340 | } else { |
---|
1341 | const char * pch = end + 1; |
---|
1342 | b = strtol( pch, &end, 10 ); |
---|
1343 | if( errno || ( pch == end ) ) |
---|
1344 | success = false; |
---|
1345 | else if( *end ) /* trailing data */ |
---|
1346 | success = false; |
---|
1347 | else |
---|
1348 | success = true; |
---|
1349 | } |
---|
1350 | tr_free( tmp ); |
---|
1351 | |
---|
1352 | setme->low = MIN( a, b ); |
---|
1353 | setme->high = MAX( a, b ); |
---|
1354 | |
---|
1355 | errno = error; |
---|
1356 | return success; |
---|
1357 | } |
---|
1358 | |
---|
1359 | int |
---|
1360 | compareInt( const void * va, const void * vb ) |
---|
1361 | { |
---|
1362 | const int a = *(const int *)va; |
---|
1363 | const int b = *(const int *)vb; |
---|
1364 | return a - b; |
---|
1365 | } |
---|
1366 | |
---|
1367 | /** |
---|
1368 | * Given a string like "1-4" or "1-4,6,9,14-51", this allocates and returns an |
---|
1369 | * array of setmeCount ints of all the values in the array. |
---|
1370 | * For example, "5-8" will return [ 5, 6, 7, 8 ] and setmeCount will be 4. |
---|
1371 | * It's the caller's responsibility to call tr_free() on the returned array. |
---|
1372 | * If a fragment of the string can't be parsed, NULL is returned. |
---|
1373 | */ |
---|
1374 | int* |
---|
1375 | tr_parseNumberRange( const char * str_in, int len, int * setmeCount ) |
---|
1376 | { |
---|
1377 | int n = 0; |
---|
1378 | int * uniq = NULL; |
---|
1379 | char * str = tr_strndup( str_in, len ); |
---|
1380 | const char * walk; |
---|
1381 | tr_list * ranges = NULL; |
---|
1382 | bool success = true; |
---|
1383 | |
---|
1384 | walk = str; |
---|
1385 | while( walk && *walk && success ) { |
---|
1386 | struct number_range range; |
---|
1387 | const char * pch = strchr( walk, ',' ); |
---|
1388 | if( pch ) { |
---|
1389 | success = parseNumberSection( walk, pch-walk, &range ); |
---|
1390 | walk = pch + 1; |
---|
1391 | } else { |
---|
1392 | success = parseNumberSection( walk, strlen( walk ), &range ); |
---|
1393 | walk += strlen( walk ); |
---|
1394 | } |
---|
1395 | if( success ) |
---|
1396 | tr_list_append( &ranges, tr_memdup( &range, sizeof( struct number_range ) ) ); |
---|
1397 | } |
---|
1398 | |
---|
1399 | if( !success ) |
---|
1400 | { |
---|
1401 | *setmeCount = 0; |
---|
1402 | uniq = NULL; |
---|
1403 | } |
---|
1404 | else |
---|
1405 | { |
---|
1406 | int i; |
---|
1407 | int n2; |
---|
1408 | tr_list * l; |
---|
1409 | int * sorted = NULL; |
---|
1410 | |
---|
1411 | /* build a sorted number array */ |
---|
1412 | n = n2 = 0; |
---|
1413 | for( l=ranges; l!=NULL; l=l->next ) { |
---|
1414 | const struct number_range * r = l->data; |
---|
1415 | n += r->high + 1 - r->low; |
---|
1416 | } |
---|
1417 | sorted = tr_new( int, n ); |
---|
1418 | for( l=ranges; l!=NULL; l=l->next ) { |
---|
1419 | const struct number_range * r = l->data; |
---|
1420 | int i; |
---|
1421 | for( i=r->low; i<=r->high; ++i ) |
---|
1422 | sorted[n2++] = i; |
---|
1423 | } |
---|
1424 | qsort( sorted, n, sizeof( int ), compareInt ); |
---|
1425 | assert( n == n2 ); |
---|
1426 | |
---|
1427 | /* remove duplicates */ |
---|
1428 | uniq = tr_new( int, n ); |
---|
1429 | for( i=n=0; i<n2; ++i ) |
---|
1430 | if( !n || uniq[n-1] != sorted[i] ) |
---|
1431 | uniq[n++] = sorted[i]; |
---|
1432 | |
---|
1433 | tr_free( sorted ); |
---|
1434 | } |
---|
1435 | |
---|
1436 | /* cleanup */ |
---|
1437 | tr_list_free( &ranges, tr_free ); |
---|
1438 | tr_free( str ); |
---|
1439 | |
---|
1440 | /* return the result */ |
---|
1441 | *setmeCount = n; |
---|
1442 | return uniq; |
---|
1443 | } |
---|
1444 | |
---|
1445 | /*** |
---|
1446 | **** |
---|
1447 | ***/ |
---|
1448 | |
---|
1449 | double |
---|
1450 | tr_truncd( double x, int precision ) |
---|
1451 | { |
---|
1452 | char * pt; |
---|
1453 | char buf[128]; |
---|
1454 | const int max_precision = (int) log10( 1.0 / DBL_EPSILON ) - 1; |
---|
1455 | tr_snprintf( buf, sizeof( buf ), "%.*f", max_precision, x ); |
---|
1456 | if(( pt = strstr( buf, localeconv()->decimal_point ))) |
---|
1457 | pt[precision ? precision+1 : 0] = '\0'; |
---|
1458 | return atof(buf); |
---|
1459 | } |
---|
1460 | |
---|
1461 | /* return a truncated double as a string */ |
---|
1462 | static char* |
---|
1463 | tr_strtruncd( char * buf, double x, int precision, size_t buflen ) |
---|
1464 | { |
---|
1465 | tr_snprintf( buf, buflen, "%.*f", precision, tr_truncd( x, precision ) ); |
---|
1466 | return buf; |
---|
1467 | } |
---|
1468 | |
---|
1469 | char* |
---|
1470 | tr_strpercent( char * buf, double x, size_t buflen ) |
---|
1471 | { |
---|
1472 | if( x < 10.0 ) |
---|
1473 | tr_strtruncd( buf, x, 2, buflen ); |
---|
1474 | else if( x < 100.0 ) |
---|
1475 | tr_strtruncd( buf, x, 1, buflen ); |
---|
1476 | else |
---|
1477 | tr_strtruncd( buf, x, 0, buflen ); |
---|
1478 | return buf; |
---|
1479 | } |
---|
1480 | |
---|
1481 | char* |
---|
1482 | tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity ) |
---|
1483 | { |
---|
1484 | if( (int)ratio == TR_RATIO_NA ) |
---|
1485 | tr_strlcpy( buf, _( "None" ), buflen ); |
---|
1486 | else if( (int)ratio == TR_RATIO_INF ) |
---|
1487 | tr_strlcpy( buf, infinity, buflen ); |
---|
1488 | else |
---|
1489 | tr_strpercent( buf, ratio, buflen ); |
---|
1490 | return buf; |
---|
1491 | } |
---|
1492 | |
---|
1493 | /*** |
---|
1494 | **** |
---|
1495 | ***/ |
---|
1496 | |
---|
1497 | int |
---|
1498 | tr_moveFile( const char * oldpath, const char * newpath, bool * renamed ) |
---|
1499 | { |
---|
1500 | int in; |
---|
1501 | int out; |
---|
1502 | char * buf; |
---|
1503 | struct stat st; |
---|
1504 | off_t bytesLeft; |
---|
1505 | const size_t buflen = 1024 * 128; /* 128 KiB buffer */ |
---|
1506 | |
---|
1507 | /* make sure the old file exists */ |
---|
1508 | if( stat( oldpath, &st ) ) { |
---|
1509 | const int err = errno; |
---|
1510 | errno = err; |
---|
1511 | return -1; |
---|
1512 | } |
---|
1513 | if( !S_ISREG( st.st_mode ) ) { |
---|
1514 | errno = ENOENT; |
---|
1515 | return -1; |
---|
1516 | } |
---|
1517 | bytesLeft = st.st_size; |
---|
1518 | |
---|
1519 | /* make sure the target directory exists */ |
---|
1520 | { |
---|
1521 | char * newdir = tr_dirname( newpath ); |
---|
1522 | int i = tr_mkdirp( newdir, 0777 ); |
---|
1523 | tr_free( newdir ); |
---|
1524 | if( i ) |
---|
1525 | return i; |
---|
1526 | } |
---|
1527 | |
---|
1528 | /* they might be on the same filesystem... */ |
---|
1529 | { |
---|
1530 | const int i = rename( oldpath, newpath ); |
---|
1531 | if( renamed != NULL ) |
---|
1532 | *renamed = i == 0; |
---|
1533 | if( !i ) |
---|
1534 | return 0; |
---|
1535 | } |
---|
1536 | |
---|
1537 | /* copy the file */ |
---|
1538 | in = tr_open_file_for_scanning( oldpath ); |
---|
1539 | out = tr_open_file_for_writing( newpath ); |
---|
1540 | buf = tr_valloc( buflen ); |
---|
1541 | while( bytesLeft > 0 ) |
---|
1542 | { |
---|
1543 | ssize_t bytesWritten; |
---|
1544 | const off_t bytesThisPass = MIN( bytesLeft, (off_t)buflen ); |
---|
1545 | const int numRead = read( in, buf, bytesThisPass ); |
---|
1546 | if( numRead < 0 ) |
---|
1547 | break; |
---|
1548 | bytesWritten = write( out, buf, numRead ); |
---|
1549 | if( bytesWritten < 0 ) |
---|
1550 | break; |
---|
1551 | bytesLeft -= bytesWritten; |
---|
1552 | } |
---|
1553 | |
---|
1554 | /* cleanup */ |
---|
1555 | tr_free( buf ); |
---|
1556 | tr_close_file( out ); |
---|
1557 | tr_close_file( in ); |
---|
1558 | if( bytesLeft != 0 ) |
---|
1559 | return -1; |
---|
1560 | |
---|
1561 | unlink( oldpath ); |
---|
1562 | return 0; |
---|
1563 | } |
---|
1564 | |
---|
1565 | bool |
---|
1566 | tr_is_same_file( const char * filename1, const char * filename2 ) |
---|
1567 | { |
---|
1568 | struct stat sb1, sb2; |
---|
1569 | |
---|
1570 | return !stat( filename1, &sb1 ) |
---|
1571 | && !stat( filename2, &sb2 ) |
---|
1572 | && ( sb1.st_dev == sb2.st_dev ) |
---|
1573 | && ( sb1.st_ino == sb2.st_ino ); |
---|
1574 | } |
---|
1575 | |
---|
1576 | /*** |
---|
1577 | **** |
---|
1578 | ***/ |
---|
1579 | |
---|
1580 | void* |
---|
1581 | tr_valloc( size_t bufLen ) |
---|
1582 | { |
---|
1583 | size_t allocLen; |
---|
1584 | void * buf = NULL; |
---|
1585 | static size_t pageSize = 0; |
---|
1586 | |
---|
1587 | if( !pageSize ) { |
---|
1588 | #ifdef HAVE_GETPAGESIZE |
---|
1589 | pageSize = (size_t) getpagesize(); |
---|
1590 | #else /* guess */ |
---|
1591 | pageSize = 4096; |
---|
1592 | #endif |
---|
1593 | } |
---|
1594 | |
---|
1595 | allocLen = pageSize; |
---|
1596 | while( allocLen < bufLen ) |
---|
1597 | allocLen += pageSize; |
---|
1598 | |
---|
1599 | #ifdef HAVE_POSIX_MEMALIGN |
---|
1600 | if( !buf ) |
---|
1601 | if( posix_memalign( &buf, pageSize, allocLen ) ) |
---|
1602 | buf = NULL; /* just retry with valloc/malloc */ |
---|
1603 | #endif |
---|
1604 | #ifdef HAVE_VALLOC |
---|
1605 | if( !buf ) |
---|
1606 | buf = valloc( allocLen ); |
---|
1607 | #endif |
---|
1608 | if( !buf ) |
---|
1609 | buf = tr_malloc( allocLen ); |
---|
1610 | |
---|
1611 | return buf; |
---|
1612 | } |
---|
1613 | |
---|
1614 | char * |
---|
1615 | tr_realpath( const char * path, char * resolved_path ) |
---|
1616 | { |
---|
1617 | #ifdef WIN32 |
---|
1618 | /* From a message to the Mingw-msys list, Jun 2, 2005 by Mark Junker. */ |
---|
1619 | if( GetFullPathNameA( path, TR_PATH_MAX, resolved_path, NULL ) == 0 ) |
---|
1620 | return NULL; |
---|
1621 | return resolved_path; |
---|
1622 | #else |
---|
1623 | return realpath( path, resolved_path ); |
---|
1624 | #endif |
---|
1625 | } |
---|
1626 | |
---|
1627 | /*** |
---|
1628 | **** |
---|
1629 | ***/ |
---|
1630 | |
---|
1631 | uint64_t |
---|
1632 | tr_htonll( uint64_t x ) |
---|
1633 | { |
---|
1634 | #ifdef HAVE_HTONLL |
---|
1635 | return htonll( x ); |
---|
1636 | #else |
---|
1637 | /* fallback code by bdonlan at |
---|
1638 | * http://stackoverflow.com/questions/809902/64-bit-ntohl-in-c/875505#875505 */ |
---|
1639 | union { uint32_t lx[2]; uint64_t llx; } u; |
---|
1640 | u.lx[0] = htonl(x >> 32); |
---|
1641 | u.lx[1] = htonl(x & 0xFFFFFFFFULL); |
---|
1642 | return u.llx; |
---|
1643 | #endif |
---|
1644 | } |
---|
1645 | |
---|
1646 | uint64_t |
---|
1647 | tr_ntohll( uint64_t x ) |
---|
1648 | { |
---|
1649 | #ifdef HAVE_NTOHLL |
---|
1650 | return ntohll( x ); |
---|
1651 | #else |
---|
1652 | /* fallback code by bdonlan at |
---|
1653 | * http://stackoverflow.com/questions/809902/64-bit-ntohl-in-c/875505#875505 */ |
---|
1654 | union { uint32_t lx[2]; uint64_t llx; } u; |
---|
1655 | u.llx = x; |
---|
1656 | return ((uint64_t)ntohl(u.lx[0]) << 32) | (uint64_t)ntohl(u.lx[1]); |
---|
1657 | #endif |
---|
1658 | } |
---|
1659 | |
---|
1660 | /*** |
---|
1661 | **** |
---|
1662 | **** |
---|
1663 | **** |
---|
1664 | ***/ |
---|
1665 | |
---|
1666 | struct formatter_unit |
---|
1667 | { |
---|
1668 | char * name; |
---|
1669 | int64_t value; |
---|
1670 | }; |
---|
1671 | |
---|
1672 | struct formatter_units |
---|
1673 | { |
---|
1674 | struct formatter_unit units[4]; |
---|
1675 | }; |
---|
1676 | |
---|
1677 | enum { TR_FMT_KB, TR_FMT_MB, TR_FMT_GB, TR_FMT_TB }; |
---|
1678 | |
---|
1679 | static void |
---|
1680 | formatter_init( struct formatter_units * units, |
---|
1681 | unsigned int kilo, |
---|
1682 | const char * kb, const char * mb, |
---|
1683 | const char * gb, const char * tb ) |
---|
1684 | { |
---|
1685 | uint64_t value = kilo; |
---|
1686 | units->units[TR_FMT_KB].name = tr_strdup( kb ); |
---|
1687 | units->units[TR_FMT_KB].value = value; |
---|
1688 | |
---|
1689 | value *= kilo; |
---|
1690 | units->units[TR_FMT_MB].name = tr_strdup( mb ); |
---|
1691 | units->units[TR_FMT_MB].value = value; |
---|
1692 | |
---|
1693 | value *= kilo; |
---|
1694 | units->units[TR_FMT_GB].name = tr_strdup( gb ); |
---|
1695 | units->units[TR_FMT_GB].value = value; |
---|
1696 | |
---|
1697 | value *= kilo; |
---|
1698 | units->units[TR_FMT_TB].name = tr_strdup( tb ); |
---|
1699 | units->units[TR_FMT_TB].value = value; |
---|
1700 | } |
---|
1701 | |
---|
1702 | static char* |
---|
1703 | formatter_get_size_str( const struct formatter_units * u, |
---|
1704 | char * buf, int64_t bytes, size_t buflen ) |
---|
1705 | { |
---|
1706 | int precision; |
---|
1707 | double value; |
---|
1708 | const char * units; |
---|
1709 | const struct formatter_unit * unit; |
---|
1710 | |
---|
1711 | if( bytes < u->units[1].value ) unit = &u->units[0]; |
---|
1712 | else if( bytes < u->units[2].value ) unit = &u->units[1]; |
---|
1713 | else if( bytes < u->units[3].value ) unit = &u->units[2]; |
---|
1714 | else unit = &u->units[3]; |
---|
1715 | |
---|
1716 | value = (double)bytes / unit->value; |
---|
1717 | units = unit->name; |
---|
1718 | if( unit->value == 1 ) |
---|
1719 | precision = 0; |
---|
1720 | else if( value < 100 ) |
---|
1721 | precision = 2; |
---|
1722 | else |
---|
1723 | precision = 1; |
---|
1724 | tr_snprintf( buf, buflen, "%.*f %s", precision, value, units ); |
---|
1725 | return buf; |
---|
1726 | } |
---|
1727 | |
---|
1728 | static struct formatter_units size_units; |
---|
1729 | |
---|
1730 | void |
---|
1731 | tr_formatter_size_init( unsigned int kilo, |
---|
1732 | const char * kb, const char * mb, |
---|
1733 | const char * gb, const char * tb ) |
---|
1734 | { |
---|
1735 | formatter_init( &size_units, kilo, kb, mb, gb, tb ); |
---|
1736 | } |
---|
1737 | |
---|
1738 | char* |
---|
1739 | tr_formatter_size_B( char * buf, int64_t bytes, size_t buflen ) |
---|
1740 | { |
---|
1741 | return formatter_get_size_str( &size_units, buf, bytes, buflen ); |
---|
1742 | } |
---|
1743 | |
---|
1744 | static struct formatter_units speed_units; |
---|
1745 | |
---|
1746 | unsigned int tr_speed_K = 0u; |
---|
1747 | |
---|
1748 | void |
---|
1749 | tr_formatter_speed_init( unsigned int kilo, |
---|
1750 | const char * kb, const char * mb, |
---|
1751 | const char * gb, const char * tb ) |
---|
1752 | { |
---|
1753 | tr_speed_K = kilo; |
---|
1754 | formatter_init( &speed_units, kilo, kb, mb, gb, tb ); |
---|
1755 | } |
---|
1756 | |
---|
1757 | char* |
---|
1758 | tr_formatter_speed_KBps( char * buf, double KBps, size_t buflen ) |
---|
1759 | { |
---|
1760 | const double K = speed_units.units[TR_FMT_KB].value; |
---|
1761 | double speed = KBps; |
---|
1762 | |
---|
1763 | if( speed <= 999.95 ) /* 0.0 KB to 999.9 KB */ |
---|
1764 | tr_snprintf( buf, buflen, "%d %s", (int)speed, speed_units.units[TR_FMT_KB].name ); |
---|
1765 | else { |
---|
1766 | speed /= K; |
---|
1767 | if( speed <= 99.995 ) /* 0.98 MB to 99.99 MB */ |
---|
1768 | tr_snprintf( buf, buflen, "%.2f %s", speed, speed_units.units[TR_FMT_MB].name ); |
---|
1769 | else if (speed <= 999.95) /* 100.0 MB to 999.9 MB */ |
---|
1770 | tr_snprintf( buf, buflen, "%.1f %s", speed, speed_units.units[TR_FMT_MB].name ); |
---|
1771 | else { |
---|
1772 | speed /= K; |
---|
1773 | tr_snprintf( buf, buflen, "%.1f %s", speed, speed_units.units[TR_FMT_GB].name ); |
---|
1774 | } |
---|
1775 | } |
---|
1776 | |
---|
1777 | return buf; |
---|
1778 | } |
---|
1779 | |
---|
1780 | static struct formatter_units mem_units; |
---|
1781 | |
---|
1782 | unsigned int tr_mem_K = 0u; |
---|
1783 | |
---|
1784 | void |
---|
1785 | tr_formatter_mem_init( unsigned int kilo, |
---|
1786 | const char * kb, const char * mb, |
---|
1787 | const char * gb, const char * tb ) |
---|
1788 | { |
---|
1789 | tr_mem_K = kilo; |
---|
1790 | formatter_init( &mem_units, kilo, kb, mb, gb, tb ); |
---|
1791 | } |
---|
1792 | |
---|
1793 | char* |
---|
1794 | tr_formatter_mem_B( char * buf, int64_t bytes_per_second, size_t buflen ) |
---|
1795 | { |
---|
1796 | return formatter_get_size_str( &mem_units, buf, bytes_per_second, buflen ); |
---|
1797 | } |
---|
1798 | |
---|
1799 | void |
---|
1800 | tr_formatter_get_units( tr_benc * d ) |
---|
1801 | { |
---|
1802 | int i; |
---|
1803 | tr_benc * l; |
---|
1804 | |
---|
1805 | tr_bencDictReserve( d, 6 ); |
---|
1806 | |
---|
1807 | tr_bencDictAddInt( d, "memory-bytes", mem_units.units[TR_FMT_KB].value ); |
---|
1808 | l = tr_bencDictAddList( d, "memory-units", 4 ); |
---|
1809 | for( i=0; i<4; i++ ) tr_bencListAddStr( l, mem_units.units[i].name ); |
---|
1810 | |
---|
1811 | tr_bencDictAddInt( d, "size-bytes", size_units.units[TR_FMT_KB].value ); |
---|
1812 | l = tr_bencDictAddList( d, "size-units", 4 ); |
---|
1813 | for( i=0; i<4; i++ ) tr_bencListAddStr( l, size_units.units[i].name ); |
---|
1814 | |
---|
1815 | tr_bencDictAddInt( d, "speed-bytes", speed_units.units[TR_FMT_KB].value ); |
---|
1816 | l = tr_bencDictAddList( d, "speed-units", 4 ); |
---|
1817 | for( i=0; i<4; i++ ) tr_bencListAddStr( l, speed_units.units[i].name ); |
---|
1818 | } |
---|
1819 | |
---|