aboutsummaryrefslogtreecommitdiff
path: root/llong.h
blob: c4c3d4dd78074d4e61ced51d94974c0ccd55fb5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef MTOOLS_LLONG_H
#define MTOOLS_LLONG_H

/*  Copyright 1999,2001-2004,2007-2009,2021 Alain Knaff.
 *  This file is part of mtools.
 *
 *  Mtools is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Mtools is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
 */

#if 1


#if SIZEOF_OFF_T >= 8
/* if off_t is already 64 bits, be happy, and don't worry about the
 * loff_t and llseek stuff */
# define MT_OFF_T off_t
# define SIZEOF_MT_OFF_T SIZEOF_OFF_T
#endif

#ifndef MT_OFF_T
# if defined(HAVE_LSEEK64) && defined (HAVE_OFF64_T)
#  define MT_OFF_T off64_t
#  define SIZEOF_MT_OFF_T 8
# endif
#endif


#ifndef MT_OFF_T
# if defined(HAVE_LLSEEK) || defined(HAVE_LSEEK64)
/* we have llseek. Now, what's its type called? loff_t or offset_t ? */
#  ifdef HAVE_LOFF_T
#   define MT_OFF_T loff_t
#   define SIZEOF_MT_OFF_T 8
/* use the same type for size. Better to get signedness wrong than width */
#  else
#   ifdef HAVE_OFFSET_T
#    define MT_OFF_T offset_t
#    define SIZEOF_MT_OFF_T 8
#   endif
#  endif
# endif
#endif

#ifndef MT_OFF_T
/* we still don't have a suitable mt_off_t type...*/
# ifdef HAVE_LONG_LONG
/* ... first try long long ... */
#  define MT_OFF_T long long
#  define SIZEOF_MT_OFF_T 8
# else
/* ... and if that fails, fall back on good ole' off_t, even if that
 * only has 32 bits */
#  define MT_OFF_T off_t
#  define SIZEOF_MT_OFF_T SIZEOF_OFF_T
# endif
#endif

typedef MT_OFF_T mt_off_t;

/* Define a common supertype of uint32_t and mt_off_t. Usually,
 * mt_off_t is bigger, except on 32-bit architectures without large
 * file support, where uint32_t can represent larger values. N.B. in
 * such a setup, negative values of mt_off_t could not be handled, but
 * we don't use any such values anyways in mtools
 */
#if SIZEOF_MT_OFF_T == 4

typedef uint32_t smt_off_t;
mt_off_t to_mt_off_t(uint32_t off);

#else

typedef mt_off_t smt_off_t;
#define to_mt_off_t(x) (x)

#endif

#else
/* testing: meant to flag dubious assignments between 32 bit length types
 * and 64 bit ones */
typedef struct {
	unsigned int lo;
	int high;
} *mt_off_t;

#endif

#define min(a,b) ((a) < (b) ? (a) : (b))
#define MAX_OFF_T_B(bits) \
	((((mt_off_t) 1 << min(bits-1, sizeof(mt_off_t)*8 - 2)) -1) << 1 | 1)

#if defined(HAVE_LLSEEK) || defined(HAVE_LSEEK64)
# define SEEK_BITS 63
#else
# define SEEK_BITS (sizeof(off_t) * 8 - 1)
#endif

extern const mt_off_t max_off_t_31;
extern const mt_off_t max_off_t_41;
extern const mt_off_t max_off_t_seek;

extern off_t truncBytes32(mt_off_t off); /* truncMtOffToOff */
extern uint32_t truncMtOffTo32u(mt_off_t off);
extern uint32_t truncSizeTo32u(size_t siz);
extern int fileTooBig(mt_off_t off);

int mt_lseek(int fd, mt_off_t where, int whence);

unsigned int log_2(unsigned int);

#endif