source: trunk/libtransmission/utils.h @ 1534

Last change on this file since 1534 was 1534, checked in by joshe, 15 years ago

Do bounds checking on bitfields.

  • Property svn:keywords set to Date Rev Author Id
File size: 7.8 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 1534 2007-03-05 23:03:38Z joshe $
3 *
4 * Copyright (c) 2005-2006 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
28void 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 )
33void tr_msg  ( int level, char * msg, ... ) PRINTF( 2, 3 );
34
35int  tr_rand ( int );
36
37void * 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 **********************************************************************/
45int 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 ) )
53int 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 **********************************************************************/
61int 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() */
64int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list );
65/* this concatenates some binary data onto the end of a buffer */
66int 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 **********************************************************************/
74char * tr_dupstr( const char * base, int len );
75
76char * tr_errorString( int code );
77
78/***********************************************************************
79 * tr_date
80 ***********************************************************************
81 * Returns the current date in milliseconds
82 **********************************************************************/
83static inline uint64_t tr_date()
84{
85    struct timeval tv;
86    gettimeofday( &tv, NULL );
87    return (uint64_t) tv.tv_sec * 1000 + ( tv.tv_usec / 1000 );
88}
89
90/***********************************************************************
91 * tr_wait
92 ***********************************************************************
93 * Wait 'delay' milliseconds
94 **********************************************************************/
95static inline void tr_wait( uint64_t delay )
96{
97#ifdef SYS_BEOS
98    snooze( 1000 * delay );
99#else
100    usleep( 1000 * delay );
101#endif
102}
103
104struct tr_bitfield_s
105{
106    uint8_t * bits;
107    int       len;
108};
109
110/* note that the argument is how many bits are needed, not bytes */
111static inline tr_bitfield_t * tr_bitfieldNew( int bitcount )
112{
113    tr_bitfield_t * ret;
114
115    ret = calloc( 1, sizeof *ret );
116    if( NULL == ret )
117    {
118        return NULL;
119    }
120    ret->len = ( bitcount + 7 ) / 8;
121    ret->bits = calloc( ret->len, 1 );
122    if( NULL == ret->bits )
123    {
124        free( ret );
125        return NULL;
126    }
127
128    return ret;
129}
130
131static inline void tr_bitfieldFree( tr_bitfield_t * bitfield )
132{
133    if( bitfield )
134    {
135        free( bitfield->bits );
136        free( bitfield );
137    }
138}
139
140static inline void tr_bitfieldClear( tr_bitfield_t * bitfield )
141{
142    memset( bitfield->bits, 0, bitfield->len );
143}
144
145/***********************************************************************
146 * tr_bitfieldHas
147 **********************************************************************/
148static inline int tr_bitfieldHas( tr_bitfield_t * bitfield, int piece )
149{
150    assert( piece / 8 < bitfield->len );
151    return ( bitfield->bits[ piece / 8 ] & ( 1 << ( 7 - ( piece % 8 ) ) ) );
152}
153
154/***********************************************************************
155 * tr_bitfieldAdd
156 **********************************************************************/
157static inline void tr_bitfieldAdd( tr_bitfield_t * bitfield, int piece )
158{
159    assert( piece / 8 < bitfield->len );
160    bitfield->bits[ piece / 8 ] |= ( 1 << ( 7 - ( piece % 8 ) ) );
161}
162
163static inline void tr_bitfieldRem( tr_bitfield_t * bitfield, int piece )
164{
165    assert( piece / 8 < bitfield->len );
166    bitfield->bits[ piece / 8 ] &= ~( 1 << ( 7 - ( piece % 8 ) ) );
167}
168
169#define tr_blockPiece(a) _tr_blockPiece(tor,a)
170static inline int _tr_blockPiece( tr_torrent_t * tor, int block )
171{
172    tr_info_t * inf = &tor->info;
173    return block / ( inf->pieceSize / tor->blockSize );
174}
175
176#define tr_blockSize(a) _tr_blockSize(tor,a)
177static inline int _tr_blockSize( tr_torrent_t * tor, int block )
178{
179    tr_info_t * inf = &tor->info;
180    int dummy;
181
182    if( block != tor->blockCount - 1 ||
183        !( dummy = inf->totalSize % tor->blockSize ) )
184    {
185        return tor->blockSize;
186    }
187
188    return dummy;
189}
190
191#define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a)
192static inline int _tr_blockPosInPiece( tr_torrent_t * tor, int block )
193{
194    tr_info_t * inf = &tor->info;
195    return tor->blockSize *
196        ( block % ( inf->pieceSize / tor->blockSize ) );
197}
198
199#define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a)
200static inline int _tr_pieceCountBlocks( tr_torrent_t * tor, int piece )
201{
202    tr_info_t * inf = &tor->info;
203    if( piece < inf->pieceCount - 1 ||
204        !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) )
205    {
206        return inf->pieceSize / tor->blockSize;
207    }
208    return tor->blockCount % ( inf->pieceSize / tor->blockSize );
209}
210
211#define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a)
212static inline int _tr_pieceStartBlock( tr_torrent_t * tor, int piece )
213{
214    tr_info_t * inf = &tor->info;
215    return piece * ( inf->pieceSize / tor->blockSize );
216}
217
218#define tr_pieceSize(a) _tr_pieceSize(tor,a)
219static inline int _tr_pieceSize( tr_torrent_t * tor, int piece )
220{
221    tr_info_t * inf = &tor->info;
222    if( piece < inf->pieceCount - 1 ||
223        !( inf->totalSize % inf->pieceSize ) )
224    {
225        return inf->pieceSize;
226    }
227    return inf->totalSize % inf->pieceSize;
228}
229
230#define tr_block(a,b) _tr_block(tor,a,b)
231static inline int _tr_block( tr_torrent_t * tor, int index, int begin )
232{
233    tr_info_t * inf = &tor->info;
234    return index * ( inf->pieceSize / tor->blockSize ) +
235        begin / tor->blockSize;
236}
237
238#endif
Note: See TracBrowser for help on using the repository browser.