1 | /****************************************************************************** |
---|
2 | * $Id: utils.h 1787 2007-04-23 20:16:16Z titer $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2007 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 | #ifndef TR_UTILS_H |
---|
26 | #define TR_UTILS_H 1 |
---|
27 | |
---|
28 | void tr_msgInit( void ); |
---|
29 | |
---|
30 | #define tr_err( a... ) tr_msg( TR_MSG_ERR, ## a ) |
---|
31 | #define tr_inf( a... ) tr_msg( TR_MSG_INF, ## a ) |
---|
32 | #define tr_dbg( a... ) tr_msg( TR_MSG_DBG, ## a ) |
---|
33 | void tr_msg ( int level, char * msg, ... ) PRINTF( 2, 3 ); |
---|
34 | |
---|
35 | int tr_rand ( int ); |
---|
36 | |
---|
37 | void * tr_memmem( const void *, size_t, const void *, size_t ); |
---|
38 | |
---|
39 | /*********************************************************************** |
---|
40 | * tr_mkdir |
---|
41 | *********************************************************************** |
---|
42 | * Create a directory and any needed parent directories. |
---|
43 | * Note that the string passed in must be writable! |
---|
44 | **********************************************************************/ |
---|
45 | int tr_mkdir( char * path ); |
---|
46 | |
---|
47 | /*********************************************************************** |
---|
48 | * tr_strcasecmp |
---|
49 | *********************************************************************** |
---|
50 | * A case-insensitive strncmp() |
---|
51 | **********************************************************************/ |
---|
52 | #define tr_strcasecmp( ff, ss ) ( tr_strncasecmp( (ff), (ss), -1 ) ) |
---|
53 | int tr_strncasecmp( const char * first, const char * second, int len ); |
---|
54 | |
---|
55 | /*********************************************************************** |
---|
56 | * tr_sprintf |
---|
57 | *********************************************************************** |
---|
58 | * Appends to the end of a buffer using printf formatting, |
---|
59 | * growing the buffer if needed |
---|
60 | **********************************************************************/ |
---|
61 | int tr_sprintf( char ** buf, int * used, int * max, |
---|
62 | const char * format, ... ) PRINTF( 4, 5 ); |
---|
63 | /* gee, it sure would be nice if BeOS had va_copy() */ |
---|
64 | int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list ); |
---|
65 | /* this concatenates some binary data onto the end of a buffer */ |
---|
66 | int tr_concat( char ** buf, int * used, int * max, |
---|
67 | const char * data, int len ); |
---|
68 | |
---|
69 | /*********************************************************************** |
---|
70 | * tr_dupstr |
---|
71 | *********************************************************************** |
---|
72 | * Creates a nul-terminated string |
---|
73 | **********************************************************************/ |
---|
74 | char * tr_dupstr( const char * base, int len ); |
---|
75 | |
---|
76 | int tr_ioErrorFromErrno( void ); |
---|
77 | |
---|
78 | char * tr_errorString( int code ); |
---|
79 | |
---|
80 | /*********************************************************************** |
---|
81 | * tr_date |
---|
82 | *********************************************************************** |
---|
83 | * Returns the current date in milliseconds |
---|
84 | **********************************************************************/ |
---|
85 | static inline uint64_t tr_date() |
---|
86 | { |
---|
87 | struct timeval tv; |
---|
88 | gettimeofday( &tv, NULL ); |
---|
89 | return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 ); |
---|
90 | } |
---|
91 | |
---|
92 | /*********************************************************************** |
---|
93 | * tr_wait |
---|
94 | *********************************************************************** |
---|
95 | * Wait 'delay' milliseconds |
---|
96 | **********************************************************************/ |
---|
97 | static inline void tr_wait( uint64_t delay ) |
---|
98 | { |
---|
99 | #ifdef SYS_BEOS |
---|
100 | snooze( 1000 * delay ); |
---|
101 | #else |
---|
102 | usleep( 1000 * delay ); |
---|
103 | #endif |
---|
104 | } |
---|
105 | |
---|
106 | struct tr_bitfield_s |
---|
107 | { |
---|
108 | uint8_t * bits; |
---|
109 | int len; |
---|
110 | }; |
---|
111 | |
---|
112 | /* note that the argument is how many bits are needed, not bytes */ |
---|
113 | static inline tr_bitfield_t * tr_bitfieldNew( int bitcount ) |
---|
114 | { |
---|
115 | tr_bitfield_t * ret; |
---|
116 | |
---|
117 | ret = calloc( 1, sizeof *ret ); |
---|
118 | if( NULL == ret ) |
---|
119 | { |
---|
120 | return NULL; |
---|
121 | } |
---|
122 | ret->len = ( bitcount + 7 ) / 8; |
---|
123 | ret->bits = calloc( ret->len, 1 ); |
---|
124 | if( NULL == ret->bits ) |
---|
125 | { |
---|
126 | free( ret ); |
---|
127 | return NULL; |
---|
128 | } |
---|
129 | |
---|
130 | return ret; |
---|
131 | } |
---|
132 | |
---|
133 | static inline void tr_bitfieldFree( tr_bitfield_t * bitfield ) |
---|
134 | { |
---|
135 | if( bitfield ) |
---|
136 | { |
---|
137 | free( bitfield->bits ); |
---|
138 | free( bitfield ); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | static inline void tr_bitfieldClear( tr_bitfield_t * bitfield ) |
---|
143 | { |
---|
144 | memset( bitfield->bits, 0, bitfield->len ); |
---|
145 | } |
---|
146 | |
---|
147 | /*********************************************************************** |
---|
148 | * tr_bitfieldHas |
---|
149 | **********************************************************************/ |
---|
150 | static inline int tr_bitfieldHas( tr_bitfield_t * bitfield, int piece ) |
---|
151 | { |
---|
152 | assert( piece / 8 < bitfield->len ); |
---|
153 | return ( bitfield->bits[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) ); |
---|
154 | } |
---|
155 | |
---|
156 | /*********************************************************************** |
---|
157 | * tr_bitfieldAdd |
---|
158 | **********************************************************************/ |
---|
159 | static inline void tr_bitfieldAdd( tr_bitfield_t * bitfield, int piece ) |
---|
160 | { |
---|
161 | assert( piece / 8 < bitfield->len ); |
---|
162 | bitfield->bits[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) ); |
---|
163 | } |
---|
164 | |
---|
165 | static inline void tr_bitfieldRem( tr_bitfield_t * bitfield, int piece ) |
---|
166 | { |
---|
167 | assert( piece / 8 < bitfield->len ); |
---|
168 | bitfield->bits[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) ); |
---|
169 | } |
---|
170 | |
---|
171 | #define tr_blockPiece(a) _tr_blockPiece(tor,a) |
---|
172 | static inline int _tr_blockPiece( tr_torrent_t * tor, int block ) |
---|
173 | { |
---|
174 | tr_info_t * inf = &tor->info; |
---|
175 | return block / ( inf->pieceSize / tor->blockSize ); |
---|
176 | } |
---|
177 | |
---|
178 | #define tr_blockSize(a) _tr_blockSize(tor,a) |
---|
179 | static inline int _tr_blockSize( tr_torrent_t * tor, int block ) |
---|
180 | { |
---|
181 | tr_info_t * inf = &tor->info; |
---|
182 | int dummy; |
---|
183 | |
---|
184 | if( block != tor->blockCount - 1 || |
---|
185 | !( dummy = inf->totalSize % tor->blockSize ) ) |
---|
186 | { |
---|
187 | return tor->blockSize; |
---|
188 | } |
---|
189 | |
---|
190 | return dummy; |
---|
191 | } |
---|
192 | |
---|
193 | #define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a) |
---|
194 | static inline int _tr_blockPosInPiece( tr_torrent_t * tor, int block ) |
---|
195 | { |
---|
196 | tr_info_t * inf = &tor->info; |
---|
197 | return tor->blockSize * |
---|
198 | ( block % ( inf->pieceSize / tor->blockSize ) ); |
---|
199 | } |
---|
200 | |
---|
201 | #define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a) |
---|
202 | static inline int _tr_pieceCountBlocks( tr_torrent_t * tor, int piece ) |
---|
203 | { |
---|
204 | tr_info_t * inf = &tor->info; |
---|
205 | if( piece < inf->pieceCount - 1 || |
---|
206 | !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) ) |
---|
207 | { |
---|
208 | return inf->pieceSize / tor->blockSize; |
---|
209 | } |
---|
210 | return tor->blockCount % ( inf->pieceSize / tor->blockSize ); |
---|
211 | } |
---|
212 | |
---|
213 | #define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a) |
---|
214 | static inline int _tr_pieceStartBlock( tr_torrent_t * tor, int piece ) |
---|
215 | { |
---|
216 | tr_info_t * inf = &tor->info; |
---|
217 | return piece * ( inf->pieceSize / tor->blockSize ); |
---|
218 | } |
---|
219 | |
---|
220 | #define tr_pieceSize(a) _tr_pieceSize(tor,a) |
---|
221 | static inline int _tr_pieceSize( tr_torrent_t * tor, int piece ) |
---|
222 | { |
---|
223 | tr_info_t * inf = &tor->info; |
---|
224 | if( piece < inf->pieceCount - 1 || |
---|
225 | !( inf->totalSize % inf->pieceSize ) ) |
---|
226 | { |
---|
227 | return inf->pieceSize; |
---|
228 | } |
---|
229 | return inf->totalSize % inf->pieceSize; |
---|
230 | } |
---|
231 | |
---|
232 | #define tr_block(a,b) _tr_block(tor,a,b) |
---|
233 | static inline int _tr_block( tr_torrent_t * tor, int index, int begin ) |
---|
234 | { |
---|
235 | tr_info_t * inf = &tor->info; |
---|
236 | return index * ( inf->pieceSize / tor->blockSize ) + |
---|
237 | begin / tor->blockSize; |
---|
238 | } |
---|
239 | |
---|
240 | #endif |
---|