1 | /****************************************************************************** |
---|
2 | * $Id: utils.c 6072 2008-06-07 14:41:31Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <assert.h> |
---|
26 | #include <ctype.h> /* isalpha */ |
---|
27 | #include <errno.h> |
---|
28 | #include <stdarg.h> |
---|
29 | #include <stdio.h> |
---|
30 | #include <stdlib.h> |
---|
31 | #include <string.h> /* strerror */ |
---|
32 | |
---|
33 | #include <libgen.h> /* basename */ |
---|
34 | #include <sys/time.h> |
---|
35 | #include <sys/types.h> |
---|
36 | #include <sys/stat.h> |
---|
37 | #include <unistd.h> /* usleep, stat */ |
---|
38 | |
---|
39 | #include "event.h" |
---|
40 | |
---|
41 | #ifdef WIN32 |
---|
42 | #include <windows.h> /* for Sleep */ |
---|
43 | #elif defined(__BEOS__) |
---|
44 | #include <kernel/OS.h> |
---|
45 | #endif |
---|
46 | |
---|
47 | #include "transmission.h" |
---|
48 | #include "utils.h" |
---|
49 | #include "platform.h" |
---|
50 | |
---|
51 | static tr_lock * messageLock = NULL; |
---|
52 | static int messageLevel = 0; |
---|
53 | static int messageQueuing = FALSE; |
---|
54 | static tr_msg_list * messageQueue = NULL; |
---|
55 | static tr_msg_list ** messageQueueTail = &messageQueue; |
---|
56 | |
---|
57 | void tr_msgInit( void ) |
---|
58 | { |
---|
59 | if( !messageLock ) |
---|
60 | messageLock = tr_lockNew( ); |
---|
61 | } |
---|
62 | |
---|
63 | FILE* |
---|
64 | tr_getLog( void ) |
---|
65 | { |
---|
66 | static int initialized = FALSE; |
---|
67 | static FILE * file= NULL; |
---|
68 | |
---|
69 | if( !initialized ) |
---|
70 | { |
---|
71 | const char * str = getenv( "TR_DEBUG_FD" ); |
---|
72 | int fd = 0; |
---|
73 | if( str && *str ) |
---|
74 | fd = atoi( str ); |
---|
75 | switch( fd ) { |
---|
76 | case 1: file = stdout; break; |
---|
77 | case 2: file = stderr; break; |
---|
78 | default: file = NULL; break; |
---|
79 | } |
---|
80 | initialized = TRUE; |
---|
81 | } |
---|
82 | |
---|
83 | return file; |
---|
84 | } |
---|
85 | |
---|
86 | void |
---|
87 | tr_setMessageLevel( int level ) |
---|
88 | { |
---|
89 | tr_msgInit(); |
---|
90 | tr_lockLock( messageLock ); |
---|
91 | messageLevel = MAX( 0, level ); |
---|
92 | tr_lockUnlock( messageLock ); |
---|
93 | } |
---|
94 | |
---|
95 | int |
---|
96 | tr_getMessageLevel( void ) |
---|
97 | { |
---|
98 | int ret; |
---|
99 | |
---|
100 | tr_msgInit(); |
---|
101 | tr_lockLock( messageLock ); |
---|
102 | ret = messageLevel; |
---|
103 | tr_lockUnlock( messageLock ); |
---|
104 | |
---|
105 | return ret; |
---|
106 | } |
---|
107 | |
---|
108 | void |
---|
109 | tr_setMessageQueuing( int enabled ) |
---|
110 | { |
---|
111 | tr_msgInit(); |
---|
112 | tr_lockLock( messageLock ); |
---|
113 | messageQueuing = enabled; |
---|
114 | tr_lockUnlock( messageLock ); |
---|
115 | } |
---|
116 | |
---|
117 | tr_msg_list * |
---|
118 | tr_getQueuedMessages( void ) |
---|
119 | { |
---|
120 | tr_msg_list * ret; |
---|
121 | |
---|
122 | assert( NULL != messageLock ); |
---|
123 | tr_lockLock( messageLock ); |
---|
124 | ret = messageQueue; |
---|
125 | messageQueue = NULL; |
---|
126 | messageQueueTail = &messageQueue; |
---|
127 | tr_lockUnlock( messageLock ); |
---|
128 | |
---|
129 | return ret; |
---|
130 | } |
---|
131 | |
---|
132 | void |
---|
133 | tr_freeMessageList( tr_msg_list * list ) |
---|
134 | { |
---|
135 | tr_msg_list * next; |
---|
136 | |
---|
137 | while( NULL != list ) |
---|
138 | { |
---|
139 | next = list->next; |
---|
140 | free( list->message ); |
---|
141 | free( list->name ); |
---|
142 | free( list ); |
---|
143 | list = next; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | *** |
---|
149 | **/ |
---|
150 | |
---|
151 | char* |
---|
152 | tr_getLogTimeStr( char * buf, int buflen ) |
---|
153 | { |
---|
154 | char tmp[64]; |
---|
155 | time_t now; |
---|
156 | struct tm now_tm; |
---|
157 | struct timeval tv; |
---|
158 | int milliseconds; |
---|
159 | |
---|
160 | now = time( NULL ); |
---|
161 | gettimeofday( &tv, NULL ); |
---|
162 | |
---|
163 | #ifdef WIN32 |
---|
164 | now_tm = *localtime( &now ); |
---|
165 | #else |
---|
166 | localtime_r( &now, &now_tm ); |
---|
167 | #endif |
---|
168 | strftime( tmp, sizeof(tmp), "%H:%M:%S", &now_tm ); |
---|
169 | milliseconds = (int)(tv.tv_usec / 1000); |
---|
170 | snprintf( buf, buflen, "%s.%03d", tmp, milliseconds ); |
---|
171 | |
---|
172 | return buf; |
---|
173 | } |
---|
174 | |
---|
175 | void |
---|
176 | tr_deepLog( const char * file, int line, const char * name, const char * fmt, ... ) |
---|
177 | { |
---|
178 | FILE * fp = tr_getLog( ); |
---|
179 | if( fp != NULL ) |
---|
180 | { |
---|
181 | va_list args; |
---|
182 | char timestr[64]; |
---|
183 | struct evbuffer * buf = evbuffer_new( ); |
---|
184 | char * myfile = tr_strdup( file ); |
---|
185 | |
---|
186 | evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof(timestr) ) ); |
---|
187 | if( name ) |
---|
188 | evbuffer_add_printf( buf, "%s ", name ); |
---|
189 | va_start( args, fmt ); |
---|
190 | evbuffer_add_vprintf( buf, fmt, args ); |
---|
191 | va_end( args ); |
---|
192 | evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line ); |
---|
193 | fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp ); |
---|
194 | |
---|
195 | tr_free( myfile ); |
---|
196 | evbuffer_free( buf ); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | /*** |
---|
201 | **** |
---|
202 | ***/ |
---|
203 | |
---|
204 | void |
---|
205 | tr_msg( const char * file, int line, int level, |
---|
206 | const char * name, |
---|
207 | const char * fmt, ... ) |
---|
208 | { |
---|
209 | FILE * fp; |
---|
210 | |
---|
211 | if( messageLock ) |
---|
212 | tr_lockLock( messageLock ); |
---|
213 | |
---|
214 | fp = tr_getLog( ); |
---|
215 | |
---|
216 | if( !messageLevel ) |
---|
217 | { |
---|
218 | char * env = getenv( "TR_DEBUG" ); |
---|
219 | messageLevel = ( env ? atoi( env ) : 0 ) + 1; |
---|
220 | messageLevel = MAX( 1, messageLevel ); |
---|
221 | } |
---|
222 | |
---|
223 | if( messageLevel >= level ) |
---|
224 | { |
---|
225 | va_list ap; |
---|
226 | struct evbuffer * buf = evbuffer_new( ); |
---|
227 | |
---|
228 | /* build the text message */ |
---|
229 | va_start( ap, fmt ); |
---|
230 | evbuffer_add_vprintf( buf, fmt, ap ); |
---|
231 | va_end( ap ); |
---|
232 | |
---|
233 | if( EVBUFFER_LENGTH( buf ) ) |
---|
234 | { |
---|
235 | if( messageQueuing ) |
---|
236 | { |
---|
237 | tr_msg_list * newmsg; |
---|
238 | newmsg = tr_new0( tr_msg_list, 1 ); |
---|
239 | newmsg->level = level; |
---|
240 | newmsg->when = time( NULL ); |
---|
241 | newmsg->message = tr_strdup( (char*)EVBUFFER_DATA( buf ) ); |
---|
242 | newmsg->file = file; |
---|
243 | newmsg->line = line; |
---|
244 | newmsg->name = tr_strdup( name ); |
---|
245 | |
---|
246 | *messageQueueTail = newmsg; |
---|
247 | messageQueueTail = &newmsg->next; |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | if( fp == NULL ) |
---|
252 | fp = stderr; |
---|
253 | if( name ) |
---|
254 | fprintf( fp, "%s: %s\n", name, (char*)EVBUFFER_DATA(buf) ); |
---|
255 | else |
---|
256 | fprintf( fp, "%s\n", (char*)EVBUFFER_DATA(buf) ); |
---|
257 | fflush( fp ); |
---|
258 | } |
---|
259 | |
---|
260 | evbuffer_free( buf ); |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | if( messageLock ) |
---|
265 | tr_lockUnlock( messageLock ); |
---|
266 | } |
---|
267 | |
---|
268 | int tr_rand( int sup ) |
---|
269 | { |
---|
270 | static int init = 0; |
---|
271 | |
---|
272 | assert( sup > 0 ); |
---|
273 | |
---|
274 | if( !init ) |
---|
275 | { |
---|
276 | srand( tr_date() ); |
---|
277 | init = 1; |
---|
278 | } |
---|
279 | return rand() % sup; |
---|
280 | } |
---|
281 | |
---|
282 | /*** |
---|
283 | **** |
---|
284 | ***/ |
---|
285 | |
---|
286 | void |
---|
287 | tr_set_compare( const void * va, size_t aCount, |
---|
288 | const void * vb, size_t bCount, |
---|
289 | int compare( const void * a, const void * b ), |
---|
290 | size_t elementSize, |
---|
291 | tr_set_func in_a_cb, |
---|
292 | tr_set_func in_b_cb, |
---|
293 | tr_set_func in_both_cb, |
---|
294 | void * userData ) |
---|
295 | { |
---|
296 | const uint8_t * a = (const uint8_t *) va; |
---|
297 | const uint8_t * b = (const uint8_t *) vb; |
---|
298 | const uint8_t * aend = a + elementSize*aCount; |
---|
299 | const uint8_t * bend = b + elementSize*bCount; |
---|
300 | |
---|
301 | while( a!=aend || b!=bend ) |
---|
302 | { |
---|
303 | if( a==aend ) |
---|
304 | { |
---|
305 | (*in_b_cb)( (void*)b, userData ); |
---|
306 | b += elementSize; |
---|
307 | } |
---|
308 | else if ( b==bend ) |
---|
309 | { |
---|
310 | (*in_a_cb)( (void*)a, userData ); |
---|
311 | a += elementSize; |
---|
312 | } |
---|
313 | else |
---|
314 | { |
---|
315 | const int val = (*compare)( a, b ); |
---|
316 | |
---|
317 | if( !val ) |
---|
318 | { |
---|
319 | (*in_both_cb)( (void*)a, userData ); |
---|
320 | a += elementSize; |
---|
321 | b += elementSize; |
---|
322 | } |
---|
323 | else if( val < 0 ) |
---|
324 | { |
---|
325 | (*in_a_cb)( (void*)a, userData ); |
---|
326 | a += elementSize; |
---|
327 | } |
---|
328 | else if( val > 0 ) |
---|
329 | { |
---|
330 | (*in_b_cb)( (void*)b, userData ); |
---|
331 | b += elementSize; |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | /*** |
---|
338 | **** |
---|
339 | ***/ |
---|
340 | |
---|
341 | int |
---|
342 | tr_compareUint16( uint16_t a, uint16_t b ) |
---|
343 | { |
---|
344 | if( a < b ) return -1; |
---|
345 | if( a > b ) return 1; |
---|
346 | return 0; |
---|
347 | } |
---|
348 | |
---|
349 | int |
---|
350 | tr_compareUint32( uint32_t a, uint32_t b ) |
---|
351 | { |
---|
352 | if( a < b ) return -1; |
---|
353 | if( a > b ) return 1; |
---|
354 | return 0; |
---|
355 | } |
---|
356 | |
---|
357 | int |
---|
358 | tr_compareUint64( uint64_t a, uint64_t b ) |
---|
359 | { |
---|
360 | if( a < b ) return -1; |
---|
361 | if( a > b ) return 1; |
---|
362 | return 0; |
---|
363 | } |
---|
364 | |
---|
365 | /** |
---|
366 | *** |
---|
367 | **/ |
---|
368 | |
---|
369 | struct timeval |
---|
370 | tr_timevalMsec( uint64_t milliseconds ) |
---|
371 | { |
---|
372 | struct timeval ret; |
---|
373 | const uint64_t microseconds = milliseconds * 1000; |
---|
374 | ret.tv_sec = microseconds / 1000000; |
---|
375 | ret.tv_usec = microseconds % 1000000; |
---|
376 | return ret; |
---|
377 | } |
---|
378 | |
---|
379 | uint8_t * |
---|
380 | tr_loadFile( const char * path, size_t * size ) |
---|
381 | { |
---|
382 | uint8_t * buf; |
---|
383 | struct stat sb; |
---|
384 | FILE * file; |
---|
385 | const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); |
---|
386 | |
---|
387 | /* try to stat the file */ |
---|
388 | errno = 0; |
---|
389 | if( stat( path, &sb ) ) |
---|
390 | { |
---|
391 | tr_dbg( err_fmt, path, tr_strerror(errno) ); |
---|
392 | return NULL; |
---|
393 | } |
---|
394 | |
---|
395 | if( ( sb.st_mode & S_IFMT ) != S_IFREG ) |
---|
396 | { |
---|
397 | tr_err( err_fmt, path, _( "Not a regular file" ) ); |
---|
398 | return NULL; |
---|
399 | } |
---|
400 | |
---|
401 | /* Load the torrent file into our buffer */ |
---|
402 | file = fopen( path, "rb" ); |
---|
403 | if( !file ) |
---|
404 | { |
---|
405 | tr_err( err_fmt, path, tr_strerror(errno) ); |
---|
406 | return NULL; |
---|
407 | } |
---|
408 | buf = malloc( sb.st_size ); |
---|
409 | if( NULL == buf ) |
---|
410 | { |
---|
411 | tr_err( err_fmt, path, _( "Memory allocation failed" ) ); |
---|
412 | fclose( file ); |
---|
413 | return NULL; |
---|
414 | } |
---|
415 | fseek( file, 0, SEEK_SET ); |
---|
416 | if( fread( buf, sb.st_size, 1, file ) != 1 ) |
---|
417 | { |
---|
418 | tr_err( err_fmt, path, tr_strerror(errno) ); |
---|
419 | free( buf ); |
---|
420 | fclose( file ); |
---|
421 | return NULL; |
---|
422 | } |
---|
423 | fclose( file ); |
---|
424 | |
---|
425 | *size = sb.st_size; |
---|
426 | |
---|
427 | return buf; |
---|
428 | } |
---|
429 | |
---|
430 | int |
---|
431 | tr_mkdir( const char * path, int permissions |
---|
432 | #ifdef WIN32 |
---|
433 | UNUSED |
---|
434 | #endif |
---|
435 | ) |
---|
436 | { |
---|
437 | #ifdef WIN32 |
---|
438 | if( path && isalpha(path[0]) && path[1]==':' && !path[2] ) |
---|
439 | return 0; |
---|
440 | return mkdir( path ); |
---|
441 | #else |
---|
442 | return mkdir( path, permissions ); |
---|
443 | #endif |
---|
444 | } |
---|
445 | |
---|
446 | int |
---|
447 | tr_mkdirp( const char * path_in, int permissions ) |
---|
448 | { |
---|
449 | char * path = tr_strdup( path_in ); |
---|
450 | char * p, * pp; |
---|
451 | struct stat sb; |
---|
452 | int done; |
---|
453 | |
---|
454 | /* walk past the root */ |
---|
455 | p = path; |
---|
456 | while( *p == TR_PATH_DELIMITER ) |
---|
457 | ++p; |
---|
458 | |
---|
459 | pp = p; |
---|
460 | done = 0; |
---|
461 | while( ( p = strchr( pp, TR_PATH_DELIMITER ) ) || ( p = strchr( pp, '\0' ) ) ) |
---|
462 | { |
---|
463 | if( !*p ) |
---|
464 | done = 1; |
---|
465 | else |
---|
466 | *p = '\0'; |
---|
467 | |
---|
468 | if( stat( path, &sb ) ) |
---|
469 | { |
---|
470 | /* Folder doesn't exist yet */ |
---|
471 | if( tr_mkdir( path, permissions ) ) { |
---|
472 | const int err = errno; |
---|
473 | tr_err( _( "Couldn't create \"%1$s\": %2$s" ), path, tr_strerror( err ) ); |
---|
474 | tr_free( path ); |
---|
475 | errno = err; |
---|
476 | return -1; |
---|
477 | } |
---|
478 | } |
---|
479 | else if( ( sb.st_mode & S_IFMT ) != S_IFDIR ) |
---|
480 | { |
---|
481 | /* Node exists but isn't a folder */ |
---|
482 | char buf[MAX_PATH_LENGTH]; |
---|
483 | snprintf( buf, sizeof( buf ), _( "File \"%s\" is in the way" ), path ); |
---|
484 | tr_err( _( "Couldn't create \"%1$s\": %2$s" ), path_in, buf ); |
---|
485 | tr_free( path ); |
---|
486 | errno = ENOTDIR; |
---|
487 | return -1; |
---|
488 | } |
---|
489 | |
---|
490 | if( done ) |
---|
491 | break; |
---|
492 | |
---|
493 | *p = TR_PATH_DELIMITER; |
---|
494 | p++; |
---|
495 | pp = p; |
---|
496 | } |
---|
497 | |
---|
498 | tr_free( path ); |
---|
499 | return 0; |
---|
500 | } |
---|
501 | |
---|
502 | void |
---|
503 | tr_buildPath ( char *buf, size_t buflen, const char *first_element, ... ) |
---|
504 | { |
---|
505 | struct evbuffer * evbuf = evbuffer_new( ); |
---|
506 | const char * element = first_element; |
---|
507 | va_list vl; |
---|
508 | va_start( vl, first_element ); |
---|
509 | while( element ) { |
---|
510 | if( EVBUFFER_LENGTH(evbuf) ) |
---|
511 | evbuffer_add_printf( evbuf, "%c", TR_PATH_DELIMITER ); |
---|
512 | evbuffer_add_printf( evbuf, "%s", element ); |
---|
513 | element = (const char*) va_arg( vl, const char* ); |
---|
514 | } |
---|
515 | if( EVBUFFER_LENGTH(evbuf) ) |
---|
516 | tr_strlcpy( buf, (char*)EVBUFFER_DATA(evbuf), buflen ); |
---|
517 | else |
---|
518 | *buf = '\0'; |
---|
519 | evbuffer_free( evbuf ); |
---|
520 | } |
---|
521 | |
---|
522 | int |
---|
523 | tr_ioErrorFromErrno( int err ) |
---|
524 | { |
---|
525 | switch( err ) |
---|
526 | { |
---|
527 | case 0: |
---|
528 | return TR_OK; |
---|
529 | case EACCES: |
---|
530 | case EROFS: |
---|
531 | return TR_ERROR_IO_PERMISSIONS; |
---|
532 | case ENOSPC: |
---|
533 | return TR_ERROR_IO_SPACE; |
---|
534 | case EMFILE: |
---|
535 | return TR_ERROR_IO_OPEN_FILES; |
---|
536 | case EFBIG: |
---|
537 | return TR_ERROR_IO_FILE_TOO_BIG; |
---|
538 | default: |
---|
539 | tr_dbg( "generic i/o errno from errno: %s", tr_strerror( errno ) ); |
---|
540 | return TR_ERROR_IO_OTHER; |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | const char * |
---|
545 | tr_errorString( int code ) |
---|
546 | { |
---|
547 | switch( code ) |
---|
548 | { |
---|
549 | case TR_OK: |
---|
550 | return _( "No error" ); |
---|
551 | |
---|
552 | case TR_ERROR: |
---|
553 | return _( "Unspecified error" ); |
---|
554 | case TR_ERROR_ASSERT: |
---|
555 | return _( "Assert error" ); |
---|
556 | |
---|
557 | case TR_ERROR_IO_PARENT: |
---|
558 | return _( "Destination folder doesn't exist" ); |
---|
559 | case TR_ERROR_IO_PERMISSIONS: |
---|
560 | return tr_strerror( EACCES ); |
---|
561 | case TR_ERROR_IO_SPACE: |
---|
562 | return tr_strerror( ENOSPC ); |
---|
563 | case TR_ERROR_IO_FILE_TOO_BIG: |
---|
564 | return tr_strerror( EFBIG ); |
---|
565 | case TR_ERROR_IO_OPEN_FILES: |
---|
566 | return tr_strerror( EMFILE ); |
---|
567 | case TR_ERROR_IO_DUP_DOWNLOAD: |
---|
568 | return _( "A torrent with this name and destination folder already exists." ); |
---|
569 | case TR_ERROR_IO_CHECKSUM: |
---|
570 | return _( "Checksum failed" ); |
---|
571 | case TR_ERROR_IO_OTHER: |
---|
572 | return _( "Unspecified I/O error" ); |
---|
573 | |
---|
574 | case TR_ERROR_TC_ERROR: |
---|
575 | return _( "Tracker error" ); |
---|
576 | case TR_ERROR_TC_WARNING: |
---|
577 | return _( "Tracker warning" ); |
---|
578 | |
---|
579 | case TR_ERROR_PEER_MESSAGE: |
---|
580 | return _( "Peer sent a bad message" ); |
---|
581 | |
---|
582 | default: |
---|
583 | return _( "Unknown error" ); |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | /**** |
---|
588 | ***** |
---|
589 | ****/ |
---|
590 | |
---|
591 | void* |
---|
592 | tr_memdup( const void * in, int byteCount ) |
---|
593 | { |
---|
594 | void * out = tr_new( uint8_t, byteCount ); |
---|
595 | memcpy( out, in, byteCount ); |
---|
596 | return out; |
---|
597 | } |
---|
598 | |
---|
599 | char* |
---|
600 | tr_strdup( const char * in ) |
---|
601 | { |
---|
602 | return tr_strndup( in, in ? strlen(in) : 0 ); |
---|
603 | } |
---|
604 | |
---|
605 | char* |
---|
606 | tr_strndup( const char * in, int len ) |
---|
607 | { |
---|
608 | char * out = NULL; |
---|
609 | |
---|
610 | if( len < 0 ) |
---|
611 | { |
---|
612 | out = tr_strdup( in ); |
---|
613 | } |
---|
614 | else if( in != NULL ) |
---|
615 | { |
---|
616 | out = tr_malloc( len+1 ); |
---|
617 | memcpy( out, in, len ); |
---|
618 | out[len] = '\0'; |
---|
619 | } |
---|
620 | |
---|
621 | return out; |
---|
622 | } |
---|
623 | |
---|
624 | char* |
---|
625 | tr_strdup_printf( const char * fmt, ... ) |
---|
626 | { |
---|
627 | char * ret = NULL; |
---|
628 | struct evbuffer * buf; |
---|
629 | va_list ap; |
---|
630 | |
---|
631 | buf = evbuffer_new( ); |
---|
632 | va_start( ap, fmt ); |
---|
633 | if( evbuffer_add_vprintf( buf, fmt, ap ) != -1 ) |
---|
634 | ret = tr_strdup( (char*)EVBUFFER_DATA( buf ) ); |
---|
635 | evbuffer_free( buf ); |
---|
636 | |
---|
637 | return ret; |
---|
638 | } |
---|
639 | |
---|
640 | void* |
---|
641 | tr_calloc( size_t nmemb, size_t size ) |
---|
642 | { |
---|
643 | return nmemb && size ? calloc( nmemb, size ) : NULL; |
---|
644 | } |
---|
645 | |
---|
646 | void* |
---|
647 | tr_malloc( size_t size ) |
---|
648 | { |
---|
649 | return size ? malloc( size ) : NULL; |
---|
650 | } |
---|
651 | |
---|
652 | void* |
---|
653 | tr_malloc0( size_t size ) |
---|
654 | { |
---|
655 | void * ret = tr_malloc( size ); |
---|
656 | memset( ret, 0, size ); |
---|
657 | return ret; |
---|
658 | } |
---|
659 | |
---|
660 | void |
---|
661 | tr_free( void * p ) |
---|
662 | { |
---|
663 | if( p ) |
---|
664 | free( p ); |
---|
665 | } |
---|
666 | |
---|
667 | const char* |
---|
668 | tr_strerror( int i ) |
---|
669 | { |
---|
670 | const char * ret = strerror( i ); |
---|
671 | if( ret == NULL ) |
---|
672 | ret = "Unknown Error"; |
---|
673 | return ret; |
---|
674 | } |
---|
675 | |
---|
676 | /**** |
---|
677 | ***** |
---|
678 | ****/ |
---|
679 | |
---|
680 | tr_bitfield* |
---|
681 | tr_bitfieldNew( size_t bitCount ) |
---|
682 | { |
---|
683 | tr_bitfield * ret = tr_new0( tr_bitfield, 1 ); |
---|
684 | ret->bitCount = bitCount; |
---|
685 | ret->byteCount = (bitCount+7u) / 8u; |
---|
686 | ret->bits = tr_new0( uint8_t, ret->byteCount ); |
---|
687 | return ret; |
---|
688 | } |
---|
689 | |
---|
690 | tr_bitfield* |
---|
691 | tr_bitfieldDup( const tr_bitfield * in ) |
---|
692 | { |
---|
693 | tr_bitfield * ret = calloc( 1, sizeof(tr_bitfield) ); |
---|
694 | ret->bitCount = in->bitCount; |
---|
695 | ret->byteCount = in->byteCount; |
---|
696 | ret->bits = tr_memdup( in->bits, in->byteCount ); |
---|
697 | return ret; |
---|
698 | } |
---|
699 | |
---|
700 | void |
---|
701 | tr_bitfieldFree( tr_bitfield * bitfield ) |
---|
702 | { |
---|
703 | if( bitfield ) |
---|
704 | { |
---|
705 | tr_free( bitfield->bits ); |
---|
706 | tr_free( bitfield ); |
---|
707 | } |
---|
708 | } |
---|
709 | |
---|
710 | void |
---|
711 | tr_bitfieldClear( tr_bitfield * bitfield ) |
---|
712 | { |
---|
713 | memset( bitfield->bits, 0, bitfield->byteCount ); |
---|
714 | } |
---|
715 | |
---|
716 | int |
---|
717 | tr_bitfieldIsEmpty( const tr_bitfield * bitfield ) |
---|
718 | { |
---|
719 | size_t i; |
---|
720 | |
---|
721 | for( i=0; i<bitfield->byteCount; ++i ) |
---|
722 | if( bitfield->bits[i] ) |
---|
723 | return 0; |
---|
724 | |
---|
725 | return 1; |
---|
726 | } |
---|
727 | |
---|
728 | int |
---|
729 | tr_bitfieldHas( const tr_bitfield * bitfield, size_t nth ) |
---|
730 | { |
---|
731 | return ( tr_bitfieldTestFast( bitfield, nth ) ) |
---|
732 | && ( tr_bitfieldHasFast( bitfield, nth ) ); |
---|
733 | } |
---|
734 | |
---|
735 | static int |
---|
736 | find_top_bit( uint8_t val ) |
---|
737 | { |
---|
738 | int pos = 0; |
---|
739 | if ( val & 0xF0U ) pos |= 4, val >>= 4; |
---|
740 | if ( val & 0xCU ) pos |= 2, val >>= 2; |
---|
741 | if ( val & 0x2 ) pos |= 1; |
---|
742 | return 7 - pos; |
---|
743 | } |
---|
744 | |
---|
745 | int |
---|
746 | tr_bitfieldFindTrue( const tr_bitfield * bitfield, |
---|
747 | size_t startBit, |
---|
748 | size_t * setmePos ) |
---|
749 | { |
---|
750 | if( bitfield && bitfield->bits && startBit < bitfield->bitCount ) |
---|
751 | { |
---|
752 | const uint8_t * b = bitfield->bits + startBit/8; |
---|
753 | const uint8_t * end = bitfield->bits + bitfield->byteCount; |
---|
754 | |
---|
755 | /* If first byte already contains a set bit after startBit*/ |
---|
756 | if( *b & ( 0xff >> (startBit&7) ) ) { |
---|
757 | *setmePos = 8 * ( b - bitfield->bits ); |
---|
758 | *setmePos += find_top_bit( *b & ( 0xff >> (startBit&7) ) ); |
---|
759 | return 1; |
---|
760 | } |
---|
761 | |
---|
762 | /* Test bitfield for first non zero byte */ |
---|
763 | ++b; |
---|
764 | while( (b < end) && !*b ) |
---|
765 | ++b; |
---|
766 | |
---|
767 | /* If we hit the end of our bitfield, no set bit was found */ |
---|
768 | if( b == end ) |
---|
769 | return 0; |
---|
770 | |
---|
771 | /* New bitposition is byteoff*8 */ |
---|
772 | *setmePos = 8 * ( b - bitfield->bits ) + find_top_bit( *b ); |
---|
773 | |
---|
774 | return 1; |
---|
775 | } |
---|
776 | |
---|
777 | return 0; |
---|
778 | } |
---|
779 | |
---|
780 | int |
---|
781 | tr_bitfieldAdd( tr_bitfield * bitfield, size_t nth ) |
---|
782 | { |
---|
783 | assert( bitfield != NULL ); |
---|
784 | assert( bitfield->bits != NULL ); |
---|
785 | |
---|
786 | if( nth >= bitfield->bitCount ) |
---|
787 | return -1; |
---|
788 | |
---|
789 | bitfield->bits[nth>>3u] |= (0x80 >> (nth&7u)); |
---|
790 | return 0; |
---|
791 | } |
---|
792 | |
---|
793 | int |
---|
794 | tr_bitfieldAddRange( tr_bitfield * bitfield, |
---|
795 | size_t begin, |
---|
796 | size_t end ) |
---|
797 | { |
---|
798 | int err = 0; |
---|
799 | size_t i; |
---|
800 | for( i=begin; i<end; ++i ) |
---|
801 | if(( err = tr_bitfieldAdd( bitfield, i ))) |
---|
802 | break; |
---|
803 | return err; |
---|
804 | } |
---|
805 | |
---|
806 | int |
---|
807 | tr_bitfieldRem( tr_bitfield * bitfield, |
---|
808 | size_t nth ) |
---|
809 | { |
---|
810 | assert( bitfield != NULL ); |
---|
811 | assert( bitfield->bits != NULL ); |
---|
812 | |
---|
813 | if( nth >= bitfield->bitCount ) |
---|
814 | return -1; |
---|
815 | |
---|
816 | bitfield->bits[nth>>3u] &= (0xff7f >> (nth&7u)); |
---|
817 | return 0; |
---|
818 | } |
---|
819 | |
---|
820 | int |
---|
821 | tr_bitfieldRemRange ( tr_bitfield * b, |
---|
822 | size_t begin, |
---|
823 | size_t end ) |
---|
824 | { |
---|
825 | int err = 0; |
---|
826 | size_t i; |
---|
827 | for( i=begin; i<end; ++i ) |
---|
828 | if(( err = tr_bitfieldRem( b, i ))) |
---|
829 | break; |
---|
830 | return err; |
---|
831 | } |
---|
832 | |
---|
833 | tr_bitfield* |
---|
834 | tr_bitfieldOr( tr_bitfield * a, const tr_bitfield * b ) |
---|
835 | { |
---|
836 | uint8_t *ait; |
---|
837 | const uint8_t *aend, *bit; |
---|
838 | |
---|
839 | assert( a->bitCount == b->bitCount ); |
---|
840 | |
---|
841 | for( ait=a->bits, bit=b->bits, aend=ait+a->byteCount; ait!=aend; ) |
---|
842 | *ait++ |= *bit++; |
---|
843 | |
---|
844 | return a; |
---|
845 | } |
---|
846 | |
---|
847 | /* set 'a' to all the flags that were in 'a' but not 'b' */ |
---|
848 | void |
---|
849 | tr_bitfieldDifference( tr_bitfield * a, const tr_bitfield * b ) |
---|
850 | { |
---|
851 | uint8_t *ait; |
---|
852 | const uint8_t *aend, *bit; |
---|
853 | |
---|
854 | assert( a->bitCount == b->bitCount ); |
---|
855 | |
---|
856 | for( ait=a->bits, bit=b->bits, aend=ait+a->byteCount; ait!=aend; ) |
---|
857 | *ait++ &= ~(*bit++); |
---|
858 | } |
---|
859 | |
---|
860 | |
---|
861 | size_t |
---|
862 | tr_bitfieldCountTrueBits( const tr_bitfield* b ) |
---|
863 | { |
---|
864 | size_t ret = 0; |
---|
865 | const uint8_t *it, *end; |
---|
866 | static const int trueBitCount[512] = { |
---|
867 | 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, |
---|
868 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
---|
869 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
---|
870 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
---|
871 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
---|
872 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
---|
873 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
---|
874 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8, |
---|
875 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
---|
876 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
---|
877 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
---|
878 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8, |
---|
879 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
---|
880 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8, |
---|
881 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8, |
---|
882 | 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,5,6,6,7,6,7,7,8,6,7,7,8,7,8,8,9 |
---|
883 | }; |
---|
884 | |
---|
885 | if( !b ) |
---|
886 | return 0; |
---|
887 | |
---|
888 | for( it=b->bits, end=it+b->byteCount; it!=end; ++it ) |
---|
889 | ret += trueBitCount[*it]; |
---|
890 | |
---|
891 | return ret; |
---|
892 | } |
---|
893 | |
---|
894 | /*** |
---|
895 | **** |
---|
896 | ***/ |
---|
897 | |
---|
898 | uint64_t |
---|
899 | tr_date( void ) |
---|
900 | { |
---|
901 | struct timeval tv; |
---|
902 | gettimeofday( &tv, NULL ); |
---|
903 | return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 ); |
---|
904 | } |
---|
905 | |
---|
906 | void |
---|
907 | tr_wait( uint64_t delay_milliseconds ) |
---|
908 | { |
---|
909 | #ifdef __BEOS__ |
---|
910 | snooze( 1000 * delay_milliseconds ); |
---|
911 | #elif defined(WIN32) |
---|
912 | Sleep( (DWORD)delay_milliseconds ); |
---|
913 | #else |
---|
914 | usleep( 1000 * delay_milliseconds ); |
---|
915 | #endif |
---|
916 | } |
---|
917 | |
---|
918 | /*** |
---|
919 | **** |
---|
920 | ***/ |
---|
921 | |
---|
922 | /* |
---|
923 | * Copy src to string dst of size siz. At most siz-1 characters |
---|
924 | * will be copied. Always NUL terminates (unless siz == 0). |
---|
925 | * Returns strlen(src); if retval >= siz, truncation occurred. |
---|
926 | */ |
---|
927 | size_t |
---|
928 | tr_strlcpy(char *dst, const char *src, size_t siz) |
---|
929 | { |
---|
930 | #ifdef HAVE_STRLCPY |
---|
931 | return strlcpy( dst, src, siz ); |
---|
932 | #else |
---|
933 | char *d = dst; |
---|
934 | const char *s = src; |
---|
935 | size_t n = siz; |
---|
936 | |
---|
937 | assert( s != NULL ); |
---|
938 | assert( d != NULL ); |
---|
939 | |
---|
940 | /* Copy as many bytes as will fit */ |
---|
941 | if (n != 0) { |
---|
942 | while (--n != 0) { |
---|
943 | if ((*d++ = *s++) == '\0') |
---|
944 | break; |
---|
945 | } |
---|
946 | } |
---|
947 | |
---|
948 | /* Not enough room in dst, add NUL and traverse rest of src */ |
---|
949 | if (n == 0) { |
---|
950 | if (siz != 0) |
---|
951 | *d = '\0'; /* NUL-terminate dst */ |
---|
952 | while (*s++) |
---|
953 | ; |
---|
954 | } |
---|
955 | |
---|
956 | return(s - src - 1); /* count does not include NUL */ |
---|
957 | #endif |
---|
958 | } |
---|
959 | |
---|
960 | /*** |
---|
961 | **** |
---|
962 | ***/ |
---|
963 | |
---|
964 | double |
---|
965 | tr_getRatio( double numerator, double denominator ) |
---|
966 | { |
---|
967 | double ratio; |
---|
968 | |
---|
969 | if( denominator ) |
---|
970 | ratio = numerator / denominator; |
---|
971 | else if( numerator ) |
---|
972 | ratio = TR_RATIO_INF; |
---|
973 | else |
---|
974 | ratio = TR_RATIO_NA; |
---|
975 | |
---|
976 | return ratio; |
---|
977 | } |
---|
978 | |
---|
979 | void |
---|
980 | tr_sha1_to_hex( char * out, const uint8_t * sha1 ) |
---|
981 | { |
---|
982 | static const char hex[] = "0123456789abcdef"; |
---|
983 | int i; |
---|
984 | for (i = 0; i < 20; i++) { |
---|
985 | unsigned int val = *sha1++; |
---|
986 | *out++ = hex[val >> 4]; |
---|
987 | *out++ = hex[val & 0xf]; |
---|
988 | } |
---|
989 | *out = '\0'; |
---|
990 | } |
---|
991 | |
---|
992 | /*** |
---|
993 | **** |
---|
994 | ***/ |
---|
995 | |
---|
996 | int |
---|
997 | tr_httpIsValidURL( const char * url ) |
---|
998 | { |
---|
999 | const char * c; |
---|
1000 | static const char * rfc2396_valid_chars = |
---|
1001 | "abcdefghijklmnopqrstuvwxyz" /* lowalpha */ |
---|
1002 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* upalpha */ |
---|
1003 | "0123456789" /* digit */ |
---|
1004 | "-_.!~*'()" /* mark */ |
---|
1005 | ";/?:@&=+$," /* reserved */ |
---|
1006 | "<>#%<\"" /* delims */ |
---|
1007 | "{}|\\^[]`"; /* unwise */ |
---|
1008 | |
---|
1009 | for( c=url; c && *c; ++c ) |
---|
1010 | if( !strchr( rfc2396_valid_chars, *c ) ) |
---|
1011 | return FALSE; |
---|
1012 | |
---|
1013 | return !tr_httpParseURL( url, -1, NULL, NULL, NULL ); |
---|
1014 | } |
---|
1015 | |
---|
1016 | int |
---|
1017 | tr_httpParseURL( const char * url_in, int len, |
---|
1018 | char ** setme_host, |
---|
1019 | int * setme_port, |
---|
1020 | char ** setme_path ) |
---|
1021 | { |
---|
1022 | int err; |
---|
1023 | int port = 0; |
---|
1024 | int n; |
---|
1025 | char * tmp; |
---|
1026 | char * pch; |
---|
1027 | const char * protocol = NULL; |
---|
1028 | const char * host = NULL; |
---|
1029 | const char * path = NULL; |
---|
1030 | |
---|
1031 | tmp = tr_strndup( url_in, len ); |
---|
1032 | if(( pch = strstr( tmp, "://" ))) |
---|
1033 | { |
---|
1034 | *pch = '\0'; |
---|
1035 | protocol = tmp; |
---|
1036 | pch += 3; |
---|
1037 | /*fprintf( stderr, "protocol is [%s]... what's left is [%s]\n", protocol, pch );*/ |
---|
1038 | if(( n = strcspn( pch, ":/" ))) |
---|
1039 | { |
---|
1040 | const int havePort = pch[n] == ':'; |
---|
1041 | host = pch; |
---|
1042 | pch += n; |
---|
1043 | *pch++ = '\0'; |
---|
1044 | /*fprintf( stderr, "host is [%s]... what's left is [%s]\n", host, pch );*/ |
---|
1045 | if( havePort ) |
---|
1046 | { |
---|
1047 | char * end; |
---|
1048 | port = strtol( pch, &end, 10 ); |
---|
1049 | pch = end; |
---|
1050 | /*fprintf( stderr, "port is [%d]... what's left is [%s]\n", port, pch );*/ |
---|
1051 | } |
---|
1052 | path = pch; |
---|
1053 | /*fprintf( stderr, "path is [%s]\n", path );*/ |
---|
1054 | } |
---|
1055 | } |
---|
1056 | |
---|
1057 | err = !host || !path || !protocol || ( strcmp(protocol,"http") && strcmp(protocol,"https") ); |
---|
1058 | |
---|
1059 | if( !err && !port ) { |
---|
1060 | if( !strcmp(protocol,"http") ) port = 80; |
---|
1061 | if( !strcmp(protocol,"https") ) port = 443; |
---|
1062 | } |
---|
1063 | |
---|
1064 | if( !err ) { |
---|
1065 | if( setme_host) { ((char*)host)[-3]=':'; *setme_host = tr_strdup( protocol ); } |
---|
1066 | if( setme_path) { ((char*)path)[-1]='/'; *setme_path = tr_strdup( path-1 ); } |
---|
1067 | if( setme_port) *setme_port = port; |
---|
1068 | } |
---|
1069 | |
---|
1070 | |
---|
1071 | tr_free( tmp ); |
---|
1072 | return err; |
---|
1073 | } |
---|
1074 | |
---|
1075 | #include <string.h> |
---|
1076 | #include <openssl/sha.h> |
---|
1077 | #include <openssl/hmac.h> |
---|
1078 | #include <openssl/evp.h> |
---|
1079 | #include <openssl/bio.h> |
---|
1080 | #include <openssl/buffer.h> |
---|
1081 | |
---|
1082 | char * |
---|
1083 | tr_base64_encode( const void * input, int length, int * setme_len ) |
---|
1084 | { |
---|
1085 | char * ret; |
---|
1086 | BIO * b64; |
---|
1087 | BIO * bmem; |
---|
1088 | BUF_MEM * bptr; |
---|
1089 | |
---|
1090 | if( length < 1 ) |
---|
1091 | length = strlen( input ); |
---|
1092 | |
---|
1093 | bmem = BIO_new( BIO_s_mem( ) ); |
---|
1094 | b64 = BIO_new( BIO_f_base64( ) ); |
---|
1095 | b64 = BIO_push( b64, bmem ); |
---|
1096 | BIO_write( b64, input, length ); |
---|
1097 | (void) BIO_flush( b64 ); |
---|
1098 | BIO_get_mem_ptr( b64, &bptr ); |
---|
1099 | ret = tr_strndup( bptr->data, bptr->length ); |
---|
1100 | if( setme_len ) |
---|
1101 | *setme_len = bptr->length; |
---|
1102 | |
---|
1103 | BIO_free_all( b64 ); |
---|
1104 | return ret; |
---|
1105 | } |
---|
1106 | |
---|
1107 | char * |
---|
1108 | tr_base64_decode( const void * input, int length, int * setme_len ) |
---|
1109 | { |
---|
1110 | char * ret; |
---|
1111 | BIO * b64; |
---|
1112 | BIO * bmem; |
---|
1113 | int retlen; |
---|
1114 | |
---|
1115 | if( length < 1 ) |
---|
1116 | length = strlen( input ); |
---|
1117 | |
---|
1118 | ret = tr_new0( char, length ); |
---|
1119 | b64 = BIO_new( BIO_f_base64( ) ); |
---|
1120 | bmem = BIO_new_mem_buf( (unsigned char*)input, length ); |
---|
1121 | bmem = BIO_push( b64, bmem ); |
---|
1122 | retlen = BIO_read( bmem, ret, length ); |
---|
1123 | if( !retlen ) |
---|
1124 | { |
---|
1125 | /* try again, but with the BIO_FLAGS_BASE64_NO_NL flag */ |
---|
1126 | BIO_free_all( bmem ); |
---|
1127 | b64 = BIO_new( BIO_f_base64( ) ); |
---|
1128 | BIO_set_flags( b64, BIO_FLAGS_BASE64_NO_NL ); |
---|
1129 | bmem = BIO_new_mem_buf( (unsigned char*)input, length ); |
---|
1130 | bmem = BIO_push( b64, bmem ); |
---|
1131 | retlen = BIO_read( bmem, ret, length ); |
---|
1132 | } |
---|
1133 | |
---|
1134 | if( setme_len ) |
---|
1135 | *setme_len = retlen; |
---|
1136 | |
---|
1137 | BIO_free_all( bmem ); |
---|
1138 | return ret; |
---|
1139 | } |
---|