blob: c4c3d4dd78074d4e61ced51d94974c0ccd55fb5c [file] [log] [blame]
Alistair Delvabeaee832021-02-24 11:27:23 -08001#ifndef MTOOLS_LLONG_H
2#define MTOOLS_LLONG_H
3
Yi Kong39bbd962022-01-09 19:41:38 +08004/* Copyright 1999,2001-2004,2007-2009,2021 Alain Knaff.
Alistair Delvabeaee832021-02-24 11:27:23 -08005 * This file is part of mtools.
6 *
7 * Mtools is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Mtools is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#if 1
22
23
Yi Kong39bbd962022-01-09 19:41:38 +080024#if SIZEOF_OFF_T >= 8
Alistair Delvabeaee832021-02-24 11:27:23 -080025/* if off_t is already 64 bits, be happy, and don't worry about the
26 * loff_t and llseek stuff */
27# define MT_OFF_T off_t
Yi Kong39bbd962022-01-09 19:41:38 +080028# define SIZEOF_MT_OFF_T SIZEOF_OFF_T
29#endif
30
31#ifndef MT_OFF_T
32# if defined(HAVE_LSEEK64) && defined (HAVE_OFF64_T)
33# define MT_OFF_T off64_t
34# define SIZEOF_MT_OFF_T 8
Alistair Delvabeaee832021-02-24 11:27:23 -080035# endif
36#endif
37
Yi Kong39bbd962022-01-09 19:41:38 +080038
Alistair Delvabeaee832021-02-24 11:27:23 -080039#ifndef MT_OFF_T
40# if defined(HAVE_LLSEEK) || defined(HAVE_LSEEK64)
41/* we have llseek. Now, what's its type called? loff_t or offset_t ? */
42# ifdef HAVE_LOFF_T
43# define MT_OFF_T loff_t
Yi Kong39bbd962022-01-09 19:41:38 +080044# define SIZEOF_MT_OFF_T 8
Alistair Delvabeaee832021-02-24 11:27:23 -080045/* use the same type for size. Better to get signedness wrong than width */
Alistair Delvabeaee832021-02-24 11:27:23 -080046# else
47# ifdef HAVE_OFFSET_T
48# define MT_OFF_T offset_t
Yi Kong39bbd962022-01-09 19:41:38 +080049# define SIZEOF_MT_OFF_T 8
Alistair Delvabeaee832021-02-24 11:27:23 -080050# endif
51# endif
52# endif
53#endif
54
55#ifndef MT_OFF_T
56/* we still don't have a suitable mt_off_t type...*/
57# ifdef HAVE_LONG_LONG
58/* ... first try long long ... */
59# define MT_OFF_T long long
Yi Kong39bbd962022-01-09 19:41:38 +080060# define SIZEOF_MT_OFF_T 8
Alistair Delvabeaee832021-02-24 11:27:23 -080061# else
Yi Kong39bbd962022-01-09 19:41:38 +080062/* ... and if that fails, fall back on good ole' off_t, even if that
63 * only has 32 bits */
64# define MT_OFF_T off_t
65# define SIZEOF_MT_OFF_T SIZEOF_OFF_T
Alistair Delvabeaee832021-02-24 11:27:23 -080066# endif
67#endif
68
69typedef MT_OFF_T mt_off_t;
Yi Kong39bbd962022-01-09 19:41:38 +080070
71/* Define a common supertype of uint32_t and mt_off_t. Usually,
72 * mt_off_t is bigger, except on 32-bit architectures without large
73 * file support, where uint32_t can represent larger values. N.B. in
74 * such a setup, negative values of mt_off_t could not be handled, but
75 * we don't use any such values anyways in mtools
76 */
77#if SIZEOF_MT_OFF_T == 4
78
79typedef uint32_t smt_off_t;
80mt_off_t to_mt_off_t(uint32_t off);
81
82#else
83
84typedef mt_off_t smt_off_t;
85#define to_mt_off_t(x) (x)
86
87#endif
Alistair Delvabeaee832021-02-24 11:27:23 -080088
89#else
90/* testing: meant to flag dubious assignments between 32 bit length types
91 * and 64 bit ones */
92typedef struct {
93 unsigned int lo;
94 int high;
95} *mt_off_t;
96
Alistair Delvabeaee832021-02-24 11:27:23 -080097#endif
98
99#define min(a,b) ((a) < (b) ? (a) : (b))
100#define MAX_OFF_T_B(bits) \
101 ((((mt_off_t) 1 << min(bits-1, sizeof(mt_off_t)*8 - 2)) -1) << 1 | 1)
102
103#if defined(HAVE_LLSEEK) || defined(HAVE_LSEEK64)
104# define SEEK_BITS 63
105#else
106# define SEEK_BITS (sizeof(off_t) * 8 - 1)
107#endif
108
109extern const mt_off_t max_off_t_31;
110extern const mt_off_t max_off_t_41;
111extern const mt_off_t max_off_t_seek;
112
Yi Kong39bbd962022-01-09 19:41:38 +0800113extern off_t truncBytes32(mt_off_t off); /* truncMtOffToOff */
114extern uint32_t truncMtOffTo32u(mt_off_t off);
115extern uint32_t truncSizeTo32u(size_t siz);
Alistair Delvabeaee832021-02-24 11:27:23 -0800116extern int fileTooBig(mt_off_t off);
117
118int mt_lseek(int fd, mt_off_t where, int whence);
119
Yi Kong39bbd962022-01-09 19:41:38 +0800120unsigned int log_2(unsigned int);
Alistair Delvabeaee832021-02-24 11:27:23 -0800121
122#endif