source: trunk/libtransmission/utils.h @ 2348

Last change on this file since 2348 was 2348, checked in by charles, 15 years ago

fix crash on zero-byte torrents

  • Property svn:keywords set to Date Rev Author Id
File size: 7.0 KB
Line 
1/******************************************************************************
2 * $Id: utils.h 2348 2007-07-15 03:52:51Z charles $
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#include <stdarg.h>
29
30void tr_msgInit( void );
31
32#define tr_err( a... ) tr_msg( TR_MSG_ERR, ## a )
33#define tr_inf( a... ) tr_msg( TR_MSG_INF, ## a )
34#define tr_dbg( a... ) tr_msg( TR_MSG_DBG, ## a )
35void tr_msg  ( int level, char * msg, ... ) PRINTF( 2, 3 );
36
37int  tr_rand ( int );
38
39void * tr_memmem( const void *, size_t, const void *, size_t );
40
41/***********************************************************************
42 * tr_mkdir
43 ***********************************************************************
44 * Create a directory and any needed parent directories.
45 * Note that the string passed in must be writable!
46 **********************************************************************/
47int tr_mkdir( char * path );
48
49/***********************************************************************
50 * tr_strcasecmp
51 ***********************************************************************
52 * A case-insensitive strncmp()
53 **********************************************************************/
54#define tr_strcasecmp( ff, ss ) ( tr_strncasecmp( (ff), (ss), ULONG_MAX ) )
55int tr_strncasecmp( const char * first, const char * second, size_t len );
56
57/***********************************************************************
58 * tr_sprintf
59 ***********************************************************************
60 * Appends to the end of a buffer using printf formatting,
61 * growing the buffer if needed
62 **********************************************************************/
63int tr_sprintf( char ** buf, int * used, int * max,
64                const char * format, ... ) PRINTF( 4, 5 );
65/* gee, it sure would be nice if BeOS had va_copy() */
66int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list );
67/* this concatenates some binary data onto the end of a buffer */
68int tr_concat( char ** buf, int * used, int * max,
69               const char * data, int len );
70
71/* creates a filename from a series of elements using the
72   correct separator for filenames. */
73void tr_buildPath ( char* buf, size_t buflen,
74                    const char * first_element, ... );
75
76
77int    tr_ioErrorFromErrno( void );
78
79char * tr_errorString( int code );
80
81/* return the current date in milliseconds */
82uint64_t tr_date( void );
83
84/* wait the specified number of milliseconds */
85void tr_wait( uint64_t delay_milliseconds );
86
87#define tr_blockPiece(a) _tr_blockPiece(tor,a)
88static inline int _tr_blockPiece( const tr_torrent_t * tor, int block )
89{
90    const tr_info_t * inf = &tor->info;
91    return block / ( inf->pieceSize / tor->blockSize );
92}
93
94#define tr_blockSize(a) _tr_blockSize(tor,a)
95static inline int _tr_blockSize( const tr_torrent_t * tor, int block )
96{
97    const tr_info_t * inf = &tor->info;
98    int dummy;
99
100    if( block != tor->blockCount - 1 ||
101        !( dummy = inf->totalSize % tor->blockSize ) )
102    {
103        return tor->blockSize;
104    }
105
106    return dummy;
107}
108
109#define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a)
110static inline int _tr_blockPosInPiece( const tr_torrent_t * tor, int block )
111{
112    const tr_info_t * inf = &tor->info;
113    return tor->blockSize *
114        ( block % ( inf->pieceSize / tor->blockSize ) );
115}
116
117#define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a)
118static inline int _tr_pieceCountBlocks( const tr_torrent_t * tor, int piece )
119{
120    const tr_info_t * inf = &tor->info;
121    if( piece < inf->pieceCount - 1 ||
122        !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) )
123    {
124        return inf->pieceSize / tor->blockSize;
125    }
126    return tor->blockCount % ( inf->pieceSize / tor->blockSize );
127}
128
129#define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a)
130static inline int _tr_pieceStartBlock( const tr_torrent_t * tor, int piece )
131{
132    const tr_info_t * inf = &tor->info;
133    return piece * ( inf->pieceSize / tor->blockSize );
134}
135
136#define tr_pieceSize(a) _tr_pieceSize(tor,a)
137static inline int _tr_pieceSize( const tr_torrent_t * tor, int piece )
138{
139    const tr_info_t * inf = &tor->info;
140    if( piece < inf->pieceCount - 1 ||
141        !( inf->totalSize % inf->pieceSize ) )
142    {
143        return inf->pieceSize;
144    }
145    return inf->totalSize % inf->pieceSize;
146}
147
148#define tr_block(a,b) _tr_block(tor,a,b)
149static inline int _tr_block( const tr_torrent_t * tor, int index, int begin )
150{
151    const tr_info_t * inf = &tor->info;
152    return index * ( inf->pieceSize / tor->blockSize ) +
153        begin / tor->blockSize;
154}
155
156/***
157****
158***/
159
160#define tr_new(struct_type, n_structs)           \
161    ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
162#define tr_new0(struct_type, n_structs)          \
163    ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
164#define tr_renew(struct_type, mem, n_structs)    \
165    ((struct_type *) realloc ((mem), ((size_t) sizeof (struct_type)) * ((size_t) (n_structs))))
166
167void* tr_malloc  ( size_t );
168void* tr_malloc0 ( size_t );
169void* tr_calloc  ( size_t nmemb, size_t size );
170void  tr_free    ( void* );
171
172char* tr_strdup( const char * str );
173char* tr_strndup( const char * str, int len );
174
175/***
176****
177***/
178
179struct tr_bitfield_s
180{
181    uint8_t * bits;
182    size_t len;
183};
184
185tr_bitfield_t* tr_bitfieldNew( size_t bitcount );
186tr_bitfield_t* tr_bitfieldDup( const tr_bitfield_t* );
187void tr_bitfieldFree( tr_bitfield_t*);
188
189void tr_bitfieldClear( tr_bitfield_t* );
190void tr_bitfieldAdd( tr_bitfield_t*, size_t bit );
191void tr_bitfieldRem( tr_bitfield_t*, size_t bit );
192void tr_bitfieldAddRange( tr_bitfield_t *, size_t begin, size_t end );
193void tr_bitfieldRemRange ( tr_bitfield_t*, size_t begin, size_t end );
194
195int    tr_bitfieldIsEmpty( const tr_bitfield_t* );
196int    tr_bitfieldHas( const tr_bitfield_t *, size_t bit );
197size_t tr_bitfieldCountTrueBits( const tr_bitfield_t* );
198
199tr_bitfield_t* tr_bitfieldNegate( tr_bitfield_t* );
200tr_bitfield_t* tr_bitfieldAnd( tr_bitfield_t*, const tr_bitfield_t* );
201
202
203
204#endif
Note: See TracBrowser for help on using the repository browser.